Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-upgrader.php

    r42214 r42343  
    121121     */
    122122    public function __construct( $skin = null ) {
    123         if ( null == $skin )
     123        if ( null == $skin ) {
    124124            $this->skin = new WP_Upgrader_Skin();
    125         else
     125        } else {
    126126            $this->skin = $skin;
     127        }
    127128    }
    128129
     
    136137     */
    137138    public function init() {
    138         $this->skin->set_upgrader($this);
     139        $this->skin->set_upgrader( $this );
    139140        $this->generic_strings();
    140141    }
     
    146147     */
    147148    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.' );
    155156        /* 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…' );
    168169    }
    169170
     
    191192        if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
    192193            $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() ) {
    194195                $error = $wp_filesystem->errors;
     196            }
    195197            // Failed to connect, Error and request again
    196198            $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
     
    198200        }
    199201
    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 ) {
    207211            switch ( $dir ) {
    208212                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                    }
    211216                    break;
    212217                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                    }
    215221                    break;
    216222                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                    }
    219226                    break;
    220227                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                    }
    223231                    break;
    224232                default:
    225                     if ( ! $wp_filesystem->find_folder($dir) )
     233                    if ( ! $wp_filesystem->find_folder( $dir ) ) {
    226234                        return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
     235                    }
    227236                    break;
    228237            }
     
    253262         */
    254263        $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
    255         if ( false !== $reply )
     264        if ( false !== $reply ) {
    256265            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?
    259269            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        }
    270283
    271284        return $download_file;
     
    287300        global $wp_filesystem;
    288301
    289         $this->skin->feedback('unpack_package');
     302        $this->skin->feedback( 'unpack_package' );
    290303
    291304        $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
    292305
    293306        //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            }
    298312        }
    299313
     
    302316
    303317        // 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        }
    306321
    307322        // Unzip package to working directory
     
    309324
    310325        // 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 );
    316332            if ( 'incompatible_archive' == $result->get_error_code() ) {
    317333                return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
     
    433449
    434450        $defaults = array(
    435             'source' => '', // Please always pass this
    436             'destination' => '', // and this
    437             '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,
    439455            'abort_if_destination_exists' => true,
    440             'hook_extra' => array()
     456            'hook_extra'                  => array(),
    441457        );
    442458
    443         $args = wp_parse_args($args, $defaults);
     459        $args = wp_parse_args( $args, $defaults );
    444460
    445461        // These were previously extract()'d.
    446         $source = $args['source'];
    447         $destination = $args['destination'];
     462        $source            = $args['source'];
     463        $destination       = $args['destination'];
    448464        $clear_destination = $args['clear_destination'];
    449465
     
    474490
    475491        //Retain the Original source and destinations
    476         $remote_source = $args['source'];
     492        $remote_source     = $args['source'];
    477493        $local_destination = $destination;
    478494
    479         $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
     495        $source_files       = array_keys( $wp_filesystem->dirlist( $remote_source ) );
    480496        $remote_destination = $wp_filesystem->find_folder( $local_destination );
    481497
     
    526542        if ( in_array( $destination, $protected_directories ) ) {
    527543            $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
    528             $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
     544            $destination        = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
    529545        }
    530546
    531547        if ( $clear_destination ) {
    532548            // We're going to clear the destination if there's something there.
    533             $this->skin->feedback('remove_old');
     549            $this->skin->feedback( 'remove_old' );
    534550
    535551            $removed = $this->clear_destination( $remote_destination );
     
    550566                return $removed;
    551567            }
    552         } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
     568        } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
    553569            //If we're not clearing the destination folder and something exists there already, Bail.
    554570            //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 );
    559575            }
    560576        }
     
    567583        }
    568584        // 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 ) ) {
    571587            if ( $args['clear_working'] ) {
    572588                $wp_filesystem->delete( $remote_source, true );
     
    580596        }
    581597
    582         $destination_name = basename( str_replace($local_destination, '', $destination) );
     598        $destination_name = basename( str_replace( $local_destination, '', $destination ) );
    583599        if ( '.' == $destination_name ) {
    584600            $destination_name = '';
     
    598614        $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
    599615
    600         if ( is_wp_error($res) ) {
     616        if ( is_wp_error( $res ) ) {
    601617            $this->result = $res;
    602618            return $res;
     
    643659
    644660        $defaults = array(
    645             'package' => '', // Please always pass this.
    646             'destination' => '', // And this
    647             'clear_destination' => false,
     661            'package'                     => '', // Please always pass this.
     662            'destination'                 => '', // And this
     663            'clear_destination'           => false,
    648664            '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.
    652668        );
    653669
     
    702718        $this->skin->before();
    703719
    704         if ( is_wp_error($res) ) {
    705             $this->skin->error($res);
     720        if ( is_wp_error( $res ) ) {
     721            $this->skin->error( $res );
    706722            $this->skin->after();
    707723            if ( ! $options['is_multi'] ) {
     
    716732         */
    717733        $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 );
    720736            $this->skin->after();
    721737            if ( ! $options['is_multi'] ) {
     
    729745        // Unzips the file into a temporary directory.
    730746        $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 );
    733749            $this->skin->after();
    734750            if ( ! $options['is_multi'] ) {
     
    739755
    740756        // 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' );
    754772        } else {
    755773            // Installation succeeded.
    756             $this->skin->feedback('process_success');
     774            $this->skin->feedback( 'process_success' );
    757775        }
    758776
     
    814832        $file = $wp_filesystem->abspath() . '.maintenance';
    815833        if ( $enable ) {
    816             $this->skin->feedback('maintenance_start');
     834            $this->skin->feedback( 'maintenance_start' );
    817835            // Create maintenance file to signal that we are upgrading
    818836            $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 );
    821839        } 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.0
    831      * @static
    832      *
    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.
    835853     *                                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     */
    838856    public static function create_lock( $lock_name, $release_timeout = null ) {
    839857        global $wpdb;
     
    872890
    873891    /**
    874      * Releases an upgrader lock.
    875      *
    876      * @since 4.5.0
    877      * @static
     892     * Releases an upgrader lock.
     893     *
     894     * @since 4.5.0
     895     * @static
    878896     *
    879897     * @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.
    882900     * @return bool True if the lock was successfully released. False on failure.
    883      */
     901     */
    884902    public static function release_lock( $lock_name ) {
    885903        return delete_option( $lock_name . '.lock' );
Note: See TracChangeset for help on using the changeset viewer.