mundschenk-at /
check-wp-requirements
| 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 | * 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; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 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() { |
|
| 163 | 1 | $this->display_error_notice( |
|
| 164 | /* translators: 1: plugin name 2: mbstring documentation URL */ |
||
| 165 | 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 ), |
|
| 166 | 1 | "<strong>{$this->plugin_name}</strong>", |
|
| 167 | /* translators: URL with mbstring PHP extension installation instructions */ |
||
| 168 | 1 | __( 'http://www.php.net/manual/en/mbstring.installation.php', $this->textdomain ) |
|
| 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 ) { |
|
| 191 | 2 | if ( func_num_args() < 1 || empty( $format ) ) { |
|
| 192 | 1 | return; // abort. |
|
| 193 | } |
||
| 194 | |||
| 195 | 1 | $args = func_get_args(); |
|
| 196 | 1 | $format = array_shift( $args ); |
|
| 197 | 1 | $message = vsprintf( $format, $args ); |
|
| 198 | |||
| 199 | 1 | require dirname( $this->plugin_file ) . '/partials/requirements-error-notice.php'; |
|
| 200 | 1 | } |
|
| 201 | } |
||
| 202 |