Skip to content

Instantly share code, notes, and snippets.

@rsoury
Created November 12, 2020 13:43
Show Gist options
  • Save rsoury/b292473d4e904e3f4b71655a65cb17d3 to your computer and use it in GitHub Desktop.
Save rsoury/b292473d4e904e3f4b71655a65cb17d3 to your computer and use it in GitHub Desktop.
Environment based Plugin Activation for Wordpress Bedrock. Use as a mu-plugin.
<?php
/**
* @package env-plugin-manager
* @version 1.0
*
* Plugin Name: Environment Plugin Manager
* Description: This plugin manages the activation of other plugins based on the website's environment
* Version: 1.0
* Author: Ryan Soury | Web Doodle
* Author URI: https://www.webdoodle.com.au
*/
class EnvPluginManager
{
private static $production_plugins = [
'stream/stream.php',
'wp-optimize/wp-optimize.php',
'all-in-one-wp-migration-s3-extension/all-in-one-wp-migration-s3-extension.php',
'all-in-one-wp-migration-unlimited-extension/all-in-one-wp-migration-unlimited-extension.php',
'all-in-one-wp-migration/all-in-one-wp-migration.php',
'ithemes-security-pro/ithemes-security-pro.php',
'wp-rocket/wp-rocket.php',
'seo-by-rank-math/rank-math.php',
'woocommerce-google-analytics-pro/woocommerce-google-analytics-pro.php'
];
// Oh look, a singleton
private static $__instance = null;
private function __construct()
{
}
private function setup()
{
add_filter('option_active_plugins', [__CLASS__, 'filter_plugins_for_env']);
add_filter('plugin_row_meta', [__CLASS__, 'add_env_notice'], 999, 2);
}
public static function instance()
{
if (!is_a(self::$__instance, __CLASS__)) {
$class = get_called_class();
self::$__instance = new $class;
self::$__instance->setup();
}
return self::$__instance;
}
public static function dev()
{
if (defined('WP_ENV')) {
if (WP_ENV == 'development') {
return true;
}
}
return false;
}
public static function filter_plugins_for_env($plugins)
{
if (self::dev()) {
$filtered_plugins = [];
foreach ($plugins as $plugin) {
if (!in_array($plugin, self::$production_plugins)) {
$filtered_plugins[] = $plugin;
}
}
return $filtered_plugins;
}
return $plugins;
}
public static function add_env_notice($links, $file)
{
if (self::dev()) {
if (in_array($file, self::$production_plugins)) {
$links = array_merge($links, [
'production_notice' => '<strong>Production Only</strong>'
]);
}
}
return (array) $links;
}
}
EnvPluginManager::instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment