Completed
Push — whitelist/sync ( 830219...fbc430 )
by
unknown
13:02 queued 02:23
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
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
	public function get_update_checksum( $value ) {
116
		// Create an new array so we don't modify the object passed in.
117
		$a_value = (array) $value;
118
		// ignore `last_checked`
119
		unset( $a_value['last_checked'] );
120
		unset( $a_value['checked'] );
121
		unset( $a_value['version_checked'] );
122
		if ( empty( $a_value['updates'] ) ) {
123
			unset( $a_value['updates'] );
124
		}
125
126
		if ( empty( $a_value ) ) {
127
			return false;
128
		}
129
		return $this->get_check_sum( $a_value );
130
	}
131
132
	public function validate_update_change( $value, $expiration, $transient ) {
133
134
		$new_checksum = $this->get_update_checksum( $value );
135
		if ( false === $new_checksum  ) {
136
			return;
137
		}
138
139
		$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
140
141
		if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
142
			return;
143
		}
144
145
		$checksums[ $transient ] = $new_checksum;
146
147
		update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
148
		// possible $transient value are update_plugins, update_themes, update_core
149
		do_action( "jetpack_{$transient}_change", $value );
150
	}
151
152
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
153
		/**
154
		 * Tells the client to sync all updates to the server
155
		 *
156
		 * @since 4.2.0
157
		 *
158
		 * @param boolean Whether to expand updates (should always be true)
159
		 */
160
		do_action( 'jetpack_full_sync_updates', true );
161
162
		// The number of actions enqueued, and next module state (true == done)
163
		return array( 1, true );
164
	}
165
166
	public function estimate_full_sync_actions( $config ) {
167
		return 1;
168
	}
169
170
	function get_full_sync_actions() {
171
		return array( 'jetpack_full_sync_updates' );
172
	}
173
174
	public function get_all_updates() {
175
		return array(
176
			'core'    => get_site_transient( 'update_core' ),
177
			'plugins' => get_site_transient( 'update_plugins' ),
178
			'themes'  => get_site_transient( 'update_themes' ),
179
		);
180
	}
181
182
	// removes unnecessary keys from synced updates data
183
	function filter_update_keys( $args ) {
184
		$updates = $args[0];
185
186
		if ( isset( $updates->no_update ) ) {
187
			unset( $updates->no_update );
188
		}
189
190
		return $args;
191
	}
192
193
	function filter_upgrader_process_complete( $args ) {
194
		array_shift( $args );
195
196
		return $args;
197
	}
198
199
	public function expand_updates( $args ) {
200
		if ( $args[0] ) {
201
			return $this->get_all_updates();
202
		}
203
204
		return $args;
205
	}
206
207
	public function expand_themes( $args ) {
208
		if ( ! isset( $args[0], $args[0]->response ) ) {
209
			return $args;
210
		}
211
		foreach ( $args[0]->response as $stylesheet => &$theme_data ) {
212
			$theme = wp_get_theme( $stylesheet );
213
			$theme_data['name'] = $theme->name;
214
		}
215
		return $args;
216
	}
217
218
	public function reset_data() {
219
		delete_option( self::UPDATES_CHECKSUM_OPTION_NAME );
220
	}
221
}
222