Completed
Push — fix/check-queue-status-before-... ( 3b1e08...42b93d )
by
unknown
07:13 queued 31s
created

Full_Sync::continue_enqueuing_with_lock()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Full sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Listener;
11
use Automattic\Jetpack\Sync\Modules;
12
use Automattic\Jetpack\Sync\Queue;
13
use Automattic\Jetpack\Sync\Settings;
14
15
/**
16
 * This class does a full resync of the database by
17
 * enqueuing an outbound action for every single object
18
 * that we care about.
19
 *
20
 * This class, and its related class Jetpack_Sync_Module, contain a few non-obvious optimisations that should be explained:
21
 * - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database
22
 * - for each object type, we page through the object IDs and enqueue them by firing some monitored actions
23
 * - we load the full objects for those IDs in chunks of Jetpack_Sync_Module::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls)
24
 * - we fire a trigger for the entire array which the Automattic\Jetpack\Sync\Listener then serializes and queues.
25
 */
26
class Full_Sync extends Module {
27
	/**
28
	 * Prefix of the full sync status option name.
29
	 *
30
	 * @var string
31
	 */
32
	const STATUS_OPTION_PREFIX = 'jetpack_sync_full_';
33
34
	/**
35
	 * Timeout between the previous and the next allowed full sync.
36
	 *
37
	 * @todo Remove this as it's no longer used since https://github.com/Automattic/jetpack/pull/4561
38
	 *
39
	 * @var int
40
	 */
41
	const FULL_SYNC_TIMEOUT = 3600;
42
43
	/**
44
	 *
45
	 * Remaining Items to enqueue.
46
	 *
47
	 * @var int
48
	 */
49
	private $remaining_items_to_enqueue = 0;
50
51
	/**
52
	 *
53
	 * Per each module: total items to send, how many have been enqueued, the last object_id enqueued
54
	 *
55
	 * @var array
56
	 */
57
	private $enqueue_status;
58
59
	/**
60
	 * Sync module name.
61
	 *
62
	 * @access public
63
	 *
64
	 * @return string
65
	 */
66
	public function name() {
67
		return 'full-sync';
68
	}
69
70
	/**
71
	 * Initialize action listeners for full sync.
72
	 *
73
	 * @access public
74
	 *
75
	 * @param callable $callable Action handler callable.
76
	 */
77
	public function init_full_sync_listeners( $callable ) {
78
		// Synthetic actions for full sync.
79
		add_action( 'jetpack_full_sync_start', $callable, 10, 3 );
80
		add_action( 'jetpack_full_sync_end', $callable, 10, 2 );
81
		add_action( 'jetpack_full_sync_cancelled', $callable );
82
	}
83
84
	/**
85
	 * Initialize the module in the sender.
86
	 *
87
	 * @access public
88
	 */
89
	public function init_before_send() {
90
		// This is triggered after actions have been processed on the server.
91
		add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) );
92
	}
93
94
	/**
95
	 * Start a full sync.
96
	 *
97
	 * @access public
98
	 *
99
	 * @param array $module_configs Full sync configuration for all sync modules.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $module_configs not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
100
	 * @return bool Always returns true at success.
101
	 */
102
	public function start( $module_configs = null ) {
103
		$was_already_running = $this->is_started() && ! $this->is_finished();
104
105
		// Remove all evidence of previous full sync items and status.
106
		$this->reset_data();
107
108
		if ( $was_already_running ) {
109
			/**
110
			 * Fires when a full sync is cancelled.
111
			 *
112
			 * @since 4.2.0
113
			 */
114
			do_action( 'jetpack_full_sync_cancelled' );
115
		}
116
117
		$this->update_status_option( 'started', time() );
118
		$this->update_status_option( 'params', $module_configs );
119
120
		$enqueue_status   = array();
121
		$full_sync_config = array();
122
		$include_empty    = false;
123
		$empty            = array();
124
125
		// Default value is full sync.
126
		if ( ! is_array( $module_configs ) ) {
127
			$module_configs = array();
128
			$include_empty  = true;
129
			foreach ( Modules::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression \Automattic\Jetpack\Sync\Modules::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
130
				$module_configs[ $module->name() ] = true;
131
			}
132
		}
133
134
		// Set default configuration, calculate totals, and save configuration if totals > 0.
135
		foreach ( Modules::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression \Automattic\Jetpack\Sync\Modules::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
136
			$module_name   = $module->name();
137
			$module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false;
138
139
			if ( ! $module_config ) {
140
				continue;
141
			}
142
143
			if ( 'users' === $module_name && 'initial' === $module_config ) {
144
				$module_config = $module->get_initial_sync_user_config();
145
			}
146
147
			$enqueue_status[ $module_name ] = false;
148
149
			$total_items = $module->estimate_full_sync_actions( $module_config );
150
151
			// If there's information to process, configure this module.
152
			if ( ! is_null( $total_items ) && $total_items > 0 ) {
153
				$full_sync_config[ $module_name ] = $module_config;
154
				$enqueue_status[ $module_name ]   = array(
155
					$total_items,   // Total.
156
					0,              // Queued.
157
					false,          // Current state.
158
				);
159
			} elseif ( $include_empty && 0 === $total_items ) {
160
				$empty[ $module_name ] = true;
161
			}
162
		}
163
164
		$this->set_config( $full_sync_config );
165
		$this->set_enqueue_status( $enqueue_status );
166
167
		$range = $this->get_content_range( $full_sync_config );
168
		/**
169
		 * Fires when a full sync begins. This action is serialized
170
		 * and sent to the server so that it knows a full sync is coming.
171
		 *
172
		 * @since 4.2.0
173
		 * @since 7.3.0 Added $range arg.
174
		 * @since 7.4.0 Added $empty arg.
175
		 *
176
		 * @param array $full_sync_config Sync configuration for all sync modules.
177
		 * @param array $range            Range of the sync items, containing min and max IDs for some item types.
178
		 * @param array $empty            The modules with no items to sync during a full sync.
179
		 */
180
		do_action( 'jetpack_full_sync_start', $full_sync_config, $range, $empty );
181
182
		$this->continue_enqueuing( $full_sync_config, $enqueue_status );
183
184
		return true;
185
	}
186
187
	/**
188
	 * Enqueue the next items to sync.
189
	 *
190
	 * @access public
191
	 *
192
	 * @param array $configs Full sync configuration for all sync modules.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $configs not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
193
	 * @param array $enqueue_status Current status of the queue, indexed by sync modules.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $enqueue_status not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
194
	 */
195
	public function continue_enqueuing( $configs = null, $enqueue_status = null ) {
196
		if ( ! $this->is_started() ) {
197
			return;
198
		}
199
		if ( ! $this->attempt_enqueue_lock() ) {
200
			return;
201
		}
202
		if ( ! $this->get_status_option( 'queue_finished' ) ) {
203
			return;
204
		}
205
		$this->enqueue_status = $enqueue_status ? $enqueue_status : $this->get_enqueue_status();
206
		$this->continue_enqueuing_with_lock( $configs );
207
		$this->set_enqueue_status( $this->enqueue_status );
208
209
		$this->remove_enqueue_lock();
210
	}
211
212
	/**
213
	 *
214
	 * Get Available Full Sync queue slots.
215
	 *
216
	 * @return int
217
	 */
218
	public function get_available_queue_slots() {
219
		// If full sync queue is full, don't enqueue more items.
220
		$max_queue_size_full_sync = Settings::get_setting( 'max_queue_size_full_sync' );
221
		$full_sync_queue          = new Queue( 'full_sync' );
222
223
		return $max_queue_size_full_sync - $full_sync_queue->size();
224
	}
225
226
	/**
227
	 * Get Modules that are configured to Full Sync and haven't finished enqueuing
228
	 *
229
	 * @param array $configs Full sync configuration for all sync modules.
230
	 *
231
	 * @return array
232
	 */
233
	public function get_remaining_modules_to_enqueue( $configs ) {
234
		return array_filter(
235
			Modules::get_modules(),
236
			function ( $module ) use ( $configs ) {
237
				// Skip module if not configured for this sync or module is done.
238
				if ( ! isset( $configs[ $module->name() ] ) ) {
239
					return false;
240
				}
241
				if ( ! $configs[ $module->name() ] ) {
242
					return false;
243
				}
244
				if ( ! isset( $this->enqueue_status[ $module->name() ] ) ) {
245
					return false;
246
				}
247
				if ( true === $this->enqueue_status[ $module->name() ][2] ) {
248
					return false;
249
				}
250
251
				return true;
252
			}
253
		);
254
	}
255
256
	/**
257
	 * Enqueue the next items to sync.
258
	 *
259
	 * @access public
260
	 *
261
	 * @param array $configs Full sync configuration for all sync modules.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $configs not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
262
	 */
