[ Index ]

PHP Cross Reference of WordPress

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 we will be unable to copy some files. 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       *
 248       * @param string $package          The URI of the package. If this is the full path to an
 249       *                                 existing local file, it will be returned untouched.
 250       * @param bool   $check_signatures Whether to validate file signatures. Default false.
 251       * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
 252       */
 253  	public function download_package( $package, $check_signatures = false ) {
 254  
 255          /**
 256           * Filters whether to return the package.
 257           *
 258           * @since 3.7.0
 259           *
 260           * @param bool        $reply   Whether to bail without returning the package.
 261           *                             Default false.
 262           * @param string      $package The package file name.
 263           * @param WP_Upgrader $this    The WP_Upgrader instance.
 264           */
 265          $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
 266          if ( false !== $reply ) {
 267              return $reply;
 268          }
 269  
 270          if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote?
 271              return $package; // Must be a local file.
 272          }
 273  
 274          if ( empty( $package ) ) {
 275              return new WP_Error( 'no_package', $this->strings['no_package'] );
 276          }
 277  
 278          $this->skin->feedback( 'downloading_package', $package );
 279  
 280          $download_file = download_url( $package, 300, $check_signatures );
 281  
 282          if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
 283              return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
 284          }
 285  
 286          return $download_file;
 287      }
 288  
 289      /**
 290       * Unpack a compressed package file.
 291       *
 292       * @since 2.8.0
 293       *
 294       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 295       *
 296       * @param string $package        Full path to the package file.
 297       * @param bool   $delete_package Optional. Whether to delete the package file after attempting
 298       *                               to unpack it. Default true.
 299       * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
 300       */
 301  	public function unpack_package( $package, $delete_package = true ) {
 302          global $wp_filesystem;
 303  
 304          $this->skin->feedback( 'unpack_package' );
 305  
 306          $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
 307  
 308          // Clean up contents of upgrade directory beforehand.
 309          $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
 310          if ( ! empty( $upgrade_files ) ) {
 311              foreach ( $upgrade_files as $file ) {
 312                  $wp_filesystem->delete( $upgrade_folder . $file['name'], true );
 313              }
 314          }
 315  
 316          // We need a working directory - strip off any .tmp or .zip suffixes.
 317          $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
 318  
 319          // Clean up working directory.
 320          if ( $wp_filesystem->is_dir( $working_dir ) ) {
 321              $wp_filesystem->delete( $working_dir, true );
 322          }
 323  
 324          // Unzip package to working directory.
 325          $result = unzip_file( $package, $working_dir );
 326  
 327          // Once extracted, delete the package if required.
 328          if ( $delete_package ) {
 329              unlink( $package );
 330          }
 331  
 332          if ( is_wp_error( $result ) ) {
 333              $wp_filesystem->delete( $working_dir, true );
 334              if ( 'incompatible_archive' == $result->get_error_code() ) {
 335                  return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
 336              }
 337              return $result;
 338          }
 339  
 340          return $working_dir;
 341      }
 342  
 343      /**
 344       * Flatten the results of WP_Filesystem::dirlist() for iterating over.
 345       *
 346       * @since 4.9.0
 347       * @access protected
 348       *
 349       * @param  array  $nested_files  Array of files as returned by WP_Filesystem::dirlist()
 350       * @param  string $path          Relative path to prepend to child nodes. Optional.
 351       * @return array A flattened array of the $nested_files specified.
 352       */
 353  	protected function flatten_dirlist( $nested_files, $path = '' ) {
 354          $files = array();
 355  
 356          foreach ( $nested_files as $name => $details ) {
 357              $files[ $path . $name ] = $details;
 358  
 359              // Append children recursively.
 360              if ( ! empty( $details['files'] ) ) {
 361                  $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' );
 362  
 363                  // Merge keeping possible numeric keys, which array_merge() will reindex from 0..n.
 364                  $files = $files + $children;
 365              }
 366          }
 367  
 368          return $files;
 369      }
 370  
 371      /**
 372       * Clears the directory where this item is going to be installed into.
 373       *
 374       * @since 4.3.0
 375       *
 376       * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 377       *
 378       * @param string $remote_destination The location on the remote filesystem to be cleared
 379       * @return bool|WP_Error True upon success, WP_Error on failure.
 380       */
 381  	public function clear_destination( $remote_destination ) {
 382          global $wp_filesystem;
 383  
 384          $files = $wp_filesystem->dirlist( $remote_destination, true, true );
 385  
 386          // False indicates that the $remote_destination doesn't exist.
 387          if ( false === $files ) {
 388              return true;
 389          }
 390  
 391          // Flatten the file list to iterate over.
 392          $files = $this->flatten_dirlist( $files );
 393  
 394          // Check all files are writable before attempting to clear the destination.
 395          $unwritable_files = array();
 396  
 397          // Check writability.
 398          foreach ( $files as $filename => $file_details ) {
 399              if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
 400                  // Attempt to alter permissions to allow writes and try again.
 401                  $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
 402                  if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
 403                      $unwritable_files[] = $filename;
 404                  }
 405              }
 406          }
 407  
 408          if ( ! empty( $unwritable_files ) ) {
 409              return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
 410          }
 411  
 412          if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
 413              return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
 414          }
 415  
 416          return true;
 417      }
 418  
 419      /**
 420       * Install a package.
 421       *
 422       * Copies the contents of a package form a source directory, and installs them in
 423       * a destination directory. Optionally removes the source. It can also optionally
 424       * clear out the destination folder if it already exists.
 425       *
 426       * @since 2.8.0
 427       *
 428       * @global WP_Filesystem_Base $wp_filesystem        WordPress filesystem subclass.
 429       * @global array              $wp_theme_directories
 430       *
 431       * @param array|string $args {
 432       *     Optional. Array or string of arguments for installing a package. Default empty array.
 433       *
 434       *     @type string $source                      Required path to the package source. Default empty.
 435       *     @type string $destination                 Required path to a folder to install the package in.
 436       *                                               Default empty.
 437       *     @type bool   $clear_destination           Whether to delete any files already in the destination
 438       *                                               folder. Default false.
 439       *     @type bool   $clear_working               Whether to delete the files form the working directory
 440       *                                               after copying to the destination. Default false.
 441       *     @type bool   $abort_if_destination_exists Whether to abort the installation if
 442       *                                               the destination folder already exists. Default true.
 443       *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
 444       *                                               WP_Upgrader::install_package(). Default empty array.
 445       * }
 446       *
 447       * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
 448       */
 449  	public function install_package( $args = array() ) {
 450          global $wp_filesystem, $wp_theme_directories;
 451  
 452          $defaults = array(
 453              'source'                      => '', // Please always pass this.
 454              'destination'                 => '', // ...and this.
 455              'clear_destination'           => false,
 456              'clear_working'               => false,
 457              'abort_if_destination_exists' => true,
 458              'hook_extra'                  => array(),
 459          );
 460  
 461          $args = wp_parse_args( $args, $defaults );
 462  
 463          // These were previously extract()'d.
 464          $source            = $args['source'];
 465          $destination       = $args['destination'];
 466          $clear_destination = $args['clear_destination'];
 467  
 468          set_time_limit( 300 );
 469  
 470          if ( empty( $source ) || empty( $destination ) ) {
 471              return new WP_Error( 'bad_request', $this->strings['bad_request'] );
 472          }
 473          $this->skin->feedback( 'installing_package' );
 474  
 475          /**
 476           * Filters the install response before the installation has started.
 477           *
 478           * Returning a truthy value, or one that could be evaluated as a WP_Error
 479           * will effectively short-circuit the installation, returning that value
 480           * instead.
 481           *
 482           * @since 2.8.0
 483           *
 484           * @param bool|WP_Error $response   Response.
 485           * @param array         $hook_extra Extra arguments passed to hooked filters.
 486           */
 487          $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
 488  
 489          if ( is_wp_error( $res ) ) {
 490              return $res;
 491          }
 492  
 493          // Retain the original source and destinations.
 494          $remote_source     = $args['source'];
 495          $local_destination = $destination;
 496  
 497          $source_files       = array_keys( $wp_filesystem->dirlist( $remote_source ) );
 498          $remote_destination = $wp_filesystem->find_folder( $local_destination );
 499  
 500          // Locate which directory to copy to the new folder. This is based on the actual folder holding the files.
 501          if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) {
 502              // Only one folder? Then we want its contents.
 503              $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
 504          } elseif ( count( $source_files ) == 0 ) {
 505              // There are no files?
 506              return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] );
 507          } else {
 508              // It's only a single file, the upgrader will use the folder name of this file as the destination folder.
 509              // Folder name is based on zip filename.
 510              $source = trailingslashit( $args['source'] );
 511          }
 512  
 513          /**
 514           * Filters the source file location for the upgrade package.
 515           *
 516           * @since 2.8.0
 517           * @since 4.4.0 The $hook_extra parameter became available.
 518           *
 519           * @param string      $source        File source location.
 520           * @param string      $remote_source Remote file source location.
 521           * @param WP_Upgrader $this          WP_Upgrader instance.
 522           * @param array       $hook_extra    Extra arguments passed to hooked filters.
 523           */
 524          $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
 525  
 526          if ( is_wp_error( $source ) ) {
 527              return $source;
 528          }
 529  
 530          // Has the source location changed? If so, we need a new source_files list.
 531          if ( $source !== $remote_source ) {
 532              $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
 533          }
 534  
 535          /*
 536           * Protection against deleting files in any important base directories.
 537           * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
 538           * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
 539           * to copy the directory into the directory, whilst they pass the source
 540           * as the actual files to copy.
 541           */
 542          $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
 543  
 544          if ( is_array( $wp_theme_directories ) ) {
 545              $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
 546          }
 547  
 548          if ( in_array( $destination, $protected_directories ) ) {
 549              $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
 550              $destination        = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
 551          }
 552  
 553          if ( $clear_destination ) {
 554              // We're going to clear the destination if there's something there.
 555              $this->skin->feedback( 'remove_old' );
 556  
 557              $removed = $this->clear_destination( $remote_destination );
 558  
 559              /**
 560               * Filters whether the upgrader cleared the destination.
 561               *
 562               * @since 2.8.0
 563               *
 564               * @param true|WP_Error $removed            Whether the destination was cleared. true upon success, WP_Error on failure.
 565               * @param string        $local_destination  The local package destination.
 566               * @param string        $remote_destination The remote package destination.
 567               * @param array         $hook_extra         Extra arguments passed to hooked filters.
 568               */
 569              $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
 570  
 571              if ( is_wp_error( $removed ) ) {
 572                  return $removed;
 573              }
 574          } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
 575              // If we're not clearing the destination folder and something exists there already, bail.
 576              // But first check to see if there are actually any files in the folder.
 577              $_files = $wp_filesystem->dirlist( $remote_destination );
 578              if ( ! empty( $_files ) ) {
 579                  $wp_filesystem->delete( $remote_source, true ); // Clear out the source files.
 580                  return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
 581              }
 582          }
 583  
 584          // Create destination if needed.
 585          if ( ! $wp_filesystem->exists( $remote_destination ) ) {
 586              if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
 587                  return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
 588              }
 589          }
 590  
 591          // Copy new version of item into place.
 592          $result = copy_dir( $source, $remote_destination );
 593          if ( is_wp_error( $result ) ) {
 594              if ( $args['clear_working'] ) {
 595                  $wp_filesystem->delete( $remote_source, true );
 596              }
 597              return $result;
 598          }
 599  
 600          // Clear the working folder?
 601          if ( $args['clear_working'] ) {
 602              $wp_filesystem->delete( $remote_source, true );
 603          }
 604  
 605          $destination_name = basename( str_replace( $local_destination, '', $destination ) );
 606          if ( '.' == $destination_name ) {
 607              $destination_name = '';
 608          }
 609  
 610          $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
 611  
 612          /**
 613           * Filters the installation response after the installation has finished.
 614           *
 615           * @since 2.8.0
 616           *
 617           * @param bool  $response   Installation response.
 618           * @param array $hook_extra Extra arguments passed to hooked filters.
 619           * @param array $result     Installation result data.
 620           */
 621          $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
 622  
 623          if ( is_wp_error( $res ) ) {
 624              $this->result = $res;
 625              return $res;
 626          }
 627  
 628          // Bombard the calling function will all the info which we've just used.
 629          return $this->result;
 630      }
 631  
 632      /**
 633       * Run an upgrade/installation.
 634       *
 635       * Attempts to download the package (if it is not a local file), unpack it, and
 636       * install it in the destination folder.
 637       *
 638       * @since 2.8.0
 639       *
 640       * @param array $options {
 641       *     Array or string of arguments for upgrading/installing a package.
 642       *
 643       *     @type string $package                     The full path or URI of the package to install.
 644       *                                               Default empty.
 645       *     @type string $destination                 The full path to the destination folder.
 646       *                                               Default empty.
 647       *     @type bool   $clear_destination           Whether to delete any files already in the
 648       *                                               destination folder. Default false.
 649       *     @type bool   $clear_working               Whether to delete the files form the working
 650       *                                               directory after copying to the destination.
 651       *                                               Default false.
 652       *     @type bool   $abort_if_destination_exists Whether to abort the installation if the destination
 653       *                                               folder already exists. When true, `$clear_destination`
 654       *                                               should be false. Default true.
 655       *     @type bool   $is_multi                    Whether this run is one of multiple upgrade/installation
 656       *                                               actions being performed in bulk. When true, the skin
 657       *                                               WP_Upgrader::header() and WP_Upgrader::footer()
 658       *                                               aren't called. Default false.
 659       *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
 660       *                                               WP_Upgrader::run().
 661       * }
 662       * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
 663       *                              or false if unable to connect to the filesystem.
 664       */
 665  	public function run( $options ) {
 666  
 667          $defaults = array(
 668              'package'                     => '', // Please always pass this.
 669              'destination'                 => '', // ...and this.
 670              'clear_destination'           => false,
 671              'abort_if_destination_exists' => true, // Abort if the destination directory exists. Pass clear_destination as false please.
 672              'clear_working'               => true,
 673              'is_multi'                    => false,
 674              'hook_extra'                  => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
 675          );
 676  
 677          $options = wp_parse_args( $options, $defaults );
 678  
 679          /**
 680           * Filters the package options before running an update.
 681           *
 682           * See also {@see 'upgrader_process_complete'}.
 683           *
 684           * @since 4.3.0
 685           *
 686           * @param array $options {
 687           *     Options used by the upgrader.
 688           *
 689           *     @type string $package                     Package for update.
 690           *     @type string $destination                 Update location.
 691           *     @type bool   $clear_destination           Clear the destination resource.
 692           *     @type bool   $clear_working               Clear the working resource.
 693           *     @type bool   $abort_if_destination_exists Abort if the Destination directory exists.
 694           *     @type bool   $is_multi                    Whether the upgrader is running multiple times.
 695           *     @type array  $hook_extra {
 696           *         Extra hook arguments.
 697           *
 698           *         @type string $action               Type of action. Default 'update'.
 699           *         @type string $type                 Type of update process. Accepts 'plugin', 'theme', or 'core'.
 700           *         @type bool   $bulk                 Whether the update process is a bulk update. Default true.
 701           *         @type string $plugin               Path to the plugin file relative to the plugins directory.
 702           *         @type string $theme                The stylesheet or template name of the theme.
 703           *         @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
 704           *                                            or 'core'.
 705           *         @type object $language_update      The language pack update offer.
 706           *     }
 707           * }
 708           */
 709          $options = apply_filters( 'upgrader_package_options', $options );
 710  
 711          if ( ! $options['is_multi'] ) { // Call $this->header separately if running multiple times.
 712              $this->skin->header();
 713          }
 714  
 715          // Connect to the filesystem first.
 716          $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
 717          // Mainly for non-connected filesystem.
 718          if ( ! $res ) {
 719              if ( ! $options['is_multi'] ) {
 720                  $this->skin->footer();
 721              }
 722              return false;
 723          }
 724  
 725          $this->skin->before();
 726  
 727          if ( is_wp_error( $res ) ) {
 728              $this->skin->error( $res );
 729              $this->skin->after();
 730              if ( ! $options['is_multi'] ) {
 731                  $this->skin->footer();
 732              }
 733              return $res;
 734          }
 735  
 736          /*
 737           * Download the package (Note, This just returns the filename
 738           * of the file if the package is a local file)
 739           */
 740          $download = $this->download_package( $options['package'], true );
 741  
 742          // Allow for signature soft-fail.
 743          // WARNING: This may be removed in the future.
 744          if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
 745  
 746              // Don't output the 'no signature could be found' failure message for now.
 747              if ( 'signature_verification_no_signature' != $download->get_error_code() || WP_DEBUG ) {
 748                  // Output the failure error as a normal feedback, and not as an error.
 749                  $this->skin->feedback( $download->get_error_message() );
 750  
 751                  // Report this failure back to WordPress.org for debugging purposes.
 752                  wp_version_check(
 753                      array(
 754                          'signature_failure_code' => $download->get_error_code(),
 755                          'signature_failure_data' => $download->get_error_data(),
 756                      )
 757                  );
 758              }
 759  
 760              // Pretend this error didn't happen.
 761              $download = $download->get_error_data( 'softfail-filename' );
 762          }
 763  
 764          if ( is_wp_error( $download ) ) {
 765              $this->skin->error( $download );
 766              $this->skin->after();
 767              if ( ! $options['is_multi'] ) {
 768                  $this->skin->footer();
 769              }
 770              return $download;
 771          }
 772  
 773          $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file.
 774  
 775          // Unzips the file into a temporary directory.
 776          $working_dir = $this->unpack_package( $download, $delete_package );
 777          if ( is_wp_error( $working_dir ) ) {
 778              $this->skin->error( $working_dir );
 779              $this->skin->after();
 780              if ( ! $options['is_multi'] ) {
 781                  $this->skin->footer();
 782              }
 783              return $working_dir;
 784          }
 785  
 786          // With the given options, this installs it to the destination directory.
 787          $result = $this->install_package(
 788              array(
 789                  'source'                      => $working_dir,
 790                  'destination'                 => $options['destination'],
 791                  'clear_destination'           => $options['clear_destination'],
 792                  'abort_if_destination_exists' => $options['abort_if_destination_exists'],
 793                  'clear_working'               => $options['clear_working'],
 794                  'hook_extra'                  => $options['hook_extra'],
 795              )
 796          );
 797  
 798          $this->skin->set_result( $result );
 799          if ( is_wp_error( $result ) ) {
 800              $this->skin->error( $result );
 801              $this->skin->feedback( 'process_failed' );
 802          } else {
 803              // Installation succeeded.
 804              $this->skin->feedback( 'process_success' );
 805          }
 806  
 807          $this->skin->after();
 808  
 809          if ( ! $options['is_multi'] ) {
 810  
 811              /**
 812               * Fires when the upgrader process is complete.
 813               *
 814               * See also {@see 'upgrader_package_options'}.
 815               *
 816               * @since 3.6.0
 817               * @since 3.7.0 Added to WP_Upgrader::run().
 818               * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
 819               *
 820               * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
 821               *                          Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
 822               * @param array       $hook_extra {
 823               *     Array of bulk item update data.
 824               *
 825               *     @type string $action       Type of action. Default 'update'.
 826               *     @type string $type         Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
 827               *     @type bool   $bulk         Whether the update process is a bulk update. Default true.
 828               *     @type array  $plugins      Array of the basename paths of the plugins' main files.
 829               *     @type array  $themes       The theme slugs.
 830               *     @type array  $translations {
 831               *         Array of translations update data.
 832               *
 833               *         @type string $language The locale the translation is for.
 834               *         @type string $type     Type of translation. Accepts 'plugin', 'theme', or 'core'.
 835               *         @type string $slug     Text domain the translation is for. The slug of a theme/plugin or
 836               *                                'default' for core translations.
 837               *         @type string $version  The version of a theme, plugin, or core.
 838               *     }
 839               * }
 840               */
 841              do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
 842  
 843              $this->skin->footer();
 844          }
 845  
 846          return $result;
 847      }
 848  
 849      /**
 850       * Toggle maintenance mode for the site.
 851       *
 852       * Creates/deletes the maintenance file to enable/disable maintenance mode.
 853       *
 854       * @since 2.8.0
 855       *
 856       * @global WP_Filesystem_Base $wp_filesystem Subclass
 857       *
 858       * @param bool $enable True to enable maintenance mode, false to disable.
 859       */
 860  	public function maintenance_mode( $enable = false ) {
 861          global $wp_filesystem;
 862          $file = $wp_filesystem->abspath() . '.maintenance';
 863          if ( $enable ) {
 864              $this->skin->feedback( 'maintenance_start' );
 865              // Create maintenance file to signal that we are upgrading.
 866              $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
 867              $wp_filesystem->delete( $file );
 868              $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
 869          } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
 870              $this->skin->feedback( 'maintenance_end' );
 871              $wp_filesystem->delete( $file );
 872          }
 873      }
 874  
 875      /**
 876       * Creates a lock using WordPress options.
 877       *
 878       * @since 4.5.0
 879       *
 880       * @param string $lock_name       The name of this unique lock.
 881       * @param int    $release_timeout Optional. The duration in seconds to respect an existing lock.
 882       *                                Default: 1 hour.
 883       * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
 884       */
 885  	public static function create_lock( $lock_name, $release_timeout = null ) {
 886          global $wpdb;
 887          if ( ! $release_timeout ) {
 888              $release_timeout = HOUR_IN_SECONDS;
 889          }
 890          $lock_option = $lock_name . '.lock';
 891  
 892          // Try to lock.
 893          $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
 894  
 895          if ( ! $lock_result ) {
 896              $lock_result = get_option( $lock_option );
 897  
 898              // If a lock couldn't be created, and there isn't a lock, bail.
 899              if ( ! $lock_result ) {
 900                  return false;
 901              }
 902  
 903              // Check to see if the lock is still valid. If it is, bail.
 904              if ( $lock_result > ( time() - $release_timeout ) ) {
 905                  return false;
 906              }
 907  
 908              // There must exist an expired lock, clear it and re-gain it.
 909              WP_Upgrader::release_lock( $lock_name );
 910  
 911              return WP_Upgrader::create_lock( $lock_name, $release_timeout );
 912          }
 913  
 914          // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
 915          update_option( $lock_option, time() );
 916  
 917          return true;
 918      }
 919  
 920      /**
 921       * Releases an upgrader lock.
 922       *
 923       * @since 4.5.0
 924       *
 925       * @see WP_Upgrader::create_lock()
 926       *
 927       * @param string $lock_name The name of this unique lock.
 928       * @return bool True if the lock was successfully released. False on failure.
 929       */
 930  	public static function release_lock( $lock_name ) {
 931          return delete_option( $lock_name . '.lock' );
 932      }
 933  
 934  }
 935  
 936  /** Plugin_Upgrader class */
 937  require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
 938  
 939  /** Theme_Upgrader class */
 940  require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
 941  
 942  /** Language_Pack_Upgrader class */
 943  require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
 944  
 945  /** Core_Upgrader class */
 946  require_once ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
 947  
 948  /** File_Upload_Upgrader class */
 949  require_once ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
 950  
 951  /** WP_Automatic_Updater class */
 952  require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';


Generated: Mon Mar 30 01:00:03 2020 Cross-referenced by PHPXref 0.7.1