-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-plugin-check.php
195 lines (176 loc) · 4.7 KB
/
wp-plugin-check.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
<?php
/**
* Class that runs checks before loading possibly incompatible code.
*
* @package WP_Forge_Plugin_Check
*/
/**
* Class WP_Forge_Plugin_Check
*
* This class is responsible for performing basic checks to see if the minimum requirements for the plugin have been met.
*/
class WP_Forge_Plugin_Check {
/**
* Callbacks for additional checks
*
* @var array
*/
public $callbacks = array();
/**
* Collection of errors
*
* @var \WP_Error
*/
public $errors;
/**
* A reference to the main plugin file
*
* @var string
*/
public $file;
/**
* Minimum PHP version required for this plugin
*
* @var string
*/
public $min_php_version;
/**
* Minimum WordPress version required for this plugin
*
* @var string
*/
public $min_wp_version;
/**
* Plugin name
*
* @var string
*/
public $name = '';
/**
* Required PHP extensions
*
* @var array
*/
public $req_php_extensions = array();
/**
* Setup our class properties
*
* @param string $file Plugin file
*/
public function __construct( $file ) {
$this->errors = new \WP_Error();
$this->file = $file;
$this->name = $this->get_plugin_name();
}
/**
* Get the plugin name from the plugin file headers
*
* @return string
*/
public function get_plugin_name() {
$plugin = get_file_data( $this->file, array( 'name' => 'Plugin Name' ) );
return isset( $plugin['name'] ) ? $plugin['name'] : '';
}
/**
* Check all our plugin requirements.
* Displays an admin notice and deactivates the plugin if all requirements are not met.
*/
public function check_plugin_requirements() {
if ( isset( $this->min_php_version ) ) {
$this->check_has_min_php_version();
}
if ( ! empty( $this->req_php_extensions ) ) {
$this->check_has_required_php_extensions();
}
if ( isset( $this->min_wp_version ) ) {
$this->check_has_min_wp_version();
}
if ( ! empty( $this->callbacks ) ) {
foreach ( $this->callbacks as $callback ) {
if ( is_callable( $callback ) ) {
call_user_func_array( $callback, array( $this ) );
}
}
}
if ( $this->has_errors() ) {
// Suppress 'Plugin Activated' notice
unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->deactivate();
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
}
/**
* Check if the minimum required PHP version is available
*/
public function check_has_min_php_version() {
if ( version_compare( PHP_VERSION, $this->min_php_version, '<' ) ) {
/* translators: 1: plugin name 2: minimum required PHP version, current PHP version */
$error_msg = sprintf( __( '%1$s requires PHP version %2$s or later. You are currently running version %3$s.' ), $this->name, $this->min_php_version, PHP_VERSION );
$error_msg .= esc_html__( ' Please contact your web host about upgrading PHP.' );
$this->errors->add( 'php_version', $error_msg );
}
}
/**
* Check if a required PHP extension is available.
* See http://www.php.net/manual/en/extensions.alphabetical.php for a full list.
*/
public function check_has_required_php_extensions() {
foreach ( $this->req_php_extensions as $extension ) {
if ( ! extension_loaded( $extension ) ) {
$this->errors->add(
'php_extension',
/* translators: 1: plugin name 2: required PHP extension */
sprintf( __( '%1$s requires the %2$s PHP extension.' ), $this->name, $extension )
);
}
}
}
/**
* Check if the minimum required WordPress version is available
*/
public function check_has_min_wp_version() {
global $wp_version;
if ( version_compare( $wp_version, $this->min_wp_version, '<' ) ) {
$this->errors->add(
'wp_version',
sprintf(
/* translators: 1: plugin name 2: minimum required WordPress version 3: current WordPress version */
__( '%1$s requires WordPress version %2$s or later. You are currently running version %3$s.' ),
$this->name,
$this->min_wp_version,
$wp_version
)
);
}
}
/**
* Check if any errors were encountered during our plugin checks
*
* @return bool
*/
public function has_errors() {
return (bool) count( $this->errors->errors );
}
/**
* Deactivate the plugin
*/
public function deactivate() {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
if ( function_exists( 'deactivate_plugins' ) ) {
deactivate_plugins( $this->file );
}
}
/**
* Display error messages in the admin
*/
public function admin_notices() {
echo '<div class="error">';
foreach ( $this->errors->get_error_messages() as $msg ) {
echo '<p>' . esc_html( $msg ) . '</p>';
}
echo '<p>';
/* translators: plugin name */
printf( esc_html__( 'The "%s" plugin has been deactivated.' ), esc_html( $this->name ) );
echo '</p></div>';
}
}