← All changes
|
jetpack_vendor/automattic/jetpack-status/src/class-modules.php
+54
-28
12.3.2
→
16.3-beta
View file →
| @@ -6,11 +6,11 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack; |
| 9 | 9 | |
| 10 | -use Automattic\Jetpack\Constants as Constants; | |
| 11 | 10 | use Automattic\Jetpack\Current_Plan as Jetpack_Plan; |
| 12 | 11 | use Automattic\Jetpack\IP\Utils as IP_Utils; |
| 12 | +use Automattic\Jetpack\Status\Host; | |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Class Automattic\Jetpack\Modules |
| 16 | 16 | * |
| @@ -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(); |
| @@ -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. |
| @@ -428,9 +445,9 @@ | ||
| 428 | 445 | } |
| 429 | 446 | if ( $deactivated ) { |
| 430 | 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 | |
| @@ -446,28 +463,23 @@ | ||
| 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(); |