Skip to content

Instantly share code, notes, and snippets.

@davebonds
Last active June 14, 2016 01:26
Show Gist options
  • Save davebonds/af946cf56c983d7cbb2d093e74cc7dc7 to your computer and use it in GitHub Desktop.
Save davebonds/af946cf56c983d7cbb2d093e74cc7dc7 to your computer and use it in GitHub Desktop.
ManageWP: Update must use plugin from S3
<?php
function update_myplugin() {
// Get current version number
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$version = get_plugin_data(WPMU_PLUGIN_DIR .'/myplugin.php');
// Check if current version is latest
if($version['Version'] < 1.0) {
// Use wp_remote_get to fetch the data and store it to a file
$response = wp_remote_get('https://s3.amazonaws.com/path/to/zipfile/myplugin.zip', array( 'stream' => true, 'filename' => WPMU_PLUGIN_DIR . '/myplugin.zip') );
// Create the name of the file and the declare the directory and path
$file = WPMU_PLUGIN_DIR . '/myplugin.zip';
// Require the filesystem API class
require_once(ABSPATH .'/wp-admin/includes/file.php');
// Make sure we have permissions
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
// Initialize the filesystem API
if ( ! WP_Filesystem($creds) ) {
/* any problems and we exit */
return false;
}
global $wp_filesystem;
// Remove old plugin directory
$remove_old_plugin = $wp_filesystem->rmdir( WPMU_PLUGIN_DIR . '/myplugin', true );
if($remove_old_plugin !== true) {
/* remove failed. handle error */
echo "Failed to remove old directory\r\n";
} else {
echo "Old directory removed\r\n";
// Delete loader mu-plugins/myplugin.php file
$delete_loader = $wp_filesystem->delete( WPMU_PLUGIN_DIR . '/myplugin.php' );
}
// Make sure path is correct
$plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WPMU_PLUGIN_DIR);
// Unzip file
$unzip = unzip_file($file, $plugin_path);
if($unzip !== true) {
/* unzip failed. Handle Error */
echo "Failed to unzip file\r\n";
} else {
echo "Plugin updated successfully\r\n";
// Move loader myplugin.php file to mu-plugin root
$move_myplugin = $wp_filesystem->move( WPMU_PLUGIN_DIR . '/myplugin/myplugin.php', WPMU_PLUGIN_DIR . '/myplugin.php' );
}
// Delete zip file
$delete_zip = $wp_filesystem->delete( $file );
// Delete loader myplugin.php from inside myplugin folder
$delete_loader = $wp_filesystem->delete( WPMU_PLUGIN_DIR . '/myplugin/myplugin.php' );
return $unzip;
} else {
echo 'Plugin is at latest version';
return false;
}
}
update_myplugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment