← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-main.php
+162
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,162 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * This class hooks the main sync actions. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-sync | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Sync; | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Sync\Actions as Sync_Actions; | |
| 11 | +use Automattic\Jetpack\Sync\Queue\Queue_Storage_Table; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Jetpack Sync main class. | |
| 15 | + */ | |
| 16 | +class 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 | + // Set up package version hook. | |
| 45 | + add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' ); | |
| 46 | + | |
| 47 | + // Add the custom capabilities for managing modules | |
| 48 | + add_filter( 'map_meta_cap', array( __CLASS__, 'module_custom_caps' ), 10, 2 ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Sets the Module custom capabilities. | |
| 53 | + * | |
| 54 | + * @param string[] $caps Array of the user's capabilities. | |
| 55 | + * @param string $cap Capability name. | |
| 56 | + * @return string[] The user's capabilities, adjusted as necessary. | |
| 57 | + */ | |
| 58 | + public static function module_custom_caps( $caps, $cap ) { | |
| 59 | + switch ( $cap ) { | |
| 60 | + case 'jetpack_manage_modules': | |
| 61 | + case 'jetpack_activate_modules': | |
| 62 | + case 'jetpack_deactivate_modules': | |
| 63 | + $caps = array( 'manage_options' ); | |
| 64 | + break; | |
| 65 | + case 'jetpack_configure_modules': | |
| 66 | + $caps = array( 'manage_options' ); | |
| 67 | + break; | |
| 68 | + } | |
| 69 | + return $caps; | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Delete all sync related data on Identity Crisis disconnect. | |
| 74 | + */ | |
| 75 | + public static function on_jetpack_idc_disconnect() { | |
| 76 | + Sender::get_instance()->uninstall(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Sync cleanup on shutdown. | |
| 81 | + */ | |
| 82 | + public static function on_jetpack_site_disconnected() { | |
| 83 | + add_action( 'shutdown', array( __CLASS__, 'sync_cleanup' ), 10000 ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Delete all sync related data on Site disconnect / clean up custom table. | |
| 88 | + * Needs to happen on shutdown to prevent fatals. | |
| 89 | + */ | |
| 90 | + public static function sync_cleanup() { | |
| 91 | + Sender::get_instance()->uninstall(); | |
| 92 | + | |
| 93 | + $table_storage = new Queue_Storage_Table( 'test_queue' ); | |
| 94 | + $table_storage->drop_table(); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Sets the Sync data settings. | |
| 99 | + * | |
| 100 | + * @param array $data_settings An array containing the Sync data options. An empty array indicates that the default | |
| 101 | + * values will be used for all Sync data. | |
| 102 | + */ | |
| 103 | + public static function set_sync_data_options( $data_settings = array() ) { | |
| 104 | + ( new Data_Settings() )->add_settings_list( $data_settings ); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Initialize the main sync actions. | |
| 109 | + * | |
| 110 | + * @action plugins_loaded | |
| 111 | + */ | |
| 112 | + public static function on_plugins_loaded_early() { | |
| 113 | + /** | |
| 114 | + * Additional Sync modules can be carried out into their own packages and they | |
| 115 | + * will get their own config settings. | |
| 116 | + * | |
| 117 | + * For now additional modules are enabled based on whether the third party plugin | |
| 118 | + * class exists or not. | |
| 119 | + */ | |
| 120 | + Sync_Actions::initialize_search(); | |
| 121 | + Sync_Actions::initialize_woocommerce(); | |
| 122 | + Sync_Actions::initialize_wp_super_cache(); | |
| 123 | + | |
| 124 | + // We need to define this here so that it's hooked before `updating_jetpack_version` is called. | |
| 125 | + add_action( 'updating_jetpack_version', array( 'Automattic\\Jetpack\\Sync\\Actions', 'cleanup_on_upgrade' ), 10, 2 ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Runs after most of plugins_loaded hook functions have been run. | |
| 130 | + * | |
| 131 | + * @action plugins_loaded | |
| 132 | + */ | |
| 133 | + public static function on_plugins_loaded_late() { | |
| 134 | + /* | |
| 135 | + * Init after plugins loaded and before the `init` action. This helps with issues where plugins init | |
| 136 | + * with a high priority or sites that use alternate cron. | |
| 137 | + */ | |
| 138 | + Sync_Actions::init(); | |
| 139 | + | |
| 140 | + // Enable non-blocking Jetpack Sync flow. | |
| 141 | + $non_block_enabled = (bool) get_option( 'jetpack_sync_non_blocking', false ); | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Filters the option to enable non-blocking sync. | |
| 145 | + * | |
| 146 | + * Default value is false, filter to true to enable non-blocking mode which will have | |
| 147 | + * WP.com return early and use the sync/close endpoint to check-in processed items. | |
| 148 | + * | |
| 149 | + * @since 1.12.3 | |
| 150 | + * | |
| 151 | + * @param bool $enabled Should non-blocking flow be enabled. | |
| 152 | + */ | |
| 153 | + $filtered = (bool) apply_filters( 'jetpack_sync_non_blocking', $non_block_enabled ); | |
| 154 | + | |
| 155 | + if ( $non_block_enabled !== $filtered ) { | |
| 156 | + update_option( 'jetpack_sync_non_blocking', $filtered, false ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + // Initialize health-related hooks after plugins have loaded. | |
| 160 | + Health::init(); | |
| 161 | + } | |
| 162 | +} | |