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/modules/class-full-sync-immediately.php +203 -95 13.5.2 → 16.3-beta View file →
@@ -12,8 +12,12 @@
12 12 use Automattic\Jetpack\Sync\Lock;
13 13 use Automattic\Jetpack\Sync\Modules;
14 14 use Automattic\Jetpack\Sync\Settings;
15 15
16 +if ( ! defined( 'ABSPATH' ) ) {
17 + exit( 0 );
18 +}
19 +
16 20 /**
17 21 * This class does a full resync of the database by
18 22 * sending an outbound action for every single object
19 23 * that we care about.
@@ -59,29 +63,36 @@
59 63 *
60 64 * @access public
61 65 *
62 66 * @param array $full_sync_config Full sync configuration.
67 + * @param mixed $context The context where the full sync was initiated from.
63 68 *
64 69 * @return bool Always returns true at success.
65 70 */
66 - public function start( $full_sync_config = null ) {
67 - // There was a full sync in progress.
68 - if ( $this->is_started() && ! $this->is_finished() ) {
69 - /**
70 - * Fires when a full sync is cancelled.
71 - *
72 - * @since 1.6.3
73 - * @since-jetpack 4.2.0
74 - */
75 - do_action( 'jetpack_full_sync_cancelled' );
76 - $this->send_action( 'jetpack_full_sync_cancelled' );
77 - }
78 -
71 + public function start( $full_sync_config = null, $context = null ) {
72 + // Check if there was a full sync in progress already before resetting the data.
73 + $should_process_cancelled_action = $this->get_status()['start_action_processed'] && ! $this->is_finished();
79 74 // Remove all evidence of previous full sync items and status.
80 75 $this->reset_data();
81 76
77 + // Update status to indicate that a new full sync is starting and need to cancel previous one.
78 + if ( $should_process_cancelled_action ) {
79 + $this->update_status(
80 + array(
81 + 'cancelled_action_processed' => false,
82 + )
83 + );
84 + }
85 +
82 86 if ( ! is_array( $full_sync_config ) ) {
83 - $full_sync_config = Defaults::$default_full_sync_config;
87 + /*
88 + * Filter default sync config to allow injecting custom configuration.
89 + *
90 + * @param array $full_sync_config Sync configuration for all sync modules.
91 + *
92 + * @since 3.10.0
93 + */
94 + $full_sync_config = apply_filters( 'jetpack_full_sync_config', Defaults::$default_full_sync_config );
84 95 if ( is_multisite() ) {
85 96 $full_sync_config['network_options'] = 1;
86 97 }
87 98 }
@@ -93,31 +104,14 @@
93 104 }
94 105
95 106 $this->update_status(
96 107 array(
97 - 'started' => time(),
98 - 'config' => $full_sync_config,
99 - 'progress' => $this->get_initial_progress( $full_sync_config ),
108 + 'started' => time(),
109 + 'config' => $full_sync_config,
110 + 'context' => $context,
100 111 )
101 112 );
102 113
103 - $range = $this->get_content_range();
104 - /**
105 - * Fires when a full sync begins. This action is serialized
106 - * and sent to the server so that it knows a full sync is coming.
107 - *
108 - * @param array $full_sync_config Sync configuration for all sync modules.
109 - * @param array $range Range of the sync items, containing min and max IDs for some item types.
110 - * @param array $empty The modules with no items to sync during a full sync.
111 - *
112 - * @since 1.6.3
113 - * @since-jetpack 4.2.0
114 - * @since-jetpack 7.3.0 Added $range arg.
115 - * @since-jetpack 7.4.0 Added $empty arg.
116 - */
117 - do_action( 'jetpack_full_sync_start', $full_sync_config, $range );
118 - $this->send_action( 'jetpack_full_sync_start', array( $full_sync_config, $range ) );
119 -
120 114 return true;
121 115 }
122 116
123 117 /**
@@ -139,12 +133,15 @@
139 133 * @return array Full sync status.
140 134 */
141 135 public function get_status() {
142 136 $default = array(
143 - 'started' => false,
144 - 'finished' => false,
145 - 'progress' => array(),
146 - 'config' => array(),
137 + 'start_action_processed' => false,
138 + 'cancelled_action_processed' => true, // true by default to avoid sending the action when there is no need,
139 + 'started' => false,
140 + 'finished' => false,
141 + 'progress' => array(),
142 + 'config' => array(),
143 + 'context' => null,
147 144 );
148 145
149 146 return wp_parse_args( \Jetpack_Options::get_raw_option( self::STATUS_OPTION ), $default );
150 147 }
@@ -240,12 +237,13 @@
240 237 /**
241 238 * Given an initial Full Sync configuration get the initial status.
242 239 *
243 240 * @param array $full_sync_config Full sync configuration.
241 + * @param array $range Range of the sync items, containing min, max and count IDs for some item types.
244 242 *
245 243 * @return array Initial Sent status.
246 244 */
247 - public function get_initial_progress( $full_sync_config ) {
245 + public function get_initial_progress( $full_sync_config, $range = null ) {
248 246 // Set default configuration, calculate totals, and save configuration if totals > 0.
249 247 $status = array();
250 248 foreach ( $full_sync_config as $name => $config ) {
251 249 $module = Modules::get_module( $name );
@@ -252,9 +250,10 @@
252 250 if ( ! $module ) {
253 251 continue;
254 252 }
255 253 $status[ $name ] = array(
256 - 'total' => $module->total( $config ),
254 + // If we have a range for the module, use the count from the range to avoid querying the database again.
255 + 'total' => $range[ $name ]->count ?? $module->total( $config ),
257 256 'sent' => 0,
258 257 'finished' => false,
259 258 );
260 259 }
@@ -266,22 +265,28 @@
266 265 * Get the range for content (posts and comments) to sync.
267 266 *
268 267 * @access private
269 268 *
269 + * @param array $full_sync_config Full sync configuration.
270 + *
270 271 * @return array Array of range (min ID, max ID, total items) for all content types.
271 272 */
272 - private function get_content_range() {
273 - $range = array();
274 - $config = $this->get_status()['config'];
275 - // Add range only when syncing all objects.
276 - if ( true === isset( $config['posts'] ) && $config['posts'] ) {
277 - $range['posts'] = $this->get_range( 'posts' );
273 + private function get_content_range( $full_sync_config ) {
274 + $range = array();
275 + foreach ( $full_sync_config as $module_name => $config ) {
276 + // Calculate ranges only for modules that get chunked.
277 + if ( in_array( $module_name, array( 'constants', 'functions', 'network_options', 'options', 'themes', 'updates' ), true ) ) {
278 + continue;
279 + }
280 + $module = Modules::get_module( $module_name );
281 + if ( ! $module ) {
282 + continue;
283 + }
284 + if ( $config ) {
285 + $range[ $module_name ] = $this->get_range( $module_name );
286 + }
278 287 }
279 288
280 - if ( true === isset( $config['comments'] ) && $config['comments'] ) {
281 - $range['comments'] = $this->get_range( 'comments' );
282 - }
283 -
284 289 return $range;
285 290 }
286 291
287 292 /**
@@ -294,25 +299,19 @@
294 299 * @return array Array of min ID, max ID and total items in the range.
295 300 */
296 301 public function get_range( $type ) {
297 302 global $wpdb;
298 - if ( ! in_array( $type, array( 'comments', 'posts' ), true ) ) {
303 + $module = Modules::get_module( $type );
304 + if ( ! $module ) {
299 305 return array();
300 306 }
301 307
302 - switch ( $type ) {
303 - case 'posts':
304 - $table = $wpdb->posts;
305 - $id = 'ID';
306 - $where_sql = Settings::get_blacklisted_post_types_sql();
307 -
308 - break;
309 - case 'comments':
310 - $table = $wpdb->comments;
311 - $id = 'comment_ID';
312 - $where_sql = Settings::get_comments_filter_sql();
313 - break;
308 + $table = $module->table();
309 + $id = $module->id_field();
310 + if ( 'terms' === $module ) { // Terms module relies on the term_taxonomy and term_taxonomy_id for the where sql, let's use term_id instead.
311 + $id = 'term_id';
314 312 }
313 + $where_sql = $module->get_where_sql( array() );
315 314
316 315 // TODO: Call $wpdb->prepare on the following query.
317 316 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
318 317 $results = $wpdb->get_results( "SELECT MAX({$id}) as max, MIN({$id}) as min, COUNT({$id}) as count FROM {$table} WHERE {$where_sql}" );
@@ -376,8 +375,16 @@
376 375 *
377 376 * @access public
378 377 */
379 378 public function send() {
379 +
380 + if ( ! $this->maybe_send_cancelled_action() ) {
381 + return false;
382 + }
383 +
384 + if ( ! $this->maybe_send_full_sync_start() ) {
385 + return false;
386 + }
380 387 $config = $this->get_status()['config'];
381 388
382 389 $max_duration = Settings::get_setting( 'full_sync_send_duration' );
383 390 $send_until = microtime( true ) + $max_duration;
@@ -383,15 +390,37 @@
383 390 $send_until = microtime( true ) + $max_duration;
384 391
385 392 $progress = $this->get_status()['progress'];
386 393
387 - foreach ( $this->get_remaining_modules_to_send() as $module ) {
388 - $progress[ $module->name() ] = $module->send_full_sync_actions( $config[ $module->name() ], $progress[ $module->name() ], $send_until );
389 - if ( isset( $progress[ $module->name() ]['error'] ) ) {
390 - unset( $progress[ $module->name() ]['error'] );
391 - $this->update_status( array( 'progress' => $progress ) );
394 + $started = $this->get_status()['started'];
395 +
396 + $remaining_modules = $this->get_remaining_modules_to_send();
397 +
398 + foreach ( $remaining_modules as $module ) {
399 + $module_name = $module->name();
400 + if ( array_key_exists( $module_name, $progress ) && array_key_exists( $module_name, $config ) ) {
401 + $progress[ $module_name ] = $module->send_full_sync_actions( $config[ $module_name ], $progress[ $module_name ], $send_until, $started );
402 + if ( isset( $progress[ $module_name ]['error'] ) ) {
403 + unset( $progress[ $module_name ]['error'] );
404 + $this->update_status( array( 'progress' => $progress ) );
405 + return false;
406 + } elseif ( ! $progress[ $module_name ]['finished'] ) {
407 + $this->update_status( array( 'progress' => $progress ) );
408 + return true;
409 + }
410 + }
411 +
412 + if ( $this->get_status()['started'] !== $started ) {
413 + // Full sync was restarted, stop sending.
392 414 return false;
393 - } elseif ( ! $progress[ $module->name() ]['finished'] ) {
415 + }
416 + }
417 +
418 + // Check that all remaining modules in progress are actually finished.
419 + // If a module was skipped in the main loop (due to being unfinished), but still exists in progress, we shouldn't mark the sync as complete.
420 + foreach ( $remaining_modules as $module ) {
421 + $name = $module->name();
422 + if ( array_key_exists( $name, $progress ) && empty( $progress[ $name ]['finished'] ) ) {
394 423 $this->update_status( array( 'progress' => $progress ) );
395 424 return true;
396 425 }
397 426 }
@@ -406,45 +435,125 @@
406 435 *
407 436 * @return array
408 437 */
409 438 public function get_remaining_modules_to_send() {
439 + $status = $this->get_status();
440 + $remaining_modules = array();
441 + foreach ( array_keys( $status['config'] ) as $module_name ) {
442 + $module = Modules::get_module( $module_name );
443 + if ( ! $module ) {
444 + continue;
445 + }
446 + if ( isset( $status['progress'][ $module_name ]['finished'] ) &&
447 + true === $status['progress'][ $module_name ]['finished'] ) {
448 + continue;
449 + }
450 + // Ensure that 'constants', 'options', and 'callables' are sent first.
451 + if ( in_array( $module_name, array( 'network_options', 'options', 'functions', 'constants' ), true ) ) {
452 + array_unshift( $remaining_modules, $module );
453 + } else {
454 + $remaining_modules[] = $module;
455 + }
456 + }
457 + return $remaining_modules;
458 + }
459 +
460 + /**
461 + * Sends the `jetpack_full_sync_start` action if it hasn't been processed yet.
462 + *
463 + * Prepares the full sync start action, sends it to WordPress.com, fires the local action,
464 + * and updates the sync status to reflect that the start action has been processed.
465 + *
466 + * @return bool True if the action was successfully sent or already processed, false on failure.
467 + */
468 + private function maybe_send_full_sync_start() {
410 469 $status = $this->get_status();
411 470
412 - return array_filter(
413 - Modules::get_modules(),
414 - /**
415 - * Select configured and not finished modules.
416 - *
417 - * @param Module $module
418 - * @return bool
419 - */
420 - function ( $module ) use ( $status ) {
421 - // Skip module if not configured for this sync or module is done.
422 - if ( ! isset( $status['config'][ $module->name() ] ) ) {
423 - return false;
424 - }
425 - if ( ! $status['config'][ $module->name() ] ) {
426 - return false;
427 - }
428 - if ( isset( $status['progress'][ $module->name() ]['finished'] ) ) {
429 - if ( true === $status['progress'][ $module->name() ]['finished'] ) {
430 - return false;
431 - }
432 - }
471 + // If already processed, nothing to do.
472 + if ( true === $status['start_action_processed'] ) {
473 + return true;
474 + }
433 475
434 - return true;
435 - }
476 + $config = $status['config'];
477 + $context = $status['context'];
478 + $range = $this->get_content_range( $config );
479 +
480 + $result = $this->send_action( 'jetpack_full_sync_start', array( $config, $range, $context ) );
481 +
482 + // If the action failed on WordPress.com, return false.
483 + if ( is_wp_error( $result ) ) {
484 + return false;
485 + }
486 +
487 + /**
488 + * Fires when a full sync begins. This action is serialized
489 + * and sent to the server so that it knows a full sync is coming.
490 + *
491 + * @param array $config Sync configuration for all sync modules.
492 + * @param array $range Range of the sync items, containing min and max IDs for some item types.
493 + * @param mixed $context The context where the full sync was initiated from.
494 + *
495 + * @since 1.6.3
496 + * @since-jetpack 4.2.0
497 + * @since-jetpack 7.3.0 Added $range arg.
498 + * @since 4.4.0 Added $context arg.
499 + */
500 + do_action( 'jetpack_full_sync_start', $config, $range );
501 +
502 + $this->update_status(
503 + array(
504 + 'start_action_processed' => true,
505 + 'progress' => $this->get_initial_progress( $config, $range ),
506 + )
436 507 );
508 +
509 + return true;
437 510 }
438 511
439 512 /**
440 - * Send 'jetpack_full_sync_end' and update 'finished' status.
513 + * Sends the `jetpack_full_sync_cancelled` action if it hasn't been processed yet.
441 514 *
515 + * @return bool True if the action was successfully sent or already processed, false on failure.
516 + */
517 + private function maybe_send_cancelled_action() {
518 + $status = $this->get_status();
519 +
520 + if ( true === $status['cancelled_action_processed'] ) {
521 + return true;
522 + }
523 +
524 + $result = $this->send_action( 'jetpack_full_sync_cancelled' );
525 +
526 + if ( is_wp_error( $result ) ) {
527 + return false;
528 + }
529 +
530 + /**
531 + * Fires when a full sync is cancelled.
532 + *
533 + * @since 1.6.3
534 + * @since-jetpack 4.2.0
535 + */
536 + do_action( 'jetpack_full_sync_cancelled' );
537 + $this->update_status( array( 'cancelled_action_processed' => true ) );
538 + return true;
539 + }
540 +
541 + /**
542 + * Sends the `jetpack_full_sync_end` action and updates the status when the full sync end action is processed.
543 + *
442 544 * @access public
443 545 */
444 546 public function send_full_sync_end() {
445 - $range = $this->get_content_range();
547 + $status = $this->get_status();
548 + $range = $this->get_content_range( $status['config'] );
549 + $context = $status['context'];
446 550
551 + $result = $this->send_action( 'jetpack_full_sync_end', array( '', $range, $context ) );
552 +
553 + if ( is_wp_error( $result ) ) { // Do not set finished status if we get an error.
554 + return;
555 + }
447 556 /**
448 557 * Fires when a full sync ends. This action is serialized
449 558 * and sent to the server.
450 559 *
@@ -455,9 +564,8 @@
455 564 * @since-jetpack 4.2.0
456 565 * @since-jetpack 7.3.0 Added $range arg.
457 566 */
458 567 do_action( 'jetpack_full_sync_end', '', $range );
459 - $this->send_action( 'jetpack_full_sync_end', array( '', $range ) );
460 568
461 569 // Setting autoload to true means that it's faster to check whether we should continue enqueuing.
462 570 $this->update_status( array( 'finished' => time() ) );
463 571 }