Completed
Push — fix/check-queue-status-before-... ( 0f047d...3b1e08 )
by
unknown
09:51 queued 03:03
created

Full_Sync::get_remaining_modules_to_enqueue()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 1
dl 0
loc 22
rs 9.2568
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->attempt_enqueue_lock() ) {
197
			return;
198
		}
199
200
		$this->enqueue_status = $enqueue_status ? $enqueue_status : $this->get_enqueue_status();
201
		$this->continue_enqueuing_with_lock( $configs );
202
		$this->set_enqueue_status( $this->enqueue_status );
203
204
		$this->remove_enqueue_lock();
205
	}
206
207
	/**
208
	 *
209
	 * Get Available Full Sync queue slots.
210
	 *
211
	 * @return int
212
	 */
213
	public function get_available_queue_slots() {
214
		// If full sync queue is full, don't enqueue more items.
215
		$max_queue_size_full_sync = Settings::get_setting( 'max_queue_size_full_sync' );
216
		$full_sync_queue          = new Queue( 'full_sync' );
217
218
		return $max_queue_size_full_sync - $full_sync_queue->size();
219
	}
220
221
	/**
222
	 * Get Modules that are configured to Full Sync and haven't finished enqueuing
223
	 *
224
	 * @param array $configs Full sync configuration for all sync modules.
225
	 *
226
	 * @return array
227
	 */
228
	public function get_remaining_modules_to_enqueue( $configs ) {
229
		return array_filter(
230
			Modules::get_modules(),
231
			function ( $module ) use ( $configs ) {
232
				// Skip module if not configured for this sync or module is done.
233
				if ( ! isset( $configs[ $module->name() ] ) ) {
234
					return false;
235
				}
236
				if ( ! $configs[ $module->name() ] ) {
237
					return false;
238
				}
239
				if ( ! isset( $this->enqueue_status[ $module->name() ] ) ) {
240
					return false;
241
				}
242
				if ( true === $this->enqueue_status[ $module->name() ][2] ) {
243
					return false;
244
				}
245
246
				return true;
247
			}
248
		);
249
	}
250
251
	/**
252
	 * Enqueue the next items to sync.
253
	 *
254
	 * @access public
255
	 *
256
	 * @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...
257
	 */
258
	public function continue_enqueuing_with_lock( $configs = null ) {
259
		if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) {
260
			return;
261
		}
262
263
		if ( ! $configs ) {
264
			$configs = $this->get_config();
265
		}
266
267
		$this->remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $this->get_available_queue_slots() );
268
269
		foreach ( $this->get_remaining_modules_to_enqueue( $configs ) as $module ) {
270
			if ( 0 >= $this->remaining_items_to_enqueue ) {
271
				return;
272
			}
273
			$this->enqueue_module( $module, $configs[ $module->name() ] );
274
			// Stop processing if we've reached our limit of items to enqueue.
275
		}
276
277
		$this->queue_full_sync_end( $configs );
278
	}
279
280
	/**
281
	 * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status.
282
	 *
283
	 * @access public
284
	 *
285
	 * @param array $configs Full sync configuration for all sync modules.
286
	 */
287
	public function queue_full_sync_end( $configs ) {
288
		$range = $this->get_content_range( $configs );
289
290
		/**
291
		 * Fires when a full sync ends. This action is serialized
292
		 * and sent to the server.
293
		 *
294
		 * @since 4.2.0
295
		 * @since 7.3.0 Added $range arg.
296
		 *
297
		 * @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/
298
		 * @param array  $range    Range of the sync items, containing min and max IDs for some item types.
299
		 */
300
		do_action( 'jetpack_full_sync_end', '', $range );
301
302
		// Setting autoload to true means that it's faster to check whether we should continue enqueuing.
303
		$this->update_status_option( 'queue_finished', time(), true );
304
	}
