diff --git a/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php b/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php index 03e6a4d412887..98875ec08f209 100644 --- a/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php +++ b/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php @@ -48,8 +48,8 @@ protected function validate_queue( $query ) { return new WP_Error( 'invalid_queue', 'Queue name is required', 400 ); } - if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) { - return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 ); + if ( ! in_array( $query, array( 'sync', 'full_sync', 'immediate' ) ) ) { + return new WP_Error( 'invalid_queue', 'Queue name should be sync, full_sync or immediate', 400 ); } return $query; } @@ -191,6 +191,14 @@ protected function result() { $number_of_items = absint( $args['number_of_items'] ); + if ( 'immediate' === $queue_name ) { + return $this->immediate_full_sync_pull( $number_of_items ); + } + + return $this->queue_pull( $queue_name, $number_of_items, $args ); + } + + function queue_pull( $queue_name, $number_of_items, $args ){ $queue = new Queue( $queue_name ); if ( 0 === $queue->size() ) { @@ -233,6 +241,36 @@ protected function result() { ); } + public $items = []; + + public function jetpack_sync_send_data_listener() { + foreach ( func_get_args()[0] as $key => $item ) { + $this->items[ $key ] = $item; + } + } + + public function immediate_full_sync_pull( $number_of_items ) { + // try to give ourselves as much time as possible. + set_time_limit( 0 ); + + $original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' ); + $temp_send_data_cb = array( $this, 'jetpack_sync_send_data_listener' ); + + Sender::get_instance()->set_enqueue_wait_time( 0 ); + remove_filter( 'jetpack_sync_send_data', $original_send_data_cb ); + add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 ); + Sender::get_instance()->do_full_sync(); + remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb ); + add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 ); + + return array( + 'items' => $this->items, + 'codec' => Sender::get_instance()->get_codec()->name(), + 'sent_timestamp' => time(), + 'status' => Actions::get_sync_status(), + ); + } + protected function get_buffer( $queue, $number_of_items ) { $start = time(); $max_duration = 5; // this will try to get the buffer