1 | <?php |
||
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 | * Sets up a new Mundschenk_WP_Requirements object. |
||
67 | * |
||
68 | * @param string $name The plugin name. |
||
69 | * @param string $plugin_path The full path to the main plugin file. |
||
70 | * @param string $textdomain The text domain used for i18n. |
||
71 | * @param array $requirements The requirements to check against. |
||
72 | */ |
||
73 | 1 | public function __construct( $name, $plugin_path, $textdomain, $requirements ) { |
|
74 | 1 | $this->plugin_name = $name; |
|
75 | 1 | $this->plugin_file = $plugin_path; |
|
76 | 1 | $this->textdomain = $textdomain; |
|
|
|||
77 | |||
78 | 1 | $this->install_requirements = \wp_parse_args( $requirements, array( |
|
79 | 1 | 'php' => '5.2.0', |
|
80 | 'multibyte' => false, |
||
81 | 'utf-8' => false, |
||
82 | ) ); |
||
83 | 1 | } |
|
84 | |||
85 | /** |
||
86 | * Checks if all runtime requirements for the plugin are met. |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | 11 | public function check() { |
|
91 | 11 | $requirements_met = true; |
|
92 | |||
93 | 11 | if ( ! empty( $this->install_requirements['php'] ) && version_compare( PHP_VERSION, $this->install_requirements['php'], '<' ) ) { |
|
94 | 6 | if ( is_admin() ) { |
|
95 | 3 | add_action( 'admin_notices', array( $this, 'admin_notices_php_version_incompatible' ) ); |
|
96 | } |
||
97 | 6 | $requirements_met = false; |
|
98 | 5 | } elseif ( ! empty( $this->install_requirements['multibyte'] ) && ! $this->check_multibyte_support() ) { |
|
99 | 2 | if ( is_admin() ) { |
|
100 | 1 | add_action( 'admin_notices', array( $this, 'admin_notices_mbstring_incompatible' ) ); |
|
101 | } |
||
102 | 2 | $requirements_met = false; |
|
103 | 3 | } elseif ( ! empty( $this->install_requirements['utf-8'] ) && ! $this->check_utf8_support() ) { |
|
104 | 1 | if ( is_admin() ) { |
|
105 | 1 | add_action( 'admin_notices', array( $this, 'admin_notices_charset_incompatible' ) ); |
|
106 | } |
||
107 | 1 | $requirements_met = false; |
|
108 | } |
||
109 | |||
110 | 11 | if ( ! $requirements_met && is_admin() ) { |
|
111 | // Load text domain to ensure translated admin notices. |
||
112 | 5 | load_plugin_textdomain( $this->textdomain ); |
|
113 | } |
||
114 | |||
115 | 11 | return $requirements_met; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Deactivates the plugin. |
||
120 | */ |
||
121 | 1 | public function deactivate_plugin() { |
|
122 | 1 | deactivate_plugins( plugin_basename( $this->plugin_file ) ); |
|
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * Checks if multibyte functions are supported. |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | 1 | protected function check_multibyte_support() { |
|
131 | 1 | return function_exists( 'mb_strlen' ) |
|
132 | 1 | && function_exists( 'mb_strtolower' ) |
|
133 | 1 | && function_exists( 'mb_substr' ) |
|
134 | 1 | && function_exists( 'mb_detect_encoding' ); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Checks if the blog charset is set to UTF-8. |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 4 | protected function check_utf8_support() { |
|
143 | 4 | return 'utf-8' === strtolower( get_bloginfo( 'charset' ) ); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Print 'PHP version incompatible' admin notice |
||
148 | */ |
||
149 | 1 | public function admin_notices_php_version_incompatible() { |
|
150 | 1 | $this->display_error_notice( |
|
151 | /* translators: 1: plugin name 2: target PHP version number 3: actual PHP version number */ |
||
152 | 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 ), |
|
153 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
154 | 1 | $this->install_requirements['php'], |
|
155 | 1 | phpversion() |
|
156 | ); |
||
157 | 1 | } |
|
158 | |||
159 | /** |
||
160 | * Prints 'mbstring extension missing' admin notice |
||
161 | */ |
||
162 | 1 | public function admin_notices_mbstring_incompatible() { |
|
169 | ); |
||
170 | 1 | } |
|
171 | |||
172 | /** |
||
173 | * Prints 'Charset incompatible' admin notice |
||
174 | */ |
||
175 | 1 | public function admin_notices_charset_incompatible() { |
|
176 | 1 | $this->display_error_notice( |
|
177 | /* translators: 1: plugin name 2: current character encoding 3: options URL */ |
||
178 | 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 ), |
|
179 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
180 | 1 | get_bloginfo( 'charset' ), |
|
181 | 1 | '/wp-admin/options-reading.php' |
|
182 | ); |
||
183 | 1 | } |
|
184 | |||
185 | /** |
||
186 | * Shows an error message in the admin area. |
||
187 | * |
||
188 | * @param string $format ... An `sprintf` format string, followd by an unspecified number of optional parameters. |
||
189 | */ |
||
190 | 2 | protected function display_error_notice( $format ) { |
|
200 | 1 | } |
|
201 | } |
||
202 |