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/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 }
Note: See TracChangeset for help on using the changeset viewer.