305
	/**
306
	 * Enqueue Full Sync Actions for the given module.
307
	 *
308
	 * @param Object $module The module to Enqueue.
309
	 * @param array  $config The Full sync configuration for the modules.
310
	 *
311
	 * @return int
312
	 */
313
	public function enqueue_module( $module, $config ) {
314
		list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $config, $this->remaining_items_to_enqueue, $this->enqueue_status[ $module->name() ][2] );
315
316
		$this->enqueue_status[ $module->name() ][2] = $next_enqueue_state;
317
318
		// If items were processed, subtract them from the limit.
319
		if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) {
320
			$this->enqueue_status[ $module->name() ][1] += $items_enqueued;
321
			$this->remaining_items_to_enqueue           -= $items_enqueued;
322
		}
323
324
		return true === $next_enqueue_state ? 1 : 0;
325
	}
326
327
	/**
328
	 * Get the range (min ID, max ID and total items) of items to sync.
329
	 *
330
	 * @access public
331
	 *
332
	 * @param string $type Type of sync item to get the range for.
333
	 * @return array Array of min ID, max ID and total items in the range.
334
	 */
335
	public function get_range( $type ) {
336
		global $wpdb;
337
		if ( ! in_array( $type, array( 'comments', 'posts' ), true ) ) {
338
			return array();
339
		}
340
341
		switch ( $type ) {
342
			case 'posts':
343
				$table     = $wpdb->posts;
344
				$id        = 'ID';
345
				$where_sql = Settings::get_blacklisted_post_types_sql();
346
347
				break;
348
			case 'comments':
349
				$table     = $wpdb->comments;
350
				$id        = 'comment_ID';
351
				$where_sql = Settings::get_comments_filter_sql();
352
				break;
353
		}
354
355
		// TODO: Call $wpdb->prepare on the following query.
356
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
357
		$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...
358
		if ( isset( $results[0] ) ) {
359
			return $results[0];
360
		}
361
362
		return array();
363
	}
364
365
	/**
366
	 * Get the range for content (posts and comments) to sync.
367
	 *
368
	 * @access private
369
	 *
370
	 * @param array $config Full sync configuration for this all sync modules.
371
	 * @return array Array of range (min ID, max ID, total items) for all content types.
372
	 */
373
	private function get_content_range( $config ) {
374
		$range = array();
375
		// Only when we are sending the whole range do we want to send also the range.
376 View Code Duplication
		if ( true === isset( $config['posts'] ) && $config['posts'] ) {
377
			$range['posts'] = $this->get_range( 'posts' );
378
		}
379
380 View Code Duplication
		if ( true === isset( $config['comments'] ) && $config['comments'] ) {
381
			$range['comments'] = $this->get_range( 'comments' );
382
		}
383
		return $range;
384
	}
385
386
	/**
387
	 * Update the progress after sync modules actions have been processed on the server.
388
	 *
389
	 * @access public
390
	 *
391
	 * @param array $actions Actions that have been processed on the server.
392
	 */
393
	public function update_sent_progress_action( $actions ) {
394
		// Quick way to map to first items with an array of arrays.
395
		$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) );
396
397
		// Total item counts for each action.
398
		$actions_with_total_counts = $this->get_actions_totals( $actions );
399
400
		if ( ! $this->is_started() || $this->is_finished() ) {
401
			return;
402
		}
403
404
		if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) {
405
			$this->update_status_option( 'send_started', time() );
406
		}
407
408
		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...
409
			$module_actions     = $module->get_full_sync_actions();
410
			$status_option_name = "{$module->name()}_sent";
411
			$total_option_name  = "{$status_option_name}_total";
412
			$items_sent         = $this->get_status_option( $status_option_name, 0 );
413
			$items_sent_total   = $this->get_status_option( $total_option_name, 0 );
414
415
			foreach ( $module_actions as $module_action ) {
416
				if ( isset( $actions_with_counts[ $module_action ] ) ) {
417
					$items_sent += $actions_with_counts[ $module_action ];
418
				}
419
420
				if ( ! empty( $actions_with_total_counts[ $module_action ] ) ) {
421
					$items_sent_total += $actions_with_total_counts[ $module_action ];
422
				}
423
			}
