PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.8.2
Jetpack – WP Security, Backup, Speed, & Growth v9.8.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 9.8.2, at class.jetpack-admin.php

466 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Automattic\Jetpack\Status;
4 use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
5
6 // Build the Jetpack admin menu as a whole
7 class Jetpack_Admin {
8
9 /**
10 * @var Jetpack_Admin
11 **/
12 private static $instance = null;
13
14 static function init() {
15 if ( isset( $_GET['page'] ) && $_GET['page'] === 'jetpack' ) {
16 add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
17 }
18
19 if ( is_null( self::$instance ) ) {
20 self::$instance = new Jetpack_Admin();
21 }
22 return self::$instance;
23 }
24
25 static function add_no_store_header( $headers ) {
26 $headers['Cache-Control'] .= ', no-store';
27 return $headers;
28 }
29
30 private function __construct() {
31 jetpack_require_lib( 'admin-pages/class.jetpack-react-page' );
32 $this->jetpack_react = new Jetpack_React_Page();
33
34 jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
35 $this->fallback_page = new Jetpack_Settings_Page();
36
37 jetpack_require_lib( 'admin-pages/class-jetpack-about-page' );
38 $this->jetpack_about = new Jetpack_About_Page();
39
40 add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 );
41 add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 );
42 add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
43 add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
44 add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
45 add_action( 'jetpack_admin_menu', array( $this->fallback_page, 'add_actions' ) );
46 add_action( 'jetpack_admin_menu', array( $this->jetpack_about, 'add_actions' ) );
47
48 // Add redirect to current page for activation/deactivation of modules
49 add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 );
50 add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
51
52 // Add module bulk actions handler
53 add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
54
55 if ( class_exists( 'Akismet_Admin' ) ) {
56 // If the site has Jetpack Anti-Spam, change the Akismet menu label accordingly.
57 $site_products = Jetpack_Plan::get_products();
58 $anti_spam_products = array( 'jetpack_anti_spam_monthly', 'jetpack_anti_spam' );
59 if ( ! empty( array_intersect( $anti_spam_products, array_column( $site_products, 'product_slug' ) ) ) ) {
60 // Prevent Akismet from adding a menu item.
61 add_action(
62 'admin_menu',
63 function () {
64 remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );
65 },
66 4
67 );
68
69 // Add an Anti-spam menu item for Jetpack.
70 add_action(
71 'jetpack_admin_menu',
72 function () {
73 add_submenu_page( 'jetpack', __( 'Anti-Spam', 'jetpack' ), __( 'Anti-Spam', 'jetpack' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
74 }
75 );
76 add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) );
77 }
78 }
79
80 add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 );
81 }
82
83 /**
84 * Generate styles to replace Akismet logo for the Jetpack logo. It's a workaround until we create a proper settings page for
85 * Jetpack Anti-Spam. Without this, we would have to change the logo from Akismet codebase and we want to avoid that.
86 */
87 public function akismet_logo_replacement_styles() {
88 $logo = new Jetpack_Logo();
89 $logo_base64 = base64_encode( $logo->get_jp_emblem_larger() );
90 $logo_base64_url = "data:image/svg+xml;base64,{$logo_base64}";
91 $style = ".akismet-masthead__logo-container { background: url({$logo_base64_url}) no-repeat .25rem; height: 1.8125rem; } .akismet-masthead__logo { display: none; }";
92 wp_add_inline_style( 'admin-bar', $style );
93 }
94
95 static function sort_requires_connection_last( $module1, $module2 ) {
96 if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
97 return 0;
98 } elseif ( $module1['requires_connection'] ) {
99 return 1;
100 } elseif ( $module2['requires_connection'] ) {
101 return -1;
102 }
103
104 return 0;
105 }
106
107 // Produce JS understandable objects of modules containing information for
108 // presentation like description, name, configuration url, etc.
109 function get_modules() {
110 include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php';
111 $available_modules = Jetpack::get_available_modules();
112 $active_modules = Jetpack::get_active_modules();
113 $modules = array();
114 $jetpack_active = Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode();
115 $overrides = Jetpack_Modules_Overrides::instance();
116 foreach ( $available_modules as $module ) {
117 if ( $module_array = Jetpack::get_module( $module ) ) {
118 /**
119 * Filters each module's short description.
120 *
121 * @since 3.0.0
122 *
123 * @param string $module_array['description'] Module description.
124 * @param string $module Module slug.
125 */
126 $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
127 // Fix: correct multibyte strings truncate with checking for mbstring extension
128 $short_desc_trunc = ( function_exists( 'mb_strlen' ) )
129 ? ( ( mb_strlen( $short_desc ) > 143 )
130 ? mb_substr( $short_desc, 0, 140 ) . '...'
131 : $short_desc )
132 : ( ( strlen( $short_desc ) > 143 )
133 ? substr( $short_desc, 0, 140 ) . '...'
134 : $short_desc );
135
136 $module_array['module'] = $module;
137
138 $is_available = self::is_module_available( $module_array );
139
140 $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules, true ) : false );
141 $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module );
142 $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module );
143 $module_array['available'] = $is_available;
144 $module_array['unavailable_reason'] = $is_available ? false : self::get_module_unavailable_reason( $module_array );
145 $module_array['short_description'] = $short_desc_trunc;
146 $module_array['configure_url'] = Jetpack::module_configuration_url( $module );
147 $module_array['override'] = $overrides->get_module_override( $module );
148
149 ob_start();
150 /**
151 * Allow the display of a "Learn More" button.
152 * The dynamic part of the action, $module, is the module slug.
153 *
154 * @since 3.0.0
155 */
156 do_action( 'jetpack_learn_more_button_' . $module );
157 $module_array['learn_more_button'] = ob_get_clean();
158
159 ob_start();
160 /**
161 * Allow the display of information text when Jetpack is connected to WordPress.com.
162 * The dynamic part of the action, $module, is the module slug.
163 *
164 * @since 3.0.0
165 */
166 do_action( 'jetpack_module_more_info_' . $module );
167
168 /**
169 * Filter the long description of a module.
170 *
171 * @since 3.5.0
172 *
173 * @param string ob_get_clean() The module long description.
174 * @param string $module The module name.
175 */
176 $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
177
178 ob_start();
179 /**
180 * Filter the search terms for a module
181 *
182 * Search terms are typically added to the module headers, under "Additional Search Queries".
183 *
184 * Use syntax:
185 * function jetpack_$module_search_terms( $terms ) {
186 * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
187 * return $terms;
188 * }
189 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
190 *
191 * @since 3.5.0
192 *
193 * @param string The search terms (comma separated).
194 */
195 echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] );
196 $module_array['search_terms'] = ob_get_clean();
197
198 $module_array['configurable'] = false;
199 if (
200 current_user_can( 'manage_options' ) &&
201 /**
202 * Allow the display of a configuration link in the Jetpack Settings screen.
203 *
204 * @since 3.0.0
205 *
206 * @param string $module Module name.
207 * @param bool false Should the Configure module link be displayed? Default to false.
208 */
209 apply_filters( 'jetpack_module_configurable_' . $module, false )
210 ) {
211 $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
212 }
213
214 $modules[ $module ] = $module_array;
215 }
216 }
217
218 uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
219
220 if ( ! Jetpack::is_connection_ready() ) {
221 uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
222 }
223
224 return $modules;
225 }
226
227 static function is_module_available( $module ) {
228 if ( ! is_array( $module ) || empty( $module ) ) {
229 return false;
230 }
231
232 /**
233 * We never want to show VaultPress as activatable through Jetpack.
234 */
235 if ( 'vaultpress' === $module['module'] ) {
236 return false;
237 }
238
239 /*
240 * WooCommerce Analytics should only be available
241 * when running WooCommerce 3+
242 */
243 if (
244 'woocommerce-analytics' === $module['module']
245 && (
246 ! class_exists( 'WooCommerce' )
247 || version_compare( WC_VERSION, '3.0', '<' )
248 )
249 ) {
250 return false;
251 }
252
253 /*
254 * In Offline mode, modules that require a site or user
255 * level connection should be unavailable.
256 */
257 if ( ( new Status() )->is_offline_mode() ) {
258 return ! ( $module['requires_connection'] || $module['requires_user_connection'] );
259 }
260
261 /*
262 * Jetpack not connected.
263 */
264 if ( ! Jetpack::is_connection_ready() ) {
265 return false;
266 }
267
268 /*
269 * Jetpack connected at a site level only. Make sure to make
270 * modules that require a user connection unavailable.
271 */
272 if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
273 return false;
274 }
275
276 return Jetpack_Plan::supports( $module['module'] );
277
278 }
279
280 /**
281 * Returns why a module is unavailable.
282 *
283 * @param array $module The module.
284 * @return string|false A string stating why the module is not available or false if the module is available.
285 */
286 public static function get_module_unavailable_reason( $module ) {
287 if ( ! is_array( $module ) || empty( $module ) ) {
288 return false;
289 }
290
291 if ( self::is_module_available( $module ) ) {
292 return false;
293 }
294
295 /**
296 * We never want to show VaultPress as activatable through Jetpack so return an empty string.
297 */
298 if ( 'vaultpress' === $module['module'] ) {
299 return '';
300 }
301
302 /*
303 * WooCommerce Analytics should only be available
304 * when running WooCommerce 3+
305 */
306 if (
307 'woocommerce-analytics' === $module['module']
308 && (
309 ! class_exists( 'WooCommerce' )
310 || version_compare( WC_VERSION, '3.0', '<' )
311 )
312 ) {
313 return __( 'Requires WooCommerce 3+ plugin', 'jetpack' );
314 }
315
316 /*
317 * In Offline mode, modules that require a site or user
318 * level connection should be unavailable.
319 */
320 if ( ( new Status() )->is_offline_mode() ) {
321 if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
322 return __( 'Offline mode', 'jetpack' );
323 }
324 }
325
326 /*
327 * Jetpack not connected.
328 */
329 if ( ! Jetpack::is_connection_ready() ) {
330 return __( 'Jetpack is not connected', 'jetpack' );
331 }
332
333 /*
334 * Jetpack connected at a site level only and module requires a user connection.
335 */
336 if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
337 return __( 'Requires a connected WordPress.com account', 'jetpack' );
338 }
339
340 /*
341 * Plan restrictions.
342 */
343 if ( ! Jetpack_Plan::supports( $module['module'] ) ) {
344 return __( 'Not supported by current plan', 'jetpack' );
345 }
346
347 return '';
348 }
349
350 function handle_unrecognized_action( $action ) {
351 switch ( $action ) {
352 case 'bulk-activate':
353 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
354 break;
355 }
356
357 $modules = (array) $_GET['modules'];
358 $modules = array_map( 'sanitize_key', $modules );
359 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
360 foreach ( $modules as $module ) {
361 Jetpack::log( 'activate', $module );
362 Jetpack::activate_module( $module, false );
363 }
364 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
365 wp_safe_redirect( wp_get_referer() );
366 exit;
367 case 'bulk-deactivate':
368 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
369 break;
370 }
371
372 $modules = (array) $_GET['modules'];
373 $modules = array_map( 'sanitize_key', $modules );
374 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
375 foreach ( $modules as $module ) {
376 Jetpack::log( 'deactivate', $module );
377 Jetpack::deactivate_module( $module );
378 Jetpack::state( 'message', 'module_deactivated' );
379 }
380 Jetpack::state( 'module', $modules );
381 wp_safe_redirect( wp_get_referer() );
382 exit;
383 default:
384 return;
385 }
386 }
387
388 function fix_redirect( $module, $redirect = true ) {
389 if ( ! $redirect ) {
390 return;
391 }
392 if ( wp_get_referer() ) {
393 add_filter( 'wp_redirect', 'wp_get_referer' );
394 }
395 }
396
397 function admin_menu_debugger() {
398 jetpack_require_lib( 'debugger' );
399 Jetpack_Debugger::disconnect_and_redirect();
400 $debugger_hook = add_submenu_page(
401 null,
402 __( 'Debugging Center', 'jetpack' ),
403 '',
404 'manage_options',
405 'jetpack-debugger',
406 array( $this, 'wrap_debugger_page' )
407 );
408 add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
409 }
410
411 function wrap_debugger_page() {
412 nocache_headers();
413 if ( ! current_user_can( 'manage_options' ) ) {
414 die( '-1' );
415 }
416 Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ) );
417 }
418
419 function debugger_page() {
420 jetpack_require_lib( 'debugger' );
421 Jetpack_Debugger::jetpack_debug_display_handler();
422 }
423
424 /**
425 * Determines if JITMs should display on a particular screen.
426 *
427 * @param bool $value The default value of the filter.
428 * @param string $screen_id The ID of the screen being tested for JITM display.
429 *
430 * @return bool True if JITMs should display, false otherwise.
431 */
432 public function should_display_jitms_on_screen( $value, $screen_id ) {
433 // Disable all JITMs on these pages.
434 if (
435 in_array(
436 $screen_id,
437 array(
438 'jetpack_page_akismet-key-config',
439 'admin_page_jetpack_modules',
440 ),
441 true
442 ) ) {
443 return false;
444 }
445
446 // Disable all JITMs on pages where the recommendations banner is displaying.
447 if (
448 in_array(
449 $screen_id,
450 array(
451 'dashboard',
452 'plugins',
453 'jetpack_page_stats',
454 ),
455 true
456 )
457 && \Jetpack_Recommendations_Banner::can_be_displayed()
458 ) {
459 return false;
460 }
461
462 return $value;
463 }
464 }
465 Jetpack_Admin::init();
466