Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
32 / 36 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Module_Control | |
91.18% |
31 / 34 |
|
75.00% |
9 / 12 |
28.54 | |
0.00% |
0 / 1 |
| __construct | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
| is_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_instant_search_enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_swap_classic_to_inline_search | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| activate | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
7.04 | |||
| deactivate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| update_status | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| disable_instant_search | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| enable_instant_search | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| update_instant_search_status | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| update_swap_classic_to_inline_search | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_active_modules | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| search_filter_available_modules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack Search: Module_Control class |
| 4 | * |
| 5 | * @package automattic/jetpack-search |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Search; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | use Automattic\Jetpack\Modules; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use WP_Error; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * To get and set Search module settings |
| 21 | */ |
| 22 | class Module_Control { |
| 23 | /** |
| 24 | * Plan object |
| 25 | * |
| 26 | * @var Plan |
| 27 | */ |
| 28 | protected $plan; |
| 29 | |
| 30 | /** |
| 31 | * Connection_Manager object |
| 32 | * |
| 33 | * @var \Automattic\Jetpack\Connection\Manager |
| 34 | */ |
| 35 | protected $connection_manager; |
| 36 | |
| 37 | /** |
| 38 | * We use the same options as Jetpack the plugin to flag whether Search is active. |
| 39 | */ |
| 40 | const JETPACK_ACTIVE_MODULES_OPTION_KEY = 'active_modules'; |
| 41 | const JETPACK_SEARCH_MODULE_SLUG = 'search'; |
| 42 | const SEARCH_MODULE_INSTANT_SEARCH_OPTION_KEY = 'instant_search_enabled'; |
| 43 | const SEARCH_MODULE_SWAP_CLASSIC_TO_INLINE_OPTION_KEY = 'swap_classic_to_inline_search'; |
| 44 | |
| 45 | /** |
| 46 | * Contructor |
| 47 | * |
| 48 | * @param Plan|null $plan - Plan object. |
| 49 | * @param \Automattic\Jetpack\Connection\Manager|null $connection_manager - Connection_Manager object. |
| 50 | */ |
| 51 | public function __construct( $plan = null, $connection_manager = null ) { |
| 52 | $this->plan = $plan === null ? new Plan() : $plan; |
| 53 | $this->connection_manager = $connection_manager === null ? new Connection_Manager( Package::SLUG ) : $connection_manager; |
| 54 | if ( ! did_action( 'jetpack_search_module_control_initialized' ) ) { |
| 55 | add_filter( 'jetpack_get_available_standalone_modules', array( $this, 'search_filter_available_modules' ), 10, 1 ); |
| 56 | if ( Helper::is_wpcom() ) { |
| 57 | add_filter( 'jetpack_active_modules', array( $this, 'search_filter_available_modules' ), 10, 2 ); |
| 58 | } |
| 59 | /** |
| 60 | * Fires when the Automattic\Jetpack\Search\Module_Control is initialized for the first time. |
| 61 | */ |
| 62 | do_action( 'jetpack_search_module_control_initialized' ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns a boolean for whether of the module is enabled. |
| 68 | * |
| 69 | * @return bool |
| 70 | */ |
| 71 | public function is_active() { |
| 72 | return ( new Modules() )->is_active( self::JETPACK_SEARCH_MODULE_SLUG ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns a boolean for whether instant search is enabled. |
| 77 | * |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function is_instant_search_enabled() { |
| 81 | return (bool) $this->plan->supports_instant_search() && get_option( self::SEARCH_MODULE_INSTANT_SEARCH_OPTION_KEY ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns a boolean for whether new inline search is enabled. |
| 86 | * |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function is_swap_classic_to_inline_search() { |
| 90 | return (bool) get_option( self::SEARCH_MODULE_SWAP_CLASSIC_TO_INLINE_OPTION_KEY, false ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Activiate Search module |
| 95 | */ |
| 96 | public function activate() { |
| 97 | $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 98 | if ( ( new Status() )->is_offline_mode() ) { |
| 99 | return new WP_Error( 'site_offline', __( 'Jetpack Search cannot be used in offline mode.', 'jetpack-search-pkg' ) ); |
| 100 | } |
| 101 | if ( ! $is_wpcom && ! $this->connection_manager->is_connected() ) { |
| 102 | return new WP_Error( 'connection_required', __( 'Connect your site to use Jetpack Search.', 'jetpack-search-pkg' ) ); |
| 103 | } |
| 104 | if ( ! $this->plan->supports_search() ) { |
| 105 | return new WP_Error( 'not_supported', __( 'Your plan does not support Jetpack Search.', 'jetpack-search-pkg' ) ); |
| 106 | } |
| 107 | |
| 108 | $success = ( new Modules() )->activate( self::JETPACK_SEARCH_MODULE_SLUG, false, false ); |
| 109 | if ( false === $success ) { |
| 110 | return new WP_Error( 'not_updated', __( 'Setting not updated.', 'jetpack-search-pkg' ) ); |
| 111 | } |
| 112 | return $success; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Deactiviate Search module |
| 117 | */ |
| 118 | public function deactivate() { |
| 119 | $success = ( new Modules() )->deactivate( self::JETPACK_SEARCH_MODULE_SLUG ); |
| 120 | |
| 121 | $this->disable_instant_search(); |
| 122 | |
| 123 | return $success; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Update module status |
| 128 | * |
| 129 | * @param boolean $active - true to activate, false to deactivate. |
| 130 | */ |
| 131 | public function update_status( $active ) { |
| 132 | return $active ? $this->activate() : $this->deactivate(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Disable Instant Search Experience |
| 137 | */ |
| 138 | public function disable_instant_search() { |
| 139 | return update_option( self::SEARCH_MODULE_INSTANT_SEARCH_OPTION_KEY, false ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Enable Instant Search Experience |
| 144 | */ |
| 145 | public function enable_instant_search() { |
| 146 | if ( ! $this->is_active() ) { |
| 147 | return new WP_Error( 'search_module_inactive', __( 'Search module needs to be activated before enabling instant search.', 'jetpack-search-pkg' ) ); |
| 148 | } |
| 149 | if ( ! $this->plan->supports_instant_search() ) { |
| 150 | return new WP_Error( 'not_supported', __( 'Your plan does not support Instant Search.', 'jetpack-search-pkg' ) ); |
| 151 | } |
| 152 | return update_option( self::SEARCH_MODULE_INSTANT_SEARCH_OPTION_KEY, true ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Update instant search status |
| 157 | * |
| 158 | * @param boolean $enabled - true to enable, false to disable. |
| 159 | */ |
| 160 | public function update_instant_search_status( $enabled ) { |
| 161 | return $enabled ? $this->enable_instant_search() : $this->disable_instant_search(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Update setting indicating whether inline search should use newer 1.3 API. |
| 166 | * |
| 167 | * @param bool $swap_classic_to_inline_search - true to use Inline Search, false to use Classic Search. |
| 168 | */ |
| 169 | public function update_swap_classic_to_inline_search( bool $swap_classic_to_inline_search ) { |
| 170 | return update_option( self::SEARCH_MODULE_SWAP_CLASSIC_TO_INLINE_OPTION_KEY, $swap_classic_to_inline_search ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get a list of activated modules as an array of module slugs. |
| 175 | * |
| 176 | * @deprecated 0.12.3 |
| 177 | * @return Array $active_modules |
| 178 | */ |
| 179 | public function get_active_modules() { |
| 180 | _deprecated_function( |
| 181 | __METHOD__, |
| 182 | 'jetpack-search-0.12.3', |
| 183 | 'Automattic\\Jetpack\\Modules\\get_active' |
| 184 | ); |
| 185 | |
| 186 | return ( new Modules() )->get_active(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Adds search to the list of available modules |
| 191 | * |
| 192 | * @param array $modules The available modules. |
| 193 | * @return array |
| 194 | */ |
| 195 | public function search_filter_available_modules( $modules ) { |
| 196 | return array_merge( array( self::JETPACK_SEARCH_MODULE_SLUG ), $modules ); |
| 197 | } |
| 198 | } |