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

git-svn-id: http://svn.automattic.com/wordpress/trunk@11005 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-04-19 19:36:28 +00:00
parent c72dd79062
commit 1cd542a5e2
13 changed files with 1176 additions and 1182 deletions

View File

@ -17,7 +17,7 @@
class WP_Filesystem_FTPext extends WP_Filesystem_Base {
var $link;
var $timeout = 5;
var $errors = array();
var $errors = null;
var $options = array();
var $permission = null;
@ -65,7 +65,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
}
function connect() {
if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
$this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
else
$this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
@ -187,30 +187,31 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return ftp_rename($this->link, $source, $destination);
}
function delete($file,$recursive=false) {
function delete($file, $recursive = false ) {
if ( empty($file) )
return false;
if ( $this->is_file($file) )
return @ftp_delete($this->link, $file);
if ( !$recursive )
return @ftp_rmdir($this->link, $file);
$filelist = $this->dirlist($file);
foreach ((array) $filelist as $filename => $fileinfo) {
$this->delete($file . '/' . $filename, $recursive);
}
$filelist = $this->dirlist( trailingslashit($file) );
if ( !empty($filelist) )
foreach ( $filelist as $delete_file )
$this->delete( trailingslashit($file) . $delete_file['name'], $recursive);
return @ftp_rmdir($this->link, $file);
}
function exists($file) {
$list = ftp_rawlist($this->link, $file, false);
if( ! $list )
return false;
return count($list) == 1 ? true : false;
$list = @ftp_rawlist($this->link, $file, false);
return !empty($list); //empty list = no file, so invert.
}
function is_file($file) {
return $this->is_dir($file) ? false : true;
return $this->exists($file) && !$this->is_dir($file);
}
function is_dir($path) {
$cwd = $this->cwd();
$result = @ftp_chdir($this->link, $path);
$result = @ftp_chdir($this->link, trailingslashit($path) );
if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
@ftp_chdir($this->link, $cwd);
return true;
@ -218,7 +219,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return false;
}
function is_readable($file) {
//Get dir list, Check if the file is writable by the current user??
//Get dir list, Check if the file is readable by the current user??
return true;
}
function is_writable($file) {
@ -238,7 +239,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return false;
}
function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
if( !@ftp_mkdir($this->link, $path) )
if( !ftp_mkdir($this->link, $path) )
return false;
if( $chmod )
$this->chmod($path, $chmod);
@ -249,17 +250,14 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return true;
}
function rmdir($path, $recursive = false) {
if( ! $recursive )
return @ftp_rmdir($this->link, $path);
//TODO: Recursive Directory delete, Have to delete files from the folder first.
//$dir = $this->dirlist($path);
//foreach($dir as $file)
return $this->delete($path, $recursive);
}
function parselisting($line) {
$is_windows = ($this->OS_remote == FTP_OS_Windows);
static $is_windows;
if ( is_null($is_windows) )
$is_windows = strpos( strtolower(ftp_systype($this->link)), 'win') !== false;
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)) {
$b = array();
if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix

View File

@ -17,7 +17,7 @@
class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
var $ftp = false;
var $timeout = 5;
var $errors;
var $errors = null;
var $options = array();
var $permission = null;
@ -210,6 +210,8 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
}
function delete($file, $recursive = false ) {
if ( empty($file) )
return false;
if ( $this->is_file($file) )
return $this->ftp->delete($file);
if ( !$recursive )

View File

@ -0,0 +1,966 @@
<?php
/**
* This file is an attempt at an abstracted version of the plugin/theme/core installer/upgrader which can be used interchangably for all uses needed within WordPress.
* It is designed to be as flexible as possible, but some logic may seem rather, crazy to say the least.
* Yes, this header is designed to be replaced before commiting, Hopefully i'll get some proper documentation in here.
*
* This File obviously needs some new PHPDoc, However:
* Tested:
* Theme/Plugin Upgrades/Installs
* Core Upgrade
* FTP Extension, FTP Sockets, Direct.
* Untested:
* SSH2 Layer - Needs a good cleanup.
*
* TODO: Remove this commentblock and replace with some better docs.
*/
class WP_Upgrader {
var $strings = array();
var $skin = null;
var $result = array();
function WP_Upgrader(&$skin = null) {
return __construct($skin);
}
function __construct(&$skin = null) {
if ( null == $skin )
$this->skin = new WP_Upgrader_Skin();
else
$this->skin = $skin;
$this->skin->set_upgrader($this);
}
function generic_strings() {
$this->strings['bad_request'] = __('Invalid Data provided.');
$this->strings['fs_unavailable'] = __('Could not access filesystem.');
$this->strings['fs_error'] = __('Filesystem error');
$this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
$this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).');
$this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
$this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
$this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
$this->strings['download_failed'] = __('Download failed.');
$this->strings['installing_package'] = __('Installing the latest version.');
$this->strings['folder_exists'] = __('Destination folder already exists.');
$this->strings['mkdir_failed'] = __('Could not create directory.');
$this->strings['bad_package'] = __('Incompatible Archive');
$this->strings['maintenance_start'] = __('Enabling Maintenance mode.');
$this->strings['maintenance_end'] = __('Disabling Maintenance mode.');
}
function fs_connect( $directories = array() ) {
global $wp_filesystem;
if ( false === ($credentials = $this->skin->request_filesystem_credentials()) ) //request_filesystem_credentials($url)) )
return false;
if ( ! WP_Filesystem($credentials) ) {
$error = true;
if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
$error = $wp_filesystem->errors;
$this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
return false;
}
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
foreach ( (array)$directories as $dir ) {
if ( ABSPATH == $dir && ! $wp_filesystem->abspath() )
return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
elseif ( WP_CONTENT_DIR == $dir && ! $wp_filesystem->wp_content_dir() )
return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
elseif ( WP_PLUGIN_DIR == $dir && ! $wp_filesystem->wp_plugins_dir() )
return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
elseif ( WP_CONTENT_DIR . '/themes' == $dir && ! $wp_filesystem->find_folder(WP_CONTENT_DIR . '/themes') )
return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
elseif ( ! $wp_filesystem->find_folder($dir) )
return new WP_Error('fs_no_folder', sprintf($strings['fs_no_folder'], $dir));
}
return true;
} //end fs_connect();
function download_package($package) {
if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
return $package; //must be a local file..
if ( empty($package) )
return new WP_Error('no_package', $this->strings['no_package']);
$this->skin->feedback('downloading_package', $package);
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
return $download_file;
}
function unpack_package($package, $delete_package = true) {
global $wp_filesystem;
$this->skin->feedback('unpack_package');
$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
//Clean up contents of upgrade directory beforehand.
$upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
if ( !empty($upgrade_files) ) {
foreach ( $upgrade_files as $file )
$wp_filesystem->delete($upgrade_folder . $file['name'], true);
}
//We need a working directory
$working_dir = $upgrade_folder . basename($package, '.zip');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
// Unzip package to working directory
$result = unzip_file($package, $working_dir); //TODO optimizations, Copy when Move/Rename would suffice?
// Once extracted, delete the package if required.
if ( $delete_package )
unlink($package);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
return $working_dir;
}
function install_package($args = array()) {
global $wp_filesystem;
$defaults = array( 'source' => '', 'destination' => '', //Please always pass these
'clear_destination' => false, 'clear_working' => false,
'hook_extra' => array());
$args = wp_parse_args($args, $defaults);
extract($args);
@set_time_limit( 300 );
if ( empty($source) || empty($destination) )
return new WP_Error('bad_request', $this->strings['bad_request']);
$this->skin->feedback('installing_package');
$res = apply_filters('upgrader_pre_install', true, $hook_extra);
if ( is_wp_error($res) )
return $res;
//Retain the Original source and destinations
$remote_source = $source;
$local_destination = $destination;
$source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
$remote_destination = $wp_filesystem->find_folder($local_destination);
//Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
$source = trailingslashit($source) . trailingslashit($source_files[0]);
elseif ( count($source_files) == 0 )
return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
//else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
//Hook ability to change the source file location..
$source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
if ( is_wp_error($source) )
return $source;
//Has the source location changed? If so, we need a new source_files list.
if ( $source !== $remote_source )
$source_files = array_keys( $wp_filesystem->dirlist($source) );
//Protection against deleting files in any important base directories.
if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) {
$remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
$destination = trailingslashit($destination) . trailingslashit(basename($source));
}
//If we're not clearing the destination folder, and something exists there allready, Bail.
if ( ! $clear_destination && $wp_filesystem->exists($remote_destination) ) {
$wp_filesystem->delete($remote_source, true); //Clear out the source files.
return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
} else if ( $clear_destination ) {
//We're going to clear the destination if theres something there
$this->skin->feedback('remove_old');
$removed = true;
if ( $wp_filesystem->exists($remote_destination) )
$removed = $wp_filesystem->delete($remote_destination, true);
$removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra);
if ( is_wp_error($removed) )
return $removed;
else if ( ! $removed )
return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
}
//Create destination if needed
if ( !$wp_filesystem->exists($remote_destination) )
if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination);
// Copy new version of item into place.
$result = copy_dir($source, $remote_destination);
if ( is_wp_error($result) ) {
if ( $clear_working )
$wp_filesystem->delete($remote_source, true);
return $result;
}
//Clear the Working folder?
if ( $clear_working )
$wp_filesystem->delete($remote_source, true);
$destination_name = basename( str_replace($local_destination, '', $destination) );
if ( '.' == $destination_name )
$destination_name = '';
$this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
$res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
if ( is_wp_error($res) ) {
$this->result = $res;
return $res;
}
//Bombard the calling function will all the info which we've just used.
return $this->result;
}
function run($options) {
$defaults = array( 'package' => '', //Please always pass this.
'destination' => '', //And this
'clear_destination' => false,
'clear_working' => true,
'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
);
$options = wp_parse_args($options, $defaults);
extract($options);
$this->skin->header();
$this->skin->before();
//Connect to the Filesystem first.
$res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) );
if ( ! $res ) //Mainly for non-connected filesystem.
return false;
if ( is_wp_error($res) ) {
$this->skin->error($res);
return $res;
}
//Download the package (Note, This just returns the filename of the file if the package is a local file)
$download = $this->download_package( $package );
if ( is_wp_error($download) ) {
$this->skin->error($download);
return $download;
}
//Unzip's the file into a temporary directory
$working_dir = $this->unpack_package( $download );
if ( is_wp_error($working_dir) ) {
$this->skin->error($working_dir);
return $working_dir;
}
//With the given options, this installs it to the destination directory.
$result = $this->install_package( array(
'source' => $working_dir,
'destination' => $destination,
'clear_destination' => $clear_destination,
'clear_working' => $clear_working,
'hook_extra' => $hook_extra
) );
$this->skin->set_result($result);
if ( is_wp_error($result) ) {
$this->skin->error($result);
$this->skin->feedback('process_failed');
} else {
//Install Suceeded
$this->skin->feedback('process_success');
}
$this->skin->after();
$this->skin->footer();
return $result;
}
function maintenance_mode($enable = false) {
global $wp_filesystem;
$file = $wp_filesystem->abspath() . '.maintenance';
if ( $enable ) {
$this->skin->feedback('maintenance_start');
// Create maintenance file to signal that we are upgrading
$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
$wp_filesystem->delete($file);
$wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
} else if ( !$enable && $wp_filesystem->exists($file) ) {
$this->skin->feedback('maintenance_end');
$wp_filesystem->delete($file);
}
}
}
class Plugin_Upgrader extends WP_Upgrader {
var $result;
function upgrade_strings() {
$this->generic_strings();
$this->strings['up_to_date'] = __('The plugin is at the latest version.');
$this->strings['no_package'] = __('Upgrade package not available.');
$this->strings['downloading_package'] = __('Downloading update from %s.');
$this->strings['unpack_package'] = __('Unpacking the update.');
$this->strings['deactivate_plugin'] = __('Deactivating the plugin.');
$this->strings['remove_old'] = __('Removing the old version of the plugin.');
$this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
$this->strings['process_failed'] = __('Plugin upgrade Failed.');
$this->strings['process_success'] = __('Plugin upgraded successfully.');
}
function install_strings() {
$this->generic_strings();
$this->strings['no_package'] = __('Install package not available.');
$this->strings['downloading_package'] = __('Downloading install package from %s.');
$this->strings['unpack_package'] = __('Unpacking the package.');
$this->strings['installing_package'] = __('Installing the plugin.');
$this->strings['process_failed'] = __('Plugin Install Failed.');
$this->strings['process_success'] = __('Plugin Installed successfully.');
}
function install($package) {
$this->install_strings();
$this->run(array(
'package' => $package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => false, //Do not overwrite files.
'clear_working' => true,
'hook_extra' => array()
));
// Force refresh of plugin update information
delete_transient('update_plugins');
}
function upgrade($plugin) {
$this->upgrade_strings();
$current = get_transient( 'update_plugins' );
if ( !isset( $current->response[ $plugin ] ) ) {
$this->skin->error('up_to_date');
return false;
//return new WP_Error('up_to_date', $this->strings['up_to_date']);
}
// Get the URL to the zip file
$r = $current->response[ $plugin ];
add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
//'source_selection' => array(&$this, 'source_selection'), //theres a track ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
$this->run(array(
'package' => $r->package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(
'plugin' => $plugin
)
));
//Cleanup our hooks, incase something else does a upgrade on this connection.
remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
if ( ! $this->result || is_wp_error($this->result) )
return $this->result;
// Force refresh of plugin update information
delete_transient('update_plugins');
}
//return plugin info.
function plugin_info() {
if ( ! is_array($this->result) )
return false;
if ( empty($this->result['destination_name']) )
return false;
$plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
if ( empty($plugin) )
return false;
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
return $this->result['destination_name'] . '/' . $pluginfiles[0];
}
//Hooked to pre_install
function deactivate_plugin_before_upgrade($return, $plugin) {
if ( is_wp_error($return) ) //Bypass.
return $return;
$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
if ( empty($plugin) )
return new WP_Error('bad_request', $this->strings['bad_request']);
if ( is_plugin_active($plugin) ) {
$this->skin->feedback('deactivate_plugin');
//Deactivate the plugin silently, Prevent deactivation hooks from running.
deactivate_plugins($plugin, true);
}
}
//Hooked to upgrade_clear_destination
function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
global $wp_filesystem;
if ( is_wp_error($removed) )
return $removed; //Pass errors through.
$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
if ( empty($plugin) )
return new WP_Error('bad_request', $this->strings['bad_request']);
$plugins_dir = $wp_filesystem->wp_plugins_dir();
$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
return $removed;
// If plugin is in its own directory, recursively delete the directory.
if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
$deleted = $wp_filesystem->delete($this_plugin_dir, true);
else
$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
if ( ! $deleted )
return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
return $removed;
}
}
class Theme_Upgrader extends WP_Upgrader {
var $result;
function upgrade_strings() {
$this->generic_strings();
$this->strings['up_to_date'] = __('The theme is at the latest version.');
$this->strings['no_package'] = __('Upgrade package not available.');
$this->strings['downloading_package'] = __('Downloading update from %s.');
$this->strings['unpack_package'] = __('Unpacking the update.');
$this->strings['remove_old'] = __('Removing the old version of the theme.');
$this->strings['remove_old_failed'] = __('Could not remove the old theme.');
$this->strings['process_failed'] = __('Theme upgrade Failed.');
$this->strings['process_success'] = __('Theme upgraded successfully.');
}
function install_strings() {
$this->generic_strings();
$this->strings['no_package'] = __('Install package not available.');
$this->strings['downloading_package'] = __('Downloading install package from %s.');
$this->strings['unpack_package'] = __('Unpacking the package.');
$this->strings['installing_package'] = __('Installing the theme.');
$this->strings['process_failed'] = __('Theme Install Failed.');
$this->strings['process_success'] = __('Theme Installed successfully.');
}
function install($package) {
$this->install_strings();
$options = array(
'package' => $package,
'destination' => WP_CONTENT_DIR . '/themes',
'clear_destination' => false, //Do not overwrite files.
'clear_working' => true
);
$this->run($options);
if ( ! $this->result || is_wp_error($this->result) )
return $this->result;
// Force refresh of theme update information
delete_transient('update_themes');
if ( empty($result['destination_name']) )
return false;
else
return $result['destination_name'];
}
function upgrade($theme) {
$this->upgrade_strings();
// Is an update available?
$current = get_transient( 'update_themes' );
if ( !isset( $current->response[ $theme ] ) )
return new WP_Error('up_to_date', $this->strings['up_to_date']);
$r = $current->response[ $theme ];
add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
$options = array(
'package' => $r['package'],
'destination' => WP_CONTENT_DIR . '/themes',
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(
'theme' => $theme
)
);
$this->run($options);
if ( ! $this->result || is_wp_error($this->result) )
return $this->result;
// Force refresh of theme update information
delete_transient('update_themes');
return true;
}
function current_before($return, $theme) {
if ( is_wp_error($return) )
return $return;
$theme = isset($theme['theme']) ? $theme['theme'] : '';
if ( $theme != get_stylesheet() ) //If not current
return $return;
//Change to maintainence mode now.
$this->maintenance_mode(true);
return $return;
}
function current_after($return, $theme) {
if ( is_wp_error($return) )
return $return;
$theme = isset($theme['theme']) ? $theme['theme'] : '';
if ( $theme != get_stylesheet() ) //If not current
return $return;
//Ensure stylesheet name hasnt changed after the upgrade:
if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
$theme_info = $this->theme_info();
$stylesheet = $this->result['destination_name'];
$template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
switch_theme($template, $stylesheet, true);
}
//Time to remove maintainence mode
$this->maintenance_mode(false);
return $return;
}
function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
global $wp_filesystem;
$theme = isset($theme['theme']) ? $theme['theme'] : '';
if ( is_wp_error($removed) || empty($theme) )
return $removed; //Pass errors through.
$themes_dir = $wp_filesystem->wp_themes_dir();
if ( $wp_filesystem->exists( trailingslashit($themes_dir) . $theme ) )
if ( ! $wp_filesystem->delete( trailingslashit($themes_dir) . $theme, true ) )
return false;
return true;
}
function theme_info() {
if ( empty($this->result['destination_name']) )
return false;
return get_theme_data(WP_CONTENT_DIR . '/themes/' . $this->result['destination_name'] . '/style.css');
}
}
//Untested.
class Core_Upgrader extends WP_Upgrader {
function upgrade_strings() {
$this->generic_strings();
$this->strings['up_to_date'] = __('WordPress is at the latest version.');
$this->strings['no_package'] = __('Upgrade package not available.');
$this->strings['downloading_package'] = __('Downloading update from %s.');
$this->strings['unpack_package'] = __('Unpacking the update.');
$this->strings['copy_failed'] = __('Could not copy files.');
}
function upgrade($current) {
global $wp_filesystem;
$this->upgrade_strings();
if ( !empty($feedback) )
add_filter('update_feedback', $feedback);
// Is an update available?
if ( !isset( $current->response ) || $current->response == 'latest' )
return new WP_Error('up_to_date', $this->strings['up_to_date']);
$res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
if ( is_wp_error($res) )
return $res;
$wp_dir = trailingslashit($wp_filesystem->abspath());
$download = $this->download_package( $current->package );
if ( is_wp_error($download) )
return $download;
$working_dir = $this->unpack_package( $download );
if ( is_wp_error($working_dir) )
return $working_dir;
// Copy update-core.php from the new version into place.
if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('copy_failed', $this->strings['copy_failed']);
}
$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
require(ABSPATH . 'wp-admin/includes/update-core.php');
return update_core($working_dir, $wp_dir);
}
}
/**
* Skin stuff here.
* ============================================
* ============================================
* ============================================
*/
class WP_Upgrader_Skin {
var $upgrader;
var $done_header = false;
function WP_Upgrader_Skin($args = array()) {
return __construct($args);
}
function __construct($args = array()) {
$defaults = array( 'url' => '', 'nonce' => '', 'title' => '' );
$this->options = wp_parse_args($args, $defaults);
}
function set_upgrader(&$upgrader) {
if ( is_object($upgrader) )
$this->upgrader =& $upgrader;
}
function set_result($result) {
$this->result = $result;
}
function request_filesystem_credentials($error = false) {
$url = $this->options['url'];
if ( !empty($this->options['nonce']) )
$url = wp_nonce_url($url, $this->options['nonce']);
return request_filesystem_credentials($url, '', $error); //Possible to bring inline, Leaving as0is for now.
}
function header() {
if ( $this->done_header )
return;
$this->done_header = true;
echo '<div class="wrap">';
echo screen_icon();
echo '<h2>' . $this->options['title'] . '</h2>';
}
function footer() {
echo '</div>';
}
function error($errors) {
if ( ! $this->done_header )
$this->header();
if ( is_string($errors) ) {
$this->feedback($errors);
} elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
foreach ( $errors->get_error_messages() as $message )
$this->feedback($message);
}
}
function feedback($string) {
if ( isset( $this->upgrader->strings[$string]) )
$string = $this->upgrader->strings[$string];
$args = func_get_args();
$args = array_splice($args, 1);
if ( !empty($args) )
$string = vsprintf($string, $args);
if ( empty($string) )
return;
show_message($string);
}
function before() {}
function after() {}
}
class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
var $plugin = '';
var $plugin_active = false;
function Plugin_Upgrader_Skin($args = array()) {
return __construct($args);
}
function __construct($args = array()) {
$defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Upgrade Plugin') );
$args = wp_parse_args($args, $defaults);
$this->plugin = $args['plugin'];
$this->plugin_active = is_plugin_active($this->plugin);
parent::__construct($args);
}
function after() {
$this->plugin = $this->upgrader->plugin_info();
if( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
show_message(__('Attempting reactivation of the plugin'));
echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) .'"></iframe>';
}
$update_actions = array(
'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) . '" title="' . attribute_escape(__('Activate this plugin')) . '" target="_parent">' . __('Activate Plugin') . '</a>',
'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
);
if ( $this->plugin_active )
unset( $update_actions['activate_plugin'] );
if ( ! $this->result || is_wp_error($this->result) )
unset( $update_actions['activate_plugin'] );
$update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
if ( ! empty($update_actions) )
$this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
}
}
class Plugin_Installer_Skin extends WP_Upgrader_Skin {
var $api;
var $type;
function Plugin_Installer_Skin($args = array()) {
return __construct($args);
}
function __construct($args = array()) {
$defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
$args = wp_parse_args($args, $defaults);
$this->type = $args['type'];
$this->api = isset($args['api']) ? $args['api'] : array();
parent::__construct($args);
}
function before() {
if ( !empty($this->api) )
$this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
}
function after() {
$plugin_file = $this->upgrader->plugin_info();
$install_actions = array(
'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>',
);
if ( $this->type == 'web' )
$install_actions['plugins_page'] = '<a href="' . admin_url('plugin-install.php') . '" title="' . attribute_escape(__('Return to Plugin Installer')) . '" target="_parent">' . __('Return to Plugin Installer') . '</a>';
else
$install_actions['plugins_page'] = '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Return to Plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>';
if ( ! $this->result || is_wp_error($this->result) )
unset( $update_actions['activate_plugin'] );
$install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
if ( ! empty($install_actions) )
$this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
class Theme_Installer_Skin extends WP_Upgrader_Skin {
var $api;
var $type;
function Theme_Installer_Skin($args = array()) {
return __construct($args);
}
function __construct($args = array()) {
$defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
$args = wp_parse_args($args, $defaults);
$this->type = $args['type'];
$this->api = isset($args['api']) ? $args['api'] : array();
parent::__construct($args);
}
function before() {
if ( !empty($this->api) )
$this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the theme <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
}
function after() {
if ( empty($this->upgrader->result['destination_name']) )
return;
$theme_info = $this->upgrader->theme_info();
if ( empty($theme_info) )
return;
$name = $theme_info['Name'];
$stylesheet = $this->upgrader->result['destination_name'];
$template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
$preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(clean_url(get_option('home'))) ) );
$activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
$install_actions = array(
'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . attribute_escape(sprintf(__('Preview "%s"'), $name)) . '">' . __('Preview') . '</a>',
'activate' => '<a href="' . $activate_link . '" class="activatelink" title="' . attribute_escape( sprintf( __('Activate "%s"'), $name ) ) . '">' . __('Activate') . '</a>'
);
if ( $this->type == 'web' )
$install_actions['themes_page'] = '<a href="' . admin_url('theme-install.php') . '" title="' . attribute_escape(__('Back to Theme Installer')) . '" target="_parent">' . __('Return to Theme Installer.') . '</a>';
else
$install_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . attribute_escape(__('Themes page')) . '" target="_parent">' . __('Return to Themes page') . '</a>';
if ( ! $this->result || is_wp_error($this->result) )
unset( $update_actions['activate'], $update_actions['preview'] );
$install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
if ( ! empty($install_actions) )
$this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
var $theme = '';
function Theme_Upgrader_Skin($args = array()) {
return __construct($args);
}
function __construct($args = array()) {
$defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Upgrade Theme') );
$args = wp_parse_args($args, $defaults);
$this->theme = $args['theme'];
parent::__construct($args);
}
function after() {
if ( empty($this->upgrader->result['destination_name']) )
return;
$theme_info = $this->upgrader->theme_info();
if ( empty($theme_info) )
return;
$name = $theme_info['Name'];
$stylesheet = $this->upgrader->result['destination_name'];
$template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
$preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(clean_url(get_option('home'))) ) );
$activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
$update_actions = array(
'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . attribute_escape(sprintf(__('Preview "%s"'), $name)) . '">' . __('Preview') . '</a>',
'activate' => '<a href="' . $activate_link . '" class="activatelink" title="' . attribute_escape( sprintf( __('Activate "%s"'), $name ) ) . '">' . __('Activate') . '</a>',
'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . attribute_escape(__('Return to Themes page')) . '" target="_parent">' . __('Return to Themes page') . '</a>',
);
if ( ( ! $this->result || is_wp_error($this->result) ) || $stylesheet == get_stylesheet() )
unset($update_actions['preview'], $update_actions['activate']);
$update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
if ( ! empty($update_actions) )
$this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
}
}
class File_Upload_Upgrader {
var $package;
var $filename;
function File_Upload_Upgrader($form, $urlholder) {
return __construct($form, $urlholder);
}
function __construct($form, $urlholder) {
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
wp_die($uploads['error']);
if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
wp_die(__('Please select a file'));
if ( !empty($_FILES) )
$this->filename = $_FILES[$form]['name'];
else if ( isset($_GET[$urlholder]) )
$this->filename = $_GET[$urlholder];
//Handle a newly uploaded file, Else assume its already been uploaded
if ( !empty($_FILES) ) {
$this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
$this->package = $uploads['basedir'] . '/' . $this->filename;
// Move the file to the uploads dir
if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
} else {
$this->package = $uploads['basedir'] . '/' . $this->filename;
}
}
}

