Changeset 11005 for trunk/wp-admin/includes/theme-install.php
- Timestamp:
- 04/19/2009 07:36:28 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/theme-install.php
r10943 r11005 278 278 <h4><?php _e('Install a theme in .zip format') ?></h4> 279 279 <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') ?>"> 281 281 <?php wp_nonce_field( 'theme-upload') ?> 282 282 <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') ?>" /> 284 285 </form> 285 <?php286 <?php 286 287 } 287 288 … … 412 413 <tr> 413 414 <?php 415 414 416 foreach ( $cols as $col => $theme_index ) { 415 417 $class = array('available-theme'); … … 512 514 case 'install': 513 515 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>'; 515 517 endif; 516 518 break; … … 543 545 exit; 544 546 } 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 was563 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 dir568 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.0584 */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.0607 *608 * @param string $download_url Download URL.609 * @param object $theme_information Optional. Theme information610 */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 again631 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&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.0662 *663 * @param string $package Local Theme zip664 * @param string $filename Optional. Original filename665 * @param object $theme_information Optional. Theme information666 */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 again687 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&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.0718 *719 * @param string $package720 * @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 folder740 $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 package756 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 directory765 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 directory770 $result = unzip_file($download_file, $working_dir);771 772 // Once extracted, delete the package773 @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 theme781 $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 theme797 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );798 799 // Remove working directory800 $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: TODO806 $stylesheet = $filelist[0];807 // $theme = get_themes('/' . $folder); //Ensure to pass with leading slash //TODO: TODO: TODO808 // $themefiles = array_keys($theme); //Assume the requested theme is the first in the list809 810 //Return the theme files name.811 return $stylesheet; //$folder . '/' . $themefiles[0];812 }813 814 /**815 * Install theme from local package816 *817 * @since 2.8.0818 *819 * @param string $package820 * @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 folder840 $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 directory859 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 directory864 $result = unzip_file($package, $working_dir);865 866 // Once extracted, delete the package867 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 theme875 $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 theme891 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );892 893 // Remove working directory894 $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 TODO900 $stylesheet = $filelist[0];901 // $theme = get_themes('/' . $folder); //Ensure to pass with leading slash902 // $themefiles = array_keys($theme); //Assume the requested theme is the first in the list903 904 //Return the theme files name.905 return $stylsheet; //$folder . '/' . $themefiles[0];906 }
Note: See TracChangeset
for help on using the changeset viewer.