PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 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 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-status/src/class-modules.php +48 -25 12.7.3 → 16.3 View file →
@@ -21,12 +21,18 @@
21 21 /**
22 22 * Check whether or not a Jetpack module is active.
23 23 *
24 24 * @param string $module The slug of a Jetpack module.
25 + * @param bool $available_only Whether to only check among available modules.
26 + *
25 27 * @return bool
26 28 */
27 - public function is_active( $module ) {
28 - 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 );
29 35 }
30 36
31 37 /**
32 38 * Load module data from module file. Headers differ from WordPress
@@ -68,10 +74,10 @@
68 74 $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
69 75 $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
70 76 $mod['deactivate'] = empty( $mod['deactivate'] );
71 77 $mod['free'] = empty( $mod['free'] );
72 - $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' === $mod['requires_connection'] ) ? false : true;
73 - $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'] );
74 80
75 81 if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ), true ) ) {
76 82 $mod['auto_activate'] = 'No';
77 83 } else {
@@ -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();
@@ -201,12 +211,16 @@
201 211 && get_site_option( 'jetpack_protect_active' ) ) {
202 212 $active[] = 'protect';
203 213 }
204 214
205 - // If it's not available, it shouldn't be active.
206 - // We don't delete it from the options though, as it will be active again when a plugin gets reactivated.
207 - $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 + }
208 220
221 + Feature_Policy::ensure_hooks();
222 +
209 223 /**
210 224 * Allow filtering of the active modules.
211 225 *
212 226 * Gives theme and plugin developers the power to alter the modules that
@@ -404,9 +418,9 @@
404 418 $status = new Status();
405 419 $state = new CookieState();
406 420
407 421 if ( ! \Jetpack::is_connection_ready() ) {
408 - if ( ! $status->is_offline_mode() && ! $status->is_onboarding() ) {
422 + if ( ! $status->is_offline_mode() ) {
409 423 return false;
410 424 }
411 425
412 426 // If we're not connected but in offline mode, make sure the module doesn't require a connection.
@@ -431,9 +445,9 @@
431 445 }
432 446 if ( $deactivated ) {
433 447 $state->state( 'deactivated_plugins', implode( ',', $deactivated ) );
434 448 wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
435 - exit;
449 + exit( 0 );
436 450 }
437 451 }
438 452 }
439 453
@@ -449,28 +463,23 @@
449 463 return false;
450 464 }
451 465
452 466 // Check the file for fatal errors, a la wp-admin/plugins.php::activate.
453 - $errors = new Errors();
454 467 $state->state( 'module', $module );
455 468 $state->state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error.
456 - $errors->catch_errors( true );
457 469
458 470 ob_start();
459 471 $module_path = $this->get_path( $module );
460 472 if ( file_exists( $module_path ) ) {
461 - require $this->get_path( $module ); // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
473 + require_once $this->get_path( $module );
462 474 }
463 475
464 - $active[] = $module;
465 - $this->update_active( $active );
476 + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
466 477
467 478 $state->state( 'error', false ); // the override.
468 479 ob_end_clean();
469 - $errors->catch_errors( false );
470 480 } else { // Not a Jetpack plugin.
471 - $active[] = $module;
472 - $this->update_active( $active );
481 + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
473 482 }
474 483
475 484 if ( $redirect ) {
476 485 wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) );
@@ -475,9 +484,9 @@
475 484 if ( $redirect ) {
476 485 wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) );
477 486 }
478 487 if ( $exit ) {
479 - exit;
488 + exit( 0 );
480 489 }
481 490 return true;
482 491 }
483 492
@@ -483,11 +492,14 @@
483 492
484 493 /**
485 494 * Deactivate module.
486 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 + *
487 499 * @param string $module Module slug.
488 500 *
489 - * @return bool
501 + * @return bool Whether the saved list changed.
490 502 */
491 503 public function deactivate( $module ) {
492 504 /**
493 505 * Fires when a module is deactivated.
@@ -497,12 +509,23 @@
497 509 * @param string $module Module slug.
498 510 */
499 511 do_action( 'jetpack_pre_deactivate_module', $module );
500 512
501 - $active = $this->get_active();
502 - $new = array_filter( array_diff( $active, (array) $module ) );
513 + return $this->update_active( array_filter( array_diff( $this->get_saved_active(), (array) $module ) ) );
514 + }
503 515
504 - 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();
505 528 }
506 529
507 530 /**
508 531 * Generate a module's path from its slug.
@@ -529,9 +552,9 @@
529 552 * Also fires Action hooks for each newly activated and deactivated module.
530 553 *
531 554 * @param array $modules Array of active modules to be saved in options.
532 555 *
533 - * @return $success bool true for success, false for failure.
556 + * @return bool $success true for success, false for failure.
534 557 */
535 558 public function update_active( $modules ) {
536 559 $current_modules = \Jetpack_Options::get_option( 'active_modules', array() );
537 560 $active_modules = $this->get_active();