Skip to content

Instantly share code, notes, and snippets.

@atxulo
Created April 16, 2014 14:13
Show Gist options
  • Save atxulo/10882282 to your computer and use it in GitHub Desktop.
Save atxulo/10882282 to your computer and use it in GitHub Desktop.
Download latest version of a WordPress plugin
<?php
/**
* Script to download latest version of a WordPress plugin .
* This script must be in the folder where WordPress is installed.
*
* @param slug (name) of the plugin. See http://plugins.svn.wordpress.org/
* @return exit code 0 if downloaded, 1 if already exists, 2 if error
*
* @author enekogb
*
* TODO Improve
*/
if (! function_exists('plugins_api')) {
require_once(dirname(__FILE__).'/wp-load.php');
require_once(dirname(__FILE__).'/wp-admin/includes/plugin-install.php');
require_once(dirname(__FILE__).'/wp-includes/http.php');
}
// Search plugin using WordPress api
// see http://code.tutsplus.com/tutorials/communicating-with-the-wordpressorg-plugin-api--wp-33069
$call_api = plugins_api('plugin_information', array('slug' => $argv[1]));
if (is_wp_error($call_api)) {
echo 'Error getting '.$argv[1].' plugin info: '.print_r($call_api->get_error_message(), true);
exit(2);
} else {
// Get download link
$url = $call_api -> download_link;
// Extract filename
$url_parts = parse_url($url);
$file_name = dirname(__FILE__)."/wp-content/plugins/".basename($url_parts['path']);
// Check file before download
if (file_exists($file_name)) {
echo "El plugin ya esta descargado en su ultima version: ".$file_name;
exit(1);
} else {
// Get file from url
$param = array( 'timeout' => 500000); // TODO Timeout as a parameter, more here: http://codex.wordpress.org/Function_Reference/wp_remote_get
$response = wp_remote_get($url, $param);
// Check error before saving to disk
if (is_wp_error($response)) {
echo 'Error downloading '.$url.' : '.print_r($response->get_error_message(), true);
print_r($response, true);
exit (2);
} else {
// Save file to disk and exit
$zip =$response['body'];
$fp = fopen($file_name, "w");
fwrite($fp, $zip);
fclose($fp);
exit (0);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment