1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
defined( 'WPINC' ) || die; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Checks for minimum system requirments on plugin activation |
7
|
|
|
* @version 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
class GL_Plugin_Check |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
const MIN_PHP_VERSION = '5.6.0'; |
12
|
|
|
const MIN_WORDPRESS_VERSION = '4.7.0'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected static $file; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var static |
21
|
|
|
*/ |
22
|
|
|
protected static $instance; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
protected static $versions; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public static function isValid( array $args = array() ) |
33
|
|
|
{ |
34
|
|
|
$versions = static::normalize( $args ); |
35
|
|
|
return static::isPhpValid( $versions->php ) && static::isWpValid( $versions->wordpress ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $version |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public static function isPhpValid( $version = '' ) |
43
|
|
|
{ |
44
|
|
|
$versions = array( 'php' => $version ); |
45
|
|
|
return !version_compare( PHP_VERSION, static::normalize( $versions )->php, '<' ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $version |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public static function isWpValid( $version = '' ) |
53
|
|
|
{ |
54
|
|
|
global $wp_version; |
55
|
|
|
$versions = array( 'wordpress' => $version ); |
56
|
|
|
return !version_compare( $wp_version, static::normalize( $versions )->wordpress, '<' ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public static function shouldDeactivate( $file, array $args = [] ) |
63
|
|
|
{ |
64
|
|
|
if( empty( static::$instance )) { |
65
|
|
|
static::$file = realpath( $file ); |
66
|
|
|
static::$instance = new static; |
67
|
|
|
static::$versions = static::normalize( $args ); |
68
|
|
|
} |
69
|
|
|
if( !static::isValid() ) { |
70
|
|
|
add_action( 'activated_plugin', array( static::$instance, 'deactivate' )); |
71
|
|
|
add_action( 'admin_notices', array( static::$instance, 'deactivate' )); |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function deactivate( $plugin ) |
81
|
|
|
{ |
82
|
|
|
if( static::isValid() )return; |
83
|
|
|
$pluginSlug = plugin_basename( static::$file ); |
84
|
|
|
if( $plugin == $pluginSlug ) { |
85
|
|
|
$this->redirect(); //exit |
86
|
|
|
} |
87
|
|
|
$pluginData = get_file_data( static::$file, array( 'name' => 'Plugin Name' ), 'plugin' ); |
88
|
|
|
deactivate_plugins( $pluginSlug ); |
89
|
|
|
$this->printNotice( $pluginData['name'] ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return object |
94
|
|
|
*/ |
95
|
|
|
protected static function normalize( array $args = [] ) |
96
|
|
|
{ |
97
|
|
|
return (object) wp_parse_args( $args, array( |
98
|
|
|
'php' => static::MIN_PHP_VERSION, |
99
|
|
|
'wordpress' => static::MIN_WORDPRESS_VERSION, |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
protected function redirect() |
107
|
|
|
{ |
108
|
|
|
wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s', |
109
|
|
|
filter_input( INPUT_GET, 'plugin_status' ), |
110
|
|
|
filter_input( INPUT_GET, 'paged' ), |
111
|
|
|
filter_input( INPUT_GET, 's' ) |
112
|
|
|
))); |
113
|
|
|
exit; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $pluginName |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function printNotice( $pluginName ) |
121
|
|
|
{ |
122
|
|
|
$noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
123
|
|
|
$messages = array( |
124
|
|
|
__( 'The %s plugin was deactivated.', 'logger' ), |
125
|
|
|
__( 'Sorry, this plugin requires %s or greater in order to work properly.', 'logger' ), |
126
|
|
|
__( 'Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'logger' ), |
127
|
|
|
__( 'PHP version', 'logger' ), |
128
|
|
|
__( 'WordPress version', 'logger' ), |
129
|
|
|
__( 'Update WordPress', 'logger' ), |
130
|
|
|
); |
131
|
|
|
if( !static::isPhpValid() ) { |
132
|
|
|
printf( $noticeTemplate, |
133
|
|
|
sprintf( $messages[0], $pluginName ), |
134
|
|
|
sprintf( $messages[1], $messages[3].' '.(static::$versions)->php ), |
135
|
|
|
sprintf( $messages[2], PHP_VERSION ) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
else if( !static::isWpValid() ) { |
139
|
|
|
printf( $noticeTemplate, |
140
|
|
|
sprintf( $messages[0], $pluginName ), |
141
|
|
|
sprintf( $messages[1], $messages[4].' '.(static::$versions)->wordpress ), |
142
|
|
|
sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] ) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.