Completed
Push — try/wp-client-i18n ( 5ca50e...f7e161 )
by Jeremy
26:36 queued 16:52
created

Jetpack_Sync_Module_Updates::expand_updates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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