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