mirrored from git://develop.git.wordpress.org/
/
class-wp-site-health.php
3642 lines (3234 loc) · 120 KB
/
class-wp-site-health.php
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class for looking up a site's health based on a user's WordPress environment.
*
* @package WordPress
* @subpackage Site_Health
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Site_Health {
private static $instance = null;
private $is_acceptable_mysql_version;
private $is_recommended_mysql_version;
public $is_mariadb = false;
private $mysql_server_version = '';
private $mysql_required_version = '5.5';
private $mysql_recommended_version = '5.7';
private $mariadb_recommended_version = '10.4';
public $php_memory_limit;
public $schedules;
public $crons;
public $last_missed_cron = null;
public $last_late_cron = null;
private $timeout_missed_cron = null;
private $timeout_late_cron = null;
/**
* WP_Site_Health constructor.
*
* @since 5.2.0
*/
public function __construct() {
$this->maybe_create_scheduled_event();
// Save memory limit before it's affected by wp_raise_memory_limit( 'admin' ).
$this->php_memory_limit = ini_get( 'memory_limit' );
$this->timeout_late_cron = 0;
$this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS;
if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
$this->timeout_late_cron = - 15 * MINUTE_IN_SECONDS;
$this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS;
}
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_site_health_scheduled_check', array( $this, 'wp_cron_scheduled_check' ) );
add_action( 'site_health_tab_content', array( $this, 'show_site_health_tab' ) );
}
/**
* Outputs the content of a tab in the Site Health screen.
*
* @since 5.8.0
*
* @param string $tab Slug of the current tab being displayed.
*/
public function show_site_health_tab( $tab ) {
if ( 'debug' === $tab ) {
require_once ABSPATH . 'wp-admin/site-health-info.php';
}
}
/**
* Returns an instance of the WP_Site_Health class, or create one if none exist yet.
*
* @since 5.4.0
*
* @return WP_Site_Health|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new WP_Site_Health();
}
return self::$instance;
}
/**
* Enqueues the site health scripts.
*
* @since 5.2.0
*/
public function enqueue_scripts() {
$screen = get_current_screen();
if ( 'site-health' !== $screen->id && 'dashboard' !== $screen->id ) {
return;
}
$health_check_js_variables = array(
'screen' => $screen->id,
'nonce' => array(
'site_status' => wp_create_nonce( 'health-check-site-status' ),
'site_status_result' => wp_create_nonce( 'health-check-site-status-result' ),
),
'site_status' => array(
'direct' => array(),
'async' => array(),
'issues' => array(
'good' => 0,
'recommended' => 0,
'critical' => 0,
),
),
);
$issue_counts = get_transient( 'health-check-site-status-result' );
if ( false !== $issue_counts ) {
$issue_counts = json_decode( $issue_counts );
$health_check_js_variables['site_status']['issues'] = $issue_counts;
}
if ( 'site-health' === $screen->id && ( ! isset( $_GET['tab'] ) || empty( $_GET['tab'] ) ) ) {
$tests = WP_Site_Health::get_tests();
// Don't run https test on development environments.
if ( $this->is_development_environment() ) {
unset( $tests['async']['https_status'] );
}
foreach ( $tests['direct'] as $test ) {
if ( is_string( $test['test'] ) ) {
$test_function = sprintf(
'get_test_%s',
$test['test']
);
if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
$health_check_js_variables['site_status']['direct'][] = $this->perform_test( array( $this, $test_function ) );
continue;
}
}
if ( is_callable( $test['test'] ) ) {
$health_check_js_variables['site_status']['direct'][] = $this->perform_test( $test['test'] );
}
}
foreach ( $tests['async'] as $test ) {
if ( is_string( $test['test'] ) ) {
$health_check_js_variables['site_status']['async'][] = array(
'test' => $test['test'],
'has_rest' => ( isset( $test['has_rest'] ) ? $test['has_rest'] : false ),
'completed' => false,
'headers' => isset( $test['headers'] ) ? $test['headers'] : array(),
);
}
}
}
wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables );
}
/**
* Runs a Site Health test directly.
*
* @since 5.4.0
*
* @param callable $callback
* @return mixed|void
*/
private function perform_test( $callback ) {
/**
* Filters the output of a finished Site Health test.
*
* @since 5.3.0
*
* @param array $test_result {
* An associative array of test result data.
*
* @type string $label A label describing the test, and is used as a header in the output.
* @type string $status The status of the test, which can be a value of `good`, `recommended` or `critical`.
* @type array $badge {
* Tests are put into categories which have an associated badge shown, these can be modified and assigned here.
*
* @type string $label The test label, for example `Performance`.
* @type string $color Default `blue`. A string representing a color to use for the label.
* }
* @type string $description A more descriptive explanation of what the test looks for, and why it is important for the end user.
* @type string $actions An action to direct the user to where they can resolve the issue, if one exists.
* @type string $test The name of the test being ran, used as a reference point.
* }
*/
return apply_filters( 'site_status_test_result', call_user_func( $callback ) );
}
/**
* Runs the SQL version checks.
*
* These values are used in later tests, but the part of preparing them is more easily managed
* early in the class for ease of access and discovery.
*
* @since 5.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
private function prepare_sql_data() {
global $wpdb;
$mysql_server_type = $wpdb->db_server_info();
$this->mysql_server_version = $wpdb->get_var( 'SELECT VERSION()' );
if ( stristr( $mysql_server_type, 'mariadb' ) ) {
$this->is_mariadb = true;
$this->mysql_recommended_version = $this->mariadb_recommended_version;
}
$this->is_acceptable_mysql_version = version_compare( $this->mysql_required_version, $this->mysql_server_version, '<=' );
$this->is_recommended_mysql_version = version_compare( $this->mysql_recommended_version, $this->mysql_server_version, '<=' );
}
/**
* Tests whether `wp_version_check` is blocked.
*
* It's possible to block updates with the `wp_version_check` filter, but this can't be checked
* during an Ajax call, as the filter is never introduced then.
*
* This filter overrides a standard page request if it's made by an admin through the Ajax call
* with the right query argument to check for this.
*
* @since 5.2.0
*/
public function check_wp_version_check_exists() {
if ( ! is_admin() || ! is_user_logged_in() || ! current_user_can( 'update_core' ) || ! isset( $_GET['health-check-test-wp_version_check'] ) ) {
return;
}
echo ( has_filter( 'wp_version_check', 'wp_version_check' ) ? 'yes' : 'no' );
die();
}
/**
* Tests for WordPress version and outputs it.
*
* Gives various results depending on what kind of updates are available, if any, to encourage
* the user to install security updates as a priority.
*
* @since 5.2.0
*
* @return array The test result.
*/
public function get_test_wordpress_version() {
$result = array(
'label' => '',
'status' => '',
'badge' => array(
'label' => __( 'Performance' ),
'color' => 'blue',
),
'description' => '',
'actions' => '',
'test' => 'wordpress_version',
);
$core_current_version = get_bloginfo( 'version' );
$core_updates = get_core_updates();
if ( ! is_array( $core_updates ) ) {
$result['status'] = 'recommended';
$result['label'] = sprintf(
/* translators: %s: Your current version of WordPress. */
__( 'WordPress version %s' ),
$core_current_version
);
$result['description'] = sprintf(
'<p>%s</p>',
__( 'Unable to check if any new versions of WordPress are available.' )
);
$result['actions'] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'update-core.php?force-check=1' ) ),
__( 'Check for updates manually' )
);
} else {
foreach ( $core_updates as $core => $update ) {
if ( 'upgrade' === $update->response ) {
$current_version = explode( '.', $core_current_version );
$new_version = explode( '.', $update->version );
$current_major = $current_version[0] . '.' . $current_version[1];
$new_major = $new_version[0] . '.' . $new_version[1];
$result['label'] = sprintf(
/* translators: %s: The latest version of WordPress available. */
__( 'WordPress update available (%s)' ),
$update->version
);
$result['actions'] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'update-core.php' ) ),
__( 'Install the latest version of WordPress' )
);
if ( $current_major !== $new_major ) {
// This is a major version mismatch.
$result['status'] = 'recommended';
$result['description'] = sprintf(
'<p>%s</p>',
__( 'A new version of WordPress is available.' )
);
} else {
// This is a minor version, sometimes considered more critical.
$result['status'] = 'critical';
$result['badge']['label'] = __( 'Security' );
$result['description'] = sprintf(
'<p>%s</p>',
__( 'A new minor update is available for your site. Because minor updates often address security, it’s important to install them.' )
);
}
} else {
$result['status'] = 'good';
$result['label'] = sprintf(
/* translators: %s: The current version of WordPress installed on this site. */
__( 'Your version of WordPress (%s) is up to date' ),
$core_current_version
);
$result['description'] = sprintf(
'<p>%s</p>',
__( 'You are currently running the latest version of WordPress available, keep it up!' )
);
}
}
}
return $result;
}
/**
* Tests if plugins are outdated, or unnecessary.
*
* The test checks if your plugins are up to date, and encourages you to remove any
* that are not in use.
*
* @since 5.2.0
*
* @return array The test result.
*/
public function get_test_plugin_version() {
$result = array(
'label' => __( 'Your plugins are all up to date' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Security' ),
'color' => 'blue',
),
'description' => sprintf(
'<p>%s</p>',
__( 'Plugins extend your site’s functionality with things like contact forms, ecommerce and much more. That means they have deep access to your site, so it’s vital to keep them up to date.' )
),
'actions' => sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( admin_url( 'plugins.php' ) ),
__( 'Manage your plugins' )
),
'test' => 'plugin_version',
);
$plugins = get_plugins();
$plugin_updates = get_plugin_updates();
$plugins_active = 0;
$plugins_total = 0;
$plugins_need_update = 0;
// Loop over the available plugins and check their versions and active state.
foreach ( $plugins as $plugin_path => $plugin ) {
$plugins_total++;
if ( is_plugin_active( $plugin_path ) ) {
$plugins_active++;
}
if ( array_key_exists( $plugin_path, $plugin_updates ) ) {
$plugins_need_update++;
}
}
// Add a notice if there are outdated plugins.
if ( $plugins_need_update > 0 ) {
$result['status'] = 'critical';
$result['label'] = __( 'You have plugins waiting to be updated' );
$result['description'] .= sprintf(
'<p>%s</p>',
sprintf(
/* translators: %d: The number of outdated plugins. */
_n(
'Your site has %d plugin waiting to be updated.',
'Your site has %d plugins waiting to be updated.',
$plugins_need_update
),
$plugins_need_update
)
);
$result['actions'] .= sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( network_admin_url( 'plugins.php?plugin_status=upgrade' ) ),
__( 'Update your plugins' )
);
} else {
if ( 1 === $plugins_active ) {
$result['description'] .= sprintf(
'<p>%s</p>',
__( 'Your site has 1 active plugin, and it is up to date.' )
);
} elseif ( $plugins_active > 0 ) {
$result['description'] .= sprintf(
'<p>%s</p>',
sprintf(
/* translators: %d: The number of active plugins. */
_n(
'Your site has %d active plugin, and it is up to date.',
'Your site has %d active plugins, and they are all up to date.',
$plugins_active
),
$plugins_active
)
);
} else {
$result['description'] .= sprintf(
'<p>%s</p>',
__( 'Your site does not have any active plugins.' )
);
}
}
// Check if there are inactive plugins.
if ( $plugins_total > $plugins_active && ! is_multisite() ) {
$unused_plugins = $plugins_total - $plugins_active;
$result['status'] = 'recommended';
$result['label'] = __( 'You should remove inactive plugins' );
$result['description'] .= sprintf(
'<p>%s %s</p>',
sprintf(
/* translators: %d: The number of inactive plugins. */
_n(
'Your site has %d inactive plugin.',
'Your site has %d inactive plugins.',
$unused_plugins
),
$unused_plugins
),
__( 'Inactive plugins are tempting targets for attackers. If you are not going to use a plugin, you should consider removing it.' )
);
$result['actions'] .= sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( admin_url( 'plugins.php?plugin_status=inactive' ) ),
__( 'Manage inactive plugins' )
);
}
return $result;
}
/**
* Tests if themes are outdated, or unnecessary.
*
* Checks if your site has a default theme (to fall back on if there is a need),
* if your themes are up to date and, finally, encourages you to remove any themes
* that are not needed.
*
* @since 5.2.0
*
* @return array The test results.
*/
public function get_test_theme_version() {
$result = array(
'label' => __( 'Your themes are all up to date' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Security' ),
'color' => 'blue',
),
'description' => sprintf(
'<p>%s</p>',
__( 'Themes add your site’s look and feel. It’s important to keep them up to date, to stay consistent with your brand and keep your site secure.' )
),
'actions' => sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( admin_url( 'themes.php' ) ),
__( 'Manage your themes' )
),
'test' => 'theme_version',
);
$theme_updates = get_theme_updates();
$themes_total = 0;
$themes_need_updates = 0;
$themes_inactive = 0;
// This value is changed during processing to determine how many themes are considered a reasonable amount.
$allowed_theme_count = 1;
$has_default_theme = false;
$has_unused_themes = false;
$show_unused_themes = true;
$using_default_theme = false;
// Populate a list of all themes available in the install.
$all_themes = wp_get_themes();
$active_theme = wp_get_theme();
// If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme.
$default_theme = wp_get_theme( WP_DEFAULT_THEME );
if ( ! $default_theme->exists() ) {
$default_theme = WP_Theme::get_core_default_theme();
}
if ( $default_theme ) {
$has_default_theme = true;
if (
$active_theme->get_stylesheet() === $default_theme->get_stylesheet()
||
is_child_theme() && $active_theme->get_template() === $default_theme->get_template()
) {
$using_default_theme = true;
}
}
foreach ( $all_themes as $theme_slug => $theme ) {
$themes_total++;
if ( array_key_exists( $theme_slug, $theme_updates ) ) {
$themes_need_updates++;
}
}
// If this is a child theme, increase the allowed theme count by one, to account for the parent.
if ( is_child_theme() ) {
$allowed_theme_count++;
}
// If there's a default theme installed and not in use, we count that as allowed as well.
if ( $has_default_theme && ! $using_default_theme ) {
$allowed_theme_count++;
}
if ( $themes_total > $allowed_theme_count ) {
$has_unused_themes = true;
$themes_inactive = ( $themes_total - $allowed_theme_count );
}
// Check if any themes need to be updated.
if ( $themes_need_updates > 0 ) {
$result['status'] = 'critical';
$result['label'] = __( 'You have themes waiting to be updated' );
$result['description'] .= sprintf(
'<p>%s</p>',
sprintf(
/* translators: %d: The number of outdated themes. */
_n(
'Your site has %d theme waiting to be updated.',
'Your site has %d themes waiting to be updated.',
$themes_need_updates
),
$themes_need_updates
)
);
} else {
// Give positive feedback about the site being good about keeping things up to date.
if ( 1 === $themes_total ) {
$result['description'] .= sprintf(
'<p>%s</p>',
__( 'Your site has 1 installed theme, and it is up to date.' )
);
} elseif ( $themes_total > 0 ) {
$result['description'] .= sprintf(
'<p>%s</p>',
sprintf(
/* translators: %d: The number of themes. */
_n(
'Your site has %d installed theme, and it is up to date.',
'Your site has %d installed themes, and they are all up to date.',
$themes_total
),
$themes_total
)
);
} else {
$result['description'] .= sprintf(
'<p>%s</p>',
__( 'Your site does not have any installed themes.' )
);
}
}
if ( $has_unused_themes && $show_unused_themes && ! is_multisite() ) {
// This is a child theme, so we want to be a bit more explicit in our messages.
if ( $active_theme->parent() ) {
// Recommend removing inactive themes, except a default theme, your current one, and the parent theme.
$result['status'] = 'recommended';
$result['label'] = __( 'You should remove inactive themes' );
if ( $using_default_theme ) {
$result['description'] .= sprintf(
'<p>%s %s</p>',
sprintf(
/* translators: %d: The number of inactive themes. */
_n(
'Your site has %d inactive theme.',
'Your site has %d inactive themes.',
$themes_inactive
),
$themes_inactive
),
sprintf(
/* translators: 1: The currently active theme. 2: The active theme's parent theme. */
__( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep your active theme, %1$s, and %2$s, its parent theme.' ),
$active_theme->name,
$active_theme->parent()->name
)
);
} else {
$result['description'] .= sprintf(
'<p>%s %s</p>',
sprintf(
/* translators: %d: The number of inactive themes. */
_n(
'Your site has %d inactive theme.',
'Your site has %d inactive themes.',
$themes_inactive
),
$themes_inactive
),
sprintf(
/* translators: 1: The default theme for WordPress. 2: The currently active theme. 3: The active theme's parent theme. */
__( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep %1$s, the default WordPress theme, %2$s, your active theme, and %3$s, its parent theme.' ),
$default_theme ? $default_theme->name : WP_DEFAULT_THEME,
$active_theme->name,
$active_theme->parent()->name
)
);
}
} else {
// Recommend removing all inactive themes.
$result['status'] = 'recommended';
$result['label'] = __( 'You should remove inactive themes' );
if ( $using_default_theme ) {
$result['description'] .= sprintf(
'<p>%s %s</p>',
sprintf(
/* translators: 1: The amount of inactive themes. 2: The currently active theme. */
_n(
'Your site has %1$d inactive theme, other than %2$s, your active theme.',
'Your site has %1$d inactive themes, other than %2$s, your active theme.',
$themes_inactive
),
$themes_inactive,
$active_theme->name
),
__( 'You should consider removing any unused themes to enhance your site’s security.' )
);
} else {
$result['description'] .= sprintf(
'<p>%s %s</p>',
sprintf(
/* translators: 1: The amount of inactive themes. 2: The default theme for WordPress. 3: The currently active theme. */
_n(
'Your site has %1$d inactive theme, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
'Your site has %1$d inactive themes, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
$themes_inactive
),
$themes_inactive,
$default_theme ? $default_theme->name : WP_DEFAULT_THEME,
$active_theme->name
),
__( 'You should consider removing any unused themes to enhance your site’s security.' )
);
}
}
}
// If no default Twenty* theme exists.
if ( ! $has_default_theme ) {
$result['status'] = 'recommended';
$result['label'] = __( 'Have a default theme available' );
$result['description'] .= sprintf(
'<p>%s</p>',
__( 'Your site does not have any default theme. Default themes are used by WordPress automatically if anything is wrong with your chosen theme.' )
);
}
return $result;
}
/**
* Tests if the supplied PHP version is supported.
*
* @since 5.2.0
*
* @return array The test results.
*/
public function get_test_php_version() {
$response = wp_check_php_version();
$result = array(
'label' => sprintf(
/* translators: %s: The current PHP version. */
__( 'Your site is running the current version of PHP (%s)' ),
PHP_VERSION
),
'status' => 'good',
'badge' => array(
'label' => __( 'Performance' ),
'color' => 'blue',
),
'description' => sprintf(
'<p>%s</p>',
sprintf(
/* translators: %s: The minimum recommended PHP version. */
__( 'PHP is one of the programming languages used to build WordPress. Newer versions of PHP receive regular security updates and may increase your site’s performance. The minimum recommended version of PHP is %s.' ),
$response ? $response['recommended_version'] : ''
)
),
'actions' => sprintf(
'<p><a href="%s" target="_blank" rel="noopener">%s<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
esc_url( wp_get_update_php_url() ),
__( 'Learn more about updating PHP' ),
/* translators: Hidden accessibility text. */
__( '(opens in a new tab)' )
),
'test' => 'php_version',
);
// PHP is up to date.
if ( ! $response || version_compare( PHP_VERSION, $response['recommended_version'], '>=' ) ) {
return $result;
}
// The PHP version is older than the recommended version, but still receiving active support.
if ( $response['is_supported'] ) {
$result['label'] = sprintf(
/* translators: %s: The server PHP version. */
__( 'Your site is running on an older version of PHP (%s)' ),
PHP_VERSION
);
$result['status'] = 'recommended';
return $result;
}
/*
* The PHP version is still receiving security fixes, but is lower than
* the expected minimum version that will be required by WordPress in the near future.
*/
if ( $response['is_secure'] && $response['is_lower_than_future_minimum'] ) {
// The `is_secure` array key name doesn't actually imply this is a secure version of PHP. It only means it receives security updates.
$result['label'] = sprintf(
/* translators: %s: The server PHP version. */
__( 'Your site is running on an outdated version of PHP (%s), which soon will not be supported by WordPress.' ),
PHP_VERSION
);
$result['status'] = 'critical';
$result['badge']['label'] = __( 'Requirements' );
return $result;
}
// The PHP version is only receiving security fixes.
if ( $response['is_secure'] ) {
$result['label'] = sprintf(
/* translators: %s: The server PHP version. */
__( 'Your site is running on an older version of PHP (%s), which should be updated' ),
PHP_VERSION
);
$result['status'] = 'recommended';
return $result;
}
// No more security updates for the PHP version, and lower than the expected minimum version required by WordPress.
if ( $response['is_lower_than_future_minimum'] ) {
$message = sprintf(
/* translators: %s: The server PHP version. */
__( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates and soon will not be supported by WordPress.' ),
PHP_VERSION
);
} else {
// No more security updates for the PHP version, must be updated.
$message = sprintf(
/* translators: %s: The server PHP version. */
__( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates. It should be updated.' ),
PHP_VERSION
);
}
$result['label'] = $message;
$result['status'] = 'critical';
$result['badge']['label'] = __( 'Security' );
return $result;
}
/**
* Checks if the passed extension or function are available.
*
* Make the check for available PHP modules into a simple boolean operator for a cleaner test runner.
*
* @since 5.2.0
* @since 5.3.0 The `$constant_name` and `$class_name` parameters were added.
*
* @param string $extension_name Optional. The extension name to test. Default null.
* @param string $function_name Optional. The function name to test. Default null.
* @param string $constant_name Optional. The constant name to test for. Default null.
* @param string $class_name Optional. The class name to test for. Default null.
* @return bool Whether or not the extension and function are available.
*/
private function test_php_extension_availability( $extension_name = null, $function_name = null, $constant_name = null, $class_name = null ) {
// If no extension or function is passed, claim to fail testing, as we have nothing to test against.
if ( ! $extension_name && ! $function_name && ! $constant_name && ! $class_name ) {
return false;
}
if ( $extension_name && ! extension_loaded( $extension_name ) ) {
return false;
}
if ( $function_name && ! function_exists( $function_name ) ) {
return false;
}
if ( $constant_name && ! defined( $constant_name ) ) {
return false;
}
if ( $class_name && ! class_exists( $class_name ) ) {
return false;
}
return true;
}
/**
* Tests if required PHP modules are installed on the host.
*
* This test builds on the recommendations made by the WordPress Hosting Team
* as seen at https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions
*
* @since 5.2.0
*
* @return array
*/
public function get_test_php_extensions() {
$result = array(
'label' => __( 'Required and recommended modules are installed' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Performance' ),
'color' => 'blue',
),
'description' => sprintf(
'<p>%s</p><p>%s</p>',
__( 'PHP modules perform most of the tasks on the server that make your site run. Any changes to these must be made by your server administrator.' ),
sprintf(
/* translators: 1: Link to the hosting group page about recommended PHP modules. 2: Additional link attributes. 3: Accessibility text. */
__( 'The WordPress Hosting Team maintains a list of those modules, both recommended and required, in <a href="%1$s" %2$s>the team handbook%3$s</a>.' ),
/* translators: Localized team handbook, if one exists. */
esc_url( __( 'https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions' ) ),
'target="_blank" rel="noopener"',
sprintf(
'<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span>',
/* translators: Hidden accessibility text. */
__( '(opens in a new tab)' )
)
)
),
'actions' => '',
'test' => 'php_extensions',
);
$modules = array(
'curl' => array(
'function' => 'curl_version',
'required' => false,
),
'dom' => array(
'class' => 'DOMNode',
'required' => false,
),
'exif' => array(
'function' => 'exif_read_data',
'required' => false,
),
'fileinfo' => array(
'function' => 'finfo_file',
'required' => false,
),
'hash' => array(
'function' => 'hash',
'required' => false,
),
'imagick' => array(
'extension' => 'imagick',
'required' => false,
),
'json' => array(
'function' => 'json_last_error',
'required' => true,
),
'mbstring' => array(
'function' => 'mb_check_encoding',
'required' => false,
),
'mysqli' => array(
'function' => 'mysqli_connect',
'required' => false,
),
'libsodium' => array(
'constant' => 'SODIUM_LIBRARY_VERSION',
'required' => false,
'php_bundled_version' => '7.2.0',
),
'openssl' => array(
'function' => 'openssl_encrypt',
'required' => false,
),
'pcre' => array(
'function' => 'preg_match',
'required' => false,
),
'mod_xml' => array(
'extension' => 'libxml',
'required' => false,
),
'zip' => array(
'class' => 'ZipArchive',
'required' => false,
),
'filter' => array(
'function' => 'filter_list',
'required' => false,
),
'gd' => array(
'extension' => 'gd',
'required' => false,
'fallback_for' => 'imagick',
),
'iconv' => array(
'function' => 'iconv',
'required' => false,
),
'intl' => array(
'extension' => 'intl',
'required' => false,
),
'mcrypt' => array(
'extension' => 'mcrypt',
'required' => false,
'fallback_for' => 'libsodium',
),
'simplexml' => array(
'extension' => 'simplexml',
'required' => false,
'fallback_for' => 'mod_xml',
),
'xmlreader' => array(
'extension' => 'xmlreader',
'required' => false,
'fallback_for' => 'mod_xml',
),
'zlib' => array(
'extension' => 'zlib',
'required' => false,
'fallback_for' => 'zip',