Completed
Push — fix/core-updates-via-api ( 2f15cd )
by
unknown
10:48
created

validate_update_change()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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