diff --git a/wp-admin/includes/class-wp-upgrader.php b/wp-admin/includes/class-wp-upgrader.php index f7dea257ea..fd70ba469e 100644 --- a/wp-admin/includes/class-wp-upgrader.php +++ b/wp-admin/includes/class-wp-upgrader.php @@ -322,6 +322,33 @@ class WP_Upgrader { return $working_dir; } + /** + * Flatten the results of WP_Filesystem::dirlist() for iterating over. + * + * @since 4.9.0 + * @access protected + * + * @param array $nested_files Array of files as returned by WP_Filesystem::dirlist() + * @param string $path Relative path to prepend to child nodes. Optional. + * @return array $files A flattened array of the $nested_files specified. + */ + protected function flatten_dirlist( $nested_files, $path = '' ) { + $files = array(); + + foreach ( $nested_files as $name => $details ) { + $files[ $path . $name ] = $details; + + // Append children recursively + if ( ! empty( $details['files'] ) ) { + $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' ); + + $files = array_merge( $files, $children ); + } + } + + return $files; + } + /** * Clears the directory where this item is going to be installed into. * @@ -335,33 +362,22 @@ class WP_Upgrader { public function clear_destination( $remote_destination ) { global $wp_filesystem; - if ( ! $wp_filesystem->exists( $remote_destination ) ) { + $files = $wp_filesystem->dirlist( $remote_destination, true, true ); + + // False indicates that the $remote_destination doesn't exist. + if ( false === $files ) { return true; } + // Flatten the file list to iterate over + $files = $this->flatten_dirlist( $files ); + // Check all files are writable before attempting to clear the destination. $unwritable_files = array(); - $_files = $wp_filesystem->dirlist( $remote_destination, true, true ); - - // Flatten the resulting array, iterate using each as we append to the array during iteration. - while ( $f = each( $_files ) ) { - $file = $f['value']; - $name = $f['key']; - - if ( ! isset( $file['files'] ) ) { - continue; - } - - foreach ( $file['files'] as $filename => $details ) { - $_files[ $name . '/' . $filename ] = $details; - } - } - // Check writability. - foreach ( $_files as $filename => $file_details ) { + foreach ( $files as $filename => $file_details ) { if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) { - // Attempt to alter permissions to allow writes and try again. $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) ); if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 1980a5f570..10cbb283d7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9-beta1-41820'; +$wp_version = '4.9-beta1-41821'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.