1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Handles items that have been selected for automatic updates. |
5
|
|
|
* Hooks into WP_Automatic_Updater |
6
|
|
|
*/ |
7
|
|
|
class Jetpack_Autoupdate { |
8
|
|
|
|
9
|
|
|
private $results = array(); |
10
|
|
|
|
11
|
|
|
private $expected = array(); |
12
|
|
|
|
13
|
|
|
private $success = array( |
14
|
|
|
'plugin' => array(), |
15
|
|
|
'theme' => array(), |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private $failed = array( |
19
|
|
|
'plugin' => array(), |
20
|
|
|
'theme' => array(), |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $instance = null; |
24
|
|
|
|
25
|
|
|
static function init() { |
26
|
|
|
if ( is_null( self::$instance ) ) { |
27
|
|
|
self::$instance = new Jetpack_Autoupdate; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return self::$instance; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function __construct() { |
34
|
|
|
if ( Jetpack::is_module_active( 'manage' ) ) { |
35
|
|
|
add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 ); |
36
|
|
|
add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 ); |
37
|
|
|
add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 ); |
38
|
|
|
add_filter( 'auto_update_translation', array( $this, 'autoupdate_translation', 10, 2 ) ); |
39
|
|
|
add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 ); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function autoupdate_plugin( $update, $item ) { |
44
|
|
|
$autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
45
|
|
|
if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) { |
46
|
|
|
$this->expect( $item->plugin, 'plugin' ); |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $update; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function autoupdate_translation( $update, $item ) { |
55
|
|
|
$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ); |
56
|
|
|
$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
$item = { |
60
|
|
|
"type":"theme", |
61
|
|
|
"slug":"twentyfourteen", |
62
|
|
|
"language":"en_CA", |
63
|
|
|
"version":"1.8", |
64
|
|
|
"updated":"2015-07-18 11:27:20", |
65
|
|
|
"package":"https:\/\/downloads.wordpress.org\/translation\/theme\/twentyfourteen\/1.8\/en_CA.zip", |
66
|
|
|
"autoupdate":true |
67
|
|
|
} |
68
|
|
|
*/ |
69
|
|
|
if ( ( in_array( $item->slug, $autoupdate_themes_translations ) |
70
|
|
|
|| in_array( $item->slug, $autoupdate_theme_list ) ) |
71
|
|
|
&& 'theme' === $item->type |
72
|
|
|
) { |
73
|
|
|
$this->expect( $item->slug, 'theme' ); |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $update; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function autoupdate_theme( $update, $item ) { |
82
|
|
|
$autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
83
|
|
|
if ( in_array( $item->theme, $autoupdate_theme_list ) ) { |
84
|
|
|
$this->expect( $item->theme, 'theme' ); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $update; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function autoupdate_core( $update, $item ) { |
|
|
|
|
93
|
|
|
$autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false ); |
94
|
|
|
if ( $autoupdate_core ) { |
95
|
|
|
return $autoupdate_core; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $update; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Stores the an item identifier to the expected array. |
103
|
|
|
* |
104
|
|
|
* @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
105
|
|
|
* @param string $type 'plugin' or 'theme' |
106
|
|
|
*/ |
107
|
|
|
private function expect( $item, $type ) { |
108
|
|
|
if ( ! isset( $this->expected[ $type ] ) ) { |
109
|
|
|
$this->expected[ $type ] = array(); |
110
|
|
|
} |
111
|
|
|
$this->expected[ $type ][] = $item; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* On completion of an automatic update, let's store the results. |
116
|
|
|
* |
117
|
|
|
* @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. |
118
|
|
|
*/ |
119
|
|
|
public function automatic_updates_complete( $results ) { |
120
|
|
|
if ( empty( $this->expected ) ) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
$this->results = empty( $results ) ? self::get_possible_failures() : $results; |
124
|
|
|
|
125
|
|
|
add_action( 'shutdown', array( $this, 'bump_stats' ) ); |
126
|
|
|
|
127
|
|
|
Jetpack::init(); |
128
|
|
|
|
129
|
|
|
$items_to_log = array( 'plugin', 'theme' ); |
130
|
|
|
foreach ( $items_to_log as $items ) { |
131
|
|
|
$this->log_items( $items ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
Jetpack::log( 'autoupdates', $this->get_log() ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function get_log() { |
138
|
|
|
return array( |
139
|
|
|
'results' => $this->results, |
140
|
|
|
'failed' => $this->failed, |
141
|
|
|
'success' => $this->success |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Iterates through expected items ( plugins or themes ) and compares them to actual results. |
147
|
|
|
* |
148
|
|
|
* @param $items 'plugin' or 'theme' |
149
|
|
|
*/ |
150
|
|
|
private function log_items( $items ) { |
151
|
|
|
|
152
|
|
|
if ( ! isset( $this->expected[ $items ] ) ) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$item_results = $this->get_successful_updates( $items ); |
157
|
|
|
|
158
|
|
|
if ( is_array( $this->expected[ $items ] ) ) { |
159
|
|
|
foreach ( $this->expected[ $items ] as $item ) { |
160
|
|
|
if ( in_array( $item, $item_results ) ) { |
161
|
|
|
$this->success[ $items ][] = $item; |
162
|
|
|
} else { |
163
|
|
|
$this->failed[ $items ][] = $item; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function bump_stats() { |
170
|
|
|
$instance = Jetpack::init(); |
171
|
|
|
$log = array(); |
172
|
|
|
// Bump numbers |
173
|
|
View Code Duplication |
if ( ! empty( $this->success['plugin'] ) ) { |
174
|
|
|
$instance->stat( 'autoupdates/plugin-success', count( $this->success['plugin'] ) ); |
175
|
|
|
$log['plugins_success'] = $this->success['plugin']; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
if ( ! empty( $this->failed['plugin'] ) ) { |
179
|
|
|
$instance->stat( 'autoupdates/plugin-fail', count( $this->failed['plugin'] ) ); |
180
|
|
|
$log['plugins_failed'] = $this->failed['plugin']; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
if ( ! empty( $this->success['theme'] ) ) { |
184
|
|
|
$instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) ); |
185
|
|
|
$log['themes_success'] = $this->success['theme']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
View Code Duplication |
if ( ! empty( $this->failed['theme'] ) ) { |
189
|
|
|
$instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) ); |
190
|
|
|
$log['themes_failed'] = $this->failed['theme']; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$instance->do_stats( 'server_side' ); |
194
|
|
|
|
195
|
|
|
// Send a more detailed log to logstash |
196
|
|
|
if ( ! empty( $log ) ) { |
197
|
|
|
Jetpack::load_xml_rpc_client(); |
198
|
|
|
$xml = new Jetpack_IXR_Client( array( |
199
|
|
|
'user_id' => get_current_user_id() |
200
|
|
|
) ); |
201
|
|
|
$log['blog_id'] = Jetpack_Options::get_option( 'id' ); |
202
|
|
|
$xml->query( 'jetpack.debug_autoupdate', $log ); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items |
208
|
|
|
* |
209
|
|
|
* @param string $type 'plugin' or 'theme' |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
private function get_successful_updates( $type ) { |
214
|
|
|
$successful_updates = array(); |
215
|
|
|
|
216
|
|
|
if ( ! isset( $this->results[ $type ] ) ) { |
217
|
|
|
return $successful_updates; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
foreach ( $this->results[ $type ] as $result ) { |
221
|
|
|
if ( $result->result ) { |
222
|
|
|
switch ( $type ) { |
223
|
|
|
case 'theme': |
224
|
|
|
$successful_updates[] = $result->item->theme; |
225
|
|
|
break; |
226
|
|
|
case 'plugin': |
227
|
|
|
$successful_updates[] = $result->item->plugin; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $successful_updates; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
static function get_possible_failures() { |
236
|
|
|
$result = array(); |
237
|
|
|
// Lets check some reasons why it might not be working as expected |
238
|
|
|
include_once( ABSPATH . '/wp-admin/includes/admin.php' ); |
239
|
|
|
include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); |
240
|
|
|
$upgrader = new WP_Automatic_Updater; |
241
|
|
|
|
242
|
|
|
if ( $upgrader->is_disabled() ) { |
243
|
|
|
$result[] = 'autoupdates-disabled'; |
244
|
|
|
} |
245
|
|
|
if ( ! is_main_site() ) { |
246
|
|
|
$result[] = 'is-not-main-site'; |
247
|
|
|
} |
248
|
|
|
if ( ! is_main_network() ) { |
249
|
|
|
$result[] = 'is-not-main-network'; |
250
|
|
|
} |
251
|
|
|
if ( $upgrader->is_vcs_checkout( ABSPATH ) ) { |
252
|
|
|
$result[] = 'site-on-vcs'; |
253
|
|
|
} |
254
|
|
|
if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) { |
255
|
|
|
$result[] = 'plugin-directory-on-vcs'; |
256
|
|
|
} |
257
|
|
|
if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) { |
258
|
|
|
$result[] = 'content-directory-on-vcs'; |
259
|
|
|
} |
260
|
|
|
$lock = get_option( 'auto_updater.lock' ); |
261
|
|
|
if ( $lock > ( time() - HOUR_IN_SECONDS ) ) { |
262
|
|
|
$result[] = 'lock-is-set'; |
263
|
|
|
} |
264
|
|
|
$skin = new Automatic_Upgrader_Skin; |
265
|
|
|
include_once( ABSPATH . 'wp-admin/includes/file.php' ); |
266
|
|
|
include_once( ABSPATH . 'wp-admin/includes/template.php' ); |
267
|
|
|
if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) { |
268
|
|
|
$result[] = 'no-system-write-access'; |
269
|
|
|
} |
270
|
|
|
if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false ) ) { |
271
|
|
|
$result[] = 'no-plugin-directory-write-access'; |
272
|
|
|
} |
273
|
|
|
if ( ! $skin->request_filesystem_credentials( false, WP_CONTENT_DIR, false ) ) { |
274
|
|
|
$result[] = 'no-wp-content-directory-write-access'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $result; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
Jetpack_Autoupdate::init(); |
283
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.