Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.12% covered (success)
96.12%
99 / 103
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_My_Jetpack
99.00% covered (success)
99.00%
99 / 100
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%
12 / 12
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            // Jetpack > Monetize leaves Calypso's Earn page once the Monetize page loads.
216            'earn'                     => 'monetize',
217            'forms-admin'              => 'forms',
218            'forms-responses-wp-admin' => 'forms',
219        );
220
221        return $aliases[ $key ] ?? $key;
222    }
223
224    /**
225     * Gets a menu title's text, without the count badges rendered into its spans.
226     *
227     * @param string $title The menu title's HTML.
228     * @return string
229     */
230    private static function get_tool_title( $title ) {
231        $text = trim( wp_strip_all_tags( preg_replace( '#<span\b[^>]*>.*?</span>#s', '', $title ) ) );
232
233        // A title may wrap its whole name in a span.
234        return html_entity_decode( '' !== $text ? $text : wp_strip_all_tags( $title ), ENT_QUOTES, 'UTF-8' );
235    }
236
237    /**
238     * Gets each known tool's section and description, keyed as get_tool_key() returns.
239     *
240     * The key also names the tool's icon in the page's script. A tool missing here shows under More, with the Jetpack mark.
241     *
242     * @return array[] Tools with `section` and `description` keys.
243     */
244    private static function get_known_tools() {
245        $tools = array(
246            'security' => array(
247                'activity-log'       => __( 'See every change made to this site', 'jetpack-mu-wpcom' ),
248                'akismet-key-config' => __( 'Filter spam from comments and forms', 'jetpack-mu-wpcom' ),
249                'backup'             => __( 'Restorable copies of your site', 'jetpack-mu-wpcom' ),
250                'protect'            => __( 'Protect your site from malware and attacks', 'jetpack-mu-wpcom' ),
251                'scan'               => __( 'Check your site for malware and threats', 'jetpack-mu-wpcom' ),
252            ),
253            'growth'   => array(
254                'advertising'       => __( 'Promote posts across WordPress.com and Tumblr', 'jetpack-mu-wpcom' ),
255                'marketing/traffic' => __( 'Google Analytics and site verification', 'jetpack-mu-wpcom' ),
256                'monetize'          => __( 'Earn from ads, donations and paid content', 'jetpack-mu-wpcom' ),
257                'newsletter'        => __( 'Send your posts to subscribers', 'jetpack-mu-wpcom' ),
258                'seo'               => __( 'Titles and descriptions for search engines', 'jetpack-mu-wpcom' ),
259                'social'            => __( 'Share posts to your social accounts', 'jetpack-mu-wpcom' ),
260            ),
261            'content'  => array(
262                'forms'      => __( 'Collect and review form responses', 'jetpack-mu-wpcom' ),
263                'podcast'    => __( 'Publish your posts as a podcast feed', 'jetpack-mu-wpcom' ),
264                'search'     => __( 'Fast, relevant search for your visitors', 'jetpack-mu-wpcom' ),
265                'videopress' => __( 'Ad-free video hosting', 'jetpack-mu-wpcom' ),
266            ),
267        );
268
269        $known = array();
270        foreach ( $tools as $section => $descriptions ) {
271            foreach ( $descriptions as $key => $description ) {
272                $known[ $key ] = array(
273                    'section'     => $section,
274                    'description' => $description,
275                );
276            }
277        }
278
279        return $known;
280    }
281
282    /**
283     * Tells code that links to My Jetpack's Features screen that this page doesn't have one.
284     *
285     * @param bool $available Whether modules can be managed from My Jetpack.
286     * @return bool
287     */
288    public static function filter_modules_management_available( $available ) {
289        return $available && ! static::owns_page();
290    }
291}
292
293add_action( 'init', array( WPCOM_My_Jetpack::class, 'init' ) );
294add_filter( 'jetpack_my_jetpack_modules_management_available', array( WPCOM_My_Jetpack::class, 'filter_modules_management_available' ) );