Changeset 42343 for trunk/src/wp-admin/includes/class-wp-upgrader.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-upgrader.php
r42214 r42343 121 121 */ 122 122 public function __construct( $skin = null ) { 123 if ( null == $skin ) 123 if ( null == $skin ) { 124 124 $this->skin = new WP_Upgrader_Skin(); 125 else125 } else { 126 126 $this->skin = $skin; 127 } 127 128 } 128 129 … … 136 137 */ 137 138 public function init() { 138 $this->skin->set_upgrader( $this);139 $this->skin->set_upgrader( $this ); 139 140 $this->generic_strings(); 140 141 } … … 146 147 */ 147 148 public function generic_strings() { 148 $this->strings['bad_request'] = __('Invalid data provided.');149 $this->strings['fs_unavailable'] = __('Could not access filesystem.');150 $this->strings['fs_error'] = __('Filesystem error.');151 $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress root directory.');152 $this->strings['fs_no_content_dir'] = __( 'Unable to locate WordPress content directory (wp-content).');153 $this->strings['fs_no_plugins_dir'] = __( 'Unable to locate WordPress plugin directory.');154 $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress theme directory.');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.' ); 155 156 /* translators: %s: directory name */ 156 $this->strings['fs_no_folder'] = __( 'Unable to locate needed folder (%s).');157 158 $this->strings['download_failed'] = __('Download failed.');159 $this->strings['installing_package'] = __('Installing the latest version…');160 $this->strings['no_files'] = __('The package contains no files.');161 $this->strings['folder_exists'] = __('Destination folder already exists.');162 $this->strings['mkdir_failed'] = __('Could not create directory.');163 $this->strings['incompatible_archive'] = __( 'The package could not be installed.');164 $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.' );165 166 $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode…');167 $this->strings['maintenance_end'] = __('Disabling Maintenance mode…');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…' ); 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…' ); 168 $this->strings['maintenance_end'] = __( 'Disabling Maintenance mode…' ); 168 169 } 169 170 … … 191 192 if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) { 192 193 $error = true; 193 if ( is_object( $wp_filesystem) && $wp_filesystem->errors->get_error_code() )194 if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->get_error_code() ) { 194 195 $error = $wp_filesystem->errors; 196 } 195 197 // Failed to connect, Error and request again 196 198 $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership ); … … 198 200 } 199 201 200 if ( ! is_object($wp_filesystem) ) 201 return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] ); 202 203 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 204 return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors); 205 206 foreach ( (array)$directories as $dir ) { 202 if ( ! is_object( $wp_filesystem ) ) { 203 return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] ); 204 } 205 206 if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 207 return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors ); 208 } 209 210 foreach ( (array) $directories as $dir ) { 207 211 switch ( $dir ) { 208 212 case ABSPATH: 209 if ( ! $wp_filesystem->abspath() ) 210 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']); 213 if ( ! $wp_filesystem->abspath() ) { 214 return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] ); 215 } 211 216 break; 212 217 case WP_CONTENT_DIR: 213 if ( ! $wp_filesystem->wp_content_dir() ) 214 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']); 218 if ( ! $wp_filesystem->wp_content_dir() ) { 219 return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] ); 220 } 215 221 break; 216 222 case WP_PLUGIN_DIR: 217 if ( ! $wp_filesystem->wp_plugins_dir() ) 218 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']); 223 if ( ! $wp_filesystem->wp_plugins_dir() ) { 224 return new WP_Error( 'fs_no_plugins_dir', $this->strings['fs_no_plugins_dir'] ); 225 } 219 226 break; 220 227 case get_theme_root(): 221 if ( ! $wp_filesystem->wp_themes_dir() ) 222 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']); 228 if ( ! $wp_filesystem->wp_themes_dir() ) { 229 return new WP_Error( 'fs_no_themes_dir', $this->strings['fs_no_themes_dir'] ); 230 } 223 231 break; 224 232 default: 225 if ( ! $wp_filesystem->find_folder( $dir) )233 if ( ! $wp_filesystem->find_folder( $dir ) ) { 226 234 return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) ); 235 } 227 236 break; 228 237 } … … 253 262 */ 254 263 $reply = apply_filters( 'upgrader_pre_download', false, $package, $this ); 255 if ( false !== $reply ) 264 if ( false !== $reply ) { 256 265 return $reply; 257 258 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote? 266 } 267 268 if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { //Local file or remote? 259 269 return $package; //must be a local file.. 260 261 if ( empty($package) ) 262 return new WP_Error('no_package', $this->strings['no_package']); 263 264 $this->skin->feedback('downloading_package', $package); 265 266 $download_file = download_url($package); 267 268 if ( is_wp_error($download_file) ) 269 return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message()); 270 } 271 272 if ( empty( $package ) ) { 273 return new WP_Error( 'no_package', $this->strings['no_package'] ); 274 } 275 276 $this->skin->feedback( 'downloading_package', $package ); 277 278 $download_file = download_url( $package ); 279 280 if ( is_wp_error( $download_file ) ) { 281 return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() ); 282 } 270 283 271 284 return $download_file; … … 287 300 global $wp_filesystem; 288 301 289 $this->skin->feedback( 'unpack_package');302 $this->skin->feedback( 'unpack_package' ); 290 303 291 304 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/'; 292 305 293 306 //Clean up contents of upgrade directory beforehand. 294 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder); 295 if ( !empty($upgrade_files) ) { 296 foreach ( $upgrade_files as $file ) 297 $wp_filesystem->delete($upgrade_folder . $file['name'], true); 307 $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder ); 308 if ( ! empty( $upgrade_files ) ) { 309 foreach ( $upgrade_files as $file ) { 310 $wp_filesystem->delete( $upgrade_folder . $file['name'], true ); 311 } 298 312 } 299 313 … … 302 316 303 317 // Clean up working directory 304 if ( $wp_filesystem->is_dir($working_dir) ) 305 $wp_filesystem->delete($working_dir, true); 318 if ( $wp_filesystem->is_dir( $working_dir ) ) { 319 $wp_filesystem->delete( $working_dir, true ); 320 } 306 321 307 322 // Unzip package to working directory … … 309 324 310 325 // Once extracted, delete the package if required. 311 if ( $delete_package ) 312 unlink($package); 313 314 if ( is_wp_error($result) ) { 315 $wp_filesystem->delete($working_dir, true); 326 if ( $delete_package ) { 327 unlink( $package ); 328 } 329 330 if ( is_wp_error( $result ) ) { 331 $wp_filesystem->delete( $working_dir, true ); 316 332 if ( 'incompatible_archive' == $result->get_error_code() ) { 317 333 return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() ); … … 433 449 434 450 $defaults = array( 435 'source' => '', // Please always pass this436 'destination' => '', // and this437 'clear_destination' => false,438 'clear_working' => false,451 'source' => '', // Please always pass this 452 'destination' => '', // and this 453 'clear_destination' => false, 454 'clear_working' => false, 439 455 'abort_if_destination_exists' => true, 440 'hook_extra' => array()456 'hook_extra' => array(), 441 457 ); 442 458 443 $args = wp_parse_args( $args, $defaults);459 $args = wp_parse_args( $args, $defaults ); 444 460 445 461 // These were previously extract()'d. 446 $source = $args['source'];447 $destination = $args['destination'];462 $source = $args['source']; 463 $destination = $args['destination']; 448 464 $clear_destination = $args['clear_destination']; 449 465 … … 474 490 475 491 //Retain the Original source and destinations 476 $remote_source = $args['source'];492 $remote_source = $args['source']; 477 493 $local_destination = $destination; 478 494 479 $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );495 $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) ); 480 496 $remote_destination = $wp_filesystem->find_folder( $local_destination ); 481 497 … … 526 542 if ( in_array( $destination, $protected_directories ) ) { 527 543 $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) ); 528 $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );544 $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) ); 529 545 } 530 546 531 547 if ( $clear_destination ) { 532 548 // We're going to clear the destination if there's something there. 533 $this->skin->feedback( 'remove_old');549 $this->skin->feedback( 'remove_old' ); 534 550 535 551 $removed = $this->clear_destination( $remote_destination ); … … 550 566 return $removed; 551 567 } 552 } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination) ) {568 } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) { 553 569 //If we're not clearing the destination folder and something exists there already, Bail. 554 570 //But first check to see if there are actually any files in the folder. 555 $_files = $wp_filesystem->dirlist( $remote_destination);556 if ( ! empty( $_files) ) {557 $wp_filesystem->delete( $remote_source, true); //Clear out the source files.558 return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );571 $_files = $wp_filesystem->dirlist( $remote_destination ); 572 if ( ! empty( $_files ) ) { 573 $wp_filesystem->delete( $remote_source, true ); //Clear out the source files. 574 return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination ); 559 575 } 560 576 } … … 567 583 } 568 584 // Copy new version of item into place. 569 $result = copy_dir( $source, $remote_destination);570 if ( is_wp_error( $result) ) {585 $result = copy_dir( $source, $remote_destination ); 586 if ( is_wp_error( $result ) ) { 571 587 if ( $args['clear_working'] ) { 572 588 $wp_filesystem->delete( $remote_source, true ); … … 580 596 } 581 597 582 $destination_name = basename( str_replace( $local_destination, '', $destination) );598 $destination_name = basename( str_replace( $local_destination, '', $destination ) ); 583 599 if ( '.' == $destination_name ) { 584 600 $destination_name = ''; … … 598 614 $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result ); 599 615 600 if ( is_wp_error( $res) ) {616 if ( is_wp_error( $res ) ) { 601 617 $this->result = $res; 602 618 return $res; … … 643 659 644 660 $defaults = array( 645 'package' => '', // Please always pass this.646 'destination' => '', // And this647 'clear_destination' => false,661 'package' => '', // Please always pass this. 662 'destination' => '', // And this 663 'clear_destination' => false, 648 664 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please 649 'clear_working' => true,650 'is_multi' => false,651 'hook_extra' => array()// Pass any extra $hook_extra args here, this will be passed to any hooked filters.665 'clear_working' => true, 666 'is_multi' => false, 667 'hook_extra' => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters. 652 668 ); 653 669 … … 702 718 $this->skin->before(); 703 719 704 if ( is_wp_error( $res) ) {705 $this->skin->error( $res);720 if ( is_wp_error( $res ) ) { 721 $this->skin->error( $res ); 706 722 $this->skin->after(); 707 723 if ( ! $options['is_multi'] ) { … … 716 732 */ 717 733 $download = $this->download_package( $options['package'] ); 718 if ( is_wp_error( $download) ) {719 $this->skin->error( $download);734 if ( is_wp_error( $download ) ) { 735 $this->skin->error( $download ); 720 736 $this->skin->after(); 721 737 if ( ! $options['is_multi'] ) { … … 729 745 // Unzips the file into a temporary directory. 730 746 $working_dir = $this->unpack_package( $download, $delete_package ); 731 if ( is_wp_error( $working_dir) ) {732 $this->skin->error( $working_dir);747 if ( is_wp_error( $working_dir ) ) { 748 $this->skin->error( $working_dir ); 733 749 $this->skin->after(); 734 750 if ( ! $options['is_multi'] ) { … … 739 755 740 756 // With the given options, this installs it to the destination directory. 741 $result = $this->install_package( array( 742 'source' => $working_dir, 743 'destination' => $options['destination'], 744 'clear_destination' => $options['clear_destination'], 745 'abort_if_destination_exists' => $options['abort_if_destination_exists'], 746 'clear_working' => $options['clear_working'], 747 'hook_extra' => $options['hook_extra'] 748 ) ); 749 750 $this->skin->set_result($result); 751 if ( is_wp_error($result) ) { 752 $this->skin->error($result); 753 $this->skin->feedback('process_failed'); 757 $result = $this->install_package( 758 array( 759 'source' => $working_dir, 760 'destination' => $options['destination'], 761 'clear_destination' => $options['clear_destination'], 762 'abort_if_destination_exists' => $options['abort_if_destination_exists'], 763 'clear_working' => $options['clear_working'], 764 'hook_extra' => $options['hook_extra'], 765 ) 766 ); 767 768 $this->skin->set_result( $result ); 769 if ( is_wp_error( $result ) ) { 770 $this->skin->error( $result ); 771 $this->skin->feedback( 'process_failed' ); 754 772 } else { 755 773 // Installation succeeded. 756 $this->skin->feedback( 'process_success');774 $this->skin->feedback( 'process_success' ); 757 775 } 758 776 … … 814 832 $file = $wp_filesystem->abspath() . '.maintenance'; 815 833 if ( $enable ) { 816 $this->skin->feedback( 'maintenance_start');834 $this->skin->feedback( 'maintenance_start' ); 817 835 // Create maintenance file to signal that we are upgrading 818 836 $maintenance_string = '<?php $upgrading = ' . time() . '; ?>'; 819 $wp_filesystem->delete( $file);820 $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE);837 $wp_filesystem->delete( $file ); 838 $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE ); 821 839 } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) { 822 $this->skin->feedback( 'maintenance_end');823 $wp_filesystem->delete( $file);824 } 825 } 826 827 /** 828 * Creates a lock using WordPress options.829 *830 * @since 4.5.0831 * @static832 *833 * @param string $lock_name The name of this unique lock.834 * @param int $release_timeout Optional. The duration in seconds to respect an existing lock.840 $this->skin->feedback( 'maintenance_end' ); 841 $wp_filesystem->delete( $file ); 842 } 843 } 844 845 /** 846 * Creates a lock using WordPress options. 847 * 848 * @since 4.5.0 849 * @static 850 * 851 * @param string $lock_name The name of this unique lock. 852 * @param int $release_timeout Optional. The duration in seconds to respect an existing lock. 835 853 * Default: 1 hour. 836 * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.837 */854 * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise. 855 */ 838 856 public static function create_lock( $lock_name, $release_timeout = null ) { 839 857 global $wpdb; … … 872 890 873 891 /** 874 * Releases an upgrader lock.875 *876 * @since 4.5.0877 * @static892 * Releases an upgrader lock. 893 * 894 * @since 4.5.0 895 * @static 878 896 * 879 897 * @see WP_Upgrader::create_lock() 880 *881 * @param string $lock_name The name of this unique lock.898 * 899 * @param string $lock_name The name of this unique lock. 882 900 * @return bool True if the lock was successfully released. False on failure. 883 */901 */ 884 902 public static function release_lock( $lock_name ) { 885 903 return delete_option( $lock_name . '.lock' );
Note: See TracChangeset
for help on using the changeset viewer.