Completed
Push — update/add_grunion_after_feedb... ( 614f06...9f6c1a )
by
unknown
07:04
created

Plugin_Storage::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
rs 9.8333
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 Automattic\Jetpack\Config;
11
use WP_Error;
12
13
/**
14
 * The class serves a single purpose - to store the data that plugins use the connection, along with some auxiliary information.
15
 * Well, we don't really store all that. The information is provided on runtime,
16
 * so all we need to do is to save the data into the class property and retrieve it from there on demand.
17
 *
18
 * @todo Adapt for multisite installations.
19
 */
20
class Plugin_Storage {
21
22
	/**
23
	 * Connected plugins.
24
	 *
25
	 * @var array
26
	 */
27
	private static $plugins = array();
28
29
	/**
30
	 * Whether the plugins were configured.
31
	 * To make sure we don't call the configuration process again and again.
32
	 *
33
	 * @var bool
34
	 */
35
	private static $plugins_configuration_finished = false;
36
37
	/**
38
	 * Add or update the plugin information in the storage.
39
	 *
40
	 * @param string $slug Plugin slug.
41
	 * @param array  $args Plugin arguments, optional.
42
	 *
43
	 * @return bool
44
	 */
45
	public static function upsert( $slug, array $args = array() ) {
46
		self::$plugins[ $slug ] = $args;
47
48
		return true;
49
	}
50
51
	/**
52
	 * Retrieve the plugin information by slug.
53
	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2).
54
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
55
	 * so please make sure not to run the method too early in the code.
56
	 *
57
	 * @param string $slug The plugin slug.
58
	 *
59
	 * @return array|null|WP_Error
60
	 */
61
	public static function get_one( $slug ) {
62
		$plugins = self::get_all();
63
64
		if ( $plugins instanceof WP_Error ) {
65
			return $plugins;
66
		}
67
68
		return empty( $plugins[ $slug ] ) ? null : $plugins[ $slug ];
69
	}
70
71
	/**
72
	 * Retrieve info for all plugins that use the connection.
73
	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2).
74
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
75
	 * so please make sure not to run the method too early in the code.
76
	 *
77
	 * @return array|WP_Error
78
	 */
79
	public static function get_all() {
80
		$maybe_error = self::ensure_configured();
81
82
		if ( $maybe_error instanceof WP_Error ) {
83
			return $maybe_error;
84
		}
85
86
		return self::$plugins;
87
	}
88
89
	/**
90
	 * Remove the plugin connection info from Jetpack.
91
	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2).
92
	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins,
93
	 * so please make sure not to run the method too early in the code.
94
	 *
95
	 * @param string $slug The plugin slug.
96
	 *
97
	 * @return bool|WP_Error
98
	 */
99
	public static function delete( $slug ) {
100
		$maybe_error = self::ensure_configured();
101
102
		if ( $maybe_error instanceof WP_Error ) {
103
			return $maybe_error;
104
		}
105
106
		if ( array_key_exists( $slug, self::$plugins ) ) {
107
			unset( self::$plugins[ $slug ] );
108
		}
109
110
		return true;
111
	}
112
113
	/**
114
	 * The method makes sure that `Jetpack\Config` has finished, and it's now safe to retrieve the list of plugins.
115
	 *
116
	 * @return bool|WP_Error
117
	 */
118
	private static function ensure_configured() {
119
		if ( class_exists( Config::class ) && method_exists( Config::class, 'is_configured' ) && ! Config::is_configured() ) {
120
			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...
121
		}
122
123
		return true;
124
	}
125
126
}
127