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_action( 'update_site_option_wpmu_upgrade_site', array ( $this, 'update_core_network_event' ), 10, 3 ); |
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( $option, $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
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function update_core( $new_wp_version ) { |
74
|
|
|
global $pagenow; |
75
|
|
|
|
76
|
|
|
if ( isset( $_GET[ 'action' ] ) && 'do-core-reinstall' === $_GET[ 'action' ] ) { |
77
|
|
|
/** |
78
|
|
|
* Sync event that fires when core reinstall was successful |
79
|
|
|
* |
80
|
|
|
* @since 5.0.0 |
81
|
|
|
* |
82
|
|
|
* @param string $new_wp_version the updated WordPress version |
83
|
|
|
*/ |
84
|
|
|
do_action( 'jetpack_sync_core_reinstalled_successfully', $new_wp_version ); |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Core was autoudpated |
89
|
|
|
if ( 'update-core.php' !== $pagenow ) { |
90
|
|
|
/** |
91
|
|
|
* Sync event that fires when core autoupdate was successful |
92
|
|
|
* |
93
|
|
|
* @since 5.0.0 |
94
|
|
|
* |
95
|
|
|
* @param string $new_wp_version the updated WordPress version |
96
|
|
|
* @param string $old_wp_version the previous WordPress version |
97
|
|
|
*/ |
98
|
|
|
do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version ); |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Sync event that fires when core update was successful |
103
|
|
|
* |
104
|
|
|
* @since 5.0.0 |
105
|
|
|
* |
106
|
|
|
* @param string $new_wp_version the updated WordPress version |
107
|
|
|
* @param string $old_wp_version the previous WordPress version |
108
|
|
|
*/ |
109
|
|
|
do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version ); |
110
|
|
|
return; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function get_update_checksum( $value ) { |
115
|
|
|
// Create an new array so we don't modify the object passed in. |
116
|
|
|
$a_value = (array) $value; |
117
|
|
|
// ignore `last_checked` |
118
|
|
|
unset( $a_value['last_checked'] ); |
119
|
|
|
unset( $a_value['checked'] ); |
120
|
|
|
unset( $a_value['version_checked'] ); |
121
|
|
|
if ( empty( $a_value['updates'] ) ) { |
122
|
|
|
unset( $a_value['updates'] ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( empty( $a_value ) ) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
return $this->get_check_sum( $a_value ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function validate_update_change( $value, $expiration, $transient ) { |
132
|
|
|
|
133
|
|
|
$new_checksum = $this->get_update_checksum( $value ); |
134
|
|
|
if ( false === $new_checksum ) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() ); |
139
|
|
|
|
140
|
|
|
if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$checksums[ $transient ] = $new_checksum; |
145
|
|
|
|
146
|
|
|
update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums ); |
147
|
|
|
// possible $transient value are update_plugins, update_themes, update_core |
148
|
|
|
do_action( "jetpack_{$transient}_change", $value ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
152
|
|
|
/** |
153
|
|
|
* Tells the client to sync all updates to the server |
154
|
|
|
* |
155
|
|
|
* @since 4.2.0 |
156
|
|
|
* |
157
|
|
|
* @param boolean Whether to expand updates (should always be true) |
158
|
|
|
*/ |
159
|
|
|
do_action( 'jetpack_full_sync_updates', true ); |
160
|
|
|
|
161
|
|
|
// The number of actions enqueued, and next module state (true == done) |
162
|
|
|
return array( 1, true ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function estimate_full_sync_actions( $config ) { |
166
|
|
|
return 1; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
function get_full_sync_actions() { |
170
|
|
|
return array( 'jetpack_full_sync_updates' ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function get_all_updates() { |
174
|
|
|
return array( |
175
|
|
|
'core' => get_site_transient( 'update_core' ), |
176
|
|
|
'plugins' => get_site_transient( 'update_plugins' ), |
177
|
|
|
'themes' => get_site_transient( 'update_themes' ), |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// removes unnecessary keys from synced updates data |
182
|
|
|
function filter_update_keys( $args ) { |
183
|
|
|
$updates = $args[0]; |
184
|
|
|
|
185
|
|
|
if ( isset( $updates->no_update ) ) { |
186
|
|
|
unset( $updates->no_update ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $args; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
function filter_upgrader_process_complete( $args ) { |
193
|
|
|
array_shift( $args ); |
194
|
|
|
|
195
|
|
|
return $args; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function expand_updates( $args ) { |
199
|
|
|
if ( $args[0] ) { |
200
|
|
|
return $this->get_all_updates(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $args; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function expand_themes( $args ) { |
207
|
|
|
if ( ! isset( $args[0], $args[0]->response ) ) { |
208
|
|
|
return $args; |
209
|
|
|
} |
210
|
|
|
foreach ( $args[0]->response as $stylesheet => &$theme_data ) { |
211
|
|
|
$theme = wp_get_theme( $stylesheet ); |
212
|
|
|
$theme_data['name'] = $theme->name; |
213
|
|
|
} |
214
|
|
|
return $args; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function reset_data() { |
218
|
|
|
delete_option( self::UPDATES_CHECKSUM_OPTION_NAME ); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|