Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.08% |
98 / 102 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_My_Jetpack | |
98.99% |
98 / 99 |
|
90.91% |
10 / 11 |
32 | |
0.00% |
0 / 1 |
| should_register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_page_title | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_menu_title | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_initial_state | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| get_tools | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
8 | |||
| get_stats_tool | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
| get_tool_url | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| get_tool_key | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| get_tool_title | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_known_tools | |
100.00% |
31 / 31 |
|
100.00% |
1 / 1 |
3 | |||
| filter_modules_management_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- Feature entry file, named after the feature, also holds the WPCOM_My_Jetpack bootstrap class. |
| 2 | /** |
| 3 | * A lightweight My Jetpack page for WordPress.com Simple sites. |
| 4 | * |
| 5 | * It lists what the site's Jetpack menu already holds, and steps aside wherever the real My Jetpack |
| 6 | * registers. It is a stopgap: delete it once My Jetpack itself runs on Simple. |
| 7 | * |
| 8 | * @package automattic/jetpack-mu-wpcom |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 12 | |
| 13 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 14 | use Automattic\Jetpack\Status\Host; |
| 15 | |
| 16 | require_once __DIR__ . '/../../common/class-wpcom-wp-build-page.php'; |
| 17 | |
| 18 | /** |
| 19 | * Registers the page and its wp-build assets, unless the real My Jetpack claimed the slug. |
| 20 | */ |
| 21 | class WPCOM_My_Jetpack extends WPCOM_WP_Build_Page { |
| 22 | |
| 23 | /** |
| 24 | * Where the rest of Jetpack links My Jetpack, and first in the Jetpack menu's sort. |
| 25 | */ |
| 26 | const MENU_SLUG = 'my-jetpack'; |
| 27 | |
| 28 | /** |
| 29 | * As declared in routes/wpcom-my-jetpack/package.json. |
| 30 | */ |
| 31 | const WP_BUILD_PAGE = 'wpcom-my-jetpack'; |
| 32 | |
| 33 | /** |
| 34 | * Authors and up: Contributors can reach no Jetpack tool on Simple, so the page would only ever be empty for them. |
| 35 | */ |
| 36 | const CAPABILITY = 'publish_posts'; |
| 37 | |
| 38 | /** |
| 39 | * Global the page's boot script reads get_initial_state() from. |
| 40 | */ |
| 41 | const INITIAL_STATE_VAR = 'wpcomMyJetpackInitialState'; |
| 42 | |
| 43 | /** |
| 44 | * Records clicks, to see which tools people reach from here. |
| 45 | */ |
| 46 | const ENQUEUE_TRACKS = true; |
| 47 | |
| 48 | /** |
| 49 | * Whether this page should claim its slug: everywhere but P2s, which drop the Jetpack menu. |
| 50 | * |
| 51 | * @return bool |
| 52 | */ |
| 53 | public static function should_register() { |
| 54 | return ! ( new Host() )->is_p2_site(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * The page title. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public static function get_page_title() { |
| 63 | return __( 'My Jetpack', 'jetpack-mu-wpcom' ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * The menu entry's label. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public static function get_menu_title() { |
| 72 | return __( 'My Jetpack', 'jetpack-mu-wpcom' ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * The state the page's boot script reads. |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | public static function get_initial_state() { |
| 81 | $plan = null; |
| 82 | |
| 83 | // Agencies handle billing for the sites they fully manage, so their clients get no upsell. |
| 84 | if ( current_user_can( 'manage_options' ) && ! get_option( 'wpcom_is_staging_site' ) && ! is_fully_managed_agency_site() ) { |
| 85 | $plan = array( |
| 86 | 'name' => function_exists( 'wpcom_get_current_plan_name' ) ? wpcom_get_current_plan_name() : null, |
| 87 | 'url' => 'https://wordpress.com/plans/' . wpcom_get_site_slug(), |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return array( |
| 92 | 'tools' => static::get_tools(), |
| 93 | 'plan' => $plan, |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gets the tools to list: Stats, then the visible Jetpack menu items, in menu order. |
| 99 | * |
| 100 | * @return array[] Tools with `title`, `description`, `key`, `section` and `url` keys. |
| 101 | */ |
| 102 | public static function get_tools() { |
| 103 | global $submenu; |
| 104 | |
| 105 | // This runs at enqueue, before admin_head drops hidden items, so an entry stays off this page |
| 106 | // only if admin-ui hides it or it carries `hide-if-js`. |
| 107 | if ( method_exists( Admin_Menu::class, 'remove_hidden_menu_items' ) ) { |
| 108 | Admin_Menu::remove_hidden_menu_items(); |
| 109 | } |
| 110 | |
| 111 | $known = static::get_known_tools(); |
| 112 | $tools = array(); |
| 113 | |
| 114 | $stats = static::get_stats_tool(); |
| 115 | if ( $stats ) { |
| 116 | $tools[ $stats['url'] ] = $stats; |
| 117 | } |
| 118 | |
| 119 | foreach ( $submenu[ static::PARENT_SLUG ] ?? array() as $item ) { |
| 120 | if ( static::MENU_SLUG === $item[2] || str_contains( $item[4] ?? '', 'hide-if-js' ) ) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | // Slugs and titles are escaped for wp-admin's menu HTML, and React escapes them again. |
| 125 | $slug = html_entity_decode( $item[2], ENT_QUOTES, 'UTF-8' ); |
| 126 | |
| 127 | // Drops a link in any protocol WordPress doesn't allow, like `javascript:`. |
| 128 | $url = esc_url_raw( static::get_tool_url( $slug ) ); |
| 129 | if ( '' === $url || isset( $tools[ $url ] ) ) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | $key = static::get_tool_key( $slug ); |
| 134 | $tool = $known[ $key ] ?? null; |
| 135 | |
| 136 | $tools[ $url ] = array( |
| 137 | 'title' => static::get_tool_title( $item[0] ), |
| 138 | 'description' => $tool['description'] ?? '', |
| 139 | 'key' => $key, |
| 140 | 'section' => $tool['section'] ?? 'more', |
| 141 | 'url' => $url, |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return array_values( $tools ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Gets the Stats tool, which the menu walk misses because Stats is a top-level menu on Simple. |
| 150 | * |
| 151 | * @return array|null The tool, or null when the user can't view stats. |
| 152 | */ |
| 153 | private static function get_stats_tool() { |
| 154 | if ( ! current_user_can( 'view_stats' ) ) { |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /** This filter is documented in projects/packages/stats-admin/src/class-admin-bar.php */ |
| 159 | $url = esc_url_raw( apply_filters( 'jetpack_stats_url', admin_url( 'admin.php?page=stats' ), array( 'view' => 'dashboard' ) ) ); |
| 160 | if ( '' === $url ) { |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | return array( |
| 165 | 'title' => __( 'Stats', 'jetpack-mu-wpcom' ), |
| 166 | 'description' => __( 'See your visitors, views and top posts', 'jetpack-mu-wpcom' ), |
| 167 | 'key' => 'stats', |
| 168 | 'section' => 'growth', |
| 169 | 'url' => $url, |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets where a menu slug leads, the way wp-admin's sidebar links it. |
| 175 | * |
| 176 | * @param string $slug The menu slug. |
| 177 | * @return string |
| 178 | */ |
| 179 | private static function get_tool_url( $slug ) { |
| 180 | if ( wp_parse_url( $slug, PHP_URL_HOST ) ) { |
| 181 | return $slug; |
| 182 | } |
| 183 | |
| 184 | // Only a slug with a page callback goes through admin.php; any other is a path under wp-admin. |
| 185 | if ( get_plugin_page_hook( $slug, static::PARENT_SLUG ) ) { |
| 186 | return add_query_arg( 'page', $slug, admin_url( 'admin.php' ) ); |
| 187 | } |
| 188 | |
| 189 | return admin_url( $slug ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Gets the key a menu slug is known by: the path of a WordPress.com link to this site, or the slug without `jetpack-`. |
| 194 | * |
| 195 | * @param string $slug The menu slug. |
| 196 | * @return string The key, or '' for any other link that leaves wp-admin, so no address reaches Tracks. |
| 197 | */ |
| 198 | private static function get_tool_key( $slug ) { |
| 199 | $domain = wp_parse_url( home_url(), PHP_URL_HOST ); |
| 200 | |
| 201 | if ( preg_match( '#^https://wordpress\.com/(.+)/' . preg_quote( $domain, '#' ) . '/?$#', $slug, $matches ) ) { |
| 202 | $key = $matches[1]; |
| 203 | } elseif ( wp_parse_url( $slug, PHP_URL_HOST ) ) { |
| 204 | return ''; |
| 205 | } else { |
| 206 | $key = preg_replace( '/^jetpack-/', '', $slug ); |
| 207 | } |
| 208 | |
| 209 | $aliases = array( |
| 210 | 'forms-admin' => 'forms', |
| 211 | 'forms-responses-wp-admin' => 'forms', |
| 212 | ); |
| 213 | |
| 214 | return $aliases[ $key ] ?? $key; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets a menu title's text, without the count badges rendered into its spans. |
| 219 | * |
| 220 | * @param string $title The menu title's HTML. |
| 221 | * @return string |
| 222 | */ |
| 223 | private static function get_tool_title( $title ) { |
| 224 | $text = trim( wp_strip_all_tags( preg_replace( '#<span\b[^>]*>.*?</span>#s', '', $title ) ) ); |
| 225 | |
| 226 | // A title may wrap its whole name in a span. |
| 227 | return html_entity_decode( '' !== $text ? $text : wp_strip_all_tags( $title ), ENT_QUOTES, 'UTF-8' ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets each known tool's section and description, keyed as get_tool_key() returns. |
| 232 | * |
| 233 | * The key also names the tool's icon in the page's script. A tool missing here shows under More, with the Jetpack mark. |
| 234 | * |
| 235 | * @return array[] Tools with `section` and `description` keys. |
| 236 | */ |
| 237 | private static function get_known_tools() { |
| 238 | $tools = array( |
| 239 | 'security' => array( |
| 240 | 'activity-log' => __( 'See every change made to this site', 'jetpack-mu-wpcom' ), |
| 241 | 'akismet-key-config' => __( 'Filter spam from comments and forms', 'jetpack-mu-wpcom' ), |
| 242 | 'backup' => __( 'Restorable copies of your site', 'jetpack-mu-wpcom' ), |
| 243 | 'protect' => __( 'Protect your site from malware and attacks', 'jetpack-mu-wpcom' ), |
| 244 | 'scan' => __( 'Check your site for malware and threats', 'jetpack-mu-wpcom' ), |
| 245 | ), |
| 246 | 'growth' => array( |
| 247 | 'advertising' => __( 'Promote posts across WordPress.com and Tumblr', 'jetpack-mu-wpcom' ), |
| 248 | 'earn' => __( 'Earn from ads, donations and paid content', 'jetpack-mu-wpcom' ), |
| 249 | 'marketing/traffic' => __( 'Google Analytics and site verification', 'jetpack-mu-wpcom' ), |
| 250 | 'newsletter' => __( 'Send your posts to subscribers', 'jetpack-mu-wpcom' ), |
| 251 | 'seo' => __( 'Titles and descriptions for search engines', 'jetpack-mu-wpcom' ), |
| 252 | 'social' => __( 'Share posts to your social accounts', 'jetpack-mu-wpcom' ), |
| 253 | ), |
| 254 | 'content' => array( |
| 255 | 'forms' => __( 'Collect and review form responses', 'jetpack-mu-wpcom' ), |
| 256 | 'podcast' => __( 'Publish your posts as a podcast feed', 'jetpack-mu-wpcom' ), |
| 257 | 'search' => __( 'Fast, relevant search for your visitors', 'jetpack-mu-wpcom' ), |
| 258 | 'videopress' => __( 'Ad-free video hosting', 'jetpack-mu-wpcom' ), |
| 259 | ), |
| 260 | ); |
| 261 | |
| 262 | $known = array(); |
| 263 | foreach ( $tools as $section => $descriptions ) { |
| 264 | foreach ( $descriptions as $key => $description ) { |
| 265 | $known[ $key ] = array( |
| 266 | 'section' => $section, |
| 267 | 'description' => $description, |
| 268 | ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $known; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Tells code that links to My Jetpack's Features screen that this page doesn't have one. |
| 277 | * |
| 278 | * @param bool $available Whether modules can be managed from My Jetpack. |
| 279 | * @return bool |
| 280 | */ |
| 281 | public static function filter_modules_management_available( $available ) { |
| 282 | return $available && ! static::owns_page(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | add_action( 'init', array( WPCOM_My_Jetpack::class, 'init' ) ); |
| 287 | add_filter( 'jetpack_my_jetpack_modules_management_available', array( WPCOM_My_Jetpack::class, 'filter_modules_management_available' ) ); |