Created
August 16, 2022 15:20
-
-
Save broskees/05b82ca70ab7ded6edebd305b7ca7a92 to your computer and use it in GitHub Desktop.
plugin disabler per environment WordPress (slightly modified)
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 | |
/* | |
Plugin Name: Simple Environment plugin disabler | |
Description: Disables plugins based on environment settings. | |
Original Author: Kamil Grzegorczyk | |
Related blog post: https://roots.io/guides/how-to-disable-plugins-on-certain-environments/ | |
Version: 0.1 | |
*/ | |
class DisablePlugins | |
{ | |
public static $instance; | |
private $disabled = []; | |
/** | |
* Sets up the options filter, and optionally handles an array of plugins to disable | |
* @param array $disables Optional array of plugin filenames to disable | |
*/ | |
public function __construct(array $disables = null) | |
{ | |
/** | |
* Handle what was passed in | |
*/ | |
if (is_array($disables)) { | |
foreach ($disables as $disable) { | |
$this->disable($disable); | |
} | |
} | |
/** | |
* Add the filters | |
*/ | |
add_filter('option_active_plugins', [$this, 'do_disabling']); | |
add_filter('site_option_active_sitewide_plugins', [$this, 'do_network_disabling']); | |
/** | |
* Allow other plugins to access this instance | |
*/ | |
self::$instance = $this; | |
} | |
/** | |
* Adds a filename to the list of plugins to disable | |
*/ | |
public function disable($file) | |
{ | |
$this->disabled[] = $file; | |
} | |
/** | |
* Hooks in to the option_active_plugins filter and does the disabling | |
* @param array $plugins WP-provided list of plugin filenames | |
* @return array The filtered array of plugin filenames | |
*/ | |
public function do_disabling($plugins) | |
{ | |
if (count($this->disabled)) { | |
foreach ((array)$this->disabled as $plugin) { | |
$key = array_search($plugin, $plugins); | |
if (false !== $key) { | |
unset($plugins[$key]); | |
} | |
} | |
} | |
return $plugins; | |
} | |
/** | |
* Hooks in to the site_option_active_sitewide_plugins filter and does the disabling | |
* | |
* @param array $plugins | |
* | |
* @return array | |
*/ | |
public function do_network_disabling($plugins) | |
{ | |
if (count($this->disabled)) { | |
foreach ((array)$this->disabled as $plugin) { | |
if (isset($plugins[$plugin])) { | |
unset($plugins[$plugin]); | |
} | |
} | |
} | |
return $plugins; | |
} | |
} | |
if (defined('DEV_DISABLED_PLUGINS')) { | |
$plugins_to_disable = unserialize(DEV_DISABLED_PLUGINS); | |
if (!empty($plugins_to_disable) && is_array($plugins_to_disable)) { | |
$utility = new DisablePlugins($plugins_to_disable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment