Skip to content

Instantly share code, notes, and snippets.

@dipakcg
Last active September 13, 2020 06:27
Show Gist options
  • Save dipakcg/bb45bdad65463ab4fc0dce81f5980439 to your computer and use it in GitHub Desktop.
Save dipakcg/bb45bdad65463ab4fc0dce81f5980439 to your computer and use it in GitHub Desktop.
Check WordPress Plugin Dependency
/* Check plugin dependency */
dcg_check_plugin_dependency('Easy Digital Downloads - Product Gallery for Frontend Submissions', 'Easy Digital Downloads - Frontend Submissions', 'edd-fes/edd-fes.php', '', null);
dcg_check_plugin_dependency('Easy Digital Downloads - Product Gallery for Frontend Submissions', 'Olam Multiple Image', 'olam-multiple-images/olam-multiple-images.php', '', null);
/**
* Verify if a plugin is active, if not deactivate the actual plugin an show an error
* @param [string] $my_plugin_name
* The plugin name trying to activate. The name of this plugin
* Ex:
* WooCommerce new Shipping Method
*
* @param [string] $dependency_plugin_name
* The dependency plugin name.
* Ex:
* WooCommerce.
*
* @param [string] $path_to_plugin
* Path of the plugin to verify with the format 'dependency_plugin/dependency_plugin.php'
* Ex:
* woocommerce/woocommerce.php
*
* @param [string] $textdomain
* Text domain to looking the localization (the translated strings)
*
* @param [string] $version_to_check
* Optional, verify certain version of the dependent plugin
*/
function dcg_check_plugin_dependency($my_plugin_name, $dependency_plugin_name, $path_to_plugin, $textdomain = '', $version_to_check = null) {
# Needed to the function "deactivate_plugins" works
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( !is_plugin_active( $path_to_plugin ) )
{
# Deactivate the current plugin
deactivate_plugins( plugin_basename( __FILE__ ) );
# Show an error alert on the admin area
add_action( 'admin_notices', function() use($my_plugin_name, $dependency_plugin_name, $textdomain)
{
?>
<div class="updated error">
<p>
<?php
echo sprintf(
__( '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">Sorry! The plugin <strong><em>%s</em></strong> requires the plugin <strong><em>%s</em></strong> to be installed and active.</span>', $textdomain ),
$my_plugin_name, $dependency_plugin_name
);
echo sprintf(
__( '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;"><strong><em>%s</em></strong> has been deactivated.</span>', $textdomain ),
$my_plugin_name
);
?>
</p>
</div>
<?php
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
} );
}
else {
# If version to check is not defined do nothing
if($version_to_check === null)
return;
# Get the plugin dependency info
$depPlugin_data = get_plugin_data( WP_PLUGIN_DIR.'/'.$path_to_plugin);
# Compare version
$error = !version_compare ( $depPlugin_data['Version'], $version_to_check, '>=') ? true : false;
if($error) {
# Deactivate the current plugin
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', function() use($my_plugin_name, $dependency_plugin_name, $version_to_check, $textdomain)
{
?>
<div class="updated error">
<p>
<?php
echo sprintf(
__( '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">Sorry! The plugin <strong><em>%s</em></strong> requires <strong>version %s</strong> or newer of the plugin <strong><em>%s</em>.</strong></span>', $textdomain ),
$my_plugin_name,
$version_to_check,
$dependency_plugin_name
);
echo sprintf(
__( '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;"><strong><em>%s</em></strong> has been deactivated.</span>', $textdomain ),
$my_plugin_name
);
?>
</p>
</div>
<?php
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
} );
}
}# else
}
@dipakcg
Copy link
Author

dipakcg commented Sep 13, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment