[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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 WP_Error|array $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 array $directories                  Optional. A list 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      } //end fs_connect();
 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 $files 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] . '/' ) ) { //Only one folder? Then we want its contents.
 502              $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
 503          } elseif ( count( $source_files ) == 0 ) {
 504              return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
 505          } else { // It's only a single file, the upgrader will use the folder name of this file as the destination folder. Folder name is based on zip filename.
 506              $source = trailingslashit( $args['source'] );
 507          }
 508  
 509          /**
 510           * Filters the source file location for the upgrade package.
 511           *
 512           * @since 2.8.0
 513           * @since 4.4.0 The $hook_extra parameter became available.
 514           *
 515           * @param string      $source        File source location.
 516           * @param string      $remote_source Remote file source location.
 517           * @param WP_Upgrader $this          WP_Upgrader instance.
 518           * @param array       $hook_extra    Extra arguments passed to hooked filters.
 519           */
 520          $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
 521  
 522          if ( is_wp_error( $source ) ) {
 523              return $source;
 524          }
 525  
 526          // Has the source location changed? If so, we need a new source_files list.
 527          if ( $source !== $remote_source ) {
 528              $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
 529          }
 530  
 531          /*
 532           * Protection against deleting files in any important base directories.
 533           * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
 534           * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
 535           * to copy the directory into the directory, whilst they pass the source
 536           * as the actual files to copy.
 537           */
 538          $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
 539  
 540          if ( is_array( $wp_theme_directories ) ) {
 541              $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
 542          }
 543  
 544          if ( in_array( $destination, $protected_directories ) ) {
 545              $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
 546              $destination        = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
 547          }
 548  
 549          if ( $clear_destination ) {
 550              // We're going to clear the destination if there's something there.
 551              $this->skin->feedback( 'remove_old' );
 552  
 553              $removed = $this->clear_destination( $remote_destination );
 554  
 555              /**
 556               * Filters whether the upgrader cleared the destination.
 557               *
 558               * @since 2.8.0
 559               *
 560               * @param mixed  $removed            Whether the destination was cleared. true on success, WP_Error on failure
 561               * @param string $local_destination  The local package destination.
 562               * @param string $remote_destination The remote package destination.
 563               * @param array  $hook_extra         Extra arguments passed to hooked filters.
 564               */
 565              $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
 566  
 567              if ( is_wp_error( $removed ) ) {
 568                  return $removed;
 569              }
 570          } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
 571              //If we're not clearing the destination folder and something exists there already, Bail.
 572              //But first check to see if there are actually any files in the folder.
 573              $_files = $wp_filesystem->dirlist( $remote_destination );
 574              if ( ! empty( $_files ) ) {
 575                  $wp_filesystem->delete( $remote_source, true ); //Clear out the source files.
 576                  return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
 577              }
 578          }
 579  
 580          //Create destination if needed
 581          if ( ! $wp_filesystem->exists( $remote_destination ) ) {
 582              if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
 583                  return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
 584              }
 585          }
 586          // Copy new version of item into place.
 587          $result = copy_dir( $source, $remote_destination );
 588          if ( is_wp_error( $result ) ) {
 589              if ( $args['clear_working'] ) {
 590                  $wp_filesystem->delete( $remote_source, true );
 591              }
 592              return $result;
 593          }
 594  
 595          //Clear the Working folder?
 596          if ( $args['clear_working'] ) {
 597              $wp_filesystem->delete( $remote_source, true );
 598          }
 599  
 600          $destination_name = basename( str_replace( $local_destination, '', $destination ) );
 601          if ( '.' == $destination_name ) {
 602              $destination_name = '';
 603          }
 604  
 605          $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
 606  
 607          /**
 608           * Filters the installation response after the installation has finished.
 609           *
 610           * @since 2.8.0
 611           *
 612           * @param bool  $response   Installation response.
 613           * @param array $hook_extra Extra arguments passed to hooked filters.
 614           * @param array $result     Installation result data.
 615           */
 616          $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
 617  
 618          if ( is_wp_error( $res ) ) {
 619              $this->result = $res;
 620              return $res;
 621          }
 622  
 623          //Bombard the calling function will all the info which we've just used.
 624          return $this->result;
 625      }
 626  
 627      /**
 628       * Run an upgrade/installation.
 629       *
 630       * Attempts to download the package (if it is not a local file), unpack it, and
 631       * install it in the destination folder.
 632       *
 633       * @since 2.8.0
 634       *
 635       * @param array $options {
 636       *     Array or string of arguments for upgrading/installing a package.
 637       *
 638       *     @type string $package                     The full path or URI of the package to install.
 639       *                                               Default empty.
 640       *     @type string $destination                 The full path to the destination folder.
 641       *                                               Default empty.
 642       *     @type bool   $clear_destination           Whether to delete any files already in the
 643       *                                               destination folder. Default false.
 644       *     @type bool   $clear_working               Whether to delete the files form the working
 645       *                                               directory after copying to the destination.
 646       *                                               Default false.
 647       *     @type bool   $abort_if_destination_exists Whether to abort the installation if the destination
 648       *                                               folder already exists. When true, `$clear_destination`
 649       *                                               should be false. Default true.
 650       *     @type bool   $is_multi                    Whether this run is one of multiple upgrade/installation
 651       *                                               actions being performed in bulk. When true, the skin
 652       *                                               WP_Upgrader::header() and WP_Upgrader::footer()
 653       *                                               aren't called. Default false.
 654       *     @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by
 655       *                                               WP_Upgrader::run().
 656       * }
 657       * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
 658       *                              or false if unable to connect to the filesystem.
 659       */
 660  	public function run( $options ) {
 661  
 662          $defaults = array(
 663              'package'                     => '', // Please always pass this.
 664              'destination'                 => '', // And this
 665              'clear_destination'           => false,
 666              'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
 667              'clear_working'               => true,
 668              'is_multi'                    => false,
 669              'hook_extra'                  => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
 670          );
 671  
 672          $options = wp_parse_args( $options, $defaults );
 673  
 674          /**
 675           * Filters the package options before running an update.
 676           *
 677           * See also {@see 'upgrader_process_complete'}.
 678           *
 679           * @since 4.3.0
 680           *
 681           * @param array $options {
 682           *     Options used by the upgrader.
 683           *
 684           *     @type string $package                     Package for update.
 685           *     @type string $destination                 Update location.
 686           *     @type bool   $clear_destination           Clear the destination resource.
 687           *     @type bool   $clear_working               Clear the working resource.
 688           *     @type bool   $abort_if_destination_exists Abort if the Destination directory exists.
 689           *     @type bool   $is_multi                    Whether the upgrader is running multiple times.
 690           *     @type array  $hook_extra {
 691           *         Extra hook arguments.
 692           *
 693           *         @type string $action               Type of action. Default 'update'.
 694           *         @type string $type                 Type of update process. Accepts 'plugin', 'theme', or 'core'.
 695           *         @type bool   $bulk                 Whether the update process is a bulk update. Default true.
 696           *         @type string $plugin               Path to the plugin file relative to the plugins directory.
 697           *         @type string $theme                The stylesheet or template name of the theme.
 698           *         @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
 699           *                                            or 'core'.
 700           *         @type object $language_update      The language pack update offer.
 701           *     }
 702           * }
 703           */
 704          $options = apply_filters( 'upgrader_package_options', $options );
 705  
 706          if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
 707              $this->skin->header();
 708          }
 709  
 710          // Connect to the Filesystem first.
 711          $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
 712          // Mainly for non-connected filesystem.
 713          if ( ! $res ) {
 714              if ( ! $options['is_multi'] ) {
 715                  $this->skin->footer();
 716              }
 717              return false;
 718          }
 719  
 720          $this->skin->before();
 721  
 722          if ( is_wp_error( $res ) ) {
 723              $this->skin->error( $res );
 724              $this->skin->after();
 725              if ( ! $options['is_multi'] ) {
 726                  $this->skin->footer();
 727              }
 728              return $res;
 729          }
 730  
 731          /*
 732           * Download the package (Note, This just returns the filename
 733           * of the file if the package is a local file)
 734           */
 735          $download = $this->download_package( $options['package'], true );
 736  
 737          // Allow for signature soft-fail.
 738          // WARNING: This may be removed in the future.
 739          if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
 740  
 741              // Don't output the 'no signature could be found' failure message for now.
 742              if ( 'signature_verification_no_signature' != $download->get_error_code() || WP_DEBUG ) {
 743                  // Outout the failure error as a normal feedback, and not as an error:
 744                  $this->skin->feedback( $download->get_error_message() );
 745  
 746                  // Report this failure back to WordPress.org for debugging purposes.
 747                  wp_version_check(
 748                      array(
 749                          'signature_failure_code' => $download->get_error_code(),
 750                          'signature_failure_data' => $download->get_error_data(),
 751                      )
 752                  );
 753              }
 754  
 755              // Pretend this error didn't happen.
 756              $download = $download->get_error_data( 'softfail-filename' );
 757          }
 758  
 759          if ( is_wp_error( $download ) ) {
 760              $this->skin->error( $download );
 761              $this->skin->after();
 762              if ( ! $options['is_multi'] ) {
 763                  $this->skin->footer();
 764              }
 765              return $download;
 766          }
 767  
 768          $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
 769  
 770          // Unzips the file into a temporary directory.
 771          $working_dir = $this->unpack_package( $download, $delete_package );
 772          if ( is_wp_error( $working_dir ) ) {
 773              $this->skin->error( $working_dir );
 774              $this->skin->after();
 775              if ( ! $options['is_multi'] ) {
 776                  $this->skin->footer();
 777              }
 778              return $working_dir;
 779          }
 780  
 781          // With the given options, this installs it to the destination directory.
 782          $result = $this->install_package(
 783              array(
 784                  'source'                      => $working_dir,
 785                  'destination'                 => $options['destination'],
 786                  'clear_destination'           => $options['clear_destination'],
 787                  'abort_if_destination_exists' => $options['abort_if_destination_exists'],
 788                  'clear_working'               => $options['clear_working'],
 789                  'hook_extra'                  => $options['hook_extra'],
 790              )
 791          );
 792  
 793          $this->skin->set_result( $result );
 794          if ( is_wp_error( $result ) ) {
 795              $this->skin->error( $result );
 796              $this->skin->feedback( 'process_failed' );
 797          } else {
 798              // Installation succeeded.
 799              $this->skin->feedback( 'process_success' );
 800          }
 801  
 802          $this->skin->after();
 803  
 804          if ( ! $options['is_multi'] ) {
 805  
 806              /**
 807               * Fires when the upgrader process is complete.
 808               *
 809               * See also {@see 'upgrader_package_options'}.
 810               *
 811               * @since 3.6.0
 812               * @since 3.7.0 Added to WP_Upgrader::run().
 813               * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
 814               *
 815               * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
 816               *                          Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
 817               * @param array       $hook_extra {
 818               *     Array of bulk item update data.
 819               *
 820               *     @type string $action       Type of action. Default 'update'.
 821               *     @type string $type         Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
 822               *     @type bool   $bulk         Whether the update process is a bulk update. Default true.
 823               *     @type array  $plugins      Array of the basename paths of the plugins' main files.
 824               *     @type array  $themes       The theme slugs.
 825               *     @type array  $translations {
 826               *         Array of translations update data.
 827               *
 828               *         @type string $language The locale the translation is for.
 829               *         @type string $type     Type of translation. Accepts 'plugin', 'theme', or 'core'.
 830               *         @type string $slug     Text domain the translation is for. The slug of a theme/plugin or
 831               *                                'default' for core translations.
 832               *         @type string $version  The version of a theme, plugin, or core.
 833               *     }
 834               * }
 835               */
 836              do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
 837  
 838              $this->skin->footer();
 839          }
 840  
 841          return $result;
 842      }
 843  
 844      /**
 845       * Toggle maintenance mode for the site.
 846       *
 847       * Creates/deletes the maintenance file to enable/disable maintenance mode.
 848       *
 849       * @since 2.8.0
 850       *
 851       * @global WP_Filesystem_Base $wp_filesystem Subclass
 852       *
 853       * @param bool $enable True to enable maintenance mode, false to disable.
 854       */
 855  	public function maintenance_mode( $enable = false ) {
 856          global $wp_filesystem;
 857          $file = $wp_filesystem->abspath() . '.maintenance';
 858          if ( $enable ) {
 859              $this->skin->feedback( 'maintenance_start' );
 860              // Create maintenance file to signal that we are upgrading
 861              $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
 862              $wp_filesystem->delete( $file );
 863              $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
 864          } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
 865              $this->skin->feedback( 'maintenance_end' );
 866              $wp_filesystem->delete( $file );
 867          }
 868      }
 869  
 870      /**
 871       * Creates a lock using WordPress options.
 872       *
 873       * @since 4.5.0
 874       *
 875       * @param string $lock_name       The name of this unique lock.
 876       * @param int    $release_timeout Optional. The duration in seconds to respect an existing lock.
 877       *                                Default: 1 hour.
 878       * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
 879       */
 880  	public static function create_lock( $lock_name, $release_timeout = null ) {
 881          global $wpdb;
 882          if ( ! $release_timeout ) {
 883              $release_timeout = HOUR_IN_SECONDS;
 884          }
 885          $lock_option = $lock_name . '.lock';
 886  
 887          // Try to lock.
 888          $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
 889  
 890          if ( ! $lock_result ) {
 891              $lock_result = get_option( $lock_option );
 892  
 893              // If a lock couldn't be created, and there isn't a lock, bail.
 894              if ( ! $lock_result ) {
 895                  return false;
 896              }
 897  
 898              // Check to see if the lock is still valid. If it is, bail.
 899              if ( $lock_result > ( time() - $release_timeout ) ) {
 900                  return false;
 901              }
 902  
 903              // There must exist an expired lock, clear it and re-gain it.
 904              WP_Upgrader::release_lock( $lock_name );
 905  
 906              return WP_Upgrader::create_lock( $lock_name, $release_timeout );
 907          }
 908  
 909          // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
 910          update_option( $lock_option, time() );
 911  
 912          return true;
 913      }
 914  
 915      /**
 916       * Releases an upgrader lock.
 917       *
 918       * @since 4.5.0
 919       *
 920       * @see WP_Upgrader::create_lock()
 921       *
 922       * @param string $lock_name The name of this unique lock.
 923       * @return bool True if the lock was successfully released. False on failure.
 924       */
 925  	public static function release_lock( $lock_name ) {
 926          return delete_option( $lock_name . '.lock' );
 927      }
 928  
 929  }
 930  
 931  /** Plugin_Upgrader class */
 932  require_once  ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
 933  
 934  /** Theme_Upgrader class */
 935  require_once  ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
 936  
 937  /** Language_Pack_Upgrader class */
 938  require_once  ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
 939  
 940  /** Core_Upgrader class */
 941  require_once  ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
 942  
 943  /** File_Upload_Upgrader class */
 944  require_once  ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
 945  
 946  /** WP_Automatic_Updater class */
 947  require_once  ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';


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