PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2
Jetpack – WP Security, Backup, Speed, & Growth v11.2
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 in Jetpack – WP Security, Backup, Speed, & Growth 11.2, at class.jetpack-admin.php

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