Completed
Push — add/decouple-full-sync ( 633ca0 )
by
unknown
12:50
created

Jetpack_Sync_Sender::do_all_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php';
4
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
5
require_once dirname( __FILE__ ) . '/class.jetpack-sync-json-deflate-array-codec.php';
6
require_once dirname( __FILE__ ) . '/class.jetpack-sync-modules.php';
7
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
8
9
/**
10
 * This class grabs pending actions from the queue and sends them
11
 */
12
class Jetpack_Sync_Sender {
13
14
	const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait';
15
	const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time';
16
	const WPCOM_ERROR_SYNC_DELAY = 60;
17
18
	private $dequeue_max_bytes;
19
	private $upload_max_bytes;
20
	private $upload_max_rows;
21
	private $sync_wait_time;
22
	private $sync_wait_threshold;
23
	private $sync_queue;
24
	private $full_sync_queue;
25
	private $codec;
26
27
	// singleton functions
28
	private static $instance;
29
30
	public static function get_instance() {
31
		if ( null === self::$instance ) {
32
			self::$instance = new self();
33
		}
34
35
		return self::$instance;
36
	}
37
38
	// this is necessary because you can't use "new" when you declare instance properties >:(
39
	protected function __construct() {
40
		$this->set_defaults();
41
		$this->init();
42
	}
43
44
	private function init() {
45
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
46
			$module->init_before_send();
47
		}
48
	}
49
50
	public function get_next_sync_time( $queue_name ) {
51
		return (double) get_option( self::NEXT_SYNC_TIME_OPTION_NAME.'_'.$queue_name, 0 );
52
	}
53
54
	public function set_next_sync_time( $time, $queue_name ) {
55
		return update_option( self::NEXT_SYNC_TIME_OPTION_NAME.'_'.$queue_name, $time, true );
56
	}
57
58
	public function do_all_sync() {
59
		$this->do_full_sync();
60
		$this->do_sync();
61
	}
62
63
	public function do_full_sync() {
64
		return $this->do_sync_and_set_delays( $this->full_sync_queue );
65
	}
66
67
	public function do_sync() {
68
		return $this->do_sync_and_set_delays( $this->sync_queue );
69
	}
70
71
	public function do_sync_and_set_delays( $queue ) {
72
		// don't sync if importing
73
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
74
			return false;
75
		}
76
		
77
		// don't sync if we are throttled
78
		if ( $this->get_next_sync_time( $queue->id ) > microtime( true ) ) {
79
			return false;
80
		}
81
82
		$start_time = microtime( true );
83
84
		$sync_result = $this->do_sync_for_queue( $queue );
85
86
		$exceeded_sync_wait_threshold = ( microtime( true ) - $start_time ) > (double) $this->get_sync_wait_threshold();
87
88
		if ( is_wp_error( $sync_result ) ) {
89
			$this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id );
90
			$sync_result = false;
91
		} elseif ( $exceeded_sync_wait_threshold ) {
92
			// if we actually sent data and it took a while, wait before sending again
93
			$this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id );
94
		}
95
96
		return $sync_result;
97
	}
98
99
	public function do_sync_for_queue( $queue ) {
100
101
		do_action( 'jetpack_sync_before_send_queue_' . $queue->id );
102
103
		if ( $queue->size() === 0 ) {
104
			return false;
105
		}
106
107
		// now that we're sure we are about to sync, try to
108
		// ignore user abort so we can avoid getting into a
109
		// bad state
110
		if ( function_exists( 'ignore_user_abort' ) ) {
111
			ignore_user_abort( true );
112
		}
113
114
		$buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
115
116
		if ( ! $buffer ) {
117
			// buffer has no items
118
			return false;
119
		}
120
121
		if ( is_wp_error( $buffer ) ) {
122
			// another buffer is currently sending
123
			return false;
124
		}
125
126
		$upload_size   = 0;
127
		$items_to_send = array();
128
		$items         = $buffer->get_items();
129
130
		// set up current screen to avoid errors rendering content
131
		require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php');
132
		require_once(ABSPATH . 'wp-admin/includes/screen.php');
133
		set_current_screen( 'sync' );
134
135
		$skipped_items_ids = array();
136
137
		// we estimate the total encoded size as we go by encoding each item individually
138
		// this is expensive, but the only way to really know :/
139
		foreach ( $items as $key => $item ) {
140
			// Suspending cache addition help prevent overloading in memory cache of large sites.
141
			wp_suspend_cache_addition( true );
142
			/**
143
			 * Modify the data within an action before it is serialized and sent to the server
144
			 * For example, during full sync this expands Post ID's into full Post objects,
145
			 * so that we don't have to serialize the whole object into the queue.
146
			 *
147
			 * @since 4.2.0
148
			 *
149
			 * @param array The action parameters
150
			 * @param int The ID of the user who triggered the action
151
			 */
152
			$item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] );
153
			wp_suspend_cache_addition( false );
154
			if ( $item[1] === false ) {
155
				$skipped_items_ids[] = $key;
156
				continue;
157
			}
158
159
			$encoded_item = $this->codec->encode( $item );
160
161
			$upload_size += strlen( $encoded_item );
162
163
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
164
				break;
165
			}
166
167
			$items_to_send[ $key ] = $encoded_item;
168
		}
169
170
		/**
171
		 * Fires when data is ready to send to the server.
172
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
173
		 * The items will be automatically re-sent later
174
		 *
175
		 * @since 4.2.0
176
		 *
177
		 * @param array $data The action buffer
178
		 * @param string $codec The codec name used to encode the data
179
		 * @param double $time The current time
180
		 * @param string $queue The queue used to send ('sync' or 'full_sync')
181
		 */
182
		$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id );
183
184
		if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) {
185
			$checked_in_item_ids = $queue->checkin( $buffer );
186
187
			if ( is_wp_error( $checked_in_item_ids ) ) {
188
				error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() );
189
				$queue->force_checkin();
190
			}
191
192
			if ( is_wp_error( $processed_item_ids ) ) {
193
				return $processed_item_ids;
194
			}
195
196
			// returning a WP_Error is a sign to the caller that we should wait a while
197
			// before syncing again
198
			return new WP_Error( 'server_error' );
199
			
200
		} else {
201
202
			// detect if the last item ID was an error
203
			$had_wp_error = is_wp_error( end( $processed_item_ids ) );
204
205
			if ( $had_wp_error ) {
206
				$wp_error = array_pop( $processed_item_ids );
207
			}
208
209
			// also checkin any items that were skipped
210
			if ( count( $skipped_items_ids ) > 0 ) {
211
				$processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids );
212
			}
213
214
			$processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) );
215
216
			/**
217
			 * Allows us to keep track of all the actions that have been sent.
218
			 * Allows us to calculate the progress of specific actions.
219
			 *
220
			 * @since 4.2.0
221
			 *
222
			 * @param array $processed_actions The actions that we send successfully.
223
			 */
224
			do_action( 'jetpack_sync_processed_actions', $processed_items );
225
226
			$queue->close( $buffer, $processed_item_ids );
227
228
			// returning a WP_Error is a sign to the caller that we should wait a while
229
			// before syncing again
230
			if ( $had_wp_error ) {
231
				return $wp_error;
0 ignored issues
show
Bug introduced by
The variable $wp_error 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...
232
			} 
233
		}
234
		
235
		return true;
236
	}
237
238
	function get_sync_queue() {
239
		return $this->sync_queue;
240
	}
241
242
	function get_full_sync_queue() {
243
		return $this->full_sync_queue;
244
	}
245
246
	function get_codec() {
247
		return $this->codec;
248
	}
249
250
	function send_checksum() {
251
		require_once 'class.jetpack-sync-wp-replicastore.php';
252
		$store = new Jetpack_Sync_WP_Replicastore();
253
		do_action( 'jetpack_sync_checksum', $store->checksum_all() );
254
	}
255
256
	function reset_sync_queue() {
257
		$this->sync_queue->reset();
258
	}
259
260
	function set_dequeue_max_bytes( $size ) {
261
		$this->dequeue_max_bytes = $size;
262
	}
263
264
	// in bytes
265
	function set_upload_max_bytes( $max_bytes ) {
266
		$this->upload_max_bytes = $max_bytes;
267
	}
268
269
	// in rows
270
	function set_upload_max_rows( $max_rows ) {
271
		$this->upload_max_rows = $max_rows;
272
	}
273
274
	// in seconds
275
	function set_sync_wait_time( $seconds ) {
276
		$this->sync_wait_time = $seconds;
277
	}
278
279
	function get_sync_wait_time() {
280
		return $this->sync_wait_time;
281
	}
282
283
	// in seconds
284
	function set_sync_wait_threshold( $seconds ) {
285
		$this->sync_wait_threshold = $seconds;
286
	}
287
288
	function get_sync_wait_threshold() {
289
		return $this->sync_wait_threshold;
290
	}
291
292
	function set_defaults() {
293
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
294
		$this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' );
295
		$this->codec      = new Jetpack_Sync_JSON_Deflate_Array_Codec();
296
297
		// saved settings
298
		Jetpack_Sync_Settings::set_importing( null );
299
		$settings = Jetpack_Sync_Settings::get_settings();
300
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
301
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
302
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
303
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
304
		$this->set_sync_wait_threshold( $settings['sync_wait_threshold'] );
305
	}
306
307
	function reset_data() {
308
		$this->reset_sync_queue();
309
310
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
311
			$module->reset_data();
312
		}
313
314
		delete_option( self::SYNC_THROTTLE_OPTION_NAME );
315
		delete_option( self::NEXT_SYNC_TIME_OPTION_NAME );
316
317
		Jetpack_Sync_Settings::reset_data();
318
	}
319
320
	function uninstall() {
321
		// Lets delete all the other fun stuff like transient and option and the sync queue
322
		$this->reset_data();
323
324
		// delete the full sync status
325
		delete_option( 'jetpack_full_sync_status' );
326
327
		// clear the sync cron.
328
		wp_clear_scheduled_hook( 'jetpack_sync_cron' );
329
	}
330
}
331