Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

Jetpack_Sync_Module_Updates::expand_themes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
rs 9.4285
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 ( 'update-core.php' !== $pagenow ) {
91
			/**
92
			 * Sync event that fires when core autoupdate was successful
93
			 *
94
			 * @since 5.0.0
95
			 *
96
			 * @param string $new_wp_version the updated WordPress version
97
			 * @param string $old_wp_version the previous WordPress version
98
			 */
99
			do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version );
100
			return;
101
		}
102
		/**
103
		 * Sync event that fires when core update was successful
104
		 *
105
		 * @since 5.0.0
106
		 *
107
		 * @param string $new_wp_version the updated WordPress version
108
		 * @param string $old_wp_version the previous WordPress version
109
		 */
110
		do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version );
111
		return;
112
113
	}
114
115
116
	public function get_update_checksum( $update, $transient ) {
117
		$updates = array();
118
		$no_updated = array();
119
		switch ( $transient ) {
120
			case 'update_plugins':
121
				if ( ! empty( $update->response ) ) {
122
					foreach ( $update->response as $plugin_slug => $response ) {
123
						if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) {
124
							$updates[] = array( $plugin_slug => $response->new_version );
125
						}
126
					}
127
				}
128
				if ( ! empty( $update->no_update ) ) {
129
					$no_updated = array_keys( $update->no_update );
130
				}
131
132
				if ( ! isset( $no_updated[ 'jetpack/jetpack.php' ] ) && isset( $updates[ 'jetpack/jetpack.php' ] ) ) {
133
					return false;
134
				}
135
136
				break;
137
			case 'update_themes':
138
				if ( ! empty( $update->response ) ) {
139
					foreach ( $update->response as $theme_slug => $response ) {
140
						if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) {
141
							$updates[] = array( $theme_slug => $response['new_version'] );
142
						}
143
					}
144
				}
145
146
				if ( ! empty( $update->checked ) ) {
147
					$no_updated = $update->checked;
148
				}
149
150
				break;
151
			case 'update_core':
152
				if ( ! empty( $update->updates ) ) {
153
					foreach ( $update->updates as $response ) {
154
						if( ! empty( $response->response ) && $response->response === 'latest' ) {
155
							continue;
156
						}
157
						if ( ! empty( $response->response ) && isset( $response->packages->full ) ) {
158
							$updates[] = array( $response->response => $response->packages->full );
159
						}
160
					}
161
				}
162
163
				if (  ! empty( $update->version_checked ) ) {
164
					$no_updated = $update->version_checked;
165
				}
166
167
				if ( empty( $updates ) ) {
168
					return false;
169
				}
170
				break;
171
172
		}
173
		if ( empty( $updates ) && empty( $no_updated ) ) {
174
			return false;
175
		}
176
		return $this->get_check_sum( array( $no_updated, $updates ) );
177
	}
178
179
	public function validate_update_change( $value, $expiration, $transient ) {
180
181
		$new_checksum = $this->get_update_checksum( $value, $transient );
182
		if ( false === $new_checksum  ) {
183
			return;
184
		}
185
186
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
187
188
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
189
			return;
190
		}
191
192
		$checksums[ $transient ] = $new_checksum;
193
194
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
195
		do_action( "jetpack_{$transient}_change", $value );
196
	}
197
198
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
199
		/**
200
		 * Tells the client to sync all updates to the server
201
		 *
202
		 * @since 4.2.0
203
		 *
204
		 * @param boolean Whether to expand updates (should always be true)
205
		 */
206
		do_action( 'jetpack_full_sync_updates', true );
207
208
		// The number of actions enqueued, and next module state (true == done)
209
		return array( 1, true );
210
	}
211
212
	public function estimate_full_sync_actions( $config ) {
213
		return 1;
214
	}
215
216
	function get_full_sync_actions() {
217
		return array( 'jetpack_full_sync_updates' );
218
	}
219
220
	public function get_all_updates() {
221
		return array(
222
			'core'    => get_site_transient( 'update_core' ),
223
			'plugins' => get_site_transient( 'update_plugins' ),
224
			'themes'  => get_site_transient( 'update_themes' ),
225
		);
226
	}
227
228
	// removes unnecessary keys from synced updates data
229
	function filter_update_keys( $args ) {
230
		$updates = $args[0];
231
232
		if ( isset( $updates->no_update ) ) {
233
			unset( $updates->no_update );
234
		}
235
236
		return $args;
237
	}
238
239
	function filter_upgrader_process_complete( $args ) {
240
		array_shift( $args );
241
242
		return $args;
243
	}
244
245
	public function expand_updates( $args ) {
246
		if ( $args[0] ) {
247
			return $this->get_all_updates();
248
		}
249
250
		return $args;
251
	}
252
253
	public function expand_themes( $args ) {
254
		if ( ! isset( $args[0], $args[0]->response ) ) {
255
			return $args;
256
		}
257
		foreach ( $args[0]->response as $stylesheet => &$theme_data ) {
258
			$theme = wp_get_theme( $stylesheet );
259
			$theme_data['name'] = $theme->name;
260
		}
261
		return $args;
262
	}
263
264
	public function reset_data() {
265
		delete_option( self::UPDATES_CHECKSUM_OPTION_NAME );
266
	}
267
}
268