Skip to content

Instantly share code, notes, and snippets.

@dazecoop
Last active July 21, 2020 20:53
Show Gist options
  • Save dazecoop/179e09d67036c724bf668287e9b19407 to your computer and use it in GitHub Desktop.
Save dazecoop/179e09d67036c724bf668287e9b19407 to your computer and use it in GitHub Desktop.
Check specific WordPress plugins are activated, and alert if not
<?php
/**
* Check a list of plugins are activated, and alert if not.
* @param required_plugins[] should be updated to include the list of required plugins.
* @param `?show_active_plugins` can be appended to any URL within wp-admin to show a list of currently active plugins.
*/
class checkRequiredPlugin {
private $required_plugins = [
'woocommerce/woocommerce.php',
];
private $active_plugins = [];
private $dependant_plugins = [];
function __construct() {
$this->check_required_plugins();
}
function active_plugins() {
if (!function_exists('is_plugin_active')) {
include_once ABSPATH.'wp-admin/includes/plugin.php';
}
return (array)get_option('active_plugins', []);
}
function check_required_plugins() {
$this->active_plugins = $this->active_plugins();
if (isset($_GET['show_active_plugins'])) {
echo '<pre style="position:fixed; right:20px; z-index:999; padding:20px; background:white; box-shadow:0 0 60px rgba(0,0,0,.3);">';
print_r( $this->active_plugins );
echo '</pre>';
}
$this->dependant_plugins = array_intersect($this->active_plugins, $this->required_plugins);
if (count($this->dependant_plugins) !== count($this->required_plugins)) {
add_action('admin_notices', [$this, 'required_plugins_notice']);
}
}
function required_plugins_notice() {
$this->missing_plugins = array_diff($this->required_plugins, $this->dependant_plugins);
$num_missing = count($this->missing_plugins);
echo '<div class="notice notice-error">
<p><strong>There ' . ($num_missing > 1 ? 'are ' : 'is ') . $num_missing . ' missing plugin'.($num_missing > 1 && 's').' for the currently active theme!</strong><br />
This could seriously affect the front-end of the website and should be fixed urgently!</p>
<ul>';
foreach($this->missing_plugins as $missing):
echo '<li><a href="/wp-admin/plugin-install.php?tab=plugin-information&plugin='.substr($missing, 0, strpos($missing, '/')).'&TB_iframe=true&width=772&height=886" class="thickbox open-plugin-details-modal">' . $missing . '</a></li>';
endforeach;
echo '</ul>
</div>';
}
}
add_action('admin_init',function(){
new checkRequiredPlugin;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment