Completed
Push — update/react-createclass ( df2c20...b27c69 )
by
unknown
08:42
created

Jetpack_Modules_Overrides::clear_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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