Completed
Push — alternative-to-8611 ( e260fd )
by
unknown
30:33 queued 21:32
created

Jetpack_Sync_Module_Updates::get_update_checksum()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 16
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 $callable = null;
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
18
		$update_types = array( 'plugins', 'themes', 'core' );
19
20
		foreach ( $update_types as $type ) {
21
			add_action( "set_site_transient_update_{$type}", array( $this, 'validate_update_change' ), 10, 3 );
22
		}
23
24
		$this->callable = $callable;
25
		foreach ( $update_types as $type ) {
26
			add_action( "jetpack_update_{$type}_change", $callable );
27
		}
28
29
		// before enqueue
30
		$this->callable = $callable;
31
		foreach ( $update_types as $type ) {
32
			add_action( "jetpack_sync_before_enqueue_jetpack_update_{$type}_change", array( $this, "expand_before_enqueue_{$type}" ) );
33
		}
34
35
		add_filter( 'jetpack_sync_before_enqueue_upgrader_process_complete', array(
36
			$this,
37
			'filter_upgrader_process_complete',
38
		), 10, 2 );
39
40
		add_action( 'automatic_updates_complete', $callable );
41
42
43
		if ( is_multisite() ) {
44
			add_filter( 'pre_update_site_option_wpmu_upgrade_site', array ( $this, 'update_core_network_event' ), 10, 2 );
45
			add_action( 'jetpack_sync_core_update_network', $callable, 10, 3 );
46
		}
47
48
		// Send data when update completes
49
		add_action( '_core_updated_successfully', array( $this, 'update_core' ) );
50
		add_action( 'jetpack_sync_core_reinstalled_successfully', $callable );
51
		add_action( 'jetpack_sync_core_autoupdated_successfully', $callable, 10, 2 );
52
		add_action( 'jetpack_sync_core_updated_successfully', $callable, 10, 2 );
53
54
	}
55
56
	public function init_full_sync_listeners( $callable ) {
57
		add_action( 'jetpack_full_sync_updates', $callable );
58
	}
59
60
	public function init_before_send() {
61
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) );
62
		add_filter( 'jetpack_sync_before_send_jetpack_update_themes_change', array( $this, 'expand_themes' ) );
63
	}
64
65
	public function update_core_network_event( $wp_db_version, $old_wp_db_version ) {
66
		global $wp_version;
67
		/**
68
		 * Sync event for when core wp network updates to a new db version
69
		 *
70
		 * @since 5.0.0
71
		 *
72
		 * @param int $wp_db_version the latest wp_db_version
73
		 * @param int $old_wp_db_version previous wp_db_version
74
		 * @param string $wp_version the latest wp_version
75
		 *
76
		 */
77
		do_action( 'jetpack_sync_core_update_network', $wp_db_version, $old_wp_db_version, $wp_version );
78
		return $wp_db_version;
79
	}
80
81
	public function update_core( $new_wp_version ) {
82
		global $pagenow;
83
84
		if ( isset( $_GET[ 'action' ] ) && 'do-core-reinstall' === $_GET[ 'action' ] ) {
85
			/**
86
			 * Sync event that fires when core reinstall was successful
87
			 *
88
			 * @since 5.0.0
89
			 *
90
			 * @param string $new_wp_version the updated WordPress version
91
			 */
92
			do_action( 'jetpack_sync_core_reinstalled_successfully', $new_wp_version );
93
			return;
94
		}
95
96
		// Core was autoudpated
97
		if ( 'update-core.php' !== $pagenow ) {
98
			/**
99
			 * Sync event that fires when core autoupdate was successful
100
			 *
101
			 * @since 5.0.0
102
			 *
103
			 * @param string $new_wp_version the updated WordPress version
104
			 * @param string $old_wp_version the previous WordPress version
105
			 */
106
			do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version );
107
			return;
108
		}
