Created
October 5, 2011 19:31
-
-
Save stas/1265419 to your computer and use it in GitHub Desktop.
WordPress Plugins Dependency Manger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once ABSPATH . '/wp-admin/includes/file.php'; | |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
require_once ABSPATH . '/wp-admin/includes/plugin-install.php'; | |
require_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; | |
/** | |
* Main Deps Manager Class | |
*/ | |
class DepManager { | |
/** | |
* Plugins we depend on | |
*/ | |
static $deps = array(); | |
/** | |
* Static constructor | |
*/ | |
function init() { | |
if ( self::check_fs() ) | |
self::check_packages(); | |
else | |
set_transient( 'depman_fs_error', true ); | |
add_action( 'admin_notices', array( __CLASS__, 'notifications' ) ); | |
} | |
function check_fs() { | |
$method = get_filesystem_method( false, false ); | |
$creds = array_filter( get_option( 'ftp_credentials', array() ) ); | |
if ( !$method || $method != 'direct' ) | |
return false; | |
return true; | |
} | |
/** | |
* Bring some notifications if any errors occur | |
*/ | |
function notifications() { | |
$errors = get_transient( 'depman_deps_errors' ); | |
if ( get_transient( 'depman_fs_error' ) ) { | |
$errors[] = __( 'Current settings do not allow automatic installation of plugins.', 'depman' ); | |
$errors[] = __( 'Please visit theme options page for further instructions.', 'depman' ); | |
} | |
if ( !current_user_can( 'update_plugins' ) ) | |
$errors[] = __( 'Current theme requires some plugins installed, please ask an administrator to log in.', 'depman' ); | |
if ( !empty( $errors ) ) | |
echo '<div class="error">' . self::walk_errors( $errors ) . '</div>'; | |
delete_transient( 'depman_deps_errors' ); | |
delete_transient( 'depman_fs_error' ); | |
} | |
/** | |
* Walk through error messages | |
* | |
* @param $messages, Mixed array of messages | |
*/ | |
function walk_errors( $messages ) { | |
$ps = ''; | |
foreach ( $messages as $m ) { | |
$ps .= "<p>{$m}</p>"; | |
} | |
return $ps; | |
} | |
/** | |
* check_packages() | |
* Checks available installed packages for active dependencies | |
* On failure fires __CLASS__::install_dep( $dep ) | |
*/ | |
function check_packages() { | |
if ( !current_user_can( 'update_plugins' ) ) | |
return; | |
$available = get_plugins(); | |
foreach( self::$deps as $dep ) { | |
$dep_data = plugins_api( 'plugin_information', | |
array( | |
'slug' => $dep, | |
'fields' => array( 'sections' => false ) | |
) | |
); | |
if ( !is_wp_error( $dep_data ) ) | |
foreach ( $available as $av_dep ) { | |
if ( $av_dep['Name'] == $dep_data->name ) | |
$dep_data->dep_installed = true; | |
} | |
if ( !isset( $dep_data->dep_installed ) || !$dep_data->dep_installed ) { | |
$result = self::install_dep( $dep_data ); | |
if ( $result != true ) { | |
set_transient( 'depman_deps_errors', $result ); | |
update_option( 'depman_deps_welcomed', false ); | |
break; | |
} | |
} | |
// Plugins installed | |
update_option( 'depman_deps_welcomed', true ); | |
} | |
} | |
/** | |
* load_deps_info() | |
* Load all the dependencies details from WordPress.org API | |
* | |
* @return Mixed, deps data | |
*/ | |
function load_deps_info() { | |
$plugins = array(); | |
foreach( self::$deps as $dep ) { | |
$dep_data = plugins_api( 'plugin_information', | |
array( | |
'slug' => $dep, | |
'fields' => array( | |
'sections' => false, | |
'description' => true | |
) | |
) | |
); | |
if ( !is_wp_error( $dep_data ) ) | |
$plugins[] = $dep_data; | |
} | |
return $plugins; | |
} | |
/** | |
* install_dep( $dep ) | |
* Installs a dependency | |
* | |
* @param Mixed, $dep, the dependency information object | |
* @return Bool, true on success, Mixed an array of error informations on failure | |
*/ | |
function install_dep( $dep = null ) { | |
$errors = array(); | |
$plugin = false; | |
if ( !$dep ) | |
return true; | |
$result = install_plugin_install_status( $dep ); | |
if ( isset( $result['status'] ) && $result['status'] == 'install' ) { | |
$installer = new Plugin_Upgrader( | |
new DepManagerSkin ( | |
array ( | |
'plugin' => $dep->slug, | |
'api' => $dep | |
) | |
) | |
); | |
$installer->install( $dep->download_link ); | |
$plugin = $installer->plugin_info(); | |
if ( !empty( $installer->error ) ) | |
$errors[] = $installer->error; | |
} | |
if ( empty( $errors ) ) { | |
// To flush installed plugins cache | |
wp_cache_flush(); | |
$errors[] = activate_plugin( $plugin ); | |
return true; | |
} | |
return array_filter( $errors ); | |
} | |
} | |
/** | |
* Helpers/Wrappers | |
*/ | |
class DepManagerSkin extends Plugin_Installer_Skin { | |
function header() {} | |
function error( $errors ) {} | |
function feedback( $string ) {} | |
function before() {} | |
function after() {} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example usage of dependency manager | |
* Link to /?install_deps to fire the dep-manager | |
*/ | |
function load_depman() { | |
// Check if deps were installed | |
$deps_welcomed = get_option( 'depman_deps_welcomed', false ); | |
if ( !$deps_welcomed && isset( $_GET['install_deps'] ) ) { | |
// Try to install the deps | |
require_once dirname( __FILE__ ) . '/deps-manager.class.php'; | |
// Set deps | |
DepManager::$deps = array( | |
'sharedaddy', | |
'post-types-calendar' | |
); | |
DepManager::init(); | |
} | |
} | |
add_action( 'admin_init', 'load_depman' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment