Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Created January 21, 2015 23:00
Show Gist options
  • Save joshuadavidnelson/973677ce547b96aae66b to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/973677ce547b96aae66b to your computer and use it in GitHub Desktop.
Check for plugin requirements are met, deactivate and send up admin notice if not
<?php
/**
* Checks for dependencies, if not present it deactivates the plugin and sends up an admin notices
*
* Intended to be placed within a plugin singleton class, calling self::$instance->includes() or similar.
* Could be adapted to be in a regular class or classless
*
* @author Joshua David Nelson, josh@joshuadnelson.com
* @see http://10up.com/blog/2012/wordpress-plug-in-self-deactivation/
*/
/**
* Include required files and starts the plugin
*
* @access private
* @since 1.0.0
* @return void
*/
private function includes() {
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
public function plugins_loaded() {
if ( ! self::is_supported() ) {
add_action( 'admin_notices', array( $this, 'deactivate_admin_notice' ) );
add_action( 'admin_init', array( $this, 'plugin_deactivate' ) );
}
}
private function plugin_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
private static function is_supported() {
// Check for required classes/plugins, for example GFCommon for Gravity Forms
if( class_exists( 'GFCommon' ) ) {
return true;
}
return false;
}
/**
* Output admin notices
*/
public function deactivate_admin_notice( $message = '', $class = 'error' ) {
if( empty( $message ) ) {
$message = __( 'Plugin has been deactived. It requires Gravity Forms', 'text-domain' );
}
echo '<div class="' . $class . '"><p>' . $message . '</p></div>';
// Removes "plugin activated" message
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment