Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Search_Plugin | |
0.00% |
0 / 41 |
|
0.00% |
0 / 9 |
210 | |
0.00% |
0 / 1 |
| bootstrap | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| plugin_page_add_links | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| load_compatibility_files | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| configure_packages | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
| initialize_other_packages | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| handle_plugin_activation | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| filter_available_modules_add_stats | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| activate_stats_module_on_site_registered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| activate_stats_module_on_deactivate_jetpack | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Put your classes in this `src` folder! |
| 4 | * |
| 5 | * @package automattic/jetpack-search-plugin |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Search_Plugin; |
| 9 | |
| 10 | use Automattic\Jetpack\Config; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication; |
| 13 | use Automattic\Jetpack\Modules; |
| 14 | use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer; |
| 15 | use Automattic\Jetpack\Search\Module_Control as Search_Module_Control; |
| 16 | |
| 17 | /** |
| 18 | * Class to bootstrap Jetpack Search Plugin |
| 19 | * |
| 20 | * @package automattic/jetpack-search |
| 21 | */ |
| 22 | class Jetpack_Search_Plugin { |
| 23 | /** |
| 24 | * Register hooks to initialize the plugin |
| 25 | */ |
| 26 | public static function bootstrap() { |
| 27 | add_action( 'plugins_loaded', array( self::class, 'load_compatibility_files' ), 1 ); |
| 28 | add_action( 'plugins_loaded', array( self::class, 'configure_packages' ), 1 ); |
| 29 | add_action( 'plugins_loaded', array( self::class, 'initialize_other_packages' ) ); |
| 30 | add_action( 'activated_plugin', array( self::class, 'handle_plugin_activation' ) ); |
| 31 | add_action( 'jetpack_site_registered', array( self::class, 'activate_stats_module_on_site_registered' ) ); |
| 32 | /** |
| 33 | * In case the Jetpack plugin is deactivated, make sure to re-activate the Stats module, as the |
| 34 | * module toggle, available in the Jetpack plugin no longer needs to be respected. |
| 35 | * |
| 36 | * @todo Consider removing this action if a Stats module toggle is implemented in the Jetpack Search Plugin. |
| 37 | */ |
| 38 | add_action( 'deactivate_jetpack/jetpack.php', array( self::class, 'activate_stats_module_on_deactivate_jetpack' ) ); |
| 39 | add_filter( 'plugin_action_links_' . JETPACK_SEARCH_PLUGIN__FILE_RELATIVE_PATH, array( self::class, 'plugin_page_add_links' ) ); |
| 40 | add_filter( 'jetpack_get_available_standalone_modules', array( self::class, 'filter_available_modules_add_stats' ), 10, 1 ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add settings and My Jetpack links to plugin actions |
| 45 | * |
| 46 | * @param array $links the array of links. |
| 47 | */ |
| 48 | public static function plugin_page_add_links( $links ) { |
| 49 | $settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-search' ) . '">' . esc_html__( 'Settings', 'jetpack-search' ) . '</a>'; |
| 50 | array_unshift( $links, $settings_link ); |
| 51 | |
| 52 | return $links; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Extra tweaks to make Jetpack Search play well with others. |
| 57 | */ |
| 58 | public static function load_compatibility_files() { |
| 59 | if ( class_exists( 'Jetpack' ) ) { |
| 60 | require_once JETPACK_SEARCH_PLUGIN__DIR . '/compatibility/jetpack.php'; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Configure packages controlled by the `Config` class. |
| 66 | * |
| 67 | * Note: the function only configures the packages, but doesn't initialize them. |
| 68 | * The actual initialization is done on 'plugins_loaded' priority 2, which is the |
| 69 | * reason the function is hooked on priority 1. |
| 70 | */ |
| 71 | public static function configure_packages() { |
| 72 | $config = new Config(); |
| 73 | // Connection package. |
| 74 | $config->ensure( |
| 75 | 'connection', |
| 76 | array( |
| 77 | 'slug' => JETPACK_SEARCH_PLUGIN__SLUG, |
| 78 | 'name' => 'Jetpack Search', |
| 79 | 'url_info' => 'https://jetpack.com/upgrade/search/', |
| 80 | ) |
| 81 | ); |
| 82 | // Sync package. |
| 83 | $config->ensure( 'sync' ); |
| 84 | // Identity crisis package. |
| 85 | $config->ensure( 'identity_crisis' ); |
| 86 | // Search package. |
| 87 | $config->ensure( 'search' ); |
| 88 | // Stats package. |
| 89 | $config->ensure( 'stats' ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Initialize packages not controlled by the `Config` class. |
| 94 | */ |
| 95 | public static function initialize_other_packages() { |
| 96 | // Set up the REST authentication hooks. |
| 97 | Connection_Rest_Authentication::init(); |
| 98 | // Initialize My Jetpack. |
| 99 | My_Jetpack_Initializer::init(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Redirects to the Search Dashboard when the Search plugin is activated. |
| 104 | * |
| 105 | * @param string $plugin Path to the plugin file relative to the plugins directory. |
| 106 | */ |
| 107 | public static function handle_plugin_activation( $plugin ) { |
| 108 | // If site is already connected, enable the search and stats modules and enable instant search. |
| 109 | if ( ( new Connection_Manager() )->is_connected() ) { |
| 110 | $controller = new Search_Module_Control(); |
| 111 | $activation_result = $controller->activate(); |
| 112 | |
| 113 | if ( true === $activation_result ) { |
| 114 | $controller->enable_instant_search(); |
| 115 | } |
| 116 | ( new Modules() )->activate( 'stats', false, false ); |
| 117 | } |
| 118 | |
| 119 | if ( |
| 120 | JETPACK_SEARCH_PLUGIN__FILE_RELATIVE_PATH === $plugin && |
| 121 | ( new \Automattic\Jetpack\Paths() )->is_current_request_activating_plugin_from_plugins_screen( JETPACK_SEARCH_PLUGIN__FILE_RELATIVE_PATH ) |
| 122 | ) { |
| 123 | wp_safe_redirect( esc_url( admin_url( 'admin.php?page=jetpack-search' ) ) ); |
| 124 | exit( 0 ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Adds stats module to the list of available modules. |
| 130 | * |
| 131 | * @param array $modules The available modules. |
| 132 | * @return array |
| 133 | */ |
| 134 | public static function filter_available_modules_add_stats( $modules ) { |
| 135 | return array_merge( array( 'stats' ), $modules ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Fires on the `jetpack_site_registered` action and activates stats module. |
| 140 | */ |
| 141 | public static function activate_stats_module_on_site_registered() { |
| 142 | ( new Modules() )->activate( 'stats', false, false ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Fires on the `deactivate_jetpack` action and activates stats module. |
| 147 | * |
| 148 | * @todo Consider removing this action if a Stats module toggle is implemented in the Jetpack Search Plugin. |
| 149 | */ |
| 150 | public static function activate_stats_module_on_deactivate_jetpack() { |
| 151 | ( new Modules() )->activate( 'stats', false, false ); |
| 152 | } |
| 153 | } |