Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.89% covered (danger)
38.89%
14 / 36
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Main
38.89% covered (danger)
38.89%
14 / 36
0.00% covered (danger)
0.00%
0 / 8
58.73
0.00% covered (danger)
0.00%
0 / 1
 configure
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
2.02
 module_custom_caps
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
7.19
 on_jetpack_idc_disconnect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 on_jetpack_site_disconnected
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sync_cleanup
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 set_sync_data_options
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 on_plugins_loaded_early
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 on_plugins_loaded_late
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This class hooks the main sync actions.
4 *
5 * @package automattic/jetpack-sync
6 */
7
8namespace Automattic\Jetpack\Sync;
9
10use Automattic\Jetpack\Sync\Actions as Sync_Actions;
11use Automattic\Jetpack\Sync\Queue\Queue_Storage_Table;
12
13/**
14 * Jetpack Sync main class.
15 */
16class Main {
17
18    /**
19     * Sets up event handlers for the Sync package. Is used from the Config package.
20     *
21     * @action plugins_loaded
22     */
23    public static function configure() {
24        // Activity Log custom events must register even when Sync is disabled, e.g. on WordPress.com Simple sites.
25        Activity_Log_Event::init();
26
27        if ( Actions::sync_allowed() ) {
28            add_action( 'plugins_loaded', array( __CLASS__, 'on_plugins_loaded_early' ), 5 );
29            add_action( 'plugins_loaded', array( __CLASS__, 'on_plugins_loaded_late' ), 90 );
30        }
31
32        // Add REST endpoints.
33        add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Sync\\REST_Endpoints', 'initialize_rest_api' ) );
34
35        // Add IDC disconnect action.
36        add_action( 'jetpack_idc_disconnect', array( __CLASS__, 'on_jetpack_idc_disconnect' ), 100 );
37
38        // Any hooks below are special cases that need to be declared even if Sync is not allowed.
39        add_action( 'jetpack_site_registered', array( 'Automattic\\Jetpack\\Sync\\Actions', 'do_initial_sync' ), 10, 0 );
40
41        // Sync clean up, when Jetpack is disconnected.
42        add_action( 'jetpack_site_disconnected', array( __CLASS__, 'on_jetpack_site_disconnected' ), 1000 );
43
44        add_filter( 'jetpack_connection_debug_data', array( Debug_Data::class, 'add_fields' ) );
45
46        // Set up package version hook.
47        add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
48
49        // Add the custom capabilities for managing modules
50        add_filter( 'map_meta_cap', array( __CLASS__, 'module_custom_caps' ), 10, 2 );
51    }
52
53    /**
54     * Sets the Module custom capabilities.
55     *
56     * @param  string[] $caps Array of the user's capabilities.
57     * @param  string   $cap  Capability name.
58     * @return string[] The user's capabilities, adjusted as necessary.
59     */
60    public static function module_custom_caps( $caps, $cap ) {
61        switch ( $cap ) {
62            case 'jetpack_manage_modules':
63            case 'jetpack_activate_modules':
64            case 'jetpack_deactivate_modules':
65                $caps = array( 'manage_options' );
66                break;
67            case 'jetpack_configure_modules':
68                $caps = array( 'manage_options' );
69                break;
70        }
71        return $caps;
72    }
73
74    /**
75     * Delete all sync related data on Identity Crisis disconnect.
76     */
77    public static function on_jetpack_idc_disconnect() {
78        Sender::get_instance()->uninstall();
79    }
80
81    /**
82     * Sync cleanup on shutdown.
83     */
84    public static function on_jetpack_site_disconnected() {
85        add_action( 'shutdown', array( __CLASS__, 'sync_cleanup' ), 10000 );
86    }
87
88    /**
89     * Delete all sync related data on Site disconnect / clean up custom table.
90     * Needs to happen on shutdown to prevent fatals.
91     */
92    public static function sync_cleanup() {
93        Sender::get_instance()->uninstall();
94
95        $table_storage = new Queue_Storage_Table( 'test_queue' );
96        $table_storage->drop_table();
97    }
98
99    /**
100     * Sets the Sync data settings.
101     *
102     * @param array $data_settings An array containing the Sync data options. An empty array indicates that the default
103     *                             values will be used for all Sync data.
104     */
105    public static function set_sync_data_options( $data_settings = array() ) {
106        ( new Data_Settings() )->add_settings_list( $data_settings );
107    }
108
109    /**
110     * Initialize the main sync actions.
111     *
112     * @action plugins_loaded
113     */
114    public static function on_plugins_loaded_early() {
115        /**
116         * Additional Sync modules can be carried out into their own packages and they
117         * will get their own config settings.
118         *
119         * For now additional modules are enabled based on whether the third party plugin
120         * class exists or not.
121         */
122        Sync_Actions::initialize_search();
123        Sync_Actions::initialize_woocommerce();
124        Sync_Actions::initialize_wp_super_cache();
125
126        // We need to define this here so that it's hooked before `updating_jetpack_version` is called.
127        add_action( 'updating_jetpack_version', array( 'Automattic\\Jetpack\\Sync\\Actions', 'cleanup_on_upgrade' ), 10, 2 );
128    }
129
130    /**
131     * Runs after most of plugins_loaded hook functions have been run.
132     *
133     * @action plugins_loaded
134     */
135    public static function on_plugins_loaded_late() {
136        /*
137         * Init after plugins loaded and before the `init` action. This helps with issues where plugins init
138         * with a high priority or sites that use alternate cron.
139         */
140        Sync_Actions::init();
141
142        // Enable non-blocking Jetpack Sync flow.
143        $non_block_enabled = (bool) get_option( 'jetpack_sync_non_blocking', false );
144
145        /**
146         * Filters the option to enable non-blocking sync.
147         *
148         * Default value is false, filter to true to enable non-blocking mode which will have
149         * WP.com return early and use the sync/close endpoint to check-in processed items.
150         *
151         * @since 1.12.3
152         *
153         * @param bool $enabled Should non-blocking flow be enabled.
154         */
155        $filtered = (bool) apply_filters( 'jetpack_sync_non_blocking', $non_block_enabled );
156
157        if ( $non_block_enabled !== $filtered ) {
158            update_option( 'jetpack_sync_non_blocking', $filtered, false );
159        }
160
161        // Initialize health-related hooks after plugins have loaded.
162        Health::init();
163    }
164}