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