Completed
Push — fix/check-queue-status-before-... ( 30a5c8...0f047d )
by
unknown
07:19 queued 21s
created

Full_Sync::queue_full_sync_end()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 18
rs 9.6666
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
	 * Enqueue the next items to sync.
223
	 *
224
	 * @access public
225
	 *
226
	 * @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...
227
	 */
228
	public function continue_enqueuing_with_lock( $configs = null ) {
229
		if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) {
230
			return;
231
		}
232
233
		if ( ! $configs ) {
234
			$configs = $this->get_config();
235
		}
236
237
		$this->remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $this->get_available_queue_slots() );
238
239
		$modules           = Modules::get_modules();
240
		$modules_processed = 0;
241
		foreach ( $modules as $module ) {
0 ignored issues
show
Bug introduced by
The expression $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...
242
			if ( 0 >= $this->remaining_items_to_enqueue ) {
243
				return;
244
			}
245
			$modules_processed += $this->enqueue_module( $module, $configs, $this->enqueue_status[ $module->name() ], $this->remaining_items_to_enqueue );
0 ignored issues
show
Unused Code introduced by
The call to Full_Sync::enqueue_module() has too many arguments starting with $this->remaining_items_to_enqueue.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
246
			// Stop processing if we've reached our limit of items to enqueue.
247
		}
248
249
		if ( count( $modules ) === $modules_processed ) {
250
			$this->queue_full_sync_end( $configs );
251
		}
252
	}
253
254
	/**
255
	 * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status.
256
	 *
257
	 * @access public
258
	 *
259
	 * @param array $configs Full sync configuration for all sync modules.
260
	 */
261
	public function queue_full_sync_end( $configs ) {
262
		$range = $this->get_content_range( $configs );
263
264
		/**
265
		 * Fires when a full sync ends. This action is serialized
266
		 * and sent to the server.
267
		 *
268
		 * @since 4.2.0
269
		 * @since 7.3.0 Added $range arg.
270
		 *
271
		 * @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/
272
		 * @param array  $range    Range of the sync items, containing min and max IDs for some item types.
273
		 */
274
		do_action( 'jetpack_full_sync_end', '', $range );
275
276
		// Setting autoload to true means that it's faster to check whether we should continue enqueuing.
277
		$this->update_status_option( 'queue_finished', time(), true );
278
	}
279
	/**
280
	 * Enqueue Full Sync Actions for the given module.
281
	 *
282
	 * @param Object $module The module to Enqueue.
283
	 * @param array  $configs The Full sync configuration for all modules.
284
	 * @param array  $status The Full sync enqueue status for the module.
285
	 *
286
	 * @return int
287
	 */
288
	public function enqueue_module( $module, $configs, $status ) {
289
		// Skip module if not configured for this sync or module is done.
290
		if ( ! isset( $configs[ $module->name() ] )
291
			 || // No module config.
292
			 ! $configs[ $module->name() ]
293
			 || // No enqueue status.
294
			 ! $status
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

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