1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugin connection management class. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Plugin connection management class. |
12
|
|
|
* The class represents a single plugin that uses Jetpack connection. |
13
|
|
|
* Its functionality has been pretty simplistic so far: add to the storage (`Plugin_Storage`), remove it from there, |
14
|
|
|
* and determine whether it's the last active connection. As the component grows, there'll be more functionality added. |
15
|
|
|
*/ |
16
|
|
|
class Plugin { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* List of the keys allowed as arguments |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $arguments_whitelist = array( |
24
|
|
|
'url_info', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Plugin slug. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $slug; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize the plugin manager. |
36
|
|
|
* |
37
|
|
|
* @param string $slug Plugin slug. |
38
|
|
|
*/ |
39
|
|
|
public function __construct( $slug ) { |
40
|
|
|
$this->slug = $slug; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add the plugin connection info into Jetpack. |
45
|
|
|
* |
46
|
|
|
* @param string $name Plugin name, required. |
47
|
|
|
* @param array $args Plugin arguments, optional. |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
* @see $this->arguments_whitelist |
51
|
|
|
*/ |
52
|
|
|
public function add( $name, array $args = array() ) { |
53
|
|
|
$args = compact( 'name' ) + array_intersect_key( $args, array_flip( $this->arguments_whitelist ) ); |
54
|
|
|
|
55
|
|
|
Plugin_Storage::upsert( $this->slug, $args ); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove the plugin connection info from Jetpack. |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function remove() { |
66
|
|
|
Plugin_Storage::delete( $this->slug ); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Determine if this plugin connection is the only one active at the moment, if any. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function is_only() { |
77
|
|
|
$plugins = Plugin_Storage::get_all(); |
78
|
|
|
|
79
|
|
|
return ! $plugins || ( array_key_exists( $this->slug, $plugins ) && 1 === count( $plugins ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|