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