1 | <?php |
||
2 | /** |
||
3 | * This file is part of mundschenk-at/check-wp-requirements. |
||
4 | * |
||
5 | * Copyright 2014-2018 Peter Putzer. |
||
6 | * Copyright 2009-2011 KINGdesk, LLC. |
||
7 | * |
||
8 | * This program is free software; you can redistribute it and/or |
||
9 | * modify it under the terms of the GNU General Public License |
||
10 | * as published by the Free Software Foundation; either version 2 |
||
11 | * of the License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with this program; if not, write to the Free Software |
||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
21 | * |
||
22 | * *** |
||
23 | * |
||
24 | * @package mundschenk-at/check-wp-requirements |
||
25 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
||
26 | */ |
||
27 | |||
28 | /** |
||
29 | * This class checks if the required runtime environment is available. |
||
30 | * |
||
31 | * Included checks: |
||
32 | * - PHP version |
||
33 | * - mb_string extension |
||
34 | * - UTF-8 encoding |
||
35 | * |
||
36 | * Note: All code must be executable on PHP 5.2. |
||
37 | */ |
||
38 | class Mundschenk_WP_Requirements { |
||
39 | |||
40 | /** |
||
41 | * The minimum requirements for running the plugins. Must contain: |
||
42 | * - 'php' |
||
43 | * - 'multibyte' |
||
44 | * - 'utf-8' |
||
45 | * |
||
46 | * @var array A hash containing the version requirements for the plugin. |
||
47 | */ |
||
48 | private $install_requirements; |
||
49 | |||
50 | /** |
||
51 | * The user-visible name of the plugin. |
||
52 | * |
||
53 | * @todo Should the plugin name be translated? |
||
54 | * @var string |
||
55 | */ |
||
56 | private $plugin_name; |
||
57 | |||
58 | /** |
||
59 | * The full path to the main plugin file. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $plugin_file; |
||
64 | |||
65 | /** |
||
66 | * The textdomain used for loading plugin translations. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $textdomain; |
||
71 | |||
72 | /** |
||
73 | * Sets up a new Mundschenk_WP_Requirements object. |
||
74 | * |
||
75 | * @param string $name The plugin name. |
||
76 | * @param string $plugin_path The full path to the main plugin file. |
||
77 | * @param string $textdomain The text domain used for i18n. |
||
78 | * @param array $requirements The requirements to check against. |
||
79 | */ |
||
80 | 1 | public function __construct( $name, $plugin_path, $textdomain, $requirements ) { |
|
81 | 1 | $this->plugin_name = $name; |
|
82 | 1 | $this->plugin_file = $plugin_path; |
|
83 | 1 | $this->textdomain = $textdomain; |
|
84 | |||
85 | 1 | $this->install_requirements = \wp_parse_args( $requirements, array( |
|
86 | 1 | 'php' => '5.2.0', |
|
87 | 'multibyte' => false, |
||
88 | 'utf-8' => false, |
||
89 | ) ); |
||
90 | 1 | } |
|
91 | |||
92 | /** |
||
93 | * Checks if all runtime requirements for the plugin are met. |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | 11 | public function check() { |
|
98 | 11 | $requirements_met = true; |
|
99 | |||
100 | 11 | if ( ! empty( $this->install_requirements['php'] ) && version_compare( PHP_VERSION, $this->install_requirements['php'], '<' ) ) { |
|
101 | 6 | $notice = 'admin_notices_php_version_incompatible'; |
|
102 | 6 | $requirements_met = false; |
|
103 | 5 | } elseif ( ! empty( $this->install_requirements['multibyte'] ) && ! $this->check_multibyte_support() ) { |
|
104 | 2 | $notice = 'admin_notices_mbstring_incompatible'; |
|
105 | 2 | $requirements_met = false; |
|
106 | 3 | } elseif ( ! empty( $this->install_requirements['utf-8'] ) && ! $this->check_utf8_support() ) { |
|
107 | 1 | $notice = 'admin_notices_charset_incompatible'; |
|
108 | 1 | $requirements_met = false; |
|
109 | } |
||
110 | |||
111 | 11 | if ( ! $requirements_met && is_admin() ) { |
|
112 | // Load text domain to ensure translated admin notices. |
||
113 | 5 | load_plugin_textdomain( $this->textdomain ); |
|
114 | |||
115 | // Add admin notice. |
||
116 | 5 | add_action( 'admin_notices', array( $this, $notice ) ); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
117 | } |
||
118 | |||
119 | 11 | return $requirements_met; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Deactivates the plugin. |
||
124 | */ |
||
125 | 1 | public function deactivate_plugin() { |
|
126 | 1 | deactivate_plugins( plugin_basename( $this->plugin_file ) ); |
|
127 | 1 | } |
|
128 | |||
129 | /** |
||
130 | * Checks if multibyte functions are supported. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 1 | protected function check_multibyte_support() { |
|
135 | 1 | return function_exists( 'mb_strlen' ) |
|
136 | 1 | && function_exists( 'mb_strtolower' ) |
|
137 | 1 | && function_exists( 'mb_substr' ) |
|
138 | 1 | && function_exists( 'mb_detect_encoding' ); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Checks if the blog charset is set to UTF-8. |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 4 | protected function check_utf8_support() { |
|
147 | 4 | return 'utf-8' === strtolower( get_bloginfo( 'charset' ) ); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Print 'PHP version incompatible' admin notice |
||
152 | */ |
||
153 | 1 | public function admin_notices_php_version_incompatible() { |
|
154 | 1 | $this->display_error_notice( |
|
155 | /* translators: 1: plugin name 2: target PHP version number 3: actual PHP version number */ |
||
156 | 1 | __( 'The activated plugin %1$s requires PHP %2$s or later. Your server is running PHP %3$s. Please deactivate this plugin, or upgrade your server\'s installation of PHP.', $this->textdomain ), |
|
157 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
158 | 1 | $this->install_requirements['php'], |
|
159 | 1 | phpversion() |
|
160 | ); |
||
161 | 1 | } |
|
162 | |||
163 | /** |
||
164 | * Prints 'mbstring extension missing' admin notice |
||
165 | */ |
||
166 | 1 | public function admin_notices_mbstring_incompatible() { |
|
167 | 1 | $this->display_error_notice( |
|
168 | /* translators: 1: plugin name 2: mbstring documentation URL */ |
||
169 | 1 | __( 'The activated plugin %1$s requires the mbstring PHP extension to be enabled on your server. Please deactivate this plugin, or <a href="%2$s">enable the extension</a>.', $this->textdomain ), |
|
170 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
171 | /* translators: URL with mbstring PHP extension installation instructions */ |
||
172 | 1 | __( 'http://www.php.net/manual/en/mbstring.installation.php', $this->textdomain ) |
|
173 | ); |
||
174 | 1 | } |
|
175 | |||
176 | /** |
||
177 | * Prints 'Charset incompatible' admin notice |
||
178 | */ |
||
179 | 1 | public function admin_notices_charset_incompatible() { |
|
180 | 1 | $this->display_error_notice( |
|
181 | /* translators: 1: plugin name 2: current character encoding 3: options URL */ |
||
182 | 1 | __( 'The activated plugin %1$s requires your blog use the UTF-8 character encoding. You have set your blogs encoding to %2$s. Please deactivate this plugin, or <a href="%3$s">change your character encoding to UTF-8</a>.', $this->textdomain ), |
|
183 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
184 | 1 | get_bloginfo( 'charset' ), |
|
185 | 1 | '/wp-admin/options-reading.php' |
|
186 | ); |
||
187 | 1 | } |
|
188 | |||
189 | /** |
||
190 | * Shows an error message in the admin area. |
||
191 | * |
||
192 | * @param string $format ... An `sprintf` format string, followd by an unspecified number of optional parameters. |
||
193 | */ |
||
194 | 2 | protected function display_error_notice( $format ) { |
|
195 | 2 | if ( func_num_args() < 1 || empty( $format ) ) { |
|
196 | 1 | return; // abort. |
|
197 | } |
||
198 | |||
199 | 1 | $args = func_get_args(); |
|
200 | 1 | $format = array_shift( $args ); |
|
201 | 1 | $message = vsprintf( $format, $args ); |
|
202 | |||
203 | 1 | require dirname( $this->plugin_file ) . '/partials/requirements-error-notice.php'; |
|
204 | 1 | } |
|
205 | } |
||
206 |