View File

@ -598,7 +598,7 @@ function WP_Filesystem( $args = false ) {
$wp_filesystem = new $method($args);
if ( $wp_filesystem->errors->get_error_code() )
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return false;
if ( !$wp_filesystem->connect() )

View File

@ -208,7 +208,7 @@ function install_plugins_upload( $page = 1 ) {
?>
<h4><?php _e('Install a plugin in .zip format') ?></h4>
<p class="install-help"><?php _e('If you have a plugin in a .zip format, You may install it by uploading it here.') ?></p>
<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('plugin-install.php?tab=do_upload') ?>">
<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-plugin') ?>">
<?php wp_nonce_field( 'plugin-upload') ?>
<input type="file" name="pluginzip" />
<input type="submit" class="button" value="<?php _e('Install Now') ?>" />
@ -461,7 +461,7 @@ function install_plugin_information() {
default:
case 'install':
if ( current_user_can('install_plugins') ) :
?><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
?><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
endif;
break;
case 'update_available':
@ -544,364 +544,3 @@ function install_plugin_information() {
iframe_footer();
exit;
}
add_action('install_plugins_do_upload', 'upload_plugin');
function upload_plugin() {
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
wp_die($uploads['error']);
if ( !empty($_FILES) )
$filename = $_FILES['pluginzip']['name'];
else if ( isset($_GET['package']) )
$filename = $_GET['package'];
check_admin_referer('plugin-upload');
echo '<div class="wrap">';
echo '<h2>', sprintf( __('Installing Plugin from file: %s'), basename($filename) ), '</h2>';
//Handle a newly uploaded file, Else assume it was
if ( !empty($_FILES) ) {
$filename = wp_unique_filename( $uploads['basedir'], $filename );
$local_file = $uploads['basedir'] . '/' . $filename;
// Move the file to the uploads dir
if ( false === @ move_uploaded_file( $_FILES['pluginzip']['tmp_name'], $local_file) )
wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
} else {
$local_file = $uploads['basedir'] . '/' . $filename;
}
do_plugin_install_local_package($local_file, $filename);
echo '</div>';
}
add_action('install_plugins_install', 'install_plugin');
/**
* Display plugin link and execute install.
*
* @since 2.7.0
*/
function install_plugin() {
$plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
check_admin_referer('install-plugin_' . $plugin);
$api = plugins_api('plugin_information', array('slug' => $plugin, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
if ( is_wp_error($api) )
wp_die($api);
echo '<div class="wrap">';
echo '<h2>', sprintf( __('Installing Plugin: %s'), $api->name . ' ' . $api->version ), '</h2>';
do_plugin_install($api->download_link, $api);
echo '</div>';
}
/**
* Retrieve plugin and install.
*
* @since 2.7.0
*
* @param string $download_url Download URL.
* @param object $plugin_information Optional. Plugin information
*/
function do_plugin_install($download_url, $plugin_information = null) {
global $wp_filesystem;
if ( empty($download_url) ) {
show_message( __('No plugin Specified') );
return;
}
$plugin = isset($_REQUEST['plugin']) ? stripslashes( $_REQUEST['plugin'] ) : '';
$url = 'plugin-install.php?tab=install';
$url = add_query_arg(array('plugin' => $plugin, 'plugin_name' => stripslashes( $_REQUEST['plugin_name'] ), 'download_url' => stripslashes( $_REQUEST['download_url'] ) ), $url);
$url = wp_nonce_url($url, 'install-plugin_' . $plugin);
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
return;
}
$result = wp_install_plugin( $download_url, 'show_message' );
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Installation Failed') );
} else {
show_message( sprintf(__('Successfully installed the plugin <strong>%s %s</strong>.'), $plugin_information->name, $plugin_information->version) );
$plugin_file = $result;
$install_actions = apply_filters('install_plugin_complete_actions', array(
'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>',
'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
), $plugin_information, $plugin_file);
if ( ! empty($install_actions) )
show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
/**
* Install a plugin from a local file.
*
* @since 2.7.0
*
* @param string $package Local Plugin zip
* @param string $filename Optional. Original filename
* @param object $plugin_information Optional. Plugin information
*/
function do_plugin_install_local_package($package, $filename = '') {
global $wp_filesystem;
if ( empty($package) ) {
show_message( __('No plugin Specified') );
return;
}
if ( empty($filename) )
$filename = basename($package);
$url = 'plugin-install.php?tab=upload';
$url = add_query_arg(array('package' => $filename), $url);
$url = wp_nonce_url($url, 'plugin-upload');
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
return;
}
$result = wp_install_plugin_local_package( $package, 'show_message' );
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Installation Failed') );
} else {
show_message( __('Successfully installed the plugin.') );
$plugin_file = $result;
$install_actions = apply_filters('install_plugin_complete_actions', array(
'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>',
'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . __('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
), array(), $plugin_file);
if ( ! empty($install_actions) )
show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
/**
* Install plugin.
*
* @since 2.7.0
*
* @param string $package
* @param string $feedback Optional.
* @return mixed.
*/
function wp_install_plugin($package, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('install_feedback', $feedback);
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder
$plugins_dir = $wp_filesystem->wp_plugins_dir();
if ( empty($plugins_dir) )
return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
//And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$plugins_dir = trailingslashit( $plugins_dir );
$content_dir = trailingslashit( $content_dir );
if ( empty($package) )
return new WP_Error('no_package', __('Install package not available.'));
// Download the package
apply_filters('install_feedback', sprintf(__('Downloading plugin package from %s'), $package));
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('install_feedback', __('Unpacking the plugin package'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
@unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
}
apply_filters('install_feedback', __('Installing the plugin'));
// Copy new version of plugin into place.
$result = copy_dir($working_dir, $plugins_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
if( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
$folder = $filelist[0];
$plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
//Return the plugin files name.
return $folder . '/' . $pluginfiles[0];
}
/**
* Install plugin from local package
*
* @since 2.7.0
*
* @param string $package
* @param string $feedback Optional.
* @return mixed.
*/
function wp_install_plugin_local_package($package, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('install_feedback', $feedback);
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder
$plugins_dir = $wp_filesystem->wp_plugins_dir();
if ( empty($plugins_dir) )
return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
//And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$plugins_dir = trailingslashit( $plugins_dir );
$content_dir = trailingslashit( $content_dir );
if ( empty($package) )
return new WP_Error('no_package', __('Install package not available.'));
$working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('install_feedback', __('Unpacking the plugin package'));
// Unzip package to working directory
$result = unzip_file($package, $working_dir);
// Once extracted, delete the package
unlink($package);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
if( $wp_filesystem->exists( $plugins_dir . $filelist[0] ) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
}
apply_filters('install_feedback', __('Installing the plugin'));
// Copy new version of plugin into place.
$result = copy_dir($working_dir, $plugins_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
if( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
$folder = $filelist[0];
$plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
//Return the plugin files name.
return $folder . '/' . $pluginfiles[0];
}
?>

View File

@ -216,6 +216,7 @@ function get_plugins($plugin_folder = '') {
// Files in wp-content/plugins directory
$plugins_dir = @ opendir( $plugin_root);
$plugin_files = array();
if ( $plugins_dir ) {
while (($file = readdir( $plugins_dir ) ) !== false ) {
if ( substr($file, 0, 1) == '.' )
@ -239,7 +240,7 @@ function get_plugins($plugin_folder = '') {
@closedir( $plugins_dir );
@closedir( $plugins_subdir );
if ( !$plugins_dir || !$plugin_files )
if ( !$plugins_dir || empty($plugin_files) )
return $wp_plugins;
foreach ( $plugin_files as $plugin_file ) {
@ -432,14 +433,10 @@ function delete_plugins($plugins, $redirect = '' ) {
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
return $wp_filesystem->errors;
}
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder

View File

@ -277,12 +277,13 @@ function install_themes_upload($page = 1) {
?>
<h4><?php _e('Install a theme in .zip format') ?></h4>
<p class="install-help"><?php _e('If you have a theme in a .zip format, you may install it by uploading it here.') ?></p>
<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('theme-install.php?tab=do_upload') ?>">
<form method="post" enctype="multipart/form-data" action="<?php echo admin_url('update.php?action=upload-theme') ?>">
<?php wp_nonce_field( 'theme-upload') ?>
<input type="file" name="themezip" />
<input type="submit" class="button" value="<?php _e('Install Now') ?>" />
<input type="submit"
class="button" value="<?php _e('Install Now') ?>" />
</form>
<?php
<?php
}
function display_theme($theme, $actions = null, $show_details = true) {
@ -411,6 +412,7 @@ function display_themes($themes, $page = 1, $totalpages = 1) {
?>
<tr>
<?php
foreach ( $cols as $col => $theme_index ) {
$class = array('available-theme');
if ( $row == 1 ) $class[] = 'top';
@ -511,7 +513,7 @@ switch ( $type ) {
default:
case 'install':
if ( current_user_can('install_themes') ) :
$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>';
$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>';
endif;
break;
case 'update_available':
@ -542,365 +544,3 @@ case 'latest_installed':
iframe_footer();
exit;
}
add_action('install_themes_do_upload', 'upload_theme');
function upload_theme() {
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
wp_die($uploads['error']);
if ( !empty($_FILES) )
$filename = $_FILES['themezip']['name'];
else if ( isset($_GET['package']) )
$filename = $_GET['package'];
check_admin_referer('theme-upload');
echo '<div class="wrap">';
echo '<h2>', sprintf( __('Installing theme from file: %s'), basename($filename) ), '</h2>';
//Handle a newly uploaded file, Else assume it was
if ( !empty($_FILES) ) {
$filename = wp_unique_filename( $uploads['basedir'], $filename );
$local_file = $uploads['basedir'] . '/' . $filename;
// Move the file to the uploads dir
if ( false === @ move_uploaded_file( $_FILES['themezip']['tmp_name'], $local_file) )
wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
} else {
$local_file = $uploads['basedir'] . '/' . $filename;
}
do_theme_install_local_package($local_file, $filename);
echo '</div>';
}
add_action('install_themes_install', 'install_theme');
/**
* Display theme link and execute install.
*
* @since 2.8.0
*/
function install_theme() {
$theme = isset($_REQUEST['theme']) ? stripslashes( $_REQUEST['theme'] ) : '';
check_admin_referer('install-theme_' . $theme);
$api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
if ( is_wp_error($api) )
wp_die($api);
echo '<div class="wrap">';
echo '<h2>', sprintf( __('Installing theme: %s'), $api->name . ' ' . $api->version ), '</h2>';
do_theme_install($api->download_link, $api);
echo '</div>';
}
/**
* Retrieve theme and install.
*
* @since 2.8.0
*
* @param string $download_url Download URL.
* @param object $theme_information Optional. Theme information
*/
function do_theme_install($download_url, $theme_information = null) {
global $wp_filesystem;
if ( empty($download_url) ) {
show_message( __('No theme specified') );
return;
}
$theme = isset($_REQUEST['theme']) ? stripslashes( $_REQUEST['theme'] ) : '';
$theme_name = isset($_REQUEST['theme_name']) ? stripslashes( $_REQUEST['theme_name'] ) : '';
$url = 'theme-install.php?tab=install';
$url = add_query_arg(array('theme' => $theme, 'theme_name' => $theme_name, 'download_url' => $download_url ), $url);
$url = wp_nonce_url($url, 'install-theme_' . $theme);
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
return;
}
$result = wp_install_theme( $download_url, 'show_message' );
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Installation Failed') );
} else {
show_message( sprintf(__('Successfully installed the theme <strong>%s %s</strong>.'), $theme_information->name, $theme_information->version) );
$theme_file = $result;
$install_actions = apply_filters('install_theme_complete_actions', array(
//'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>',
'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . attribute_escape(__('Return to Themes page')) . '" target="_parent">' . __('Return to Themes page') . '</a>'
), $theme_information, $theme_file);
if ( ! empty($install_actions) )
show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
/**
* Install a theme from a local file.
*
* @since 2.8.0
*
* @param string $package Local Theme zip
* @param string $filename Optional. Original filename
* @param object $theme_information Optional. Theme information
*/
function do_theme_install_local_package($package, $filename = '') {
global $wp_filesystem;
if ( empty($package) ) {
show_message( __('No theme specified') );
return;
}
if ( empty($filename) )
$filename = basename($package);
$url = 'theme-install.php?tab=upload';
$url = add_query_arg(array('package' => $filename), $url);
$url = wp_nonce_url($url, 'theme-upload');
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
return;
}
$result = wp_install_theme_local_package( $package, 'show_message' );
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Installation Failed') );
} else {
show_message( __('Successfully installed the theme.') );
$theme_file = $result;
$install_actions = apply_filters('install_theme_complete_actions', array(
//'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>',
'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . __('Goto themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>'
), array(), $theme_file);
if ( ! empty($install_actions) )
show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
}
}
/**
* Install theme.
*
* @since 2.8.0
*
* @param string $package
* @param string $feedback Optional.
* @return mixed.
*/
function wp_install_theme($package, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('install_feedback', $feedback);
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
// Get the base theme folder
$themes_dir = $wp_filesystem->wp_themes_dir();
if ( empty($themes_dir) )
return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress themes directory.'));
// And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if ( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress content directory (wp-content).'));
$themes_dir = trailingslashit( $themes_dir );
$content_dir = trailingslashit( $content_dir );
if ( empty($package) )
return new WP_Error('no_package', __('Install package not available.'));
// Download the package
apply_filters('install_feedback', sprintf(__('Downloading theme package from %s'), $package));
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('install_feedback', __('Unpacking the theme package'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
@unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
if ( $wp_filesystem->exists( $themes_dir . $filelist[0] ) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
}
apply_filters('install_feedback', __('Installing the theme'));
// Copy new version of theme into place.
$result = copy_dir($working_dir, $themes_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
if ( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no theme installed? Failsafe backup.
//TODO: TODO: TODO
$stylesheet = $filelist[0];
// $theme = get_themes('/' . $folder); //Ensure to pass with leading slash //TODO: TODO: TODO
// $themefiles = array_keys($theme); //Assume the requested theme is the first in the list
//Return the theme files name.
return $stylesheet; //$folder . '/' . $themefiles[0];
}
/**
* Install theme from local package
*
* @since 2.8.0
*
* @param string $package
* @param string $feedback Optional.
* @return mixed.
*/
function wp_install_theme_local_package($package, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('install_feedback', $feedback);
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base theme folder
$themes_dir = $wp_filesystem->wp_themes_dir();
if ( empty($themes_dir) )
return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress themes directory.'));
//And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if ( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress content directory (wp-content).'));
$themes_dir = trailingslashit( $themes_dir );
$content_dir = trailingslashit( $content_dir );
if ( empty($package) )
return new WP_Error('no_package', __('Install package not available.'));
$working_dir = $content_dir . 'upgrade/' . basename($package, '.zip');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('install_feedback', __('Unpacking the theme package'));
// Unzip package to working directory
$result = unzip_file($package, $working_dir);
// Once extracted, delete the package
unlink($package);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
if ( $wp_filesystem->exists( $themes_dir . $filelist[0] ) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('install_folder_exists', __('Folder already exists.'), $filelist[0] );
}
apply_filters('install_feedback', __('Installing the theme'));
// Copy new version of theme into place.
$result = copy_dir($working_dir, $themes_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the theme
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
if ( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no theme installed? Failsafe backup.
//TODO TODO TODO
$stylesheet = $filelist[0];
// $theme = get_themes('/' . $folder); //Ensure to pass with leading slash
// $themefiles = array_keys($theme); //Assume the requested theme is the first in the list
//Return the theme files name.
return $stylsheet; //$folder . '/' . $themefiles[0];
}

View File

@ -72,14 +72,11 @@ function delete_theme($template) {
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
return $wp_filesystem->errors;
}
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder

View File

@ -229,7 +229,7 @@ function update_core($from, $to) {
$result = copy_dir($from . '/wordpress', $to);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($maintenance_file);
//$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
$wp_filesystem->delete($working_dir, true);
return $result;
}

View File

@ -167,312 +167,35 @@ function wp_plugin_update_row( $file, $plugin_data ) {
add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
function wp_update_plugin($plugin, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('update_feedback', $feedback);
// Is an update available?
$current = get_transient( 'update_plugins' );
if ( !isset( $current->response[ $plugin ] ) )
return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder
$plugins_dir = $wp_filesystem->wp_plugins_dir();
if ( empty($plugins_dir) )
return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
//And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$plugins_dir = trailingslashit( $plugins_dir );
$content_dir = trailingslashit( $content_dir );
// Get the URL to the zip file
$r = $current->response[ $plugin ];
if ( empty($r->package) )
return new WP_Error('no_package', __('Upgrade package not available.'));
// Download the package
$package = $r->package;
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('update_feedback', __('Unpacking the update'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
if ( is_plugin_active($plugin) ) {
//Deactivate the plugin silently, Prevent deactivation hooks from running.
apply_filters('update_feedback', __('Deactivating the plugin'));
deactivate_plugins($plugin, true);
}
// Remove the existing plugin.
apply_filters('update_feedback', __('Removing the old version of the plugin'));
$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
// If plugin is in its own directory, recursively delete the directory.
if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
$deleted = $wp_filesystem->delete($this_plugin_dir, true);
else
$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
if ( ! $deleted ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('delete_failed', __('Could not remove the old plugin'));
}
apply_filters('update_feedback', __('Installing the latest version'));
// Copy new version of plugin into place.
$result = copy_dir($working_dir, $plugins_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
// Force refresh of plugin update information
delete_transient('update_plugins');
if( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
$folder = $filelist[0];
$plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
return $folder . '/' . $pluginfiles[0];
include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new Plugin_Upgrader();
return $upgrader->upgrade($plugin);
}
function wp_update_theme($theme, $feedback = '') {
global $wp_filesystem;
if ( !empty($feedback) )
add_filter('update_feedback', $feedback);
// Is an update available?
$current = get_transient( 'update_themes' );
if ( !isset( $current->response[ $theme ] ) )
return new WP_Error('up_to_date', __('The theme is at the latest version.'));
$r = $current->response[ $theme ];
$themes = get_themes();
foreach ( (array) $themes as $this_theme ) {
if ( $this_theme['Stylesheet'] == $theme ) {
$theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
break;
}
}
unset($themes);
if ( empty($theme_directory) )
return new WP_Error('theme_non_existant', __('Theme does not exist.'));
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
//Get the base plugin folder
$themes_dir = $wp_filesystem->wp_themes_dir();
if ( empty($themes_dir) )
return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
//And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$themes_dir = trailingslashit( $themes_dir );
$content_dir = trailingslashit( $content_dir );
if ( empty($r->package) )
return new WP_Error('no_package', __('Upgrade package not available.'));
// Download the package
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
$download_file = download_url($r['package']);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) )
$wp_filesystem->delete($working_dir, true);
apply_filters('update_feedback', __('Unpacking the update'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//TODO: Is theme currently active? If so, set default theme
/*
if ( is_plugin_active($plugin) ) {
//Deactivate the plugin silently, Prevent deactivation hooks from running.
apply_filters('update_feedback', __('Deactivating the plugin'));
deactivate_plugins($plugin, true);
}*/
// Remove the existing plugin.
apply_filters('update_feedback', __('Removing the old version of the theme'));
$deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
if ( ! $deleted ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('delete_failed', __('Could not remove the old plugin'));
}
apply_filters('update_feedback', __('Installing the latest version'));
// Copy new version of plugin into place.
$result = copy_dir($working_dir, $themes_dir);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
//$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
// Remove working directory
$wp_filesystem->delete($working_dir, true);
// Force refresh of plugin update information
delete_transient('update_themes');
/*if( empty($filelist) )
return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
$folder = $filelist[0];
$plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
return $folder . '/' . $pluginfiles[0];*/
include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new Theme_Upgrader();
return $upgrader->upgrade($theme);
}
function wp_update_core($current, $feedback = '') {
global $wp_filesystem;
@set_time_limit( 300 );
if ( !empty($feedback) )
add_filter('update_feedback', $feedback);
// Is an update available?
if ( !isset( $current->response ) || $current->response == 'latest' )
return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new Core_Upgrader();
return $upgrader->upgrade($current);
// Is a filesystem accessor setup?
if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
WP_Filesystem();
if ( ! is_object($wp_filesystem) )
return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
if ( $wp_filesystem->errors->get_error_code() )
return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
// Get the base WP folder
$wp_dir = $wp_filesystem->abspath();
if ( empty($wp_dir) )
return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
// And the same for the Content directory.
$content_dir = $wp_filesystem->wp_content_dir();
if( empty($content_dir) )
return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
$wp_dir = trailingslashit( $wp_dir );
$content_dir = trailingslashit( $content_dir );
// Get the URL to the zip file
$package = $current->package;
// Download the package
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
$download_file = download_url($package);
if ( is_wp_error($download_file) )
return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
$working_dir = $content_dir . 'upgrade/core';
// Clean up working directory
if ( $wp_filesystem->is_dir($working_dir) ) {
$wp_filesystem->delete($working_dir, true);
}
apply_filters('update_feedback', __('Unpacking the core update'));
// Unzip package to working directory
$result = unzip_file($download_file, $working_dir);
// Once extracted, delete the package
unlink($download_file);
if ( is_wp_error($result) ) {
$wp_filesystem->delete($working_dir, true);
return $result;
}
// Copy update-core.php from the new version into place.
if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
$wp_filesystem->delete($working_dir, true);
return new WP_Error('copy_failed', __('Could not copy files'));
}
$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
require(ABSPATH . 'wp-admin/includes/update-core.php');
return update_core($working_dir, $wp_dir);
}
function maintenance_nag() {

View File

@ -24,7 +24,7 @@ add_thickbox();
//These are the tabs which are shown on the page,
$tabs = array();
$tabs['dashboard'] = __('Search'); //TODO: Better name?
$tabs['dashboard'] = __('Search');
if ( 'search' == $tab )
$tabs['search'] = __('Search Results');
$tabs['upload'] = __('Upload');
@ -33,7 +33,7 @@ $tabs['popular'] = __('Popular');
$tabs['new'] = __('Newest');
$tabs['updated'] = __('Recently Updated');
$nonmenu_tabs = array('install', 'plugin-information', 'do_upload'); //Valid actions to perform which do not have a Menu item.
$nonmenu_tabs = array('plugin-information'); //Valid actions to perform which do not have a Menu item.
$tabs = apply_filters('install_plugins_tabs', $tabs );
$nonmenu_tabs = apply_filters('install_plugins_nonmenu_tabs', $nonmenu_tabs);
@ -62,7 +62,7 @@ $display_tabs = array();
foreach ( (array)$tabs as $action => $text ) {
$sep = ( end($tabs) != $text ) ? ' | ' : '';
$class = ( $action == $tab ) ? ' class="current"' : '';
$href = admin_url('plugin-install.php?tab='. $action);
$href = admin_url('plugin-install.php?tab=' . $action);
echo "\t\t<li><a href='$href'$class>$text</a>$sep</li>\n";
}
?>
@ -71,5 +71,4 @@ foreach ( (array)$tabs as $action => $text ) {
<?php do_action('install_plugins_' . $tab, $paged); ?>
</div>
<?php
include('admin-footer.php');
?>
include('admin-footer.php');

View File

@ -35,7 +35,7 @@ $tabs['featured'] = __('Featured');
$tabs['new'] = __('Newest');
$tabs['updated'] = __('Recently Updated');
$nonmenu_tabs = array('install', 'theme-information', 'do_upload'); //Valid actions to perform which do not have a Menu item.
$nonmenu_tabs = array('theme-information'); //Valid actions to perform which do not have a Menu item.
$tabs = apply_filters('install_themes_tabs', $tabs );
$nonmenu_tabs = apply_filters('install_themes_nonmenu_tabs', $nonmenu_tabs);

View File

@ -1,6 +1,6 @@
<?php
/**
* Update Plugin/Theme administration panel.
* Update/Install Plugin/Theme administration panel.
*
* @package WordPress
* @subpackage Administration
@ -9,134 +9,36 @@
/** WordPress Administration Bootstrap */
require_once('admin.php');
if ( ! current_user_can('update_plugins') )
wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
/**
* Plugin upgrade display.
*
* @since 2.5
*
* @param string $plugin Plugin
*/
function do_plugin_upgrade($plugin) {
global $wp_filesystem;
$url = wp_nonce_url("update.php?action=upgrade-plugin&plugin=$plugin", "upgrade-plugin_$plugin");
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
$error = true;
if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
$error = $wp_filesystem->errors;
request_filesystem_credentials($url, '', $error); //Failed to connect, Error and request again
return;
}
echo '<div class="wrap">';
echo screen_icon();
echo '<h2>' . __('Upgrade Plugin') . '</h2>';
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
echo '</div>';
return;
}
$was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
$result = wp_update_plugin($plugin, 'show_message');
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Plugin upgrade Failed') );
} else {
$plugin_file = $result;
show_message( __('Plugin upgraded successfully') );
if( $result && $was_activated ){
show_message(__('Attempting reactivation of the plugin'));
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>';
}
$update_actions = array(
'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>',
'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . attribute_escape(__('Goto plugins page')) . '" target="_parent">' . __('Return to Plugins page') . '</a>'
);
if ( $was_activated )
unset( $update_actions['activate_plugin'] );
$update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $plugin_file);
if ( ! empty($update_actions) )
show_message('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
}
echo '</div>';
}
/**
* Theme upgrade display.
*
* @since 2.5
*
* @param string $plugin Plugin
*/
function do_theme_upgrade($theme) {
global $wp_filesystem;
$url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($theme), 'upgrade-theme_' . urlencode($theme));
if ( false === ($credentials = request_filesystem_credentials($url)) )
return;
if ( ! WP_Filesystem($credentials) ) {
$error = true;
if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
$error = $wp_filesystem->errors;
request_filesystem_credentials($url, '', $error); //Failed to connect, Error and request again
return;
}
echo '<div class="wrap">';
echo screen_icon();
echo '<h2>' . __('Upgrade Theme') . '</h2>';
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
echo '</div>';
return;
}
//TODO: Is theme currently active?
$was_current = false; //is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
$result = wp_update_theme($theme, 'show_message');
if ( is_wp_error($result) ) {
show_message($result);
show_message( __('Installation Failed') );
} else {
//Result is the new plugin file relative to WP_PLUGIN_DIR
show_message( __('Theme upgraded successfully') );
if( $result && $was_current ){
show_message(__('Setting theme as Current'));
//TODO: Actually set it as active again.
//echo '<iframe style="border:0" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $result, 'activate-plugin_' . $result) .'"></iframe>';
}
}
echo '</div>';
}
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
if ( isset($_GET['action']) ) {
$plugin = isset($_GET['plugin']) ? trim($_GET['plugin']) : '';
$plugin = isset($_REQUEST['plugin']) ? trim($_REQUEST['plugin']) : '';
$theme = isset($_REQUEST['theme']) ? urldecode($_REQUEST['theme']) : '';
$action = isset($_GET['action']) ? $_GET['action'] : '';
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
if ( 'upgrade-plugin' == $action ) {
if ( ! current_user_can('update_plugins') )
wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
check_admin_referer('upgrade-plugin_' . $plugin);
$title = __('Upgrade Plugin');
$parent_file = 'plugins.php';
$submenu_file = 'plugins.php';
require_once('admin-header.php');
do_plugin_upgrade($plugin);
$nonce = 'upgrade-plugin_' . $plugin;
$url = 'update.php?action=upgrade-plugin&plugin=' . $plugin;
$upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( compact('title', 'nonce', 'url', 'plugin') ) );
$upgrader->upgrade($plugin);
include('admin-footer.php');
} elseif ('activate-plugin' == $action ) {
if ( ! current_user_can('update_plugins') )
wp_die(__('You do not have sufficient permissions to update plugins for this blog.'));
check_admin_referer('activate-plugin_' . $plugin);
if( ! isset($_GET['failure']) && ! isset($_GET['success']) ) {
wp_redirect( 'update.php?action=activate-plugin&failure=true&plugin=' . $plugin . '&_wpnonce=' . $_GET['_wpnonce'] );
@ -155,14 +57,145 @@ if ( isset($_GET['action']) ) {
include(WP_PLUGIN_DIR . '/' . $plugin);
}
iframe_footer();
} elseif ( 'install-plugin' == $action ) {
if ( ! current_user_can('install_plugins') )
wp_die(__('You do not have sufficient permissions to install plugins for this blog.'));
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; //for plugins_api..
check_admin_referer('install-plugin_' . $plugin);
$api = plugins_api('plugin_information', array('slug' => $plugin, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
if ( is_wp_error($api) )
wp_die($api);
$title = __('Plugin Install');
$parent_file = 'plugins.php';
$submenu_file = 'plugin-install.php';
require_once('admin-header.php');
$title = sprintf( __('Installing Plugin: %s'), $api->name . ' ' . $api->version );
$nonce = 'install-plugin_' . $plugin;
$url = add_query_arg( array(
'plugin' => $plugin,
'plugin_name' => $api->name . ' ' . $api->version,
'download_url' => $api->download_link
), 'update.php?action=install-plugin');
$type = 'web'; //Install plugin type, From Web or an Upload.
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api') ) );
$upgrader->install($api->download_link);
include('admin-footer.php');
} elseif ( 'upload-plugin' == $action ) {
if ( ! current_user_can('install_plugins') )
wp_die(__('You do not have sufficient permissions to install plugins for this blog.'));
check_admin_referer('plugin-upload');
$file_upload = new File_Upload_Upgrader('pluginzip', 'package');
$title = __('Upload Plugin');
$parent_file = 'plugins.php';
$submenu_file = 'plugin-install.php';
require_once('admin-header.php');
$title = sprintf( __('Installing Plugin from uploaded file: %s'), basename( $file_upload->filename ) );
$nonce = 'plugin-upload';
$url = add_query_arg(array('package' => $file_upload->filename ), 'update.php?action=upload-plugin');
$type = 'upload'; //Install plugin type, From Web or an Upload.
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('type', 'title', 'nonce', 'url') ) );
$upgrader->install( $file_upload->package );
include('admin-footer.php');
} elseif ( 'upgrade-theme' == $action ) {
if ( ! current_user_can('update_themes') )
wp_die(__('You do not have sufficient permissions to update themes for this blog.'));
check_admin_referer('upgrade-theme_' . $theme);
add_thickbox();
wp_enqueue_script('theme-preview');
$title = __('Upgrade Theme');
$parent_file = 'themes.php';
$submenu_file = 'themes.php';
require_once('admin-header.php');
do_theme_upgrade($theme);
include('admin-footer.php');
}
}
?>
$nonce = 'upgrade-theme_' . $theme;
$url = 'update.php?action=upgrade-theme&theme=' . $theme;
$upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( compact('title', 'nonce', 'url', 'theme') ) );
$upgrader->upgrade($theme);
include('admin-footer.php');
} elseif ( 'install-theme' == $action ) {
if ( ! current_user_can('install_themes') )
wp_die(__('You do not have sufficient permissions to install themes for this blog.'));
include_once ABSPATH . 'wp-admin/includes/theme-install.php'; //for themes_api..
check_admin_referer('install-theme_' . $theme);
$api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false) ) ); //Save on a bit of bandwidth.
if ( is_wp_error($api) )
wp_die($api);
add_thickbox();
wp_enqueue_script('theme-preview');
$title = __('Install Themes');
$parent_file = 'themes.php';
$submenu_file = 'theme-install.php';
require_once('admin-header.php');
$title = sprintf( __('Installing theme: %s'), $api->name . ' ' . $api->version );
$nonce = 'install-theme_' . $theme;
$url = add_query_arg( array(
'theme' => $theme,
'theme_name' => $api->name . ' ' . $api->version,
'download_url' => $api->download_link
), 'update.php?action=install-theme');
$type = 'web'; //Install theme type, From Web or an Upload.
$upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api') ) );
$upgrader->install($api->download_link);
include('admin-footer.php');
} elseif ( 'upload-theme' == $action ) {
if ( ! current_user_can('install_themes') )
wp_die(__('You do not have sufficient permissions to install themes for this blog.'));
check_admin_referer('theme-upload');
$file_upload = new File_Upload_Upgrader('themezip', 'package');
$title = __('Upload Theme');
$parent_file = 'themes.php';
$submenu_file = 'theme-install.php';
add_thickbox();
wp_enqueue_script('theme-preview');
require_once('admin-header.php');
$title = sprintf( __('Installing Theme from uploaded file: %s'), basename( $file_upload->filename ) );
$nonce = 'theme-upload';
$url = add_query_arg(array('package' => $file_upload->filename), 'update.php?action=upload-theme');
$type = 'upload'; //Install plugin type, From Web or an Upload.
$upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('type', 'title', 'nonce', 'url') ) );
$upgrader->install( $file_upload->package );
include('admin-footer.php');
} else {
do_action('update-custom_' . $action);
}
}