Make WordPress Core

Ticket #41524: 41524_recursive.diff

File 41524_recursive.diff, 2.9 KB (added by chris@…, 6 years ago)

Recursive version of flatten_dir_list

  • wp-admin/includes/class-wp-upgrader.php

     
    323323        }
    324324
    325325        /**
     326         * Flatten the results of $wp_filesystem->dirlist
     327         *
     328         * @param  array &$_flat_files  New array of files to be built, passed by reference.
     329         * @param  array $_files        Array of files as returned from WP_Filesystem->dirlist
     330         * @param  string $path         Path to prepend to child arrays, optional
     331         */
     332        private function _flatten_dir_list( &$_flat_files, $_files, $path = '' ) {
     333            foreach ( $_files as $file ) {
     334
     335                //The dirlist command returns the filename as both a parent array key
     336                //as well as a named key within the child array, use that for simplicity
     337                $name = $file[ 'name' ];
     338
     339                //Append the current file with the supplied relative path
     340                $_flat_files[ $path . $name ] = $file;
     341
     342                //Make sure we've got a sub-array of files
     343                if ( array_key_exists( 'files', $file ) && is_array( $file[ 'files' ] ) && count( $file[ 'files' ]  > 0 ) ) {
     344
     345                    //Iterate over the sub files and folders
     346                    foreach ( $file['files'] as $details ) {
     347
     348                        //flatten_dir_list expects the second
     349                        $this->_flatten_dir_list( $_flat_files, array( $details ), $path . $name . '/' );
     350                    }
     351                }
     352
     353            }
     354        }
     355
     356        /**
    326357         * Clears the directory where this item is going to be installed into.
    327358         *
    328359         * @since 4.3.0
     
    335366        public function clear_destination( $remote_destination ) {
    336367                global $wp_filesystem;
    337368
    338                 if ( ! $wp_filesystem->exists( $remote_destination ) ) {
     369                $_files = $wp_filesystem->dirlist( $remote_destination, true, true );
     370
     371                //dirlist() returns false if the directory doesn't exist
     372                if ( false === $_files ) {
    339373                        return true;
    340374                }
    341375
     376                //Will hold a flattened version of $_files
     377        $_flat_files = array();
     378
     379        $this->_flatten_dir_list( $_flat_files, $_files );
     380
    342381                // Check all files are writable before attempting to clear the destination.
    343382                $unwritable_files = array();
    344383
    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 
    361384                // Check writability.
    362                 foreach ( $_files as $filename => $file_details ) {
     385                foreach ( $_flat_files as $filename => $file_details ) {
    363386                        if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
    364387
    365388                                // Attempt to alter permissions to allow writes and try again.