[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Upgrade API: WP_Upgrader class
   4   *
   5   * Requires skin classes and WP_Upgrader subclasses for backward compatibility.
   6   *
   7   * @package WordPress
   8   * @subpackage Upgrader
   9   * @since 2.8.0
  10   */
  11  
  12  /** WP_Upgrader_Skin class */
  13  require_once  ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
  14  
  15  /** Plugin_Upgrader_Skin class */
  16  require_once  ABSPATH . 'wp-admin/includes/class-plugin-upgrader-skin.php';
  17  
  18  /** Theme_Upgrader_Skin class */
  19  require_once  ABSPATH . 'wp-admin/includes/class-theme-upgrader-skin.php';
  20  
  21  /** Bulk_Upgrader_Skin class */
  22  require_once  ABSPATH . 'wp-admin/includes/class-bulk-upgrader-skin.php';
  23  
  24  /** Bulk_Plugin_Upgrader_Skin class */
  25  require_once  ABSPATH . 'wp-admin/includes/class-bulk-plugin-upgrader-skin.php';
  26  
  27  /** Bulk_Theme_Upgrader_Skin class */
  28  require_once  ABSPATH . 'wp-admin/includes/class-bulk-theme-upgrader-skin.php';
  29  
  30  /** Plugin_Installer_Skin class */
  31  require_once  ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
  32  
  33  /** Theme_Installer_Skin class */
  34  require_once  ABSPATH . 'wp-admin/includes/class-theme-installer-skin.php';
  35  
  36  /** Language_Pack_Upgrader_Skin class */
  37  require_once  ABSPATH . 'wp-admin/includes/class-language-pack-upgrader-skin.php';
  38  
  39  /** Automatic_Upgrader_Skin class */
  40  require_once  ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
  41  
  42  /** WP_Ajax_Upgrader_Skin class */
  43  require_once  ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
  44  
  45  /**
  46   * Core class used for upgrading/installing a local set of files via
  47   * the Filesystem Abstraction classes from a Zip file.
  48   *
  49   * @since 2.8.0
  50   */
  51  class WP_Upgrader {
  52  
  53      /**
  54       * The error/notification strings used to update the user on the progress.
  55       *
  56       * @since 2.8.0
  57       * @var array $strings
  58       */
  59      public $strings = array();
  60  
  61      /**
  62       * The upgrader skin being used.
  63       *
  64       * @since 2.8.0
  65       * @var Automatic_Upgrader_Skin|WP_Upgrader_Skin $skin
  66       */
  67      public $skin = null;
  68  
  69      /**
  70       * The result of the installation.
  71       *
  72       * This is set by WP_Upgrader::install_package(), only when the package is installed
  73       * successfully. It will then be an array, unless a WP_Error is returned by the
  74       * {@see 'upgrader_post_install'} filter. In that case, the WP_Error will be assigned to
  75       * it.
  76       *
  77       * @since 2.8.0
  78       *
  79       * @var array|WP_Error $result {
  80       *     @type string $source             The full path to the source the files were installed from.
  81       *     @type string $source_files       List of all the files in the source directory.
  82       *     @type string $destination        The full path to the installation destination folder.
  83       *     @type string $destination_name   The name of the destination folder, or empty if `$destination`
  84       *                                      and `$local_destination` are the same.
  85       *     @type string $local_destination  The full local path to the destination folder. This is usually
  86       *                                      the same as `$destination`.
  87       *     @type string $remote_destination The full remote path to the destination folder
  88       *                                      (i.e., from `$wp_filesystem`).
  89       *     @type bool   $clear_destination  Whether the destination folder was cleared.
  90       * }
  91       */
  92      public $result = array();
  93  
  94      /**
  95       * The total number of updates being performed.
  96       *
  97       * Set by the bulk update methods.
  98       *
  99       * @since 3.0.0
 100       * @var int $update_count
 101       */
 102      public $update_count = 0;
 103  
 104      /**
 105       * The current update if multiple updates are being performed.
 106       *
 107       * Used by the bulk update methods, and incremented for each update.
 108       *
 109       * @since 3.0.0
 110       * @var int
 111       */
 112      public $update_current = 0;
 113  
 114      /**
 115       * Construct the upgrader with a skin.
 116       *
 117       * @since 2.8.0
 118       *
 119       * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a WP_Upgrader_Skin
 120       *                               instance.
 121       */
 122  	public function __construct( $skin = null ) {
 123          if ( null === $skin ) {
 124              $this->skin = new WP_Upgrader_Skin();
 125          } else {
 126              $this->skin = $skin;
 127          }
 128      }
 129  
 130      /**
 131       * Initialize the upgrader.
 132       *
 133       * This will set the relationship between the skin being used and this upgrader,
 134       * and also add the generic strings to `WP_Upgrader::$strings`.
 135       *
 136       * @since 2.8.0
 137       */
 138  	public function init() {
 139          $this->skin->set_upgrader( $this );
 140          $this->generic_strings();
 141      }
 142  
 143      /**
 144       * Add the generic strings to WP_Upgrader::$strings.
 145       *
 146       * @since 2.8.0
 147       */
 148  	public function generic_strings() {
 149          $this->strings['bad_request']       = __( 'Invalid data provided.' );
 150          $this->strings['fs_unavailable']    = __( 'Could not access filesystem.' );
 151          $this->strings['fs_error']          = __( 'Filesystem error.' );
 152          $this->strings['fs_no_root_dir']    = __( 'Unable to locate WordPress root directory.' );
 153          $this->strings['fs_no_content_dir'] = __( 'Unable to locate WordPress content directory (wp-content).' );
 154          $this->strings['fs_no_plugins_dir'] = __( 'Unable to locate WordPress plugin directory.' );
 155          $this->strings['fs_no_themes_dir']  = __( 'Unable to locate WordPress theme directory.' );
 156          /* translators: %s: Directory name. */
 157          $this->strings['fs_no_folder'] = __( 'Unable to locate needed folder (%s).' );
 158  
 159          $this->strings['download_failed']      = __( 'Download failed.' );
 160          $this->strings['installing_package']   = __( 'Installing the latest version&#8230;' );
 161          $this->strings['no_files']             = __( 'The package contains no files.' );
 162          $this->strings['folder_exists']        = __( 'Destination folder already exists.' );
 163          $this->strings['mkdir_failed']         = __( 'Could not create directory.' );
 164          $this->strings['incompatible_archive'] = __( 'The package could not be installed.' );
 165          $this->strings['files_not_writable']   = __( 'The update cannot be installed because some files could not be copied. This is usually due to inconsistent file permissions.' );
 166  
 167          $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode&#8230;' );
 168          $this->strings['maintenance_end']   = __( 'Disabling Maintenance mode&#8230;' );
 169      }
 170  
 171      /**
 172       * Connect to the filesystem.
 173       *
 174       * @since 2.8.0
 175       *
 176       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 177       *
 178       * @param string[] $directories                  Optional. Array of directories. If any of these do
 179       *                                               not exist, a WP_Error object will be returned.
 180       *                                               Default empty array.
 181       * @param bool     $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
 182       *                                               Default false.
 183       * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise.
 184       */
 185  	public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
 186          global $wp_filesystem;
 187  
 188          $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership );
 189          if ( false === $credentials ) {
 190              return false;
 191          }
 192  
 193          if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
 194              $error = true;
 195              if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
 196                  $error = $wp_filesystem->errors;
 197              }
 198              // Failed to connect. Error and request again.
 199              $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
 200              return false;
 201          }
 202  
 203          if ( ! is_object( $wp_filesystem ) ) {
 204              return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
 205          }
 206  
 207          if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
 208              return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
 209          }
 210  
 211          foreach ( (array) $directories as $dir ) {
 212              switch ( $dir ) {
 213                  case ABSPATH:
 214                      if ( ! $wp_filesystem->abspath() ) {
 215                          return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] );
 216                      }
 217                      break;
 218                  case WP_CONTENT_DIR:
 219                      if ( ! $wp_filesystem->wp_content_dir() ) {
 220                          return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
 221                      }
 222                      break;
 223                  case WP_PLUGIN_DIR:
 224                      if ( ! $wp_filesystem->wp_plugins_dir() ) {
 225                          return new WP_Error( 'fs_no_plugins_dir', $this->strings['fs_no_plugins_dir'] );
 226                      }
 227                      break;
 228                  case get_theme_root():
 229                      if ( ! $wp_filesystem->wp_themes_dir() ) {
 230                          return new WP_Error( 'fs_no_themes_dir', $this->strings['fs_no_themes_dir'] );
 231                      }
 232                      break;
 233                  default:
 234                      if ( ! $wp_filesystem->find_folder( $dir ) ) {
 235                          return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
 236                      }
 237                      break;
 238              }
 239          }
 240          return true;
 241      }
 242  
 243      /**
 244       * Download a package.
 245       *
 246       * @since 2.8.0
 247       * @since 5.2.0 Added the `$check_signatures` parameter.
 248       * @since 5.5.0 Added the `$hook_extra` parameter.
 249       *
 250       * @param string $package          The URI of the package. If this is the full path to an
 251       *                                 existing local file, it will be returned untouched.
 252       * @param bool   $check_signatures Whether to validate file signatures. Default false.
 253       * @param array  $hook_extra       Extra arguments to pass to the filter hooks. Default empty array.
 254       * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
 255       */
 256  	public function download_package( $package, $check_signatures = false, $hook_extra = array() ) {
 257          /**
 258           * Filters whether to return the package.
 259           *
 260           * @since 3.7.0
 261           * @since 5.5.0 Added the `$hook_extra` parameter.
 262           *
 263           * @param bool        $reply      Whether to bail without returning the package.
 264           *                                Default false.
 265           * @param string      $package    The package file name.
 266           * @param WP_Upgrader $upgrader   The WP_Upgrader instance.
 267           * @param array       $hook_extra Extra arguments passed to hooked filters.
 268           */
 269          $reply = apply_filters( 'upgrader_pre_download', false, $package, $this, $hook_extra );
 270          if ( false !== $reply ) {
 271              return $reply;
 272          }
 273  
 274          if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote?
 275              return $package; // Must be a local file.
 276          }
 277  
 278          if ( empty( $package ) ) {
 279              return new WP_Error( 'no_package', $this->strings['no_package'] );
 280          }
 281  
 282          $this->skin->feedback( 'downloading_package', $package );
 283  
 284          $download_file = download_url( $package, 300, $check_signatures );
 285  
 286          if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
 287              return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
 288          }
 289  
 290          return $download_file;
 291      }
 292  
 293      /**
 294       * Unpack a compressed package file.
 295       *
 296       * @since 2.8.0
 297       *
 298       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 299       *
 300       * @param string $package        Full path to the package file.
 301       * @param bool   $delete_package Optional. Whether to delete the package file after attempting
 302       *                               to unpack it. Default true.
 303       * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
 304       */
 305  	public function unpack_package( $package, $delete_package = true ) {
 306          global $wp_filesystem;
 307  
 308          $this->skin->feedback( 'unpack_package' );
 309  
 310          $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
 311  
 312          // Clean up contents of upgrade directory beforehand.
 313          $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
 314          if ( ! empty( $upgrade_files ) ) {
 315              foreach ( $upgrade_files as $file ) {
 316                  $wp_filesystem->delete( $upgrade_folder . $file['name'], true );
 317              }
 318          }
 319  
 320          // We need a working directory - strip off any .tmp or .zip suffixes.
 321          $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
 322  
 323          // Clean up working directory.
 324          if ( $wp_filesystem->is_dir( $working_dir ) ) {
 325              $wp_filesystem->delete( $working_dir, true );
 326          }
 327  
 328          // Unzip package to working directory.
 329          $result = unzip_file( $package, $working_dir );
 330  
 331          // Once extracted, delete the package if required.
 332          if ( $delete_package ) {
 333              unlink( $package );
 334          }
 335  
 336          if ( is_wp_error( $result ) ) {
 337              $wp_filesystem->delete( $working_dir, true );
 338              if ( 'incompatible_archive' === $result->get_error_code() ) {
 339                  return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
 340              }
 341              return $result;
 342          }
 343  
 344          return $working_dir;
 345      }
 346  
 347      /**
 348       * Flatten the results of WP_Filesystem_Base::dirlist() for iterating over.
 349       *
 350       * @since 4.9.0
 351       * @access protected
 352       *
 353       * @param array  $nested_files Array of files as returned by WP_Filesystem_Base::dirlist().
 354       * @param string $path         Relative path to prepend to child nodes. Optional.
 355       * @return array A flattened array of the $nested_files specified.
 356       */
 357  	protected function flatten_dirlist( $nested_files, $path = '' ) {
 358          $files = array();
 359  
 360          foreach ( $nested_files as $name => $details ) {
 361              $files[ $path . $name ] = $details;
 362  
 363              // Append children recursively.
 364              if ( ! empty( $details['files'] ) ) {
 365                  $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' );
 366  
 367                  // Merge keeping possible numeric keys, which array_merge() will reindex from 0..n.
 368                  $files = $files + $children;
 369              }
 370          }
 371  
 372          return $files;
 373      }
 374  
 375      /**
 376       * Clears the directory where this item is going to be installed into.
 377       *
 378       * @since 4.3.0
 379       *
 380       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 381       *
 382       * @param string $remote_destination The location on the remote filesystem to be cleared.
 383       * @return true|WP_Error True upon success, WP_Error on failure.
 384       */
 385  	public function clear_destination( $remote_destination ) {
 386          global $wp_filesystem;
 387  
 388          $files = $wp_filesystem->dirlist( $remote_destination, true, true );
 389  
 390          // False indicates that the $remote_destination doesn't exist.
 391          if ( false === $files ) {
 392              return true;
 393          }
 394  
 395          // Flatten the file list to iterate over.
 396          $files = $this->flatten_dirlist( $files );
 397  
 398          // Check all files are writable before attempting to clear the destination.
 399          $unwritable_files = array();
 400  
 401          // Check writability.
 402          foreach ( $files as $filename => $file_details ) {
 403              if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
 404                  // Attempt to alter permissions to allow writes and try again.
 405                  $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' === $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
 406                  if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
 407                      $unwritable_files[] = $filename;
 408                  }
 409              }
 410          }
 411  
 412          if ( ! empty( $unwritable_files ) ) {
 413              return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
 414          }
 415  
 416          if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
 417              return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
 418          }
 419  
 420          return true;
 421      }
 422  
 423      /**
 424       * Install a package.
 425       *
 426       * Copies the contents of a package from a source directory, and installs them in
 427       * a destination directory. Optionally removes the source. It can also optionally
 428       * clear out the destination folder if it already exists.
 429       *
 430       * @since 2.8.0
 431       *
 432       * @global WP_Filesystem_Base $wp_filesystem        WordPress filesystem subclass.
 433       * @global array              $wp_theme_directories
 434       *
 435       * @param array|string $args {
 436       *     Optional. Array or string of arguments for installing a package. Default empty array.
 437       *
 438       *     @type string $source                      Required path to the package source. Default empty.
 439       *     @type string $destination                 Required path to a folder to install the package in.
 440       *                                               Default empty.
 441       *     @type bool   $clear_destination           Whether to delete any files already in the destination
 442       *                                               folder. Default false.
 443       *     @type bool   $clear_working               Whether to delete the files from the working directory
 444       *                                               after copying them to the destination. Default false.
 445       *     @type bool   $abort_if_destination_exists Whether to abort the installation if
 446       *                                               the destination folder already exists. Default true.
 447       *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
 448       *                                               WP_Upgrader::install_package(). Default empty array.
 449       * }
 450       *
 451       * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
 452       */
 453  	public function install_package( $args = array() ) {
 454          global $wp_filesystem, $wp_theme_directories;
 455  
 456          $defaults = array(
 457              'source'                      => '', // Please always pass this.
 458              'destination'                 => '', // ...and this.
 459              'clear_destination'           => false,
 460              'clear_working'               => false,
 461              'abort_if_destination_exists' => true,
 462              'hook_extra'                  => array(),
 463          );
 464  
 465          $args = wp_parse_args( $args, $defaults );
 466  
 467          // These were previously extract()'d.
 468          $source            = $args['source'];
 469          $destination       = $args['destination'];
 470          $clear_destination = $args['clear_destination'];
 471  
 472          set_time_limit( 300 );
 473  
 474          if ( empty( $source ) || empty( $destination ) ) {
 475              return new WP_Error( 'bad_request', $this->strings['bad_request'] );
 476          }
 477          $this->skin->feedback( 'installing_package' );
 478  
 479          /**
 480           * Filters the installation response before the installation has started.
 481           *
 482           * Returning a value that could be evaluated as a `WP_Error` will effectively
 483           * short-circuit the installation, returning that value instead.
 484           *
 485           * @since 2.8.0
 486           *
 487           * @param bool|WP_Error $response   Installation response.
 488           * @param array         $hook_extra Extra arguments passed to hooked filters.
 489           */
 490          $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
 491  
 492          if ( is_wp_error( $res ) ) {
 493              return $res;
 494          }
 495  
 496          // Retain the original source and destinations.
 497          $remote_source     = $args['source'];
 498          $local_destination = $destination;
 499  
 500          $source_files       = array_keys( $wp_filesystem->dirlist( $remote_source ) );
 501          $remote_destination = $wp_filesystem->find_folder( $local_destination );
 502  
 503          // Locate which directory to copy to the new folder. This is based on the actual folder holding the files.
 504          if ( 1 === count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) {
 505              // Only one folder? Then we want its contents.
 506              $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
 507          } elseif ( 0 === count( $source_files ) ) {
 508              // There are no files?
 509              return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] );
 510          } else {
 511              // It's only a single file, the upgrader will use the folder name of this file as the destination folder.
 512              // Folder name is based on zip filename.
 513              $source = trailingslashit( $args['source'] );
 514          }
 515  
 516          /**
 517           * Filters the source file location for the upgrade package.
 518           *
 519           * @since 2.8.0
 520           * @since 4.4.0 The $hook_extra parameter became available.
 521           *
 522           * @param string      $source        File source location.
 523           * @param string      $remote_source Remote file source location.
 524           * @param WP_Upgrader $upgrader      WP_Upgrader instance.
 525           * @param array       $hook_extra    Extra arguments passed to hooked filters.
 526           */
 527          $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
 528  
 529          if ( is_wp_error( $source ) ) {
 530              return $source;
 531          }
 532  
 533          // Has the source location changed? If so, we need a new source_files list.
 534          if ( $source !== $remote_source ) {
 535              $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
 536          }
 537  
 538          /*
 539           * Protection against deleting files in any important base directories.
 540           * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
 541           * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
 542           * to copy the directory into the directory, whilst they pass the source
 543           * as the actual files to copy.
 544           */
 545          $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
 546  
 547          if ( is_array( $wp_theme_directories ) ) {
 548              $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
 549          }
 550  
 551          if ( in_array( $destination, $protected_directories, true ) ) {
 552              $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
 553              $destination        = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
 554          }
 555  
 556          if ( $clear_destination ) {
 557              // We're going to clear the destination if there's something there.
 558              $this->skin->feedback( 'remove_old' );
 559  
 560              $removed = $this->clear_destination( $remote_destination );
 561  
 562              /**
 563               * Filters whether the upgrader cleared the destination.
 564               *
 565               * @since 2.8.0
 566               *
 567               * @param true|WP_Error $removed            Whether the destination was cleared.
 568               *                                          True upon success, WP_Error on failure.
 569               * @param string        $local_destination  The local package destination.
 570               * @param string        $remote_destination The remote package destination.
 571               * @param array         $hook_extra         Extra arguments passed to hooked filters.
 572               */
 573              $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
 574  
 575              if ( is_wp_error( $removed ) ) {
 576                  return $removed;
 577              }
 578          } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
 579              // If we're not clearing the destination folder and something exists there already, bail.
 580              // But first check to see if there are actually any files in the folder.
 581              $_files = $wp_filesystem->dirlist( $remote_destination );
 582              if ( ! empty( $_files ) ) {
 583                  $wp_filesystem->delete( $remote_source, true ); // Clear out the source files.
 584                  return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
 585              }
 586          }
 587  
 588          // Create destination if needed.
 589          if ( ! $wp_filesystem->exists( $remote_destination ) ) {
 590              if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
 591                  return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
 592              }
 593          }
 594  
 595          // Copy new version of item into place.
 596          if ( class_exists( 'Rollback_Update_Failure\WP_Upgrader' )
 597              && function_exists( '\Rollback_Update_Failure\move_dir' )
 598          ) {
 599              /*
 600               * If the {@link https://wordpress.org/plugins/rollback-update-failure/ Rollback Update Failure}
 601               * feature plugin is installed, use the move_dir() function from there for better performance.
 602               * Instead of copying a directory from one location to another, it uses the rename() PHP function
 603               * to speed up the process. If the renaming failed, it falls back to copy_dir().
 604               *
 605               * This condition aims to facilitate broader testing of the Rollbacks (temp backups) feature project.
 606               * It is temporary, until the plugin is merged into core.
 607               */
 608              $result = \Rollback_Update_Failure\move_dir( $source, $remote_destination );
 609          } else {
 610              $result = copy_dir( $source, $remote_destination );
 611          }
 612  
 613          if ( is_wp_error( $result ) ) {
 614              if ( $args['clear_working'] ) {
 615                  $wp_filesystem->delete( $remote_source, true );
 616              }
 617              return $result;
 618          }
 619  
 620          // Clear the working folder?
 621          if ( $args['clear_working'] ) {
 622              $wp_filesystem->delete( $remote_source, true );
 623          }
 624  
 625          $destination_name = basename( str_replace( $local_destination, '', $destination ) );
 626          if ( '.' === $destination_name ) {
 627              $destination_name = '';
 628          }
 629  
 630          $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
 631  
 632          /**
 633           * Filters the installation response after the installation has finished.
 634           *
 635           * @since 2.8.0
 636           *
 637           * @param bool  $response   Installation response.
 638           * @param array $hook_extra Extra arguments passed to hooked filters.
 639           * @param array $result     Installation result data.
 640           */
 641          $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
 642  
 643          if ( is_wp_error( $res ) ) {
 644              $this->result = $res;
 645              return $res;
 646          }
 647  
 648          // Bombard the calling function will all the info which we've just used.
 649          return $this->result;
 650      }
 651  
 652      /**
 653       * Run an upgrade/installation.
 654       *
 655       * Attempts to download the package (if it is not a local file), unpack it, and
 656       * install it in the destination folder.
 657       *
 658       * @since 2.8.0
 659       *
 660       * @param array $options {
 661       *     Array or string of arguments for upgrading/installing a package.
 662       *
 663       *     @type string $package                     The full path or URI of the package to install.
 664       *                                               Default empty.
 665       *     @type string $destination                 The full path to the destination folder.
 666       *                                               Default empty.
 667       *     @type bool   $clear_destination           Whether to delete any files already in the
 668       *                                               destination folder. Default false.
 669       *     @type bool   $clear_working               Whether to delete the files from the working
 670       *                                               directory after copying them to the destination.
 671       *                                               Default true.
 672       *     @type bool   $abort_if_destination_exists Whether to abort the installation if the destination
 673       *                                               folder already exists. When true, `$clear_destination`
 674       *                                               should be false. Default true.
 675       *     @type bool   $is_multi                    Whether this run is one of multiple upgrade/installation
 676       *                                               actions being performed in bulk. When true, the skin
 677       *                                               WP_Upgrader::header() and WP_Upgrader::footer()
 678       *                                               aren't called. Default false.
 679       *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
 680       *                                               WP_Upgrader::run().
 681       * }
 682       * @return array|false|WP_Error The result from self::install_package() on success, otherwise a WP_Error,
 683       *                              or false if unable to connect to the filesystem.
 684       */
 685  	public function run( $options ) {
 686  
 687          $defaults = array(
 688              'package'                     => '', // Please always pass this.
 689              'destination'                 => '', // ...and this.
 690              'clear_destination'           => false,
 691              'clear_working'               => true,
 692              'abort_if_destination_exists' => true, // Abort if the destination directory exists. Pass clear_destination as false please.
 693              'is_multi'                    => false,
 694              'hook_extra'                  => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
 695          );
 696  
 697          $options = wp_parse_args( $options, $defaults );
 698  
 699          /**
 700           * Filters the package options before running an update.
 701           *
 702           * See also {@see 'upgrader_process_complete'}.
 703           *
 704           * @since 4.3.0
 705           *
 706           * @param array $options {
 707           *     Options used by the upgrader.
 708           *
 709           *     @type string $package                     Package for update.
 710           *     @type string $destination                 Update location.
 711           *     @type bool   $clear_destination           Clear the destination resource.
 712           *     @type bool   $clear_working               Clear the working resource.
 713           *     @type bool   $abort_if_destination_exists Abort if the Destination directory exists.
 714           *     @type bool   $is_multi                    Whether the upgrader is running multiple times.
 715           *     @type array  $hook_extra {
 716           *         Extra hook arguments.
 717           *
 718           *         @type string $action               Type of action. Default 'update'.
 719           *         @type string $type                 Type of update process. Accepts 'plugin', 'theme', or 'core'.
 720           *         @type bool   $bulk                 Whether the update process is a bulk update. Default true.
 721           *         @type string $plugin               Path to the plugin file relative to the plugins directory.
 722           *         @type string $theme                The stylesheet or template name of the theme.
 723           *         @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
 724           *                                            or 'core'.
 725           *         @type object $language_update      The language pack update offer.
 726           *     }
 727           * }
 728           */
 729          $options = apply_filters( 'upgrader_package_options', $options );
 730  
 731          if ( ! $options['is_multi'] ) { // Call $this->header separately if running multiple times.
 732              $this->skin->header();
 733          }
 734  
 735          // Connect to the filesystem first.
 736          $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
 737          // Mainly for non-connected filesystem.
 738          if ( ! $res ) {
 739              if ( ! $options['is_multi'] ) {
 740                  $this->skin->footer();
 741              }
 742              return false;
 743          }
 744  
 745          $this->skin->before();
 746  
 747          if ( is_wp_error( $res ) ) {
 748              $this->skin->error( $res );
 749              $this->skin->after();
 750              if ( ! $options['is_multi'] ) {
 751                  $this->skin->footer();
 752              }
 753              return $res;
 754          }
 755  
 756          /*
 757           * Download the package. Note: If the package is the full path
 758           * to an existing local file, it will be returned untouched.
 759           */
 760          $download = $this->download_package( $options['package'], true, $options['hook_extra'] );
 761  
 762          // Allow for signature soft-fail.
 763          // WARNING: This may be removed in the future.
 764          if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
 765  
 766              // Don't output the 'no signature could be found' failure message for now.
 767              if ( 'signature_verification_no_signature' !== $download->get_error_code() || WP_DEBUG ) {
 768                  // Output the failure error as a normal feedback, and not as an error.
 769                  $this->skin->feedback( $download->get_error_message() );
 770  
 771                  // Report this failure back to WordPress.org for debugging purposes.
 772                  wp_version_check(
 773                      array(
 774                          'signature_failure_code' => $download->get_error_code(),
 775                          'signature_failure_data' => $download->get_error_data(),
 776                      )
 777                  );
 778              }
 779  
 780              // Pretend this error didn't happen.
 781              $download = $download->get_error_data( 'softfail-filename' );
 782          }
 783  
 784          if ( is_wp_error( $download ) ) {
 785              $this->skin->error( $download );
 786              $this->skin->after();
 787              if ( ! $options['is_multi'] ) {
 788                  $this->skin->footer();
 789              }
 790              return $download;
 791          }
 792  
 793          $delete_package = ( $download !== $options['package'] ); // Do not delete a "local" file.
 794  
 795          // Unzips the file into a temporary directory.
 796          $working_dir = $this->unpack_package( $download, $delete_package );
 797          if ( is_wp_error( $working_dir ) ) {
 798              $this->skin->error( $working_dir );
 799              $this->skin->after();
 800              if ( ! $options['is_multi'] ) {
 801                  $this->skin->footer();
 802              }
 803              return $working_dir;
 804          }
 805  
 806          // With the given options, this installs it to the destination directory.
 807          $result = $this->install_package(
 808              array(
 809                  'source'                      => $working_dir,
 810                  'destination'                 => $options['destination'],
 811                  'clear_destination'           => $options['clear_destination'],
 812                  'abort_if_destination_exists' => $options['abort_if_destination_exists'],
 813                  'clear_working'               => $options['clear_working'],
 814                  'hook_extra'                  => $options['hook_extra'],
 815              )
 816          );
 817  
 818          /**
 819           * Filters the result of WP_Upgrader::install_package().
 820           *
 821           * @since 5.7.0
 822           *
 823           * @param array|WP_Error $result     Result from WP_Upgrader::install_package().
 824           * @param array          $hook_extra Extra arguments passed to hooked filters.
 825           */
 826          $result = apply_filters( 'upgrader_install_package_result', $result, $options['hook_extra'] );
 827  
 828          $this->skin->set_result( $result );
 829          if ( is_wp_error( $result ) ) {
 830              $this->skin->error( $result );
 831  
 832              if ( ! method_exists( $this->skin, 'hide_process_failed' ) || ! $this->skin->hide_process_failed( $result ) ) {
 833                  $this->skin->feedback( 'process_failed' );
 834              }
 835          } else {
 836              // Installation succeeded.
 837              $this->skin->feedback( 'process_success' );
 838          }
 839  
 840          $this->skin->after();
 841  
 842          if ( ! $options['is_multi'] ) {
 843  
 844              /**
 845               * Fires when the upgrader process is complete.
 846               *
 847               * See also {@see 'upgrader_package_options'}.
 848               *
 849               * @since 3.6.0
 850               * @since 3.7.0 Added to WP_Upgrader::run().
 851               * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
 852               *
 853               * @param WP_Upgrader $upgrader   WP_Upgrader instance. In other contexts this might be a
 854               *                                Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
 855               * @param array       $hook_extra {
 856               *     Array of bulk item update data.
 857               *
 858               *     @type string $action       Type of action. Default 'update'.
 859               *     @type string $type         Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
 860               *     @type bool   $bulk         Whether the update process is a bulk update. Default true.
 861               *     @type array  $plugins      Array of the basename paths of the plugins' main files.
 862               *     @type array  $themes       The theme slugs.
 863               *     @type array  $translations {
 864               *         Array of translations update data.
 865               *
 866               *         @type string $language The locale the translation is for.
 867               *         @type string $type     Type of translation. Accepts 'plugin', 'theme', or 'core'.
 868               *         @type string $slug     Text domain the translation is for. The slug of a theme/plugin or
 869               *                                'default' for core translations.
 870               *         @type string $version  The version of a theme, plugin, or core.
 871               *     }
 872               * }
 873               */
 874              do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
 875  
 876              $this->skin->footer();
 877          }
 878  
 879          return $result;
 880      }
 881  
 882      /**
 883       * Toggle maintenance mode for the site.
 884       *
 885       * Creates/deletes the maintenance file to enable/disable maintenance mode.
 886       *
 887       * @since 2.8.0
 888       *
 889       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 890       *
 891       * @param bool $enable True to enable maintenance mode, false to disable.
 892       */
 893  	public function maintenance_mode( $enable = false ) {
 894          global $wp_filesystem;
 895          $file = $wp_filesystem->abspath() . '.maintenance';
 896          if ( $enable ) {
 897              $this->skin->feedback( 'maintenance_start' );
 898              // Create maintenance file to signal that we are upgrading.
 899              $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
 900              $wp_filesystem->delete( $file );
 901              $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
 902          } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
 903              $this->skin->feedback( 'maintenance_end' );
 904              $wp_filesystem->delete( $file );
 905          }
 906      }
 907  
 908      /**
 909       * Creates a lock using WordPress options.
 910       *
 911       * @since 4.5.0
 912       *
 913       * @param string $lock_name       The name of this unique lock.
 914       * @param int    $release_timeout Optional. The duration in seconds to respect an existing lock.
 915       *                                Default: 1 hour.
 916       * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
 917       */
 918  	public static function create_lock( $lock_name, $release_timeout = null ) {
 919          global $wpdb;
 920          if ( ! $release_timeout ) {
 921              $release_timeout = HOUR_IN_SECONDS;
 922          }
 923          $lock_option = $lock_name . '.lock';
 924  
 925          // Try to lock.
 926          $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
 927  
 928          if ( ! $lock_result ) {
 929              $lock_result = get_option( $lock_option );
 930  
 931              // If a lock couldn't be created, and there isn't a lock, bail.
 932              if ( ! $lock_result ) {
 933                  return false;
 934              }
 935  
 936              // Check to see if the lock is still valid. If it is, bail.
 937              if ( $lock_result > ( time() - $release_timeout ) ) {
 938                  return false;
 939              }
 940  
 941              // There must exist an expired lock, clear it and re-gain it.
 942              WP_Upgrader::release_lock( $lock_name );
 943  
 944              return WP_Upgrader::create_lock( $lock_name, $release_timeout );
 945          }
 946  
 947          // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
 948          update_option( $lock_option, time() );
 949  
 950          return true;
 951      }
 952  
 953      /**
 954       * Releases an upgrader lock.
 955       *
 956       * @since 4.5.0
 957       *
 958       * @see WP_Upgrader::create_lock()
 959       *
 960       * @param string $lock_name The name of this unique lock.
 961       * @return bool True if the lock was successfully released. False on failure.
 962       */
 963  	public static function release_lock( $lock_name ) {
 964          return delete_option( $lock_name . '.lock' );
 965      }
 966  }
 967  
 968  /** Plugin_Upgrader class */
 969  require_once  ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
 970  
 971  /** Theme_Upgrader class */
 972  require_once  ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
 973  
 974  /** Language_Pack_Upgrader class */
 975  require_once  ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
 976  
 977  /** Core_Upgrader class */
 978  require_once  ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
 979  
 980  /** File_Upload_Upgrader class */
 981  require_once  ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
 982  
 983  /** WP_Automatic_Updater class */
 984  require_once  ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';


Generated : Fri Aug 19 08:20:02 2022 Cross-referenced by PHPXref