1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
<?php /** * Class for testing plugin/theme updates in the WordPress code. * * @package Health Check */
// Make sure the file is not directly accessible. if ( ! defined( 'ABSPATH' ) ) { die( 'We\'re sorry, but you can not directly access this file.' ); }
/** * Class Health_Check_Updates */ class Health_Check_Updates { private $plugins_before; private $plugins_after; private static $plugins_blocked; private $themes_before; private $themes_after; private static $themes_blocked;
/** * Health_Check_Updates constructor. * * @uses Health_Check_Updates::init() * * @return void */ public function __construct() { $this->init(); }
/** * Initiate the plugin class. * * @return void */ public function init() { $this->plugins_before = (array) array(); $this->plugins_after = (array) array(); self::$plugins_blocked = (bool) false;
$this->themes_before = (array) array(); $this->themes_after = (array) array(); self::$themes_blocked = (bool) false; }
/** * Run tests to determine if auto-updates can run. * * @uses get_class_methods() * @uses substr() * @uses call_user_func() * * @return array */ public function run_tests() { $tests = array();
foreach ( get_class_methods( $this ) as $method ) { if ( 'test_' !== substr( $method, 0, 5 ) ) { continue; }
$result = call_user_func( array( $this, $method ) );
if ( false === $result || null === $result ) { continue; }
$result = (object) $result;
if ( empty( $result->severity ) ) { $result->severity = 'warning'; }
$tests[ $method ] = $result; }
return $tests; }
/** * Check if plugin updates have been tampered with. * * @uses Health_Check_Updates::check_plugin_update_hooks() * @uses esc_html__() * @uses Health_Check_Updates::check_plugin_update_pre_request() * @uses Health_Check_Updates::check_plugin_update_request_args() * * @return array */ function test_plugin_updates() { // Check if any update hooks have been removed. $hooks = $this->check_plugin_update_hooks(); if ( ! $hooks ) { return array( 'desc' => esc_html__( 'Plugin update hooks have been removed.', 'health-check' ), 'severity' => 'fail', ); }
// Check if update requests are being blocked. $blocked = $this->check_plugin_update_pre_request(); if ( true === $blocked ) { return array( 'desc' => esc_html__( 'Plugin update requests have been blocked.', 'health-check' ), 'severity' => 'fail', ); }
// Check if plugins have been removed from the update requests. $diff = (array) $this->check_plugin_update_request_args(); if ( 0 !== count( $diff ) ) { return array( 'desc' => sprintf( /* translators: %s: List of plugin names. */ esc_html__( 'The following Plugins have been removed from update checks: %s.', 'health-check' ), implode( ',', $diff ) ), 'severity' => 'warning', ); }
return array( 'desc' => esc_html__( 'Plugin updates should be working as expected.', 'health-check' ), 'severity' => 'pass', ); }
/** * Check if any plugin update hooks have been removed. * * @uses has_filter() * @uses wp_next_scheduled() * * @return array */ function check_plugin_update_hooks() { $test1 = has_filter( 'load-plugins.php', 'wp_update_plugins' ); $test2 = has_filter( 'load-update.php', 'wp_update_plugins' ); $test3 = has_filter( 'load-update-core.php', 'wp_update_plugins' ); $test4 = has_filter( 'wp_update_plugins', 'wp_update_plugins' ); $test5 = has_filter( 'admin_init', '_maybe_update_plugins' ); $test6 = wp_next_scheduled( 'wp_update_plugins' );
return $test1 && $test2 && $test3 && $test4 && $test5 && $test6; }
/** * Check if plugin update request checks are being tampered with at the 'pre_http_request' filter. * * @uses add_action() * @uses Health_Check_Updates::wp_plugin_update_fake_request() * @uses esc_html__() * * @return array */ function check_plugin_update_pre_request() { add_action( 'pre_http_request', array( $this, 'plugin_pre_request_check' ), PHP_INT_MAX, 3 ); add_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX, 3 );
$this->plugin_update_fake_request();
remove_action( 'pre_http_request', array( $this, 'plugin_pre_request_check' ), PHP_INT_MAX ); remove_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX );
return self::$plugins_blocked; }
/** * Check plugin update requests to see if they are being blocked. * * @param bool $pre If not false, request cancelled. * @param array $r Request parameters. * @param string $url Request URL. * @return bool */ function plugin_pre_request_check( $pre, $r, $url ) { $check_url = 'api.wordpress.org/plugins/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $pre; // Not a plugin update request. }
// If not false something is blocking update checks if ( false !== $pre ) { self::$plugins_blocked = (bool) true; }
return $pre; }
/** * Check if plugins are being removed at the 'http_request_args' filter. * * @uses add_action() * @uses Health_Check_Updates::wp_plugin_update_fake_request() * @uses remove_action() * * @return array */ function check_plugin_update_request_args() { add_action( 'http_request_args', array( $this, 'plugin_request_args_before' ), 1, 2 ); add_action( 'http_request_args', array( $this, 'plugin_request_args_after' ), PHP_INT_MAX, 2 ); add_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX, 3 );
$this->plugin_update_fake_request();
remove_action( 'http_request_args', array( $this, 'plugin_request_args_before' ), 1 ); remove_action( 'http_request_args', array( $this, 'plugin_request_args_after' ), PHP_INT_MAX ); remove_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX );
$diff = array_diff_key( $this->plugins_before['plugins'], $this->plugins_after['plugins'] );
$titles = array(); foreach ( $diff as $item ) { $titles[] = $item['Title']; }
return $titles; }
/** * Record the list of plugins from plugin update requests at the start of filtering. * * @param array $r Request parameters. * @param string $url Request URL. * @return array */ function plugin_request_args_before( $r, $url ) { $check_url = 'api.wordpress.org/plugins/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $r; // Not a plugin update request. }
$this->plugins_before = (array) json_decode( $r['body']['plugins'], true );
return $r; }
/** * Record the list of plugins from plugin update requests at the end of filtering. * * @param array $r Request parameters. * @param string $url Request URL. * @return array */ function plugin_request_args_after( $r, $url ) { $check_url = 'api.wordpress.org/plugins/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $r; // Not a plugin update request. }
$this->plugins_after = (array) json_decode( $r['body']['plugins'], true );
return $r; }
/** * Create and trigger a fake plugin update check request. * * @uses get_plugins() * @uses get_option() * @uses wp_get_installed_translations() * @uses apply_filters() * @uses wp_json_encode() * @uses get_bloginfo() * @uses home_url() * @uses wp_http_supports() * @uses set_url_scheme() * @uses wp_remote_post() * * @return void */ function plugin_update_fake_request() { if ( ! function_exists( 'get_plugins' ) ) { require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); }
// Prepare data for the request. $plugins = get_plugins(); $active = get_option( 'active_plugins', array() ); $to_send = compact( 'plugins', 'active' ); $translations = wp_get_installed_translations( 'plugins' ); $locales = array_values( get_available_languages() ); $locales = (array) apply_filters( 'plugins_update_check_locales', $locales ); $locales = array_unique( $locales ); $timeout = 3 + (int) ( count( $plugins ) / 10 );
// Setup the request options. if ( function_exists( 'wp_json_encode' ) ) { $options = array( 'timeout' => $timeout, 'body' => array( 'plugins' => wp_json_encode( $to_send ), 'translations' => wp_json_encode( $translations ), 'locale' => wp_json_encode( $locales ), 'all' => wp_json_encode( true ), ), 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . home_url( '/' ), ); } else { $options = array( 'timeout' => $timeout, 'body' => array( 'plugins' => json_encode( $to_send ), 'translations' => json_encode( $translations ), 'locale' => json_encode( $locales ), 'all' => json_encode( true ), ), 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . home_url( '/' ), ); }
// Set the URL $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/'; $url = wp_http_supports( array( 'ssl' ) ) ? set_url_scheme( $http_url, 'https' ) : $http_url;
// Ignore the response. Just need the hooks to fire. wp_remote_post( $url, $options ); }
/** * Check if theme updates have been tampered with. * * @uses Health_Check_Updates::check_theme_update_hooks() * @uses esc_html__() * @uses Health_Check_Updates::check_theme_update_pre_request() * @uses Health_Check_Updates::check_theme_update_request_args() * * @return array */ function test_constant_theme_updates() { // Check if any update hooks have been removed. $hooks = $this->check_theme_update_hooks(); if ( ! $hooks ) { return array( 'desc' => esc_html__( 'Theme update hooks have been removed.', 'health-check' ), 'severity' => 'fail', ); }
// Check if update requests are being blocked. $blocked = $this->check_theme_update_pre_request(); if ( true === $blocked ) { return array( 'desc' => esc_html__( 'Theme update requests have been blocked.', 'health-check' ), 'severity' => 'fail', ); }
// Check if themes have been removed from the update requests. $diff = (array) $this->check_theme_update_request_args(); if ( 0 !== count( $diff ) ) { return array( 'desc' => sprintf( /* translators: %s: List of theme names. */ esc_html__( 'The following Themes have been removed from update checks: %s.', 'health-check' ), implode( ',', $diff ) ), 'severity' => 'warning', ); }
return array( 'desc' => esc_html__( 'Theme updates should be working as expected.', 'health-check' ), 'severity' => 'pass', ); }
/** * Check if any theme update hooks have been removed. * * @uses has_filter() * @uses wp_next_scheduled() * * @return array */ function check_theme_update_hooks() { $test1 = has_filter( 'load-themes.php', 'wp_update_themes' ); $test2 = has_filter( 'load-update.php', 'wp_update_themes' ); $test3 = has_filter( 'load-update-core.php', 'wp_update_themes' ); $test4 = has_filter( 'wp_update_themes', 'wp_update_themes' ); $test5 = has_filter( 'admin_init', '_maybe_update_themes' ); $test6 = wp_next_scheduled( 'wp_update_themes' );
return $test1 && $test2 && $test3 && $test4 && $test5 && $test6; }
/** * Check if theme update request checks are being tampered with at the 'pre_http_request' filter. * * @uses add_action() * @uses Health_Check_Updates::wp_theme_update_fake_request() * @uses esc_html__() * * @return array */ function check_theme_update_pre_request() { add_action( 'pre_http_request', array( $this, 'theme_pre_request_check' ), PHP_INT_MAX, 3 ); add_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX, 3 );
$this->theme_update_fake_request();
remove_action( 'pre_http_request', array( $this, 'theme_pre_request_check' ), PHP_INT_MAX ); remove_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX );
return self::$themes_blocked; }
/** * Check theme update requests to see if they are being blocked. * * @param bool $pre If not false, request cancelled. * @param array $r Request parameters. * @param string $url Request URL. * @return bool */ function theme_pre_request_check( $pre, $r, $url ) { $check_url = 'api.wordpress.org/themes/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $pre; // Not a theme update request. }
// If not false something is blocking update checks if ( false !== $pre ) { self::$themes_blocked = (bool) true; }
return $pre; }
/** * Check if themes are being removed at the 'http_request_args' filter. * * @uses add_action() * @uses Health_Check_Updates::wp_theme_update_fake_request() * @uses remove_action() * * @return array */ function check_theme_update_request_args() { add_action( 'http_request_args', array( $this, 'theme_request_args_before' ), 1, 2 ); add_action( 'http_request_args', array( $this, 'theme_request_args_after' ), PHP_INT_MAX, 2 ); add_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX, 3 );
$this->theme_update_fake_request();
remove_action( 'http_request_args', array( $this, 'theme_request_args_before' ), 1 ); remove_action( 'http_request_args', array( $this, 'theme_request_args_after' ), PHP_INT_MAX ); remove_action( 'pre_http_request', array( $this, 'block_fake_request' ), PHP_INT_MAX );
$diff = array_diff_key( $this->themes_before['themes'], $this->themes_after['themes'] );
$titles = array(); foreach ( $diff as $item ) { $titles[] = $item['Title']; }
return $titles; }
/** * Record the list of themes from theme update requests at the start of filtering. * * @param array $r Request parameters. * @param string $url Request URL. * @return array */ function theme_request_args_before( $r, $url ) { $check_url = 'api.wordpress.org/themes/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $r; // Not a theme update request. }
$this->themes_before = (array) json_decode( $r['body']['themes'], true );
return $r; }
/** * Record the list of themes from theme update requests at the end of filtering. * * @param array $r Request parameters. * @param string $url Request URL. * @return array */ function theme_request_args_after( $r, $url ) { $check_url = 'api.wordpress.org/themes/update-check/1.1/'; if ( 0 !== substr_compare( $url, $check_url, -strlen( $check_url ) ) ) { return $r; // Not a theme update request. }
$this->themes_after = (array) json_decode( $r['body']['themes'], true );
return $r; }
/** * Create and trigger a fake theme update check request. * * @uses wp_get_themes() * @uses wp_get_installed_translations() * @uses get_option() * @uses get_available_languages() * @uses wp_json_encode() * @uses get_bloginfo() * @uses home_url() * @uses wp_http_supports() * @uses set_url_scheme() * @uses wp_remote_post() * * @return void */ function theme_update_fake_request() { $themes = array(); $checked = array(); $request = array(); $installed_themes = wp_get_themes(); $translations = wp_get_installed_translations( 'themes' ); $request['active'] = get_option( 'stylesheet' );
foreach ( $installed_themes as $theme ) { $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
$themes[ $theme->get_stylesheet() ] = array( 'Name' => $theme->get( 'Name' ), 'Title' => $theme->get( 'Name' ), 'Version' => $theme->get( 'Version' ), 'Author' => $theme->get( 'Author' ), 'Author URI' => $theme->get( 'AuthorURI' ), 'Template' => $theme->get_template(), 'Stylesheet' => $theme->get_stylesheet(), ); }
$request['themes'] = $themes;
$locales = array_values( get_available_languages() ); $timeout = 3 + (int) ( count( $themes ) / 10 );
if ( function_exists( 'wp_json_encode' ) ) { $options = array( 'timeout' => $timeout, 'body' => array( 'themes' => wp_json_encode( $request ), 'translations' => wp_json_encode( $translations ), 'locale' => wp_json_encode( $locales ), ), 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . home_url( '/' ), ); } else { $options = array( 'timeout' => $timeout, 'body' => array( 'themes' => json_encode( $request ), 'translations' => json_encode( $translations ), 'locale' => json_encode( $locales ), ), 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . home_url( '/' ), ); }
// Set the URL $http_url = 'http://api.wordpress.org/themes/update-check/1.1/'; $url = wp_http_supports( array( 'ssl' ) ) ? set_url_scheme( $http_url, 'https' ) : $http_url;
// Ignore the response. Just need the hooks to fire. wp_remote_post( $url, $options ); }
/** * Blocks the fake update requests, ensuring they do not slow down page loads. * * @param bool $pre If not false, request cancelled. * @param array $r Request parameters. * @param string $url Request URL. * @return bool */ function block_fake_request( $pre, $r, $url ) { switch ( $url ) { case 'https://api.wordpress.org/plugins/update-check/1.1/': return 'block_request'; case 'https://api.wordpress.org/themes/update-check/1.1/': return 'block_request'; default: return $pre; } } }
|