-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathServerPlugin.php
More file actions
164 lines (141 loc) · 3.5 KB
/
ServerPlugin.php
File metadata and controls
164 lines (141 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace WP_CLI_Login;
use UnexpectedValueException;
use WP_CLI_Login\WP_CLI_Login_Server;
class ServerPlugin
{
/**
* Plugin file path, relative to plugins directory.
*/
const PLUGIN_FILE = 'wp-cli-login-server/wp-cli-login-server.php';
/**
* Absolute path to primary plugin file.
* @var
*/
private $file;
/**
* ServerPlugin constructor.
*
* @param $file
*/
public function __construct($file)
{
$this->file = $file;
}
/**
* Check if the plugin is currently active.
*
* @return bool
*/
public static function isActive()
{
return class_exists(WP_CLI_Login_Server::class, false);
}
/**
* Get a new instance for the installed plugin.
*
* @return static
*/
public static function installed()
{
$must_use = static::mustUse();
if ($must_use->exists()) {
return $must_use;
}
return new static(WP_PLUGIN_DIR . '/' . static::PLUGIN_FILE);
}
/**
* @return MustUseServerPlugin
*/
public static function mustUse()
{
return new MustUseServerPlugin(WPMU_PLUGIN_DIR . '/' . basename(static::PLUGIN_FILE));
}
/**
* Try to see if the plugin was installed with Composer or not.
*
* Checks if there is a composer.json adjacent to the main plugin file.
* This file will not exist when installed by the command.
*
* @return bool
*/
public function isComposerInstalled()
{
return file_exists(dirname($this->file) . '/composer.json');
}
/**
* Check if the main plugin file exists.
*
* @return bool
*/
public function exists()
{
return file_exists($this->file);
}
/**
* Get the absolute path to the main plugin file.
*
* @return mixed
*/
public function fullPath()
{
return $this->file;
}
/**
* Get the plugin name, as defined in the header.
*
* @return mixed
*/
public function name()
{
return $this->data()['Name'];
}
/**
* Get the plugin version, as defined in the header.
*
* @return mixed
*/
public function version()
{
return $this->data()['Version'];
}
/**
* Check if the plugin's version satisfies the given constraints.
*
* @param string $constraints Composer Semver-style constraints
*
* @return bool
*/
public function versionSatisfies($constraints)
{
$constraints = is_string($constraints) ? trim($constraints) : '';
$version = is_string($this->version()) ? trim($this->version()) : '';
if ($constraints === '' || $version === '') {
// Saved installs prior to 1.5 did not persist a constraint; force reset flow.
return false;
}
try {
$semver = 'Composer\\Semver\\Semver';
if (!class_exists($semver)) {
return false;
}
return call_user_func([$semver, 'satisfies'], $version, $constraints);
} catch (UnexpectedValueException $exception) {
// Bail gracefully if either side is not a valid semver string.
return false;
}
}
/**
* Get the plugin file header data.
*
* @return array
*/
public function data()
{
static $loaded;
if (!$loaded) {
$loaded = get_plugin_data($this->file);
}
return $loaded;
}
}