plugin_name = $name; $this->plugin_file = $plugin_path; $this->textdomain = $textdomain; $this->base_dir = \dirname(__FILE__); $this->install_requirements = \wp_parse_args($requirements, ['php' => '5.2.0', 'multibyte' => \false, 'utf-8' => \false]); } /** * Checks if all runtime requirements for the plugin are met. * * @return bool */ public function check() { $requirements_met = \true; foreach ($this->get_requirements() as $requirement) { if (!empty($this->install_requirements[$requirement['enable_key']]) && !\call_user_func($requirement['check'])) { $notice = $requirement['notice']; $requirements_met = \false; break; } } if (!$requirements_met && !empty($notice) && \is_admin()) { // Load text domain to ensure translated admin notices. \load_plugin_textdomain($this->textdomain); // Add admin notice. \add_action('admin_notices', $notice); } return $requirements_met; } /** * Retrieves an array of requirement specifications. * * @return array { * An array of requirements checks. * * @type string $enable_key An index in the $install_requirements array to switch the check on and off. * @type callable $check A function returning true if the check was successful, false otherwise. * @type callable $notice A function displaying an appropriate error notice. * } */ protected function get_requirements() { return [['enable_key' => 'php', 'check' => [$this, 'check_php_support'], 'notice' => [$this, 'admin_notices_php_version_incompatible']], ['enable_key' => 'multibyte', 'check' => [$this, 'check_multibyte_support'], 'notice' => [$this, 'admin_notices_mbstring_incompatible']], ['enable_key' => 'utf-8', 'check' => [$this, 'check_utf8_support'], 'notice' => [$this, 'admin_notices_charset_incompatible']]]; } /** * Deactivates the plugin. */ public function deactivate_plugin() { \deactivate_plugins(\plugin_basename($this->plugin_file)); } /** * Checks if the PHP version in use is at least equal to the required version. * * @return bool */ protected function check_php_support() { return \version_compare(\PHP_VERSION, $this->install_requirements['php'], '>='); } /** * Checks if multibyte functions are supported. * * @return bool */ protected function check_multibyte_support() { return \function_exists('mb_strlen') && \function_exists('mb_strtolower') && \function_exists('mb_substr') && \function_exists('mb_detect_encoding'); } /** * Checks if the blog charset is set to UTF-8. * * @return bool */ protected function check_utf8_support() { return 'utf-8' === \strtolower(\get_bloginfo('charset')); } /** * Print 'PHP version incompatible' admin notice */ public function admin_notices_php_version_incompatible() { $this->display_error_notice( /* translators: 1: plugin name 2: target PHP version number 3: actual PHP version number */ \__('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), "{$this->plugin_name}", $this->install_requirements['php'], \PHP_VERSION ); } /** * Prints 'mbstring extension missing' admin notice */ public function admin_notices_mbstring_incompatible() { $this->display_error_notice( /* translators: 1: plugin name 2: mbstring documentation URL */ \__('The activated plugin %1$s requires the mbstring PHP extension to be enabled on your server. Please deactivate this plugin, or enable the extension.', $this->textdomain), "{$this->plugin_name}", /* translators: URL with mbstring PHP extension installation instructions */ \__('http://www.php.net/manual/en/mbstring.installation.php', $this->textdomain) ); } /** * Prints 'Charset incompatible' admin notice */ public function admin_notices_charset_incompatible() { $this->display_error_notice( /* translators: 1: plugin name 2: current character encoding 3: options URL */ \__('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 change your character encoding to UTF-8.', $this->textdomain), "{$this->plugin_name}", \get_bloginfo('charset'), '/wp-admin/options-reading.php' ); } /** * Shows an error message in the admin area. * * @param string $format ... An `sprintf` format string, followd by an unspecified number of optional parameters. */ protected function display_error_notice($format) { if (\func_num_args() < 1 || empty($format)) { return; // abort. } $args = \func_get_args(); $format = \array_shift($args); $message = \vsprintf($format, $args); require "{$this->base_dir}/partials/requirements-error-notice.php"; } }