WordPress.org

Make WordPress Core

Changeset 41821


Ignore:
Timestamp:
10/11/2017 04:23:49 AM (9 months ago)
Author:
dd32
Message:

Upgrades: Remove the usage of each() from WP_Upgrader for PHP 7.2 compatibility.

Props chrisvendiadvertisingcom, dd32.
Fixes #41524

File:
1 edited

Legend:

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

    r41289 r41821  
    324324
    325325    /**
     326     * Flatten the results of WP_Filesystem::dirlist() for iterating over.
     327     *
     328     * @since 4.9.0
     329     * @access protected
     330     *
     331     * @param  array  $nested_files  Array of files as returned by WP_Filesystem::dirlist()
     332     * @param  string $path          Relative path to prepend to child nodes. Optional.
     333     * @return array $files A flattened array of the $nested_files specified.
     334     */
     335    protected function flatten_dirlist( $nested_files, $path = '' ) {
     336        $files = array();
     337
     338        foreach ( $nested_files as $name => $details ) {
     339            $files[ $path . $name ] = $details;
     340
     341            // Append children recursively
     342            if ( ! empty( $details['files'] ) ) {
     343                $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' );
     344
     345                $files = array_merge( $files, $children );
     346            }
     347        }
     348
     349        return $files;
     350    }
     351
     352    /**
    326353     * Clears the directory where this item is going to be installed into.
    327354     *
     
    336363        global $wp_filesystem;
    337364
    338         if ( ! $wp_filesystem->exists( $remote_destination ) ) {
     365        $files = $wp_filesystem->dirlist( $remote_destination, true, true );
     366
     367        // False indicates that the $remote_destination doesn't exist.
     368        if ( false === $files ) {
    339369            return true;
    340370        }
     371
     372        // Flatten the file list to iterate over
     373        $files = $this->flatten_dirlist( $files );
    341374
    342375        // Check all files are writable before attempting to clear the destination.
    343376        $unwritable_files = array();
    344377
    345         $_files = $wp_filesystem->dirlist( $remote_destination, true, true );
    346 
    347         // Flatten the resulting array, iterate using each as we append to the array during iteration.
    348         while ( $f = each( $_files ) ) {
    349             $file = $f['value'];
    350             $name = $f['key'];
    351 
    352             if ( ! isset( $file['files'] ) ) {
    353                 continue;
    354             }
    355 
    356             foreach ( $file['files'] as $filename => $details ) {
    357                 $_files[ $name . '/' . $filename ] = $details;
    358             }
    359         }
    360 
    361378        // Check writability.
    362         foreach ( $_files as $filename => $file_details ) {
     379        foreach ( $files as $filename => $file_details ) {
    363380            if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
    364 
    365381                // Attempt to alter permissions to allow writes and try again.
    366382                $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
Note: See TracChangeset for help on using the changeset viewer.