Completed
Push — fix/inline-docs-410 ( f96891...63b75c )
by
unknown
43:24 queued 33:40
created

Jetpack_Sync_Sender::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 9.4285
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-codec.php';
6
require_once dirname( __FILE__ ) . '/class.jetpack-sync-full.php';
7
require_once dirname( __FILE__ ) . '/class.jetpack-sync-modules.php';
8
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
9
10
/** 
11
 * This class grabs pending actions from the queue and sends them
12
 */
13
class Jetpack_Sync_Sender {
14
15
	const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait';
16
	const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time';
17
18
	private $dequeue_max_bytes;
19
	private $upload_max_bytes;
20
	private $upload_max_rows;
21
	private $sync_queue;
22
	private $full_sync_client;
23
	private $codec;
24
25
	// singleton functions
26
	private static $instance;
27
28
	public static function getInstance() {
29
		if ( null === self::$instance ) {
30
			self::$instance = new self();
31
		}
32
33
		return self::$instance;
34
	}
35
36
	// this is necessary because you can't use "new" when you declare instance properties >:(
37
	protected function __construct() {
38
		$this->set_defaults();
39
		$this->init();
40
	}
41
42
	private function init() {
43
44
		foreach( Jetpack_Sync_Modules::get_modules() as $module ) {
45
			$module->init_before_send();
46
		}
47
48
		/**
49
		 * Sync all pending actions with server
50
		 */
51
		add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) );
52
	}
53
54
	public function do_sync() {
55
		// don't sync if importing
56
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
57
			$this->schedule_sync( "+1 minute" );
58
59
			return false;
60
		}
61
62
		// don't sync if we are throttled
63
		$sync_wait = $this->get_sync_wait_time();
64
		$last_sync = $this->get_last_sync_time();
65
66
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
67
			return false;
68
		}
69
70
		$this->set_last_sync_time();
71
72
		do_action( 'jetpack_sync_before_send' );
73
74
		if ( $this->sync_queue->size() === 0 ) {
75
			return false;
76
		}
77
78
		// now that we're sure we are about to sync, try to
79
		// ignore user abort so we can avoid getting into a
80
		// bad state
81
		if ( function_exists( 'ignore_user_abort' ) ) {
82
			ignore_user_abort( true );
83
		}
84
85
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
86
87
		if ( ! $buffer ) {
88
			// buffer has no items
89
			return false;
90
		}
91
92
		if ( is_wp_error( $buffer ) ) {
93
			// another buffer is currently sending
94
			return false;
95
		}
96
97
		$upload_size   = 0;
98
		$items_to_send = array();
99
		$actions_to_send = array();
100
		// we estimate the total encoded size as we go by encoding each item individually
101
		// this is expensive, but the only way to really know :/
102
		foreach ( $buffer->get_items() as $key => $item ) {
103
			/**
104
			 * Modify the data within an action before it is serialized and sent to the server
105
			 * For example, during full sync this expands Post ID's into full Post objects,
106
			 * so that we don't have to serialize the whole object into the queue.
107
			 *
108
			 * @since 4.2.0
109
			 *
110
			 * @param array The action parameters
111
			 */
112
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
113
114
			$encoded_item = $this->codec->encode( $item );
115
116
			$upload_size += strlen( $encoded_item );
117
118
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
119
				break;
120
			}
121
122
			$items_to_send[ $key ] = $encoded_item;
123
			$actions_to_send[ $key ] = $item[0];
124
		}
125
126
		/**
127
		 * Fires when data is ready to send to the server.
128
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
129
		 * The items will be automatically re-sent later
130
		 *
131
		 * @since 4.2.0
132
		 *
133
		 * @param array $data The action buffer
134
		 */
135
		$result = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name() );
136
137
		if ( ! $result || is_wp_error( $result ) ) {
138
			$result = $this->sync_queue->checkin( $buffer );
139
140
			if ( is_wp_error( $result ) ) {
141
				error_log( "Error checking in buffer: " . $result->get_error_message() );
142
				$this->sync_queue->force_checkin();
143
			}
144
			// try again in 1 minute
145
			$this->schedule_sync( "+1 minute" );
146
		} else {
147
			$processed_actions = array();
148
			foreach( $result as $result_id ) {
149
				if ( isset( $actions_to_send[ $result_id ] ) ) {
150
					$processed_actions[] =  $actions_to_send[ $result_id ];
151
				}
152
			}
153
154
			/**
155
			 * Allows us to keep track of all the actions that have been sent.
156
			 * Allows us to calculate the progress of specific actions.
157
			 *
158
			 * @since 4.2.0
159
			 *
160
			 * @param array $processed_actions The actions that we send successfully.
161
			 */
162
			do_action( 'jetpack_sync_processed_actions', $processed_actions );
163
164
165
			$this->sync_queue->close( $buffer, $result );
166
			// check if there are any more events in the buffer
167
			// if so, schedule a cron job to happen soon
168
			if ( $this->sync_queue->has_any_items() ) {
169
				$this->schedule_sync( "+1 minute" );
170
			}
171
		}
172
	}
173
174
	private function schedule_sync( $when ) {
175
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
176
	}
177
178
	function get_sync_queue() {
179
		return $this->sync_queue;
180
	}
181
182
	function get_codec() {
183
		return $this->codec;
184
	}
185
186
	function send_checksum() {
187
		require_once 'class.jetpack-sync-wp-replicastore.php';
188
		$store = new Jetpack_Sync_WP_Replicastore();
189
		do_action( 'jetpack_sync_checksum', $store->checksum_all() );
190
	}
191
192
	function reset_sync_queue() {
193
		$this->sync_queue->reset();
194
	}
195
196
	function set_dequeue_max_bytes( $size ) {
197
		$this->dequeue_max_bytes = $size;
198
	}
199
200
	// in bytes
201
	function set_upload_max_bytes( $max_bytes ) {
202
		$this->upload_max_bytes = $max_bytes;
203
	}
204
205
	// in rows
206
	function set_upload_max_rows( $max_rows ) {
207
		$this->upload_max_rows = $max_rows;
208
	}
209
210
	// in seconds
211
	function set_sync_wait_time( $seconds ) {
212
		update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true );
213
	}
214
215
	function get_sync_wait_time() {
216
		return (int) get_option( self::SYNC_THROTTLE_OPTION_NAME );
217
	}
218
219
	private function get_last_sync_time() {
220
		return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME );
221
	}
222
223
	private function set_last_sync_time() {
224
		return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true );
225
	}
226
227
	function get_full_sync_client() {
228
		return $this->full_sync_client;
229
	}
230
231
	function set_full_sync_client( $full_sync_client ) {
232
		if ( $this->full_sync_client ) {
233
			remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
234
		}
235
236
		$this->full_sync_client = $full_sync_client;
237
238
		/**
239
		 * Sync all objects in the database with the server
240
		 */
241
		add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
242
	}
243
244
	function set_defaults() {
245
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
246
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
247
		$this->codec = new Jetpack_Sync_JSON_Deflate_Codec();
248
249
		// saved settings
250
		$settings = Jetpack_Sync_Settings::get_settings();
251
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
252
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
253
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
254
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
255
	}
256
257
	function reset_data() {
258
		$this->reset_sync_queue();
259
260
		foreach( Jetpack_Sync_Modules::get_modules() as $module ) {
261
			$module->reset_data();
262
		}
263
		
264
		delete_option( self::SYNC_THROTTLE_OPTION_NAME );
265
		delete_option( self::LAST_SYNC_TIME_OPTION_NAME );
266
267
		Jetpack_Sync_Settings::reset_data();
268
	}
269
270
	function uninstall() {
271
		// Lets delete all the other fun stuff like transient and option and the sync queue
272
		$this->reset_data();
273
274
		// delete the full sync status
275
		delete_option( 'jetpack_full_sync_status' );
276
277
		// clear the sync cron.
278
		wp_clear_scheduled_hook( 'jetpack_sync_actions' );
279
280
		// clear the checksum cron
281
		wp_clear_scheduled_hook( 'jetpack_send_db_checksum' );
282
	}
283
}
284