[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Class for testing automatic updates in the WordPress code.
   4   *
   5   * @package WordPress
   6   * @subpackage Site_Health
   7   * @since 5.2.0
   8   */
   9  
  10  class WP_Site_Health_Auto_Updates {
  11      /**
  12       * WP_Site_Health_Auto_Updates constructor.
  13       *
  14       * @since 5.2.0
  15       */
  16  	public function __construct() {
  17          require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  18      }
  19  
  20  
  21      /**
  22       * Run tests to determine if auto-updates can run.
  23       *
  24       * @since 5.2.0
  25       *
  26       * @return array The test results.
  27       */
  28  	public function run_tests() {
  29          $tests = array(
  30              $this->test_constants( 'WP_AUTO_UPDATE_CORE', array( true, 'beta', 'rc', 'minor' ) ),
  31              $this->test_wp_version_check_attached(),
  32              $this->test_filters_automatic_updater_disabled(),
  33              $this->test_wp_automatic_updates_disabled(),
  34              $this->test_if_failed_update(),
  35              $this->test_vcs_abspath(),
  36              $this->test_check_wp_filesystem_method(),
  37              $this->test_all_files_writable(),
  38              $this->test_accepts_dev_updates(),
  39              $this->test_accepts_minor_updates(),
  40          );
  41  
  42          $tests = array_filter( $tests );
  43          $tests = array_map(
  44              function( $test ) {
  45                  $test = (object) $test;
  46  
  47                  if ( empty( $test->severity ) ) {
  48                      $test->severity = 'warning';
  49                  }
  50  
  51                  return $test;
  52              },
  53              $tests
  54          );
  55  
  56          return $tests;
  57      }
  58  
  59      /**
  60       * Test if auto-updates related constants are set correctly.
  61       *
  62       * @since 5.2.0
  63       * @since 5.5.1 The `$value` parameter can accept an array.
  64       *
  65       * @param string $constant         The name of the constant to check.
  66       * @param bool|string|array $value The value that the constant should be, if set,
  67       *                                 or an array of acceptable values.
  68       * @return array The test results.
  69       */
  70  	public function test_constants( $constant, $value ) {
  71          $acceptable_values = (array) $value;
  72  
  73          if ( defined( $constant ) && ! in_array( constant( $constant ), $acceptable_values, true ) ) {
  74              return array(
  75                  'description' => sprintf(
  76                      /* translators: %s: Name of the constant used. */
  77                      __( 'The %s constant is defined and enabled.' ),
  78                      "<code>$constant</code>"
  79                  ),
  80                  'severity'    => 'fail',
  81              );
  82          }
  83      }
  84  
  85      /**
  86       * Check if updates are intercepted by a filter.
  87       *
  88       * @since 5.2.0
  89       *
  90       * @return array The test results.
  91       */
  92  	public function test_wp_version_check_attached() {
  93          if ( ! has_filter( 'wp_version_check', 'wp_version_check' ) ) {
  94              return array(
  95                  'description' => sprintf(
  96                      /* translators: %s: Name of the filter used. */
  97                      __( 'A plugin has prevented updates by disabling %s.' ),
  98                      '<code>wp_version_check()</code>'
  99                  ),
 100                  'severity'    => 'fail',
 101              );
 102          }
 103      }
 104  
 105      /**
 106       * Check if automatic updates are disabled by a filter.
 107       *
 108       * @since 5.2.0
 109       *
 110       * @return array The test results.
 111       */
 112  	public function test_filters_automatic_updater_disabled() {
 113          /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
 114          if ( apply_filters( 'automatic_updater_disabled', false ) ) {
 115              return array(
 116                  'description' => sprintf(
 117                      /* translators: %s: Name of the filter used. */
 118                      __( 'The %s filter is enabled.' ),
 119                      '<code>automatic_updater_disabled</code>'
 120                  ),
 121                  'severity'    => 'fail',
 122              );
 123          }
 124      }
 125  
 126      /**
 127       * Check if automatic updates are disabled.
 128       *
 129       * @since 5.3.0
 130       *
 131       * @return array|bool The test results. False if auto-updates are enabled.
 132       */
 133  	public function test_wp_automatic_updates_disabled() {
 134          if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
 135              require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
 136          }
 137  
 138          $auto_updates = new WP_Automatic_Updater();
 139  
 140          if ( ! $auto_updates->is_disabled() ) {
 141              return false;
 142          }
 143  
 144          return array(
 145              'description' => __( 'All automatic updates are disabled.' ),
 146              'severity'    => 'fail',
 147          );
 148      }
 149  
 150      /**
 151       * Check if automatic updates have tried to run, but failed, previously.
 152       *
 153       * @since 5.2.0
 154       *
 155       * @return array|bool The test results. False if the auto-updates failed.
 156       */
 157  	function test_if_failed_update() {
 158          $failed = get_site_option( 'auto_core_update_failed' );
 159  
 160          if ( ! $failed ) {
 161              return false;
 162          }
 163  
 164          if ( ! empty( $failed['critical'] ) ) {
 165              $description  = __( 'A previous automatic background update ended with a critical failure, so updates are now disabled.' );
 166              $description .= ' ' . __( 'You would have received an email because of this.' );
 167              $description .= ' ' . __( "When you've been able to update using the \"Update Now\" button on Dashboard > Updates, we'll clear this error for future update attempts." );
 168              $description .= ' ' . sprintf(
 169                  /* translators: %s: Code of error shown. */
 170                  __( 'The error code was %s.' ),
 171                  '<code>' . $failed['error_code'] . '</code>'
 172              );
 173              return array(
 174                  'description' => $description,
 175                  'severity'    => 'warning',
 176              );
 177          }
 178  
 179          $description = __( 'A previous automatic background update could not occur.' );
 180          if ( empty( $failed['retry'] ) ) {
 181              $description .= ' ' . __( 'You would have received an email because of this.' );
 182          }
 183  
 184          $description .= ' ' . __( "We'll try again with the next release." );
 185          $description .= ' ' . sprintf(
 186              /* translators: %s: Code of error shown. */
 187              __( 'The error code was %s.' ),
 188              '<code>' . $failed['error_code'] . '</code>'
 189          );
 190          return array(
 191              'description' => $description,
 192              'severity'    => 'warning',
 193          );
 194      }
 195  
 196      /**
 197       * Check if WordPress is controlled by a VCS (Git, Subversion etc).
 198       *
 199       * @since 5.2.0
 200       *
 201       * @return array The test results.
 202       */
 203  	public function test_vcs_abspath() {
 204          $context_dirs = array( ABSPATH );
 205          $vcs_dirs     = array( '.svn', '.git', '.hg', '.bzr' );
 206          $check_dirs   = array();
 207  
 208          foreach ( $context_dirs as $context_dir ) {
 209              // Walk up from $context_dir to the root.
 210              do {
 211                  $check_dirs[] = $context_dir;
 212  
 213                  // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
 214                  if ( dirname( $context_dir ) === $context_dir ) {
 215                      break;
 216                  }
 217  
 218                  // Continue one level at a time.
 219              } while ( $context_dir = dirname( $context_dir ) );
 220          }
 221  
 222          $check_dirs = array_unique( $check_dirs );
 223  
 224          // Search all directories we've found for evidence of version control.
 225          foreach ( $vcs_dirs as $vcs_dir ) {
 226              foreach ( $check_dirs as $check_dir ) {
 227                  // phpcs:ignore
 228                  if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
 229                      break 2;
 230                  }
 231              }
 232          }
 233  
 234          /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
 235          if ( $checkout && ! apply_filters( 'automatic_updates_is_vcs_checkout', true, ABSPATH ) ) {
 236              return array(
 237                  'description' => sprintf(
 238                      /* translators: 1: Folder name. 2: Version control directory. 3: Filter name. */
 239                      __( 'The folder %1$s was detected as being under version control (%2$s), but the %3$s filter is allowing updates.' ),
 240                      '<code>' . $check_dir . '</code>',
 241                      "<code>$vcs_dir</code>",
 242                      '<code>automatic_updates_is_vcs_checkout</code>'
 243                  ),
 244                  'severity'    => 'info',
 245              );
 246          }
 247  
 248          if ( $checkout ) {
 249              return array(
 250                  'description' => sprintf(
 251                      /* translators: 1: Folder name. 2: Version control directory. */
 252                      __( 'The folder %1$s was detected as being under version control (%2$s).' ),
 253                      '<code>' . $check_dir . '</code>',
 254                      "<code>$vcs_dir</code>"
 255                  ),
 256                  'severity'    => 'warning',
 257              );
 258          }
 259  
 260          return array(
 261              'description' => __( 'No version control systems were detected.' ),
 262              'severity'    => 'pass',
 263          );
 264      }
 265  
 266      /**
 267       * Check if we can access files without providing credentials.
 268       *
 269       * @since 5.2.0
 270       *
 271       * @return array The test results.
 272       */
 273  	function test_check_wp_filesystem_method() {
 274          // Make sure the `request_filesystem_credentials` function is available during our REST call.
 275          if ( ! function_exists( 'request_filesystem_credentials' ) ) {
 276              require_once ABSPATH . '/wp-admin/includes/file.php';
 277          }
 278  
 279          $skin    = new Automatic_Upgrader_Skin;
 280          $success = $skin->request_filesystem_credentials( false, ABSPATH );
 281  
 282          if ( ! $success ) {
 283              $description  = __( 'Your installation of WordPress prompts for FTP credentials to perform updates.' );
 284              $description .= ' ' . __( '(Your site is performing updates over FTP due to file ownership. Talk to your hosting company.)' );
 285  
 286              return array(
 287                  'description' => $description,
 288                  'severity'    => 'fail',
 289              );
 290          }
 291  
 292          return array(
 293              'description' => __( "Your installation of WordPress doesn't require FTP credentials to perform updates." ),
 294              'severity'    => 'pass',
 295          );
 296      }
 297  
 298      /**
 299       * Check if core files are writable by the web user/group.
 300       *
 301       * @since 5.2.0
 302       *
 303       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 304       *
 305       * @return array|bool The test results. False if they're not writeable.
 306       */
 307  	function test_all_files_writable() {
 308          global $wp_filesystem;
 309  
 310          require  ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
 311  
 312          $skin    = new Automatic_Upgrader_Skin;
 313          $success = $skin->request_filesystem_credentials( false, ABSPATH );
 314  
 315          if ( ! $success ) {
 316              return false;
 317          }
 318  
 319          WP_Filesystem();
 320  
 321          if ( 'direct' !== $wp_filesystem->method ) {
 322              return false;
 323          }
 324  
 325          // Make sure the `get_core_checksums` function is available during our REST call.
 326          if ( ! function_exists( 'get_core_checksums' ) ) {
 327              require_once ABSPATH . '/wp-admin/includes/update.php';
 328          }
 329  
 330          $checksums = get_core_checksums( $wp_version, 'en_US' );
 331          $dev       = ( false !== strpos( $wp_version, '-' ) );
 332          // Get the last stable version's files and test against that.
 333          if ( ! $checksums && $dev ) {
 334              $checksums = get_core_checksums( (float) $wp_version - 0.1, 'en_US' );
 335          }
 336  
 337          // There aren't always checksums for development releases, so just skip the test if we still can't find any.
 338          if ( ! $checksums && $dev ) {
 339              return false;
 340          }
 341  
 342          if ( ! $checksums ) {
 343              $description = sprintf(
 344                  /* translators: %s: WordPress version. */
 345                  __( "Couldn't retrieve a list of the checksums for WordPress %s." ),
 346                  $wp_version
 347              );
 348              $description .= ' ' . __( 'This could mean that connections are failing to WordPress.org.' );
 349              return array(
 350                  'description' => $description,
 351                  'severity'    => 'warning',
 352              );
 353          }
 354  
 355          $unwritable_files = array();
 356          foreach ( array_keys( $checksums ) as $file ) {
 357              if ( 'wp-content' === substr( $file, 0, 10 ) ) {
 358                  continue;
 359              }
 360              if ( ! file_exists( ABSPATH . $file ) ) {
 361                  continue;
 362              }
 363              if ( ! is_writable( ABSPATH . $file ) ) {
 364                  $unwritable_files[] = $file;
 365              }
 366          }
 367  
 368          if ( $unwritable_files ) {
 369              if ( count( $unwritable_files ) > 20 ) {
 370                  $unwritable_files   = array_slice( $unwritable_files, 0, 20 );
 371                  $unwritable_files[] = '...';
 372              }
 373              return array(
 374                  'description' => __( 'Some files are not writable by WordPress:' ) . ' <ul><li>' . implode( '</li><li>', $unwritable_files ) . '</li></ul>',
 375                  'severity'    => 'fail',
 376              );
 377          } else {
 378              return array(
 379                  'description' => __( 'All of your WordPress files are writable.' ),
 380                  'severity'    => 'pass',
 381              );
 382          }
 383      }
 384  
 385      /**
 386       * Check if the install is using a development branch and can use nightly packages.
 387       *
 388       * @since 5.2.0
 389       *
 390       * @return array|bool The test results. False if it isn't a development version.
 391       */
 392  	function test_accepts_dev_updates() {
 393          require  ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
 394          // Only for dev versions.
 395          if ( false === strpos( $wp_version, '-' ) ) {
 396              return false;
 397          }
 398  
 399          if ( defined( 'WP_AUTO_UPDATE_CORE' ) && ( 'minor' === WP_AUTO_UPDATE_CORE || false === WP_AUTO_UPDATE_CORE ) ) {
 400              return array(
 401                  'description' => sprintf(
 402                      /* translators: %s: Name of the constant used. */
 403                      __( 'WordPress development updates are blocked by the %s constant.' ),
 404                      '<code>WP_AUTO_UPDATE_CORE</code>'
 405                  ),
 406                  'severity'    => 'fail',
 407              );
 408          }
 409  
 410          /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
 411          if ( ! apply_filters( 'allow_dev_auto_core_updates', $wp_version ) ) {
 412              return array(
 413                  'description' => sprintf(
 414                      /* translators: %s: Name of the filter used. */
 415                      __( 'WordPress development updates are blocked by the %s filter.' ),
 416                      '<code>allow_dev_auto_core_updates</code>'
 417                  ),
 418                  'severity'    => 'fail',
 419              );
 420          }
 421      }
 422  
 423      /**
 424       * Check if the site supports automatic minor updates.
 425       *
 426       * @since 5.2.0
 427       *
 428       * @return array The test results.
 429       */
 430  	function test_accepts_minor_updates() {
 431          if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
 432              return array(
 433                  'description' => sprintf(
 434                      /* translators: %s: Name of the constant used. */
 435                      __( 'WordPress security and maintenance releases are blocked by %s.' ),
 436                      "<code>define( 'WP_AUTO_UPDATE_CORE', false );</code>"
 437                  ),
 438                  'severity'    => 'fail',
 439              );
 440          }
 441  
 442          /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
 443          if ( ! apply_filters( 'allow_minor_auto_core_updates', true ) ) {
 444              return array(
 445                  'description' => sprintf(
 446                      /* translators: %s: Name of the filter used. */
 447                      __( 'WordPress security and maintenance releases are blocked by the %s filter.' ),
 448                      '<code>allow_minor_auto_core_updates</code>'
 449                  ),
 450                  'severity'    => 'fail',
 451              );
 452          }
 453      }
 454  }


Generated: Fri Dec 4 01:00:03 2020 Cross-referenced by PHPXref 0.7.1