Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created October 16, 2019 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/4deb10c994056ab063b7d19f0209af2c to your computer and use it in GitHub Desktop.
Save wpmudev-sls/4deb10c994056ab063b7d19f0209af2c to your computer and use it in GitHub Desktop.
WPMUDEV MU Plugins Upgrader
<?php
/**
* Plugin Name: WPMUDEV MU Plugins Upgrader
* Version: 1.0.0
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_mu_plugin_upgrader_func' );
function wpmudev_mu_plugin_upgrader_func(){
if( ! is_admin() ) return;
if( ! class_exists('WPMUDEV_MU_Plugins_Upgrader') ){
class WPMUDEV_MU_Plugins_Upgrader{
private $version = '1.0.0';
private $slug;
private $plugin; // plugin slug
private $plugin_data; // plugin data
private $plugin_API_result; // holds data
private $api_url = 'https://inc-gist.wpmudev.host/plugins/';
function __construct() {
global $pagenow;
$this->plugin = plugin_basename( __FILE__ );
add_filter( "pre_set_site_transient_update_plugins", array( $this, "set_transitent" ) );
add_filter( "plugins_api", array( $this, "set_plugin_info" ), 10, 3 );
add_action( 'upgrader_process_complete', array( $this, 'move_mu_plugins'), 10, 2 );
// add_filter( "upgrader_post_install", array( $this, "post_install" ), 10, 3 );
if( ( 'update-core.php' == $pagenow && isset( $_GET['force-check'] ) ) || isset( $_GET['wpmudev-force-mu-plugin-upgrader'] ) ){
$transient_key = "wpmudev_". basename( $this->plugin );
if ( is_multisite() ) {
$cache = delete_site_transient( $transient_key );
} else {
$cache = delete_transient( $transient_key );
}
}
}
private function get_list_mu_plugins(){
if( ! @is_dir( WPMU_PLUGIN_DIR ) ){
return;
}
return scandir( WPMU_PLUGIN_DIR );
// return !empty($result) ? array_diff( $result, array('.','..','.git','.svn', '.DS_Store') ) : array();
}
// Get information regarding our plugin
private function get_release_info() {
// Only do this once
if ( !empty( $this->plugin_API_result ) ) {
return;
}
$basename = basename( $this->plugin );
$transient_key = "wpmudev_{$basename}";
if ( is_multisite() ) {
$cache = get_site_transient( $transient_key );
} else {
$cache = get_transient( $transient_key );
}
$this->plugin_data = array(
'Name' => 'WPMUDEV MU Plugins Upgrader',
'PluginURI' => 'https://premium.wpmudev.org',
'Version' => $this->version,
'Description' => 'WPMUDEV MU Plugins Upgrader',
'Author' => 'Thobk @ WPMUDEV',
'AuthorURI' => 'https://premium.wpmudev.org',
'TextDomain' => 'wpmudev',
);
if( ! empty( $cache ) ){
$this->plugin_API_result = $cache;
return;
}
// Query the API
$url = $this->api_url . basename( $basename ) ."/info.json";
// Get the results
$http_args = array(
'timeout' => ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 5,
'user-agent' => 'WPMUDEV/1.0; ' . home_url( '/' ),
);
$this->plugin_API_result = wp_remote_retrieve_body( wp_remote_get( $url, $http_args ) );
if ( !empty( $this->plugin_API_result ) ) {
$this->plugin_API_result = @json_decode( $this->plugin_API_result );
// remove filter
remove_filter( 'pre_set_site_transient_update_plugins', array( $this, "set_transitent" ) );
}
// Use only the latest release
if ( is_array( $this->plugin_API_result ) ) {
$this->plugin_API_result = $this->plugin_API_result[0];
}
if( is_multisite() ){
set_site_transient( $transient_key, $this->plugin_API_result, WEEK_IN_SECONDS);
}else{
set_transient( $transient_key, $this->plugin_API_result, WEEK_IN_SECONDS);
}
}
public function get_mu_plugin_version( $mu_plugin ){
$plugin_data = get_file_data( trailingslashit( WPMU_PLUGIN_DIR ) . $mu_plugin, array(
'Version' => 'Version'
));
return ! empty( $plugin_data['Version'] ) ? $plugin_data['Version'] : '1.0';
}
// Push in plugin version information to get the update notification
public function set_transitent( $transient ) {
// If we have checked the plugin data before, don't re-check
if ( empty( $transient->checked ) ) {
return $transient;
}
// Get plugin release information
$this->get_release_info();
// Update the transient to include our updated plugin data
$should_update = 0;
if ( $this->plugin_API_result && isset( $transient->checked[$this->plugin] ) ) {
$should_update = version_compare( $this->plugin_API_result->version, $transient->checked[$this->plugin], '>' );
if( $should_update ){
$transient->response[$this->plugin] = $this->retrive_plugin_info();
}
if( ! $should_update && ! empty( $this->plugin_API_result->mu_plugins ) ){
$mu_plugins = $this->get_list_mu_plugins();
if( $mu_plugins ){
// $mu_plugins_data = get_site_option('wpmudev_mu_plugins_data', array());
foreach( $this->plugin_API_result->mu_plugins as $mu_plugin => $plugin_info ){
if( in_array( $mu_plugin, $mu_plugins ) ){
// $current_version = isset( $mu_plugins_data[ $mu_plugin ] ) ? $mu_plugins_data[ $mu_plugin ] : $this->get_mu_plugin_version( $mu_plugin );
$current_version = $this->get_mu_plugin_version( $mu_plugin );
if( version_compare( $plugin_info->version, $current_version, '>' ) ){
$plugin_info->zip_url = $this->plugin_API_result->zip_url;
$this->plugin_API_result = $plugin_info;
// $mu_plugins_data[ $mu_plugin ] = $plugin_info->version;
$should_update = 1;
break;
}
}
}
}
if( $should_update ){
// update_site_option( 'wpmudev_mu_plugins_data', $mu_plugins_data );
$transient->response[$this->plugin] = $this->retrive_plugin_info();
}
}
}
return $transient;
}
public function retrive_plugin_info(){
$obj = new stdClass();
$obj->slug = dirname( $this->plugin );
$obj->plugin = $this->plugin;
$obj->new_version = version_compare( $this->plugin_API_result->version, $this->version, '>' ) ? '2.0' : $this->version;
$obj->url = $this->plugin_data["PluginURI"];
// $obj->name = $this->plugin_data['Name'];
$obj->package = $this->plugin_API_result->zip_url;
return $obj;
}
// Push in plugin version information to display in the details lightbox
public function set_plugin_info( $res, $action, $response ) {
// do nothing if this is not about getting plugin information
if( $action !== 'plugin_information' ){
return false;
}
// If nothing is found, do nothing
if ( empty( $response->slug ) || $response->slug !== dirname( $this->plugin ) ) {
return $res;
}
// Get plugin release information
$this->get_release_info();
if( $this->plugin_API_result ){
// Add our plugin information
$res = new stdClass();
$res->last_updated = $this->plugin_API_result->last_updated;
$res->slug = dirname( $this->plugin );
$res->name = $this->plugin_data["Name"];
// $res->plugin = $this->plugin;
$res->author = $this->plugin_data["Author"];
$res->homepage = $this->plugin_data["PluginURI"];
$version = $this->plugin_API_result->version;
// Create tabs in the lightbox
$description = $this->plugin_API_result->description;
$changelog = $this->plugin_API_result->changelog;
if( ! empty( $this->plugin_API_result->mu_plugins ) ){
$should_check = 0;
// $should_update_mu_data = 0;
if( ! version_compare($version, $this->version, '>') ){
$should_check = 1;
$mu_plugins = $this->get_list_mu_plugins();
// $mu_plugins_data = get_site_option('wpmudev_mu_plugins_data', array());
}
$description .= '<h3>List all MU Plugins Available:</h3><ol style="font-size:13px">';
foreach( $this->plugin_API_result->mu_plugins as $mu_plugin => $plugin_info ){
if( in_array( $mu_plugin, $mu_plugins ) ){
$description .= sprintf('<li style="margin-bottom:25px"><strong>%s</strong> - version: %s - gist: <a href="%s">source</a>:<div style="padding-left:10px">', $plugin_info->name, $plugin_info->version, $plugin_info->gist );
// $current_version = isset( $mu_plugins_data[ $mu_plugin ] ) ? $mu_plugins_data[ $mu_plugin ] : $this->get_mu_plugin_version( $mu_plugin );
$current_version = $this->get_mu_plugin_version( $mu_plugin );
if( $should_check && version_compare( $plugin_info->version, $current_version, '>' ) ){
$plugin_info->zip_url = $this->plugin_API_result->zip_url;
$this->plugin_API_result = $plugin_info;
$version = $plugin_info->version;
// $mu_plugins_data[ $mu_plugin ] = $version;
$should_check = 0;
// $should_update_mu_data = 1;
$description .= '<p><strong>** Have a new version of this MU plugin! **</strong></p>';
}
if( ! empty( $plugin_info->description ) ){
$description .= $plugin_info->description;
}
if( ! empty( $plugin_info->changelog ) ){
$description .= $plugin_info->changelog;
}
$description .= '</div></li>';
}
}
$description .='</ol>';
// This is our release download zip file
$res->download_link = $this->plugin_API_result->zip_url;
// if( $should_update_mu_data ){
// update_site_option( 'wpmudev_mu_plugins_data', $mu_plugins_data );
// }
}
// update version
$res->version = $version;
$res->sections = array(
'description' => $description
);
if( $changelog ){
$res->sections['changelog'] = $changelog;
}
// // Gets the required version of WP if available
// $matches = null;
// preg_match( "/requires:\s([\d\.]+)/i", $changelog, $matches );
// if ( !empty( $matches ) ) {
// if ( is_array( $matches ) ) {
// if ( count( $matches ) > 1 ) {
// $res->requires = $matches[1];
// }
// }
// }
// // Gets the tested version of WP if available
// $matches = null;
// preg_match( "/tested:\s([\d\.]+)/i", $changelog, $matches );
// if ( !empty( $matches ) ) {
// if ( is_array( $matches ) ) {
// if ( count( $matches ) > 1 ) {
// $res->tested = $matches[1];
// }
// }
// }
return $res;
}
// remove filter
remove_filter( "plugins_api", array( $this, "set_plugin_info" ));
return false;
}
public function move_mu_plugins( $upgrader_object, $options ){
if ($options['action'] == 'update' ){
if( $options['type'] === 'plugin' && isset( $options['plugins'] ) && in_array( $this->plugin, $options['plugins'] ) ){
$mu_plugin_dir = $upgrader_object->result['destination'] .'/mu-plugins/';
if( @file_exists( $mu_plugin_dir ) ){
$mu_plugins = $this->get_list_mu_plugins();
$mu_plugins_upgrade = scandir( $mu_plugin_dir );
if( $mu_plugins_upgrade ){
$mu_plugins_upgrade = array_diff( $mu_plugins_upgrade, array('.','..','.git','.svn', '.DS_Store') );
$mu_plugins = array_intersect($mu_plugins, $mu_plugins_upgrade);
$is_live = ( ! isset( $_SERVER['SERVER_NAME'] ) || $_SERVER['SERVER_NAME'] !== 'wp.local' );
if( $mu_plugins ){
foreach( $mu_plugins as $mu_plugin ){
$mu_plugin_file = $mu_plugin_dir . $mu_plugin;
if( @file_exists( $mu_plugin_file ) ){
if( $is_live ){
@rename( $mu_plugin_file, trailingslashit( WPMU_PLUGIN_DIR ) . $mu_plugin );
}else{
@copy( $mu_plugin_file, trailingslashit( WPMU_PLUGIN_DIR ) . $mu_plugin );
}
}
}
}
// unlink mu-plugins dir inside plugin
if( $is_live && $this->can_access_file() ){
global $wp_filesystem;
$wp_filesystem->delete( $mu_plugin_dir, true );
}
}
}
}
}
}
public function can_access_file($folder = 'wp-content'){
static $can_access_file;
if( null === $can_access_file ){
if( ! function_exists('request_filesystem_credentials') ){
require_once( ABSPATH .'/wp-admin/includes/file.php');
}
$creds = request_filesystem_credentials( site_url() .'/'. $folder, '', false, false, array());
/* initialize the API */
$can_access_file = false;
if ( WP_Filesystem($creds) ) {
/* any problems and we exit */
$can_access_file = true;
}
}
return $can_access_file;
}
}
new WPMUDEV_MU_Plugins_Upgrader( __FILE__ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment