-
Notifications
You must be signed in to change notification settings - Fork 9
/
wp-requirements.php
271 lines (228 loc) · 5.63 KB
/
wp-requirements.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* WP Requirements
*
* Utility to check current PHP version, WordPress version and PHP extensions.
*
* @package WP_Requirements
* @version 1.4.0
* @author Fulvio Notarstefano <[email protected]>
* @link https://github.com/nekojira/wp-requirements
* @license GPL2+
*/
if ( ! class_exists( 'WP_Requirements' ) ) {
class WP_Requirements {
/**
* Plugin name.
*
* @access private
* @var string
*/
private $name = '';
/**
* Plugin main file.
*
* plugin_basename( __FILE__ )
*
* @access private
* @var string
*/
private $plugin = '';
/**
* WordPress.
*
* @access private
* @var bool
*/
private $wp = true;
/**
* PHP.
*
* @access private
* @var bool
*/
private $php = true;
/**
* PHP Extensions.
*
* @access private
* @var bool
*/
private $extensions = true;
/**
* Requirements to check.
*
* @access private
* @var array
*/
private $requirements = array();
/**
* Results failures.
*
* Associative array with requirements results.
*
* @access private
* @var array
*/
private $failures = array();
/**
* Admin notice.
*
* @access private
* @var string
*/
private $notice = '';
/**
* Run checks.
*
* @param string $name The plugin name.
* @param string $plugin Output of `plugin_basename( __FILE__ )`.
* @param array $requirements Associative array with requirements.
*/
public function __construct( $name, $plugin, $requirements ) {
$this->name = htmlspecialchars( strip_tags( $name ) );
$this->plugin = $plugin;
$this->requirements = $requirements;
if ( ! empty( $requirements ) && is_array( $requirements ) ) {
$failures = $extensions = array();
$requirements = array_merge(
array(
'WordPress' => '',
'PHP' => '',
'Extensions' => '',
), $requirements
);
// Check for WordPress version.
if ( $requirements['WordPress'] && is_string( $requirements['WordPress'] ) ) {
if ( function_exists( 'get_bloginfo' ) ) {
$wp_version = get_bloginfo( 'version' );
if ( version_compare( $wp_version, $requirements['WordPress'] ) === - 1 ) {
$failures['WordPress'] = $wp_version;
$this->wp = false;
}
}
}
// Check fo PHP version.
if ( $requirements['PHP'] && is_string( $requirements['PHP'] ) ) {
if ( version_compare( PHP_VERSION, $requirements['PHP'] ) === -1 ) {
$failures['PHP'] = PHP_VERSION;
$this->php = false;
}
}
// Check fo PHP Extensions.
if ( $requirements['Extensions'] && is_array( $requirements['Extensions'] ) ) {
foreach ( $requirements['Extensions'] as $extension ) {
if ( $extension && is_string( $extension ) ) {
$extensions[ $extension ] = extension_loaded( $extension );
}
}
if ( in_array( false, $extensions ) ) {
foreach ( $extensions as $extension_name => $found ) {
if ( $found === false ) {
$failures['Extensions'][ $extension_name ] = $extension_name;
}
}
$this->extensions = false;
}
}
$this->failures = $failures;
} else {
trigger_error( 'WP Requirements: the requirements are invalid.', E_USER_ERROR );
}
}
/**
* Get requirements results.
*
* @return array
*/
public function failures() {
return $this->failures;
}
/**
* Check if versions check pass.
*
* @return bool
*/
public function pass() {
if ( in_array( false, array(
$this->wp,
$this->php,
$this->extensions,
) ) ) {
return false;
}
return true;
}
/**
* Notice message.
*
* @param string $message An additional message.
*
* @return string
*/
public function get_notice( $message = '' ) {
$notice = '';
$name = $this->name;
$failures = $this->failures;
if ( ! empty( $failures ) && is_array( $failures ) ) {
$notice = '<div class="error">' . "\n";
$notice .= "\t" . '<p>' . "\n";
$notice .= '<strong>' . sprintf( '%s could not be activated.', $name ) . '</strong><br>';
foreach ( $failures as $requirement => $found ) {
$required = $this->requirements[ $requirement ];
if ( 'Extensions' == $requirement ) {
if ( is_array( $found ) ) {
$notice .= sprintf(
'Required PHP Extension(s) not found: %s.',
join( ', ', $found )
) . '<br>';
}
} else {
$notice .= sprintf(
'Required %1$s version: %2$s - Version found: %3$s',
$requirement,
$required,
$found
) . '<br>';
}
}
$notice .= '<em>' . sprintf( 'Please update to meet %s requirements.', $name ) . '</em>' . "\n";
$notice .= "\t" . '</p>' . "\n";
if ( $message ) {
$notice .= $message;
}
$notice .= '</div>';
}
return $notice;
}
/**
* Print notice.
*/
public function print_notice() {
echo $this->notice;
}
/**
* Deactivate plugin.
*/
public function deactivate_plugin() {
if ( function_exists( 'deactivate_plugins' ) && function_exists( 'plugin_basename' ) ) {
deactivate_plugins( $this->plugin );
}
}
/**
* Deactivate plugin and display admin notice.
*
* @param string $message An additional message in notice.
*/
public function halt( $message = '' ) {
$this->notice = $this->get_notice( $message );
if ( $this->notice && function_exists( 'add_action' ) ) {
add_action( 'admin_notices', array( $this, 'print_notice' ) );
add_action( 'admin_init', array( $this, 'deactivate_plugin' ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
}