[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/includes/ -> class-core-upgrader.php (source)

   1  <?php
   2  /**
   3   * Upgrade API: Core_Upgrader class
   4   *
   5   * @package WordPress
   6   * @subpackage Upgrader
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Core class used for updating core.
  12   *
  13   * It allows for WordPress to upgrade itself in combination with
  14   * the wp-admin/includes/update-core.php file.
  15   *
  16   * @since 2.8.0
  17   * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  18   *
  19   * @see WP_Upgrader
  20   */
  21  class Core_Upgrader extends WP_Upgrader {
  22  
  23      /**
  24       * Initialize the upgrade strings.
  25       *
  26       * @since 2.8.0
  27       */
  28  	public function upgrade_strings() {
  29          $this->strings['up_to_date'] = __( 'WordPress is at the latest version.' );
  30          $this->strings['locked']     = __( 'Another update is currently in progress.' );
  31          $this->strings['no_package'] = __( 'Update package not available.' );
  32          /* translators: %s: Package URL. */
  33          $this->strings['downloading_package']   = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
  34          $this->strings['unpack_package']        = __( 'Unpacking the update&#8230;' );
  35          $this->strings['copy_failed']           = __( 'Could not copy files.' );
  36          $this->strings['copy_failed_space']     = __( 'Could not copy files. You may have run out of disk space.' );
  37          $this->strings['start_rollback']        = __( 'Attempting to roll back to previous version.' );
  38          $this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has rolled back to your previous version.' );
  39      }
  40  
  41      /**
  42       * Upgrade WordPress core.
  43       *
  44       * @since 2.8.0
  45       *
  46       * @global WP_Filesystem_Base $wp_filesystem                WordPress filesystem subclass.
  47       * @global callable           $_wp_filesystem_direct_method
  48       *
  49       * @param object $current Response object for whether WordPress is current.
  50       * @param array  $args {
  51       *        Optional. Arguments for upgrading WordPress core. Default empty array.
  52       *
  53       *        @type bool $pre_check_md5    Whether to check the file checksums before
  54       *                                     attempting the upgrade. Default true.
  55       *        @type bool $attempt_rollback Whether to attempt to rollback the chances if
  56       *                                     there is a problem. Default false.
  57       *        @type bool $do_rollback      Whether to perform this "upgrade" as a rollback.
  58       *                                     Default false.
  59       * }
  60       * @return null|false|WP_Error False or WP_Error on failure, null on success.
  61       */
  62  	public function upgrade( $current, $args = array() ) {
  63          global $wp_filesystem;
  64  
  65          include ( ABSPATH . WPINC . '/version.php' ); // $wp_version;
  66  
  67          $start_time = time();
  68  
  69          $defaults    = array(
  70              'pre_check_md5'                => true,
  71              'attempt_rollback'             => false,
  72              'do_rollback'                  => false,
  73              'allow_relaxed_file_ownership' => false,
  74          );
  75          $parsed_args = wp_parse_args( $args, $defaults );
  76  
  77          $this->init();
  78          $this->upgrade_strings();
  79  
  80          // Is an update available?
  81          if ( ! isset( $current->response ) || $current->response == 'latest' ) {
  82              return new WP_Error( 'up_to_date', $this->strings['up_to_date'] );
  83          }
  84  
  85          $res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] );
  86          if ( ! $res || is_wp_error( $res ) ) {
  87              return $res;
  88          }
  89  
  90          $wp_dir = trailingslashit( $wp_filesystem->abspath() );
  91  
  92          $partial = true;
  93          if ( $parsed_args['do_rollback'] ) {
  94              $partial = false;
  95          } elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() ) {
  96              $partial = false;
  97          }
  98  
  99          /*
 100           * If partial update is returned from the API, use that, unless we're doing
 101           * a reinstallation. If we cross the new_bundled version number, then use
 102           * the new_bundled zip. Don't though if the constant is set to skip bundled items.
 103           * If the API returns a no_content zip, go with it. Finally, default to the full zip.
 104           */
 105          if ( $parsed_args['do_rollback'] && $current->packages->rollback ) {
 106              $to_download = 'rollback';
 107          } elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial ) {
 108              $to_download = 'partial';
 109          } elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
 110              && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) {
 111              $to_download = 'new_bundled';
 112          } elseif ( $current->packages->no_content ) {
 113              $to_download = 'no_content';
 114          } else {
 115              $to_download = 'full';
 116          }
 117  
 118          // Lock to prevent multiple Core Updates occurring
 119          $lock = WP_Upgrader::create_lock( 'core_updater', 15 * MINUTE_IN_SECONDS );
 120          if ( ! $lock ) {
 121              return new WP_Error( 'locked', $this->strings['locked'] );
 122          }
 123  
 124          $download = $this->download_package( $current->packages->$to_download, true );
 125  
 126          // Allow for signature soft-fail.
 127          // WARNING: This may be removed in the future.
 128          if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
 129              // Outout the failure error as a normal feedback, and not as an error:
 130              apply_filters( 'update_feedback', $download->get_error_message() );
 131  
 132              // Report this failure back to WordPress.org for debugging purposes.
 133              wp_version_check(
 134                  array(
 135                      'signature_failure_code' => $download->get_error_code(),
 136                      'signature_failure_data' => $download->get_error_data(),
 137                  )
 138              );
 139  
 140              // Pretend this error didn't happen.
 141              $download = $download->get_error_data( 'softfail-filename' );
 142          }
 143  
 144          if ( is_wp_error( $download ) ) {
 145              WP_Upgrader::release_lock( 'core_updater' );
 146              return $download;
 147          }
 148  
 149          $working_dir = $this->unpack_package( $download );
 150          if ( is_wp_error( $working_dir ) ) {
 151              WP_Upgrader::release_lock( 'core_updater' );
 152              return $working_dir;
 153          }
 154  
 155          // Copy update-core.php from the new version into place.
 156          if ( ! $wp_filesystem->copy( $working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true ) ) {
 157              $wp_filesystem->delete( $working_dir, true );
 158              WP_Upgrader::release_lock( 'core_updater' );
 159              return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
 160          }
 161          $wp_filesystem->chmod( $wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE );
 162  
 163          require_once ( ABSPATH . 'wp-admin/includes/update-core.php' );
 164  
 165          if ( ! function_exists( 'update_core' ) ) {
 166              WP_Upgrader::release_lock( 'core_updater' );
 167              return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
 168          }
 169  
 170          $result = update_core( $working_dir, $wp_dir );
 171  
 172          // In the event of an issue, we may be able to roll back.
 173          if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
 174              $try_rollback = false;
 175              if ( is_wp_error( $result ) ) {
 176                  $error_code = $result->get_error_code();
 177                  /*
 178                   * Not all errors are equal. These codes are critical: copy_failed__copy_dir,
 179                   * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
 180                   * do_rollback allows for update_core() to trigger a rollback if needed.
 181                   */
 182                  if ( false !== strpos( $error_code, 'do_rollback' ) ) {
 183                      $try_rollback = true;
 184                  } elseif ( false !== strpos( $error_code, '__copy_dir' ) ) {
 185                      $try_rollback = true;
 186                  } elseif ( 'disk_full' === $error_code ) {
 187                      $try_rollback = true;
 188                  }
 189              }
 190  
 191              if ( $try_rollback ) {
 192                  /** This filter is documented in wp-admin/includes/update-core.php */
 193                  apply_filters( 'update_feedback', $result );
 194  
 195                  /** This filter is documented in wp-admin/includes/update-core.php */
 196                  apply_filters( 'update_feedback', $this->strings['start_rollback'] );
 197  
 198                  $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
 199  
 200                  $original_result = $result;
 201                  $result          = new WP_Error(
 202                      'rollback_was_required',
 203                      $this->strings['rollback_was_required'],
 204                      (object) array(
 205                          'update'   => $original_result,
 206                          'rollback' => $rollback_result,
 207                      )
 208                  );
 209              }
 210          }
 211  
 212          /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
 213          do_action(
 214              'upgrader_process_complete',
 215              $this,
 216              array(
 217                  'action' => 'update',
 218                  'type'   => 'core',
 219              )
 220          );
 221  
 222          // Clear the current updates
 223          delete_site_transient( 'update_core' );
 224  
 225          if ( ! $parsed_args['do_rollback'] ) {
 226              $stats = array(
 227                  'update_type'      => $current->response,
 228                  'success'          => true,
 229                  'fs_method'        => $wp_filesystem->method,
 230                  'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
 231                  'fs_method_direct' => ! empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '',
 232                  'time_taken'       => time() - $start_time,
 233                  'reported'         => $wp_version,
 234                  'attempted'        => $current->version,
 235              );
 236  
 237              if ( is_wp_error( $result ) ) {
 238                  $stats['success'] = false;
 239                  // Did a rollback occur?
 240                  if ( ! empty( $try_rollback ) ) {
 241                      $stats['error_code'] = $original_result->get_error_code();
 242                      $stats['error_data'] = $original_result->get_error_data();
 243                      // Was the rollback successful? If not, collect its error too.
 244                      $stats['rollback'] = ! is_wp_error( $rollback_result );
 245                      if ( is_wp_error( $rollback_result ) ) {
 246                          $stats['rollback_code'] = $rollback_result->get_error_code();
 247                          $stats['rollback_data'] = $rollback_result->get_error_data();
 248                      }
 249                  } else {
 250                      $stats['error_code'] = $result->get_error_code();
 251                      $stats['error_data'] = $result->get_error_data();
 252                  }
 253              }
 254  
 255              wp_version_check( $stats );
 256          }
 257  
 258          WP_Upgrader::release_lock( 'core_updater' );
 259  
 260          return $result;
 261      }
 262  
 263      /**
 264       * Determines if this WordPress Core version should update to an offered version or not.
 265       *
 266       * @since 3.7.0
 267       *
 268       * @param string $offered_ver The offered version, of the format x.y.z.
 269       * @return bool True if we should update to the offered version, otherwise false.
 270       */
 271  	public static function should_update_to_version( $offered_ver ) {
 272          include ( ABSPATH . WPINC . '/version.php' ); // $wp_version; // x.y.z
 273  
 274          $current_branch                 = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
 275          $new_branch                     = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
 276          $current_is_development_version = (bool) strpos( $wp_version, '-' );
 277  
 278          // Defaults:
 279          $upgrade_dev   = true;
 280          $upgrade_minor = true;
 281          $upgrade_major = false;
 282  
 283          // WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
 284          if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
 285              if ( false === WP_AUTO_UPDATE_CORE ) {
 286                  // Defaults to turned off, unless a filter allows it
 287                  $upgrade_dev   = false;
 288                  $upgrade_minor = false;
 289                  $upgrade_major = false;
 290              } elseif ( true === WP_AUTO_UPDATE_CORE ) {
 291                  // ALL updates for core
 292                  $upgrade_dev   = true;
 293                  $upgrade_minor = true;
 294                  $upgrade_major = true;
 295              } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
 296                  // Only minor updates for core
 297                  $upgrade_dev   = false;
 298                  $upgrade_minor = true;
 299                  $upgrade_major = false;
 300              }
 301          }
 302  
 303          // 1: If we're already on that version, not much point in updating?
 304          if ( $offered_ver == $wp_version ) {
 305              return false;
 306          }
 307  
 308          // 2: If we're running a newer version, that's a nope
 309          if ( version_compare( $wp_version, $offered_ver, '>' ) ) {
 310              return false;
 311          }
 312  
 313          $failure_data = get_site_option( 'auto_core_update_failed' );
 314          if ( $failure_data ) {
 315              // If this was a critical update failure, cannot update.
 316              if ( ! empty( $failure_data['critical'] ) ) {
 317                  return false;
 318              }
 319  
 320              // Don't claim we can update on update-core.php if we have a non-critical failure logged.
 321              if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) ) {
 322                  return false;
 323              }
 324  
 325              // Cannot update if we're retrying the same A to B update that caused a non-critical failure.
 326              // Some non-critical failures do allow retries, like download_failed.
 327              // 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2.
 328              if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] ) {
 329                  return false;
 330              }
 331          }
 332  
 333          // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
 334          if ( $current_is_development_version ) {
 335  
 336              /**
 337               * Filters whether to enable automatic core updates for development versions.
 338               *
 339               * @since 3.7.0
 340               *
 341               * @param bool $upgrade_dev Whether to enable automatic updates for
 342               *                          development versions.
 343               */
 344              if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) ) {
 345                  return false;
 346              }
 347              // Else fall through to minor + major branches below.
 348          }
 349  
 350          // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
 351          if ( $current_branch == $new_branch ) {
 352  
 353              /**
 354               * Filters whether to enable minor automatic core updates.
 355               *
 356               * @since 3.7.0
 357               *
 358               * @param bool $upgrade_minor Whether to enable minor automatic core updates.
 359               */
 360              return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
 361          }
 362  
 363          // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
 364          if ( version_compare( $new_branch, $current_branch, '>' ) ) {
 365  
 366              /**
 367               * Filters whether to enable major automatic core updates.
 368               *
 369               * @since 3.7.0
 370               *
 371               * @param bool $upgrade_major Whether to enable major automatic core updates.
 372               */
 373              return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
 374          }
 375  
 376          // If we're not sure, we don't want it
 377          return false;
 378      }
 379  
 380      /**
 381       * Compare the disk file checksums against the expected checksums.
 382       *
 383       * @since 3.7.0
 384       *
 385       * @global string $wp_version
 386       * @global string $wp_local_package
 387       *
 388       * @return bool True if the checksums match, otherwise false.
 389       */
 390  	public function check_files() {
 391          global $wp_version, $wp_local_package;
 392  
 393          $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
 394  
 395          if ( ! is_array( $checksums ) ) {
 396              return false;
 397          }
 398  
 399          foreach ( $checksums as $file => $checksum ) {
 400              // Skip files which get updated
 401              if ( 'wp-content' == substr( $file, 0, 10 ) ) {
 402                  continue;
 403              }
 404              if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum ) {
 405                  return false;
 406              }
 407          }
 408  
 409          return true;
 410      }
 411  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7