263
	public function continue_enqueuing_with_lock( $configs = null ) {
264
		if ( ! $configs ) {
265
			$configs = $this->get_config();
266
		}
267
268
		$this->remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $this->get_available_queue_slots() );
269
270
		foreach ( $this->get_remaining_modules_to_enqueue( $configs ) as $module ) {
271
			if ( 0 >= $this->remaining_items_to_enqueue ) {
272
				return;
273
			}
274
			$this->enqueue_module( $module, $configs[ $module->name() ] );
275
			// Stop processing if we've reached our limit of items to enqueue.
276
		}
277
278
		$this->queue_full_sync_end( $configs );
279
	}
280
281
	/**
282
	 * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status.
283
	 *
284
	 * @access public
285
	 *
286
	 * @param array $configs Full sync configuration for all sync modules.
287
	 */
288
	public function queue_full_sync_end( $configs ) {
289
		$range = $this->get_content_range( $configs );
290
291
		/**
292
		 * Fires when a full sync ends. This action is serialized
293
		 * and sent to the server.
294
		 *
295
		 * @since 4.2.0
296
		 * @since 7.3.0 Added $range arg.
297
		 *
298
		 * @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/
299
		 * @param array  $range    Range of the sync items, containing min and max IDs for some item types.
300
		 */
301
		do_action( 'jetpack_full_sync_end', '', $range );
302
303
		// Setting autoload to true means that it's faster to check whether we should continue enqueuing.
304
		$this->update_status_option( 'queue_finished', time(), true );
305
	}
306
	/**
307
	 * Enqueue Full Sync Actions for the given module.
308
	 *
309
	 * @param Object $module The module to Enqueue.
310
	 * @param array  $config The Full sync configuration for the modules.
311
	 *
312
	 * @return int
313
	 */
314
	public function enqueue_module( $module, $config ) {
315
		list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $config, $this->remaining_items_to_enqueue, $this->enqueue_status[ $module->name() ][2] );
316
317
		$this->enqueue_status[ $module->name() ][2] = $next_enqueue_state;
318
319
		// If items were processed, subtract them from the limit.
320
		if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) {
321
			$this->enqueue_status[ $module->name() ][1] += $items_enqueued;
322
			$this->remaining_items_to_enqueue           -= $items_enqueued;
323
		}
324
325
		return true === $next_enqueue_state ? 1 : 0;
326
	}
327
328
	/**
329
	 * Get the range (min ID, max ID and total items) of items to sync.
330
	 *
331
	 * @access public
332
	 *
333
	 * @param string $type Type of sync item to get the range for.
334
	 * @return array Array of min ID, max ID and total items in the range.
335
	 */
336
	public function get_range( $type ) {
337
		global $wpdb;
338
		if ( ! in_array( $type, array( 'comments', 'posts' ), true ) ) {
339
			return array();
340
		}
341
342
		switch ( $type ) {
343
			case 'posts':
344
				$table     = $wpdb->posts;
345
				$id        = 'ID';
346
				$where_sql = Settings::get_blacklisted_post_types_sql();
347
348
				break;
349
			case 'comments':
350
				$table     = $wpdb->comments;
351
				$id        = 'comment_ID';
352
				$where_sql = Settings::get_comments_filter_sql();
353
				break;
354
		}
355
356
		// TODO: Call $wpdb->prepare on the following query.
357
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
358
		$results = $wpdb->get_results( "SELECT MAX({$id}) as max, MIN({$id}) as min, COUNT({$id}) as count FROM {$table} WHERE {$where_sql}" );
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $table does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $where_sql does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
359
		if ( isset( $results[0] ) ) {
360
			return $results[0];
361
		}
362
363
		return array();
364
	}
365
366
	/**
367
	 * Get the range for content (posts and comments) to sync.
368
	 *
369
	 * @access private
370
	 *
371
	 * @param array $config Full sync configuration for this all sync modules.
372
	 * @return array Array of range (min ID, max ID, total items) for all content types.
373
	 */
374
	private function get_content_range( $config ) {
375
		$range = array();
376
		// Only when we are sending the whole range do we want to send also the range.
377 View Code Duplication
		if ( true === isset( $config['posts'] ) && $config['posts'] ) {
378
			$range['posts'] = $this->get_range( 'posts' );
379
		}
380
381 View Code Duplication
		if ( true === isset( $config['comments'] ) && $config['comments'] ) {
382
			$range['comments'] = $this->get_range( 'comments' );
383
		}
384
		return $range;
385
	}