109
		/**
110
		 * Sync event that fires when core update was successful
111
		 *
112
		 * @since 5.0.0
113
		 *
114
		 * @param string $new_wp_version the updated WordPress version
115
		 * @param string $old_wp_version the previous WordPress version
116
		 */
117
		do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version );
118
		return;
119
120
	}
121
122
	public function get_update_checksum( $value ) {
123
		// Create an new array so we don't modify the object passed in.
124
		$a_value = (array) $value;
125
		// ignore `last_checked`
126
		unset( $a_value['last_checked'] );
127
		unset( $a_value['checked'] );
128
		unset( $a_value['version_checked'] );
129
		if ( empty( $a_value['updates'] ) ) {
130
			unset( $a_value['updates'] );
131
		}
132
133
		if ( empty( $a_value ) ) {
134
			return false;
135
		}
136
		return $this->get_check_sum( $a_value );
137
	}
138
139
	public function validate_update_change( $value, $expiration, $transient ) {
140
141
		$new_checksum = $this->get_update_checksum( $value );
142
		if ( false === $new_checksum  ) {
143
			return;
144
		}
145
146
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
147
148
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
149
			return;
150
		}
151
152
		$checksums[ $transient ] = $new_checksum;
153
154
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
155
		// possible $transient value are update_plugins, update_themes, update_core
156
157
		do_action( "jetpack_{$transient}_change", $value );
158
		// only send one change notice per request
159
		remove_filter( "jetpack_{$transient}_change", $this->callable );
160
	}
161
162
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
163
		/**
164
		 * Tells the client to sync all updates to the server
165
		 *
166
		 * @since 4.2.0
167
		 *
168
		 * @param boolean Whether to expand updates (should always be true)
169
		 */
170
		do_action( 'jetpack_full_sync_updates', true );
171
172
		// The number of actions enqueued, and next module state (true == done)
173
		return array( 1, true );
174
	}
175
176
	public function estimate_full_sync_actions( $config ) {
177
		return 1;
178
	}
179
180
	function get_full_sync_actions() {
181
		return array( 'jetpack_full_sync_updates' );
182
	}
183
184
	public function get_all_updates() {
185
		return array(
186
			'core'    => get_site_transient( 'update_core' ),
187
			'plugins' => get_site_transient( 'update_plugins' ),
188
			'themes'  => get_site_transient( 'update_themes' ),
189
		);
190
	}
191
192
	// removes unnecessary keys from synced updates data
193
	function filter_update_keys( $args ) {
194
		$updates = $args[0];
195
196
		if ( isset( $updates->no_update ) ) {
197
			unset( $updates->no_update );
198
		}
199
200
		return $args;
201
	}
202
203
	function filter_upgrader_process_complete( $args ) {
204
		array_shift( $args );
205
206
		return $args;
207
	}
208
209
	public function expand_updates( $args ) {
210
		if ( $args[0] ) {
211
			return $this->get_all_updates();
212
		}
213
214
		return $args;
215
	}
216
217
	public function expand_themes( $args ) {
218
		if ( ! isset( $args[0], $args[0]->response ) ) {
219
			return $args;
220
		}
221
		foreach ( $args[0]->response as $stylesheet => &$theme_data ) {
222
			$theme = wp_get_theme( $stylesheet );
223
			$theme_data['name'] = $theme->name;
224
		}
225
		return $args;
226
	}
227
228
	public function expand_before_enqueue_themes( $args ) {
229
		return get_site_transient( 'update_themes' );
230
	}
231
232
	public function expand_before_enqueue_plugins( $args ) {
233
		return $this->filter_update_keys( get_site_transient( 'update_plugins' ) );
234
	}
235
236
	public function expand_before_enqueue_core( $args ) {
237
		return get_site_transient( 'update_core' );
238
	}
239
240
	public function reset_data() {
241
		delete_option( self::UPDATES_CHECKSUM_OPTION_NAME );
242
	}
243
}
244