Return WP_Error from download_url() on HTTP Errors. Fix cases where current plugin filename differs from the WordPress.org slug. Props DD32. see #5586
git-svn-id: https://develop.svn.wordpress.org/trunk@7547 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ad508b0252
commit
69139c4672
@ -192,25 +192,30 @@ function wp_handle_upload( &$file, $overrides = false ) {
|
|||||||
* Downloads a url to a local file using the Snoopy HTTP Class
|
* Downloads a url to a local file using the Snoopy HTTP Class
|
||||||
*
|
*
|
||||||
* @param string $url the URL of the file to download
|
* @param string $url the URL of the file to download
|
||||||
* @return mixed false on failure, string Filename on success.
|
* @return mixed WP_Error on failure, string Filename on success.
|
||||||
*/
|
*/
|
||||||
function download_url( $url ) {
|
function download_url( $url ) {
|
||||||
//WARNING: The file is not automatically deleted, The script must unlink() the file.
|
//WARNING: The file is not automatically deleted, The script must unlink() the file.
|
||||||
if( ! $url )
|
if( ! $url )
|
||||||
return false;
|
return new WP_Error('http_no_url', __('Invalid URL Provided'));
|
||||||
|
|
||||||
$tmpfname = tempnam(get_temp_dir(), 'wpupdate');
|
$tmpfname = tempnam(get_temp_dir(), 'wpupdate');
|
||||||
if( ! $tmpfname )
|
if( ! $tmpfname )
|
||||||
return false;
|
return new WP_Error('http_no_file', __('Could not create Temporary file'));
|
||||||
|
|
||||||
$handle = @fopen($tmpfname, 'w');
|
$handle = @fopen($tmpfname, 'w');
|
||||||
if( ! $handle )
|
if( ! $handle )
|
||||||
return false;
|
return new WP_Error('http_no_file', __('Could not create Temporary file'));
|
||||||
|
|
||||||
require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
|
require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
|
||||||
$snoopy = new Snoopy();
|
$snoopy = new Snoopy();
|
||||||
$snoopy->fetch($url);
|
$snoopy->fetch($url);
|
||||||
|
|
||||||
|
if( $snoopy->status != '200' ){
|
||||||
|
fclose($handle);
|
||||||
|
unlink($tmpfname);
|
||||||
|
return new WP_Error('http_404', trim($snoopy->response_code));
|
||||||
|
}
|
||||||
fwrite($handle, $snoopy->results);
|
fwrite($handle, $snoopy->results);
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|
||||||
|
@ -176,8 +176,8 @@ function wp_update_plugin($plugin, $feedback = '') {
|
|||||||
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
|
apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
|
||||||
$file = download_url($package);
|
$file = download_url($package);
|
||||||
|
|
||||||
if ( !$file )
|
if ( is_wp_error($file) )
|
||||||
return new WP_Error('download_failed', __('Download failed.'));
|
return new WP_Error('download_failed', __('Download failed.'), $file->get_error_message());
|
||||||
|
|
||||||
$working_dir = $base . 'wp-content/upgrade/' . basename($plugin, '.php');
|
$working_dir = $base . 'wp-content/upgrade/' . basename($plugin, '.php');
|
||||||
|
|
||||||
@ -194,11 +194,11 @@ function wp_update_plugin($plugin, $feedback = '') {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Once installed, delete the package
|
// Once extracted, delete the package
|
||||||
unlink($file);
|
unlink($file);
|
||||||
|
|
||||||
if ( is_plugin_active($plugin) ) {
|
if ( is_plugin_active($plugin) ) {
|
||||||
//Deactivate the plugin
|
//Deactivate the plugin silently, Prevent deactivation hooks from running.
|
||||||
apply_filters('update_feedback', __('Deactivating the plugin'));
|
apply_filters('update_feedback', __('Deactivating the plugin'));
|
||||||
deactivate_plugins($plugin, true);
|
deactivate_plugins($plugin, true);
|
||||||
}
|
}
|
||||||
@ -209,7 +209,7 @@ function wp_update_plugin($plugin, $feedback = '') {
|
|||||||
$plugin_dir = trailingslashit($plugin_dir);
|
$plugin_dir = trailingslashit($plugin_dir);
|
||||||
|
|
||||||
// If plugin is in its own directory, recursively delete the directory.
|
// If plugin is in its own directory, recursively delete the directory.
|
||||||
if ( strpos($plugin, '/') && $plugin_dir != $base . PLUGINDIR . '/' )
|
if ( strpos($plugin, '/') && $plugin_dir != $base . PLUGINDIR . '/' ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
|
||||||
$deleted = $wp_filesystem->delete($plugin_dir, true);
|
$deleted = $wp_filesystem->delete($plugin_dir, true);
|
||||||
else
|
else
|
||||||
$deleted = $wp_filesystem->delete($base . PLUGINDIR . "/$plugin");
|
$deleted = $wp_filesystem->delete($base . PLUGINDIR . "/$plugin");
|
||||||
@ -226,18 +226,23 @@ function wp_update_plugin($plugin, $feedback = '') {
|
|||||||
return new WP_Error('install_failed', __('Installation failed'));
|
return new WP_Error('install_failed', __('Installation failed'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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
|
// Remove working directory
|
||||||
$wp_filesystem->delete($working_dir, true);
|
$wp_filesystem->delete($working_dir, true);
|
||||||
|
|
||||||
// Force refresh of plugin update information
|
// Force refresh of plugin update information
|
||||||
delete_option('update_plugins');
|
delete_option('update_plugins');
|
||||||
|
|
||||||
//Return the new plugin file.
|
if( empty($filelist) )
|
||||||
if ( ! preg_match('!/([a-z0-9\-]+)/?$!i', $working_dir, $mat) )
|
return false; //We couldnt find any files in the working dir
|
||||||
return false;
|
|
||||||
$plugin = get_plugins('/' . $mat[1]); //Pass it with a leading slash
|
$folder = $filelist[0];
|
||||||
$list = array_keys($plugin);
|
$plugin = get_plugins('/' . $folder); //Pass it with a leading slash, search out the plugins in the folder,
|
||||||
return $mat[1] . '/' . $list[0]; //Pass it without a leading slash.
|
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
|
||||||
|
|
||||||
|
return $folder . '/' . $pluginfiles[0]; //Pass it without a leading slash as WP requires
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user