PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.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 All 507 releases
← All changes | jetpack_vendor/automattic/jetpack-status/src/class-modules.php +59 -33 12.0.3 → 16.3-beta View file →
@@ -6,10 +6,11 @@
6 6 */
7 7
8 8 namespace Automattic\Jetpack;
9 9
10 -use Automattic\Jetpack\Constants as Constants;
10 +use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
11 11 use Automattic\Jetpack\IP\Utils as IP_Utils;
12 +use Automattic\Jetpack\Status\Host;
12 13
13 14 /**
14 15 * Class Automattic\Jetpack\Modules
15 16 *
@@ -20,12 +21,18 @@
20 21 /**
21 22 * Check whether or not a Jetpack module is active.
22 23 *
23 24 * @param string $module The slug of a Jetpack module.
25 + * @param bool $available_only Whether to only check among available modules.
26 + *
24 27 * @return bool
25 28 */
26 - public function is_active( $module ) {
27 - return in_array( $module, self::get_active(), true );
29 + public function is_active( $module, $available_only = true ) {
30 + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
31 + return true;
32 + }
33 +
34 + return in_array( $module, self::get_active( $available_only ), true );
28 35 }
29 36
30 37 /**
31 38 * Load module data from module file. Headers differ from WordPress
@@ -67,10 +74,10 @@
67 74 $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
68 75 $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
69 76 $mod['deactivate'] = empty( $mod['deactivate'] );
70 77 $mod['free'] = empty( $mod['free'] );
71 - $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' === $mod['requires_connection'] ) ? false : true;
72 - $mod['requires_user_connection'] = ( empty( $mod['requires_user_connection'] ) || 'No' === $mod['requires_user_connection'] ) ? false : true;
78 + $mod['requires_connection'] = empty( $mod['requires_connection'] ) || 'No' !== $mod['requires_connection'];
79 + $mod['requires_user_connection'] = ! ( empty( $mod['requires_user_connection'] ) || 'No' === $mod['requires_user_connection'] );
73 80
74 81 if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ), true ) ) {
75 82 $mod['auto_activate'] = 'No';
76 83 } else {
@@ -79,11 +86,10 @@
79 86
80 87 if ( $mod['module_tags'] ) {
81 88 $mod['module_tags'] = explode( ',', $mod['module_tags'] );
82 89 $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
83 - $mod['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $mod['module_tags'] );
84 90 } else {
85 - $mod['module_tags'] = array( jetpack_get_module_i18n_tag( 'Other' ) );
91 + $mod['module_tags'] = array( 'Other' );
86 92 }
87 93
88 94 if ( $mod['plan_classes'] ) {
89 95 $mod['plan_classes'] = explode( ',', $mod['plan_classes'] );
@@ -95,9 +101,9 @@
95 101 if ( $mod['feature'] ) {
96 102 $mod['feature'] = explode( ',', $mod['feature'] );
97 103 $mod['feature'] = array_map( 'trim', $mod['feature'] );
98 104 } else {
99 - $mod['feature'] = array( jetpack_get_module_i18n_tag( 'Other' ) );
105 + $mod['feature'] = array( 'Other' );
100 106 }
101 107
102 108 $modules_details[ $module ] = $mod;
103 109
@@ -161,9 +167,9 @@
161 167 $file_data_option = array();
162 168 }
163 169
164 170 $key = md5( $file_name . maybe_serialize( $headers ) );
165 - $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
171 + $refresh_cache = is_admin() && isset( $_GET['page'] ) && is_string( $_GET['page'] ) && str_starts_with( $_GET['page'], 'jetpack' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
166 172
167 173 // If we don't need to refresh the cache, and already have the value, short-circuit!
168 174 if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) {
169 175 return $file_data_option[ $key ];
@@ -179,10 +185,14 @@
179 185 }
180 186
181 187 /**
182 188 * Get a list of activated modules as an array of module slugs.
189 + *
190 + * @param bool $available_only Filter out the unavailable (deleted) modules.
191 + *
192 + * @return array
183 193 */
184 - public function get_active() {
194 + public function get_active( $available_only = true ) {
185 195 $active = \Jetpack_Options::get_option( 'active_modules' );
186 196
187 197 if ( ! is_array( $active ) ) {
188 198 $active = array();
@@ -193,17 +203,24 @@
193 203 } else {
194 204 $active = array_diff( $active, array( 'vaultpress' ) );
195 205 }
196 206
197 - // If protect is active on the main site of a multisite, it should be active on all sites.
198 - if ( ! in_array( 'protect', $active, true ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) {
207 + // If protect is active on the main site of a multisite, it should be active on all sites. Doesn't apply to WP.com.
208 + if ( ! in_array( 'protect', $active, true )
209 + && ! ( new Host() )->is_wpcom_simple()
210 + && is_multisite()
211 + && get_site_option( 'jetpack_protect_active' ) ) {
199 212 $active[] = 'protect';
200 213 }
201 214
202 - // If it's not available, it shouldn't be active.
203 - // We don't delete it from the options though, as it will be active again when a plugin gets reactivated.
204 - $active = array_intersect( $active, $this->get_available() );
215 + if ( $available_only ) {
216 + // If it's not available, it shouldn't be active.
217 + // We don't delete it from the options though, as it will be active again when a plugin gets reactivated.
218 + $active = array_intersect( $active, $this->get_available() );
219 + }
205 220
221 + Feature_Policy::ensure_hooks();
222 +
206 223 /**
207 224 * Allow filtering of the active modules.
208 225 *
209 226 * Gives theme and plugin developers the power to alter the modules that
@@ -401,9 +418,9 @@
401 418 $status = new Status();
402 419 $state = new CookieState();
403 420
404 421 if ( ! \Jetpack::is_connection_ready() ) {
405 - if ( ! $status->is_offline_mode() && ! $status->is_onboarding() ) {
422 + if ( ! $status->is_offline_mode() ) {
406 423 return false;
407 424 }
408 425
409 426 // If we're not connected but in offline mode, make sure the module doesn't require a connection.
@@ -426,11 +443,11 @@
426 443 $deactivated[] = "$module:$idx";
427 444 }
428 445 }
429 446 if ( $deactivated ) {
430 - $state->state( 'deactivated_plugins', join( ',', $deactivated ) );
447 + $state->state( 'deactivated_plugins', implode( ',', $deactivated ) );
431 448 wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
432 - exit;
449 + exit( 0 );
433 450 }
434 451 }
435 452 }
436 453
@@ -441,33 +458,28 @@
441 458 return false;
442 459 }
443 460 }
444 461
445 - if ( class_exists( 'Jetpack_Plan' ) && ! \Jetpack_Plan::supports( $module ) ) {
462 + if ( ! Jetpack_Plan::supports( $module ) ) {
446 463 return false;
447 464 }
448 465
449 466 // Check the file for fatal errors, a la wp-admin/plugins.php::activate.
450 - $errors = new Errors();
451 467 $state->state( 'module', $module );
452 468 $state->state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error.
453 - $errors->catch_errors( true );
454 469
455 470 ob_start();
456 471 $module_path = $this->get_path( $module );
457 472 if ( file_exists( $module_path ) ) {
458 - require $this->get_path( $module ); // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
473 + require_once $this->get_path( $module );
459 474 }
460 475
461 - $active[] = $module;
462 - $this->update_active( $active );
476 + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
463 477
464 478 $state->state( 'error', false ); // the override.
465 479 ob_end_clean();
466 - $errors->catch_errors( false );
467 480 } else { // Not a Jetpack plugin.
468 - $active[] = $module;
469 - $this->update_active( $active );
481 + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
470 482 }
471 483
472 484 if ( $redirect ) {
473 485 wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) );
@@ -472,9 +484,9 @@
472 484 if ( $redirect ) {
473 485 wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) );
474 486 }
475 487 if ( $exit ) {
476 - exit;
488 + exit( 0 );
477 489 }
478 490 return true;
479 491 }
480 492
@@ -480,11 +492,14 @@
480 492
481 493 /**
482 494 * Deactivate module.
483 495 *
496 + * A module a `jetpack_active_modules` callback forces on keeps running; callers that report
497 + * back to a person should check is_active() afterwards.
498 + *
484 499 * @param string $module Module slug.
485 500 *
486 - * @return bool
501 + * @return bool Whether the saved list changed.
487 502 */
488 503 public function deactivate( $module ) {
489 504 /**
490 505 * Fires when a module is deactivated.
@@ -494,12 +509,23 @@
494 509 * @param string $module Module slug.
495 510 */
496 511 do_action( 'jetpack_pre_deactivate_module', $module );
497 512
498 - $active = $this->get_active();
499 - $new = array_filter( array_diff( $active, (array) $module ) );
513 + return $this->update_active( array_filter( array_diff( $this->get_saved_active(), (array) $module ) ) );
514 + }
500 515
501 - return $this->update_active( $new );
516 + /**
517 + * The active modules as saved, before filters add or drop any.
518 + *
519 + * Switching one module builds on this rather than get_active(), which would save whatever a
520 + * `jetpack_active_modules` callback forces on.
521 + *
522 + * @return string[] Module slugs.
523 + */
524 + private function get_saved_active() {
525 + $saved = \Jetpack_Options::get_option( 'active_modules', array() );
526 +
527 + return is_array( $saved ) ? array_values( $saved ) : array();
502 528 }
503 529
504 530 /**
505 531 * Generate a module's path from its slug.
@@ -526,9 +552,9 @@
526 552 * Also fires Action hooks for each newly activated and deactivated module.
527 553 *
528 554 * @param array $modules Array of active modules to be saved in options.
529 555 *
530 - * @return $success bool true for success, false for failure.
556 + * @return bool $success true for success, false for failure.
531 557 */
532 558 public function update_active( $modules ) {
533 559 $current_modules = \Jetpack_Options::get_option( 'active_modules', array() );
534 560 $active_modules = $this->get_active();