386
387
	/**
388
	 * Update the progress after sync modules actions have been processed on the server.
389
	 *
390
	 * @access public
391
	 *
392
	 * @param array $actions Actions that have been processed on the server.
393
	 */
394
	public function update_sent_progress_action( $actions ) {
395
		// Quick way to map to first items with an array of arrays.
396
		$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) );
397
398
		// Total item counts for each action.
399
		$actions_with_total_counts = $this->get_actions_totals( $actions );
400
401
		if ( ! $this->is_started() || $this->is_finished() ) {
402
			return;
403
		}
404
405
		if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) {
406
			$this->update_status_option( 'send_started', time() );
407
		}
408
409
		foreach ( Modules::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression \Automattic\Jetpack\Sync\Modules::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
410
			$module_actions     = $module->get_full_sync_actions();
411
			$status_option_name = "{$module->name()}_sent";
412
			$total_option_name  = "{$status_option_name}_total";
413
			$items_sent         = $this->get_status_option( $status_option_name, 0 );
414
			$items_sent_total   = $this->get_status_option( $total_option_name, 0 );
415
416
			foreach ( $module_actions as $module_action ) {
417
				if ( isset( $actions_with_counts[ $module_action ] ) ) {
418
					$items_sent += $actions_with_counts[ $module_action ];
419
				}
420
421
				if ( ! empty( $actions_with_total_counts[ $module_action ] ) ) {
422
					$items_sent_total += $actions_with_total_counts[ $module_action ];
423
				}
424
			}
425
426
			if ( $items_sent > 0 ) {
427
				$this->update_status_option( $status_option_name, $items_sent );
428
			}
429
430
			if ( 0 !== $items_sent_total ) {
431
				$this->update_status_option( $total_option_name, $items_sent_total );
432
			}
433
		}
434
435
		if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) {
436
			$this->update_status_option( 'finished', time() );
437
		}
438
	}
439
440
	/**
441
	 * Get the name of the action for an item in the sync queue.
442
	 *
443
	 * @access public
444
	 *
445
	 * @param array $queue_item Item of the sync queue.
446
	 * @return string|boolean Name of the action, false if queue item is invalid.
447
	 */
448
	public function get_action_name( $queue_item ) {
449
		if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) {
450
			return $queue_item[0];
451
		}
452
		return false;
453
	}
454
455
	/**
456
	 * Retrieve the total number of items we're syncing in a particular queue item (action).
457
	 * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]`
458
	 * represents the first (and only) chunk of items to sync in that action.
459
	 *
460
	 * @access public
461
	 *
462
	 * @param array $queue_item Item of the sync queue that corresponds to a particular action.
463
	 * @return int Total number of items in the action.
464
	 */
465
	public function get_action_totals( $queue_item ) {
466
		if ( is_array( $queue_item ) && isset( $queue_item[1][0] ) ) {
467
			if ( is_array( $queue_item[1][0] ) ) {
468
				// Let's count the items we sync in this action.
469
				return count( $queue_item[1][0] );
470
			}
471
			// -1 indicates that this action syncs all items by design.
472
			return -1;
473
		}
474
		return 0;
475
	}
476
477
	/**
478
	 * Retrieve the total number of items for a set of actions, grouped by action name.
479
	 *
480
	 * @access public
481
	 *
482
	 * @param array $actions An array of actions.
483
	 * @return array An array, representing the total number of items, grouped per action.
484
	 */
485
	public function get_actions_totals( $actions ) {
486
		$totals = array();
487
488
		foreach ( $actions as $action ) {
489
			$name          = $this->get_action_name( $action );
490
			$action_totals = $this->get_action_totals( $action );
491
			if ( ! isset( $totals[ $name ] ) ) {
492
				$totals[ $name ] = 0;
493
			}
494
			$totals[ $name ] += $action_totals;
495
		}
496
497
		return $totals;
498
	}
499
500
	/**
501
	 * Whether full sync has started.
502
	 *
503
	 * @access public
504
	 *
505
	 * @return boolean
506
	 */
507
	public function is_started() {
508
		return ! ! $this->get_status_option( 'started' );
509
	}
510
511
	/**
512
	 * Whether full sync has finished.
513
	 *
514
	 * @access public
515
	 *
516
	 * @return boolean
517
	 */
518
	public function is_finished() {
519
		return ! ! $this->get_status_option( 'finished' );
520
	}
