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

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