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