← All changes
|
jetpack_vendor/automattic/jetpack-status/src/class-modules.php
+44
-25
13.0.2
→
16.3-beta
View file →
| @@ -21,16 +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 ) { | |
| 29 | + public function is_active( $module, $available_only = true ) { | |
| 28 | 30 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 29 | 31 | return true; |
| 30 | 32 | } |
| 31 | 33 | |
| 32 | - return in_array( $module, self::get_active(), true ); | |
| 34 | + return in_array( $module, self::get_active( $available_only ), true ); | |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | /** |
| 36 | 38 | * Load module data from module file. Headers differ from WordPress |
| @@ -72,10 +74,10 @@ | ||
| 72 | 74 | $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; |
| 73 | 75 | $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; |
| 74 | 76 | $mod['deactivate'] = empty( $mod['deactivate'] ); |
| 75 | 77 | $mod['free'] = empty( $mod['free'] ); |
| 76 | - $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' === $mod['requires_connection'] ) ? false : true; | |
| 77 | - $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'] ); | |
| 78 | 80 | |
| 79 | 81 | if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ), true ) ) { |
| 80 | 82 | $mod['auto_activate'] = 'No'; |
| 81 | 83 | } else { |
| @@ -165,9 +167,9 @@ | ||
| 165 | 167 | $file_data_option = array(); |
| 166 | 168 | } |
| 167 | 169 | |
| 168 | 170 | $key = md5( $file_name . maybe_serialize( $headers ) ); |
| 169 | - $refresh_cache = is_admin() && isset( $_GET['page'] ) && str_starts_with( $_GET['page'], 'jetpack' ); // 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 | |
| 170 | 172 | |
| 171 | 173 | // If we don't need to refresh the cache, and already have the value, short-circuit! |
| 172 | 174 | if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) { |
| 173 | 175 | return $file_data_option[ $key ]; |
| @@ -183,10 +185,14 @@ | ||
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | /** |
| 186 | 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 | |
| 187 | 193 | */ |
| 188 | - public function get_active() { | |
| 194 | + public function get_active( $available_only = true ) { | |
| 189 | 195 | $active = \Jetpack_Options::get_option( 'active_modules' ); |
| 190 | 196 | |
| 191 | 197 | if ( ! is_array( $active ) ) { |
| 192 | 198 | $active = array(); |
| @@ -205,12 +211,16 @@ | ||
| 205 | 211 | && get_site_option( 'jetpack_protect_active' ) ) { |
| 206 | 212 | $active[] = 'protect'; |
| 207 | 213 | } |
| 208 | 214 | |
| 209 | - // If it's not available, it shouldn't be active. | |
| 210 | - // We don't delete it from the options though, as it will be active again when a plugin gets reactivated. | |
| 211 | - $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 | + } | |
| 212 | 220 | |
| 221 | + Feature_Policy::ensure_hooks(); | |
| 222 | + | |
| 213 | 223 | /** |
| 214 | 224 | * Allow filtering of the active modules. |
| 215 | 225 | * |
| 216 | 226 | * Gives theme and plugin developers the power to alter the modules that |
| @@ -408,9 +418,9 @@ | ||
| 408 | 418 | $status = new Status(); |
| 409 | 419 | $state = new CookieState(); |
| 410 | 420 | |
| 411 | 421 | if ( ! \Jetpack::is_connection_ready() ) { |
| 412 | - if ( ! $status->is_offline_mode() && ! $status->is_onboarding() ) { | |
| 422 | + if ( ! $status->is_offline_mode() ) { | |
| 413 | 423 | return false; |
| 414 | 424 | } |
| 415 | 425 | |
| 416 | 426 | // If we're not connected but in offline mode, make sure the module doesn't require a connection. |
| @@ -435,9 +445,9 @@ | ||
| 435 | 445 | } |
| 436 | 446 | if ( $deactivated ) { |
| 437 | 447 | $state->state( 'deactivated_plugins', implode( ',', $deactivated ) ); |
| 438 | 448 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
| 439 | - exit; | |
| 449 | + exit( 0 ); | |
| 440 | 450 | } |
| 441 | 451 | } |
| 442 | 452 | } |
| 443 | 453 | |
| @@ -453,28 +463,23 @@ | ||
| 453 | 463 | return false; |
| 454 | 464 | } |
| 455 | 465 | |
| 456 | 466 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate. |
| 457 | - $errors = new Errors(); | |
| 458 | 467 | $state->state( 'module', $module ); |
| 459 | 468 | $state->state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error. |
| 460 | - $errors->catch_errors( true ); | |
| 461 | 469 | |
| 462 | 470 | ob_start(); |
| 463 | 471 | $module_path = $this->get_path( $module ); |
| 464 | 472 | if ( file_exists( $module_path ) ) { |
| 465 | - require $this->get_path( $module ); // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath | |
| 473 | + require_once $this->get_path( $module ); | |
| 466 | 474 | } |
| 467 | 475 | |
| 468 | - $active[] = $module; | |
| 469 | - $this->update_active( $active ); | |
| 476 | + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) ); | |
| 470 | 477 | |
| 471 | 478 | $state->state( 'error', false ); // the override. |
| 472 | 479 | ob_end_clean(); |
| 473 | - $errors->catch_errors( false ); | |
| 474 | 480 | } else { // Not a Jetpack plugin. |
| 475 | - $active[] = $module; | |
| 476 | - $this->update_active( $active ); | |
| 481 | + $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) ); | |
| 477 | 482 | } |
| 478 | 483 | |
| 479 | 484 | if ( $redirect ) { |
| 480 | 485 | wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) ); |
| @@ -479,9 +484,9 @@ | ||
| 479 | 484 | if ( $redirect ) { |
| 480 | 485 | wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) ); |
| 481 | 486 | } |
| 482 | 487 | if ( $exit ) { |
| 483 | - exit; | |
| 488 | + exit( 0 ); | |
| 484 | 489 | } |
| 485 | 490 | return true; |
| 486 | 491 | } |
| 487 | 492 | |
| @@ -487,11 +492,14 @@ | ||
| 487 | 492 | |
| 488 | 493 | /** |
| 489 | 494 | * Deactivate module. |
| 490 | 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 | + * | |
| 491 | 499 | * @param string $module Module slug. |
| 492 | 500 | * |
| 493 | - * @return bool | |
| 501 | + * @return bool Whether the saved list changed. | |
| 494 | 502 | */ |
| 495 | 503 | public function deactivate( $module ) { |
| 496 | 504 | /** |
| 497 | 505 | * Fires when a module is deactivated. |
| @@ -501,12 +509,23 @@ | ||
| 501 | 509 | * @param string $module Module slug. |
| 502 | 510 | */ |
| 503 | 511 | do_action( 'jetpack_pre_deactivate_module', $module ); |
| 504 | 512 | |
| 505 | - $active = $this->get_active(); | |
| 506 | - $new = array_filter( array_diff( $active, (array) $module ) ); | |
| 513 | + return $this->update_active( array_filter( array_diff( $this->get_saved_active(), (array) $module ) ) ); | |
| 514 | + } | |
| 507 | 515 | |
| 508 | - 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(); | |
| 509 | 528 | } |
| 510 | 529 | |
| 511 | 530 | /** |
| 512 | 531 | * Generate a module's path from its slug. |
| @@ -533,9 +552,9 @@ | ||
| 533 | 552 | * Also fires Action hooks for each newly activated and deactivated module. |
| 534 | 553 | * |
| 535 | 554 | * @param array $modules Array of active modules to be saved in options. |
| 536 | 555 | * |
| 537 | - * @return $success bool true for success, false for failure. | |
| 556 | + * @return bool $success true for success, false for failure. | |
| 538 | 557 | */ |
| 539 | 558 | public function update_active( $modules ) { |
| 540 | 559 | $current_modules = \Jetpack_Options::get_option( 'active_modules', array() ); |
| 541 | 560 | $active_modules = $this->get_active(); |