Make WordPress Core

Changeset 9523


Ignore:
Timestamp:
11/05/2008 05:07:45 PM (16 years ago)
Author:
ryan
Message:

Support installation of plugins living in a subdir in svn. Props DD32. fixes #7395

Location:
trunk/wp-admin/includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/file.php

    r9493 r9523  
    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 '/'
     
    560568        }
    561569    }
     570}
     571
     572/**
     573 * Locates the lowest safe directory in a Plugin folder to copy to the plugins directory.
     574 *
     575 * Note: Desination directory will allways be unique.
     576 *
     577 * @since 2.7.0
     578 *
     579 * @param string $from the Working directory of the plugin files
     580 * @param string $to the proposed destination for the for the plugin files
     581 * @return array an array of the keys 'from' and 'to' for the actual copy.
     582 */
     583function update_pluginfiles_base_dir($from, $to) {
     584    $files = list_files($from);
     585
     586    //Remove non-php files
     587    foreach ( (array)$files as $key => $file )
     588        if ( ! preg_match('!.php$!i', $file) )
     589            unset($files[$key]);
     590
     591    //remove non-plugin files
     592    foreach ( (array)$files as $key => $file ) {
     593        $details = get_plugin_data($file, false, false);
     594        if ( empty($details['Name']) )
     595            unset($files[$key]);
     596    }
     597
     598    if ( empty($files) )
     599        return false;
     600
     601    //Left with paths to files which ARE plugins.
     602    $min_num = 100; //assume no zips with deeper paths will come along
     603    $min_file = '';
     604    foreach ( (array)$files as $key => $file ) {
     605        $this_num = substr_count($file, '/');
     606        if ( $this_num < $min_num ) {
     607            $min_file = $file;
     608            $min_num = $this_num;
     609        }
     610    }
     611    unset($min_num);
     612
     613    $from = dirname($min_file);
     614    //Ensure its a unique install folder, Upgrades delete the folder prior to this.
     615    $to = dirname($to) . '/' . wp_unique_filename(dirname($to), basename($to));
     616
     617    return compact('from', 'to');
    562618}
    563619
  • trunk/wp-admin/includes/plugin-install.php

    r9521 r9523  
    745745    }
    746746
    747     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     747    apply_filters('install_feedback', __('Installing the plugin'));
     748   
    748749    $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    749750
    750     if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    751         $wp_filesystem->delete($working_dir, true);
    752         return new WP_Error('install_folder_exists', __('Folder allready exists.'), $filelist[0] );
    753     }
    754 
    755     apply_filters('install_feedback', __('Installing the plugin'));
     751    //find base plugin directory
     752    $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
     753
     754    //Create folder if not exists.
     755    if( ! $wp_filesystem->exists( $res['to'] ) )
     756        if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     757            return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']); 
     758
    756759    // Copy new version of plugin into place.
    757     $result = copy_dir($working_dir, $plugins_dir);
     760    $result = copy_dir($res['from'], $res['to']);
    758761    if ( is_wp_error($result) ) {
    759762        $wp_filesystem->delete($working_dir, true);
     
    761764    }
    762765
    763     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    764     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    765 
    766766    // Remove working directory
    767767    $wp_filesystem->delete($working_dir, true);
    768768
    769     if( empty($filelist) )
    770         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    771 
    772     $folder = $filelist[0];
     769    $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    773770    $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    774771    $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    775772
    776773    //Return the plugin files name.
    777     return  $folder . '/' . $pluginfiles[0];
     774    return $folder . $pluginfiles[0];
    778775}
    779776
     
    840837    }
    841838
    842     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     839    apply_filters('install_feedback', __('Installing the plugin'));
     840   
    843841    $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    844842
    845     if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    846         $wp_filesystem->delete($working_dir, true);
    847         return new WP_Error('install_folder_exists', __('Folder allready exists.'), $filelist[0] );
    848     }
    849 
    850     apply_filters('install_feedback', __('Installing the plugin'));
     843    //find base plugin directory
     844    $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
     845
     846    //Create folder if not exists.
     847    if( ! $wp_filesystem->exists( $res['to'] ) )
     848        if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     849            return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']); 
     850
    851851    // Copy new version of plugin into place.
    852     $result = copy_dir($working_dir, $plugins_dir);
     852    $result = copy_dir($res['from'], $res['to']);
    853853    if ( is_wp_error($result) ) {
    854854        $wp_filesystem->delete($working_dir, true);
     
    856856    }
    857857
    858     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    859     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    860 
    861858    // Remove working directory
    862859    $wp_filesystem->delete($working_dir, true);
    863860
    864     if( empty($filelist) )
    865         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    866 
    867     $folder = $filelist[0];
     861    $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    868862    $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    869863    $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    870864
    871865    //Return the plugin files name.
    872     return  $folder . '/' . $pluginfiles[0];
     866    return $folder . $pluginfiles[0];
    873867}
    874868
  • trunk/wp-admin/includes/update.php

    r9512 r9523  
    247247
    248248    apply_filters('update_feedback', __('Installing the latest version'));
     249
     250    $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
     251
     252    //find base plugin directory
     253    $res = update_pluginfiles_base_dir($working_dir . '/' . $filelist[0], $plugins_dir . $filelist[0]);
     254
     255    //Create folder if not exists.
     256    if( ! $wp_filesystem->exists( $res['to'] ) )
     257        if ( ! $wp_filesystem->mkdir( $res['to'] ) )
     258            return new WP_Error('mkdir_failed', __('Could not create directory'), $res['to']); 
     259
    249260    // Copy new version of plugin into place.
    250     $result = copy_dir($working_dir, $plugins_dir);
     261    $result = copy_dir($res['from'], $res['to']);
    251262    if ( is_wp_error($result) ) {
    252263        $wp_filesystem->delete($working_dir, true);
    253264        return $result;
    254265    }
    255 
    256     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    257     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    258266
    259267    // Remove working directory
     
    263271    delete_option('update_plugins');
    264272
    265     if( empty($filelist) )
    266         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    267 
    268     $folder = $filelist[0];
     273    $folder = trailingslashit(str_replace($plugins_dir, '', $res['to']));
    269274    $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    270275    $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    271276
    272     return  $folder . '/' . $pluginfiles[0];
     277    //Return the plugin files name.
     278    return $folder . $pluginfiles[0];
    273279}
    274280
Note: See TracChangeset for help on using the changeset viewer.