424
425
			if ( $items_sent > 0 ) {
426
				$this->update_status_option( $status_option_name, $items_sent );
427
			}
428
429
			if ( 0 !== $items_sent_total ) {
430
				$this->update_status_option( $total_option_name, $items_sent_total );
431
			}
432
		}
433
434
		if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) {
435
			$this->update_status_option( 'finished', time() );
436
		}
437
	}
438
439
	/**
440
	 * Get the name of the action for an item in the sync queue.
441
	 *
442
	 * @access public
443
	 *
444
	 * @param array $queue_item Item of the sync queue.
445
	 * @return string|boolean Name of the action, false if queue item is invalid.
446
	 */
447
	public function get_action_name( $queue_item ) {
448
		if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) {
449
			return $queue_item[0];
450
		}
451
		return false;
452
	}
453
454
	/**
455
	 * Retrieve the total number of items we're syncing in a particular queue item (action).
456
	 * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]`
457
	 * represents the first (and only) chunk of items to sync in that action.
458
	 *
459
	 * @access public
460
	 *
461
	 * @param array $queue_item Item of the sync queue that corresponds to a particular action.
462
	 * @return int Total number of items in the action.
463
	 */
464
	public function get_action_totals( $queue_item ) {
465
		if ( is_array( $queue_item ) && isset( $queue_item[1][0] ) ) {
466
			if ( is_array( $queue_item[1][0] ) ) {
467
				// Let's count the items we sync in this action.
468
				return count( $queue_item[1][0] );
469
			}
470
			// -1 indicates that this action syncs all items by design.
471
			return -1;
472
		}
473
		return 0;
474
	}
475
476
	/**
477
	 * Retrieve the total number of items for a set of actions, grouped by action name.
478
	 *
479
	 * @access public
480
	 *
481
	 * @param array $actions An array of actions.
482
	 * @return array An array, representing the total number of items, grouped per action.
483
	 */
484
	public function get_actions_totals( $actions ) {
485
		$totals = array();
486
487
		foreach ( $actions as $action ) {
488
			$name          = $this->get_action_name( $action );
489
			$action_totals = $this->get_action_totals( $action );
490
			if ( ! isset( $totals[ $name ] ) ) {
491
				$totals[ $name ] = 0;
492
			}
493
			$totals[ $name ] += $action_totals;
494
		}
495
496
		return $totals;
497
	}
498
499
	/**
500
	 * Whether full sync has started.
501
	 *
502
	 * @access public
503
	 *
504
	 * @return boolean
505
	 */
506
	public function is_started() {
507
		return ! ! $this->get_status_option( 'started' );
508
	}
509
510
	/**
511
	 * Whether full sync has finished.
512
	 *
513
	 * @access public
514
	 *
515
	 * @return boolean
516
	 */
517
	public function is_finished() {
518
		return ! ! $this->get_status_option( 'finished' );
519
	}
520
521
	/**
522
	 * Retrieve the status of the current full sync.
523
	 *
524
	 * @access public
525
	 *
526
	 * @return array Full sync status.
527
	 */
528
	public function get_status() {
529
		$status = array(
530
			'started'        => $this->get_status_option( 'started' ),
531
			'queue_finished' => $this->get_status_option( 'queue_finished' ),
532
			'send_started'   => $this->get_status_option( 'send_started' ),
533
			'finished'       => $this->get_status_option( 'finished' ),
534
			'sent'           => array(),
535
			'sent_total'     => array(),
536
			'queue'          => array(),
537
			'config'         => $this->get_status_option( 'params' ),
538
			'total'          => array(),
539
		);
540
541
		$enqueue_status = $this->get_enqueue_status();
542
543
		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...
544
			$name = $module->name();
545
546
			if ( ! isset( $enqueue_status[ $name ] ) ) {
547
				continue;
548
			}
549
550
			list( $total, $queued ) = $enqueue_status[ $name ];
551
552
			if ( $total ) {
553
				$status['total'][ $name ] = $total;
554
			}
555
556
			if ( $queued ) {
557
				$status['queue'][ $name ] = $queued;
558
			}
559
560
			$sent = $this->get_status_option( "{$name}_sent" );
561
			if ( $sent ) {
562
				$status['sent'][ $name ] = $sent;
563
			}
564
565
			$sent_total = $this->get_status_option( "{$name}_sent_total" );
566
			if ( $sent_total ) {
567
				$status['sent_total'][ $name ] = $sent_total;
568
			}
569
		}
