Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.40% covered (warning)
79.40%
158 / 199
42.11% covered (danger)
42.11%
8 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Menu
79.40% covered (warning)
79.40%
158 / 199
42.11% covered (danger)
42.11%
8 / 19
124.19
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 handle_akismet_menu
11.11% covered (danger)
11.11%
1 / 9
0.00% covered (danger)
0.00%
0 / 1
4.81
 admin_menu_hook_callback
93.62% covered (success)
93.62%
44 / 47
0.00% covered (danger)
0.00%
0 / 1
11.03
 add_menu
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 hide_core_admin_notices
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 print_hide_core_admin_notices_style
n/a
0 / 0
n/a
0 / 0
1
 get_hide_core_admin_notices_styles
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 remove_menu
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 get_top_level_menu_item_slug
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 get_top_level_menu_item_url
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 should_show_upgrade_menu
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
7.02
 is_site_and_user_connected
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
8.32
 set_connection_manager
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_free_plan
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
9
 maybe_add_upgrade_menu_item
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
8
 add_upgrade_menu_item_styles
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 enqueue_design_tokens
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_design_tokens_style
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 maybe_enqueue_design_tokens
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 enqueue_upgrade_menu_tracks_script
96.30% covered (success)
96.30%
26 / 27
0.00% covered (danger)
0.00%
0 / 1
5
1<?php
2/**
3 * Admin Menu Registration
4 *
5 * @package automattic/jetpack-admin-ui
6 */
7
8namespace Automattic\Jetpack\Admin_UI;
9
10use Automattic\Jetpack\Tracking;
11use Jetpack_Options;
12use Jetpack_Tracks_Client;
13
14/**
15 * This class offers a wrapper to add_submenu_page and makes sure stand-alone plugin's menu items are always added under the Jetpack top level menu.
16 * If the Jetpack top level was not previously registered by other plugin, it will be registered here.
17 */
18class Admin_Menu {
19
20    const PACKAGE_VERSION = '0.11.4';
21
22    /**
23     * Slug used for the upgrade menu item and redirect URL.
24     *
25     * Keep the slug in sync with `$upgrade-menu-slug` at admin-ui-upgrade-menu.scss
26     *
27     * @var string
28     */
29    const UPGRADE_MENU_SLUG = 'jetpack-wpadmin-sidebar-free-plan-upsell-menu-item';
30
31    /**
32     * Fallback upgrade URL when the Redirect class is unavailable.
33     *
34     * @var string
35     */
36    const UPGRADE_MENU_FALLBACK_URL = 'https://jetpack.com/upgrade/';
37
38    /**
39     * Handle for the shared, token-only WPDS design-tokens stylesheet.
40     *
41     * Registered once and enqueued on every Jetpack admin page so that
42     * `var(--wpds-*)` values resolve at runtime instead of falling back to
43     * their hand-written hex defaults.
44     *
45     * @var string
46     */
47    const DESIGN_TOKENS_HANDLE = 'jetpack-admin-ui-design-tokens';
48
49    /**
50     * Handle for the source-less stylesheet that hides WordPress core admin notices.
51     *
52     * @var string
53     */
54    const HIDE_CORE_NOTICES_HANDLE = 'jetpack-admin-ui-hide-core-notices';
55
56    /**
57     * Whether this class has been initialized
58     *
59     * @var boolean
60     */
61    private static $initialized = false;
62
63    /**
64     * List of menu items enqueued to be added
65     *
66     * @var array
67     */
68    private static $menu_items = array();
69
70    /**
71     * Hook suffixes of the pages registered through this class.
72     *
73     * Used to scope the design-tokens stylesheet to Jetpack admin pages.
74     *
75     * @var array
76     */
77    private static $page_hooks = array();
78
79    /**
80     * Optional connection manager dependency.
81     *
82     * @var object|null
83     */
84    private static $connection_manager = null;
85
86    /**
87     * Initialize the class and set up the main hook
88     *
89     * @return void
90     */
91    public static function init() {
92        if ( ! self::$initialized ) {
93            self::$initialized = true;
94            self::handle_akismet_menu();
95            add_action( 'admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998.
96            add_action( 'network_admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998.
97            add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_upgrade_menu_item_styles' ) );
98            add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_design_tokens' ) );
99        }
100    }
101
102    /**
103     * Handles the Akismet menu item when used alongside other stand-alone plugins
104     *
105     * When Jetpack plugin is present, Akismet menu item is moved under the Jetpack top level menu, but if Akismet is active alongside other stand-alone plugins,
106     * we use this method to move the menu item.
107     */
108    private static function handle_akismet_menu() {
109        if ( class_exists( 'Akismet_Admin' ) ) {
110            add_action(
111                'admin_menu',
112                function () {
113                    // Prevent Akismet from adding a menu item.
114                    remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );
115
116                    // Add an Anti-spam menu item for Jetpack.
117                    self::add_menu( __( 'Akismet Anti-spam', 'jetpack-admin-ui' ), __( 'Akismet Anti-spam', 'jetpack-admin-ui' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
118                },
119                4
120            );
121
122        }
123    }
124
125    /**
126     * Callback to the admin_menu and network_admin_menu hooks that will register the enqueued menu items
127     *
128     * @return void
129     */
130    public static function admin_menu_hook_callback() {
131        $can_see_toplevel_menu  = true;
132        $jetpack_plugin_present = class_exists( 'Jetpack_React_Page' );
133        $icon                   = 'dashicons-admin-plugins';
134        if ( method_exists( '\Automattic\Jetpack\Assets\Logo', 'get_base64_admin_menu_logo' ) ) {
135            $icon = ( new \Automattic\Jetpack\Assets\Logo() )->get_base64_admin_menu_logo();
136        } elseif ( method_exists( '\Automattic\Jetpack\Assets\Logo', 'get_base64_logo' ) ) {
137            $icon = ( new \Automattic\Jetpack\Assets\Logo() )->get_base64_logo();
138        }
139
140        if ( ! $jetpack_plugin_present ) {
141            add_menu_page(
142                'Jetpack',
143                'Jetpack',
144                'edit_posts',
145                'jetpack',
146                '__return_null',
147                $icon,
148                3
149            );
150
151            // If Jetpack plugin is not present, user will only be able to see this menu if they have enough capability to at least one of the sub menus being added.
152            $can_see_toplevel_menu = false;
153        }
154
155        /**
156         * The add_sub_menu function has a bug and will not keep the right order of menu items.
157         *
158         * @see https://core.trac.wordpress.org/ticket/52035
159         * Let's order the items before registering them.
160         * Since this all happens after the Jetpack plugin menu items were added, all items will be added after Jetpack plugin items - unless position is very low number (smaller than the number of menu items present in Jetpack plugin).
161         */
162        usort(
163            self::$menu_items,
164            function ( $a, $b ) {
165                $position_a = empty( $a['position'] ) ? 0 : $a['position'];
166                $position_b = empty( $b['position'] ) ? 0 : $b['position'];
167                $result     = $position_a <=> $position_b;
168
169                if ( 0 === $result ) {
170                    // Case-insensitive and number-aware, so "eCommerce" sorts with the Es.
171                    // Still a byte compare: a leading accented character sorts after Z.
172                    $result = strnatcasecmp( $a['menu_title'], $b['menu_title'] );
173                }
174
175                return $result;
176            }
177        );
178
179        foreach ( self::$menu_items as $menu_item ) {
180            if ( ! current_user_can( $menu_item['capability'] ) ) {
181                continue;
182            }
183
184            $can_see_toplevel_menu = true;
185
186            add_submenu_page(
187                'jetpack',
188                $menu_item['page_title'],
189                $menu_item['menu_title'],
190                $menu_item['capability'],
191                $menu_item['menu_slug'],
192                $menu_item['function'],
193                $menu_item['position']
194            );
195        }
196
197        if ( ! $jetpack_plugin_present ) {
198            remove_submenu_page( 'jetpack', 'jetpack' );
199        }
200
201        if ( ! $can_see_toplevel_menu ) {
202            remove_menu_page( 'jetpack' );
203        }
204
205        self::maybe_add_upgrade_menu_item();
206    }
207
208    /**
209     * Adds a new submenu to the Jetpack Top level menu
210     *
211     * The parameters this method accepts are the same as @see add_submenu_page. This class will
212     * aggreagate all menu items registered by stand-alone plugins and make sure they all go under the same
213     * Jetpack top level menu. It will also handle the top level menu registration in case the Jetpack plugin is not present.
214     *
215     * @param string        $page_title  The text to be displayed in the title tags of the page when the menu
216     *                                   is selected.
217     * @param string        $menu_title  The text to be used for the menu.
218     * @param string        $capability  The capability required for this menu to be displayed to the user.
219     * @param string        $menu_slug   The slug name to refer to this menu by. Should be unique for this menu
220     *                                   and only include lowercase alphanumeric, dashes, and underscores characters
221     *                                   to be compatible with sanitize_key().
222     * @param callable|null $function    The function to be called to output the content for this page.
223     * @param int           $position    The position in the menu order this item should appear. Leave empty typically.
224     *
225     * @return string The resulting page's hook_suffix
226     */
227    public static function add_menu( $page_title, $menu_title, $capability, $menu_slug, $function, $position = null ) {
228        self::init();
229        self::$menu_items[] = compact( 'page_title', 'menu_title', 'capability', 'menu_slug', 'function', 'position' );
230
231        /**
232         * Let's return the page hook so consumers can use.
233         * We know all pages will be under Jetpack top level menu page, so we can hardcode the first part of the string.
234         * Using get_plugin_page_hookname here won't work because the top level page is not registered yet.
235         */
236        $hook = 'jetpack_page_' . $menu_slug;
237
238        // Track the page hook so the design-tokens stylesheet can be scoped to it.
239        self::$page_hooks[] = $hook;
240
241        // Hide WordPress core admin notices on this Jetpack page. The load-<hook>
242        // action only fires when the matching screen is being rendered, so this
243        // stays scoped to Jetpack pages and reaches every page registered here.
244        add_action( 'load-' . $hook, array( __CLASS__, 'hide_core_admin_notices' ) );
245        add_action( 'load-' . $hook . '-network', array( __CLASS__, 'hide_core_admin_notices' ) );
246
247        return $hook;
248    }
249
250    /**
251     * Enqueues the stylesheet that hides WordPress core admin notices on the current Jetpack page.
252     *
253     * Hooked from the page's load-<hook> action so it only runs on Jetpack screens. That action
254     * runs before admin_enqueue_scripts, so the handle is queued in time to be printed.
255     *
256     * @return void
257     */
258    public static function hide_core_admin_notices() {
259        // wp_add_inline_style() appends, so the CSS is attached only while the handle is new to the request.
260        if ( ! wp_style_is( self::HIDE_CORE_NOTICES_HANDLE, 'registered' ) ) {
261            wp_register_style( self::HIDE_CORE_NOTICES_HANDLE, false, array(), self::PACKAGE_VERSION );
262            wp_add_inline_style( self::HIDE_CORE_NOTICES_HANDLE, self::get_hide_core_admin_notices_styles() );
263        }
264
265        wp_enqueue_style( self::HIDE_CORE_NOTICES_HANDLE );
266    }
267
268    /**
269     * Enqueues the CSS that hides WordPress core admin notices.
270     *
271     * Callers must run this before WordPress flushes the style queue in
272     * print_admin_styles() (admin_print_styles, priority 20). Later than that,
273     * the handle is never printed. The previous admin_print_styles priority-10
274     * hook still works.
275     *
276     * @deprecated 0.10.0 Use hide_core_admin_notices(), which enqueues the CSS.
277     *
278     * @return void
279     */
280    public static function print_hide_core_admin_notices_style() {
281        _deprecated_function( __METHOD__, 'admin-ui-0.10.0', __CLASS__ . '::hide_core_admin_notices' );
282        self::hide_core_admin_notices();
283    }
284
285    /**
286     * Gets the CSS that hides WordPress core admin notices.
287     *
288     * We only target direct children of #wpbody-content (where core renders notices via the
289     * admin_notices / all_admin_notices hooks). This intentionally leaves JITMs untouched â€”
290     * they output `.jetpack-jitm-message`, not `.notice` â€” and leaves in-app/React notices
291     * untouched, since those render deeper inside `.wrap`. The CSS rides on a source-less
292     * handle rather than a build asset so it also reaches older Jetpack pages that ship no
293     * stylesheet of their own.
294     *
295     * @return string CSS rules.
296     */
297    private static function get_hide_core_admin_notices_styles() {
298        return '
299        #wpbody-content > .notice,
300        #wpbody-content > .update-nag,
301        #wpbody-content > .updated,
302        #wpbody-content > .error { display: none !important; }
303        ';
304    }
305
306    /**
307     * Removes an already added submenu
308     *
309     * @param string $menu_slug   The slug of the submenu to remove.
310     *
311     * @return array|false The removed submenu on success, false if not found.
312     */
313    public static function remove_menu( $menu_slug ) {
314
315        foreach ( self::$menu_items as $index => $menu_item ) {
316            if ( $menu_item['menu_slug'] === $menu_slug ) {
317                unset( self::$menu_items[ $index ] );
318
319                return $menu_item;
320            }
321        }
322
323        return false;
324    }
325
326    /**
327     * Gets the slug for the first item under the Jetpack top level menu
328     *
329     * @return string|null
330     */
331    public static function get_top_level_menu_item_slug() {
332        global $submenu;
333        if ( ! empty( $submenu['jetpack'] ) ) {
334            $item = reset( $submenu['jetpack'] );
335            if ( isset( $item[2] ) ) {
336                return $item[2];
337            }
338        }
339    }
340
341    /**
342     * Gets the URL for the first item under the Jetpack top level menu
343     *
344     * @param string $fallback If Jetpack menu is not there or no children is found, return this fallback instead. Default to admin_url().
345     * @return string
346     */
347    public static function get_top_level_menu_item_url( $fallback = false ) {
348        $slug = self::get_top_level_menu_item_slug();
349
350        if ( $slug ) {
351            $url = menu_page_url( $slug, false );
352            return $url;
353        }
354
355        $url = $fallback ? $fallback : admin_url();
356        return $url;
357    }
358
359    /**
360     * Checks whether the current site should show the upgrade menu item.
361     *
362     * The upgrade menu is only shown to administrators on free-plan sites
363     * that are not hosted on WordPress.com.
364     *
365     * @return bool True if the upgrade menu should be shown.
366     */
367    private static function should_show_upgrade_menu() {
368
369        // Only show to administrators.
370        if ( ! current_user_can( 'manage_options' ) ) {
371            return false;
372        }
373
374        // Don't show upsells on WordPress.com platform.
375        if ( class_exists( '\Automattic\Jetpack\Status\Host' ) ) {
376            $host = new \Automattic\Jetpack\Status\Host();
377            if ( $host->is_wpcom_platform() ) {
378                return false;
379            }
380        }
381
382        // Don't show upsells in offline/development mode.
383        if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
384            $status = new \Automattic\Jetpack\Status();
385            if ( $status->is_offline_mode() ) {
386                return false;
387            }
388        }
389
390        // Only show after the site and current user are connected.
391        if ( ! self::is_site_and_user_connected() ) {
392            return false;
393        }
394
395        // Only show to free-plan sites.
396        return self::is_free_plan();
397    }
398
399    /**
400     * Checks whether the site and current user are connected to WordPress.com.
401     *
402     * @return bool True if site and current user are connected.
403     */
404    private static function is_site_and_user_connected() {
405        $connection_manager = self::$connection_manager;
406        if ( ! $connection_manager && class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
407            $connection_manager       = new \Automattic\Jetpack\Connection\Manager();
408            self::$connection_manager = $connection_manager;
409        }
410
411        if (
412            $connection_manager
413            && is_callable( array( $connection_manager, 'is_connected' ) )
414            && is_callable( array( $connection_manager, 'is_user_connected' ) )
415        ) {
416            return (bool) $connection_manager->is_connected()
417                && (bool) $connection_manager->is_user_connected( get_current_user_id() );
418        }
419
420        return false;
421    }
422
423    /**
424     * Sets the connection manager dependency; used by tests.
425     *
426     * @param object|null $connection_manager Connection manager object.
427     * @return void
428     */
429    public static function set_connection_manager( $connection_manager ) {
430        self::$connection_manager = $connection_manager;
431    }
432
433    /**
434     * Checks whether the current site is on a free Jetpack plan with no active paid license.
435     *
436     * @return bool True if the site has no paid plan.
437     */
438    private static function is_free_plan() {
439        // Check the active plan - use the is_free field or product_slug.
440        $plan = get_option( 'jetpack_active_plan', array() );
441
442        // Back-compat: older plan payloads use class to indicate paid plans.
443        if ( isset( $plan['class'] ) && 'free' !== $plan['class'] ) {
444            return false;
445        }
446
447        // If the plan explicitly says it's not free, trust that.
448        if ( isset( $plan['is_free'] ) && false === $plan['is_free'] ) {
449            return false;
450        }
451
452        // Check if the product slug indicates a paid plan.
453        if ( isset( $plan['product_slug'] ) && 'jetpack_free' !== $plan['product_slug'] ) {
454            return false;
455        }
456
457        // Also check for site products (licenses can add products without changing plan).
458        $products = get_option( 'jetpack_site_products', array() );
459        if ( ! empty( $products ) && is_array( $products ) ) {
460            return false;
461        }
462
463        return true;
464    }
465
466    /**
467     * Conditionally adds an "Upgrade Jetpack" submenu item for free-plan sites.
468     *
469     * Only shown to users with manage_options capability on self-hosted sites without a paid Jetpack plan or license.
470     *
471     * @return void
472     */
473    private static function maybe_add_upgrade_menu_item() {
474        if ( ! self::should_show_upgrade_menu() ) {
475            return;
476        }
477
478        $upgrade_url = class_exists( '\Automattic\Jetpack\Redirect' )
479            ? \Automattic\Jetpack\Redirect::get_url( self::UPGRADE_MENU_SLUG )
480            : self::UPGRADE_MENU_FALLBACK_URL;
481
482        $menu_title = esc_html__( 'Upgrade Jetpack', 'jetpack-admin-ui' );
483
484        add_submenu_page(
485            'jetpack',
486            $menu_title,
487            $menu_title,
488            'manage_options',
489            esc_url( $upgrade_url ),
490            null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539.
491            999
492        );
493
494        // Add a CSS class to the <li> element so styles can target it precisely.
495        global $submenu;
496        if ( ! empty( $submenu['jetpack'] ) ) {
497            foreach ( $submenu['jetpack'] as $index => $item ) {
498                if ( isset( $item[2] ) && false !== strpos( $item[2], self::UPGRADE_MENU_SLUG ) ) {
499                    // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
500                    $submenu['jetpack'][ $index ][4] = ( ! empty( $item[4] ) ? $item[4] . ' ' : '' ) . self::UPGRADE_MENU_SLUG;
501                    break;
502                }
503            }
504        }
505    }
506
507    /**
508     * Enqueues admin styles for the "Upgrade Jetpack" menu item.
509     *
510     * The sidebar menu is visible on every admin page, so styles load globally.
511     * Only enqueues for free-plan sites on self-hosted installs.
512     *
513     * @return void
514     */
515    public static function add_upgrade_menu_item_styles() {
516        if ( ! self::should_show_upgrade_menu() ) {
517            return;
518        }
519
520        $asset_file = dirname( __DIR__ ) . '/build/admin-ui-upgrade-menu.asset.php';
521        $asset      = file_exists( $asset_file ) ? require $asset_file : array();
522
523        wp_enqueue_style(
524            'jetpack-admin-ui-upgrade-menu',
525            plugins_url( '../build/admin-ui-upgrade-menu.css', __FILE__ ),
526            $asset['dependencies'] ?? array(),
527            $asset['version'] ?? self::PACKAGE_VERSION
528        );
529
530        self::enqueue_upgrade_menu_tracks_script( $asset );
531    }
532
533    /**
534     * Enqueues the shared, token-only WPDS design-tokens stylesheet.
535     *
536     * Single entry point for any consumer that needs WPDS `var(--wpds-*)` values
537     * to resolve at runtime on a Jetpack admin page. Registers the handle on
538     * first use (idempotent) and enqueues it; the caller is responsible for
539     * scoping the call to the right page(s). Since admin-ui is a dependency of
540     * the Jetpack plugin and the modernized packages, both the plugin's
541     * legacy/wrap_ui gate and this package's own dashboards call through here,
542     * so the handle has a single owner and there is no duplicated enqueue logic.
543     *
544     * @return void
545     */
546    public static function enqueue_design_tokens() {
547        self::register_design_tokens_style();
548        wp_enqueue_style( self::DESIGN_TOKENS_HANDLE );
549    }
550
551    /**
552     * Registers the shared, token-only WPDS design-tokens stylesheet.
553     *
554     * The stylesheet only defines `:root{--wpds-*}` custom properties (no
555     * component or class styles), giving every Jetpack admin page a single
556     * runtime source for design tokens. It is safe to call repeatedly:
557     * wp_register_style() is a no-op once the handle is registered.
558     *
559     * @return void
560     */
561    private static function register_design_tokens_style() {
562        if ( wp_style_is( self::DESIGN_TOKENS_HANDLE, 'registered' ) ) {
563            return;
564        }
565
566        $asset_file = dirname( __DIR__ ) . '/build/design-tokens.asset.php';
567        $asset      = file_exists( $asset_file ) ? require $asset_file : array();
568
569        wp_register_style(
570            self::DESIGN_TOKENS_HANDLE,
571            plugins_url( '../build/design-tokens.css', __FILE__ ),
572            $asset['dependencies'] ?? array(),
573            $asset['version'] ?? self::PACKAGE_VERSION
574        );
575    }
576
577    /**
578     * Enqueues the design tokens on the pages registered through this class.
579     *
580     * This is the admin_enqueue_scripts callback for the modernized Jetpack
581     * dashboards. Scoped to self::$page_hooks so the tokens load wherever a
582     * modernized dashboard renders, regardless of plan or connection state; the
583     * actual enqueue is delegated to the reusable enqueue_design_tokens() API.
584     *
585     * @param string $hook_suffix The current admin page's hook suffix.
586     * @return void
587     */
588    public static function maybe_enqueue_design_tokens( $hook_suffix ) {
589        if ( ! in_array( $hook_suffix, self::$page_hooks, true ) ) {
590            return;
591        }
592
593        self::enqueue_design_tokens();
594    }
595
596    /**
597     * Enqueues Tracks for the upgrade submenu item.
598     *
599     * @param array $asset Parsed contents of admin-ui-upgrade-menu.asset.php.
600     * @return void
601     */
602    private static function enqueue_upgrade_menu_tracks_script( $asset ) {
603        if ( ! class_exists( '\Automattic\Jetpack\Tracking' ) ) {
604            return;
605        }
606
607        Tracking::register_tracks_functions_scripts( true );
608
609        wp_enqueue_script(
610            'jetpack-admin-ui-upgrade-menu-tracking',
611            plugins_url( '../build/admin-ui-upgrade-menu-tracking.js', __FILE__ ),
612            $asset['dependencies'] ?? array(),
613            $asset['version'] ?? self::PACKAGE_VERSION,
614            true
615        );
616
617        $current_screen   = get_current_screen();
618        $is_admin         = current_user_can( 'jetpack_disconnect' );
619        $site_id          = class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null;
620        $tracks_user_data = class_exists( 'Jetpack_Tracks_Client' ) ? Jetpack_Tracks_Client::get_connected_user_tracks_identity() : null;
621
622        wp_localize_script(
623            'jetpack-admin-ui-upgrade-menu-tracking',
624            'jetpackAdminUiUpgradeMenu',
625            array(
626                'menuItemClass'   => self::UPGRADE_MENU_SLUG,
627                'tracksUserData'  => $tracks_user_data,
628                'tracksEventData' => array(
629                    'is_admin'       => $is_admin,
630                    'current_screen' => $current_screen ? $current_screen->id : false,
631                    'blog_id'        => $site_id,
632                ),
633            )
634        );
635    }
636}