1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Storage for plugin connection information. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The class serves a single purpose - to store the data that plugins use the connection, along with some auxiliary information. |
12
|
|
|
* Well, we don't really store all that. The information is provided on runtime, |
13
|
|
|
* so all we need to do is to save the data into the class property and retrieve it from there on demand. |
14
|
|
|
* |
15
|
|
|
* @todo Adapt for multisite installations. |
16
|
|
|
*/ |
17
|
|
|
class Plugin_Storage { |
18
|
|
|
|
19
|
|
|
const OPTION_KEY = 'connection_plugins'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Connected plugins. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $plugins = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Whether the plugins were configured. |
30
|
|
|
* To make sure we don't call the configuration process again and again. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private static $plugins_configuration_finished = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add or update the plugin information in the storage. |
38
|
|
|
* |
39
|
|
|
* @param string $slug Plugin slug. |
40
|
|
|
* @param array $args Plugin arguments, optional. |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public static function upsert( $slug, array $args = array() ) { |
45
|
|
|
self::$plugins[ $slug ] = $args; |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retrieve the plugin information by slug. |
52
|
|
|
* |
53
|
|
|
* @param string $slug The plugin slug. |
54
|
|
|
* |
55
|
|
|
* @return array|null |
56
|
|
|
*/ |
57
|
|
|
public static function get_one( $slug ) { |
58
|
|
|
return empty( self::$plugins[ $slug ] ) ? null : self::$plugins[ $slug ]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieve info for all plugins that use the connection. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public static function get_all() { |
67
|
|
|
if ( ! self::$plugins_configuration_finished ) { |
68
|
|
|
/** |
69
|
|
|
* Fires upon retrieval of the connected plugins. |
70
|
|
|
* Only fires once, as the data isn't supposed to change after it's been initialized. |
71
|
|
|
* |
72
|
|
|
* @since 8.5.0 |
73
|
|
|
*/ |
74
|
|
|
do_action( 'jetpack_connection_configure_plugin' ); |
75
|
|
|
|
76
|
|
|
self::$plugins_configuration_finished = true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return self::$plugins; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove the plugin connection info from Jetpack. |
84
|
|
|
* |
85
|
|
|
* @param string $slug The plugin slug. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public static function delete( $slug ) { |
90
|
|
|
if ( array_key_exists( $slug, self::$plugins ) ) { |
91
|
|
|
unset( self::$plugins[ $slug ] ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|