Completed
Push — try/sync-package ( 228b13 )
by Marin
07:37
created

Module_Updates::enqueue_full_sync_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync;
4
5
class Module_Updates extends Module {
6
7
	const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum';
8
9
	private $old_wp_version = null;
10
	private $updates        = array();
11
12
	public function set_defaults() {
13
		$this->updates = array();
14
	}
15
16
	function name() {
17
		return 'updates';
18
	}
19
20
	public function init_listeners( $callable ) {
21
		global $wp_version;
22
		$this->old_wp_version = $wp_version;
23
		add_action( 'set_site_transient_update_plugins', array( $this, 'validate_update_change' ), 10, 3 );
24
		add_action( 'set_site_transient_update_themes', array( $this, 'validate_update_change' ), 10, 3 );
25
		add_action( 'set_site_transient_update_core', array( $this, 'validate_update_change' ), 10, 3 );
26
27
		add_action( 'jetpack_update_plugins_change', $callable );
28
		add_action( 'jetpack_update_themes_change', $callable );
29
		add_action( 'jetpack_update_core_change', $callable );
30
31
		add_filter(
32
			'jetpack_sync_before_enqueue_jetpack_update_plugins_change',
33
			array(
34
				$this,
35
				'filter_update_keys',
36
			),
37
			10,
38
			2
39
		);
40
		add_filter(
41
			'jetpack_sync_before_enqueue_upgrader_process_complete',
42
			array(
43
				$this,
44
				'filter_upgrader_process_complete',
45
			),
46
			10,
47
			2
48
		);
49
50
		add_action( 'automatic_updates_complete', $callable );
51
52
		if ( is_multisite() ) {
53
			add_filter( 'pre_update_site_option_wpmu_upgrade_site', array( $this, 'update_core_network_event' ), 10, 2 );
54
			add_action( 'jetpack_sync_core_update_network', $callable, 10, 3 );
55
		}
56
57
		// Send data when update completes
58
		add_action( '_core_updated_successfully', array( $this, 'update_core' ) );
59
		add_action( 'jetpack_sync_core_reinstalled_successfully', $callable );
60
		add_action( 'jetpack_sync_core_autoupdated_successfully', $callable, 10, 2 );
61
		add_action( 'jetpack_sync_core_updated_successfully', $callable, 10, 2 );
62
63
	}
64
65
	public function init_full_sync_listeners( $callable ) {
66
		add_action( 'jetpack_full_sync_updates', $callable );
67
	}
68
69
	public function init_before_send() {
70
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) );
71
		add_filter( 'jetpack_sync_before_send_jetpack_update_themes_change', array( $this, 'expand_themes' ) );
72
	}
73
74
	public function update_core_network_event( $wp_db_version, $old_wp_db_version ) {
75
		global $wp_version;
76
		/**
77
		 * Sync event for when core wp network updates to a new db version
78
		 *
79
		 * @since 5.0.0
80
		 *
81
		 * @param int $wp_db_version the latest wp_db_version
82
		 * @param int $old_wp_db_version previous wp_db_version
83
		 * @param string $wp_version the latest wp_version
84
		 */
85
		do_action( 'jetpack_sync_core_update_network', $wp_db_version, $old_wp_db_version, $wp_version );
86
		return $wp_db_version;
87
	}
88
89
	public function update_core( $new_wp_version ) {
90
		global $pagenow;
91
92
		if ( isset( $_GET['action'] ) && 'do-core-reinstall' === $_GET['action'] ) {
93
			/**
94
			 * Sync event that fires when core reinstall was successful
95
			 *
96
			 * @since 5.0.0
97
			 *
98
			 * @param string $new_wp_version the updated WordPress version
99
			 */
100
			do_action( 'jetpack_sync_core_reinstalled_successfully', $new_wp_version );
101
			return;
102
		}
103
104
		// Core was autoudpated
105
		if (
106
			'update-core.php' !== $pagenow &&
107
			! Jetpack_Constants::is_true( 'REST_API_REQUEST' ) // wp.com rest api calls should never be marked as a core autoupdate
108
		) {
109
			/**
110
			 * Sync event that fires when core autoupdate was successful
111
			 *
112
			 * @since 5.0.0
113
			 *
114
			 * @param string $new_wp_version the updated WordPress version
115
			 * @param string $old_wp_version the previous WordPress version
116
			 */
117
			do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version );
118
			return;
119
		}
120
		/**
121
		 * Sync event that fires when core update was successful
122
		 *
123
		 * @since 5.0.0
124
		 *
125
		 * @param string $new_wp_version the updated WordPress version
126
		 * @param string $old_wp_version the previous WordPress version
127
		 */
128
		do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version );
129
		return;
130
131
	}
132
133
	public function get_update_checksum( $update, $transient ) {
134
		$updates    = array();
135
		$no_updated = array();
136
		switch ( $transient ) {
137
			case 'update_plugins':
138
				if ( ! empty( $update->response ) && is_array( $update->response ) ) {
139
					foreach ( $update->response as $plugin_slug => $response ) {
140
						if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) {
141
							$updates[] = array( $plugin_slug => $response->new_version );
142
						}
143
					}
144
				}
145
				if ( ! empty( $update->no_update ) ) {
146
					$no_updated = array_keys( $update->no_update );
147
				}
148
149
				if ( ! isset( $no_updated['jetpack/jetpack.php'] ) && isset( $updates['jetpack/jetpack.php'] ) ) {
150
					return false;
151
				}
152
153
				break;
154
			case 'update_themes':
155
				if ( ! empty( $update->response ) && is_array( $update->response ) ) {
156
					foreach ( $update->response as $theme_slug => $response ) {
157
						if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) {
158
							$updates[] = array( $theme_slug => $response['new_version'] );
159
						}
160
					}
161
				}
