1 | <?php |
||
1 ignored issue
–
show
|
|||
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 |
||
1 ignored issue
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries. ![]() |
|||
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; |
||
1 ignored issue
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
![]() |
|||
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 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.