-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wp-php-version-check.php
198 lines (184 loc) · 4.71 KB
/
class-wp-php-version-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
196
197
198
<?php
/**
* WordPress and PHP Version checking.
*
* @package Wp_Php_Version_Check
* @version 1.0.0
* @author Tim Elsass <[email protected]>
*/
if ( ! class_exists( 'Wp_Php_Version_Check' ) ) {
/**
* Wp_Php_Version_Check Class.
*
* This class is used to determine if a supported PHP version and
* WP version are in use before initializing plugin code.
*/
class Wp_Php_Version_Check {
/**
* @var string $plugin Main plugin file.
*
* @access private
*/
private static $plugin;
/**
* @var string $php_version Minimum PHP version required.
*
* @access private
*/
private static $php_version;
/**
* @var string $wp_version Minimum WordPress version required.
*
* @access private
*/
private static $wp_version;
/**
* Initializes the version checking process.
*
* @since 1.0.0
*
* @access public
*
* @param string $plugin Root plugin file.
* @param string $php_version Minimum PHP version required.
* @param string $wp_version Minimum WordPress version required.
* @param callable $callback Callback method to call if version check passes.
*
* @return null
*/
public static function init( $plugin, $wp_version, $php_version, $callback = null ) {
self::$plugin = $plugin;
self::$php_version = $php_version;
self::$wp_version = $wp_version;
if ( self::is_bad() ) {
// Display warnings for WP-CLI.
if ( defined( 'WP_CLI' ) ) {
WP_CLI::warning( self::get_message() );
self::deactivate();
} else {
// Shows the version error notice in the dashboard.
add_action( 'admin_notices', array( __CLASS__, 'admin_notice' ) );
// Ensures plugin deactivation happens after the notice is displayed.
add_action( 'admin_init', array( __CLASS__, 'deactivate' ) );
}
} else {
// Checks for cb and args passed.
if ( is_callable( $callback ) ) {
$arguments = array();
$args = func_num_args();
for ( $i = 4; $i < $args; $i++ ) {
$arg = func_get_arg($i);
$arguments[] = $arg;
}
// Call the callback with any args required.
call_user_func_array( $callback, $arguments );
} else {
// Add success hook to init for plugins to initialize on.
add_action( 'init', array( __CLASS__, 'success_hook' ) );
}
}
}
/**
* Adds dynamic action for plugin name:init.
*
* This is where plugins can hook to run their initialization code
* once passing the PHP and WordPress version checks.
*
* @since 1.0.0
*
* @access public
*
* @return null
*/
public static function success_hook() {
$file = explode( '/', self::$plugin );
$name = sanitize_key( $file[0] );
do_action( "{$name}:init" );
}
/**
* Responsible for the admin notice display.
*
* @since 1.0.0
*
* @access public
*
* @return null
*/
public static function admin_notice() {
printf( '<div class="error"><p>%s</p></div>', self::get_message() );
// Disables the activate message.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
/**
* Deactivate plugin.
*
* @since 1.0.0
*
* @access public
*
* @return null
*/
public static function deactivate() {
error_log( self::get_message() );
deactivate_plugins( self::$plugin );
}
/**
* Check that the PHP version and WordPress versions are passing minimum
* requirements.
*
* @since 1.0.0
*
* @access public
*
* @return bool Is this not meeting version requirements?
*/
public static function is_bad() {
return self::is_bad_php() || self::is_bad_wp();
}
/**
* Checks whether the current PHP version is insufficient.
*
* @since 1.0.0
*
* @access public
*
* @return bool Is this not meeting PHP version requirements?
*/
public static function is_bad_php() {
return version_compare( PHP_VERSION, self::$php_version, '<' );
}
/**
* Checks if the current WordPress version meets the minimum requirements.
*
* @since 1.0.0
*
* @access public
*
* @return bool Is this not meeting WordPress version requirements?
*/
public static function is_bad_wp() {
return version_compare( get_bloginfo( 'version' ), self::$wp_version, '<' );
}
/**
* Responsible for generating the error message to display.
*
* @since 1.0.0
*
* @access public
*
* @return string The error message.
*/
public static function get_message() {
return sprintf(
'The plugin <code>%1$s</code> requires at least WordPress %2$s and PHP %3$s. You are currently running WordPress %4$s and PHP %5$s.',
dirname( plugin_basename( self::$plugin ) ),
self::$wp_version,
self::$php_version,
get_bloginfo( 'version' ),
PHP_VERSION
);
}
}
}