Completed
Push — fix/update-core-plugins-theme-... ( 9a10c9 )
by
unknown
57:17 queued 48:41
created

validate_update_change()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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