summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php')
-rw-r--r--plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php134
1 files changed, 34 insertions, 100 deletions
diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php b/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php
index ea97b03c..87e76960 100644
--- a/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php
+++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/class-module-control.php
@@ -7,8 +7,7 @@
namespace Automattic\Jetpack\Search;
-use Automattic\Jetpack\Status;
-use Jetpack_Options;
+use Automattic\Jetpack\Modules;
use WP_Error;
/**
@@ -35,7 +34,15 @@ class Module_Control {
* @param Plan|null $plan - Plan object.
*/
public function __construct( $plan = null ) {
- $this->plan = is_null( $plan ) ? new Plan() : $plan;
+ $this->plan = $plan === null ? new Plan() : $plan;
+ if ( ! did_action( 'jetpack_search_module_control_initialized' ) ) {
+ add_filter( 'jetpack_get_available_standalone_modules', array( $this, 'search_filter_available_modules' ), 10, 1 );
+
+ /**
+ * Fires when the Automattic\Jetpack\Search\Module_Control is initialized for the first time.
+ */
+ do_action( 'jetpack_search_module_control_initialized' );
+ }
}
/**
@@ -44,7 +51,7 @@ class Module_Control {
* @return bool
*/
public function is_active() {
- return in_array( self::JETPACK_SEARCH_MODULE_SLUG, $this->get_active_modules(), true );
+ return ( new Modules() )->is_active( self::JETPACK_SEARCH_MODULE_SLUG );
}
/**
@@ -60,94 +67,18 @@ class Module_Control {
* Activiate Search module
*/
public function activate() {
- /**
- * Fires before a module is activated.
- *
- * @since 2.6.0
- *
- * @param string $module Module slug.
- * @param bool $exit Should we exit after the module has been activated. Default to true.
- * @param bool $redirect Should the user be redirected after module activation? Default to true.
- */
- do_action( 'jetpack_pre_activate_module', self::JETPACK_SEARCH_MODULE_SLUG );
-
- // If it's already active, then don't do it again.
- if ( $this->is_active() ) {
- return true;
- }
- // Not available for offline mode.
- $is_offline_mode = ( new Status() )->is_offline_mode();
- if ( $is_offline_mode ) {
- return new WP_Error( 'offline_mode', __( 'Search module can not be activated in offline mode.', 'jetpack-search-pkg' ) );
- }
- // Return false if no plan supports search.
if ( ! $this->plan->supports_search() ) {
return new WP_Error( 'not_supported', __( 'Your plan does not support Jetpack Search.', 'jetpack-search-pkg' ) );
}
- $active_modules = $this->get_active_modules();
- $active_modules[] = self::JETPACK_SEARCH_MODULE_SLUG;
-
- $success = Jetpack_Options::update_option( self::JETPACK_ACTIVE_MODULES_OPTION_KEY, $active_modules );
-
- /**
- * Fired after a module has been deactivated.
- *
- * @since 4.2.0
- *
- * @param string $module Module slug.
- * @param boolean $success whether the module was deactivated.
- */
- do_action( 'jetpack_activate_module', self::JETPACK_SEARCH_MODULE_SLUG, $success );
- /**
- * Fires when a module is deactivated.
- * The dynamic part of the filter, $module, is the module slug.
- *
- * @since 1.9.0
- *
- * @param string $module Module slug.
- */
- do_action( 'jetpack_activate_module_' . self::JETPACK_SEARCH_MODULE_SLUG );
-
- return $success;
+ return ( new Modules() )->activate( self::JETPACK_SEARCH_MODULE_SLUG, false, false );
}
/**
* Deactiviate Search module
*/
public function deactivate() {
- /**
- * Fires when a module is deactivated.
- *
- * @since 1.9.0
- *
- * @param string $module Module slug.
- */
- do_action( 'jetpack_pre_deactivate_module', self::JETPACK_SEARCH_MODULE_SLUG );
-
- $active_modules = $this->get_active_modules();
- $active_modules = array_values( array_diff( $active_modules, array( self::JETPACK_SEARCH_MODULE_SLUG ) ) );
-
- $success = Jetpack_Options::update_option( self::JETPACK_ACTIVE_MODULES_OPTION_KEY, $active_modules );
-
- /**
- * Fired after a module has been deactivated.
- *
- * @since 4.2.0
- *
- * @param string $module Module slug.
- * @param boolean $success whether the module was deactivated.
- */
- do_action( 'jetpack_deactivate_module', self::JETPACK_SEARCH_MODULE_SLUG, $success );
- /**
- * Fires when a module is deactivated.
- * The dynamic part of the filter, $module, is the module slug.
- *
- * @since 1.9.0
- *
- * @param string $module Module slug.
- */
- do_action( 'jetpack_deactivate_module_' . self::JETPACK_SEARCH_MODULE_SLUG );
+ $success = ( new Modules() )->deactivate( self::JETPACK_SEARCH_MODULE_SLUG );
$this->disable_instant_search();
@@ -177,6 +108,9 @@ class Module_Control {
if ( ! $this->is_active() ) {
return new WP_Error( 'search_module_inactive', __( 'Search module needs to be activated before enabling instant search.', 'jetpack-search-pkg' ) );
}
+ if ( ! $this->plan->supports_instant_search() ) {
+ return new WP_Error( 'not_supported', __( 'Your plan does not support Instant Search.', 'jetpack-search-pkg' ) );
+ }
return update_option( self::SEARCH_MODULE_INSTANT_SEARCH_OPTION_KEY, true );
}
@@ -191,27 +125,27 @@ class Module_Control {
/**
* Get a list of activated modules as an array of module slugs.
+ *
+ * @deprecated 0.12.3
+ * @return Array $active_modules
*/
public function get_active_modules() {
- $active_modules = Jetpack_Options::get_option( self::JETPACK_ACTIVE_MODULES_OPTION_KEY );
+ _deprecated_function(
+ __METHOD__,
+ 'jetpack-search-0.12.3',
+ 'Automattic\\Jetpack\\Modules\\get_active'
+ );
- if ( ! is_array( $active_modules ) ) {
- $active_modules = array();
- }
-
- /**
- * Allow filtering of the active modules.
- *
- * Gives theme and plugin developers the power to alter the modules that
- * are activated on the fly.
- *
- * @since 5.8.0
- *
- * @param array $active Array of active module slugs.
- */
- $active_modules = apply_filters( 'jetpack_active_modules', $active_modules );
-
- return array_unique( $active_modules );
+ return ( new Modules() )->get_active();
}
+ /**
+ * Adds search to the list of available modules
+ *
+ * @param array $modules The available modules.
+ * @return array
+ */
+ public function search_filter_available_modules( $modules ) {
+ return array_merge( array( self::JETPACK_SEARCH_MODULE_SLUG ), $modules );
+ }
}