diff options
Diffstat (limited to 'plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php')
-rw-r--r-- | plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php | 101 |
1 files changed, 93 insertions, 8 deletions
diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php b/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php index e2f05c98..768ade58 100644 --- a/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/class-actions.php @@ -89,6 +89,8 @@ class Actions { /** * Initialize Sync for cron jobs, set up listeners for WordPress Actions, * and set up a shut-down action for sending actions to WordPress.com + * If dedicated Sync is enabled and this is a dedicated Sync request + * up an init action for sending actions to WordPress.com instead. * * @access public * @static @@ -99,6 +101,15 @@ class Actions { return; } + // If dedicated Sync is enabled and this is a dedicated Sync request, no need to + // initialize Sync for cron jobs, set up listeners or set up a shut-down action + // for sending actions to WordPress.com. + // We only need to set up an init action for sending actions to WordPress.com and exit early. + if ( Settings::is_dedicated_sync_enabled() && Dedicated_Sender::is_dedicated_sync_request() ) { + add_action( 'init', array( __CLASS__, 'add_dedicated_sync_sender_init' ), 90 ); + return; + } + if ( self::sync_via_cron_allowed() ) { self::init_sync_cron_jobs(); } elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { @@ -164,6 +175,22 @@ class Actions { } /** + * Immediately sends actions on init for the current dedicated Sync request. + * + * @access public + * @static + */ + public static function add_dedicated_sync_sender_init() { + if ( apply_filters( + 'jetpack_sync_sender_should_load', + true + ) ) { + self::initialize_sender(); + self::$sender->do_dedicated_sync_and_exit(); + } + } + + /** * Define JETPACK_SYNC_READ_ONLY constant if not defined. * This notifies sync to not run in shutdown if it was initialized during init. * @@ -194,6 +221,13 @@ class Actions { return self::sync_via_cron_allowed(); } + /** + * For now, if dedicated Sync is enabled we will always initialize send, even for GET and unauthenticated requests. + */ + if ( Settings::is_dedicated_sync_enabled() ) { + return true; + } + if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { return true; } @@ -379,14 +413,16 @@ class Actions { public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { $query_args = array( - 'sync' => '1', // Add an extra parameter to the URL so we can tell it's a sync action. - 'codec' => $codec_name, - 'timestamp' => $sent_timestamp, - 'queue' => $queue_id, - 'cd' => sprintf( '%.4f', $checkout_duration ), - 'pd' => sprintf( '%.4f', $preprocess_duration ), - 'queue_size' => $queue_size, - 'buffer_id' => $buffer_id, + 'sync' => '1', // Add an extra parameter to the URL so we can tell it's a sync action. + 'codec' => $codec_name, + 'timestamp' => $sent_timestamp, + 'queue' => $queue_id, + 'cd' => sprintf( '%.4f', $checkout_duration ), + 'pd' => sprintf( '%.4f', $preprocess_duration ), + 'queue_size' => $queue_size, + 'buffer_id' => $buffer_id, + // TODO this will be extended in the future. Might be good to extract in a separate method to support future entries too. + 'sync_flow_type' => Settings::is_dedicated_sync_enabled() ? 'dedicated' : 'default', ); $query_args['timeout'] = Settings::is_doing_cron() ? 30 : 20; @@ -437,6 +473,17 @@ class Actions { } } + // Enable/Disable Dedicated Sync flow via response headers. + $dedicated_sync_header = $rpc->get_response_header( 'Jetpack-Dedicated-Sync' ); + if ( false !== $dedicated_sync_header ) { + $dedicated_sync_enabled = 'on' === $dedicated_sync_header ? 1 : 0; + Settings::update_settings( + array( + 'dedicated_sync_enabled' => $dedicated_sync_enabled, + ) + ); + } + if ( ! $result ) { if ( false === $retry_after ) { // We received a non standard response from WP.com, lets backoff from sending requests for 1 minute. @@ -631,6 +678,14 @@ class Actions { break; } + /** + * Only try to sync once if Dedicated Sync is enabled. Dedicated Sync has its own requeueing mechanism + * that will re-run it if there are items in the queue at the end. + */ + if ( 'sync' === $type && $executions >= 1 && Settings::is_dedicated_sync_enabled() ) { + break; + } + $result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync(); // # of send actions performed. @@ -676,6 +731,36 @@ class Actions { } /** + * Initializes sync for Instant Search. + * + * @access public + * @static + */ + public static function initialize_search() { + if ( false === class_exists( 'Automattic\\Jetpack\\Search\\Module_Control' ) ) { + return; + } + $search_module = new \Automattic\Jetpack\Search\Module_Control(); + if ( $search_module->is_instant_search_enabled() ) { + add_filter( 'jetpack_sync_modules', array( __CLASS__, 'add_search_sync_module' ) ); + } + } + + /** + * Add Search updates to Sync Filters. + * + * @access public + * @static + * + * @param array $sync_modules The list of sync modules declared prior to this filter. + * @return array A list of sync modules that now includes Search's modules. + */ + public static function add_search_sync_module( $sync_modules ) { + $sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\Search'; + return $sync_modules; + } + + /** * Adds Woo's sync modules to existing modules for sending. * * @access public |