Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 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-plugin-upgrader.php

    r41908 r42343  
    4545         */
    4646        public function upgrade_strings() {
    47                 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
    48                 $this->strings['no_package'] = __('Update package not available.');
     47                $this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
     48                $this->strings['no_package'] = __( 'Update package not available.' );
    4949                /* translators: %s: package URL */
    50                 $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
    51                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
    52                 $this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
    53                 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
    54                 $this->strings['process_failed'] = __('Plugin update failed.');
    55                 $this->strings['process_success'] = __('Plugin updated successfully.');
    56                 $this->strings['process_bulk_success'] = __('Plugins updated successfully.');
     50                $this->strings['downloading_package']  = sprintf( __( 'Downloading update from %s&#8230;' ), '<span class="code">%s</span>' );
     51                $this->strings['unpack_package']       = __( 'Unpacking the update&#8230;' );
     52                $this->strings['remove_old']           = __( 'Removing the old version of the plugin&#8230;' );
     53                $this->strings['remove_old_failed']    = __( 'Could not remove the old plugin.' );
     54                $this->strings['process_failed']       = __( 'Plugin update failed.' );
     55                $this->strings['process_success']      = __( 'Plugin updated successfully.' );
     56                $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
    5757        }
    5858
     
    6363         */
    6464        public function install_strings() {
    65                 $this->strings['no_package'] = __('Installation package not available.');
     65                $this->strings['no_package'] = __( 'Installation package not available.' );
    6666                /* translators: %s: package URL */
    6767                $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s&#8230;' ), '<span class="code">%s</span>' );
    68                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
    69                 $this->strings['installing_package'] = __('Installing the plugin&#8230;');
    70                 $this->strings['no_files'] = __('The plugin contains no files.');
    71                 $this->strings['process_failed'] = __('Plugin installation failed.');
    72                 $this->strings['process_success'] = __('Plugin installed successfully.');
     68                $this->strings['unpack_package']      = __( 'Unpacking the package&#8230;' );
     69                $this->strings['installing_package']  = __( 'Installing the plugin&#8230;' );
     70                $this->strings['no_files']            = __( 'The plugin contains no files.' );
     71                $this->strings['process_failed']      = __( 'Plugin installation failed.' );
     72                $this->strings['process_success']     = __( 'Plugin installed successfully.' );
    7373        }
    7474
     
    9090        public function install( $package, $args = array() ) {
    9191
    92                 $defaults = array(
     92                $defaults    = array(
    9393                        'clear_update_cache' => true,
    9494                );
     
    9898                $this->install_strings();
    9999
    100                 add_filter('upgrader_source_selection', array($this, 'check_package') );
     100                add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
    101101                if ( $parsed_args['clear_update_cache'] ) {
    102102                        // Clear cache so wp_update_plugins() knows about the new plugin.
     
    104104                }
    105105
    106                 $this->run( array(
    107                         'package' => $package,
    108                         'destination' => WP_PLUGIN_DIR,
    109                         'clear_destination' => false, // Do not overwrite files.
    110                         'clear_working' => true,
    111                         'hook_extra' => array(
    112                                 'type' => 'plugin',
    113                                 'action' => 'install',
     106                $this->run(
     107                        array(
     108                                'package'           => $package,
     109                                'destination'       => WP_PLUGIN_DIR,
     110                                'clear_destination' => false, // Do not overwrite files.
     111                                'clear_working'     => true,
     112                                'hook_extra'        => array(
     113                                        'type'   => 'plugin',
     114                                        'action' => 'install',
     115                                ),
    114116                        )
    115                 ) );
     117                );
    116118
    117119                remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
    118                 remove_filter('upgrader_source_selection', array($this, 'check_package') );
    119 
    120                 if ( ! $this->result || is_wp_error($this->result) )
     120                remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
     121
     122                if ( ! $this->result || is_wp_error( $this->result ) ) {
    121123                        return $this->result;
     124                }
    122125
    123126                // Force refresh of plugin update information
     
    144147        public function upgrade( $plugin, $args = array() ) {
    145148
    146                 $defaults = array(
     149                $defaults    = array(
    147150                        'clear_update_cache' => true,
    148151                );
     
    153156
    154157                $current = get_site_transient( 'update_plugins' );
    155                 if ( !isset( $current->response[ $plugin ] ) ) {
     158                if ( ! isset( $current->response[ $plugin ] ) ) {
    156159                        $this->skin->before();
    157                         $this->skin->set_result(false);
    158                         $this->skin->error('up_to_date');
     160                        $this->skin->set_result( false );
     161                        $this->skin->error( 'up_to_date' );
    159162                        $this->skin->after();
    160163                        return false;
     
    164167                $r = $current->response[ $plugin ];
    165168
    166                 add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
    167                 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
     169                add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
     170                add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
    168171                //'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
    169172                if ( $parsed_args['clear_update_cache'] ) {
     
    172175                }
    173176
    174                 $this->run( array(
    175                         'package' => $r->package,
    176                         'destination' => WP_PLUGIN_DIR,
    177                         'clear_destination' => true,
    178                         'clear_working' => true,
    179                         'hook_extra' => array(
    180                                 'plugin' => $plugin,
    181                                 'type' => 'plugin',
    182                                 'action' => 'update',
    183                         ),
    184                 ) );
     177                $this->run(
     178                        array(
     179                                'package'           => $r->package,
     180                                'destination'       => WP_PLUGIN_DIR,
     181                                'clear_destination' => true,
     182                                'clear_working'     => true,
     183                                'hook_extra'        => array(
     184                                        'plugin' => $plugin,
     185                                        'type'   => 'plugin',
     186                                        'action' => 'update',
     187                                ),
     188                        )
     189                );
    185190
    186191                // Cleanup our hooks, in case something else does a upgrade on this connection.
    187192                remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
    188                 remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
    189                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
    190 
    191                 if ( ! $this->result || is_wp_error($this->result) )
     193                remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
     194                remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
     195
     196                if ( ! $this->result || is_wp_error( $this->result ) ) {
    192197                        return $this->result;
     198                }
    193199
    194200                // Force refresh of plugin update information
     
    215221        public function bulk_upgrade( $plugins, $args = array() ) {
    216222
    217                 $defaults = array(
     223                $defaults    = array(
    218224                        'clear_update_cache' => true,
    219225                );
     
    226232                $current = get_site_transient( 'update_plugins' );
    227233
    228                 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
     234                add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
    229235
    230236                $this->skin->header();
    231237
    232238                // Connect to the Filesystem first.
    233                 $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
     239                $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
    234240                if ( ! $res ) {
    235241                        $this->skin->footer();
     
    246252                 */
    247253                $maintenance = ( is_multisite() && ! empty( $plugins ) );
    248                 foreach ( $plugins as $plugin )
    249                         $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
    250                 if ( $maintenance )
    251                         $this->maintenance_mode(true);
     254                foreach ( $plugins as $plugin ) {
     255                        $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
     256                }
     257                if ( $maintenance ) {
     258                        $this->maintenance_mode( true );
     259                }
    252260
    253261                $results = array();
    254262
    255                 $this->update_count = count($plugins);
     263                $this->update_count   = count( $plugins );
    256264                $this->update_current = 0;
    257265                foreach ( $plugins as $plugin ) {
    258266                        $this->update_current++;
    259                         $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
    260 
    261                         if ( !isset( $current->response[ $plugin ] ) ) {
    262                                 $this->skin->set_result('up_to_date');
     267                        $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
     268
     269                        if ( ! isset( $current->response[ $plugin ] ) ) {
     270                                $this->skin->set_result( 'up_to_date' );
    263271                                $this->skin->before();
    264                                 $this->skin->feedback('up_to_date');
     272                                $this->skin->feedback( 'up_to_date' );
    265273                                $this->skin->after();
    266                                 $results[$plugin] = true;
     274                                $results[ $plugin ] = true;
    267275                                continue;
    268276                        }
     
    271279                        $r = $current->response[ $plugin ];
    272280
    273                         $this->skin->plugin_active = is_plugin_active($plugin);
    274 
    275                         $result = $this->run( array(
    276                                 'package' => $r->package,
    277                                 'destination' => WP_PLUGIN_DIR,
    278                                 'clear_destination' => true,
    279                                 'clear_working' => true,
    280                                 'is_multi' => true,
    281                                 'hook_extra' => array(
    282                                         'plugin' => $plugin
     281                        $this->skin->plugin_active = is_plugin_active( $plugin );
     282
     283                        $result = $this->run(
     284                                array(
     285                                        'package'           => $r->package,
     286                                        'destination'       => WP_PLUGIN_DIR,
     287                                        'clear_destination' => true,
     288                                        'clear_working'     => true,
     289                                        'is_multi'          => true,
     290                                        'hook_extra'        => array(
     291                                                'plugin' => $plugin,
     292                                        ),
    283293                                )
    284                         ) );
    285 
    286                         $results[$plugin] = $this->result;
     294                        );
     295
     296                        $results[ $plugin ] = $this->result;
    287297
    288298                        // Prevent credentials auth screen from displaying multiple times
    289                         if ( false === $result )
     299                        if ( false === $result ) {
    290300                                break;
     301                        }
    291302                } //end foreach $plugins
    292303
    293                 $this->maintenance_mode(false);
     304                $this->maintenance_mode( false );
    294305
    295306                // Force refresh of plugin update information.
     
    297308
    298309                /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
    299                 do_action( 'upgrader_process_complete', $this, array(
    300                         'action' => 'update',
    301                         'type' => 'plugin',
    302                         'bulk' => true,
    303                         'plugins' => $plugins,
    304                 ) );
     310                do_action(
     311                        'upgrader_process_complete', $this, array(
     312                                'action'  => 'update',
     313                                'type'    => 'plugin',
     314                                'bulk'    => true,
     315                                'plugins' => $plugins,
     316                        )
     317                );
    305318
    306319                $this->skin->bulk_footer();
     
    309322
    310323                // Cleanup our hooks, in case something else does a upgrade on this connection.
    311                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
     324                remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
    312325
    313326                return $results;
     
    328341         *                         if no plugins were found.
    329342         */
    330         public function check_package($source) {
     343        public function check_package( $source ) {
    331344                global $wp_filesystem;
    332345
    333                 if ( is_wp_error($source) )
     346                if ( is_wp_error( $source ) ) {
    334347                        return $source;
    335 
    336                 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
    337                 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
     348                }
     349
     350                $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
     351                if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
    338352                        return $source;
     353                }
    339354
    340355                // Check the folder contains at least 1 valid plugin.
    341356                $plugins_found = false;
    342                 $files = glob( $working_directory . '*.php' );
     357                $files         = glob( $working_directory . '*.php' );
    343358                if ( $files ) {
    344359                        foreach ( $files as $file ) {
     
    351366                }
    352367
    353                 if ( ! $plugins_found )
     368                if ( ! $plugins_found ) {
    354369                        return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
     370                }
    355371
    356372                return $source;
     
    367383         */
    368384        public function plugin_info() {
    369                 if ( ! is_array($this->result) )
     385                if ( ! is_array( $this->result ) ) {
    370386                        return false;
    371                 if ( empty($this->result['destination_name']) )
     387                }
     388                if ( empty( $this->result['destination_name'] ) ) {
    372389                        return false;
    373 
    374                 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
    375                 if ( empty($plugin) )
     390                }
     391
     392                $plugin = get_plugins( '/' . $this->result['destination_name'] ); //Ensure to pass with leading slash
     393                if ( empty( $plugin ) ) {
    376394                        return false;
    377 
    378                 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
     395                }
     396
     397                $pluginfiles = array_keys( $plugin ); //Assume the requested plugin is the first in the list
    379398
    380399                return $this->result['destination_name'] . '/' . $pluginfiles[0];
     
    393412         * @return bool|WP_Error The passed in $return param or WP_Error.
    394413         */
    395         public function deactivate_plugin_before_upgrade($return, $plugin) {
    396 
    397                 if ( is_wp_error($return) ) //Bypass.
     414        public function deactivate_plugin_before_upgrade( $return, $plugin ) {
     415
     416                if ( is_wp_error( $return ) ) { //Bypass.
    398417                        return $return;
     418                }
    399419
    400420                // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
    401                 if ( wp_doing_cron() )
     421                if ( wp_doing_cron() ) {
    402422                        return $return;
    403 
    404                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
    405                 if ( empty($plugin) )
    406                         return new WP_Error('bad_request', $this->strings['bad_request']);
    407 
    408                 if ( is_plugin_active($plugin) ) {
     423                }
     424
     425                $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
     426                if ( empty( $plugin ) ) {
     427                        return new WP_Error( 'bad_request', $this->strings['bad_request'] );
     428                }
     429
     430                if ( is_plugin_active( $plugin ) ) {
    409431                        //Deactivate the plugin silently, Prevent deactivation hooks from running.
    410                         deactivate_plugins($plugin, true);
     432                        deactivate_plugins( $plugin, true );
    411433                }
    412434
     
    423445         *
    424446         * @global WP_Filesystem_Base $wp_filesystem Subclass
    425     *
     447        *
    426448         * @param bool|WP_Error $removed
    427449         * @param string        $local_destination
     
    430452         * @return WP_Error|bool
    431453         */
    432         public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
     454        public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
    433455                global $wp_filesystem;
    434456
    435                 if ( is_wp_error($removed) )
     457                if ( is_wp_error( $removed ) ) {
    436458                        return $removed; //Pass errors through.
    437 
    438                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
    439                 if ( empty($plugin) )
    440                         return new WP_Error('bad_request', $this->strings['bad_request']);
    441 
    442                 $plugins_dir = $wp_filesystem->wp_plugins_dir();
    443                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
    444 
    445                 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
     459                }
     460
     461                $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
     462                if ( empty( $plugin ) ) {
     463                        return new WP_Error( 'bad_request', $this->strings['bad_request'] );
     464                }
     465
     466                $plugins_dir     = $wp_filesystem->wp_plugins_dir();
     467                $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
     468
     469                if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { //If it's already vanished.
    446470                        return $removed;
     471                }
    447472
    448473                // If plugin is in its own directory, recursively delete the directory.
    449                 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
    450                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
    451                 else
    452                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
    453 
    454                 if ( ! $deleted )
    455                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
     474                if ( strpos( $plugin, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
     475                        $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
     476                } else {
     477                        $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
     478                }
     479
     480                if ( ! $deleted ) {
     481                        return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
     482                }
    456483
    457484                return true;
Note: See TracChangeset for help on using the changeset viewer.