PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.3
Jetpack – WP Security, Backup, Speed, & Growth v12.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack-admin.php
class.jetpack-admin.php
636 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Assets\Logo as Jetpack_Logo;
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 /**
15 * Build the Jetpack admin menu as a whole.
16 */
17 class Jetpack_Admin {
18
19 /**
20 * Static instance.
21 *
22 * @var Jetpack_Admin
23 */
24 private static $instance = null;
25
26 /**
27 * Initialize and fetch the static instance.
28 *
29 * @return self
30 */
31 public static function init() {
32 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
33 if ( isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) {
34 add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
35 }
36
37 if ( self::$instance === null ) {
38 self::$instance = new Jetpack_Admin();
39 }
40 return self::$instance;
41 }
42
43 /**
44 * Filter callback to add `no-store` to the `Cache-Control` header.
45 *
46 * @param array $headers Headers array.
47 * @return array Modified headers array.
48 */
49 public static function add_no_store_header( $headers ) {
50 $headers['Cache-Control'] .= ', no-store';
51 return $headers;
52 }
53
54 /** Constructor. */
55 private function __construct() {
56 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php';
57 $jetpack_react = new Jetpack_React_Page();
58
59 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-settings-page.php';
60 $fallback_page = new Jetpack_Settings_Page();
61
62 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php';
63 $jetpack_about = new Jetpack_About_Page();
64
65 add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 );
66 add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 );
67 add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
68 add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
69 add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
70 add_action( 'jetpack_admin_menu', array( $fallback_page, 'add_actions' ) );
71 add_action( 'jetpack_admin_menu', array( $jetpack_about, 'add_actions' ) );
72
73 // Add redirect to current page for activation/deactivation of modules.
74 add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 );
75 add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ), 10, 2 );
76
77 // Add module bulk actions handler.
78 add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
79
80 if ( class_exists( 'Akismet_Admin' ) ) {
81 // If the site has Jetpack Anti-Spam, change the Akismet menu label accordingly.
82 $site_products = Jetpack_Plan::get_products();
83 $anti_spam_products = array( 'jetpack_anti_spam_monthly', 'jetpack_anti_spam' );
84 if ( ! empty( array_intersect( $anti_spam_products, array_column( $site_products, 'product_slug' ) ) ) ) {
85 // Prevent Akismet from adding a menu item.
86 add_action(
87 'admin_menu',
88 function () {
89 remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );
90 },
91 4
92 );
93
94 // Add an Anti-spam menu item for Jetpack.
95 add_action(
96 'jetpack_admin_menu',
97 function () {
98 add_submenu_page( 'jetpack', __( 'Anti-Spam', 'jetpack' ), __( 'Anti-Spam', 'jetpack' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
99 }
100 );
101 add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) );
102 }
103 }
104
105 // Ensure an Additional CSS menu item is added to the Appearance menu whenever Jetpack is connected.
106 add_action( 'admin_menu', array( $this, 'additional_css_menu' ) );
107
108 add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 );
109
110 // Register Jetpack partner coupon hooks.
111 Jetpack_Partner_Coupon::register_coupon_admin_hooks( 'jetpack', Jetpack::admin_url() );
112 }
113
114 /**
115 * Generate styles to replace Akismet logo for the Jetpack logo. It's a workaround until we create a proper settings page for
116 * Jetpack Anti-Spam. Without this, we would have to change the logo from Akismet codebase and we want to avoid that.
117 */
118 public function akismet_logo_replacement_styles() {
119 $logo = new Jetpack_Logo();
120 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
121 $logo_base64 = base64_encode( $logo->get_jp_emblem_larger() );
122 $logo_base64_url = "data:image/svg+xml;base64,{$logo_base64}";
123 $style = ".akismet-masthead__logo-container { background: url({$logo_base64_url}) no-repeat .25rem; height: 1.8125rem; } .akismet-masthead__logo { display: none; }";
124 wp_add_inline_style( 'admin-bar', $style );
125 }
126
127 /**
128 * Handle our Additional CSS menu item and legacy page declaration.
129 *
130 * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php).
131 */
132 public static function additional_css_menu() {
133 /*
134 * Custom CSS for the Customizer is deprecated for block themes as of WP 6.1, so we only expose it with a menu
135 * if the site already has existing CSS code.
136 */
137 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
138 $styles = wp_get_custom_css();
139 if ( ! $styles ) {
140 return;
141 }
142 }
143
144 // If the site is a WoA site and the custom-css feature is not available, return.
145 // See https://github.com/Automattic/jetpack/pull/19965 for more on how this menu item is dealt with on WoA sites.
146 if ( ( new Host() )->is_woa_site() && ! ( in_array( 'custom-css', Jetpack::get_available_modules(), true ) ) ) {
147 return;
148 } elseif ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'custom-css' ) ) { // If the Custom CSS module is enabled, add the Additional CSS menu item and link to the Customizer.
149 // Add in our legacy page to support old bookmarks and such.
150 add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) );
151
152 // Add in our new page slug that will redirect to the customizer.
153 $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) );
154 add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) );
155 } elseif ( class_exists( 'Jetpack' ) && Jetpack::is_connection_ready() ) { // Link to the Jetpack Settings > Writing page, highlighting the Custom CSS setting.
156 add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'theme_enhancements_redirect' ) );
157
158 $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-theme-enhancements-redirect', array( __CLASS__, 'theme_enhancements_redirect' ) );
159 add_action( "load-{$hook}", array( __CLASS__, 'theme_enhancements_redirect' ) );
160 }
161 }
162
163 /**
164 * Handle the redirect for the customizer. This is necessary because
165 * we can't directly add customizer links to the admin menu.
166 *
167 * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php).
168 *
169 * There is a core patch in trac that would make this unnecessary.
170 *
171 * @link https://core.trac.wordpress.org/ticket/39050
172 */
173 public static function customizer_redirect() {
174 wp_safe_redirect(
175 self::customizer_link(
176 array(
177 'return_url' => wp_get_referer(),
178 )
179 )
180 );
181 exit;
182 }
183
184 /**
185 * Handle the Additional CSS redirect to the Jetpack settings Theme Enhancements section.
186 *
187 * @since 11.0
188 */
189 public static function theme_enhancements_redirect() {
190 wp_safe_redirect(
191 'admin.php?page=jetpack#/writing?term=custom-css'
192 );
193 exit;
194 }
195
196 /**
197 * Build the URL to deep link to the Customizer.
198 *
199 * You can modify the return url via $args.
200 *
201 * @since 11.0 in this file. This method is also located in custom-css-4.7.php to cover legacy scenarios.
202 *
203 * @param array $args Array of parameters.
204 * @return string
205 */
206 public static function customizer_link( $args = array() ) {
207 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
208 $args = wp_parse_args(
209 $args,
210 array(
211 'return_url' => rawurlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
212 )
213 );
214 }
215
216 return add_query_arg(
217 array(
218 array(
219 'autofocus' => array(
220 'section' => 'custom_css',
221 ),
222 ),
223 'return' => $args['return_url'],
224 ),
225 admin_url( 'customize.php' )
226 );
227 }
228
229 /**
230 * Sort callback to put modules with `requires_connection` last.
231 *
232 * @param array $module1 Module data.
233 * @param array $module2 Module data.
234 * @return int Indicating the relative ordering of module1 and module2.
235 */
236 public static function sort_requires_connection_last( $module1, $module2 ) {
237 if ( (bool) $module1['requires_connection'] === (bool) $module2['requires_connection'] ) {
238 return 0;
239 } elseif ( $module1['requires_connection'] ) {
240 return 1;
241 } elseif ( $module2['requires_connection'] ) {
242 return -1;
243 }
244
245 return 0;
246 }
247
248 /**
249 * Produce JS understandable objects of modules containing information for
250 * presentation like description, name, configuration url, etc.
251 */
252 public function get_modules() {
253 include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php';
254 $available_modules = Jetpack::get_available_modules();
255 $active_modules = Jetpack::get_active_modules();
256 $modules = array();
257 $jetpack_active = Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode();
258 $overrides = Jetpack_Modules_Overrides::instance();
259 foreach ( $available_modules as $module ) {
260 $module_array = Jetpack::get_module( $module );
261 if ( $module_array ) {
262 /**
263 * Filters each module's short description.
264 *
265 * @since 3.0.0
266 *
267 * @param string $module_array['description'] Module description.
268 * @param string $module Module slug.
269 */
270 $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
271 // Fix: correct multibyte strings truncate with checking for mbstring extension.
272 $short_desc_trunc = ( function_exists( 'mb_strlen' ) )
273 ? ( ( mb_strlen( $short_desc ) > 143 )
274 ? mb_substr( $short_desc, 0, 140 ) . '...'
275 : $short_desc )
276 : ( ( strlen( $short_desc ) > 143 )
277 ? substr( $short_desc, 0, 140 ) . '...'
278 : $short_desc );
279
280 $module_array['module'] = $module;
281
282 $is_available = self::is_module_available( $module_array );
283
284 $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules, true ) : false );
285 $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module );
286 $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module );
287 $module_array['available'] = $is_available;
288 $module_array['unavailable_reason'] = $is_available ? false : self::get_module_unavailable_reason( $module_array );
289 $module_array['short_description'] = $short_desc_trunc;
290 $module_array['configure_url'] = Jetpack::module_configuration_url( $module );
291 $module_array['override'] = $overrides->get_module_override( $module );
292 $module_array['disabled'] = $is_available ? '' : 'disabled="disabled"';
293
294 ob_start();
295 /**
296 * Allow the display of a "Learn More" button.
297 * The dynamic part of the action, $module, is the module slug.
298 *
299 * @since 3.0.0
300 */
301 do_action( 'jetpack_learn_more_button_' . $module );
302 $module_array['learn_more_button'] = ob_get_clean();
303
304 ob_start();
305 /**
306 * Allow the display of information text when Jetpack is connected to WordPress.com.
307 * The dynamic part of the action, $module, is the module slug.
308 *
309 * @since 3.0.0
310 */
311 do_action( 'jetpack_module_more_info_' . $module );
312
313 /**
314 * Filter the long description of a module.
315 *
316 * @since 3.5.0
317 *
318 * @param string ob_get_clean() The module long description.
319 * @param string $module The module name.
320 */
321 $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
322
323 ob_start();
324 /**
325 * Filter the search terms for a module
326 *
327 * Search terms are typically added to the module headers, under "Additional Search Queries".
328 *
329 * Use syntax:
330 * function jetpack_$module_search_terms( $terms ) {
331 * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
332 * return $terms;
333 * }
334 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
335 *
336 * @since 3.5.0
337 *
338 * @param string The search terms (comma separated).
339 */
340 echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
341 $module_array['search_terms'] = ob_get_clean();
342
343 $module_array['configurable'] = false;
344 if (
345 current_user_can( 'manage_options' ) &&
346 /**
347 * Allow the display of a configuration link in the Jetpack Settings screen.
348 *
349 * @since 3.0.0
350 *
351 * @param string $module Module name.
352 * @param bool false Should the Configure module link be displayed? Default to false.
353 */
354 apply_filters( 'jetpack_module_configurable_' . $module, false )
355 ) {
356 $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
357 }
358
359 $modules[ $module ] = $module_array;
360 }
361 }
362
363 uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
364
365 if ( ! Jetpack::is_connection_ready() ) {
366 uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
367 }
368
369 return $modules;
370 }
371
372 /**
373 * Check if a module is available.
374 *
375 * @param array $module Module data.
376 */
377 public static function is_module_available( $module ) {
378 if ( ! is_array( $module ) || empty( $module ) ) {
379 return false;
380 }
381
382 /**
383 * We never want to show VaultPress as activatable through Jetpack.
384 */
385 if ( 'vaultpress' === $module['module'] ) {
386 return false;
387 }
388
389 /*
390 * WooCommerce Analytics should only be available
391 * when running WooCommerce 3+
392 */
393 if (
394 'woocommerce-analytics' === $module['module']
395 && (
396 ! class_exists( 'WooCommerce' )
397 || version_compare( WC_VERSION, '3.0', '<' )
398 )
399 ) {
400 return false;
401 }
402
403 /*
404 * In Offline mode, modules that require a site or user
405 * level connection should be unavailable.
406 */
407 if ( ( new Status() )->is_offline_mode() ) {
408 return ! ( $module['requires_connection'] || $module['requires_user_connection'] );
409 }
410
411 /*
412 * Jetpack not connected.
413 */
414 if ( ! Jetpack::is_connection_ready() ) {
415 return false;
416 }
417
418 /*
419 * Jetpack connected at a site level only. Make sure to make
420 * modules that require a user connection unavailable.
421 */
422 if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
423 return false;
424 }
425
426 return Jetpack_Plan::supports( $module['module'] );
427 }
428
429 /**
430 * Returns why a module is unavailable.
431 *
432 * @param array $module The module.
433 * @return string|false A string stating why the module is not available or false if the module is available.
434 */
435 public static function get_module_unavailable_reason( $module ) {
436 if ( ! is_array( $module ) || empty( $module ) ) {
437 return false;
438 }
439
440 if ( self::is_module_available( $module ) ) {
441 return false;
442 }
443
444 /**
445 * We never want to show VaultPress as activatable through Jetpack so return an empty string.
446 */
447 if ( 'vaultpress' === $module['module'] ) {
448 return '';
449 }
450
451 /*
452 * WooCommerce Analytics should only be available
453 * when running WooCommerce 3+
454 */
455 if (
456 'woocommerce-analytics' === $module['module']
457 && (
458 ! class_exists( 'WooCommerce' )
459 || version_compare( WC_VERSION, '3.0', '<' )
460 )
461 ) {
462 return __( 'Requires WooCommerce 3+ plugin', 'jetpack' );
463 }
464
465 /*
466 * In Offline mode, modules that require a site or user
467 * level connection should be unavailable.
468 */
469 if ( ( new Status() )->is_offline_mode() ) {
470 if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
471 return __( 'Offline mode', 'jetpack' );
472 }
473 }
474
475 /*
476 * Jetpack not connected.
477 */
478 if ( ! Jetpack::is_connection_ready() ) {
479 return __( 'Jetpack is not connected', 'jetpack' );
480 }
481
482 /*
483 * Jetpack connected at a site level only and module requires a user connection.
484 */
485 if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
486 return __( 'Requires a connected WordPress.com account', 'jetpack' );
487 }
488
489 /*
490 * Plan restrictions.
491 */
492 if ( ! Jetpack_Plan::supports( $module['module'] ) ) {
493 return __( 'Not supported by current plan', 'jetpack' );
494 }
495
496 return '';
497 }
498
499 /**
500 * Handle an unrecognized action.
501 *
502 * @param string $action Action.
503 */
504 public function handle_unrecognized_action( $action ) {
505 switch ( $action ) {
506 case 'bulk-activate':
507 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
508 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
509 break;
510 }
511
512 $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
513 foreach ( $modules as $module ) {
514 Jetpack::log( 'activate', $module );
515 Jetpack::activate_module( $module, false );
516 }
517 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
518 wp_safe_redirect( wp_get_referer() );
519 exit;
520 case 'bulk-deactivate':
521 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
522 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
523 break;
524 }
525
526 $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
527 foreach ( $modules as $module ) {
528 Jetpack::log( 'deactivate', $module );
529 Jetpack::deactivate_module( $module );
530 Jetpack::state( 'message', 'module_deactivated' );
531 }
532 Jetpack::state( 'module', $modules );
533 wp_safe_redirect( wp_get_referer() );
534 exit;
535 default:
536 return;
537 }
538 }
539
540 /**
541 * Fix redirect.
542 *
543 * Apparently we redirect to the referrer instead of whatever WordPress
544 * wants to redirect to when activating and deactivating modules.
545 *
546 * @param string $module Module slug.
547 * @param bool $redirect Should we exit after the module has been activated. Default to true.
548 */
549 public function fix_redirect( $module, $redirect = true ) {
550 if ( ! $redirect ) {
551 return;
552 }
553 if ( wp_get_referer() ) {
554 add_filter( 'wp_redirect', 'wp_get_referer' );
555 }
556 }
557
558 /**
559 * Add debugger admin menu.
560 */
561 public function admin_menu_debugger() {
562 require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
563 Jetpack_Debugger::disconnect_and_redirect();
564 $debugger_hook = add_submenu_page(
565 '',
566 __( 'Debugging Center', 'jetpack' ),
567 '',
568 'manage_options',
569 'jetpack-debugger',
570 array( $this, 'wrap_debugger_page' )
571 );
572 add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
573 }
574
575 /**
576 * Wrap debugger page.
577 */
578 public function wrap_debugger_page() {
579 nocache_headers();
580 if ( ! current_user_can( 'manage_options' ) ) {
581 die( '-1' );
582 }
583 Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ), array( 'is-wide' => true ) );
584 }
585
586 /**
587 * Display debugger page.
588 */
589 public function debugger_page() {
590 require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
591 Jetpack_Debugger::jetpack_debug_display_handler();
592 }
593
594 /**
595 * Determines if JITMs should display on a particular screen.
596 *
597 * @param bool $value The default value of the filter.
598 * @param string $screen_id The ID of the screen being tested for JITM display.
599 *
600 * @return bool True if JITMs should display, false otherwise.
601 */
602 public function should_display_jitms_on_screen( $value, $screen_id ) {
603 // Disable all JITMs on these pages.
604 if (
605 in_array(
606 $screen_id,
607 array(
608 'jetpack_page_akismet-key-config',
609 'admin_page_jetpack_modules',
610 ),
611 true
612 ) ) {
613 return false;
614 }
615
616 // Disable all JITMs on pages where the recommendations banner is displaying.
617 if (
618 in_array(
619 $screen_id,
620 array(
621 'dashboard',
622 'plugins',
623 'jetpack_page_stats',
624 ),
625 true
626 )
627 && \Jetpack_Recommendations_Banner::can_be_displayed()
628 ) {
629 return false;
630 }
631
632 return $value;
633 }
634 }
635 Jetpack_Admin::init();
636