Last active
October 5, 2022 08:05
-
-
Save mklasen/933037c902ce8b979682c796a02b597b to your computer and use it in GitHub Desktop.
Activate and deactivate plugins in WordPress - use as must-use plugin
This file contains hidden or 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 | |
/** | |
* MK Manage Plugins | |
* Activates and deactivates plugins for a site or network-wide. | |
* - Make sure to define a WORDPRESS_ENV variable in your wp-config.php | |
* Usage Example: | |
* -- | |
* new MK_Manage_Plugins( | |
* array( | |
* 'post-smtp' => array( | |
* 'enable_on' => 'production', | |
* 'disable_on' => 'development', | |
* 'network' => true, | |
* ) | |
* ) | |
* ); | |
* -- | |
* | |
* Notes: | |
* - To prevent conflicts with other classes, it is recommended to add a namespace to this file. | |
*/ | |
class MK_Manage_Plugins { | |
/** | |
* A summary of plugins to be (network-)(de-)activated. | |
* | |
* @var array An array of plugins | |
*/ | |
private $plugins = array( | |
'enabled' => array(), | |
'disabled' => array(), | |
'network_enabled' => array(), | |
'network_disabled' => array(), | |
); | |
/** | |
* Handle the newly created class and it's arguments. | |
* | |
* @param array $plugins An array of plugins directly taken from class initiaton. | |
* @return void | |
*/ | |
public function __construct( array $plugins = array() ) { | |
$this->handle_input( $plugins ); | |
$this->set_plugins(); | |
} | |
/** | |
* Defines the filters that will be used to manage the plugins. | |
* | |
* @return void | |
*/ | |
public function set_plugins() { | |
add_filter( 'option_active_plugins', array( $this, 'manage_plugins' ) ); | |
add_filter( 'site_option_active_sitewide_plugins', array( $this, 'manage_network_plugins' ) ); | |
} | |
/** | |
* Handles the arguments set when the class is intiated. | |
* | |
* @param array $plugins An array of plugins and settings. | |
* @return void | |
*/ | |
public function handle_input( $plugins ) { | |
foreach ( $plugins as $plugin_name => $args ) { | |
if ( isset( $args['enable_on'] ) ) { | |
if ( defined( 'WORDPRESS_ENV' ) && WORDPRESS_ENV === $args['enable_on'] ) { | |
if ( isset( $args['network'] ) && $args['network'] === true ) { | |
$this->manage_plugin( $plugin_name, 'network_enabled' ); | |
} else { | |
$this->manage_plugin( $plugin_name, 'enabled' ); | |
} | |
} | |
} | |
if ( isset( $args['disable_on'] ) ) { | |
if ( defined( 'WORDPRESS_ENV' ) && WORDPRESS_ENV === $args['disable_on'] ) { | |
if ( isset( $args['network'] ) && $args['network'] === true ) { | |
$this->manage_plugin( $plugin_name, 'network_disabled' ); | |
} else { | |
$this->manage_plugin( $plugin_name, 'disabled' ); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Build the array of plugins that will be (network-)(de-)activated. | |
* | |
* @param string $plugin_name The name and file of the plugin. | |
* @param string $type of action: enabled, disabled, network_enabled, network_disabled. | |
* @return void | |
*/ | |
public function manage_plugin( $plugin_name, $type ) { | |
$this->plugins[ $type ][] = $plugin_name; | |
} | |
/** | |
* Activate or deactivate non-network plugins by hooking into the option_active_plugins filter. | |
* | |
* @param array $plugins Array of active plugins. | |
* @return array $plugins Customized array of activate plugins. | |
*/ | |
public function manage_plugins( $plugins ) { | |
$plugins = array_merge( $plugins, $this->plugins['enabled'] ); | |
if ( count( $this->plugins['disabled'] ) ) { | |
foreach ( (array) $this->plugins['disabled'] as $plugin ) { | |
$key = array_search( $plugin, $plugins ); | |
if ( false !== $key ) { | |
unset( $plugins[ $key ] ); | |
} | |
} | |
} | |
return $plugins; | |
} | |
/** | |
* Activate or deactivate network plugins by hooking into the site_option_active_sitewide_plugins filter. | |
* | |
* @param array $plugins Array of active plugins. | |
* @return array $plugins Customized array of active plugins. | |
*/ | |
public function manage_network_plugins( $plugins ) { | |
foreach ( $this->plugins['network_enabled'] as $file ) { | |
$plugins[ $file ] = time(); | |
} | |
if ( count( $this->plugins['network_disabled'] ) ) { | |
foreach ( (array) $this->plugins['network_disabled'] as $plugin ) { | |
if ( isset( $plugins[ $plugin ] ) ) { | |
unset( $plugins[ $plugin ] ); | |
} | |
} | |
} | |
return $plugins; | |
} | |
} | |
new MK_Manage_Plugins( | |
array( | |
'advanced-custom-fields-pro/acf.php' => array( | |
'enable_on' => 'development', | |
'network' => true, | |
), | |
'query-monitor/query-monitor.php' => array( | |
'enable_on' => 'development', | |
'network' => true, | |
), | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment