[ Index ]

WordPress 5.4.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/wp-admin/includes/ -> class-wp-site-health.php (source)

   1  <?php
   2  /**
   3   * Class for looking up a site's health based on a user's WordPress environment.
   4   *
   5   * @package WordPress
   6   * @subpackage Site_Health
   7   * @since 5.2.0
   8   */
   9  
  10  class WP_Site_Health {
  11      private static $instance = null;
  12  
  13      private $mysql_min_version_check;
  14      private $mysql_rec_version_check;
  15  
  16      public $is_mariadb                           = false;
  17      private $mysql_server_version                = '';
  18      private $health_check_mysql_required_version = '5.5';
  19      private $health_check_mysql_rec_version      = '';
  20  
  21      public $schedules;
  22      public $crons;
  23      public $last_missed_cron     = null;
  24      public $last_late_cron       = null;
  25      private $timeout_missed_cron = null;
  26      private $timeout_late_cron   = null;
  27  
  28      /**
  29       * WP_Site_Health constructor.
  30       *
  31       * @since 5.2.0
  32       */
  33  	public function __construct() {
  34          $this->maybe_create_scheduled_event();
  35  
  36          $this->timeout_late_cron   = 0;
  37          $this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS;
  38  
  39          if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
  40              $this->timeout_late_cron   = - 15 * MINUTE_IN_SECONDS;
  41              $this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS;
  42          }
  43  
  44          add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
  45  
  46          add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  47          add_action( 'wp_site_health_scheduled_check', array( $this, 'wp_cron_scheduled_check' ) );
  48      }
  49  
  50      /**
  51       * Return an instance of the WP_Site_Health class, or create one if none exist yet.
  52       *
  53       * @since 5.4.0
  54       *
  55       * @return WP_Site_Health|null
  56       */
  57  	public static function get_instance() {
  58          if ( null === self::$instance ) {
  59              self::$instance = new WP_Site_Health();
  60          }
  61  
  62          return self::$instance;
  63      }
  64  
  65      /**
  66       * Enqueues the site health scripts.
  67       *
  68       * @since 5.2.0
  69       */
  70  	public function enqueue_scripts() {
  71          $screen = get_current_screen();
  72          if ( 'site-health' !== $screen->id && 'dashboard' !== $screen->id ) {
  73              return;
  74          }
  75  
  76          $health_check_js_variables = array(
  77              'screen'      => $screen->id,
  78              'nonce'       => array(
  79                  'site_status'        => wp_create_nonce( 'health-check-site-status' ),
  80                  'site_status_result' => wp_create_nonce( 'health-check-site-status-result' ),
  81              ),
  82              'site_status' => array(
  83                  'direct' => array(),
  84                  'async'  => array(),
  85                  'issues' => array(
  86                      'good'        => 0,
  87                      'recommended' => 0,
  88                      'critical'    => 0,
  89                  ),
  90              ),
  91          );
  92  
  93          $issue_counts = get_transient( 'health-check-site-status-result' );
  94  
  95          if ( false !== $issue_counts ) {
  96              $issue_counts = json_decode( $issue_counts );
  97  
  98              $health_check_js_variables['site_status']['issues'] = $issue_counts;
  99          }
 100  
 101          if ( 'site-health' === $screen->id && ! isset( $_GET['tab'] ) ) {
 102              $tests = WP_Site_Health::get_tests();
 103  
 104              // Don't run https test on localhost.
 105              if ( 'localhost' === preg_replace( '|https?://|', '', get_site_url() ) ) {
 106                  unset( $tests['direct']['https_status'] );
 107              }
 108  
 109              foreach ( $tests['direct'] as $test ) {
 110                  if ( is_string( $test['test'] ) ) {
 111                      $test_function = sprintf(
 112                          'get_test_%s',
 113                          $test['test']
 114                      );
 115  
 116                      if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
 117                          $health_check_js_variables['site_status']['direct'][] = $this->perform_test( array( $this, $test_function ) );
 118                          continue;
 119                      }
 120                  }
 121  
 122                  if ( is_callable( $test['test'] ) ) {
 123                      $health_check_js_variables['site_status']['direct'][] = $this->perform_test( $test['test'] );
 124                  }
 125              }
 126  
 127              foreach ( $tests['async'] as $test ) {
 128                  if ( is_string( $test['test'] ) ) {
 129                      $health_check_js_variables['site_status']['async'][] = array(
 130                          'test'      => $test['test'],
 131                          'completed' => false,
 132                      );
 133                  }
 134              }
 135          }
 136  
 137          wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables );
 138      }
 139  
 140      /**
 141       * Run a Site Health test directly.
 142       *
 143       * @since 5.4.0
 144       *
 145       * @param $callback
 146       *
 147       * @return mixed|void
 148       */
 149  	private function perform_test( $callback ) {
 150          /**
 151           * Filter the output of a finished Site Health test.
 152           *
 153           * @since 5.3.0
 154           *
 155           * @param array $test_result {
 156           *     An associated array of test result data.
 157           *
 158           *     @param string $label  A label describing the test, and is used as a header in the output.
 159           *     @param string $status The status of the test, which can be a value of `good`, `recommended` or `critical`.
 160           *     @param array  $badge {
 161           *         Tests are put into categories which have an associated badge shown, these can be modified and assigned here.
 162           *
 163           *         @param string $label The test label, for example `Performance`.
 164           *         @param string $color Default `blue`. A string representing a color to use for the label.
 165           *     }
 166           *     @param string $description A more descriptive explanation of what the test looks for, and why it is important for the end user.
 167           *     @param string $actions     An action to direct the user to where they can resolve the issue, if one exists.
 168           *     @param string $test        The name of the test being ran, used as a reference point.
 169           * }
 170           */
 171          return apply_filters( 'site_status_test_result', call_user_func( $callback ) );
 172      }
 173  
 174      /**
 175       * Run the SQL version checks.
 176       *
 177       * These values are used in later tests, but the part of preparing them is more easily managed early
 178       * in the class for ease of access and discovery.
 179       *
 180       * @since 5.2.0
 181       *
 182       * @global wpdb $wpdb WordPress database abstraction object.
 183       */
 184  	private function prepare_sql_data() {
 185          global $wpdb;
 186  
 187          if ( $wpdb->use_mysqli ) {
 188              // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_server_info
 189              $mysql_server_type = mysqli_get_server_info( $wpdb->dbh );
 190          } else {
 191              // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_server_info,PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved
 192              $mysql_server_type = mysql_get_server_info( $wpdb->dbh );
 193          }
 194  
 195          $this->mysql_server_version = $wpdb->get_var( 'SELECT VERSION()' );
 196  
 197          $this->health_check_mysql_rec_version = '5.6';
 198  
 199          if ( stristr( $mysql_server_type, 'mariadb' ) ) {
 200              $this->is_mariadb                     = true;
 201              $this->health_check_mysql_rec_version = '10.0';
 202          }
 203  
 204          $this->mysql_min_version_check = version_compare( '5.5', $this->mysql_server_version, '<=' );
 205          $this->mysql_rec_version_check = version_compare( $this->health_check_mysql_rec_version, $this->mysql_server_version, '<=' );
 206      }
 207  
 208      /**
 209       * Test if `wp_version_check` is blocked.
 210       *
 211       * It's possible to block updates with the `wp_version_check` filter, but this can't be checked during an
 212       * AJAX call, as the filter is never introduced then.
 213       *
 214       * This filter overrides a normal page request if it's made by an admin through the AJAX call with the
 215       * right query argument to check for this.
 216       *
 217       * @since 5.2.0
 218       */
 219  	public function check_wp_version_check_exists() {
 220          if ( ! is_admin() || ! is_user_logged_in() || ! current_user_can( 'update_core' ) || ! isset( $_GET['health-check-test-wp_version_check'] ) ) {
 221              return;
 222          }
 223  
 224          echo ( has_filter( 'wp_version_check', 'wp_version_check' ) ? 'yes' : 'no' );
 225  
 226          die();
 227      }
 228  
 229      /**
 230       * Tests for WordPress version and outputs it.
 231       *
 232       * Gives various results depending on what kind of updates are available, if any, to encourage the
 233       * user to install security updates as a priority.
 234       *
 235       * @since 5.2.0
 236       *
 237       * @return array The test result.
 238       */
 239  	public function get_test_wordpress_version() {
 240          $result = array(
 241              'label'       => '',
 242              'status'      => '',
 243              'badge'       => array(
 244                  'label' => __( 'Performance' ),
 245                  'color' => 'blue',
 246              ),
 247              'description' => '',
 248              'actions'     => '',
 249              'test'        => 'wordpress_version',
 250          );
 251  
 252          $core_current_version = get_bloginfo( 'version' );
 253          $core_updates         = get_core_updates();
 254  
 255          if ( ! is_array( $core_updates ) ) {
 256              $result['status'] = 'recommended';
 257  
 258              $result['label'] = sprintf(
 259                  /* translators: %s: Your current version of WordPress. */
 260                  __( 'WordPress version %s' ),
 261                  $core_current_version
 262              );
 263  
 264              $result['description'] = sprintf(
 265                  '<p>%s</p>',
 266                  __( 'We were unable to check if any new versions of WordPress are available.' )
 267              );
 268  
 269              $result['actions'] = sprintf(
 270                  '<a href="%s">%s</a>',
 271                  esc_url( admin_url( 'update-core.php?force-check=1' ) ),
 272                  __( 'Check for updates manually' )
 273              );
 274          } else {
 275              foreach ( $core_updates as $core => $update ) {
 276                  if ( 'upgrade' === $update->response ) {
 277                      $current_version = explode( '.', $core_current_version );
 278                      $new_version     = explode( '.', $update->version );
 279  
 280                      $current_major = $current_version[0] . '.' . $current_version[1];
 281                      $new_major     = $new_version[0] . '.' . $new_version[1];
 282  
 283                      $result['label'] = sprintf(
 284                          /* translators: %s: The latest version of WordPress available. */
 285                          __( 'WordPress update available (%s)' ),
 286                          $update->version
 287                      );
 288  
 289                      $result['actions'] = sprintf(
 290                          '<a href="%s">%s</a>',
 291                          esc_url( admin_url( 'update-core.php' ) ),
 292                          __( 'Install the latest version of WordPress' )
 293                      );
 294  
 295                      if ( $current_major !== $new_major ) {
 296                          // This is a major version mismatch.
 297                          $result['status']      = 'recommended';
 298                          $result['description'] = sprintf(
 299                              '<p>%s</p>',
 300                              __( 'A new version of WordPress is available.' )
 301                          );
 302                      } else {
 303                          // This is a minor version, sometimes considered more critical.
 304                          $result['status']         = 'critical';
 305                          $result['badge']['label'] = __( 'Security' );
 306                          $result['description']    = sprintf(
 307                              '<p>%s</p>',
 308                              __( 'A new minor update is available for your site. Because minor updates often address security, it&#8217;s important to install them.' )
 309                          );
 310                      }
 311                  } else {
 312                      $result['status'] = 'good';
 313                      $result['label']  = sprintf(
 314                          /* translators: %s: The current version of WordPress installed on this site. */
 315                          __( 'Your version of WordPress (%s) is up to date' ),
 316                          $core_current_version
 317                      );
 318  
 319                      $result['description'] = sprintf(
 320                          '<p>%s</p>',
 321                          __( 'You are currently running the latest version of WordPress available, keep it up!' )
 322                      );
 323                  }
 324              }
 325          }
 326  
 327          return $result;
 328      }
 329  
 330      /**
 331       * Test if plugins are outdated, or unnecessary.
 332       *
 333       * The tests checks if your plugins are up to date, and encourages you to remove any that are not in use.
 334       *
 335       * @since 5.2.0
 336       *
 337       * @return array The test result.
 338       */
 339  	public function get_test_plugin_version() {
 340          $result = array(
 341              'label'       => __( 'Your plugins are all up to date' ),
 342              'status'      => 'good',
 343              'badge'       => array(
 344                  'label' => __( 'Security' ),
 345                  'color' => 'blue',
 346              ),
 347              'description' => sprintf(
 348                  '<p>%s</p>',
 349                  __( 'Plugins extend your site&#8217;s functionality with things like contact forms, ecommerce and much more. That means they have deep access to your site, so it&#8217;s vital to keep them up to date.' )
 350              ),
 351              'actions'     => sprintf(
 352                  '<p><a href="%s">%s</a></p>',
 353                  esc_url( admin_url( 'plugins.php' ) ),
 354                  __( 'Manage your plugins' )
 355              ),
 356              'test'        => 'plugin_version',
 357          );
 358  
 359          $plugins        = get_plugins();
 360          $plugin_updates = get_plugin_updates();
 361  
 362          $plugins_have_updates = false;
 363          $plugins_active       = 0;
 364          $plugins_total        = 0;
 365          $plugins_need_update  = 0;
 366  
 367          // Loop over the available plugins and check their versions and active state.
 368          foreach ( $plugins as $plugin_path => $plugin ) {
 369              $plugins_total++;
 370  
 371              if ( is_plugin_active( $plugin_path ) ) {
 372                  $plugins_active++;
 373              }
 374  
 375              $plugin_version = $plugin['Version'];
 376  
 377              if ( array_key_exists( $plugin_path, $plugin_updates ) ) {
 378                  $plugins_need_update++;
 379                  $plugins_have_updates = true;
 380              }
 381          }
 382  
 383          // Add a notice if there are outdated plugins.
 384          if ( $plugins_need_update > 0 ) {
 385              $result['status'] = 'critical';
 386  
 387              $result['label'] = __( 'You have plugins waiting to be updated' );
 388  
 389              $result['description'] .= sprintf(
 390                  '<p>%s</p>',
 391                  sprintf(
 392                      /* translators: %d: The number of outdated plugins. */
 393                      _n(
 394                          'Your site has %d plugin waiting to be updated.',
 395                          'Your site has %d plugins waiting to be updated.',
 396                          $plugins_need_update
 397                      ),
 398                      $plugins_need_update
 399                  )
 400              );
 401  
 402              $result['actions'] .= sprintf(
 403                  '<p><a href="%s">%s</a></p>',
 404                  esc_url( network_admin_url( 'plugins.php?plugin_status=upgrade' ) ),
 405                  __( 'Update your plugins' )
 406              );
 407          } else {
 408              if ( 1 === $plugins_active ) {
 409                  $result['description'] .= sprintf(
 410                      '<p>%s</p>',
 411                      __( 'Your site has 1 active plugin, and it is up to date.' )
 412                  );
 413              } else {
 414                  $result['description'] .= sprintf(
 415                      '<p>%s</p>',
 416                      sprintf(
 417                          /* translators: %d: The number of active plugins. */
 418                          _n(
 419                              'Your site has %d active plugin, and it is up to date.',
 420                              'Your site has %d active plugins, and they are all up to date.',
 421                              $plugins_active
 422                          ),
 423                          $plugins_active
 424                      )
 425                  );
 426              }
 427          }
 428  
 429          // Check if there are inactive plugins.
 430          if ( $plugins_total > $plugins_active && ! is_multisite() ) {
 431              $unused_plugins = $plugins_total - $plugins_active;
 432  
 433              $result['status'] = 'recommended';
 434  
 435              $result['label'] = __( 'You should remove inactive plugins' );
 436  
 437              $result['description'] .= sprintf(
 438                  '<p>%s %s</p>',
 439                  sprintf(
 440                      /* translators: %d: The number of inactive plugins. */
 441                      _n(
 442                          'Your site has %d inactive plugin.',
 443                          'Your site has %d inactive plugins.',
 444                          $unused_plugins
 445                      ),
 446                      $unused_plugins
 447                  ),
 448                  __( 'Inactive plugins are tempting targets for attackers. If you&#8217;re not going to use a plugin, we recommend you remove it.' )
 449              );
 450  
 451              $result['actions'] .= sprintf(
 452                  '<p><a href="%s">%s</a></p>',
 453                  esc_url( admin_url( 'plugins.php?plugin_status=inactive' ) ),
 454                  __( 'Manage inactive plugins' )
 455              );
 456          }
 457  
 458          return $result;
 459      }
 460  
 461      /**
 462       * Test if themes are outdated, or unnecessary.
 463       *
 464       * The tests checks if your site has a default theme (to fall back on if there is a need), if your themes
 465       * are up to date and, finally, encourages you to remove any themes that are not needed.
 466       *
 467       * @since 5.2.0
 468       *
 469       * @return array The test results.
 470       */
 471  	public function get_test_theme_version() {
 472          $result = array(
 473              'label'       => __( 'Your themes are all up to date' ),
 474              'status'      => 'good',
 475              'badge'       => array(
 476                  'label' => __( 'Security' ),
 477                  'color' => 'blue',
 478              ),
 479              'description' => sprintf(
 480                  '<p>%s</p>',
 481                  __( 'Themes add your site&#8217;s look and feel. It&#8217;s important to keep them up to date, to stay consistent with your brand and keep your site secure.' )
 482              ),
 483              'actions'     => sprintf(
 484                  '<p><a href="%s">%s</a></p>',
 485                  esc_url( admin_url( 'themes.php' ) ),
 486                  __( 'Manage your themes' )
 487              ),
 488              'test'        => 'theme_version',
 489          );
 490  
 491          $theme_updates = get_theme_updates();
 492  
 493          $themes_total        = 0;
 494          $themes_need_updates = 0;
 495          $themes_inactive     = 0;
 496  
 497          // This value is changed during processing to determine how many themes are considered a reasonable amount.
 498          $allowed_theme_count = 1;
 499  
 500          $has_default_theme   = false;
 501          $has_unused_themes   = false;
 502          $show_unused_themes  = true;
 503          $using_default_theme = false;
 504  
 505          // Populate a list of all themes available in the install.
 506          $all_themes   = wp_get_themes();
 507          $active_theme = wp_get_theme();
 508  
 509          // If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme.
 510          $default_theme = wp_get_theme( WP_DEFAULT_THEME );
 511          if ( ! $default_theme->exists() ) {
 512              $default_theme = WP_Theme::get_core_default_theme();
 513          }
 514  
 515          if ( $default_theme ) {
 516              $has_default_theme = true;
 517  
 518              if (
 519                  $active_theme->get_stylesheet() === $default_theme->get_stylesheet()
 520              ||
 521                  is_child_theme() && $active_theme->get_template() === $default_theme->get_template()
 522              ) {
 523                  $using_default_theme = true;
 524              }
 525          }
 526  
 527          foreach ( $all_themes as $theme_slug => $theme ) {
 528              $themes_total++;
 529  
 530              if ( array_key_exists( $theme_slug, $theme_updates ) ) {
 531                  $themes_need_updates++;
 532              }
 533          }
 534  
 535          // If this is a child theme, increase the allowed theme count by one, to account for the parent.
 536          if ( is_child_theme() ) {
 537              $allowed_theme_count++;
 538          }
 539  
 540          // If there's a default theme installed and not in use, we count that as allowed as well.
 541          if ( $has_default_theme && ! $using_default_theme ) {
 542              $allowed_theme_count++;
 543          }
 544  
 545          if ( $themes_total > $allowed_theme_count ) {
 546              $has_unused_themes = true;
 547              $themes_inactive   = ( $themes_total - $allowed_theme_count );
 548          }
 549  
 550          // Check if any themes need to be updated.
 551          if ( $themes_need_updates > 0 ) {
 552              $result['status'] = 'critical';
 553  
 554              $result['label'] = __( 'You have themes waiting to be updated' );
 555  
 556              $result['description'] .= sprintf(
 557                  '<p>%s</p>',
 558                  sprintf(
 559                      /* translators: %d: The number of outdated themes. */
 560                      _n(
 561                          'Your site has %d theme waiting to be updated.',
 562                          'Your site has %d themes waiting to be updated.',
 563                          $themes_need_updates
 564                      ),
 565                      $themes_need_updates
 566                  )
 567              );
 568          } else {
 569              // Give positive feedback about the site being good about keeping things up to date.
 570              if ( 1 === $themes_total ) {
 571                  $result['description'] .= sprintf(
 572                      '<p>%s</p>',
 573                      __( 'Your site has 1 installed theme, and it is up to date.' )
 574                  );
 575              } else {
 576                  $result['description'] .= sprintf(
 577                      '<p>%s</p>',
 578                      sprintf(
 579                          /* translators: %d: The number of themes. */
 580                          _n(
 581                              'Your site has %d installed theme, and it is up to date.',
 582                              'Your site has %d installed themes, and they are all up to date.',
 583                              $themes_total
 584                          ),
 585                          $themes_total
 586                      )
 587                  );
 588              }
 589          }
 590  
 591          if ( $has_unused_themes && $show_unused_themes && ! is_multisite() ) {
 592  
 593              // This is a child theme, so we want to be a bit more explicit in our messages.
 594              if ( is_child_theme() ) {
 595                  // Recommend removing inactive themes, except a default theme, your current one, and the parent theme.
 596                  $result['status'] = 'recommended';
 597  
 598                  $result['label'] = __( 'You should remove inactive themes' );
 599  
 600                  if ( $using_default_theme ) {
 601                      $result['description'] .= sprintf(
 602                          '<p>%s %s</p>',
 603                          sprintf(
 604                              /* translators: %d: The number of inactive themes. */
 605                              _n(
 606                                  'Your site has %d inactive theme.',
 607                                  'Your site has %d inactive themes.',
 608                                  $themes_inactive
 609                              ),
 610                              $themes_inactive
 611                          ),
 612                          sprintf(
 613                              /* translators: 1: The currently active theme. 2: The active theme's parent theme. */
 614                              __( 'To enhance your site&#8217;s security, we recommend you remove any themes you&#8217;re not using. You should keep your current theme, %1$s, and %2$s, its parent theme.' ),
 615                              $active_theme->name,
 616                              $active_theme->parent()->name
 617                          )
 618                      );
 619                  } else {
 620                      $result['description'] .= sprintf(
 621                          '<p>%s %s</p>',
 622                          sprintf(
 623                              /* translators: %d: The number of inactive themes. */
 624                              _n(
 625                                  'Your site has %d inactive theme.',
 626                                  'Your site has %d inactive themes.',
 627                                  $themes_inactive
 628                              ),
 629                              $themes_inactive
 630                          ),
 631                          sprintf(
 632                              /* translators: 1: The default theme for WordPress. 2: The currently active theme. 3: The active theme's parent theme. */
 633                              __( 'To enhance your site&#8217;s security, we recommend you remove any themes you&#8217;re not using. You should keep %1$s, the default WordPress theme, %2$s, your current theme, and %3$s, its parent theme.' ),
 634                              $default_theme ? $default_theme->name : WP_DEFAULT_THEME,
 635                              $active_theme->name,
 636                              $active_theme->parent()->name
 637                          )
 638                      );
 639                  }
 640              } else {
 641                  // Recommend removing all inactive themes.
 642                  $result['status'] = 'recommended';
 643  
 644                  $result['label'] = __( 'You should remove inactive themes' );
 645  
 646                  if ( $using_default_theme ) {
 647                      $result['description'] .= sprintf(
 648                          '<p>%s %s</p>',
 649                          sprintf(
 650                              /* translators: 1: The amount of inactive themes. 2: The currently active theme. */
 651                              _n(
 652                                  'Your site has %1$d inactive theme, other than %2$s, your active theme.',
 653                                  'Your site has %1$d inactive themes, other than %2$s, your active theme.',
 654                                  $themes_inactive
 655                              ),
 656                              $themes_inactive,
 657                              $active_theme->name
 658                          ),
 659                          __( 'We recommend removing any unused themes to enhance your site&#8217;s security.' )
 660                      );
 661                  } else {
 662                      $result['description'] .= sprintf(
 663                          '<p>%s %s</p>',
 664                          sprintf(
 665                              /* translators: 1: The amount of inactive themes. 2: The default theme for WordPress. 3: The currently active theme. */
 666                              _n(
 667                                  'Your site has %1$d inactive theme, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
 668                                  'Your site has %1$d inactive themes, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
 669                                  $themes_inactive
 670                              ),
 671                              $themes_inactive,
 672                              $default_theme ? $default_theme->name : WP_DEFAULT_THEME,
 673                              $active_theme->name
 674                          ),
 675                          __( 'We recommend removing any unused themes to enhance your site&#8217;s security.' )
 676                      );
 677                  }
 678              }
 679          }
 680  
 681          // If no default Twenty* theme exists.
 682          if ( ! $has_default_theme ) {
 683              $result['status'] = 'recommended';
 684  
 685              $result['label'] = __( 'Have a default theme available' );
 686  
 687              $result['description'] .= sprintf(
 688                  '<p>%s</p>',
 689                  __( 'Your site does not have any default theme. Default themes are used by WordPress automatically if anything is wrong with your normal theme.' )
 690              );
 691          }
 692  
 693          return $result;
 694      }
 695  
 696      /**
 697       * Test if the supplied PHP version is supported.
 698       *
 699       * @since 5.2.0
 700       *
 701       * @return array The test results.
 702       */
 703  	public function get_test_php_version() {
 704          $response = wp_check_php_version();
 705  
 706          $result = array(
 707              'label'       => sprintf(
 708                  /* translators: %s: The current PHP version. */
 709                  __( 'Your site is running the current version of PHP (%s)' ),
 710                  PHP_VERSION
 711              ),
 712              'status'      => 'good',
 713              'badge'       => array(
 714                  'label' => __( 'Performance' ),
 715                  'color' => 'blue',
 716              ),
 717              'description' => sprintf(
 718                  '<p>%s</p>',
 719                  sprintf(
 720                      /* translators: %s: The minimum recommended PHP version. */
 721                      __( 'PHP is the programming language used to build and maintain WordPress. Newer versions of PHP are faster and more secure, so staying up to date will help your site&#8217;s overall performance and security. The minimum recommended version of PHP is %s.' ),
 722                      $response['recommended_version']
 723                  )
 724              ),
 725              'actions'     => sprintf(
 726                  '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
 727                  esc_url( wp_get_update_php_url() ),
 728                  __( 'Learn more about updating PHP' ),
 729                  /* translators: Accessibility text. */
 730                  __( '(opens in a new tab)' )
 731              ),
 732              'test'        => 'php_version',
 733          );
 734  
 735          // PHP is up to date.
 736          if ( ! $response || version_compare( PHP_VERSION, $response['recommended_version'], '>=' ) ) {
 737              return $result;
 738          }
 739  
 740          // The PHP version is older than the recommended version, but still receiving active support.
 741          if ( $response['is_supported'] ) {
 742              $result['label'] = sprintf(
 743                  /* translators: %s: The server PHP version. */
 744                  __( 'Your site is running an older version of PHP (%s)' ),
 745                  PHP_VERSION
 746              );
 747              $result['status'] = 'recommended';
 748  
 749              return $result;
 750          }
 751  
 752          // The PHP version is only receiving security fixes.
 753          if ( $response['is_secure'] ) {
 754              $result['label'] = sprintf(
 755                  /* translators: %s: The server PHP version. */
 756                  __( 'Your site is running an older version of PHP (%s), which should be updated' ),
 757                  PHP_VERSION
 758              );
 759              $result['status'] = 'recommended';
 760  
 761              return $result;
 762          }
 763  
 764          // Anything no longer secure must be updated.
 765          $result['label'] = sprintf(
 766              /* translators: %s: The server PHP version. */
 767              __( 'Your site is running an outdated version of PHP (%s), which requires an update' ),
 768              PHP_VERSION
 769          );
 770          $result['status']         = 'critical';
 771          $result['badge']['label'] = __( 'Security' );
 772  
 773          return $result;
 774      }
 775  
 776      /**
 777       * Check if the passed extension or function are available.
 778       *
 779       * Make the check for available PHP modules into a simple boolean operator for a cleaner test runner.
 780       *
 781       * @since 5.2.0
 782       * @since 5.3.0 The `$constant` and `$class` parameters were added.
 783       *
 784       * @param string $extension Optional. The extension name to test. Default null.
 785       * @param string $function  Optional. The function name to test. Default null.
 786       * @param string $constant  Optional. The constant name to test for. Default null.
 787       * @param string $class     Optional. The class name to test for. Default null.
 788       *
 789       * @return bool Whether or not the extension and function are available.
 790       */
 791  	private function test_php_extension_availability( $extension = null, $function = null, $constant = null, $class = null ) {
 792          // If no extension or function is passed, claim to fail testing, as we have nothing to test against.
 793          if ( ! $extension && ! $function && ! $constant && ! $class ) {
 794              return false;
 795          }
 796  
 797          if ( $extension && ! extension_loaded( $extension ) ) {
 798              return false;
 799          }
 800          if ( $function && ! function_exists( $function ) ) {
 801              return false;
 802          }
 803          if ( $constant && ! defined( $constant ) ) {
 804              return false;
 805          }
 806          if ( $class && ! class_exists( $class ) ) {
 807              return false;
 808          }
 809  
 810          return true;
 811      }
 812  
 813      /**
 814       * Test if required PHP modules are installed on the host.
 815       *
 816       * This test builds on the recommendations made by the WordPress Hosting Team
 817       * as seen at https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions
 818       *
 819       * @since 5.2.0
 820       *
 821       * @return array
 822       */
 823  	public function get_test_php_extensions() {
 824          $result = array(
 825              'label'       => __( 'Required and recommended modules are installed' ),
 826              'status'      => 'good',
 827              'badge'       => array(
 828                  'label' => __( 'Performance' ),
 829                  'color' => 'blue',
 830              ),
 831              'description' => sprintf(
 832                  '<p>%s</p><p>%s</p>',
 833                  __( '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.' ),
 834                  sprintf(
 835                      /* translators: 1: Link to the hosting group page about recommended PHP modules. 2: Additional link attributes. 3: Accessibility text. */
 836                      __( '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>.' ),
 837                      /* translators: Localized team handbook, if one exists. */
 838                      esc_url( __( 'https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions' ) ),
 839                      'target="_blank" rel="noopener noreferrer"',
 840                      sprintf(
 841                          ' <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span>',
 842                          /* translators: Accessibility text. */
 843                          __( '(opens in a new tab)' )
 844                      )
 845                  )
 846              ),
 847              'actions'     => '',
 848              'test'        => 'php_extensions',
 849          );
 850  
 851          $modules = array(
 852              'curl'      => array(
 853                  'function' => 'curl_version',
 854                  'required' => false,
 855              ),
 856              'dom'       => array(
 857                  'class'    => 'DOMNode',
 858                  'required' => false,
 859              ),
 860              'exif'      => array(
 861                  'function' => 'exif_read_data',
 862                  'required' => false,
 863              ),
 864              'fileinfo'  => array(
 865                  'function' => 'finfo_file',
 866                  'required' => false,
 867              ),
 868              'hash'      => array(
 869                  'function' => 'hash',
 870                  'required' => false,
 871              ),
 872              'json'      => array(
 873                  'function' => 'json_last_error',
 874                  'required' => true,
 875              ),
 876              'mbstring'  => array(
 877                  'function' => 'mb_check_encoding',
 878                  'required' => false,
 879              ),
 880              'mysqli'    => array(
 881                  'function' => 'mysqli_connect',
 882                  'required' => false,
 883              ),
 884              'libsodium' => array(
 885                  'constant'            => 'SODIUM_LIBRARY_VERSION',
 886                  'required'            => false,
 887                  'php_bundled_version' => '7.2.0',
 888              ),
 889              'openssl'   => array(
 890                  'function' => 'openssl_encrypt',
 891                  'required' => false,
 892              ),
 893              'pcre'      => array(
 894                  'function' => 'preg_match',
 895                  'required' => false,
 896              ),
 897              'imagick'   => array(
 898                  'extension' => 'imagick',
 899                  'required'  => false,
 900              ),
 901              'mod_xml'   => array(
 902                  'extension' => 'libxml',
 903                  'required'  => false,
 904              ),
 905              'zip'       => array(
 906                  'class'    => 'ZipArchive',
 907                  'required' => false,
 908              ),
 909              'filter'    => array(
 910                  'function' => 'filter_list',
 911                  'required' => false,
 912              ),
 913              'gd'        => array(
 914                  'extension'    => 'gd',
 915                  'required'     => false,
 916                  'fallback_for' => 'imagick',
 917              ),
 918              'iconv'     => array(
 919                  'function' => 'iconv',
 920                  'required' => false,
 921              ),
 922              'mcrypt'    => array(
 923                  'extension'    => 'mcrypt',
 924                  'required'     => false,
 925                  'fallback_for' => 'libsodium',
 926              ),
 927              'simplexml' => array(
 928                  'extension'    => 'simplexml',
 929                  'required'     => false,
 930                  'fallback_for' => 'mod_xml',
 931              ),
 932              'xmlreader' => array(
 933                  'extension'    => 'xmlreader',
 934                  'required'     => false,
 935                  'fallback_for' => 'mod_xml',
 936              ),
 937              'zlib'      => array(
 938                  'extension'    => 'zlib',
 939                  'required'     => false,
 940                  'fallback_for' => 'zip',
 941              ),
 942          );
 943  
 944          /**
 945           * An array representing all the modules we wish to test for.
 946           *
 947           * @since 5.2.0
 948           * @since 5.3.0 The `$constant` and `$class` parameters were added.
 949           *
 950           * @param array $modules {
 951           *     An associated array of modules to test for.
 952           *
 953           *     array $module {
 954           *         An associated array of module properties used during testing.
 955           *         One of either `$function` or `$extension` must be provided, or they will fail by default.
 956           *
 957           *         string $function     Optional. A function name to test for the existence of.
 958           *         string $extension    Optional. An extension to check if is loaded in PHP.
 959           *         string $constant     Optional. A constant name to check for to verify an extension exists.
 960           *         string $class        Optional. A class name to check for to verify an extension exists.
 961           *         bool   $required     Is this a required feature or not.
 962           *         string $fallback_for Optional. The module this module replaces as a fallback.
 963           *     }
 964           * }
 965           */
 966          $modules = apply_filters( 'site_status_test_php_modules', $modules );
 967  
 968          $failures = array();
 969  
 970          foreach ( $modules as $library => $module ) {
 971              $extension  = ( isset( $module['extension'] ) ? $module['extension'] : null );
 972              $function   = ( isset( $module['function'] ) ? $module['function'] : null );
 973              $constant   = ( isset( $module['constant'] ) ? $module['constant'] : null );
 974              $class_name = ( isset( $module['class'] ) ? $module['class'] : null );
 975  
 976              // If this module is a fallback for another function, check if that other function passed.
 977              if ( isset( $module['fallback_for'] ) ) {
 978                  /*
 979                   * If that other function has a failure, mark this module as required for normal operations.
 980                   * If that other function hasn't failed, skip this test as it's only a fallback.
 981                   */
 982                  if ( isset( $failures[ $module['fallback_for'] ] ) ) {
 983                      $module['required'] = true;
 984                  } else {
 985                      continue;
 986                  }
 987              }
 988  
 989              if ( ! $this->test_php_extension_availability( $extension, $function, $constant, $class_name ) && ( ! isset( $module['php_bundled_version'] ) || version_compare( PHP_VERSION, $module['php_bundled_version'], '<' ) ) ) {
 990                  if ( $module['required'] ) {
 991                      $result['status'] = 'critical';
 992  
 993                      $class         = 'error';
 994                      $screen_reader = __( 'Error' );
 995                      $message       = sprintf(
 996                          /* translators: %s: The module name. */
 997                          __( 'The required module, %s, is not installed, or has been disabled.' ),
 998                          $library
 999                      );
1000                  } else {
1001                      $class         = 'warning';
1002                      $screen_reader = __( 'Warning' );
1003                      $message       = sprintf(
1004                          /* translators: %s: The module name. */
1005                          __( 'The optional module, %s, is not installed, or has been disabled.' ),
1006                          $library
1007                      );
1008                  }
1009  
1010                  if ( ! $module['required'] && 'good' === $result['status'] ) {
1011                      $result['status'] = 'recommended';
1012                  }
1013  
1014                  $failures[ $library ] = "<span class='dashicons $class'><span class='screen-reader-text'>$screen_reader</span></span> $message";
1015              }
1016          }
1017  
1018          if ( ! empty( $failures ) ) {
1019              $output = '<ul>';
1020  
1021              foreach ( $failures as $failure ) {
1022                  $output .= sprintf(
1023                      '<li>%s</li>',
1024                      $failure
1025                  );
1026              }
1027  
1028              $output .= '</ul>';
1029          }
1030  
1031          if ( 'good' !== $result['status'] ) {
1032              if ( 'recommended' === $result['status'] ) {
1033                  $result['label'] = __( 'One or more recommended modules are missing' );
1034              }
1035              if ( 'critical' === $result['status'] ) {
1036                  $result['label'] = __( 'One or more required modules are missing' );
1037              }
1038  
1039              $result['description'] .= sprintf(
1040                  '<p>%s</p>',
1041                  $output
1042              );
1043          }
1044  
1045          return $result;
1046      }
1047  
1048      /**
1049       * Test if the PHP default timezone is set to UTC.
1050       *
1051       * @since 5.3.1
1052       *
1053       * @return array The test results.
1054       */
1055  	public function get_test_php_default_timezone() {
1056          $result = array(
1057              'label'       => __( 'PHP default timezone is valid' ),
1058              'status'      => 'good',
1059              'badge'       => array(
1060                  'label' => __( 'Performance' ),
1061                  'color' => 'blue',
1062              ),
1063              'description' => sprintf(
1064                  '<p>%s</p>',
1065                  __( 'PHP default timezone was configured by WordPress on loading. This is necessary for correct calculations of dates and times.' )
1066              ),
1067              'test'        => 'php_default_timezone',
1068          );
1069  
1070          if ( 'UTC' !== date_default_timezone_get() ) {
1071              $result['status'] = 'critical';
1072  
1073              $result['label'] = __( 'PHP default timezone is invalid' );
1074  
1075              $result['description'] = sprintf(
1076                  '<p>%s</p>',
1077                  sprintf(
1078                      /* translators: %s: date_default_timezone_set() */
1079                      __( 'PHP default timezone was changed after WordPress loading by a %s function call. This interferes with correct calculations of dates and times.' ),
1080                      '<code>date_default_timezone_set()</code>'
1081                  )
1082              );
1083          }
1084  
1085          return $result;
1086      }
1087  
1088      /**
1089       * Test if the SQL server is up to date.
1090       *
1091       * @since 5.2.0
1092       *
1093       * @return array The test results.
1094       */
1095  	public function get_test_sql_server() {
1096          if ( ! $this->mysql_server_version ) {
1097              $this->prepare_sql_data();
1098          }
1099  
1100          $result = array(
1101              'label'       => __( 'SQL server is up to date' ),
1102              'status'      => 'good',
1103              'badge'       => array(
1104                  'label' => __( 'Performance' ),
1105                  'color' => 'blue',
1106              ),
1107              'description' => sprintf(
1108                  '<p>%s</p>',
1109                  __( 'The SQL server is a required piece of software for the database WordPress uses to store all your site&#8217;s content and settings.' )
1110              ),
1111              'actions'     => sprintf(
1112                  '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
1113                  /* translators: Localized version of WordPress requirements if one exists. */
1114                  esc_url( __( 'https://wordpress.org/about/requirements/' ) ),
1115                  __( 'Learn more about what WordPress requires to run.' ),
1116                  /* translators: Accessibility text. */
1117                  __( '(opens in a new tab)' )
1118              ),
1119              'test'        => 'sql_server',
1120          );
1121  
1122          $db_dropin = file_exists( WP_CONTENT_DIR . '/db.php' );
1123  
1124          if ( ! $this->mysql_rec_version_check ) {
1125              $result['status'] = 'recommended';
1126  
1127              $result['label'] = __( 'Outdated SQL server' );
1128  
1129              $result['description'] .= sprintf(
1130                  '<p>%s</p>',
1131                  sprintf(
1132                      /* translators: 1: The database engine in use (MySQL or MariaDB). 2: Database server recommended version number. */
1133                      __( 'For optimal performance and security reasons, we recommend running %1$s version %2$s or higher. Contact your web hosting company to correct this.' ),
1134                      ( $this->is_mariadb ? 'MariaDB' : 'MySQL' ),
1135                      $this->health_check_mysql_rec_version
1136                  )
1137              );
1138          }
1139  
1140          if ( ! $this->mysql_min_version_check ) {
1141              $result['status'] = 'critical';
1142  
1143              $result['label']          = __( 'Severely outdated SQL server' );
1144              $result['badge']['label'] = __( 'Security' );
1145  
1146              $result['description'] .= sprintf(
1147                  '<p>%s</p>',
1148                  sprintf(
1149                      /* translators: 1: The database engine in use (MySQL or MariaDB). 2: Database server minimum version number. */
1150                      __( 'WordPress requires %1$s version %2$s or higher. Contact your web hosting company to correct this.' ),
1151                      ( $this->is_mariadb ? 'MariaDB' : 'MySQL' ),
1152                      $this->health_check_mysql_required_version
1153                  )
1154              );
1155          }
1156  
1157          if ( $db_dropin ) {
1158              $result['description'] .= sprintf(
1159                  '<p>%s</p>',
1160                  wp_kses(
1161                      sprintf(
1162                          /* translators: 1: The name of the drop-in. 2: The name of the database engine. */
1163                          __( 'You are using a %1$s drop-in which might mean that a %2$s database is not being used.' ),
1164                          '<code>wp-content/db.php</code>',
1165                          ( $this->is_mariadb ? 'MariaDB' : 'MySQL' )
1166                      ),
1167                      array(
1168                          'code' => true,
1169                      )
1170                  )
1171              );
1172          }
1173  
1174          return $result;
1175      }
1176  
1177      /**
1178       * Test if the database server is capable of using utf8mb4.
1179       *
1180       * @since 5.2.0
1181       *
1182       * @return array The test results.
1183       */
1184  	public function get_test_utf8mb4_support() {
1185          global $wpdb;
1186  
1187          if ( ! $this->mysql_server_version ) {
1188              $this->prepare_sql_data();
1189          }
1190  
1191          $result = array(
1192              'label'       => __( 'UTF8MB4 is supported' ),
1193              'status'      => 'good',
1194              'badge'       => array(
1195                  'label' => __( 'Performance' ),
1196                  'color' => 'blue',
1197              ),
1198              'description' => sprintf(
1199                  '<p>%s</p>',
1200                  __( 'UTF8MB4 is the character set WordPress prefers for database storage because it safely supports the widest set of characters and encodings, including Emoji, enabling better support for non-English languages.' )
1201              ),
1202              'actions'     => '',
1203              'test'        => 'utf8mb4_support',
1204          );
1205  
1206          if ( ! $this->is_mariadb ) {
1207              if ( version_compare( $this->mysql_server_version, '5.5.3', '<' ) ) {
1208                  $result['status'] = 'recommended';
1209  
1210                  $result['label'] = __( 'utf8mb4 requires a MySQL update' );
1211  
1212                  $result['description'] .= sprintf(
1213                      '<p>%s</p>',
1214                      sprintf(
1215                          /* translators: %s: Version number. */
1216                          __( 'WordPress&#8217; utf8mb4 support requires MySQL version %s or greater. Please contact your server administrator.' ),
1217                          '5.5.3'
1218                      )
1219                  );
1220              } else {
1221                  $result['description'] .= sprintf(
1222                      '<p>%s</p>',
1223                      __( 'Your MySQL version supports utf8mb4.' )
1224                  );
1225              }
1226          } else { // MariaDB introduced utf8mb4 support in 5.5.0.
1227              if ( version_compare( $this->mysql_server_version, '5.5.0', '<' ) ) {
1228                  $result['status'] = 'recommended';
1229  
1230                  $result['label'] = __( 'utf8mb4 requires a MariaDB update' );
1231  
1232                  $result['description'] .= sprintf(
1233                      '<p>%s</p>',
1234                      sprintf(
1235                          /* translators: %s: Version number. */
1236                          __( 'WordPress&#8217; utf8mb4 support requires MariaDB version %s or greater. Please contact your server administrator.' ),
1237                          '5.5.0'
1238                      )
1239                  );
1240              } else {
1241                  $result['description'] .= sprintf(
1242                      '<p>%s</p>',
1243                      __( 'Your MariaDB version supports utf8mb4.' )
1244                  );
1245              }
1246          }
1247  
1248          if ( $wpdb->use_mysqli ) {
1249              // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_client_info
1250              $mysql_client_version = mysqli_get_client_info();
1251          } else {
1252              // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysql_get_client_info,PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved
1253              $mysql_client_version = mysql_get_client_info();
1254          }
1255  
1256          /*
1257           * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server.
1258           * mysqlnd has supported utf8mb4 since 5.0.9.
1259           */
1260          if ( false !== strpos( $mysql_client_version, 'mysqlnd' ) ) {
1261              $mysql_client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $mysql_client_version );
1262              if ( version_compare( $mysql_client_version, '5.0.9', '<' ) ) {
1263                  $result['status'] = 'recommended';
1264  
1265                  $result['label'] = __( 'utf8mb4 requires a newer client library' );
1266  
1267                  $result['description'] .= sprintf(
1268                      '<p>%s</p>',
1269                      sprintf(
1270                          /* translators: 1: Name of the library, 2: Number of version. */
1271                          __( 'WordPress&#8217; utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
1272                          'mysqlnd',
1273                          '5.0.9'
1274                      )
1275                  );
1276              }
1277          } else {
1278              if ( version_compare( $mysql_client_version, '5.5.3', '<' ) ) {
1279                  $result['status'] = 'recommended';
1280  
1281                  $result['label'] = __( 'utf8mb4 requires a newer client library' );
1282  
1283                  $result['description'] .= sprintf(
1284                      '<p>%s</p>',
1285                      sprintf(
1286                          /* translators: 1: Name of the library, 2: Number of version. */
1287                          __( 'WordPress&#8217; utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
1288                          'libmysql',
1289                          '5.5.3'
1290                      )
1291                  );
1292              }
1293          }
1294  
1295          return $result;
1296      }
1297  
1298      /**
1299       * Test if the site can communicate with WordPress.org.
1300       *
1301       * @since 5.2.0
1302       *
1303       * @return array The test results.
1304       */
1305  	public function get_test_dotorg_communication() {
1306          $result = array(
1307              'label'       => __( 'Can communicate with WordPress.org' ),
1308              'status'      => '',
1309              'badge'       => array(
1310                  'label' => __( 'Security' ),
1311                  'color' => 'blue',
1312              ),
1313              'description' => sprintf(
1314                  '<p>%s</p>',
1315                  __( 'Communicating with the WordPress servers is used to check for new versions, and to both install and update WordPress core, themes or plugins.' )
1316              ),
1317              'actions'     => '',
1318              'test'        => 'dotorg_communication',
1319          );
1320  
1321          $wp_dotorg = wp_remote_get(
1322              'https://api.wordpress.org',
1323              array(
1324                  'timeout' => 10,
1325              )
1326          );
1327          if ( ! is_wp_error( $wp_dotorg ) ) {
1328              $result['status'] = 'good';
1329          } else {
1330              $result['status'] = 'critical';
1331  
1332              $result['label'] = __( 'Could not reach WordPress.org' );
1333  
1334              $result['description'] .= sprintf(
1335                  '<p>%s</p>',
1336                  sprintf(
1337                      '<span class="error"><span class="screen-reader-text">%s</span></span> %s',
1338                      __( 'Error' ),
1339                      sprintf(
1340                          /* translators: 1: The IP address WordPress.org resolves to. 2: The error returned by the lookup. */
1341                          __( 'Your site is unable to reach WordPress.org at %1$s, and returned the error: %2$s' ),
1342                          gethostbyname( 'api.wordpress.org' ),
1343                          $wp_dotorg->get_error_message()
1344                      )
1345                  )
1346              );
1347  
1348              $result['actions'] = sprintf(
1349                  '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
1350                  /* translators: Localized Support reference. */
1351                  esc_url( __( 'https://wordpress.org/support' ) ),
1352                  __( 'Get help resolving this issue.' ),
1353                  /* translators: Accessibility text. */
1354                  __( '(opens in a new tab)' )
1355              );
1356          }
1357  
1358          return $result;
1359      }
1360  
1361      /**
1362       * Test if debug information is enabled.
1363       *
1364       * When WP_DEBUG is enabled, errors and information may be disclosed to site visitors, or it may be
1365       * logged to a publicly accessible file.
1366       *
1367       * Debugging is also frequently left enabled after looking for errors on a site, as site owners do
1368       * not understand the implications of this.
1369       *
1370       * @since 5.2.0
1371       *
1372       * @return array The test results.
1373       */
1374  	public function get_test_is_in_debug_mode() {
1375          $result = array(
1376              'label'       => __( 'Your site is not set to output debug information' ),
1377              'status'      => 'good',
1378              'badge'       => array(
1379                  'label' => __( 'Security' ),
1380                  'color' => 'blue',
1381              ),
1382              'description' => sprintf(
1383                  '<p>%s</p>',
1384                  __( 'Debug mode is often enabled to gather more details about an error or site failure, but may contain sensitive information which should not be available on a publicly available website.' )
1385              ),
1386              'actions'     => sprintf(
1387                  '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
1388                  /* translators: Documentation explaining debugging in WordPress. */
1389                  esc_url( __( 'https://wordpress.org/support/article/debugging-in-wordpress/' ) ),
1390                  __( 'Learn more about debugging in WordPress.' ),
1391                  /* translators: Accessibility text. */
1392                  __( '(opens in a new tab)' )
1393              ),
1394              'test'        => 'is_in_debug_mode',
1395          );
1396  
1397          if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
1398              if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
1399                  $result['label'] = __( 'Your site is set to log errors to a potentially public file.' );
1400  
1401                  $result['status'] = ( 0 === strpos( ini_get( 'error_log' ), ABSPATH ) ) ? 'critical' : 'recommended';
1402  
1403                  $result['description'] .= sprintf(
1404                      '<p>%s</p>',
1405                      sprintf(
1406                          /* translators: %s: WP_DEBUG_LOG */
1407                          __( 'The value, %s, has been added to this website&#8217;s configuration file. This means any errors on the site will be written to a file which is potentially available to normal users.' ),
1408                          '<code>WP_DEBUG_LOG</code>'
1409                      )
1410                  );
1411              }
1412  
1413              if ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) {
1414                  $result['label'] = __( 'Your site is set to display errors to site visitors' );
1415  
1416                  $result['status'] = 'critical';
1417  
1418                  $result['description'] .= sprintf(
1419                      '<p>%s</p>',
1420                      sprintf(
1421                          /* translators: 1: WP_DEBUG_DISPLAY, 2: WP_DEBUG */
1422                          __( 'The value, %1$s, has either been enabled by %2$s or added to your configuration file. This will make errors display on the front end of your site.' ),
1423                          '<code>WP_DEBUG_DISPLAY</code>',
1424                          '<code>WP_DEBUG</code>'
1425                      )
1426                  );
1427              }
1428          }
1429  
1430          return $result;
1431      }
1432  
1433      /**
1434       * Test if your site is serving content over HTTPS.
1435       *
1436       * Many sites have varying degrees of HTTPS support, the most common of which is sites that have it
1437       * enabled, but only if you visit the right site address.
1438       *
1439       * @since 5.2.0
1440       *
1441       * @return array The test results.
1442       */
1443  	public function get_test_https_status() {
1444          $result = array(
1445              'label'       => __( 'Your website is using an active HTTPS connection.' ),
1446              'status'      => 'good',
1447              'badge'       => array(
1448                  'label' => __( 'Security' ),
1449                  'color' => 'blue',
1450              ),
1451              'description' => sprintf(
1452                  '<p>%s</p>',
1453                  __( 'An HTTPS connection is a more secure way of browsing the web. Many services now have HTTPS as a requirement. HTTPS allows you to take advantage of new features that can increase site speed, improve search rankings, and gain the trust of your visitors by helping to protect their online privacy.' )
1454              ),
1455              'actions'     => sprintf(
1456                  '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s <span class="screen-reader-text">%s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
1457                  /* translators: Documentation explaining HTTPS and why it should be used. */
1458                  esc_url( __( 'https://wordpress.org/support/article/why-should-i-use-https/' ) ),
1459                  __( 'Learn more about why you should use HTTPS' ),
1460                  /* translators: Accessibility text. */
1461                  __( '(opens in a new tab)' )
1462              ),
1463              'test'        => 'https_status',
1464          );
1465  
1466          if ( is_ssl() ) {
1467              $wp_url   = get_bloginfo( 'wpurl' );
1468              $site_url = get_bloginfo( 'url' );
1469  
1470              if ( 'https' !== substr( $wp_url, 0, 5 ) || 'https' !== substr( $site_url, 0, 5 ) ) {
1471                  $result['status'] = 'recommended';
1472  
1473                  $result['label'] = __( 'Only parts of your site are using HTTPS' );
1474  
1475                  $result['description'] = sprintf(
1476                      '<p>%s</p>',
1477                      sprintf(
1478                          /* translators: %s: URL to General Settings screen. */
1479                          __( 'You are accessing this website using HTTPS, but your <a href="%s">WordPress Address</a> is not set up to use HTTPS by default.' ),
1480                          esc_url( admin_url( 'options-general.php' ) )
1481                      )
1482                  );
1483  
1484                  $result['actions'] .= sprintf(
1485                      '<p><a href="%s">%s</a></p>',
1486                      esc_url( admin_url( 'options-general.php' ) ),
1487                      __( 'Update your site addresses' )
1488                  );
1489              }
1490          } else {
1491              $result['status'] = 'recommended';
1492  
1493              $result['label'] = __( 'Your site does not use HTTPS' );
1494          }
1495  
1496          return $result;
1497      }
1498  
1499      /**
1500       * Check if the HTTP API can handle SSL/TLS requests.
1501       *
1502       * @since 5.2.0
1503       *
1504       * @return array The test results.
1505       */
1506  	public function get_test_ssl_support() {
1507          $result = array(
1508              'label'       => '',
1509              'status'      => '',
1510              'badge'       => array(
1511                  'label' => __( 'Security' ),
1512                  'color' => 'blue',
1513              ),
1514              'description' => sprintf(
1515                  '<p>%s</p>',
1516                  __( 'Securely communicating between servers are needed for transactions such as fetching files, conducting sales on store sites, and much more.' )
1517              ),
1518              'actions'     => '',
1519              'test'        => 'ssl_support',
1520          );
1521  
1522          $supports_https = wp_http_supports( array( 'ssl' ) );
1523  
1524          if ( $supports_https ) {
1525              $result['status'] = 'good';
1526  
1527              $result['label'] = __( 'Your site can communicate securely with other services' );
1528          } else {
1529              $result['status'] = 'critical';
1530  
1531              $result['label'] = __( 'Your site is unable to communicate securely with other services' );
1532  
1533              $result['description'] .= sprintf(
1534                  '<p>%s</p>',
1535                  __( 'Talk to your web host about OpenSSL support for PHP.' )
1536              );
1537          }
1538  
1539          return $result;
1540      }
1541  
1542      /**
1543       * Test if scheduled events run as intended.
1544       *
1545       * If scheduled events are not running, this may indicate something with WP_Cron is not working as intended,
1546       * or that there are orphaned events hanging around from older code.
1547       *
1548       * @since 5.2.0
1549       *
1550       * @return array The test results.
1551       */
1552  	public function get_test_scheduled_events() {
1553          $result = array(
1554              'label'       => __( 'Scheduled events are running' ),
1555              'status'      => 'good',
1556              'badge'       => array(
1557                  'label' => __( 'Performance' ),
1558                  'color' => 'blue',
1559              ),
1560              'description' => sprintf(
1561                  '<p>%s</p>',
1562                  __( 'Scheduled events are what periodically looks for updates to plugins, themes and WordPress itself. It is also what makes sure scheduled posts are published on time. It may also be used by various plugins to make sure that planned actions are executed.' )
1563              ),
1564              'actions'     => '',
1565              'test'        => 'scheduled_events',
1566          );
1567  
1568          $this->wp_schedule_test_init();
1569  
1570          if ( is_wp_error( $this->has_missed_cron() ) ) {
1571              $result['status'] = 'critical';
1572  
1573              $result['label'] = __( 'It was not possible to check your scheduled events' );
1574  
1575              $result['description'] = sprintf(
1576                  '<p>%s</p>',
1577                  sprintf(
1578                      /* translators: %s: The error message returned while from the cron scheduler. */
1579                      __( 'While trying to test your site&#8217;s scheduled events, the following error was returned: %s' ),
1580                      $this->has_missed_cron()->get_error_message()
1581                  )
1582              );
1583          } elseif ( $this->has_missed_cron() ) {
1584              $result['status'] = 'recommended';
1585  
1586              $result['label'] = __( 'A scheduled event has failed' );
1587  
1588              $result['description'] = sprintf(
1589                  '<p>%s</p>',
1590                  sprintf(
1591                      /* translators: %s: The name of the failed cron event. */
1592                      __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
1593                      $this->last_missed_cron
1594                  )
1595              );
1596          } elseif ( $this->has_late_cron() ) {
1597              $result['status'] = 'recommended';
1598  
1599              $result['label'] = __( 'A scheduled event is late' );
1600  
1601              $result['description'] = sprintf(
1602                  '<p>%s</p>',
1603                  sprintf(
1604                      /* translators: %s: The name of the late cron event. */
1605                      __( 'The scheduled event, %s, is late to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
1606                      $this->last_late_cron
1607                  )
1608              );
1609          }
1610  
1611          return $result;
1612      }
1613  
1614      /**
1615       * Test if WordPress can run automated background updates.
1616       *
1617       * Background updates in WordPress are primarily used for minor releases and security updates. It's important
1618       * to either have these working, or be aware that they are intentionally disabled for whatever reason.
1619       *
1620       * @since 5.2.0
1621       *
1622       * @return array The test results.
1623       */
1624  	public function get_test_background_updates() {
1625          $result = array(
1626              'label'       => __( 'Background updates are working' ),
1627              'status'      => 'good',
1628              'badge'       => array(
1629                  'label' => __( 'Security' ),
1630                  'color' => 'blue',
1631              ),
1632              'description' => sprintf(
1633                  '<p>%s</p>',
1634                  __( 'Background updates ensure that WordPress can auto-update if a security update is released for the version you are currently using.' )
1635              ),
1636              'actions'     => '',
1637              'test'        => 'background_updates',
1638          );
1639  
1640          if ( ! class_exists( 'WP_Site_Health_Auto_Updates' ) ) {
1641              require_once  ABSPATH . 'wp-admin/includes/class-wp-site-health-auto-updates.php';
1642          }
1643  
1644          // Run the auto-update tests in a separate class,
1645          // as there are many considerations to be made.
1646          $automatic_updates = new WP_Site_Health_Auto_Updates();
1647          $tests             = $automatic_updates->run_tests();
1648  
1649          $output = '<ul>';
1650  
1651          foreach ( $tests as $test ) {
1652              $severity_string = __( 'Passed' );
1653  
1654              if ( 'fail' === $test->severity ) {
1655                  $result['label'] = __( 'Background updates are not working as expected' );
1656  
1657                  $result['status'] = 'critical';
1658  
1659                  $severity_string = __( 'Error' );
1660              }
1661  
1662              if ( 'warning' === $test->severity && 'good' === $result['status'] ) {
1663                  $result['label'] = __( 'Background updates may not be working properly' );
1664  
1665                  $result['status'] = 'recommended';
1666  
1667                  $severity_string = __( 'Warning' );
1668              }
1669  
1670              $output .= sprintf(
1671                  '<li><span class="dashicons %s"><span class="screen-reader-text">%s</span></span> %s</li>',
1672                  esc_attr( $test->severity ),
1673                  $severity_string,
1674                  $test->description
1675              );
1676          }
1677  
1678          $output .= '</ul>';
1679  
1680          if ( 'good' !== $result['status'] ) {
1681              $result['description'] .= sprintf(
1682                  '<p>%s</p>',
1683                  $output
1684              );
1685          }
1686  
1687          return $result;
1688      }
1689  
1690      /**
1691       * Test if loopbacks work as expected.
1692       *
1693       * A loopback is when WordPress queries itself, for example to start a new WP_Cron instance, or when editing a
1694       * plugin or theme. This has shown itself to be a recurring issue as code can very easily break this interaction.
1695       *
1696       * @since 5.2.0
1697       *
1698       * @return array The test results.
1699       */
1700  	public function get_test_loopback_requests() {
1701          $result = array(
1702              'label'       => __( 'Your site can perform loopback requests' ),
1703              'status'      => 'good',
1704              'badge'       => array(
1705                  'label' => __( 'Performance' ),
1706                  'color' => 'blue',
1707              ),
1708              'description' => sprintf(
1709                  '<p>%s</p>',
1710                  __( 'Loopback requests are used to run scheduled events, and are also used by the built-in editors for themes and plugins to verify code stability.' )
1711              ),
1712              'actions'     => '',
1713              'test'        => 'loopback_requests',
1714          );
1715  
1716          $check_loopback = $this->can_perform_loopback();
1717  
1718          $result['status'] = $check_loopback->status;
1719  
1720          if ( 'good' !== $check_loopback->status ) {
1721              $result['label'] = __( 'Your site could not complete a loopback request' );
1722  
1723              $result['description'] .= sprintf(
1724                  '<p>%s</p>',
1725                  $check_loopback->message
1726              );
1727          }
1728  
1729          return $result;
1730      }
1731  
1732      /**
1733       * Test if HTTP requests are blocked.
1734       *
1735       * It's possible to block all outgoing communication (with the possibility of whitelisting hosts) via the
1736       * HTTP API. This may create problems for users as many features are running as services these days.
1737       *
1738       * @since 5.2.0
1739       *
1740       * @return array The test results.
1741       */
1742  	public function get_test_http_requests() {
1743          $result = array(
1744              'label'       => __( 'HTTP requests seem to be working as expected' ),
1745              'status'      => 'good',
1746              'badge'       => array(
1747                  'label' => __( 'Performance' ),
1748                  'color' => 'blue',
1749              ),
1750              'description' => sprintf(
1751                  '<p>%s</p>',
1752                  __( 'It is possible for site maintainers to block all, or some, communication to other sites and services. If set up incorrectly, this may prevent plugins and themes from working as intended.' )
1753              ),
1754              'actions'     => '',
1755              'test'        => 'http_requests',
1756          );
1757  
1758          $blocked = false;
1759          $hosts   = array();
1760  
1761          if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
1762              $blocked = true;
1763          }
1764  
1765          if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
1766              $hosts = explode( ',', WP_ACCESSIBLE_HOSTS );
1767          }
1768  
1769          if ( $blocked && 0 === sizeof( $hosts ) ) {
1770              $result['status'] = 'critical';
1771  
1772              $result['label'] = __( 'HTTP requests are blocked' );
1773  
1774              $result['description'] .= sprintf(
1775                  '<p>%s</p>',
1776                  sprintf(
1777                      /* translators: %s: Name of the constant used. */
1778                      __( 'HTTP requests have been blocked by the %s constant, with no allowed hosts.' ),
1779                      '<code>WP_HTTP_BLOCK_EXTERNAL</code>'
1780                  )
1781              );
1782          }
1783  
1784          if ( $blocked && 0 < sizeof( $hosts ) ) {
1785              $result['status'] = 'recommended';
1786  
1787              $result['label'] = __( 'HTTP requests are partially blocked' );
1788  
1789              $result['description'] .= sprintf(
1790                  '<p>%s</p>',
1791                  sprintf(
1792                      /* translators: 1: Name of the constant used. 2: List of hostnames whitelisted. */
1793                      __( 'HTTP requests have been blocked by the %1$s constant, with some hosts whitelisted: %2$s.' ),
1794                      '<code>WP_HTTP_BLOCK_EXTERNAL</code>',
1795                      implode( ',', $hosts )
1796                  )
1797              );
1798          }
1799  
1800          return $result;
1801      }
1802  
1803      /**
1804       * Test if the REST API is accessible.
1805       *
1806       * Various security measures may block the REST API from working, or it may have been disabled in general.
1807       * This is required for the new block editor to work, so we explicitly test for this.
1808       *
1809       * @since 5.2.0
1810       *
1811       * @return array The test results.
1812       */
1813  	public function get_test_rest_availability() {
1814          $result = array(
1815              'label'       => __( 'The REST API is available' ),
1816              'status'      => 'good',
1817              'badge'       => array(
1818                  'label' => __( 'Performance' ),
1819                  'color' => 'blue',
1820              ),
1821              'description' => sprintf(
1822                  '<p>%s</p>',
1823                  __( 'The REST API is one way WordPress, and other applications, communicate with the server. One example is the block editor screen, which relies on this to display, and save, your posts and pages.' )
1824              ),
1825              'actions'     => '',
1826              'test'        => 'rest_availability',
1827          );
1828  
1829          $cookies = wp_unslash( $_COOKIE );
1830          $timeout = 10;
1831          $headers = array(
1832              'Cache-Control' => 'no-cache',
1833              'X-WP-Nonce'    => wp_create_nonce( 'wp_rest' ),
1834          );
1835          /** This filter is documented in wp-includes/class-wp-http-streams.php */
1836          $sslverify = apply_filters( 'https_local_ssl_verify', false );
1837  
1838          // Include Basic auth in loopback requests.
1839          if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
1840              $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
1841          }
1842  
1843          $url = rest_url( 'wp/v2/types/post' );
1844  
1845          // The context for this is editing with the new block editor.
1846          $url = add_query_arg(
1847              array(
1848                  'context' => 'edit',
1849              ),
1850              $url
1851          );
1852  
1853          $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
1854  
1855          if ( is_wp_error( $r ) ) {
1856              $result['status'] = 'critical';
1857  
1858              $result['label'] = __( 'The REST API encountered an error' );
1859  
1860              $result['description'] .= sprintf(
1861                  '<p>%s</p>',
1862                  sprintf(
1863                      '%s<br>%s',
1864                      __( 'The REST API request failed due to an error.' ),
1865                      sprintf(
1866                          /* translators: 1: The WordPress error message. 2: The WordPress error code. */
1867                          __( 'Error: %1$s (%2$s)' ),
1868                          $r->get_error_message(),
1869                          $r->get_error_code()
1870                      )
1871                  )
1872              );
1873          } elseif ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
1874              $result['status'] = 'recommended';
1875  
1876              $result['label'] = __( 'The REST API encountered an unexpected result' );
1877  
1878              $result['description'] .= sprintf(
1879                  '<p>%s</p>',
1880                  sprintf(
1881                      /* translators: 1: The HTTP error code. 2: The HTTP error message. */
1882                      __( 'The REST API call gave the following unexpected result: (%1$d) %2$s.' ),
1883                      wp_remote_retrieve_response_code( $r ),
1884                      wp_remote_retrieve_body( $r )
1885                  )
1886              );
1887          } else {
1888              $json = json_decode( wp_remote_retrieve_body( $r ), true );
1889  
1890              if ( false !== $json && ! isset( $json['capabilities'] ) ) {
1891                  $result['status'] = 'recommended';
1892  
1893                  $result['label'] = __( 'The REST API did not behave correctly' );
1894  
1895                  $result['description'] .= sprintf(
1896                      '<p>%s</p>',
1897                      sprintf(
1898                          /* translators: %s: The name of the query parameter being tested. */
1899                          __( 'The REST API did not process the %s query parameter correctly.' ),
1900                          '<code>context</code>'
1901                      )
1902                  );
1903              }
1904          }
1905  
1906          return $result;
1907      }
1908  
1909      /**
1910       * Return a set of tests that belong to the site status page.
1911       *
1912       * Each site status test is defined here, they may be `direct` tests, that run on page load, or `async` tests
1913       * which will run later down the line via JavaScript calls to improve page performance and hopefully also user
1914       * experiences.
1915       *
1916       * @since 5.2.0
1917       *
1918       * @return array The list of tests to run.
1919       */
1920  	public static function get_tests() {
1921          $tests = array(
1922              'direct' => array(
1923                  'wordpress_version'    => array(
1924                      'label' => __( 'WordPress Version' ),
1925                      'test'  => 'wordpress_version',
1926                  ),
1927                  'plugin_version'       => array(
1928                      'label' => __( 'Plugin Versions' ),
1929                      'test'  => 'plugin_version',
1930                  ),
1931                  'theme_version'        => array(
1932                      'label' => __( 'Theme Versions' ),
1933                      'test'  => 'theme_version',
1934                  ),
1935                  'php_version'          => array(
1936                      'label' => __( 'PHP Version' ),
1937                      'test'  => 'php_version',
1938                  ),
1939                  'php_extensions'       => array(
1940                      'label' => __( 'PHP Extensions' ),
1941                      'test'  => 'php_extensions',
1942                  ),
1943                  'php_default_timezone' => array(
1944                      'label' => __( 'PHP Default Timezone' ),
1945                      'test'  => 'php_default_timezone',
1946                  ),
1947                  'sql_server'           => array(
1948                      'label' => __( 'Database Server version' ),
1949                      'test'  => 'sql_server',
1950                  ),
1951                  'utf8mb4_support'      => array(
1952                      'label' => __( 'MySQL utf8mb4 support' ),
1953                      'test'  => 'utf8mb4_support',
1954                  ),
1955                  'https_status'         => array(
1956                      'label' => __( 'HTTPS status' ),
1957                      'test'  => 'https_status',
1958                  ),
1959                  'ssl_support'          => array(
1960                      'label' => __( 'Secure communication' ),
1961                      'test'  => 'ssl_support',
1962                  ),
1963                  'scheduled_events'     => array(
1964                      'label' => __( 'Scheduled events' ),
1965                      'test'  => 'scheduled_events',
1966                  ),
1967                  'http_requests'        => array(
1968                      'label' => __( 'HTTP Requests' ),
1969                      'test'  => 'http_requests',
1970                  ),
1971                  'debug_enabled'        => array(
1972                      'label' => __( 'Debugging enabled' ),
1973                      'test'  => 'is_in_debug_mode',
1974                  ),
1975              ),
1976              'async'  => array(
1977                  'dotorg_communication' => array(
1978                      'label' => __( 'Communication with WordPress.org' ),
1979                      'test'  => 'dotorg_communication',
1980                  ),
1981                  'background_updates'   => array(
1982                      'label' => __( 'Background updates' ),
1983                      'test'  => 'background_updates',
1984                  ),
1985                  'loopback_requests'    => array(
1986                      'label' => __( 'Loopback request' ),
1987                      'test'  => 'loopback_requests',
1988                  ),
1989              ),
1990          );
1991  
1992          // Conditionally include REST rules if the function for it exists.
1993          if ( function_exists( 'rest_url' ) ) {
1994              $tests['direct']['rest_availability'] = array(
1995                  'label' => __( 'REST API availability' ),
1996                  'test'  => 'rest_availability',
1997              );
1998          }
1999  
2000          /**
2001           * Add or modify which site status tests are run on a site.
2002           *
2003           * The site health is determined by a set of tests based on best practices from
2004           * both the WordPress Hosting Team, but also web standards in general.
2005           *
2006           * Some sites may not have the same requirements, for example the automatic update
2007           * checks may be handled by a host, and are therefore disabled in core.
2008           * Or maybe you want to introduce a new test, is caching enabled/disabled/stale for example.
2009           *
2010           * Tests may be added either as direct, or asynchronous ones. Any test that may require some time
2011           * to complete should run asynchronously, to avoid extended loading periods within wp-admin.
2012           *
2013           * @since 5.2.0
2014           *
2015           * @param array $test_type {
2016           *     An associative array, where the `$test_type` is either `direct` or
2017           *     `async`, to declare if the test should run via AJAX calls after page load.
2018           *
2019           *     @type array $identifier {
2020           *         `$identifier` should be a unique identifier for the test that should run.
2021           *         Plugins and themes are encouraged to prefix test identifiers with their slug
2022           *         to avoid any collisions between tests.
2023           *
2024           *         @type string $label A friendly label for your test to identify it by.
2025           *         @type mixed  $test  A callable to perform a direct test, or a string AJAX action to be called
2026           *                             to perform an async test.
2027           *     }
2028           * }
2029           */
2030          $tests = apply_filters( 'site_status_tests', $tests );
2031  
2032          return $tests;
2033      }
2034  
2035      /**
2036       * Add a class to the body HTML tag.
2037       *
2038       * Filters the body class string for admin pages and adds our own class for easier styling.
2039       *
2040       * @since 5.2.0
2041       *
2042       * @param string $body_class The body class string.
2043       * @return string The modified body class string.
2044       */
2045  	public function admin_body_class( $body_class ) {
2046          $screen = get_current_screen();
2047          if ( 'site-health' !== $screen->id ) {
2048              return $body_class;
2049          }
2050  
2051          $body_class .= ' site-health';
2052  
2053          return $body_class;
2054      }
2055  
2056      /**
2057       * Initiate the WP_Cron schedule test cases.
2058       *
2059       * @since 5.2.0
2060       */
2061  	private function wp_schedule_test_init() {
2062          $this->schedules = wp_get_schedules();
2063          $this->get_cron_tasks();
2064      }
2065  
2066      /**
2067       * Populate our list of cron events and store them to a class-wide variable.
2068       *
2069       * @since 5.2.0
2070       */
2071  	private function get_cron_tasks() {
2072          $cron_tasks = _get_cron_array();
2073  
2074          if ( empty( $cron_tasks ) ) {
2075              $this->crons = new WP_Error( 'no_tasks', __( 'No scheduled events exist on this site.' ) );
2076              return;
2077          }
2078  
2079          $this->crons = array();
2080  
2081          foreach ( $cron_tasks as $time => $cron ) {
2082              foreach ( $cron as $hook => $dings ) {
2083                  foreach ( $dings as $sig => $data ) {
2084  
2085                      $this->crons[ "$hook-$sig-$time" ] = (object) array(
2086                          'hook'     => $hook,
2087                          'time'     => $time,
2088                          'sig'      => $sig,
2089                          'args'     => $data['args'],
2090                          'schedule' => $data['schedule'],
2091                          'interval' => isset( $data['interval'] ) ? $data['interval'] : null,
2092                      );
2093  
2094                  }
2095              }
2096          }
2097      }
2098  
2099      /**
2100       * Check if any scheduled tasks have been missed.
2101       *
2102       * Returns a boolean value of `true` if a scheduled task has been missed and ends processing. If the list of
2103       * crons is an instance of WP_Error, return the instance instead of a boolean value.
2104       *
2105       * @since 5.2.0
2106       *
2107       * @return bool|WP_Error True if a cron was missed, false if not. WP_Error if the cron is set to that.
2108       */
2109  	public function has_missed_cron() {
2110          if ( is_wp_error( $this->crons ) ) {
2111              return $this->crons;
2112          }
2113  
2114          foreach ( $this->crons as $id => $cron ) {
2115              if ( ( $cron->time - time() ) < $this->timeout_missed_cron ) {
2116                  $this->last_missed_cron = $cron->hook;
2117                  return true;
2118              }
2119          }
2120  
2121          return false;
2122      }
2123  
2124      /**
2125       * Check if any scheduled tasks are late.
2126       *
2127       * Returns a boolean value of `true` if a scheduled task is late and ends processing. If the list of
2128       * crons is an instance of WP_Error, return the instance instead of a boolean value.
2129       *
2130       * @since 5.3.0
2131       *
2132       * @return bool|WP_Error True if a cron is late, false if not. WP_Error if the cron is set to that.
2133       */
2134  	public function has_late_cron() {
2135          if ( is_wp_error( $this->crons ) ) {
2136              return $this->crons;
2137          }
2138  
2139          foreach ( $this->crons as $id => $cron ) {
2140              $cron_offset = $cron->time - time();
2141              if (
2142                      $cron_offset >= $this->timeout_missed_cron &&
2143                      $cron_offset < $this->timeout_late_cron
2144                  ) {
2145                  $this->last_late_cron = $cron->hook;
2146                  return true;
2147              }
2148          }
2149  
2150          return false;
2151      }
2152  
2153      /**
2154       * Run a loopback test on our site.
2155       *
2156       * Loopbacks are what WordPress uses to communicate with itself to start up WP_Cron, scheduled posts,
2157       * make sure plugin or theme edits don't cause site failures and similar.
2158       *
2159       * @since 5.2.0
2160       *
2161       * @return object The test results.
2162       */
2163  	function can_perform_loopback() {
2164          $cookies = wp_unslash( $_COOKIE );
2165          $timeout = 10;
2166          $headers = array(
2167              'Cache-Control' => 'no-cache',
2168          );
2169          /** This filter is documented in wp-includes/class-wp-http-streams.php */
2170          $sslverify = apply_filters( 'https_local_ssl_verify', false );
2171  
2172          // Include Basic auth in loopback requests.
2173          if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
2174              $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
2175          }
2176  
2177          $url = admin_url();
2178  
2179          $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
2180  
2181          if ( is_wp_error( $r ) ) {
2182              return (object) array(
2183                  'status'  => 'critical',
2184                  'message' => sprintf(
2185                      '%s<br>%s',
2186                      __( 'The loopback request to your site failed, this means features relying on them are not currently working as expected.' ),
2187                      sprintf(
2188                          /* translators: 1: The WordPress error message. 2: The WordPress error code. */
2189                          __( 'Error: %1$s (%2$s)' ),
2190                          $r->get_error_message(),
2191                          $r->get_error_code()
2192                      )
2193                  ),
2194              );
2195          }
2196  
2197          if ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
2198              return (object) array(
2199                  'status'  => 'recommended',
2200                  'message' => sprintf(
2201                      /* translators: %d: The HTTP response code returned. */
2202                      __( 'The loopback request returned an unexpected http status code, %d, it was not possible to determine if this will prevent features from working as expected.' ),
2203                      wp_remote_retrieve_response_code( $r )
2204                  ),
2205              );
2206          }
2207  
2208          return (object) array(
2209              'status'  => 'good',
2210              'message' => __( 'The loopback request to your site completed successfully.' ),
2211          );
2212      }
2213  
2214      /**
2215       * Create a weekly cron event, if one does not already exist.
2216       *
2217       * @since 5.4.0
2218       */
2219  	public function maybe_create_scheduled_event() {
2220          if ( ! wp_next_scheduled( 'wp_site_health_scheduled_check' ) && ! wp_installing() ) {
2221              wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'wp_site_health_scheduled_check' );
2222          }
2223      }
2224  
2225      /**
2226       * Run our scheduled event to check and update the latest site health status for the website.
2227       *
2228       * @since 5.4.0
2229       */
2230  	public function wp_cron_scheduled_check() {
2231          // Bootstrap wp-admin, as WP_Cron doesn't do this for us.
2232          require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/admin.php';
2233  
2234          $tests = WP_Site_Health::get_tests();
2235  
2236          $results = array();
2237  
2238          $site_status = array(
2239              'good'        => 0,
2240              'recommended' => 0,
2241              'critical'    => 0,
2242          );
2243  
2244          // Don't run https test on localhost.
2245          if ( 'localhost' === preg_replace( '|https?://|', '', get_site_url() ) ) {
2246              unset( $tests['direct']['https_status'] );
2247          }
2248  
2249          foreach ( $tests['direct'] as $test ) {
2250  
2251              if ( is_string( $test['test'] ) ) {
2252                  $test_function = sprintf(
2253                      'get_test_%s',
2254                      $test['test']
2255                  );
2256  
2257                  if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
2258                      $results[] = $this->perform_test( array( $this, $test_function ) );
2259                      continue;
2260                  }
2261              }
2262  
2263              if ( is_callable( $test['test'] ) ) {
2264                  $results[] = $this->perform_test( $test['test'] );
2265              }
2266          }
2267  
2268          foreach ( $tests['async'] as $test ) {
2269              if ( is_string( $test['test'] ) ) {
2270                  if ( isset( $test['has_rest'] ) && $test['has_rest'] ) {
2271                      $result_fetch = wp_remote_post(
2272                          rest_url( $test['test'] ),
2273                          array(
2274                              'body' => array(
2275                                  '_wpnonce' => wp_create_nonce( 'wp_rest' ),
2276                              ),
2277                          )
2278                      );
2279                  } else {
2280                      $result_fetch = wp_remote_post(
2281                          admin_url( 'admin-ajax.php' ),
2282                          array(
2283                              'body' => array(
2284                                  'action'   => $test['test'],
2285                                  '_wpnonce' => wp_create_nonce( 'health-check-site-status' ),
2286                              ),
2287                          )
2288                      );
2289                  }
2290  
2291                  if ( ! is_wp_error( $result_fetch ) ) {
2292                      $results[] = json_decode( wp_remote_retrieve_body( $result_fetch ) );
2293                  } else {
2294                      $results[] = array(
2295                          'status' => 'recommended',
2296                          'label'  => __( 'A test is unavailable' ),
2297                      );
2298                  }
2299              }
2300          }
2301  
2302          foreach ( $results as $result ) {
2303              if ( 'critical' === $result['status'] ) {
2304                  $site_status['critical']++;
2305              } elseif ( 'recommended' === $result['status'] ) {
2306                  $site_status['recommended']++;
2307              } else {
2308                  $site_status['good']++;
2309              }
2310          }
2311  
2312          set_transient( 'health-check-site-status-result', wp_json_encode( $site_status ) );
2313      }
2314  }


Generated: Tue May 19 15:51:04 2020 Cross-referenced by PHPXref 0.7.1