PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | jetpack_vendor/automattic/jetpack-sync/src/class-rest-endpoints.php +140 -14 12.9.5 → 16.3-beta View file →
@@ -57,8 +57,13 @@
57 57 'description' => __( 'Comment IDs to include in Full Sync', 'jetpack-sync' ),
58 58 'type' => 'array',
59 59 'required' => false,
60 60 ),
61 + 'context' => array(
62 + 'description' => __( 'Context for the Full Sync', 'jetpack-sync' ),
63 + 'type' => 'string',
64 + 'required' => false,
65 + ),
61 66 ),
62 67 )
63 68 );
64 69
@@ -71,9 +76,9 @@
71 76 'callback' => __CLASS__ . '::sync_status',
72 77 'permission_callback' => __CLASS__ . '::verify_default_permissions',
73 78 'args' => array(
74 79 'fields' => array(
75 - 'description' => __( 'Comma seperated list of additional fields that should be included in status.', 'jetpack-sync' ),
80 + 'description' => __( 'Comma-separated list of additional fields that should be included in status.', 'jetpack-sync' ),
76 81 'type' => 'string',
77 82 'required' => false,
78 83 ),
79 84 ),
@@ -169,8 +174,45 @@
169 174 array(
170 175 'methods' => WP_REST_Server::EDITABLE,
171 176 'callback' => __CLASS__ . '::checkout',
172 177 'permission_callback' => __CLASS__ . '::verify_default_permissions',
178 + 'args' => array(
179 + 'queue' => array(
180 + 'description' => __( 'Name of Sync queue.', 'jetpack-sync' ),
181 + 'type' => 'string',
182 + 'required' => false,
183 + ),
184 + 'number_of_items' => array(
185 + 'description' => __( 'Number of items to checkout from the queue.', 'jetpack-sync' ),
186 + 'type' => 'integer',
187 + 'required' => false,
188 + ),
189 + 'pop' => array(
190 + 'description' => __( 'Pop items from the queue instead of checking out.', 'jetpack-sync' ),
191 + 'type' => 'boolean',
192 + 'required' => false,
193 + 'sanitize_callback' => 'rest_sanitize_boolean',
194 + ),
195 + 'force' => array(
196 + 'description' => __( 'Force unlock the queue before checkout.', 'jetpack-sync' ),
197 + 'type' => 'boolean',
198 + 'required' => false,
199 + 'sanitize_callback' => 'rest_sanitize_boolean',
200 + ),
201 + 'encode' => array(
202 + 'description' => __( 'Encode the items before sending.', 'jetpack-sync' ),
203 + 'type' => 'boolean',
204 + 'required' => false,
205 + 'default' => true,
206 + 'sanitize_callback' => 'rest_sanitize_boolean',
207 + ),
208 + 'use_memory_limit' => array(
209 + 'description' => __( 'Use memory-based checkout instead of fixed item count.', 'jetpack-sync' ),
210 + 'type' => 'boolean',
211 + 'required' => false,
212 + 'sanitize_callback' => 'rest_sanitize_boolean',
213 + ),
214 + ),
173 215 )
174 216 );
175 217
176 218 // Checkin Sync Objects.
@@ -287,9 +329,9 @@
287 329 'type' => 'string',
288 330 'required' => false,
289 331 ),
290 332 'only_range_edges' => array(
291 - 'description' => __( 'Should only range endges be returned', 'jetpack-sync' ),
333 + 'description' => __( 'Should only range edges be returned', 'jetpack-sync' ),
292 334 'type' => 'boolean',
293 335 'required' => false,
294 336 ),
295 337 'detailed_drilldown' => array(
@@ -326,8 +368,19 @@
326 368 'callback' => __CLASS__ . '::reset_locks',
327 369 'permission_callback' => __CLASS__ . '::verify_default_permissions',
328 370 )
329 371 );
372 +
373 + // Clear Sync queue.
374 + register_rest_route(
375 + 'jetpack/v4',
376 + '/sync/clear-queue',
377 + array(
378 + 'methods' => WP_REST_Server::EDITABLE,
379 + 'callback' => __CLASS__ . '::clear_queue',
380 + 'permission_callback' => __CLASS__ . '::verify_default_permissions',
381 + )
382 + );
330 383 }
331 384
332 385 /**
333 386 * Trigger a Full Sync of specified modules.
@@ -362,11 +415,13 @@
362 415 if ( empty( $modules ) ) {
363 416 $modules = null;
364 417 }
365 418
419 + $context = $request->get_param( 'context' );
420 +
366 421 return rest_ensure_response(
367 422 array(
368 - 'scheduled' => Actions::do_full_sync( $modules ),
423 + 'scheduled' => Actions::do_full_sync( $modules, $context ),
369 424 )
370 425 );
371 426 }
372 427
@@ -462,23 +517,55 @@
462 517
463 518 /**
464 519 * Update Sync health.
465 520 *
521 + * IN_SYNC is only set if the incremental queue is within size and lag limits.
522 + *
466 523 * @since 1.23.1
467 524 *
468 525 * @param \WP_REST_Request $request The request sent to the WP REST API.
469 526 *
470 - * @return \WP_REST_Response
527 + * @return \WP_REST_Response|WP_Error
471 528 */
472 529 public static function sync_health( $request ) {
530 + $requested_status = $request->get_param( 'status' );
473 531
474 - switch ( $request->get_param( 'status' ) ) {
532 + switch ( $requested_status ) {
475 533 case Health::STATUS_IN_SYNC:
534 + // Only allow setting IN_SYNC if the incremental queue is healthy.
535 + $sync_queue = Listener::get_instance()->get_sync_queue();
536 + $queue_size = $sync_queue->size();
537 + $queue_lag = $sync_queue->lag();
538 + $queue_healthy = Health::is_queue_healthy(
539 + $queue_size,
540 + $queue_lag,
541 + Settings::get_setting( 'max_queue_size' ),
542 + Settings::get_setting( 'max_queue_lag' )
543 + );
544 + if ( ! $queue_healthy ) {
545 + Health::update_status( Health::STATUS_OUT_OF_SYNC );
546 + return rest_ensure_response(
547 + array(
548 + 'success' => Health::get_status(),
549 + 'message' => 'Sync queue is not healthy (size and lag over limit). Status not set to in_sync.',
550 + 'queue_size' => $queue_size,
551 + 'queue_lag' => $queue_lag,
552 + )
553 + );
554 + }
555 + Health::update_status( $requested_status );
556 + break;
476 557 case Health::STATUS_OUT_OF_SYNC:
477 - Health::update_status( $request->get_param( 'status' ) );
558 + Health::update_status( $requested_status );
478 559 break;
479 560 default:
480 - return new WP_Error( 'invalid_status', 'Invalid Sync Status Provided.' );
561 + return new WP_Error(
562 + 'invalid_status',
563 + 'Invalid Sync Status Provided.',
564 + array(
565 + 'status' => 400,
566 + )
567 + );
481 568 }
482 569
483 570 // re-fetch so we see what's really being stored.
484 571 return rest_ensure_response(
@@ -609,21 +696,38 @@
609 696 if ( is_wp_error( $queue_name ) ) {
610 697 return $queue_name;
611 698 }
612 699
613 - $number_of_items = $args['number_of_items'];
614 - if ( $number_of_items < 1 || $number_of_items > 100 ) {
615 - return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 );
700 + $use_memory_limit = ! empty( $args['use_memory_limit'] );
701 +
702 + if ( $use_memory_limit && ! empty( $args['pop'] ) ) {
703 + return new WP_Error( 'invalid_args', 'pop cannot be used with use_memory_limit', 400 );
616 704 }
617 705
706 + if ( ! $use_memory_limit ) {
707 + if ( empty( $args['number_of_items'] ) || $args['number_of_items'] < 1 || $args['number_of_items'] > 100 ) {
708 + return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and up to 100', 400 );
709 + }
710 + }
711 +
618 712 // REST Sender.
619 713 $sender = new REST_Sender();
620 714
621 715 if ( 'immediate' === $queue_name ) {
622 - return rest_ensure_response( $sender->immediate_full_sync_pull( $number_of_items ) );
716 + return rest_ensure_response( $sender->immediate_full_sync_pull() );
623 717 }
624 718
625 - return rest_ensure_response( $sender->queue_pull( $queue_name, $number_of_items, $args ) );
719 + $number_of_items = $use_memory_limit ? null : $args['number_of_items'];
720 + $response = $sender->queue_pull( $queue_name, $number_of_items, $args );
721 + // Disable sending while pulling.
722 + if ( ! is_wp_error( $response ) ) {
723 + set_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME, time(), Sender::TEMP_SYNC_DISABLE_TRANSIENT_EXPIRY );
724 + } elseif ( 'queue_size' === $response->get_error_code() ) {
725 + // Re-enable sending if the queue is empty.
726 + delete_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME );
727 + }
728 +
729 + return rest_ensure_response( $response );
626 730 }
627 731
628 732 /**
629 733 * Unlock a Sync queue.
@@ -678,9 +782,9 @@
678 782 return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
679 783 }
680 784
681 785 // Limit to A-Z,a-z,0-9,_,- .
682 - $request_body['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
786 + $request_body['buffer_id'] = preg_replace( '/[^A-Za-z0-9\-_\.]/', '', $request_body['buffer_id'] );
683 787 $request_body['item_ids'] = array_filter( array_map( array( 'Automattic\Jetpack\Sync\REST_Endpoints', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
684 788
685 789 $queue = new Queue( $queue_name );
686 790
@@ -688,8 +792,9 @@
688 792
689 793 // Update Full Sync Status if queue is "full_sync".
690 794 if ( 'full_sync' === $queue_name ) {
691 795 $full_sync_module = Modules::get_module( 'full-sync' );
796 + '@phan-var Modules\Full_Sync_Immediately|Modules\Full_Sync $full_sync_module';
692 797 $full_sync_module->update_sent_progress_action( $items );
693 798 }
694 799
695 800 $buffer = new Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
@@ -753,9 +858,9 @@
753 858 *
754 859 * @see Actions::init
755 860 * @see Sender::do_dedicated_sync_and_exit
756 861 *
757 - * @since $$next_version$$
862 + * @since 1.34.0
758 863 *
759 864 * @return \WP_REST_Response
760 865 */
761 866 public static function spawn_sync() {
@@ -784,8 +889,29 @@
784 889 * @return \WP_REST_Response
785 890 */
786 891 public static function reset_locks() {
787 892 Actions::reset_sync_locks();
893 +
894 + return rest_ensure_response(
895 + array(
896 + 'success' => true,
897 + )
898 + );
899 + }
900 +
901 + /**
902 + * Clear the Sync queue.
903 + *
904 + * @since 4.30.0
905 + *
906 + * @return \WP_REST_Response
907 + */
908 + public static function clear_queue() {
909 + $queue = new Queue( 'sync' );
910 + $queue->reset();
911 +
912 + // Re-enable sending in case it was temporarily disabled during a pull.
913 + delete_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME );
788 914
789 915 return rest_ensure_response(
790 916 array(
791 917 'success' => true,