-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDependencyChecker.php
More file actions
135 lines (115 loc) · 3.64 KB
/
DependencyChecker.php
File metadata and controls
135 lines (115 loc) · 3.64 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
<?php
/**
* Checks if dependencies are valid.
*/
namespace OWC\OpenPub\Base\Foundation;
/**
* Checks if dependencies are valid.
*/
class DependencyChecker
{
/**
* Plugins that need to be checked for.
*/
private array $dependencies;
/**
* Build up array of failed plugins, either because
* they have the wrong version or are inactive.
*/
private array $failed = [];
/**
* Determine which plugins need to be present.
*/
public function __construct(array $dependencies)
{
$this->dependencies = $dependencies;
}
/**
* Determines if the dependencies are not met.
*/
public function failed(): bool
{
foreach ($this->dependencies as $dependency) {
switch ($dependency['type']) {
case 'class':
$this->checkClass($dependency);
break;
case 'plugin':
$this->checkPlugin($dependency);
break;
}
}
return 0 < count($this->failed);
}
/**
* Notifies the administrator which plugins need to be enabled,
* or which plugins have the wrong version.
*/
public function notify(): void
{
\add_action('admin_notices', function () {
$list = '<p>' . __(
'The following plugins are required to use the OpenPub:',
'openpub-base'
) . '</p><ol>';
foreach ($this->failed as $dependency) {
$info = isset($dependency['message']) ? ' (' . $dependency['message'] . ')' : '';
$list .= sprintf('<li>%s%s</li>', $dependency['label'], $info);
}
$list .= '</ol>';
printf('<div class="notice notice-error"><p>%s</p></div>', $list);
});
}
/**
* Marks a dependency as failed.
*/
private function markFailed(array $dependency, string $defaultMessage): void
{
$this->failed[] = array_merge([
'message' => $dependency['message'] ?? $defaultMessage,
], $dependency);
}
/**
* Checks if required class exists.
*/
private function checkClass(array $dependency): void
{
if (! class_exists($dependency['name'])) {
$this->markFailed($dependency, __('Class does not exist', 'openpub-base'));
return;
}
}
/**
* Check if a plugin is enabled and has the correct version.
*/
private function checkPlugin(array $dependency): void
{
if (! function_exists('is_plugin_active')) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if (! is_plugin_active($dependency['file'])) {
$this->markFailed($dependency, __('Inactive', 'openpub-base'));
return;
}
// If there is a version lock set on the dependency...
if (isset($dependency['version'])) {
if (! $this->checkVersion($dependency)) {
$this->markFailed($dependency, __('Minimal version:', 'openpub-base') . ' <b>' . $dependency['version'] . '</b>');
}
}
}
/**
* Checks the installed version of the plugin.
*/
private function checkVersion(array $dependency): bool
{
try {
$file = file_get_contents(WP_PLUGIN_DIR . '/' . $dependency['file']);
} catch (\Exception $e) {
return false;
}
preg_match('/^(?: ?\* ?Version: ?)(.*)$/m', $file, $matches);
$version = isset($matches[1]) ? str_replace(' ', '', $matches[1]) : '0.0.0';
return version_compare($version, $dependency['version'], '>=');
}
}