1 | <?php |
||
2 | |||
3 | defined( 'WPINC' ) || die; |
||
4 | |||
5 | /** |
||
6 | * Checks for minimum system requirments on plugin activation |
||
7 | * @version 1.0.1 |
||
8 | */ |
||
9 | class GL_Plugin_Check_v1 |
||
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 | * @param string $version |
||
31 | * @return bool |
||
32 | */ |
||
33 | public static function isPhpValid( $version = '' ) |
||
34 | { |
||
35 | if( !empty( $version )) { |
||
36 | static::normalize( array( 'php' => $version )); |
||
37 | } |
||
38 | return !version_compare( PHP_VERSION, static::$versions->php, '<' ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | public static function isValid( array $args = array() ) |
||
45 | { |
||
46 | if( !empty( $args )) { |
||
47 | static::normalize( $args ); |
||
48 | } |
||
49 | return static::isPhpValid() && static::isWpValid(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $version |
||
54 | * @return bool |
||
55 | */ |
||
56 | public static function isWpValid( $version = '' ) |
||
57 | { |
||
58 | global $wp_version; |
||
59 | if( !empty( $version )) { |
||
60 | static::normalize( array( 'wordpress' => $version )); |
||
61 | } |
||
62 | return !version_compare( $wp_version, static::$versions->wordpress, '<' ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $file |
||
67 | * @return bool |
||
68 | */ |
||
69 | public static function shouldDeactivate( $file, array $args = array() ) |
||
70 | { |
||
71 | if( empty( static::$instance )) { |
||
72 | static::$file = realpath( $file ); |
||
73 | static::$instance = new static; |
||
74 | static::$versions = static::normalize( $args ); |
||
75 | } |
||
76 | if( !static::isValid() ) { |
||
77 | add_action( 'activated_plugin', array( static::$instance, 'deactivate' )); |
||
78 | add_action( 'admin_notices', array( static::$instance, 'deactivate' )); |
||
79 | return true; |
||
80 | } |
||
81 | return false; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $plugin |
||
86 | * @return void |
||
87 | */ |
||
88 | public function deactivate( $plugin ) |
||
89 | { |
||
90 | if( static::isValid() )return; |
||
91 | $pluginSlug = plugin_basename( static::$file ); |
||
92 | if( $plugin == $pluginSlug ) { |
||
93 | $this->redirect(); //exit |
||
94 | } |
||
95 | $pluginData = get_file_data( static::$file, array( 'name' => 'Plugin Name' ), 'plugin' ); |
||
96 | deactivate_plugins( $pluginSlug ); |
||
97 | $this->printNotice( $pluginData['name'] ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return object |
||
102 | */ |
||
103 | protected static function normalize( array $args = array() ) |
||
104 | { |
||
105 | return (object)wp_parse_args( $args, array( |
||
106 | 'php' => static::MIN_PHP_VERSION, |
||
107 | 'wordpress' => static::MIN_WORDPRESS_VERSION, |
||
108 | )); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function redirect() |
||
115 | { |
||
116 | wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s', |
||
117 | filter_input( INPUT_GET, 'plugin_status' ), |
||
118 | filter_input( INPUT_GET, 'paged' ), |
||
119 | filter_input( INPUT_GET, 's' ) |
||
120 | ))); |
||
121 | exit; |
||
1 ignored issue
–
show
|
|||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $pluginName |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function printNotice( $pluginName ) |
||
129 | { |
||
130 | $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
||
131 | $messages = array( |
||
132 | __( 'The %s plugin was deactivated.', 'site-reviews' ), |
||
133 | __( 'Sorry, this plugin requires %s or greater in order to work properly.', 'site-reviews' ), |
||
134 | __( '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.', 'site-reviews' ), |
||
135 | __( 'PHP version', 'site-reviews' ), |
||
136 | __( 'WordPress version', 'site-reviews' ), |
||
137 | __( 'Update WordPress', 'site-reviews' ), |
||
138 | ); |
||
139 | if( !static::isPhpValid() ) { |
||
140 | printf( $noticeTemplate, |
||
141 | sprintf( $messages[0], $pluginName ), |
||
142 | sprintf( $messages[1], $messages[3].' '.static::$versions->php ), |
||
143 | sprintf( $messages[2], PHP_VERSION ) |
||
144 | ); |
||
145 | } |
||
146 | else if( !static::isWpValid() ) { |
||
147 | printf( $noticeTemplate, |
||
148 | sprintf( $messages[0], $pluginName ), |
||
149 | sprintf( $messages[1], $messages[4].' '.static::$versions->wordpress ), |
||
150 | sprintf( '<a href="%s">%s</a>', admin_url( 'update-core.php' ), $messages[5] ) |
||
151 | ); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.