| @@ -1,0 +1,719 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Build the Jetpack admin menu as a whole. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +use Automattic\Jetpack\Admin_UI\Admin_Menu; | |
| 9 | +use Automattic\Jetpack\Current_Plan as Jetpack_Plan; | |
| 10 | +use Automattic\Jetpack\Partner_Coupon as Jetpack_Partner_Coupon; | |
| 11 | +use Automattic\Jetpack\Status; | |
| 12 | +use Automattic\Jetpack\Status\Host; | |
| 13 | + | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Build the Jetpack admin menu as a whole. | |
| 20 | + */ | |
| 21 | +class Jetpack_Admin { | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Static instance. | |
| 25 | + * | |
| 26 | + * @var Jetpack_Admin | |
| 27 | + */ | |
| 28 | + private static $instance = null; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Initialize and fetch the static instance. | |
| 32 | + * | |
| 33 | + * @return self | |
| 34 | + */ | |
| 35 | + public static function init() { | |
| 36 | + if ( self::$instance === null ) { | |
| 37 | + self::$instance = new Jetpack_Admin(); | |
| 38 | + } | |
| 39 | + return self::$instance; | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Filter callback to add `no-store` to the `Cache-Control` header. | |
| 44 | + * | |
| 45 | + * @deprecated 14.9 | |
| 46 | + * @param array $headers Headers array. | |
| 47 | + * @return array Modified headers array. | |
| 48 | + */ | |
| 49 | + public static function add_no_store_header( $headers ) { | |
| 50 | + _deprecated_function( __METHOD__, '14.9' ); | |
| 51 | + $headers['Cache-Control'] .= ', no-store'; | |
| 52 | + return $headers; | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** Constructor. */ | |
| 56 | + private function __construct() { | |
| 57 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php'; | |
| 58 | + $jetpack_react = new Jetpack_React_Page(); | |
| 59 | + | |
| 60 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-settings-react-page.php'; | |
| 61 | + $jetpack_settings = new Jetpack_Settings_React_Page(); | |
| 62 | + | |
| 63 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-settings-page.php'; | |
| 64 | + $fallback_page = new Jetpack_Settings_Page(); | |
| 65 | + | |
| 66 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php'; | |
| 67 | + $jetpack_about = new Jetpack_About_Page(); | |
| 68 | + | |
| 69 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-ai-page.php'; | |
| 70 | + $jetpack_ai = new Jetpack_AI_Page(); | |
| 71 | + | |
| 72 | + add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 ); | |
| 73 | + add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 ); | |
| 74 | + add_action( 'admin_menu', array( $jetpack_react, 'remove_jetpack_menu' ), 2000 ); | |
| 75 | + add_action( 'admin_menu', array( $jetpack_settings, 'maybe_load_wp_build' ), 1 ); | |
| 76 | + add_action( 'jetpack_admin_menu', array( $jetpack_settings, 'add_actions' ) ); | |
| 77 | + add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) ); | |
| 78 | + add_action( 'jetpack_admin_menu', array( $fallback_page, 'add_actions' ) ); | |
| 79 | + add_action( 'jetpack_admin_menu', array( $jetpack_about, 'add_actions' ) ); | |
| 80 | + add_action( 'jetpack_admin_menu', array( $jetpack_ai, 'add_actions' ) ); | |
| 81 | + | |
| 82 | + // Add redirect to current page for activation/deactivation of modules. | |
| 83 | + add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 ); | |
| 84 | + add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ), 10, 2 ); | |
| 85 | + | |
| 86 | + // Add module bulk actions handler. | |
| 87 | + add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) ); | |
| 88 | + | |
| 89 | + if ( class_exists( 'Akismet_Admin' ) ) { | |
| 90 | + // If the site has Jetpack Anti-spam, change the Akismet menu label and logo accordingly. | |
| 91 | + $site_products = array_column( Jetpack_Plan::get_products(), 'product_slug' ); | |
| 92 | + $has_anti_spam_product = count( array_intersect( array( 'jetpack_anti_spam', 'jetpack_anti_spam_monthly' ), $site_products ) ) > 0; | |
| 93 | + | |
| 94 | + if ( Jetpack_Plan::supports( 'akismet' ) || Jetpack_Plan::supports( 'antispam' ) || $has_anti_spam_product ) { | |
| 95 | + // Prevent Akismet from adding a menu item. | |
| 96 | + add_action( | |
| 97 | + 'admin_menu', | |
| 98 | + function () { | |
| 99 | + remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); | |
| 100 | + }, | |
| 101 | + 4 | |
| 102 | + ); | |
| 103 | + | |
| 104 | + // Add an Anti-spam menu item for Jetpack. This is handled automatically by the Admin_Menu as long as it has been initialized. | |
| 105 | + Admin_Menu::init(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Render the unified Jetpack header/footer chrome on Akismet's admin pages by | |
| 109 | + // consuming Akismet's `akismet_header` / `akismet_footer` action hooks. This does | |
| 110 | + // not modify the Akismet plugin; it only registers callbacks on its own hooks. | |
| 111 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-akismet-admin-chrome.php'; | |
| 112 | + $akismet_admin_chrome = new Akismet_Admin_Chrome(); | |
| 113 | + $akismet_admin_chrome->init_hooks(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Ensure an Additional CSS menu item is added to the Appearance menu whenever Jetpack is connected. | |
| 117 | + add_action( 'admin_menu', array( $this, 'additional_css_menu' ) ); | |
| 118 | + | |
| 119 | + add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 ); | |
| 120 | + | |
| 121 | + // Register Jetpack partner coupon hooks. | |
| 122 | + Jetpack_Partner_Coupon::register_coupon_admin_hooks( 'jetpack', Jetpack::admin_url() ); | |
| 123 | + | |
| 124 | + // Remove default WordPress admin footer on Jetpack pages only. | |
| 125 | + add_filter( 'admin_footer_text', array( $this, 'maybe_remove_admin_footer_text' ) ); | |
| 126 | + add_filter( 'update_footer', array( $this, 'maybe_remove_admin_footer_version' ), 11 ); | |
| 127 | + add_filter( 'admin_body_class', array( $this, 'add_jetpack_admin_body_class' ) ); | |
| 128 | + add_action( 'admin_head', array( $this, 'add_footer_removal_styles' ) ); | |
| 129 | + | |
| 130 | + // Make WPDS design tokens resolve at runtime on the legacy/wrap_ui Jetpack | |
| 131 | + // admin pages (Dashboard, Settings, Debugger) that don't ship their own | |
| 132 | + // `:root{--wpds-*}` source. Delegates to Admin_Menu's shared enqueue API. | |
| 133 | + add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_design_tokens' ) ); | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Handle our Additional CSS menu item and legacy page declaration. | |
| 138 | + * | |
| 139 | + * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php). | |
| 140 | + */ | |
| 141 | + public static function additional_css_menu() { | |
| 142 | + /* | |
| 143 | + * Custom CSS for the Customizer is deprecated for block themes as of WP 6.1, so we only expose it with a menu | |
| 144 | + * if the site already has existing CSS code. | |
| 145 | + */ | |
| 146 | + if ( wp_is_block_theme() ) { | |
| 147 | + $styles = wp_get_custom_css(); | |
| 148 | + if ( ! $styles ) { | |
| 149 | + return; | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + // If the site is a WoA site and the custom-css feature is not available, return. | |
| 154 | + // See https://github.com/Automattic/jetpack/pull/19965 for more on how this menu item is dealt with on WoA sites. | |
| 155 | + if ( ( new Host() )->is_woa_site() && ! ( in_array( 'custom-css', Jetpack::get_available_modules(), true ) ) ) { | |
| 156 | + return; | |
| 157 | + } elseif ( | |
| 158 | + class_exists( 'Jetpack' ) && ( | |
| 159 | + Jetpack::is_module_active( 'custom-css' ) || // If the Custom CSS module is enabled, add the Additional CSS menu item and link to the Customizer. | |
| 160 | + ( wp_is_block_theme() && ! empty( wp_get_custom_css() ) ) // Do the same if the theme is block-based but has existing custom CSS. | |
| 161 | + ) | |
| 162 | + ) { | |
| 163 | + // Add in our legacy page to support old bookmarks and such. | |
| 164 | + add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) ); | |
| 165 | + | |
| 166 | + // Add in our new page slug that will redirect to the customizer. | |
| 167 | + $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) ); | |
| 168 | + add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) ); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Handle the redirect for the customizer. This is necessary because | |
| 174 | + * we can't directly add customizer links to the admin menu. | |
| 175 | + * | |
| 176 | + * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php). | |
| 177 | + * | |
| 178 | + * There is a core patch in trac that would make this unnecessary. | |
| 179 | + * | |
| 180 | + * @link https://core.trac.wordpress.org/ticket/39050 | |
| 181 | + * | |
| 182 | + * @return never | |
| 183 | + */ | |
| 184 | + public static function customizer_redirect() { | |
| 185 | + wp_safe_redirect( | |
| 186 | + self::customizer_link( | |
| 187 | + array( | |
| 188 | + 'return_url' => wp_get_referer(), | |
| 189 | + ) | |
| 190 | + ) | |
| 191 | + ); | |
| 192 | + exit( 0 ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Build the URL to deep link to the Customizer. | |
| 197 | + * | |
| 198 | + * You can modify the return url via $args. | |
| 199 | + * | |
| 200 | + * @since 11.0 in this file. This method is also located in custom-css-4.7.php to cover legacy scenarios. | |
| 201 | + * | |
| 202 | + * @param array $args Array of parameters. | |
| 203 | + * @return string | |
| 204 | + */ | |
| 205 | + public static function customizer_link( $args = array() ) { | |
| 206 | + if ( isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 207 | + $args = wp_parse_args( | |
| 208 | + $args, | |
| 209 | + array( | |
| 210 | + 'return_url' => rawurlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 211 | + ) | |
| 212 | + ); | |
| 213 | + } | |
| 214 | + | |
| 215 | + return add_query_arg( | |
| 216 | + array( | |
| 217 | + array( | |
| 218 | + 'autofocus' => array( | |
| 219 | + 'section' => 'custom_css', | |
| 220 | + ), | |
| 221 | + ), | |
| 222 | + 'return' => $args['return_url'], | |
| 223 | + ), | |
| 224 | + admin_url( 'customize.php' ) | |
| 225 | + ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Sort callback to put modules with `requires_connection` last. | |
| 230 | + * | |
| 231 | + * @param array $module1 Module data. | |
| 232 | + * @param array $module2 Module data. | |
| 233 | + * @return int Indicating the relative ordering of module1 and module2. | |
| 234 | + */ | |
| 235 | + public static function sort_requires_connection_last( $module1, $module2 ) { | |
| 236 | + return ( (bool) $module1['requires_connection'] ) <=> ( (bool) $module2['requires_connection'] ); | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | + * Produce JS understandable objects of modules containing information for | |
| 241 | + * presentation like description, name, configuration url, etc. | |
| 242 | + */ | |
| 243 | + public function get_modules() { | |
| 244 | + include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php'; | |
| 245 | + $available_modules = Jetpack::get_available_modules(); | |
| 246 | + $active_modules = Jetpack::get_active_modules(); | |
| 247 | + $modules = array(); | |
| 248 | + $jetpack_active = Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode(); | |
| 249 | + $overrides = Jetpack_Modules_Overrides::instance(); | |
| 250 | + foreach ( $available_modules as $module ) { | |
| 251 | + $module_array = Jetpack::get_module( $module ); | |
| 252 | + if ( $module_array ) { | |
| 253 | + /** | |
| 254 | + * Filters each module's short description. | |
| 255 | + * | |
| 256 | + * @since 3.0.0 | |
| 257 | + * | |
| 258 | + * @param string $module_array['description'] Module description. | |
| 259 | + * @param string $module Module slug. | |
| 260 | + */ | |
| 261 | + $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); | |
| 262 | + // Fix: correct multibyte strings truncate with checking for mbstring extension. | |
| 263 | + $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) | |
| 264 | + ? ( ( mb_strlen( $short_desc ) > 143 ) | |
| 265 | + ? mb_substr( $short_desc, 0, 140 ) . '...' | |
| 266 | + : $short_desc ) | |
| 267 | + : ( ( strlen( $short_desc ) > 143 ) | |
| 268 | + ? substr( $short_desc, 0, 140 ) . '...' | |
| 269 | + : $short_desc ); | |
| 270 | + | |
| 271 | + $module_array['module'] = $module; | |
| 272 | + | |
| 273 | + $is_available = self::is_module_available( $module_array ); | |
| 274 | + | |
| 275 | + $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules, true ) : false ); | |
| 276 | + $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); | |
| 277 | + $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); | |
| 278 | + $module_array['available'] = $is_available; | |
| 279 | + $module_array['unavailable_reason'] = $is_available ? false : self::get_module_unavailable_reason( $module_array ); | |
| 280 | + $module_array['short_description'] = $short_desc_trunc; | |
| 281 | + $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); | |
| 282 | + $module_array['override'] = $overrides->get_module_override( $module ); | |
| 283 | + $module_array['disabled'] = $is_available ? '' : 'disabled="disabled"'; | |
| 284 | + | |
| 285 | + ob_start(); | |
| 286 | + /** | |
| 287 | + * Allow the display of a "Learn More" button. | |
| 288 | + * The dynamic part of the action, $module, is the module slug. | |
| 289 | + * | |
| 290 | + * @since 3.0.0 | |
| 291 | + */ | |
| 292 | + do_action( 'jetpack_learn_more_button_' . $module ); | |
| 293 | + $module_array['learn_more_button'] = ob_get_clean(); | |
| 294 | + | |
| 295 | + ob_start(); | |
| 296 | + /** | |
| 297 | + * Allow the display of information text when Jetpack is connected to WordPress.com. | |
| 298 | + * The dynamic part of the action, $module, is the module slug. | |
| 299 | + * | |
| 300 | + * @since 3.0.0 | |
| 301 | + */ | |
| 302 | + do_action( 'jetpack_module_more_info_' . $module ); | |
| 303 | + | |
| 304 | + /** | |
| 305 | + * Filter the long description of a module. | |
| 306 | + * | |
| 307 | + * @since 3.5.0 | |
| 308 | + * | |
| 309 | + * @param string ob_get_clean() The module long description. | |
| 310 | + * @param string $module The module name. | |
| 311 | + */ | |
| 312 | + $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); | |
| 313 | + | |
| 314 | + ob_start(); | |
| 315 | + /** | |
| 316 | + * Filter the search terms for a module | |
| 317 | + * | |
| 318 | + * Search terms are typically added to the module headers, under "Additional Search Queries". | |
| 319 | + * | |
| 320 | + * Use syntax: | |
| 321 | + * function jetpack_$module_search_terms( $terms ) { | |
| 322 | + * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); | |
| 323 | + * return $terms; | |
| 324 | + * } | |
| 325 | + * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); | |
| 326 | + * | |
| 327 | + * @since 3.5.0 | |
| 328 | + * | |
| 329 | + * @param string The search terms (comma-separated). | |
| 330 | + */ | |
| 331 | + echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 332 | + $module_array['search_terms'] = ob_get_clean(); | |
| 333 | + | |
| 334 | + $module_array['configurable'] = false; | |
| 335 | + if ( | |
| 336 | + current_user_can( 'manage_options' ) && | |
| 337 | + /** | |
| 338 | + * Allow the display of a configuration link in the Jetpack Settings screen. | |
| 339 | + * | |
| 340 | + * @since 3.0.0 | |
| 341 | + * | |
| 342 | + * @param string $module Module name. | |
| 343 | + * @param bool false Should the Configure module link be displayed? Default to false. | |
| 344 | + */ | |
| 345 | + apply_filters( 'jetpack_module_configurable_' . $module, false ) | |
| 346 | + ) { | |
| 347 | + $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) ); | |
| 348 | + } | |
| 349 | + | |
| 350 | + $modules[ $module ] = $module_array; | |
| 351 | + } | |
| 352 | + } | |
| 353 | + | |
| 354 | + uasort( $modules, array( 'Jetpack', 'sort_modules' ) ); | |
| 355 | + | |
| 356 | + if ( ! Jetpack::is_connection_ready() ) { | |
| 357 | + uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + return $modules; | |
| 361 | + } | |
| 362 | + | |
| 363 | + /** | |
| 364 | + * Check if a module is available. | |
| 365 | + * | |
| 366 | + * @param array $module Module data. | |
| 367 | + */ | |
| 368 | + public static function is_module_available( $module ) { | |
| 369 | + if ( ! is_array( $module ) || empty( $module ) ) { | |
| 370 | + return false; | |
| 371 | + } | |
| 372 | + | |
| 373 | + /** | |
| 374 | + * We never want to show VaultPress as activatable through Jetpack. | |
| 375 | + */ | |
| 376 | + if ( 'vaultpress' === $module['module'] ) { | |
| 377 | + return false; | |
| 378 | + } | |
| 379 | + | |
| 380 | + /* | |
| 381 | + * WooCommerce Analytics should only be available | |
| 382 | + * when running WooCommerce 3+ | |
| 383 | + */ | |
| 384 | + if ( | |
| 385 | + 'woocommerce-analytics' === $module['module'] | |
| 386 | + && ( | |
| 387 | + ! class_exists( 'WooCommerce' ) | |
| 388 | + || version_compare( WC_VERSION, '3.0', '<' ) | |
| 389 | + ) | |
| 390 | + ) { | |
| 391 | + return false; | |
| 392 | + } | |
| 393 | + | |
| 394 | + /* | |
| 395 | + * In Offline mode, modules that require a site or user | |
| 396 | + * level connection should be unavailable. | |
| 397 | + */ | |
| 398 | + if ( ( new Status() )->is_offline_mode() ) { | |
| 399 | + return ! ( $module['requires_connection'] || $module['requires_user_connection'] ); | |
| 400 | + } | |
| 401 | + | |
| 402 | + /* | |
| 403 | + * Jetpack not connected. | |
| 404 | + */ | |
| 405 | + if ( ! Jetpack::is_connection_ready() ) { | |
| 406 | + return false; | |
| 407 | + } | |
| 408 | + | |
| 409 | + /* | |
| 410 | + * Jetpack connected at a site level only. Make sure to make | |
| 411 | + * modules that require a user connection unavailable. | |
| 412 | + */ | |
| 413 | + if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) { | |
| 414 | + return false; | |
| 415 | + } | |
| 416 | + | |
| 417 | + return Jetpack_Plan::supports( $module['module'] ); | |
| 418 | + } | |
| 419 | + | |
| 420 | + /** | |
| 421 | + * Returns why a module is unavailable. | |
| 422 | + * | |
| 423 | + * @param array $module The module. | |
| 424 | + * @return string|false A string stating why the module is not available or false if the module is available. | |
| 425 | + */ | |
| 426 | + public static function get_module_unavailable_reason( $module ) { | |
| 427 | + if ( ! is_array( $module ) || empty( $module ) ) { | |
| 428 | + return false; | |
| 429 | + } | |
| 430 | + | |
| 431 | + if ( self::is_module_available( $module ) ) { | |
| 432 | + return false; | |
| 433 | + } | |
| 434 | + | |
| 435 | + /** | |
| 436 | + * We never want to show VaultPress as activatable through Jetpack so return an empty string. | |
| 437 | + */ | |
| 438 | + if ( 'vaultpress' === $module['module'] ) { | |
| 439 | + return ''; | |
| 440 | + } | |
| 441 | + | |
| 442 | + /* | |
| 443 | + * WooCommerce Analytics should only be available | |
| 444 | + * when running WooCommerce 3+ | |
| 445 | + */ | |
| 446 | + if ( | |
| 447 | + 'woocommerce-analytics' === $module['module'] | |
| 448 | + && ( | |
| 449 | + ! class_exists( 'WooCommerce' ) | |
| 450 | + || version_compare( WC_VERSION, '3.0', '<' ) | |
| 451 | + ) | |
| 452 | + ) { | |
| 453 | + return __( 'Requires WooCommerce 3+ plugin', 'jetpack' ); | |
| 454 | + } | |
| 455 | + | |
| 456 | + /* | |
| 457 | + * In Offline mode, modules that require a site or user | |
| 458 | + * level connection should be unavailable. | |
| 459 | + */ | |
| 460 | + if ( ( new Status() )->is_offline_mode() ) { | |
| 461 | + if ( $module['requires_connection'] || $module['requires_user_connection'] ) { | |
| 462 | + return __( 'Unavailable in Offline mode', 'jetpack' ); | |
| 463 | + } | |
| 464 | + } | |
| 465 | + | |
| 466 | + /* | |
| 467 | + * Jetpack not connected. | |
| 468 | + */ | |
| 469 | + if ( ! Jetpack::is_connection_ready() ) { | |
| 470 | + return __( 'Jetpack is not connected', 'jetpack' ); | |
| 471 | + } | |
| 472 | + | |
| 473 | + /* | |
| 474 | + * Jetpack connected at a site level only and module requires a user connection. | |
| 475 | + */ | |
| 476 | + if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) { | |
| 477 | + return __( 'Requires a connected WordPress.com account', 'jetpack' ); | |
| 478 | + } | |
| 479 | + | |
| 480 | + /* | |
| 481 | + * Plan restrictions. | |
| 482 | + */ | |
| 483 | + if ( ! Jetpack_Plan::supports( $module['module'] ) ) { | |
| 484 | + return __( 'Not supported by current plan', 'jetpack' ); | |
| 485 | + } | |
| 486 | + | |
| 487 | + return ''; | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * Handle an unrecognized action. | |
| 492 | + * | |
| 493 | + * @param string $action Action. | |
| 494 | + */ | |
| 495 | + public function handle_unrecognized_action( $action ) { | |
| 496 | + switch ( $action ) { | |
| 497 | + case 'bulk-activate': | |
| 498 | + check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); | |
| 499 | + if ( ! current_user_can( 'jetpack_activate_modules' ) ) { | |
| 500 | + break; | |
| 501 | + } | |
| 502 | + | |
| 503 | + $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array(); | |
| 504 | + foreach ( $modules as $module ) { | |
| 505 | + Jetpack::log( 'activate', $module ); | |
| 506 | + Jetpack::activate_module( $module, false ); | |
| 507 | + } | |
| 508 | + // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. | |
| 509 | + wp_safe_redirect( wp_get_referer() ); | |
| 510 | + exit( 0 ); | |
| 511 | + case 'bulk-deactivate': | |
| 512 | + check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); | |
| 513 | + if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { | |
| 514 | + break; | |
| 515 | + } | |
| 516 | + | |
| 517 | + $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array(); | |
| 518 | + foreach ( $modules as $module ) { | |
| 519 | + Jetpack::log( 'deactivate', $module ); | |
| 520 | + Jetpack::deactivate_module( $module ); | |
| 521 | + Jetpack::state( 'message', 'module_deactivated' ); | |
| 522 | + } | |
| 523 | + Jetpack::state( 'module', $modules ); | |
| 524 | + wp_safe_redirect( wp_get_referer() ); | |
| 525 | + exit( 0 ); | |
| 526 | + default: | |
| 527 | + return; | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 531 | + /** | |
| 532 | + * Fix redirect. | |
| 533 | + * | |
| 534 | + * Apparently we redirect to the referrer instead of whatever WordPress | |
| 535 | + * wants to redirect to when activating and deactivating modules. | |
| 536 | + * | |
| 537 | + * @param string $module Module slug. | |
| 538 | + * @param bool $redirect Should we exit after the module has been activated. Default to true. | |
| 539 | + */ | |
| 540 | + public function fix_redirect( $module, $redirect = true ) { | |
| 541 | + if ( ! $redirect ) { | |
| 542 | + return; | |
| 543 | + } | |
| 544 | + if ( wp_get_referer() ) { | |
| 545 | + add_filter( 'wp_redirect', 'wp_get_referer' ); | |
| 546 | + } | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * Add debugger admin menu. | |
| 551 | + */ | |
| 552 | + public function admin_menu_debugger() { | |
| 553 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php'; | |
| 554 | + Jetpack_Debugger::disconnect_and_redirect(); | |
| 555 | + $debugger_hook = add_submenu_page( | |
| 556 | + '', | |
| 557 | + __( 'Debugging Center', 'jetpack' ), | |
| 558 | + '', | |
| 559 | + 'manage_options', | |
| 560 | + 'jetpack-debugger', | |
| 561 | + array( $this, 'wrap_debugger_page' ) | |
| 562 | + ); | |
| 563 | + add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) ); | |
| 564 | + } | |
| 565 | + | |
| 566 | + /** | |
| 567 | + * Wrap debugger page. | |
| 568 | + */ | |
| 569 | + public function wrap_debugger_page() { | |
| 570 | + nocache_headers(); | |
| 571 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 572 | + die( '-1' ); | |
| 573 | + } | |
| 574 | + Jetpack_Admin_Page::wrap_ui( | |
| 575 | + array( $this, 'debugger_page' ), | |
| 576 | + array( | |
| 577 | + 'is-wide' => true, | |
| 578 | + 'show-nav' => false, | |
| 579 | + ) | |
| 580 | + ); | |
| 581 | + } | |
| 582 | + | |
| 583 | + /** | |
| 584 | + * Display debugger page. | |
| 585 | + */ | |
| 586 | + public function debugger_page() { | |
| 587 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php'; | |
| 588 | + Jetpack_Debugger::jetpack_debug_display_handler(); | |
| 589 | + } | |
| 590 | + | |
| 591 | + /** | |
| 592 | + * Determines if JITMs should display on a particular screen. | |
| 593 | + * | |
| 594 | + * @param bool $value The default value of the filter. | |
| 595 | + * @param string $screen_id The ID of the screen being tested for JITM display. | |
| 596 | + * | |
| 597 | + * @return bool True if JITMs should display, false otherwise. | |
| 598 | + */ | |
| 599 | + public function should_display_jitms_on_screen( $value, $screen_id ) { | |
| 600 | + // Disable all JITMs on these pages. | |
| 601 | + if ( | |
| 602 | + in_array( | |
| 603 | + $screen_id, | |
| 604 | + array( | |
| 605 | + 'jetpack_page_akismet-key-config', | |
| 606 | + 'admin_page_jetpack_modules', | |
| 607 | + ), | |
| 608 | + true | |
| 609 | + ) ) { | |
| 610 | + return false; | |
| 611 | + } | |
| 612 | + | |
| 613 | + return $value; | |
| 614 | + } | |
| 615 | + | |
| 616 | + /** | |
| 617 | + * Check if we're on a Jetpack admin page. | |
| 618 | + * | |
| 619 | + * Similar to how WooCommerce checks for its admin pages by comparing | |
| 620 | + * against known screen ID patterns. | |
| 621 | + * | |
| 622 | + * @return bool True if on a Jetpack admin page, false otherwise. | |
| 623 | + */ | |
| 624 | + private function is_jetpack_admin_page() { | |
| 625 | + $screen = get_current_screen(); | |
| 626 | + if ( ! $screen ) { | |
| 627 | + return false; | |
| 628 | + } | |
| 629 | + | |
| 630 | + // Check for Jetpack admin pages: | |
| 631 | + // - toplevel_page_jetpack (main Jetpack menu page) | |
| 632 | + // - toplevel_page_jetpack-network (Jetpack Network Admin menu page) | |
| 633 | + // - jetpack_page_* (Jetpack submenu pages) | |
| 634 | + // - admin_page_jetpack* (legacy/special Jetpack pages) | |
| 635 | + // Or check if parent_base is 'jetpack' or 'jetpack-network' (submenu pages) | |
| 636 | + return ( | |
| 637 | + $screen->id === 'toplevel_page_jetpack' || | |
| 638 | + $screen->id === 'toplevel_page_jetpack-network' || | |
| 639 | + str_starts_with( $screen->id, 'jetpack_page_' ) || | |
| 640 | + str_starts_with( $screen->id, 'admin_page_jetpack' ) || | |
| 641 | + $screen->parent_base === 'jetpack' || | |
| 642 | + $screen->parent_base === 'jetpack-network' | |
| 643 | + ); | |
| 644 | + } | |
| 645 | + | |
| 646 | + /** | |
| 647 | + * Add a body class to Jetpack admin pages. | |
| 648 | + * | |
| 649 | + * @param string $classes Space-separated list of CSS classes. | |
| 650 | + * @return string Modified class list. | |
| 651 | + */ | |
| 652 | + public function add_jetpack_admin_body_class( $classes ) { | |
| 653 | + if ( $this->is_jetpack_admin_page() ) { | |
| 654 | + return ltrim( trim( $classes ) . ' jetpack-admin-page ' ); | |
| 655 | + } | |
| 656 | + return $classes; | |
| 657 | + } | |
| 658 | + | |
| 659 | + /** | |
| 660 | + * Add inline styles to remove footer padding on Jetpack pages. | |
| 661 | + * | |
| 662 | + * This needs to be inline because jetpack-admin.css is not loaded on | |
| 663 | + * React-powered admin pages (they use load_wrapper_styles instead). | |
| 664 | + */ | |
| 665 | + public function add_footer_removal_styles() { | |
| 666 | + if ( ! $this->is_jetpack_admin_page() ) { | |
| 667 | + return; | |
| 668 | + } | |
| 669 | + echo '<style>.jetpack-admin-page #wpbody-content { padding-bottom: 0; } .jetpack-admin-page #wpfooter { display: none; }</style>'; | |
| 670 | + } | |
| 671 | + | |
| 672 | + /** | |
| 673 | + * Enqueues the shared WPDS design-tokens stylesheet on the legacy/wrap_ui pages. | |
| 674 | + * | |
| 675 | + * This is the admin_enqueue_scripts callback for the legacy Jetpack admin | |
| 676 | + * pages. The admin-ui package owns the handle and enqueues it on the | |
| 677 | + * modernized dashboards it registers; the legacy/wrap_ui pages (Dashboard, | |
| 678 | + * Settings, Debugger) aren't registered through Admin_Menu, so we cover them | |
| 679 | + * here via the central is_jetpack_admin_page() gate. The actual enqueue is | |
| 680 | + * delegated to the reusable Admin_Menu::enqueue_design_tokens() API so there | |
| 681 | + * is a single owner of the handle and no duplicated register/enqueue logic. | |
| 682 | + * | |
| 683 | + * @return void | |
| 684 | + */ | |
| 685 | + public function maybe_enqueue_design_tokens() { | |
| 686 | + if ( ! $this->is_jetpack_admin_page() ) { | |
| 687 | + return; | |
| 688 | + } | |
| 689 | + | |
| 690 | + // Guard against an older admin-ui being loaded ahead of this one by the | |
| 691 | + // package autoloader's version-precedence resolution. | |
| 692 | + if ( ! method_exists( Admin_Menu::class, 'enqueue_design_tokens' ) ) { | |
| 693 | + return; | |
| 694 | + } | |
| 695 | + | |
| 696 | + Admin_Menu::enqueue_design_tokens(); | |
| 697 | + } | |
| 698 | + | |
| 699 | + /** | |
| 700 | + * Remove the admin footer text on Jetpack pages. | |
| 701 | + * | |
| 702 | + * @param string $content The default footer text. | |
| 703 | + * @return string Empty string on Jetpack pages, original content otherwise. | |
| 704 | + */ | |
| 705 | + public function maybe_remove_admin_footer_text( $content ) { | |
| 706 | + return $this->is_jetpack_admin_page() ? '' : $content; | |
| 707 | + } | |
| 708 | + | |
| 709 | + /** | |
| 710 | + * Remove the admin footer version on Jetpack pages. | |
| 711 | + * | |
| 712 | + * @param string $content The default footer version text. | |
| 713 | + * @return string Empty string on Jetpack pages, original content otherwise. | |
| 714 | + */ | |
| 715 | + public function maybe_remove_admin_footer_version( $content ) { | |
| 716 | + return $this->is_jetpack_admin_page() ? '' : $content; | |
| 717 | + } | |
| 718 | +} | |
| 719 | +Jetpack_Admin::init(); | |