Make WordPress Core


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 ?>
Note: See TracChangeset for help on using the changeset viewer.