Completed
Push — branch-9.1 ( f63e37...603330 )
by Jeremy
11:23
created

Plugin::get_slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	 * Get the plugin slug.
45
	 *
46
	 * @return string
47
	 */
48
	public function get_slug() {
49
		return $this->slug;
50
	}
51
52
	/**
53
	 * Add the plugin connection info into Jetpack.
54
	 *
55
	 * @param string $name Plugin name, required.
56
	 * @param array  $args Plugin arguments, optional.
57
	 *
58
	 * @return $this
59
	 * @see $this->arguments_whitelist
60
	 */
61
	public function add( $name, array $args = array() ) {
62
		$args = compact( 'name' ) + array_intersect_key( $args, array_flip( $this->arguments_whitelist ) );
63
64
		Plugin_Storage::upsert( $this->slug, $args );
65
66
		return $this;
67
	}
68
69
	/**
70
	 * Remove the plugin connection info from Jetpack.
71
	 *
72
	 * @return $this
73
	 */
74
	public function remove() {
75
		Plugin_Storage::delete( $this->slug );
76
77
		return $this;
78
	}
79
80
	/**
81
	 * Determine if this plugin connection is the only one active at the moment, if any.
82
	 *
83
	 * @return bool
84
	 */
85
	public function is_only() {
86
		$plugins = Plugin_Storage::get_all( true );
87
88
		return ! $plugins || ( array_key_exists( $this->slug, $plugins ) && 1 === count( $plugins ) );
89
	}
90
91
	/**
92
	 * Add the plugin to the set of disconnected ones.
93
	 *
94
	 * @return bool
95
	 */
96
	public function disable() {
97
		return Plugin_Storage::disable_plugin( $this->slug );
98
	}
99
100
	/**
101
	 * Remove the plugin from the set of disconnected ones.
102
	 *
103
	 * @return bool
104
	 */
105
	public function enable() {
106
		return Plugin_Storage::enable_plugin( $this->slug );
107
	}
108
109
	/**
110
	 * Whether this plugin is allowed to use the connection.
111
	 *
112
	 * @return bool
113
	 */
114
	public function is_enabled() {
115
		return ! in_array( $this->slug, Plugin_Storage::get_all_disabled_plugins(), true );
116
	}
117
118
}
119