[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Nov 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1