Completed
Push — renovate/testing-library-jest-... ( fe7452...e16919 )
by
unknown
07:28
created

Plugin_Storage::configure()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Storage for plugin connection information.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use WP_Error;
11
12
/**
13
 * The class serves a single purpose - to store the data which plugins use the connection, along with some auxiliary information.
14
 */
15
class Plugin_Storage {
16
17
	const ACTIVE_PLUGINS_OPTION_NAME = 'jetpack_connection_active_plugins';
18
19
	const PLUGINS_DISABLED_OPTION_NAME = 'jetpack_connection_disabled_plugins';
20
21
	/**
22
	 * Whether this class was configured for the first time or not.
23
	 *
24
	 * @var boolean
25
	 */
26
	private static $configured = false;
27
28
	/**
29
	 * Refresh list of connected plugins upon intialization.
30
	 *
31
	 * @var boolean
32
	 */
33
	private static $refresh_connected_plugins = false;
34
35
	/**
36
	 * Connected plugins.
37
	 *
38
	 * @var array
39
	 */
40
	private static $plugins = array();
41
42
	/**
43
	 * Add or update the plugin information in the storage.
44
	 *
45
	 * @param string $slug Plugin slug.
46
	 * @param array  $args Plugin arguments, optional.
47
	 *
48
	 * @return bool
49
	 */
50
	public static function upsert( $slug, array $args = array() ) {
51
		self::$plugins[ $slug ] = $args;
52
53
		// if plugin is not in the list of active plugins, refresh the list.
54
		if ( ! array_key_exists( $slug, get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ) ) {
55
			self::$refresh_connected_plugins = true;
56
		}
57
58
		return true;
59
	}
60
61
	/**
62
	 * Retrieve the plugin information by slug.
63
	 * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded
64
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
65
	 * so please make sure not to run the method too early in the code.
66
	 *
67
	 * @param string $slug The plugin slug.
68
	 *
69
	 * @return array|null|WP_Error
70
	 */
71
	public static function get_one( $slug ) {
72
		$plugins = self::get_all();
73
74
		if ( $plugins instanceof WP_Error ) {
75
			return $plugins;
76
		}
77
78
		return empty( $plugins[ $slug ] ) ? null : $plugins[ $slug ];
79
	}
80
81
	/**
82
	 * Retrieve info for all plugins that use the connection.
83
	 * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded
84
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
85
	 * so please make sure not to run the method too early in the code.
86
	 *
87
	 * @param bool $connected_only Exclude plugins that were explicitly disconnected.
88
	 *
89
	 * @return array|WP_Error
90
	 */
91
	public static function get_all( $connected_only = false ) {
92
		$maybe_error = self::ensure_configured();
93
94
		if ( $maybe_error instanceof WP_Error ) {
95
			return $maybe_error;
96
		}
97
98
		return $connected_only ? array_diff_key( self::$plugins, array_flip( self::get_all_disabled_plugins() ) ) : self::$plugins;
99
	}
100
101
	/**
102
	 * Remove the plugin connection info from Jetpack.
103
	 * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded
104
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
105
	 * so please make sure not to run the method too early in the code.
106
	 *
107
	 * @param string $slug The plugin slug.
108
	 *
109
	 * @return bool|WP_Error
110
	 */
111
	public static function delete( $slug ) {
112
		$maybe_error = self::ensure_configured();
113
114
		if ( $maybe_error instanceof WP_Error ) {
115
			return $maybe_error;
116
		}
117
118
		if ( array_key_exists( $slug, self::$plugins ) ) {
119
			unset( self::$plugins[ $slug ] );
120
		}
121
122
		return true;
123
	}
124
125
	/**
126
	 * The method makes sure that `Jetpack\Config` has finished, and it's now safe to retrieve the list of plugins.
127
	 *
128
	 * @return bool|WP_Error
129
	 */
130
	private static function ensure_configured() {
131
		if ( ! self::$configured ) {
132
			return new WP_Error( 'too_early', __( 'You cannot call this method until Jetpack Config is configured', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'too_early'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
		}
134
135
		return true;
136
	}
137
138
	/**
139
	 * Called once to configure this class after plugins_loaded.
140
	 *
141
	 * @return void
142
	 */
143
	public static function configure() {
144
145
		if ( self::$configured ) {
146
			return;
147
		}
148
149
		// If a plugin was activated or deactivated.
150
		$number_of_plugins_differ = count( self::$plugins ) !== count( get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) );
151
152
		if ( $number_of_plugins_differ || true === self::$refresh_connected_plugins ) {
153
			self::update_active_plugins_option();
154
		}
155
156
		self::$configured = true;
157
158
	}
159
160
	/**
161
	 * Updates the active plugins option with current list of active plugins.
162
	 *
163
	 * @return void
164
	 */
165
	public static function update_active_plugins_option() {
166
		// Note: Since this options is synced to wpcom, if you change its structure, you have to update the sanitizer at wpcom side.
167
		update_option( self::ACTIVE_PLUGINS_OPTION_NAME, self::$plugins );
168
	}
169
170
	/**
171
	 * Add the plugin to the set of disconnected ones.
172
	 *
173
	 * @param string $slug Plugin slug.
174
	 *
175
	 * @return bool
176
	 */
177 View Code Duplication
	public static function disable_plugin( $slug ) {
178
		$disconnects = self::get_all_disabled_plugins();
179
180
		if ( ! in_array( $slug, $disconnects, true ) ) {
181
			$disconnects[] = $slug;
182
			update_option( self::PLUGINS_DISABLED_OPTION_NAME, $disconnects );
183
		}
184
185
		return true;
186
	}
187
188
	/**
189
	 * Remove the plugin from the set of disconnected ones.
190
	 *
191
	 * @param string $slug Plugin slug.
192
	 *
193
	 * @return bool
194
	 */
195 View Code Duplication
	public static function enable_plugin( $slug ) {
196
		$disconnects = self::get_all_disabled_plugins();
197
198
		$slug_index = array_search( $slug, $disconnects, true );
199
		if ( false !== $slug_index ) {
200
			unset( $disconnects[ $slug_index ] );
201
			update_option( self::PLUGINS_DISABLED_OPTION_NAME, $disconnects );
202
		}
203
204
		return true;
205
	}
206
207
	/**
208
	 * Get all plugins that were disconnected by user.
209
	 *
210
	 * @return array
211
	 */
212
	public static function get_all_disabled_plugins() {
213
		return get_option( self::PLUGINS_DISABLED_OPTION_NAME, array() );
214
	}
215
216
}
217