162
163
				if ( ! empty( $update->checked ) ) {
164
					$no_updated = $update->checked;
165
				}
166
167
				break;
168
			case 'update_core':
169
				if ( ! empty( $update->updates ) && is_array( $update->updates ) ) {
170
					foreach ( $update->updates as $response ) {
171
						if ( ! empty( $response->response ) && $response->response === 'latest' ) {
172
							continue;
173
						}
174
						if ( ! empty( $response->response ) && isset( $response->packages->full ) ) {
175
							$updates[] = array( $response->response => $response->packages->full );
176
						}
177
					}
178
				}
179
180
				if ( ! empty( $update->version_checked ) ) {
181
					$no_updated = $update->version_checked;
182
				}
183
184
				if ( empty( $updates ) ) {
185
					return false;
186
				}
187
				break;
188
189
		}
190
		if ( empty( $updates ) && empty( $no_updated ) ) {
191
			return false;
192
		}
193
		return $this->get_check_sum( array( $no_updated, $updates ) );
194
	}
195
196
	public function validate_update_change( $value, $expiration, $transient ) {
197
		$new_checksum = $this->get_update_checksum( $value, $transient );
198
199
		if ( false === $new_checksum ) {
200
			return;
201
		}
202
203
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
204
205
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
206
			return;
207
		}
208
209
		$checksums[ $transient ] = $new_checksum;
210
211
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
212
		if ( 'update_core' === $transient ) {
213
			/**
214
			 * jetpack_update_core_change
215
			 *
216
			 * @since 5.1.0
217
			 *
218
			 * @param array containing info that tells us what needs updating
219
			 */
220
			do_action( 'jetpack_update_core_change', $value );
221
			return;
222
		}
223
		if ( empty( $this->updates ) ) {
224
			// lets add the shutdown method once and only when the updates move from empty to filled with something
225
			add_action( 'shutdown', array( $this, 'sync_last_event' ), 9 );
226
		}
227
		if ( ! isset( $this->updates[ $transient ] ) ) {
228
			$this->updates[ $transient ] = array();
229
		}
230
		$this->updates[ $transient ][] = $value;
231
	}
232
233
	public function sync_last_event() {
234
		foreach ( $this->updates as $transient => $values ) {
235
			$value = end( $values ); // only send over the last value
236
			/**
237
			 * jetpack_{$transient}_change
238
			 * jetpack_update_plugins_change
239
			 * jetpack_update_themes_change
240
			 *
241
			 * @since 5.1.0
242
			 *
243
			 * @param array containing info that tells us what needs updating
244
			 */
245
			do_action( "jetpack_{$transient}_change", $value );
246
		}
247
248
	}
249
250
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
251
		/**
252
		 * Tells the client to sync all updates to the server
253
		 *
254
		 * @since 4.2.0
255
		 *
256
		 * @param boolean Whether to expand updates (should always be true)
257
		 */
258
		do_action( 'jetpack_full_sync_updates', true );
259
260
		// The number of actions enqueued, and next module state (true == done)
261
		return array( 1, true );
262
	}
263
264
	public function estimate_full_sync_actions( $config ) {
265
		return 1;
266
	}
267
268
	function get_full_sync_actions() {
269
		return array( 'jetpack_full_sync_updates' );
270
	}
271
272
	public function get_all_updates() {
273
		return array(
274
			'core'    => get_site_transient( 'update_core' ),
275
			'plugins' => get_site_transient( 'update_plugins' ),
276
			'themes'  => get_site_transient( 'update_themes' ),
277
		);
278
	}
279
280
	// removes unnecessary keys from synced updates data
281
	function filter_update_keys( $args ) {
282
		$updates = $args[0];
283
284
		if ( isset( $updates->no_update ) ) {
285
			unset( $updates->no_update );
286
		}
287
288
		return $args;
289
	}
290
291
	function filter_upgrader_process_complete( $args ) {
292
		array_shift( $args );
293
294
		return $args;
295
	}
296
297
	public function expand_updates( $args ) {
298
		if ( $args[0] ) {
299
			return $this->get_all_updates();
300
		}
301
302
		return $args;
303
	}
304
305
	public function expand_themes( $args ) {
306
		if ( ! isset( $args[0], $args[0]->response ) ) {
307
			return $args;
308
		}
309
		if ( ! is_array( $args[0]->response ) ) {
310
			trigger_error( 'Warning: Not an Array as expected but -> ' . wp_json_encode( $args[0]->response ) . ' instead', E_USER_WARNING );
311
			return $args;
312
		}
313
		foreach ( $args[0]->response as $stylesheet => &$theme_data ) {
314
			$theme              = wp_get_theme( $stylesheet );
315
			$theme_data['name'] = $theme->name;
316
		}
317
		return $args;
318
	}
319
320
	public function reset_data() {
321
		delete_option( self::UPDATES_CHECKSUM_OPTION_NAME );
322
	}
323
}
324