521
522
	/**
523
	 * Retrieve the status of the current full sync.
524
	 *
525
	 * @access public
526
	 *
527
	 * @return array Full sync status.
528
	 */
529
	public function get_status() {
530
		$status = array(
531
			'started'        => $this->get_status_option( 'started' ),
532
			'queue_finished' => $this->get_status_option( 'queue_finished' ),
533
			'send_started'   => $this->get_status_option( 'send_started' ),
534
			'finished'       => $this->get_status_option( 'finished' ),
535
			'sent'           => array(),
536
			'sent_total'     => array(),
537
			'queue'          => array(),
538
			'config'         => $this->get_status_option( 'params' ),
539
			'total'          => array(),
540
		);
541
542
		$enqueue_status = $this->get_enqueue_status();
543
544
		foreach ( Modules::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression \Automattic\Jetpack\Sync\Modules::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
545
			$name = $module->name();
546
547
			if ( ! isset( $enqueue_status[ $name ] ) ) {
548
				continue;
549
			}
550
551
			list( $total, $queued ) = $enqueue_status[ $name ];
552
553
			if ( $total ) {
554
				$status['total'][ $name ] = $total;
555
			}
556
557
			if ( $queued ) {
558
				$status['queue'][ $name ] = $queued;
559
			}
560
561
			$sent = $this->get_status_option( "{$name}_sent" );
562
			if ( $sent ) {
563
				$status['sent'][ $name ] = $sent;
564
			}
565
566
			$sent_total = $this->get_status_option( "{$name}_sent_total" );
567
			if ( $sent_total ) {
568
				$status['sent_total'][ $name ] = $sent_total;
569
			}
570
		}
571
572
		return $status;
573
	}
574
575
	/**
576
	 * Clear all the full sync status options.
577
	 *
578
	 * @access public
579
	 */
580
	public function clear_status() {
581
		$prefix = self::STATUS_OPTION_PREFIX;
582
		\Jetpack_Options::delete_raw_option( "{$prefix}_started" );
583
		\Jetpack_Options::delete_raw_option( "{$prefix}_params" );
584
		\Jetpack_Options::delete_raw_option( "{$prefix}_queue_finished" );
585
		\Jetpack_Options::delete_raw_option( "{$prefix}_send_started" );
586
		\Jetpack_Options::delete_raw_option( "{$prefix}_finished" );
587
588
		$this->delete_enqueue_status();
589
590
		foreach ( Modules::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression \Automattic\Jetpack\Sync\Modules::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
591
			\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent" );
592
			\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent_total" );
593
		}
594
	}
595
596
	/**
597
	 * Clear all the full sync data.
598
	 *
599
	 * @access public
600
	 */
601
	public function reset_data() {
602
		$this->clear_status();
603
		$this->delete_config();
604
605
		$listener = Listener::get_instance();
606
		$listener->get_full_sync_queue()->reset();
607
	}
608
609
	/**
610
	 * Get the value of a full sync status option.
611
	 *
612
	 * @access private
613
	 *
614
	 * @param string $name    Name of the option.
615
	 * @param mixed  $default Default value of the option.
616
	 * @return mixed Option value.
617
	 */
618
	private function get_status_option( $name, $default = null ) {
619
		$value = \Jetpack_Options::get_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $default );
620
621
		return is_numeric( $value ) ? intval( $value ) : $value;
622
	}
623
624
	/**
625
	 * Update the value of a full sync status option.
626
	 *
627
	 * @access private
628
	 *
629
	 * @param string  $name     Name of the option.
630
	 * @param mixed   $value    Value of the option.
631
	 * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request.
632
	 */
633
	private function update_status_option( $name, $value, $autoload = false ) {
634
		\Jetpack_Options::update_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload );
635
	}
636
637
	/**
638
	 * Set the full sync enqueue status.
639
	 *
640
	 * @access private
641
	 *
642
	 * @param array $new_status The new full sync enqueue status.
643
	 */
644
	private function set_enqueue_status( $new_status ) {
645
		\Jetpack_Options::update_raw_option( 'jetpack_sync_full_enqueue_status', $new_status );
646
	}
647
648
	/**
649
	 * Delete full sync enqueue status.
650
	 *
651
	 * @access private
652
	 *
653
	 * @return boolean Whether the status was deleted.
654
	 */
655
	private function delete_enqueue_status() {
656
		return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_enqueue_status' );
657
	}
658
659
	/**
660
	 * Retrieve the current full sync enqueue status.
661
	 *
662
	 * @access private
663
	 *
664
	 * @return array Full sync enqueue status.
665
	 */
666
	public function get_enqueue_status() {
667
		return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_enqueue_status' );
668
	}
669
670
	/**
671
	 * Set the full sync enqueue configuration.
672
	 *
673
	 * @access private
674
	 *
675
	 * @param array $config The new full sync enqueue configuration.
676
	 */
677
	private function set_config( $config ) {
678
		\Jetpack_Options::update_raw_option( 'jetpack_sync_full_config', $config );
679
	}
680
681
	/**
682
	 * Delete full sync configuration.
683
	 *
684
	 * @access private
685
	 *
686
	 * @return boolean Whether the configuration was deleted.
687
	 */
688
	private function delete_config() {
689
		return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_config' );
690
	}
691
692
	/**
693
	 * Retrieve the current full sync enqueue config.
694
	 *
695
	 * @access private
696
	 *
697
	 * @return array Full sync enqueue config.
698
	 */
699
	private function get_config() {
700
		return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_config' );
701
	}
702
703
	/**
704
	 * Update an option manually to bypass filters and caching.
705
	 *
706
	 * @access private
707
	 *
708
	 * @param string $name  Option name.
709
	 * @param mixed  $value Option value.
710
	 * @return int The number of updated rows in the database.
711
	 */
712
	private function write_option( $name, $value ) {
713
		// We write our own option updating code to bypass filters/caching/etc on set_option/get_option.
714
		global $wpdb;
715
		$serialized_value = maybe_serialize( $value );
716
717
		/**
718
		 * Try updating, if no update then insert
719
		 * TODO: try to deal with the fact that unchanged values can return updated_num = 0
720
		 * below we used "insert ignore" to at least suppress the resulting error.
721
		 */
722
		$updated_num = $wpdb->query(
723
			$wpdb->prepare(
724
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s",
725
				$serialized_value,
726
				$name
727
			)
728
		);
729
730
		if ( ! $updated_num ) {
731
			$updated_num = $wpdb->query(
732
				$wpdb->prepare(
733
					"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )",
734
					$name,
735
					$serialized_value
736
				)
737
			);
738
		}
739
		return $updated_num;
740
	}
741
742
	/**
743
	 * Update an option manually to bypass filters and caching.
744
	 *
745
	 * @access private
746
	 *
747
	 * @param string $name    Option name.
748
	 * @param mixed  $default Default option value.
749
	 * @return mixed Option value.
750
	 */
751
	private function read_option( $name, $default = null ) {
752
		global $wpdb;
753
		$value = $wpdb->get_var(
754
			$wpdb->prepare(
755
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
756
				$name
757
			)
758
		);
759
		$value = maybe_unserialize( $value );
760
761
		if ( null === $value && null !== $default ) {
762
			return $default;
763
		}
764
765
		return $value;
766
	}
767
768
	/**
769
	 * Prefix of the blog lock transient.
770
	 *
771
	 * @access public
772
	 *
773
	 * @var string
774
	 */
775
	const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_';
776
	/**
777
	 * Lifetime of the blog lock transient.
778
	 *
779
	 * @access public
780
	 *
781
	 * @var int
782
	 */
783
	const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds.
784
785
	/**
786
	 * Attempt to lock enqueueing when the server receives concurrent requests from the same blog.
787
	 *
788
	 * @access public
789
	 *
790
	 * @param int $expiry  enqueue transient lifetime.
791
	 * @return boolean True if succeeded, false otherwise.
792
	 */
793 View Code Duplication
	public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) {
794
		$transient_name = $this->get_concurrent_enqueue_transient_name();
795
		$locked_time    = get_site_transient( $transient_name );
796
		if ( $locked_time ) {
797
			return false;
798
		}
799
		set_site_transient( $transient_name, microtime( true ), $expiry );
800
		return true;
801
	}
802
	/**
803
	 * Retrieve the enqueue lock transient name for the current blog.
804
	 *
805
	 * @access public
806
	 *
807
	 * @return string Name of the blog lock transient.
808
	 */
809
	private function get_concurrent_enqueue_transient_name() {
810
		return self::ENQUEUE_LOCK_TRANSIENT_PREFIX . get_current_blog_id();
811
	}
812
	/**
813
	 * Remove the enqueue lock.
814
	 *
815
	 * @access public
816
	 */
817
	public function remove_enqueue_lock() {
818
		delete_site_transient( $this->get_concurrent_enqueue_transient_name() );
819
	}
820
}
821