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