WordPress.org

Make WordPress Core

Changeset 11005


Ignore:
Timestamp:
04/19/2009 07:36:28 PM (11 years ago)
Author:
ryan
Message:

consolidate plugin/theme/core upgrade/install functions. Props DD32. see #7875

Location:
trunk/wp-admin
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-filesystem-ftpext.php

    r10930 r11005  
    1818    var $link;
    1919    var $timeout = 5;
    20     var $errors = array();
     20    var $errors = null;
    2121    var $options = array();
    2222
     
    6666
    6767    function connect() {
    68         if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
     68        if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
    6969            $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
    7070        else
     
    188188    }
    189189
    190     function delete($file,$recursive=false) {
     190    function delete($file, $recursive = false ) {
     191        if ( empty($file) )
     192            return false;
    191193        if ( $this->is_file($file) )
    192194            return @ftp_delete($this->link, $file);
    193195        if ( !$recursive )
    194196            return @ftp_rmdir($this->link, $file);
    195         $filelist = $this->dirlist($file);
    196         foreach ((array) $filelist as $filename => $fileinfo) {
    197             $this->delete($file . '/' . $filename, $recursive);
    198         }
     197
     198        $filelist = $this->dirlist( trailingslashit($file) );
     199        if ( !empty($filelist) )
     200            foreach ( $filelist as $delete_file )
     201                $this->delete( trailingslashit($file) . $delete_file['name'], $recursive);
    199202        return @ftp_rmdir($this->link, $file);
    200203    }
    201204
    202205    function exists($file) {
    203         $list = ftp_rawlist($this->link, $file, false);
    204         if( ! $list )
    205             return false;
    206         return count($list) == 1 ? true : false;
     206        $list = @ftp_rawlist($this->link, $file, false);
     207        return !empty($list); //empty list = no file, so invert.
    207208    }
    208209    function is_file($file) {
    209         return $this->is_dir($file) ? false : true;
     210        return $this->exists($file) && !$this->is_dir($file);
    210211    }
    211212    function is_dir($path) {
    212213        $cwd = $this->cwd();
    213         $result = @ftp_chdir($this->link, $path);
     214        $result = @ftp_chdir($this->link, trailingslashit($path) );
    214215        if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
    215216            @ftp_chdir($this->link, $cwd);
     
    219220    }
    220221    function is_readable($file) {
    221         //Get dir list, Check if the file is writable by the current user??
     222        //Get dir list, Check if the file is readable by the current user??
    222223        return true;
    223224    }
     
    239240    }
    240241    function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
    241         if( !@ftp_mkdir($this->link, $path) )
     242        if( !ftp_mkdir($this->link, $path) )
    242243            return false;
    243244        if( $chmod )
     
    250251    }
    251252    function rmdir($path, $recursive = false) {
    252         if( ! $recursive )
    253             return @ftp_rmdir($this->link, $path);
    254 
    255         //TODO: Recursive Directory delete, Have to delete files from the folder first.
    256         //$dir = $this->dirlist($path);
    257         //foreach($dir as $file)
    258 
     253        return $this->delete($path, $recursive);
    259254    }
    260255
    261256    function parselisting($line) {
    262         $is_windows = ($this->OS_remote == FTP_OS_Windows);
     257        static $is_windows;
     258        if ( is_null($is_windows) )
     259            $is_windows = strpos( strtolower(ftp_systype($this->link)), 'win') !== false;
     260
    263261        if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
    264262            $b = array();
  • trunk/wp-admin/includes/class-wp-filesystem-ftpsockets.php

    r10930 r11005  
    1818    var $ftp = false;
    1919    var $timeout = 5;
    20     var $errors;
     20    var $errors = null;
    2121    var $options = array();
    2222
     
    211211
    212212    function delete($file, $recursive = false ) {
     213        if ( empty($file) )
     214            return false;
    213215        if ( $this->is_file($file) )
    214216            return $this->ftp->delete($file);
  • trunk/wp-admin/includes/file.php

    r10850 r11005  
    599599    $wp_filesystem = new $method($args);
    600600
    601     if ( $wp_filesystem->errors->get_error_code() )
     601    if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
    602602        return false;
    603603
  • trunk/wp-admin/includes/plugin-install.php

    r10944 r11005  
    209209    <h4><?php _e('Install a plugin in .zip format') ?></h4>
    210210    <p class="install-help"><?php _e('If you have a plugin in a .zip format, You may install it by uploading it here.') ?></p>
    211     <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('plugin-install.php?tab=do_upload') ?>">
     211    <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-plugin') ?>">
    212212        <?php wp_nonce_field( 'plugin-upload') ?>
    213213        <input type="file" name="pluginzip" />
     
    462462                case 'install':
    463463                    if ( current_user_can('install_plugins') ) :
    464                 ?><a href="<?php echo wp_nonce_url(admin_url('plugin-install.php?tab=install&plugin=' . $api->slug), 'install-plugin_' . $api->slug) ?>" target="_parent"><?php _e('Install Now') ?></a><?php
     464                ?><a href="<?php echo wp_nonce_url(admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug) ?>" target="_parent"><?php _e('Install Now') ?></a><?php
    465465                    endif;
    466466                break;
     
    545545    exit;
    546546}
    547 
    548 
    549 add_action('install_plugins_do_upload', 'upload_plugin');
    550 function upload_plugin() {
    551 
    552     if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
    553         wp_die($uploads['error']);
    554 
    555     if ( !empty($_FILES) )
    556         $filename = $_FILES['pluginzip']['name'];
    557     else if ( isset($_GET['package']) )
    558         $filename = $_GET['package'];
    559 
    560     check_admin_referer('plugin-upload');
    561 
    562     echo '<div class="wrap">';
    563     echo '<h2>', sprintf( __('Installing Plugin from file: %s'), basename($filename) ), '</h2>';
    564 
    565     //Handle a newly uploaded file, Else assume it was
    566     if ( !empty($_FILES) ) {
    567         $filename = wp_unique_filename( $uploads['basedir'], $filename );
    568         $local_file = $uploads['basedir'] . '/' . $filename;
    569 
    570         // Move the file to the uploads dir
    571         if ( false === @ move_uploaded_file( $_FILES['pluginzip']['tmp_name'], $local_file) )
    572             wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
    573     } else {
    574         $local_file = $uploads['basedir'] . '/' . $filename;
    575     }
    576 
    577     do_plugin_install_local_package($local_file, $filename);
    578     echo '</div>';
    579 }
    580 
    581 add_action('install_plugins_install', 'install_plugin');
    582 
    583 /**
    584  * Display plugin link and execute install.
    585  *
    586  * @since 2.7.0
    587  */
    588 function install_plugin() {
    589 
    590     $plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
    591 
    592     check_admin_referer('install-plugin_' . $plugin);
    593     $api = plugins_api('plugin_information', array('slug' => $plugin, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
    594 
    595     if ( is_wp_error($api) )
    596         wp_die($api);
    597 
    598     echo '<div class="wrap">';
    599     echo '<h2>', sprintf( __('Installing Plugin: %s'), $api->name . ' ' . $api->version ), '</h2>';
    600 
    601     do_plugin_install($api->download_link, $api);
    602     echo '</div>';
    603 
    604 }
    605 
    606 /**
    607  * Retrieve plugin and install.
    608  *
    609  * @since 2.7.0
    610  *
    611  * @param string $download_url Download URL.
    612  * @param object $plugin_information Optional. Plugin information
    613  */
    614 function do_plugin_install($download_url, $plugin_information = null) {
    615     global $wp_filesystem;
    616 
    617     if ( empty($download_url) ) {
    618         show_message( __('No plugin Specified') );
    619         return;
    620     }
    621 
    622     $plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
    623 
    624     $url = 'plugin-install.php?tab=install';
    625     $url = add_query_arg(array('plugin' => $plugin, 'plugin_name' => stripslashes( $_REQUEST['plugin_name'] ), 'download_url' => stripslashes( $_REQUEST['download_url'] ) ), $url);
    626 
    627     $url = wp_nonce_url($url, 'install-plugin_' . $plugin);
    628     if ( false === ($credentials = request_filesystem_credentials($url)) )
    629         return;
    630 
    631     if ( ! WP_Filesystem($credentials) ) {
    632         request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
    633         return;
    634     }
    635 
    636     if ( $wp_filesystem->errors->get_error_code() ) {
    637         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    638             show_message($message);
    639         return;
    640     }
    641 
    642     $result = wp_install_plugin( $download_url, 'show_message' );
    643 
    644     if ( is_wp_error($result) ) {
    645         show_message($result);
    646         show_message( __('Installation Failed') );
    647     } else {
    648         show_message( sprintf(__('Successfully installed the plugin <strong>%s %s</strong>.'), $plugin_information->name, $plugin_information->version) );
    649         $plugin_file = $result;
    650 
    651         $install_actions = apply_filters('install_plugin_complete_actions', array(
    652             'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . attribute_escape(__('Activate this plugin')) . '" target="_parent">' . __('Activate Plugin') . '</a>',
    653             'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
    654                             ), $plugin_information, $plugin_file);
    655         if ( ! empty($install_actions) )
    656             show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
    657     }
    658 }
    659 
    660 /**
    661  * Install a plugin from a local file.
    662  *
    663  * @since 2.7.0
    664  *
    665  * @param string $package Local Plugin zip
    666  * @param string $filename Optional. Original filename
    667  * @param object $plugin_information Optional. Plugin information
    668  */
    669 function do_plugin_install_local_package($package, $filename = '') {
    670     global $wp_filesystem;
    671 
    672     if ( empty($package) ) {
    673         show_message( __('No plugin Specified') );
    674         return;
    675     }
    676 
    677     if ( empty($filename) )
    678         $filename = basename($package);
    679 
    680     $url = 'plugin-install.php?tab=upload';
    681     $url = add_query_arg(array('package' => $filename), $url);
    682 
    683     $url = wp_nonce_url($url, 'plugin-upload');
    684     if ( false === ($credentials = request_filesystem_credentials($url)) )
    685         return;
    686 
    687     if ( ! WP_Filesystem($credentials) ) {
    688         request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
    689         return;
    690     }
    691 
    692     if ( $wp_filesystem->errors->get_error_code() ) {
    693         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    694             show_message($message);
    695         return;
    696     }
    697 
    698     $result = wp_install_plugin_local_package( $package, 'show_message' );
    699 
    700     if ( is_wp_error($result) ) {
    701         show_message($result);
    702         show_message( __('Installation Failed') );
    703     } else {
    704         show_message( __('Successfully installed the plugin.') );
    705         $plugin_file = $result;
    706 
    707         $install_actions = apply_filters('install_plugin_complete_actions', array(
    708                             'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . __('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
    709                             'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . __('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
    710                             ), array(), $plugin_file);
    711         if ( ! empty($install_actions) )
    712             show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
    713     }
    714 }
    715 
    716 /**
    717  * Install plugin.
    718  *
    719  * @since 2.7.0
    720  *
    721  * @param string $package
    722  * @param string $feedback Optional.
    723  * @return mixed.
    724  */
    725 function wp_install_plugin($package, $feedback = '') {
    726     global $wp_filesystem;
    727 
    728     if ( !empty($feedback) )
    729         add_filter('install_feedback', $feedback);
    730 
    731     // Is a filesystem accessor setup?
    732     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    733         WP_Filesystem();
    734 
    735     if ( ! is_object($wp_filesystem) )
    736         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    737 
    738     if ( $wp_filesystem->errors->get_error_code() )
    739         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    740 
    741     //Get the base plugin folder
    742     $plugins_dir = $wp_filesystem->wp_plugins_dir();
    743     if ( empty($plugins_dir) )
    744         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
    745 
    746     //And the same for the Content directory.
    747     $content_dir = $wp_filesystem->wp_content_dir();
    748     if( empty($content_dir) )
    749         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    750 
    751     $plugins_dir = trailingslashit( $plugins_dir );
    752     $content_dir = trailingslashit( $content_dir );
    753 
    754     if ( empty($package) )
    755         return new WP_Error('no_package', __('Install package not available.'));
    756 
    757     // Download the package
    758     apply_filters('install_feedback', sprintf(__('Downloading plugin package from %s'), $package));
    759     $download_file = download_url($package);
    760 
    761     if ( is_wp_error($download_file) )
    762         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    763 
    764     $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
    765 
    766     // Clean up working directory
    767     if ( $wp_filesystem->is_dir($working_dir) )
    768         $wp_filesystem->delete($working_dir, true);
    769 
    770     apply_filters('install_feedback', __('Unpacking the plugin package'));
    771     // Unzip package to working directory
    772     $result = unzip_file($download_file, $working_dir);
    773 
    774     // Once extracted, delete the package
    775     @unlink($download_file);
    776 
    777     if ( is_wp_error($result) ) {
    778         $wp_filesystem->delete($working_dir, true);
    779         return $result;
    780     }
    781 
    782     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    783     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    784 
    785     if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    786         $wp_filesystem->delete($working_dir, true);
    787         return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
    788     }
    789 
    790     apply_filters('install_feedback', __('Installing the plugin'));
    791     // Copy new version of plugin into place.
    792     $result = copy_dir($working_dir, $plugins_dir);
    793     if ( is_wp_error($result) ) {
    794         $wp_filesystem->delete($working_dir, true);
    795         return $result;
    796     }
    797 
    798     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    799     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    800 
    801     // Remove working directory
    802     $wp_filesystem->delete($working_dir, true);
    803 
    804     if( empty($filelist) )
    805         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    806 
    807     $folder = $filelist[0];
    808     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    809     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    810 
    811     //Return the plugin files name.
    812     return  $folder . '/' . $pluginfiles[0];
    813 }
    814 
    815 /**
    816  * Install plugin from local package
    817  *
    818  * @since 2.7.0
    819  *
    820  * @param string $package
    821  * @param string $feedback Optional.
    822  * @return mixed.
    823  */
    824 function wp_install_plugin_local_package($package, $feedback = '') {
    825     global $wp_filesystem;
    826 
    827     if ( !empty($feedback) )
    828         add_filter('install_feedback', $feedback);
    829 
    830     // Is a filesystem accessor setup?
    831     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    832         WP_Filesystem();
    833 
    834     if ( ! is_object($wp_filesystem) )
    835         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    836 
    837     if ( $wp_filesystem->errors->get_error_code() )
    838         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    839 
    840     //Get the base plugin folder
    841     $plugins_dir = $wp_filesystem->wp_plugins_dir();
    842     if ( empty($plugins_dir) )
    843         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
    844 
    845     //And the same for the Content directory.
    846     $content_dir = $wp_filesystem->wp_content_dir();
    847     if( empty($content_dir) )
    848         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    849 
    850     $plugins_dir = trailingslashit( $plugins_dir );
    851     $content_dir = trailingslashit( $content_dir );
    852 
    853     if ( empty($package) )
    854         return new WP_Error('no_package', __('Install package not available.'));
    855 
    856     $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
    857 
    858     // Clean up working directory
    859     if ( $wp_filesystem->is_dir($working_dir) )
    860         $wp_filesystem->delete($working_dir, true);
    861 
    862     apply_filters('install_feedback', __('Unpacking the plugin package'));
    863     // Unzip package to working directory
    864     $result = unzip_file($package, $working_dir);
    865 
    866     // Once extracted, delete the package
    867     unlink($package);
    868 
    869     if ( is_wp_error($result) ) {
    870         $wp_filesystem->delete($working_dir, true);
    871         return $result;
    872     }
    873 
    874     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    875     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    876 
    877     if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
    878         $wp_filesystem->delete($working_dir, true);
    879         return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
    880     }
    881 
    882     apply_filters('install_feedback', __('Installing the plugin'));
    883     // Copy new version of plugin into place.
    884     $result = copy_dir($working_dir, $plugins_dir);
    885     if ( is_wp_error($result) ) {
    886         $wp_filesystem->delete($working_dir, true);
    887         return $result;
    888     }
    889 
    890     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    891     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    892 
    893     // Remove working directory
    894     $wp_filesystem->delete($working_dir, true);
    895 
    896     if( empty($filelist) )
    897         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    898 
    899     $folder = $filelist[0];
    900     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    901     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    902 
    903     //Return the plugin files name.
    904     return  $folder . '/' . $pluginfiles[0];
    905 }
    906 
    907 ?>
  • trunk/wp-admin/includes/plugin.php

    r11002 r11005  
    217217    // Files in wp-content/plugins directory
    218218    $plugins_dir = @ opendir( $plugin_root);
     219    $plugin_files = array();
    219220    if ( $plugins_dir ) {
    220221        while (($file = readdir( $plugins_dir ) ) !== false ) {
     
    240241    @closedir( $plugins_subdir );
    241242
    242     if ( !$plugins_dir || !$plugin_files )
     243    if ( !$plugins_dir || empty($plugin_files) )
    243244        return $wp_plugins;
    244245
     
    433434    }
    434435
    435     if ( $wp_filesystem->errors->get_error_code() ) {
    436         return $wp_filesystem->errors;
    437     }
    438 
    439436    if ( ! is_object($wp_filesystem) )
    440437        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    441438
    442     if ( $wp_filesystem->errors->get_error_code() )
     439    if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
    443440        return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    444441
  • trunk/wp-admin/includes/theme-install.php

    r10943 r11005  
    278278<h4><?php _e('Install a theme in .zip format') ?></h4>
    279279<p class="install-help"><?php _e('If you have a theme in a .zip format, you may install it by uploading it here.') ?></p>
    280 <form method="post" enctype="multipart/form-data" action="<?php echo admin_url('theme-install.php?tab=do_upload') ?>">
     280<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-theme') ?>">
    281281    <?php wp_nonce_field( 'theme-upload') ?>
    282282    <input type="file" name="themezip" />
    283     <input type="submit" class="button" value="<?php _e('Install Now') ?>" />
     283    <input type="submit"
     284    class="button" value="<?php _e('Install Now') ?>" />
    284285</form>
    285 <?php
     286    <?php
    286287}
    287288
     
    412413    <tr>
    413414    <?php
     415
    414416    foreach ( $cols as $col => $theme_index ) {
    415417        $class = array('available-theme');
     
    512514case 'install':
    513515    if ( current_user_can('install_themes') ) :
    514     $buttons .= '<a class="button-primary" id="install" href="' . wp_nonce_url(admin_url('theme-install.php?tab=install&theme=' . $api->slug), 'install-theme_' . $api->slug) . '" target="_parent">' . __('Install Now') . '</a>';
     516    $buttons .= '<a class="button-primary" id="install" href="' . wp_nonce_url(admin_url('update.php?action=install-theme&theme=' . $api->slug), 'install-theme_' . $api->slug) . '" target="_parent">' . __('Install Now') . '</a>';
    515517    endif;
    516518    break;
     
    543545    exit;
    544546}
    545 
    546 add_action('install_themes_do_upload', 'upload_theme');
    547 function upload_theme() {
    548 
    549     if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
    550         wp_die($uploads['error']);
    551 
    552     if ( !empty($_FILES) )
    553         $filename = $_FILES['themezip']['name'];
    554     else if ( isset($_GET['package']) )
    555         $filename = $_GET['package'];
    556 
    557     check_admin_referer('theme-upload');
    558 
    559     echo '<div class="wrap">';
    560     echo '<h2>', sprintf( __('Installing theme from file: %s'), basename($filename) ), '</h2>';
    561 
    562     //Handle a newly uploaded file, Else assume it was
    563     if ( !empty($_FILES) ) {
    564         $filename = wp_unique_filename( $uploads['basedir'], $filename );
    565         $local_file = $uploads['basedir'] . '/' . $filename;
    566 
    567         // Move the file to the uploads dir
    568         if ( false === @ move_uploaded_file( $_FILES['themezip']['tmp_name'], $local_file) )
    569             wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
    570     } else {
    571         $local_file = $uploads['basedir'] . '/' . $filename;
    572     }
    573 
    574     do_theme_install_local_package($local_file, $filename);
    575     echo '</div>';
    576 }
    577 
    578 add_action('install_themes_install', 'install_theme');
    579 
    580 /**
    581  * Display theme link and execute install.
    582  *
    583  * @since 2.8.0
    584  */
    585 function install_theme() {
    586 
    587     $theme = isset($_REQUEST['theme']) ? stripslashes( $_REQUEST['theme'] ) : '';
    588 
    589     check_admin_referer('install-theme_' . $theme);
    590     $api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
    591 
    592     if ( is_wp_error($api) )
    593         wp_die($api);
    594 
    595     echo '<div class="wrap">';
    596     echo '<h2>', sprintf( __('Installing theme: %s'), $api->name . ' ' . $api->version ), '</h2>';
    597 
    598     do_theme_install($api->download_link, $api);
    599     echo '</div>';
    600 
    601 }
    602 
    603 /**
    604  * Retrieve theme and install.
    605  *
    606  * @since 2.8.0
    607  *
    608  * @param string $download_url Download URL.
    609  * @param object $theme_information Optional. Theme information
    610  */
    611 function do_theme_install($download_url, $theme_information = null) {
    612     global $wp_filesystem;
    613 
    614     if ( empty($download_url) ) {
    615         show_message( __('No theme specified') );
    616         return;
    617     }
    618 
    619     $theme        = isset($_REQUEST['theme'])        ? stripslashes( $_REQUEST['theme'] )        : '';
    620     $theme_name   = isset($_REQUEST['theme_name'])   ? stripslashes( $_REQUEST['theme_name'] )   : '';
    621 
    622     $url = 'theme-install.php?tab=install';
    623     $url = add_query_arg(array('theme' => $theme, 'theme_name' => $theme_name, 'download_url' => $download_url ), $url);
    624 
    625     $url = wp_nonce_url($url, 'install-theme_' . $theme);
    626     if ( false === ($credentials = request_filesystem_credentials($url)) )
    627         return;
    628 
    629     if ( ! WP_Filesystem($credentials) ) {
    630         request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
    631         return;
    632     }
    633 
    634     if ( $wp_filesystem->errors->get_error_code() ) {
    635         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    636         show_message($message);
    637         return;
    638     }
    639 
    640     $result = wp_install_theme( $download_url, 'show_message' );
    641 
    642     if ( is_wp_error($result) ) {
    643         show_message($result);
    644         show_message( __('Installation Failed') );
    645     } else {
    646         show_message( sprintf(__('Successfully installed the theme <strong>%s %s</strong>.'), $theme_information->name, $theme_information->version) );
    647         $theme_file = $result;
    648 
    649         $install_actions = apply_filters('install_theme_complete_actions', array(
    650         //'activate_theme' => '<a href="' . wp_nonce_url('themes.php?action=activate&amp;theme=' . $theme_file, 'activate-theme_' . $theme_file) . '" title="' . attribute_escape(__('Activate this theme')) . '" target="_parent">' . __('Activate Theme') . '</a>',
    651             'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . attribute_escape(__('Return to Themes page')) . '" target="_parent">' . __('Return to Themes page') . '</a>'
    652             ), $theme_information, $theme_file);
    653             if ( ! empty($install_actions) )
    654             show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
    655     }
    656 }
    657 
    658 /**
    659  * Install a theme from a local file.
    660  *
    661  * @since 2.8.0
    662  *
    663  * @param string $package Local Theme zip
    664  * @param string $filename Optional. Original filename
    665  * @param object $theme_information Optional. Theme information
    666  */
    667 function do_theme_install_local_package($package, $filename = '') {
    668     global $wp_filesystem;
    669 
    670     if ( empty($package) ) {
    671         show_message( __('No theme specified') );
    672         return;
    673     }
    674 
    675     if ( empty($filename) )
    676         $filename = basename($package);
    677 
    678     $url = 'theme-install.php?tab=upload';
    679     $url = add_query_arg(array('package' => $filename), $url);
    680 
    681     $url = wp_nonce_url($url, 'theme-upload');
    682     if ( false === ($credentials = request_filesystem_credentials($url)) )
    683         return;
    684 
    685     if ( ! WP_Filesystem($credentials) ) {
    686         request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
    687         return;
    688     }
    689 
    690     if ( $wp_filesystem->errors->get_error_code() ) {
    691         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    692         show_message($message);
    693         return;
    694     }
    695 
    696     $result = wp_install_theme_local_package( $package, 'show_message' );
    697 
    698     if ( is_wp_error($result) ) {
    699         show_message($result);
    700         show_message( __('Installation Failed') );
    701     } else {
    702         show_message( __('Successfully installed the theme.') );
    703         $theme_file = $result;
    704 
    705         $install_actions = apply_filters('install_theme_complete_actions', array(
    706         //'activate_theme' => '<a href="' . wp_nonce_url('themes.php?action=activate&amp;theme=' . $theme_file, 'activate-theme_' . $theme_file) . '" title="' . __('Activate this theme') . '" target="_parent">' . __('Activate Theme') . '</a>',
    707                             'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . __('Goto themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>'
    708                             ), array(), $theme_file);
    709                             if ( ! empty($install_actions) )
    710                             show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
    711     }
    712 }
    713 
    714 /**
    715  * Install theme.
    716  *
    717  * @since 2.8.0
    718  *
    719  * @param string $package
    720  * @param string $feedback Optional.
    721  * @return mixed.
    722  */
    723 function wp_install_theme($package, $feedback = '') {
    724     global $wp_filesystem;
    725 
    726     if ( !empty($feedback) )
    727         add_filter('install_feedback', $feedback);
    728 
    729     // Is a filesystem accessor setup?
    730     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    731         WP_Filesystem();
    732 
    733     if ( ! is_object($wp_filesystem) )
    734         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    735 
    736     if ( $wp_filesystem->errors->get_error_code() )
    737         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    738 
    739     // Get the base theme folder
    740     $themes_dir = $wp_filesystem->wp_themes_dir();
    741     if ( empty($themes_dir) )
    742         return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress themes directory.'));
    743 
    744     // And the same for the Content directory.
    745     $content_dir = $wp_filesystem->wp_content_dir();
    746     if ( empty($content_dir) )
    747         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress content directory (wp-content).'));
    748 
    749     $themes_dir = trailingslashit( $themes_dir );
    750     $content_dir = trailingslashit( $content_dir );
    751 
    752     if ( empty($package) )
    753         return new WP_Error('no_package', __('Install package not available.'));
    754 
    755     // Download the package
    756     apply_filters('install_feedback', sprintf(__('Downloading theme package from %s'), $package));
    757     $download_file = download_url($package);
    758 
    759     if ( is_wp_error($download_file) )
    760         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    761 
    762     $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
    763 
    764     // Clean up working directory
    765     if ( $wp_filesystem->is_dir($working_dir) )
    766         $wp_filesystem->delete($working_dir, true);
    767 
    768     apply_filters('install_feedback', __('Unpacking the theme package'));
    769     // Unzip package to working directory
    770     $result = unzip_file($download_file, $working_dir);
    771 
    772     // Once extracted, delete the package
    773     @unlink($download_file);
    774 
    775     if ( is_wp_error($result) ) {
    776         $wp_filesystem->delete($working_dir, true);
    777         return $result;
    778     }
    779 
    780     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
    781     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    782 
    783     if ( $wp_filesystem->exists( $themes_dir . $filelist[0] ) ) {
    784         $wp_filesystem->delete($working_dir, true);
    785         return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
    786     }
    787 
    788     apply_filters('install_feedback', __('Installing the theme'));
    789     // Copy new version of theme into place.
    790     $result = copy_dir($working_dir, $themes_dir);
    791     if ( is_wp_error($result) ) {
    792         $wp_filesystem->delete($working_dir, true);
    793         return $result;
    794     }
    795 
    796     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
    797     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    798 
    799     // Remove working directory
    800     $wp_filesystem->delete($working_dir, true);
    801 
    802     if ( empty($filelist) )
    803         return false; //We couldnt find any files in the working dir, therefor no theme installed? Failsafe backup.
    804 
    805     //TODO: TODO: TODO
    806     $stylesheet = $filelist[0];
    807     //  $theme = get_themes('/' . $folder); //Ensure to pass with leading slash //TODO: TODO: TODO
    808     //  $themefiles = array_keys($theme); //Assume the requested theme is the first in the list
    809 
    810     //Return the theme files name.
    811     return  $stylesheet; //$folder . '/' . $themefiles[0];
    812 }
    813 
    814 /**
    815  * Install theme from local package
    816  *
    817  * @since 2.8.0
    818  *
    819  * @param string $package
    820  * @param string $feedback Optional.
    821  * @return mixed.
    822  */
    823 function wp_install_theme_local_package($package, $feedback = '') {
    824     global $wp_filesystem;
    825 
    826     if ( !empty($feedback) )
    827         add_filter('install_feedback', $feedback);
    828 
    829     // Is a filesystem accessor setup?
    830     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    831         WP_Filesystem();
    832 
    833     if ( ! is_object($wp_filesystem) )
    834         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    835 
    836     if ( $wp_filesystem->errors->get_error_code() )
    837         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    838 
    839     //Get the base theme folder
    840     $themes_dir = $wp_filesystem->wp_themes_dir();
    841     if ( empty($themes_dir) )
    842         return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress themes directory.'));
    843 
    844     //And the same for the Content directory.
    845     $content_dir = $wp_filesystem->wp_content_dir();
    846     if ( empty($content_dir) )
    847         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress content directory (wp-content).'));
    848 
    849     $themes_dir = trailingslashit( $themes_dir );
    850     $content_dir = trailingslashit( $content_dir );
    851 
    852     if ( empty($package) )
    853         return new WP_Error('no_package', __('Install package not available.'));
    854 
    855     $working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
    856 
    857 
    858     // Clean up working directory
    859     if ( $wp_filesystem->is_dir($working_dir) )
    860         $wp_filesystem->delete($working_dir, true);
    861 
    862     apply_filters('install_feedback', __('Unpacking the theme package'));
    863     // Unzip package to working directory
    864     $result = unzip_file($package, $working_dir);
    865 
    866     // Once extracted, delete the package
    867     unlink($package);
    868 
    869     if ( is_wp_error($result) ) {
    870         $wp_filesystem->delete($working_dir, true);
    871         return $result;
    872     }
    873 
    874     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
    875     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    876 
    877     if ( $wp_filesystem->exists( $themes_dir . $filelist[0] ) ) {
    878         $wp_filesystem->delete($working_dir, true);
    879         return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
    880     }
    881 
    882     apply_filters('install_feedback', __('Installing the theme'));
    883     // Copy new version of theme into place.
    884     $result = copy_dir($working_dir, $themes_dir);
    885     if ( is_wp_error($result) ) {
    886         $wp_filesystem->delete($working_dir, true);
    887         return $result;
    888     }
    889 
    890     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
    891     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    892 
    893     // Remove working directory
    894     $wp_filesystem->delete($working_dir, true);
    895 
    896     if ( empty($filelist) )
    897         return false; //We couldnt find any files in the working dir, therefor no theme installed? Failsafe backup.
    898 
    899     //TODO TODO TODO
    900     $stylesheet = $filelist[0];
    901     //  $theme = get_themes('/' . $folder); //Ensure to pass with leading slash
    902     //  $themefiles = array_keys($theme); //Assume the requested theme is the first in the list
    903 
    904     //Return the theme files name.
    905     return  $stylsheet; //$folder . '/' . $themefiles[0];
    906 }
  • trunk/wp-admin/includes/theme.php

    r10951 r11005  
    7373    }
    7474
    75     if ( $wp_filesystem->errors->get_error_code() ) {
    76         return $wp_filesystem->errors;
    77     }
    7875
    7976    if ( ! is_object($wp_filesystem) )
    8077        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    8178
    82     if ( $wp_filesystem->errors->get_error_code() )
     79    if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
    8380        return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    8481
  • trunk/wp-admin/includes/update-core.php

    r10551 r11005  
    230230    if ( is_wp_error($result) ) {
    231231        $wp_filesystem->delete($maintenance_file);
    232         //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
     232        $wp_filesystem->delete($working_dir, true);
    233233        return $result;
    234234    }
  • trunk/wp-admin/includes/update.php

    r10939 r11005  
    168168
    169169function wp_update_plugin($plugin, $feedback = '') {
    170     global $wp_filesystem;
    171170
    172171    if ( !empty($feedback) )
    173172        add_filter('update_feedback', $feedback);
    174173
    175     // Is an update available?
    176     $current = get_transient( 'update_plugins' );
    177     if ( !isset( $current->response[ $plugin ] ) )
    178         return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
    179 
    180     // Is a filesystem accessor setup?
    181     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    182         WP_Filesystem();
    183 
    184     if ( ! is_object($wp_filesystem) )
    185         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    186 
    187     if ( $wp_filesystem->errors->get_error_code() )
    188         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    189 
    190     //Get the base plugin folder
    191     $plugins_dir = $wp_filesystem->wp_plugins_dir();
    192     if ( empty($plugins_dir) )
    193         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
    194 
    195     //And the same for the Content directory.
    196     $content_dir = $wp_filesystem->wp_content_dir();
    197     if( empty($content_dir) )
    198         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    199 
    200     $plugins_dir = trailingslashit( $plugins_dir );
    201     $content_dir = trailingslashit( $content_dir );
    202 
    203     // Get the URL to the zip file
    204     $r = $current->response[ $plugin ];
    205 
    206     if ( empty($r->package) )
    207         return new WP_Error('no_package', __('Upgrade package not available.'));
    208 
    209     // Download the package
    210     $package = $r->package;
    211     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
    212     $download_file = download_url($package);
    213 
    214     if ( is_wp_error($download_file) )
    215         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    216 
    217     $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
    218 
    219     // Clean up working directory
    220     if ( $wp_filesystem->is_dir($working_dir) )
    221         $wp_filesystem->delete($working_dir, true);
    222 
    223     apply_filters('update_feedback', __('Unpacking the update'));
    224     // Unzip package to working directory
    225     $result = unzip_file($download_file, $working_dir);
    226 
    227     // Once extracted, delete the package
    228     unlink($download_file);
    229 
    230     if ( is_wp_error($result) ) {
    231         $wp_filesystem->delete($working_dir, true);
    232         return $result;
    233     }
    234 
    235     if ( is_plugin_active($plugin) ) {
    236         //Deactivate the plugin silently, Prevent deactivation hooks from running.
    237         apply_filters('update_feedback', __('Deactivating the plugin'));
    238         deactivate_plugins($plugin, true);
    239     }
    240 
    241     // Remove the existing plugin.
    242     apply_filters('update_feedback', __('Removing the old version of the plugin'));
    243     $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
    244 
    245     // If plugin is in its own directory, recursively delete the directory.
    246     if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
    247         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
    248     else
    249         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
    250 
    251     if ( ! $deleted ) {
    252         $wp_filesystem->delete($working_dir, true);
    253         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
    254     }
    255 
    256     apply_filters('update_feedback', __('Installing the latest version'));
    257     // Copy new version of plugin into place.
    258     $result = copy_dir($working_dir, $plugins_dir);
    259     if ( is_wp_error($result) ) {
    260         $wp_filesystem->delete($working_dir, true);
    261         return $result;
    262     }
    263 
    264     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    265     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    266 
    267     // Remove working directory
    268     $wp_filesystem->delete($working_dir, true);
    269 
    270     // Force refresh of plugin update information
    271     delete_transient('update_plugins');
    272 
    273     if( empty($filelist) )
    274         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    275 
    276     $folder = $filelist[0];
    277     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    278     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    279 
    280     return  $folder . '/' . $pluginfiles[0];
     174    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     175    $upgrader = new Plugin_Upgrader();
     176    return $upgrader->upgrade($plugin);
    281177}
    282178
    283179function wp_update_theme($theme, $feedback = '') {
    284     global $wp_filesystem;
    285 
     180   
    286181    if ( !empty($feedback) )
    287182        add_filter('update_feedback', $feedback);
    288183
    289     // Is an update available?
    290     $current = get_transient( 'update_themes' );
    291     if ( !isset( $current->response[ $theme ] ) )
    292         return new WP_Error('up_to_date', __('The theme is at the latest version.'));
    293 
    294     $r = $current->response[ $theme ];
    295 
    296     $themes = get_themes();
    297     foreach ( (array) $themes as $this_theme ) {
    298         if ( $this_theme['Stylesheet'] == $theme ) {
    299             $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
    300             break;
    301         }
    302     }
    303     unset($themes);
    304 
    305     if ( empty($theme_directory) )
    306         return new WP_Error('theme_non_existant', __('Theme does not exist.'));
    307 
    308     // Is a filesystem accessor setup?
    309     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    310         WP_Filesystem();
    311 
    312     if ( ! is_object($wp_filesystem) )
    313         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    314 
    315     if ( $wp_filesystem->errors->get_error_code() )
    316         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    317 
    318     //Get the base plugin folder
    319     $themes_dir = $wp_filesystem->wp_themes_dir();
    320     if ( empty($themes_dir) )
    321         return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
    322 
    323     //And the same for the Content directory.
    324     $content_dir = $wp_filesystem->wp_content_dir();
    325     if( empty($content_dir) )
    326         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    327 
    328     $themes_dir = trailingslashit( $themes_dir );
    329     $content_dir = trailingslashit( $content_dir );
    330 
    331     if ( empty($r->package) )
    332         return new WP_Error('no_package', __('Upgrade package not available.'));
    333 
    334     // Download the package
    335     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
    336     $download_file = download_url($r['package']);
    337 
    338     if ( is_wp_error($download_file) )
    339         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    340 
    341     $working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
    342 
    343     // Clean up working directory
    344     if ( $wp_filesystem->is_dir($working_dir) )
    345         $wp_filesystem->delete($working_dir, true);
    346 
    347     apply_filters('update_feedback', __('Unpacking the update'));
    348     // Unzip package to working directory
    349     $result = unzip_file($download_file, $working_dir);
    350 
    351     // Once extracted, delete the package
    352     unlink($download_file);
    353 
    354     if ( is_wp_error($result) ) {
    355         $wp_filesystem->delete($working_dir, true);
    356         return $result;
    357     }
    358 
    359     //TODO: Is theme currently active? If so, set default theme
    360     /*
    361     if ( is_plugin_active($plugin) ) {
    362         //Deactivate the plugin silently, Prevent deactivation hooks from running.
    363         apply_filters('update_feedback', __('Deactivating the plugin'));
    364         deactivate_plugins($plugin, true);
    365     }*/
    366 
    367     // Remove the existing plugin.
    368     apply_filters('update_feedback', __('Removing the old version of the theme'));
    369     $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
    370 
    371     if ( ! $deleted ) {
    372         $wp_filesystem->delete($working_dir, true);
    373         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
    374     }
    375 
    376     apply_filters('update_feedback', __('Installing the latest version'));
    377     // Copy new version of plugin into place.
    378     $result = copy_dir($working_dir, $themes_dir);
    379     if ( is_wp_error($result) ) {
    380         $wp_filesystem->delete($working_dir, true);
    381         return $result;
    382     }
    383 
    384     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    385     //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    386 
    387     // Remove working directory
    388     $wp_filesystem->delete($working_dir, true);
    389 
    390     // Force refresh of plugin update information
    391     delete_transient('update_themes');
    392 
    393     /*if( empty($filelist) )
    394         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    395 
    396     $folder = $filelist[0];
    397     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    398     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    399 
    400     return  $folder . '/' . $pluginfiles[0];*/
     184    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     185    $upgrader = new Theme_Upgrader();
     186    return $upgrader->upgrade($theme);
    401187}
    402188
    403189
    404190function wp_update_core($current, $feedback = '') {
    405     global $wp_filesystem;
    406 
    407     @set_time_limit( 300 );
    408 
     191   
    409192    if ( !empty($feedback) )
    410193        add_filter('update_feedback', $feedback);
    411194
    412     // Is an update available?
    413     if ( !isset( $current->response ) || $current->response == 'latest' )
    414         return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
    415 
    416     // Is a filesystem accessor setup?
    417     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    418         WP_Filesystem();
    419 
    420     if ( ! is_object($wp_filesystem) )
    421         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    422 
    423     if ( $wp_filesystem->errors->get_error_code() )
    424         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    425 
    426     // Get the base WP folder
    427     $wp_dir = $wp_filesystem->abspath();
    428     if ( empty($wp_dir) )
    429         return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
    430 
    431     // And the same for the Content directory.
    432     $content_dir = $wp_filesystem->wp_content_dir();
    433     if( empty($content_dir) )
    434         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    435 
    436     $wp_dir = trailingslashit( $wp_dir );
    437     $content_dir = trailingslashit( $content_dir );
    438 
    439     // Get the URL to the zip file
    440     $package = $current->package;
    441 
    442     // Download the package
    443     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
    444     $download_file = download_url($package);
    445 
    446     if ( is_wp_error($download_file) )
    447         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    448 
    449     $working_dir = $content_dir . 'upgrade/core';
    450     // Clean up working directory
    451     if ( $wp_filesystem->is_dir($working_dir) ) {
    452         $wp_filesystem->delete($working_dir, true);
    453     }
    454 
    455     apply_filters('update_feedback', __('Unpacking the core update'));
    456     // Unzip package to working directory
    457     $result = unzip_file($download_file, $working_dir);
    458     // Once extracted, delete the package
    459     unlink($download_file);
    460 
    461     if ( is_wp_error($result) ) {
    462         $wp_filesystem->delete($working_dir, true);
    463         return $result;
    464     }
    465 
    466     // Copy update-core.php from the new version into place.
    467     if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
    468         $wp_filesystem->delete($working_dir, true);
    469         return new WP_Error('copy_failed', __('Could not copy files'));
    470     }
    471     $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
    472 
    473     require(ABSPATH . 'wp-admin/includes/update-core.php');
    474 
    475     return update_core($working_dir, $wp_dir);
     195    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     196    $upgrader = new Core_Upgrader();
     197    return $upgrader->upgrade($current);
     198
    476199}
    477200
  • trunk/wp-admin/plugin-install.php

    r10829 r11005  
    2525//These are the tabs which are shown on the page,
    2626$tabs = array();
    27 $tabs['dashboard'] = __('Search'); //TODO: Better name?
     27$tabs['dashboard'] = __('Search');
    2828if ( 'search' == $tab )
    2929    $tabs['search'] = __('Search Results');
     
    3434$tabs['updated']  = __('Recently Updated');
    3535
    36 $nonmenu_tabs = array('install', 'plugin-information', 'do_upload'); //Valid actions to perform which do not have a Menu item.
     36$nonmenu_tabs = array('plugin-information'); //Valid actions to perform which do not have a Menu item.
    3737
    3838$tabs = apply_filters('install_plugins_tabs', $tabs );
     
    6363    $sep = ( end($tabs) != $text ) ? ' | ' : '';
    6464    $class = ( $action == $tab ) ? ' class="current"' : '';
    65     $href = admin_url('plugin-install.php?tab='. $action);
     65    $href = admin_url('plugin-install.php?tab=' . $action);
    6666    echo "\t\t<li><a href='$href'$class>$text</a>$sep</li>\n";
    6767}
     
    7373<?php
    7474include('admin-footer.php');
    75 ?>
  • trunk/wp-admin/theme-install.php

    r10900 r11005  
    3636$tabs['updated']  = __('Recently Updated');
    3737
    38 $nonmenu_tabs = array('install', 'theme-information', 'do_upload'); //Valid actions to perform which do not have a Menu item.
     38$nonmenu_tabs = array('theme-information'); //Valid actions to perform which do not have a Menu item.
    3939
    4040$tabs = apply_filters('install_themes_tabs', $tabs );
  • trunk/wp-admin/update.php

    r10924 r11005  
    11<?php
    22/**
    3  * Update Plugin/Theme administration panel.
     3 * Update/Install Plugin/Theme administration panel.
    44 *
    55 * @package WordPress
     
    1010require_once('admin.php');
    1111
    12 if ( ! current_user_can('update_plugins') )
    13     wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
    14 
    15 /**
    16  * Plugin upgrade display.
    17  *
    18  * @since 2.5
    19  *
    20  * @param string $plugin Plugin
    21  */
    22 function do_plugin_upgrade($plugin) {
    23     global $wp_filesystem;
    24 
    25     $url = wp_nonce_url("update.php?action=upgrade-plugin&plugin=$plugin", "upgrade-plugin_$plugin");
    26     if ( false === ($credentials = request_filesystem_credentials($url)) )
    27         return;
    28 
    29     if ( ! WP_Filesystem($credentials) ) {
    30         $error = true;
    31         if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
    32             $error = $wp_filesystem->errors;
    33         request_filesystem_credentials($url, '', $error); //Failed to connect, Error and request again
    34         return;
    35     }
    36 
    37     echo '<div class="wrap">';
    38     echo screen_icon();
    39     echo '<h2>' . __('Upgrade Plugin') . '</h2>';
    40     if ( $wp_filesystem->errors->get_error_code() ) {
    41         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    42             show_message($message);
    43         echo '</div>';
    44         return;
    45     }
    46 
    47     $was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
    48 
    49     $result = wp_update_plugin($plugin, 'show_message');
    50 
    51     if ( is_wp_error($result) ) {
    52         show_message($result);
    53         show_message( __('Plugin upgrade Failed') );
    54     } else {
    55         $plugin_file = $result;
    56         show_message( __('Plugin upgraded successfully') );
    57         if( $result && $was_activated ){
    58             show_message(__('Attempting reactivation of the plugin'));
    59             echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) .'"></iframe>';
    60         }
    61         $update_actions =  array(
    62             'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . attribute_escape(__('Activate this plugin')) . '" target="_parent">' . __('Activate Plugin') . '</a>',
    63             'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
    64         );
    65         if ( $was_activated )
    66             unset( $update_actions['activate_plugin'] );
    67 
    68         $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $plugin_file);
    69         if ( ! empty($update_actions) )
    70             show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
    71     }
    72     echo '</div>';
    73 }
    74 
    75 /**
    76  * Theme upgrade display.
    77  *
    78  * @since 2.5
    79  *
    80  * @param string $plugin Plugin
    81  */
    82 function do_theme_upgrade($theme) {
    83     global $wp_filesystem;
    84 
    85     $url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($theme), 'upgrade-theme_' . urlencode($theme));
    86     if ( false === ($credentials = request_filesystem_credentials($url)) )
    87         return;
    88 
    89     if ( ! WP_Filesystem($credentials) ) {
    90         $error = true;
    91         if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
    92             $error = $wp_filesystem->errors;
    93         request_filesystem_credentials($url, '', $error); //Failed to connect, Error and request again
    94         return;
    95     }
    96 
    97     echo '<div class="wrap">';
    98     echo screen_icon();
    99     echo '<h2>' . __('Upgrade Theme') . '</h2>';
    100     if ( $wp_filesystem->errors->get_error_code() ) {
    101         foreach ( $wp_filesystem->errors->get_error_messages() as $message )
    102             show_message($message);
    103         echo '</div>';
    104         return;
    105     }
    106 
    107     //TODO: Is theme currently active?
    108     $was_current = false; //is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
    109 
    110     $result = wp_update_theme($theme, 'show_message');
    111 
    112     if ( is_wp_error($result) ) {
    113         show_message($result);
    114         show_message( __('Installation Failed') );
    115     } else {
    116         //Result is the new plugin file relative to WP_PLUGIN_DIR
    117         show_message( __('Theme upgraded successfully') );
    118         if( $result && $was_current ){
    119             show_message(__('Setting theme as Current'));
    120             //TODO: Actually set it as active again.
    121             //echo '<iframe style="border:0" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $result, 'activate-plugin_' . $result) .'"></iframe>';
    122         }
    123     }
    124     echo '</div>';
    125 }
     12include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    12613
    12714if ( isset($_GET['action']) ) {
    128     $plugin = isset($_GET['plugin']) ? trim($_GET['plugin']) : '';
     15    $plugin = isset($_REQUEST['plugin']) ? trim($_REQUEST['plugin']) : '';
    12916    $theme = isset($_REQUEST['theme']) ? urldecode($_REQUEST['theme']) : '';
    130     $action = isset($_GET['action']) ? $_GET['action'] : '';
     17    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
    13118
    13219    if ( 'upgrade-plugin' == $action ) {
     20        if ( ! current_user_can('update_plugins') )
     21            wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
     22
    13323        check_admin_referer('upgrade-plugin_' . $plugin);
     24
    13425        $title = __('Upgrade Plugin');
    13526        $parent_file = 'plugins.php';
    136         require_once('admin-header.php');
    137         do_plugin_upgrade($plugin);
    138         include('admin-footer.php');
     27        $submenu_file = 'plugins.php';
     28        require_once('admin-header.php');
     29
     30        $nonce = 'upgrade-plugin_' . $plugin;
     31        $url = 'update.php?action=upgrade-plugin&plugin=' . $plugin;
     32
     33        $upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( compact('title', 'nonce', 'url', 'plugin') ) );
     34        $upgrader->upgrade($plugin);
     35
     36        include('admin-footer.php');
     37       
    13938    } elseif ('activate-plugin' == $action ) {
     39        if ( ! current_user_can('update_plugins') )
     40            wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
     41
    14042        check_admin_referer('activate-plugin_' . $plugin);
    14143        if( ! isset($_GET['failure']) && ! isset($_GET['success']) ) {
     
    15658        }
    15759        iframe_footer();
     60    } elseif ( 'install-plugin' == $action ) {
     61
     62        if ( ! current_user_can('install_plugins') )
     63            wp_die(__('You do not have sufficient permissions to install plugins for this blog.'));
     64
     65        include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; //for plugins_api..
     66   
     67        check_admin_referer('install-plugin_' . $plugin);
     68        $api = plugins_api('plugin_information', array('slug' => $plugin, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
     69   
     70        if ( is_wp_error($api) )
     71            wp_die($api);
     72   
     73        $title = __('Plugin Install');
     74        $parent_file = 'plugins.php';
     75        $submenu_file = 'plugin-install.php';
     76        require_once('admin-header.php');
     77   
     78        $title = sprintf( __('Installing Plugin: %s'), $api->name . ' ' . $api->version );
     79        $nonce = 'install-plugin_' . $plugin;
     80        $url = add_query_arg( array(
     81                                'plugin' => $plugin,
     82                                'plugin_name' => $api->name . ' ' . $api->version,
     83                                'download_url' => $api->download_link
     84                            ), 'update.php?action=install-plugin');
     85        $type = 'web'; //Install plugin type, From Web or an Upload.
     86
     87        $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api') ) );
     88        $upgrader->install($api->download_link);
     89       
     90        include('admin-footer.php');
     91
     92    } elseif ( 'upload-plugin' == $action ) {
     93
     94        if ( ! current_user_can('install_plugins') )
     95            wp_die(__('You do not have sufficient permissions to install plugins for this blog.'));
     96
     97        check_admin_referer('plugin-upload');
     98
     99        $file_upload = new File_Upload_Upgrader('pluginzip', 'package');
     100
     101        $title = __('Upload Plugin');
     102        $parent_file = 'plugins.php';
     103        $submenu_file = 'plugin-install.php';
     104        require_once('admin-header.php');
     105       
     106        $title = sprintf( __('Installing Plugin from uploaded file: %s'), basename( $file_upload->filename ) );
     107        $nonce = 'plugin-upload';
     108        $url = add_query_arg(array('package' => $file_upload->filename ), 'update.php?action=upload-plugin');
     109        $type = 'upload'; //Install plugin type, From Web or an Upload.
     110
     111        $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('type', 'title', 'nonce', 'url') ) );
     112        $upgrader->install( $file_upload->package );
     113
     114        include('admin-footer.php');
     115
    158116    } elseif ( 'upgrade-theme' == $action ) {
     117
     118        if ( ! current_user_can('update_themes') )
     119            wp_die(__('You do not have sufficient permissions to update themes for this blog.'));
     120
    159121        check_admin_referer('upgrade-theme_' . $theme);
     122
     123        add_thickbox();
     124        wp_enqueue_script('theme-preview');
    160125        $title = __('Upgrade Theme');
    161126        $parent_file = 'themes.php';
    162         require_once('admin-header.php');
    163         do_theme_upgrade($theme);
    164         include('admin-footer.php');
     127        $submenu_file = 'themes.php';
     128        require_once('admin-header.php');
     129
     130        $nonce = 'upgrade-theme_' . $theme;
     131        $url = 'update.php?action=upgrade-theme&theme=' . $theme;
     132
     133        $upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( compact('title', 'nonce', 'url', 'theme') ) );
     134        $upgrader->upgrade($theme);
     135
     136        include('admin-footer.php');
     137   
     138    } elseif ( 'install-theme' == $action ) {
     139
     140        if ( ! current_user_can('install_themes') )
     141            wp_die(__('You do not have sufficient permissions to install themes for this blog.'));
     142
     143        include_once ABSPATH . 'wp-admin/includes/theme-install.php'; //for themes_api..
     144   
     145        check_admin_referer('install-theme_' . $theme);
     146        $api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
     147
     148        if ( is_wp_error($api) )
     149            wp_die($api);
     150
     151        add_thickbox();
     152        wp_enqueue_script('theme-preview');
     153        $title = __('Install Themes');
     154        $parent_file = 'themes.php';
     155        $submenu_file = 'theme-install.php';
     156        require_once('admin-header.php');
     157   
     158        $title = sprintf( __('Installing theme: %s'), $api->name . ' ' . $api->version );
     159        $nonce = 'install-theme_' . $theme;
     160        $url = add_query_arg( array(
     161                                'theme' => $theme,
     162                                'theme_name' => $api->name . ' ' . $api->version,
     163                                'download_url' => $api->download_link
     164                            ), 'update.php?action=install-theme');
     165        $type = 'web'; //Install theme type, From Web or an Upload.
     166   
     167        $upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api') ) );
     168        $upgrader->install($api->download_link);
     169       
     170        include('admin-footer.php');
     171       
     172    } elseif ( 'upload-theme' == $action ) {
     173
     174        if ( ! current_user_can('install_themes') )
     175            wp_die(__('You do not have sufficient permissions to install themes for this blog.'));
     176
     177        check_admin_referer('theme-upload');
     178
     179        $file_upload = new File_Upload_Upgrader('themezip', 'package');
     180
     181        $title = __('Upload Theme');
     182        $parent_file = 'themes.php';
     183        $submenu_file = 'theme-install.php';
     184        add_thickbox();
     185        wp_enqueue_script('theme-preview');
     186        require_once('admin-header.php');
     187
     188        $title = sprintf( __('Installing Theme from uploaded file: %s'), basename( $file_upload->filename ) );
     189        $nonce = 'theme-upload';
     190        $url = add_query_arg(array('package' => $file_upload->filename), 'update.php?action=upload-theme');
     191        $type = 'upload'; //Install plugin type, From Web or an Upload.
     192
     193        $upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('type', 'title', 'nonce', 'url') ) );
     194        $upgrader->install( $file_upload->package );
     195
     196        include('admin-footer.php');
     197
     198    } else {
     199        do_action('update-custom_' . $action);
    165200    }
    166201}
    167 
    168 ?>
Note: See TracChangeset for help on using the changeset viewer.