| 1 |
<?php |
| 2 |
/** |
| 3 |
* Special cases for overriding modules. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Provides methods for dealing with module overrides. |
| 10 |
* |
| 11 |
* @since 5.9.0 |
| 12 |
*/ |
| 13 |
class Jetpack_Modules_Overrides { |
| 14 |
/** |
| 15 |
* Used to cache module overrides so that we minimize how many times we apply the |
| 16 |
* option_jetpack_active_modules filter. |
| 17 |
* |
| 18 |
* @var null|array |
| 19 |
*/ |
| 20 |
private $overrides = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Clears the $overrides member used for caching. |
| 24 |
* |
| 25 |
* Since get_overrides() can be passed a falsey value to skip caching, this is probably |
| 26 |
* most useful for clearing cache between tests. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function clear_cache() { |
| 31 |
$this->overrides = null; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns true if there is a filter on the jetpack_active_modules option. |
| 36 |
* |
| 37 |
* @return bool Whether there is a filter on the jetpack_active_modules option. |
| 38 |
*/ |
| 39 |
public function do_overrides_exist() { |
| 40 |
return ( has_filter( 'option_jetpack_active_modules' ) || has_filter( 'jetpack_active_modules' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Gets the override for a given module. |
| 45 |
* |
| 46 |
* @param string $module_slug The module's slug. |
| 47 |
* @param boolean $use_cache Whether or not cached overrides should be used. |
| 48 |
* |
| 49 |
* @return bool|string False if no override for module. 'active' or 'inactive' if there is an override. |
| 50 |
*/ |
| 51 |
public function get_module_override( $module_slug, $use_cache = true ) { |
| 52 |
$overrides = $this->get_overrides( $use_cache ); |
| 53 |
|
| 54 |
if ( ! isset( $overrides[ $module_slug ] ) ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
return $overrides[ $module_slug ]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns an array of module overrides where the key is the module slug and the value |
| 63 |
* is true if the module is forced on and false if the module is forced off. |
| 64 |
* |
| 65 |
* @param bool $use_cache Whether or not cached overrides should be used. |
| 66 |
* |
| 67 |
* @return array The array of module overrides. |
| 68 |
*/ |
| 69 |
public function get_overrides( $use_cache = true ) { |
| 70 |
if ( $use_cache && $this->overrides !== null ) { |
| 71 |
return $this->overrides; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! $this->do_overrides_exist() ) { |
| 75 |
return array(); |
| 76 |
} |
| 77 |
|
| 78 |
$available_modules = Jetpack::get_available_modules(); |
| 79 |
|
| 80 |
/** |
| 81 |
* First, let's get all modules that have been forced on. |
| 82 |
*/ |
| 83 |
|
| 84 |
/** This filter is documented in wp-includes/option.php */ |
| 85 |
$filtered = apply_filters( 'option_jetpack_active_modules', array() ); |
| 86 |
|
| 87 |
/** This filter is documented in class.jetpack.php */ |
| 88 |
$filtered = apply_filters( 'jetpack_active_modules', $filtered ); |
| 89 |
|
| 90 |
$forced_on = array_diff( $filtered, array() ); |
| 91 |
|
| 92 |
/** |
| 93 |
* Second, let's get all modules forced off. |
| 94 |
*/ |
| 95 |
|
| 96 |
/** This filter is documented in wp-includes/option.php */ |
| 97 |
$filtered = apply_filters( 'option_jetpack_active_modules', $available_modules ); |
| 98 |
|
| 99 |
/** This filter is documented in class.jetpack.php */ |
| 100 |
$filtered = apply_filters( 'jetpack_active_modules', $filtered ); |
| 101 |
|
| 102 |
$forced_off = array_diff( $available_modules, $filtered ); |
| 103 |
|
| 104 |
/** |
| 105 |
* Last, build the return value. |
| 106 |
*/ |
| 107 |
$return_value = array(); |
| 108 |
foreach ( $forced_on as $on ) { |
| 109 |
$return_value[ $on ] = 'active'; |
| 110 |
} |
| 111 |
|
| 112 |
foreach ( $forced_off as $off ) { |
| 113 |
$return_value[ $off ] = 'inactive'; |
| 114 |
} |
| 115 |
|
| 116 |
$this->overrides = $return_value; |
| 117 |
|
| 118 |
return $return_value; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* A reference to an instance of this class. |
| 123 |
* |
| 124 |
* @var Jetpack_Modules_Overrides |
| 125 |
*/ |
| 126 |
private static $instance = null; |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the singleton instance of Jetpack_Modules_Overrides |
| 130 |
* |
| 131 |
* @return Jetpack_Modules_Overrides |
| 132 |
*/ |
| 133 |
public static function instance() { |
| 134 |
if ( self::$instance === null ) { |
| 135 |
self::$instance = new Jetpack_Modules_Overrides(); |
| 136 |
} |
| 137 |
|
| 138 |
return self::$instance; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Private construct to enforce singleton. |
| 143 |
*/ |
| 144 |
private function __construct() { |
| 145 |
} |
| 146 |
} |
| 147 |
|