570
571
		return $status;
572
	}
573
574
	/**
575
	 * Clear all the full sync status options.
576
	 *
577
	 * @access public
578
	 */
579
	public function clear_status() {
580
		$prefix = self::STATUS_OPTION_PREFIX;
581
		\Jetpack_Options::delete_raw_option( "{$prefix}_started" );
582
		\Jetpack_Options::delete_raw_option( "{$prefix}_params" );
583
		\Jetpack_Options::delete_raw_option( "{$prefix}_queue_finished" );
584
		\Jetpack_Options::delete_raw_option( "{$prefix}_send_started" );
585
		\Jetpack_Options::delete_raw_option( "{$prefix}_finished" );
586
587
		$this->delete_enqueue_status();
588
589
		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...
590
			\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent" );
591
			\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent_total" );
592
		}
593
	}
594
595
	/**
596
	 * Clear all the full sync data.
597
	 *
598
	 * @access public
599
	 */
600
	public function reset_data() {
601
		$this->clear_status();
602
		$this->delete_config();
603
604
		$listener = Listener::get_instance();
605
		$listener->get_full_sync_queue()->reset();
606
	}
607
608
	/**
609
	 * Get the value of a full sync status option.
610
	 *
611
	 * @access private
612
	 *
613
	 * @param string $name    Name of the option.
614
	 * @param mixed  $default Default value of the option.
615
	 * @return mixed Option value.
616
	 */
617
	private function get_status_option( $name, $default = null ) {
618
		$value = \Jetpack_Options::get_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $default );
619
620
		return is_numeric( $value ) ? intval( $value ) : $value;
621
	}
622
623
	/**
624
	 * Update the value of a full sync status option.
625
	 *
626
	 * @access private
627
	 *
628
	 * @param string  $name     Name of the option.
629
	 * @param mixed   $value    Value of the option.
630
	 * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request.
631
	 */
632
	private function update_status_option( $name, $value, $autoload = false ) {
633
		\Jetpack_Options::update_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload );
634
	}
635
636
	/**
637
	 * Set the full sync enqueue status.
638
	 *
639
	 * @access private
640
	 *
641
	 * @param array $new_status The new full sync enqueue status.
642
	 */
643
	private function set_enqueue_status( $new_status ) {
644
		\Jetpack_Options::update_raw_option( 'jetpack_sync_full_enqueue_status', $new_status );
645
	}
646
647
	/**
648
	 * Delete full sync enqueue status.
649
	 *
650
	 * @access private
651
	 *
652
	 * @return boolean Whether the status was deleted.
653
	 */
654
	private function delete_enqueue_status() {
655
		return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_enqueue_status' );
656
	}
657
658
	/**
659
	 * Retrieve the current full sync enqueue status.
660
	 *
661
	 * @access private
662
	 *
663
	 * @return array Full sync enqueue status.
664
	 */
665
	public function get_enqueue_status() {
666
		return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_enqueue_status' );
667
	}
668
669
	/**
670
	 * Set the full sync enqueue configuration.
671
	 *
672
	 * @access private
673
	 *
674
	 * @param array $config The new full sync enqueue configuration.
675
	 */
676
	private function set_config( $config ) {
677
		\Jetpack_Options::update_raw_option( 'jetpack_sync_full_config', $config );
678
	}
679
680
	/**
681
	 * Delete full sync configuration.
682
	 *
683
	 * @access private
684
	 *
685
	 * @return boolean Whether the configuration was deleted.
686
	 */
687
	private function delete_config() {
688
		return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_config' );
689
	}
690
691
	/**
692
	 * Retrieve the current full sync enqueue config.
693
	 *
694
	 * @access private
695
	 *
696
	 * @return array Full sync enqueue config.
697
	 */
698
	private function get_config() {
699
		return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_config' );
700
	}
701
702
	/**
703
	 * Update an option manually to bypass filters and caching.
704
	 *
705
	 * @access private
706
	 *
707
	 * @param string $name  Option name.
708
	 * @param mixed  $value Option value.
709
	 * @return int The number of updated rows in the database.
710
	 */
711
	private function write_option( $name, $value ) {
712
		// We write our own option updating code to bypass filters/caching/etc on set_option/get_option.
713
		global $wpdb;
714
		$serialized_value = maybe_serialize( $value );
715
716
		/**
717
		 * Try updating, if no update then insert
718
		 * TODO: try to deal with the fact that unchanged values can return updated_num = 0
719
		 * below we used "insert ignore" to at least suppress the resulting error.
720
		 */
721
		$updated_num = $wpdb->query(
722
			$wpdb->prepare(
723
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s",
724
				$serialized_value,
725
				$name
726
			)
727
		);
728
729
		if ( ! $updated_num ) {
730
			$updated_num = $wpdb->query(
731
				$wpdb->prepare(
732
					"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )",
733
					$name,
734
					$serialized_value
735
				)
736
			);
737
		}
738
		return $updated_num;
739
	}
740
741
	/**
742
	 * Update an option manually to bypass filters and caching.
743
	 *
744
	 * @access private
745
	 *
746
	 * @param string $name    Option name.
747
	 * @param mixed  $default Default option value.
748
	 * @return mixed Option value.
749
	 */
750
	private function read_option( $name, $default = null ) {
751
		global $wpdb;
752
		$value = $wpdb->get_var(
753
			$wpdb->prepare(
754
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
755
				$name
756
			)
757
		);
758
		$value = maybe_unserialize( $value );
759
760
		if ( null === $value && null !== $default ) {
761
			return $default;
762
		}
763
764
		return $value;
765
	}
766
767
	/**
768
	 * Prefix of the blog lock transient.
769
	 *
770
	 * @access public
771
	 *
772
	 * @var string
773
	 */
774
	const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_';
775
	/**
776
	 * Lifetime of the blog lock transient.
777
	 *
778
	 * @access public
779
	 *
780
	 * @var int
781
	 */
782
	const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds.
783
784
	/**
785
	 * Attempt to lock enqueueing when the server receives concurrent requests from the same blog.
786
	 *
787
	 * @access public
788
	 *
789
	 * @param int $expiry  enqueue transient lifetime.
790
	 * @return boolean True if succeeded, false otherwise.
791
	 */
792 View Code Duplication
	public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) {
793
		$transient_name = $this->get_concurrent_enqueue_transient_name();
794
		$locked_time    = get_site_transient( $transient_name );
795
		if ( $locked_time ) {
796
			return false;
797
		}
798
		set_site_transient( $transient_name, microtime( true ), $expiry );
799
		return true;
800
	}
801
	/**
802
	 * Retrieve the enqueue lock transient name for the current blog.
803
	 *
804
	 * @access public
805
	 *
806
	 * @return string Name of the blog lock transient.
807
	 */
808
	private function get_concurrent_enqueue_transient_name() {
809
		return self::ENQUEUE_LOCK_TRANSIENT_PREFIX . get_current_blog_id();
810
	}
811
	/**
812
	 * Remove the enqueue lock.
813
	 *
814
	 * @access public
815
	 */
816
	public function remove_enqueue_lock() {
817
		delete_site_transient( $this->get_concurrent_enqueue_transient_name() );
818
	}
819
}
820