WordPress.org

Make WordPress Core

Ticket #7395: 7395.2.diff

File 7395.2.diff, 8.3 KB (added by DD32, 10 years ago)
  • wp-admin/includes/file.php

     
    482482        if ( 0 == count($archive_files) )
    483483                return new WP_Error('empty_archive', __('Empty archive'));
    484484
     485        //Prepend another directory level if there are files in the root directory of the zip
     486        foreach ( $archive_files as $archive_file ) {
     487                if ( false === strpos($archive_file['filename'], '/') ) {
     488                        $to = trailingslashit($to) . basename($file, '.zip');
     489                        break;
     490                }
     491        }
     492
    485493        $path = explode('/', untrailingslashit($to));
    486494        for ( $i = count($path); $i > 0; $i-- ) { //>0 = first element is empty allways for paths starting with '/'
    487495                $tmppath = implode('/', array_slice($path, 0, $i) );
     
    558566}
    559567
    560568/**
     569 * Locates the lowest safe directory in a Plugin folder to copy to the plugins directory.
     570 *
     571 * Note: Desination directory will allways be unique.
     572 *
     573 * @since 2.7.0
     574 *
     575 * @param string $from the Working directory of the plugin files
     576 * @param string $to the proposed destination for the for the plugin files
     577 * @return array an array of the keys 'from' and 'to' for the actual copy.
     578 */
     579function update_pluginfiles_base_dir($from, $to) {
     580        $files = list_files($from);
     581
     582        //Remove non-php files
     583        foreach ( (array)$files as $key => $file )
     584                if ( ! preg_match('!.php$!i', $file) )
     585                        unset($files[$key]);
     586
     587        //remove non-plugin files
     588        foreach ( (array)$files as $key => $file ) {
     589                $details = get_plugin_data($file, false, false);
     590                if ( empty($details['Name']) )
     591                        unset($files[$key]);
     592        }
     593
     594        if ( empty($files) )
     595                return false;
     596
     597        //Left with paths to files which ARE plugins.
     598        $min_num = 100; //assume no zips with deeper paths will come along
     599        $min_file = '';
     600        foreach ( (array)$files as $key => $file ) {
     601                $this_num = substr_count($file, '/');
     602                if ( $this_num < $min_num ) {
     603                        $min_file = $file;
     604                        $min_num = $this_num;
     605                }
     606        }
     607        unset($min_num);
     608
     609        $from = dirname($min_file);
     610        //Ensure its a unique install folder, Upgrades delete the folder prior to this.
     611        $to = dirname($to) . '/' . wp_unique_filename(dirname($to), basename($to));
     612
     613        return compact('from', 'to');
     614}
     615
     616/**
    561617 * {@internal Missing Short Description}}
    562618 *
    563619 * @since unknown
  • wp-admin/includes/plugin-install.php

     
    745745                return $result;
    746746        }
    747747
    748         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     748        apply_filters('install_feedback', __('Installing the plugin'));
     749       
    749750        $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    750751
    751         if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    752                 $wp_filesystem->delete($working_dir, true);
    753                 return new WP_Error('install_folder_exists', __('Folder allready exists.'), $filelist[0] );
    754         }
     752        //find base plugin directory
     753        $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
    755754
    756         apply_filters('install_feedback', __('Installing the plugin'));
     755        //Create folder if not exists.
     756        if( ! $wp_filesystem->exists( $res['to'] ) )
     757                if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     758                        return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']);     
     759
    757760        // Copy new version of plugin into place.
    758         $result = copy_dir($working_dir, $plugins_dir);
     761        $result = copy_dir($res['from'], $res['to']);
    759762        if ( is_wp_error($result) ) {
    760763                $wp_filesystem->delete($working_dir, true);
    761764                return $result;
    762765        }
    763766
    764         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    765         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    766 
    767767        // Remove working directory
    768768        $wp_filesystem->delete($working_dir, true);
    769769
    770         if( empty($filelist) )
    771                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    772 
    773         $folder = $filelist[0];
     770        $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    774771        $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    775772        $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    776773
    777774        //Return the plugin files name.
    778         return  $folder . '/' . $pluginfiles[0];
     775        return $folder . $pluginfiles[0];
    779776}
    780777
    781778/**
     
    840837                return $result;
    841838        }
    842839
    843         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     840        apply_filters('install_feedback', __('Installing the plugin'));
     841       
    844842        $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    845843
    846         if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    847                 $wp_filesystem->delete($working_dir, true);
    848                 return new WP_Error('install_folder_exists', __('Folder allready exists.'), $filelist[0] );
    849         }
     844        //find base plugin directory
     845        $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
    850846
    851         apply_filters('install_feedback', __('Installing the plugin'));
     847        //Create folder if not exists.
     848        if( ! $wp_filesystem->exists( $res['to'] ) )
     849                if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     850                        return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']);     
     851
    852852        // Copy new version of plugin into place.
    853         $result = copy_dir($working_dir, $plugins_dir);
     853        $result = copy_dir($res['from'], $res['to']);
    854854        if ( is_wp_error($result) ) {
    855855                $wp_filesystem->delete($working_dir, true);
    856856                return $result;
    857857        }
    858858
    859         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    860         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    861 
    862859        // Remove working directory
    863860        $wp_filesystem->delete($working_dir, true);
    864861
    865         if( empty($filelist) )
    866                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    867 
    868         $folder = $filelist[0];
     862        $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    869863        $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    870864        $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    871865
    872866        //Return the plugin files name.
    873         return  $folder . '/' . $pluginfiles[0];
     867        return $folder . $pluginfiles[0];
    874868}
    875869
    876 ?>
     870?>
     871 No newline at end of file
  • wp-admin/includes/update.php

     
    241241        }
    242242
    243243        apply_filters('update_feedback', __('Installing the latest version'));
     244
     245        $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
     246
     247        //find base plugin directory
     248        $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
     249
     250        //Create folder if not exists.
     251        if( ! $wp_filesystem->exists( $res['to'] ) )
     252                if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     253                        return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']);     
     254
    244255        // Copy new version of plugin into place.
    245         $result = copy_dir($working_dir, $plugins_dir);
     256        $result = copy_dir($res['from'], $res['to']);
    246257        if ( is_wp_error($result) ) {
    247258                $wp_filesystem->delete($working_dir, true);
    248259                return $result;
    249260        }
    250261
    251         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    252         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    253 
    254262        // Remove working directory
    255263        $wp_filesystem->delete($working_dir, true);
    256264
    257265        // Force refresh of plugin update information
    258266        delete_option('update_plugins');
    259267
    260         if( empty($filelist) )
    261                 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    262 
    263         $folder = $filelist[0];
     268        $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    264269        $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    265270        $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    266271
    267         return  $folder . '/' . $pluginfiles[0];
     272        //Return the plugin files name.
     273        return $folder . $pluginfiles[0];
    268274}
    269275
    270276function wp_update_theme($theme, $feedback = '') {