PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.3.2
Jetpack – WP Security, Backup, Speed, & Growth v12.3.2
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 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / jetpack_vendor / automattic / jetpack-sync / src / class-rest-endpoints.php
class-rest-endpoints.php
873 lines 22.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sync package.
4 *
5 * @package automattic/jetpack-sync
6 */
7
8 namespace Automattic\Jetpack\Sync;
9
10 use Automattic\Jetpack\Connection\Rest_Authentication;
11 use WP_Error;
12 use WP_REST_Server;
13
14 /**
15 * This class will handle Sync v4 REST Endpoints.
16 *
17 * @since 1.23.1
18 */
19 class REST_Endpoints {
20
21 /**
22 * Items pending send.
23 *
24 * @var array
25 */
26 public $items = array();
27
28 /**
29 * Initialize REST routes.
30 */
31 public static function initialize_rest_api() {
32
33 // Request a Full Sync.
34 register_rest_route(
35 'jetpack/v4',
36 '/sync/full-sync',
37 array(
38 'methods' => WP_REST_Server::EDITABLE,
39 'callback' => __CLASS__ . '::full_sync_start',
40 'permission_callback' => __CLASS__ . '::verify_default_permissions',
41 'args' => array(
42 'modules' => array(
43 'description' => __( 'Data Modules that should be included in Full Sync', 'jetpack-sync' ),
44 'type' => 'array',
45 'required' => false,
46 ),
47 'users' => array(
48 'description' => __( 'User IDs to include in Full Sync or "initial"', 'jetpack-sync' ),
49 'required' => false,
50 ),
51 'posts' => array(
52 'description' => __( 'Post IDs to include in Full Sync', 'jetpack-sync' ),
53 'type' => 'array',
54 'required' => false,
55 ),
56 'comments' => array(
57 'description' => __( 'Comment IDs to include in Full Sync', 'jetpack-sync' ),
58 'type' => 'array',
59 'required' => false,
60 ),
61 ),
62 )
63 );
64
65 // Obtain Sync status.
66 register_rest_route(
67 'jetpack/v4',
68 '/sync/status',
69 array(
70 'methods' => WP_REST_Server::READABLE,
71 'callback' => __CLASS__ . '::sync_status',
72 'permission_callback' => __CLASS__ . '::verify_default_permissions',
73 'args' => array(
74 'fields' => array(
75 'description' => __( 'Comma seperated list of additional fields that should be included in status.', 'jetpack-sync' ),
76 'type' => 'string',
77 'required' => false,
78 ),
79 ),
80 )
81 );
82
83 // Update Sync health status.
84 register_rest_route(
85 'jetpack/v4',
86 '/sync/health',
87 array(
88 'methods' => WP_REST_Server::EDITABLE,
89 'callback' => __CLASS__ . '::sync_health',
90 'permission_callback' => __CLASS__ . '::verify_default_permissions',
91 'args' => array(
92 'status' => array(
93 'description' => __( 'New Sync health status', 'jetpack-sync' ),
94 'type' => 'string',
95 'required' => true,
96 ),
97 ),
98 )
99 );
100
101 // Obtain Sync settings.
102 register_rest_route(
103 'jetpack/v4',
104 '/sync/settings',
105 array(
106 array(
107 'methods' => WP_REST_Server::READABLE,
108 'callback' => __CLASS__ . '::get_sync_settings',
109 'permission_callback' => __CLASS__ . '::verify_default_permissions',
110 ),
111 array(
112 'methods' => WP_REST_Server::EDITABLE,
113 'callback' => __CLASS__ . '::update_sync_settings',
114 'permission_callback' => __CLASS__ . '::verify_default_permissions',
115 ),
116 )
117 );
118
119 // Retrieve Sync Object(s).
120 register_rest_route(
121 'jetpack/v4',
122 '/sync/object',
123 array(
124 'methods' => WP_REST_Server::ALLMETHODS,
125 'callback' => __CLASS__ . '::get_sync_objects',
126 'permission_callback' => __CLASS__ . '::verify_default_permissions',
127 'args' => array(
128 'module_name' => array(
129 'description' => __( 'Name of Sync module', 'jetpack-sync' ),
130 'type' => 'string',
131 'required' => false,
132 ),
133 'object_type' => array(
134 'description' => __( 'Object Type', 'jetpack-sync' ),
135 'type' => 'string',
136 'required' => false,
137 ),
138 'object_ids' => array(
139 'description' => __( 'Objects Identifiers', 'jetpack-sync' ),
140 'type' => 'array',
141 'required' => false,
142 ),
143 ),
144 )
145 );
146
147 // Retrieve Sync Object(s).
148 register_rest_route(
149 'jetpack/v4',
150 '/sync/now',
151 array(
152 'methods' => WP_REST_Server::EDITABLE,
153 'callback' => __CLASS__ . '::do_sync',
154 'permission_callback' => __CLASS__ . '::verify_default_permissions',
155 'args' => array(
156 'queue' => array(
157 'description' => __( 'Name of Sync queue.', 'jetpack-sync' ),
158 'type' => 'string',
159 'required' => true,
160 ),
161 ),
162 )
163 );
164
165 // Checkout Sync Objects.
166 register_rest_route(
167 'jetpack/v4',
168 '/sync/checkout',
169 array(
170 'methods' => WP_REST_Server::EDITABLE,
171 'callback' => __CLASS__ . '::checkout',
172 'permission_callback' => __CLASS__ . '::verify_default_permissions',
173 )
174 );
175
176 // Checkin Sync Objects.
177 register_rest_route(
178 'jetpack/v4',
179 '/sync/close',
180 array(
181 'methods' => WP_REST_Server::EDITABLE,
182 'callback' => __CLASS__ . '::close',
183 'permission_callback' => __CLASS__ . '::verify_default_permissions',
184 )
185 );
186
187 // Unlock Sync Queue.
188 register_rest_route(
189 'jetpack/v4',
190 '/sync/unlock',
191 array(
192 'methods' => WP_REST_Server::EDITABLE,
193 'callback' => __CLASS__ . '::unlock_queue',
194 'permission_callback' => __CLASS__ . '::verify_default_permissions',
195 'args' => array(
196 'queue' => array(
197 'description' => __( 'Name of Sync queue.', 'jetpack-sync' ),
198 'type' => 'string',
199 'required' => true,
200 ),
201 ),
202 )
203 );
204
205 // Retrieve range of Object Ids.
206 register_rest_route(
207 'jetpack/v4',
208 '/sync/object-id-range',
209 array(
210 'methods' => WP_REST_Server::READABLE,
211 'callback' => __CLASS__ . '::get_object_id_range',
212 'permission_callback' => __CLASS__ . '::verify_default_permissions',
213 'args' => array(
214 'sync_module' => array(
215 'description' => __( 'Name of Sync module.', 'jetpack-sync' ),
216 'type' => 'string',
217 'required' => true,
218 ),
219 'batch_size' => array(
220 'description' => __( 'Size of batches', 'jetpack-sync' ),
221 'type' => 'int',
222 'required' => true,
223 ),
224 ),
225 )
226 );
227
228 // Obtain table checksums.
229 register_rest_route(
230 'jetpack/v4',
231 '/sync/data-check',
232 array(
233 'methods' => WP_REST_Server::READABLE,
234 'callback' => __CLASS__ . '::data_check',
235 'permission_callback' => __CLASS__ . '::verify_default_permissions',
236 'args' => array(
237 'perform_text_conversion' => array(
238 'description' => __( 'If text fields should be converted to latin1 in checksum calculation.', 'jetpack-sync' ),
239 'type' => 'boolean',
240 'required' => false,
241 ),
242 ),
243 )
244 );
245
246 // Obtain histogram.
247 register_rest_route(
248 'jetpack/v4',
249 '/sync/data-histogram',
250 array(
251 'methods' => WP_REST_Server::EDITABLE,
252 'callback' => __CLASS__ . '::data_histogram',
253 'permission_callback' => __CLASS__ . '::verify_default_permissions',
254 'args' => array(
255 'columns' => array(
256 'description' => __( 'Column mappings', 'jetpack-sync' ),
257 'type' => 'array',
258 'required' => false,
259 ),
260 'object_type' => array(
261 'description' => __( 'Object Type', 'jetpack-sync' ),
262 'type' => 'string',
263 'required' => false,
264 ),
265 'buckets' => array(
266 'description' => __( 'Number of histogram buckets.', 'jetpack-sync' ),
267 'type' => 'int',
268 'required' => false,
269 ),
270 'start_id' => array(
271 'description' => __( 'Start ID for the histogram', 'jetpack-sync' ),
272 'type' => 'int',
273 'required' => false,
274 ),
275 'end_id' => array(
276 'description' => __( 'End ID for the histogram', 'jetpack-sync' ),
277 'type' => 'int',
278 'required' => false,
279 ),
280 'strip_non_ascii' => array(
281 'description' => __( 'Strip non-ascii characters?', 'jetpack-sync' ),
282 'type' => 'boolean',
283 'required' => false,
284 ),
285 'shared_salt' => array(
286 'description' => __( 'Shared Salt to use when generating checksum', 'jetpack-sync' ),
287 'type' => 'string',
288 'required' => false,
289 ),
290 'only_range_edges' => array(
291 'description' => __( 'Should only range endges be returned', 'jetpack-sync' ),
292 'type' => 'boolean',
293 'required' => false,
294 ),
295 'detailed_drilldown' => array(
296 'description' => __( 'Do we want the checksum or object ids.', 'jetpack-sync' ),
297 'type' => 'boolean',
298 'required' => false,
299 ),
300 'perform_text_conversion' => array(
301 'description' => __( 'If text fields should be converted to latin1 in checksum calculation.', 'jetpack-sync' ),
302 'type' => 'boolean',
303 'required' => false,
304 ),
305 ),
306 )
307 );
308
309 // Trigger Dedicated Sync request.
310 register_rest_route(
311 'jetpack/v4',
312 '/sync/spawn-sync',
313 array(
314 'methods' => WP_REST_Server::READABLE,
315 'callback' => __CLASS__ . '::spawn_sync',
316 'permission_callback' => '__return_true',
317 )
318 );
319
320 // Reset Sync locks.
321 register_rest_route(
322 'jetpack/v4',
323 '/sync/locks',
324 array(
325 'methods' => WP_REST_Server::DELETABLE,
326 'callback' => __CLASS__ . '::reset_locks',
327 'permission_callback' => __CLASS__ . '::verify_default_permissions',
328 )
329 );
330 }
331
332 /**
333 * Trigger a Full Sync of specified modules.
334 *
335 * @since 1.23.1
336 *
337 * @param \WP_REST_Request $request The request sent to the WP REST API.
338 *
339 * @return \WP_REST_Response|WP_Error
340 */
341 public static function full_sync_start( $request ) {
342
343 $modules = $request->get_param( 'modules' );
344
345 // convert list of modules into array format of "$modulename => true".
346 if ( ! empty( $modules ) ) {
347 $modules = array_map( '__return_true', array_flip( $modules ) );
348 }
349
350 // Process additional options.
351 foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
352 if ( 'users' === $module_name && 'initial' === $request->get_param( 'users' ) ) {
353 $modules['users'] = 'initial';
354 } elseif ( is_array( $request->get_param( $module_name ) ) ) {
355 $ids = $request->get_param( $module_name );
356 if ( array() !== $ids ) {
357 $modules[ $module_name ] = $ids;
358 }
359 }
360 }
361
362 if ( empty( $modules ) ) {
363 $modules = null;
364 }
365
366 return rest_ensure_response(
367 array(
368 'scheduled' => Actions::do_full_sync( $modules ),
369 )
370 );
371 }
372
373 /**
374 * Return Sync's status.
375 *
376 * @since 1.23.1
377 *
378 * @param \WP_REST_Request $request The request sent to the WP REST API.
379 *
380 * @return \WP_REST_Response
381 */
382 public static function sync_status( $request ) {
383 $fields = $request->get_param( 'fields' );
384 return rest_ensure_response( Actions::get_sync_status( $fields ) );
385 }
386
387 /**
388 * Return table checksums.
389 *
390 * @since 1.23.1
391 *
392 * @param \WP_REST_Request $request The request sent to the WP REST API.
393 *
394 * @return \WP_REST_Response
395 */
396 public static function data_check( $request ) {
397 // Disable Sync during this call, so we can resolve faster.
398 Actions::mark_sync_read_only();
399 $store = new Replicastore();
400
401 $perform_text_conversion = false;
402 if ( true === $request->get_param( 'perform_text_conversion' ) ) {
403 $perform_text_conversion = true;
404 }
405
406 return rest_ensure_response( $store->checksum_all( $perform_text_conversion ) );
407 }
408
409 /**
410 * Return Histogram.
411 *
412 * @since 1.23.1
413 *
414 * @param \WP_REST_Request $request The request sent to the WP REST API.
415 *
416 * @return \WP_REST_Response
417 */
418 public static function data_histogram( $request ) {
419
420 // Disable Sync during this call, so we can resolve faster.
421 Actions::mark_sync_read_only();
422
423 $args = $request->get_params();
424
425 if ( empty( $args['columns'] ) ) {
426 $args['columns'] = null; // go with defaults.
427 }
428
429 if ( false !== $args['strip_non_ascii'] ) {
430 $args['strip_non_ascii'] = true;
431 }
432
433 if ( true !== $args['perform_text_conversion'] ) {
434 $args['perform_text_conversion'] = false;
435 }
436
437 /**
438 * Hack: nullify the values of `start_id` and `end_id` if we're only requesting ranges.
439 *
440 * The endpoint doesn't support nullable values :(
441 */
442 if ( true === $args['only_range_edges'] ) {
443 if ( 0 === $args['start_id'] ) {
444 $args['start_id'] = null;
445 }
446
447 if ( 0 === $args['end_id'] ) {
448 $args['end_id'] = null;
449 }
450 }
451
452 $store = new Replicastore();
453 $histogram = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $args['columns'], $args['strip_non_ascii'], $args['shared_salt'], $args['only_range_edges'], $args['detailed_drilldown'], $args['perform_text_conversion'] );
454
455 return rest_ensure_response(
456 array(
457 'histogram' => $histogram,
458 'type' => $store->get_checksum_type(),
459 )
460 );
461 }
462
463 /**
464 * Update Sync health.
465 *
466 * @since 1.23.1
467 *
468 * @param \WP_REST_Request $request The request sent to the WP REST API.
469 *
470 * @return \WP_REST_Response
471 */
472 public static function sync_health( $request ) {
473
474 switch ( $request->get_param( 'status' ) ) {
475 case Health::STATUS_IN_SYNC:
476 case Health::STATUS_OUT_OF_SYNC:
477 Health::update_status( $request->get_param( 'status' ) );
478 break;
479 default:
480 return new WP_Error( 'invalid_status', 'Invalid Sync Status Provided.' );
481 }
482
483 // re-fetch so we see what's really being stored.
484 return rest_ensure_response(
485 array(
486 'success' => Health::get_status(),
487 )
488 );
489 }
490
491 /**
492 * Obtain Sync settings.
493 *
494 * @since 1.23.1
495 *
496 * @return \WP_REST_Response
497 */
498 public static function get_sync_settings() {
499 return rest_ensure_response( Settings::get_settings() );
500 }
501
502 /**
503 * Update Sync settings.
504 *
505 * @since 1.23.1
506 *
507 * @param \WP_REST_Request $request The request sent to the WP REST API.
508 *
509 * @return \WP_REST_Response
510 */
511 public static function update_sync_settings( $request ) {
512 $args = $request->get_params();
513 $sync_settings = Settings::get_settings();
514
515 foreach ( $args as $key => $value ) {
516 if ( false !== $value ) {
517 if ( is_numeric( $value ) ) {
518 $value = (int) $value;
519 }
520
521 // special case for sending empty arrays - a string with value 'empty'.
522 if ( 'empty' === $value ) {
523 $value = array();
524 }
525
526 $sync_settings[ $key ] = $value;
527 }
528 }
529
530 Settings::update_settings( $sync_settings );
531
532 // re-fetch so we see what's really being stored.
533 return rest_ensure_response( Settings::get_settings() );
534 }
535
536 /**
537 * Retrieve Sync Objects.
538 *
539 * @since 1.23.1
540 *
541 * @param \WP_REST_Request $request The request sent to the WP REST API.
542 *
543 * @return \WP_REST_Response
544 */
545 public static function get_sync_objects( $request ) {
546 $args = $request->get_params();
547
548 $module_name = $args['module_name'];
549 // Verify valid Sync Module.
550 $sync_module = Modules::get_module( $module_name );
551 if ( ! $sync_module ) {
552 return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
553 }
554
555 Actions::mark_sync_read_only();
556
557 $codec = Sender::get_instance()->get_codec();
558 Settings::set_is_syncing( true );
559 $objects = $codec->encode( $sync_module->get_objects_by_id( $args['object_type'], $args['object_ids'] ) );
560 Settings::set_is_syncing( false );
561
562 return rest_ensure_response(
563 array(
564 'objects' => $objects,
565 'codec' => $codec->name(),
566 )
567 );
568 }
569
570 /**
571 * Request Sync processing.
572 *
573 * @since 1.23.1
574 *
575 * @param \WP_REST_Request $request The request sent to the WP REST API.
576 *
577 * @return \WP_REST_Response
578 */
579 public static function do_sync( $request ) {
580
581 $queue_name = self::validate_queue( $request->get_param( 'queue' ) );
582 if ( is_wp_error( $queue_name ) ) {
583 return $queue_name;
584 }
585
586 $sender = Sender::get_instance();
587 $response = $sender->do_sync_for_queue( new Queue( $queue_name ) );
588
589 return rest_ensure_response(
590 array(
591 'response' => $response,
592 )
593 );
594 }
595
596 /**
597 * Request sync data from specified queue.
598 *
599 * @since 1.23.1
600 *
601 * @param \WP_REST_Request $request The request sent to the WP REST API.
602 *
603 * @return \WP_REST_Response
604 */
605 public static function checkout( $request ) {
606 $args = $request->get_params();
607 $queue_name = self::validate_queue( $args['queue'] );
608
609 if ( is_wp_error( $queue_name ) ) {
610 return $queue_name;
611 }
612
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 );
616 }
617
618 // REST Sender.
619 $sender = new REST_Sender();
620
621 if ( 'immediate' === $queue_name ) {
622 return rest_ensure_response( $sender->immediate_full_sync_pull( $number_of_items ) );
623 }
624
625 return rest_ensure_response( $sender->queue_pull( $queue_name, $number_of_items, $args ) );
626 }
627
628 /**
629 * Unlock a Sync queue.
630 *
631 * @since 1.23.1
632 *
633 * @param \WP_REST_Request $request The request sent to the WP REST API.
634 *
635 * @return \WP_REST_Response
636 */
637 public static function unlock_queue( $request ) {
638
639 $queue_name = $request->get_param( 'queue' );
640
641 if ( ! in_array( $queue_name, array( 'sync', 'full_sync' ), true ) ) {
642 return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
643 }
644 $queue = new Queue( $queue_name );
645
646 // False means that there was no lock to delete.
647 $response = $queue->unlock();
648 return rest_ensure_response(
649 array(
650 'success' => $response,
651 )
652 );
653 }
654
655 /**
656 * Checkin Sync actions.
657 *
658 * @since 1.23.1
659 *
660 * @param \WP_REST_Request $request The request sent to the WP REST API.
661 *
662 * @return \WP_REST_Response
663 */
664 public static function close( $request ) {
665
666 $request_body = $request->get_params();
667 $queue_name = self::validate_queue( $request_body['queue'] );
668
669 if ( is_wp_error( $queue_name ) ) {
670 return $queue_name;
671 }
672
673 if ( empty( $request_body['buffer_id'] ) ) {
674 return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
675 }
676
677 if ( ! is_array( $request_body['item_ids'] ) ) {
678 return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
679 }
680
681 // Limit to A-Z,a-z,0-9,_,- .
682 $request_body['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
683 $request_body['item_ids'] = array_filter( array_map( array( 'Automattic\Jetpack\Sync\REST_Endpoints', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
684
685 $queue = new Queue( $queue_name );
686
687 $items = $queue->peek_by_id( $request_body['item_ids'] );
688
689 // Update Full Sync Status if queue is "full_sync".
690 if ( 'full_sync' === $queue_name ) {
691 $full_sync_module = Modules::get_module( 'full-sync' );
692 $full_sync_module->update_sent_progress_action( $items );
693 }
694
695 $buffer = new Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
696 $response = $queue->close( $buffer, $request_body['item_ids'] );
697
698 // Perform another checkout?
699 if ( isset( $request_body['continue'] ) && $request_body['continue'] ) {
700 if ( in_array( $queue_name, array( 'full_sync', 'immediate' ), true ) ) {
701 // Send Full Sync Actions.
702 Sender::get_instance()->do_full_sync();
703 } elseif ( $queue->has_any_items() ) {
704 // Send Incremental Sync Actions.
705 Sender::get_instance()->do_sync();
706 }
707 }
708
709 if ( is_wp_error( $response ) ) {
710 return $response;
711 }
712
713 return rest_ensure_response(
714 array(
715 'success' => $response,
716 'status' => Actions::get_sync_status(),
717 )
718 );
719 }
720
721 /**
722 * Retrieve range of Object Ids for a specified Sync module.
723 *
724 * @since 1.23.1
725 *
726 * @param \WP_REST_Request $request The request sent to the WP REST API.
727 *
728 * @return \WP_REST_Response
729 */
730 public static function get_object_id_range( $request ) {
731
732 $module_name = $request->get_param( 'sync_module' );
733 $batch_size = $request->get_param( 'batch_size' );
734
735 if ( ! self::is_valid_sync_module( $module_name ) ) {
736 return new WP_Error( 'invalid_module', 'This sync module cannot be used to calculate a range.', 400 );
737 }
738 $module = Modules::get_module( $module_name );
739
740 return rest_ensure_response(
741 array(
742 'ranges' => $module->get_min_max_object_ids_for_batches( $batch_size ),
743 )
744 );
745 }
746
747 /**
748 * This endpoint is used by Sync to spawn a
749 * dedicated Sync request which will trigger Sync to run.
750 *
751 * If Dedicated Sync is enabled, this callback should never run as
752 * processing of Sync actions will occur earlier and exit.
753 *
754 * @see Actions::init
755 * @see Sender::do_dedicated_sync_and_exit
756 *
757 * @since $$next_version$$
758 *
759 * @return \WP_REST_Response
760 */
761 public static function spawn_sync() {
762 nocache_headers();
763
764 if ( ! Settings::is_dedicated_sync_enabled() ) {
765 return new WP_Error(
766 'dedicated_sync_disabled',
767 'Dedicated Sync flow is disabled.',
768 array( 'status' => 422 )
769 );
770 }
771
772 return new WP_Error(
773 'dedicated_sync_failed',
774 'Failed to process Dedicated Sync request',
775 array( 'status' => 500 )
776 );
777 }
778
779 /**
780 * Reset Sync locks.
781 *
782 * @since 1.43.0
783 *
784 * @return \WP_REST_Response
785 */
786 public static function reset_locks() {
787 Actions::reset_sync_locks();
788
789 return rest_ensure_response(
790 array(
791 'success' => true,
792 )
793 );
794 }
795
796 /**
797 * Verify that request has default permissions to perform sync actions.
798 *
799 * @since 1.23.1
800 *
801 * @return bool Whether user has capability 'manage_options' or a blog token is used.
802 */
803 public static function verify_default_permissions() {
804 if ( current_user_can( 'manage_options' ) || Rest_Authentication::is_signed_with_blog_token() ) {
805 return true;
806 }
807
808 $error_msg = esc_html__(
809 'You do not have the correct user permissions to perform this action.
810 Please contact your site admin if you think this is a mistake.',
811 'jetpack-sync'
812 );
813
814 return new WP_Error( 'invalid_user_permission_sync', $error_msg, array( 'status' => rest_authorization_required_code() ) );
815 }
816
817 /**
818 * Validate Queue name.
819 *
820 * @param string $value Queue Name.
821 *
822 * @return WP_Error
823 */
824 protected static function validate_queue( $value ) {
825 if ( ! isset( $value ) ) {
826 return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
827 }
828
829 if ( ! in_array( $value, array( 'sync', 'full_sync', 'immediate' ), true ) ) {
830 return new WP_Error( 'invalid_queue', 'Queue name should be sync, full_sync or immediate', 400 );
831 }
832 return $value;
833 }
834
835 /**
836 * Validate name is a valid Sync module.
837 *
838 * @param string $module_name Name of Sync Module.
839 *
840 * @return bool
841 */
842 protected static function is_valid_sync_module( $module_name ) {
843 return in_array(
844 $module_name,
845 array(
846 'comments',
847 'posts',
848 'terms',
849 'term_relationships',
850 'users',
851 ),
852 true
853 );
854 }
855
856 /**
857 * Sanitize Item Ids.
858 *
859 * @param string $item Sync item identifier.
860 *
861 * @return string|string[]|null
862 */
863 protected static function sanitize_item_ids( $item ) {
864 // lets not delete any options that don't start with jpsq_sync- .
865 if ( ! is_string( $item ) || substr( $item, 0, 5 ) !== 'jpsq_' ) {
866 return null;
867 }
868 // Limit to A-Z,a-z,0-9,_,-,. .
869 return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
870 }
871
872 }
873