diff --git a/src/Basic_Requirement_Checker.php b/src/Basic_Requirement_Checker.php index 40b7aef905a8176e31e55362110841e9de34797e..f7912b657ab3ac94ae4037ec6a404f24d3537d4f 100644 --- a/src/Basic_Requirement_Checker.php +++ b/src/Basic_Requirement_Checker.php @@ -1,509 +1,592 @@ <?php - -if ( ! interface_exists( 'WPDesk_Requirement_Checker' ) ) { - require_once 'Requirement_Checker.php'; -} - -if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { - /** - * Checks requirements for plugin - * have to be compatible with PHP 5.3.x - */ - class WPDesk_Basic_Requirement_Checker implements WPDesk_Requirement_Checker { - const EXTENSION_NAME_OPENSSL = 'openssl'; - const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices'; - - const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name'; - const PLUGIN_INFO_KEY_NAME = 'name'; - - /** @var string */ - protected $plugin_name; - /** @var string */ - private $plugin_file; - /** @var string */ - private $min_php_version; - /** @var string */ - private $min_wp_version; - /** @var string|null */ - private $min_wc_version = null; - /** @var int|null */ - private $min_openssl_version = null; - /** @var array */ - protected $plugin_require; - /** @var array */ - private $module_require; - /** @var array */ - private $setting_require; - /** @var array */ - protected $notices; - /** @var @string */ - private $text_domain; - - /** - * @param string $plugin_file - * @param string $plugin_name - * @param string $text_domain - * @param string $php_version - * @param string $wp_version - */ - public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version ) { - $this->plugin_file = $plugin_file; - $this->plugin_name = $plugin_name; - $this->text_domain = $text_domain; - - $this->set_min_php_require( $php_version ); - $this->set_min_wp_require( $wp_version ); - - $this->plugin_require = array(); - $this->module_require = array(); - $this->setting_require = array(); - $this->notices = array(); - } - - /** - * @param string $version - * - * @return $this - */ - public function set_min_php_require( $version ) { - $this->min_php_version = $version; - - return $this; - } - - /** - * @param string $version - * - * @return $this - */ - public function set_min_wp_require( $version ) { - $this->min_wp_version = $version; - - return $this; - } - - /** - * @param string $version - * - * @return $this - */ - public function set_min_wc_require( $version ) { - $this->min_wc_version = $version; - - return $this; - } - - /** - * @param $version - * - * @return $this - */ - public function set_min_openssl_require( $version ) { - $this->min_openssl_version = $version; - - return $this; - } - - /** - * @param string $plugin_name Name in wp format dir/file.php - * @param string $nice_plugin_name Nice plugin name for better looks in notice - * - * @return $this - */ - public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) { - $this->plugin_require[ $plugin_name ] = array( - self::PLUGIN_INFO_KEY_NAME => $plugin_name, - self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name - ); - - return $this; - } - - /** - * Add plugin to require list. Plugin is from repository so we can ask for installation. - * - * @param string $plugin_name Name in wp format dir/file.php - * @param string $version Required version of the plugin. - * @param string $nice_plugin_name Nice plugin name for better looks in notice - * - * @return $this - */ - public function add_plugin_repository_require( $plugin_name, $version, $nice_plugin_name = null ) { - $this->plugin_require[ $plugin_name ] = array( - self::PLUGIN_INFO_KEY_NAME => $plugin_name, - 'version' => $version, - 'repository_url' => 'http://downloads.wordpress.org/plugin/' . dirname( $plugin_name ) . '.latest-stable.zip', - self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name - ); - - return $this; - } - - /** - * @param string $module_name - * @param string $nice_name Nice module name for better looks in notice - * - * @return $this - */ - public function add_php_module_require( $module_name, $nice_name = null ) { - if ( $nice_name === null ) { - $this->module_require[ $module_name ] = $module_name; - } else { - $this->module_require[ $module_name ] = $nice_name; - } - - return $this; - } - - /** - * @param string $setting - * @param mixed $value - * - * @return $this - */ - public function add_php_setting_require( $setting, $value ) { - $this->setting_require[ $setting ] = $value; - - return $this; - } - - /** - * Returns true if are requirements are met. - * - * @return bool - */ - public function are_requirements_met() { - $this->notices = $this->prepare_requirement_notices(); - - return count( $this->notices ) === 0; - } - - /** - * @return array - */ - private function prepare_requirement_notices() { - $notices = array(); - if ( ! self::is_php_at_least( $this->min_php_version ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on PHP versions older than %s. Please contact your host and ask them to upgrade.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_php_version ) ); - } - if ( ! self::is_wp_at_least( $this->min_wp_version ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WordPress versions older than %s. Please update WordPress.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wp_version ) ); - } - if ( $this->min_wc_version !== null && $this->can_check_plugin_version() && ! self::is_wc_at_least( $this->min_wc_version ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WooCommerce versions older than %s. Please update WooCommerce.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wc_version ) ); - } - if ( $this->min_openssl_version !== null && ! self::is_open_ssl_at_least( $this->min_openssl_version ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without OpenSSL module version at least %s. Please update OpenSSL module.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), - '0x' . dechex( $this->min_openssl_version ) ) ); - } - - $notices = $this->append_plugin_require_notices( $notices ); - $notices = $this->append_module_require_notices( $notices ); - $notices = $this->append_settings_require_notices( $notices ); - - return $notices; - } - - /** - * @param $min_version - * - * @return mixed - */ - public static function is_php_at_least( $min_version ) { - return version_compare( PHP_VERSION, $min_version, '>=' ); - } - - /** - * Prepares message in html format - * - * @param string $message - * - * @return string - */ - protected function prepare_notice_message( $message ) { - return '<div class="error"><p>' . $message . '</p></div>'; - } - - public function get_text_domain() { - return $this->text_domain; - } - - /** - * @param string $min_version - * - * @return bool - */ - public static function is_wp_at_least( $min_version ) { - return version_compare( get_bloginfo( 'version' ), $min_version, '>=' ); - } - - /** - * Are plugins loaded so we can check the version - * - * @return bool - */ - private function can_check_plugin_version() { - return did_action( 'plugins_loaded' ) > 0; - } - - /** - * Checks if plugin is active and have designated version. Needs to be enabled in deferred way. - * - * @param string $min_version - * - * @return bool - */ - public static function is_wc_at_least( $min_version ) { - return defined( 'WC_VERSION' ) && - version_compare( WC_VERSION, $min_version, '>=' ); - } - - /** - * Checks if ssl version is valid - * - * @param int $required_version Version in hex. Version 9.6 is 0x000906000 - * - * @return bool - * @see https://www.openssl.org/docs/man1.1.0/crypto/OPENSSL_VERSION_NUMBER.html - * - */ - public static function is_open_ssl_at_least( $required_version ) { - return defined( 'OPENSSL_VERSION_NUMBER' ) && OPENSSL_VERSION_NUMBER > (int) $required_version; - } - + + if ( ! interface_exists( 'WPDesk_Requirement_Checker' ) ) { + require_once 'Requirement_Checker.php'; + } + + if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { /** - * @param array $notices - * - * @return array + * Checks requirements for plugin + * have to be compatible with PHP 5.3.x */ - private function append_plugin_require_notices( $notices ) { - if ( count( $this->plugin_require ) > 0 ) { - foreach ( $this->plugin_require as $plugin_name => $plugin_info ) { - $notice = null; - if ( isset( $plugin_info['repository_url'] ) ) { - $notice = $this->prepare_plugin_repository_require_notice( $plugin_info ); - } elseif ( ! self::is_wp_plugin_active( $plugin_name ) ) { - $notice = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s active. Please install and activate %s plugin.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), - esc_html( basename( $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ] ) ), - esc_html( basename( $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ] ) ) ) ); + class WPDesk_Basic_Requirement_Checker implements WPDesk_Requirement_Checker { + const EXTENSION_NAME_OPENSSL = 'openssl'; + const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices'; + + const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name'; + const PLUGIN_INFO_KEY_NAME = 'name'; + + /** @var string */ + protected $plugin_name; + /** @var string */ + private $plugin_file; + /** @var string */ + private $min_php_version; + /** @var string */ + private $min_wp_version; + /** @var string|null */ + private $min_wc_version = null; + /** @var int|null */ + private $min_openssl_version = null; + /** @var array */ + protected $plugin_require; + /** @var array */ + private $module_require; + /** @var array */ + private $setting_require; + /** @var array */ + protected $notices; + /** @var @string */ + private $text_domain; + /** @var string */ + private $minimum_required_plugin_version; + /** @var string */ + private $force_plugin_update; + + /** + * @param string $plugin_file + * @param string $plugin_name + * @param string $text_domain + * @param string $php_version + * @param string $wp_version + * @param string $minimum_required_plugin_version + * @param bool $force_plugin_update + */ + public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version, $minimum_required_plugin_version, $force_plugin_update = false ) { + $this->plugin_file = $plugin_file; + $this->plugin_name = $plugin_name; + $this->text_domain = $text_domain; + $this->minimum_required_plugin_version = $minimum_required_plugin_version; + $this->force_plugin_update = $force_plugin_update; + + $this->set_min_php_require( $php_version ); + $this->set_min_wp_require( $wp_version ); + + $this->plugin_require = array(); + $this->module_require = array(); + $this->setting_require = array(); + $this->notices = array(); + } + + /** + * @param string $version + * + * @return $this + */ + public function set_min_php_require( $version ) { + $this->min_php_version = $version; + + return $this; + } + + /** + * @param string $version + * + * @return $this + */ + public function set_min_wp_require( $version ) { + $this->min_wp_version = $version; + + return $this; + } + + /** + * @param string $version + * + * @return $this + */ + public function set_min_wc_require( $version ) { + $this->min_wc_version = $version; + + return $this; + } + + /** + * @param $version + * + * @return $this + */ + public function set_min_openssl_require( $version ) { + $this->min_openssl_version = $version; + + return $this; + } + + /** + * @param $minimum_require_plugin_version + * + * @return WPDesk_Basic_Requirement_Checker + */ + public function set_minimum_require_plugin_version( $minimum_require_plugin_version ) { + $this->minimum_required_plugin_version = $minimum_require_plugin_version; + return $this; + } + + /** + * @param $force_plugin_update + * + * @return WPDesk_Basic_Requirement_Checker + */ + public function force_plugin_update( $force_plugin_update ) { + $this->force_plugin_update = $force_plugin_update; + return $this; + } + + /** + * @param string $plugin_name Name in wp format dir/file.php + * @param string $nice_plugin_name Nice plugin name for better looks in notice + * + * @return $this + */ + public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) { + $this->plugin_require[ $plugin_name ] = array( + self::PLUGIN_INFO_KEY_NAME => $plugin_name, + self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name + ); + + return $this; + } + + /** + * Add plugin to require list. Plugin is from repository so we can ask for installation. + * + * @param string $plugin_name Name in wp format dir/file.php + * @param string $version Required version of the plugin. + * @param string $nice_plugin_name Nice plugin name for better looks in notice + * + * @return $this + */ + public function add_plugin_repository_require( $plugin_name, $version, $nice_plugin_name = null ) { + $this->plugin_require[ $plugin_name ] = array( + self::PLUGIN_INFO_KEY_NAME => $plugin_name, + 'version' => $version, + 'repository_url' => 'http://downloads.wordpress.org/plugin/' . dirname( $plugin_name ) . '.latest-stable.zip', + self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name + ); + + return $this; + } + + /** + * @param string $module_name + * @param string $nice_name Nice module name for better looks in notice + * + * @return $this + */ + public function add_php_module_require( $module_name, $nice_name = null ) { + if ( $nice_name === null ) { + $this->module_require[ $module_name ] = $module_name; + } else { + $this->module_require[ $module_name ] = $nice_name; + } + + return $this; + } + + /** + * @param string $setting + * @param mixed $value + * + * @return $this + */ + public function add_php_setting_require( $setting, $value ) { + $this->setting_require[ $setting ] = $value; + + return $this; + } + + /** + * Returns true if are requirements are met. + * + * @return bool + */ + public function are_requirements_met() { + $this->notices = $this->prepare_requirement_notices(); + + return count( $this->notices ) === 0; + } + + /** + * @return array + */ + private function prepare_requirement_notices() { + $notices = array(); + if ( ! self::is_php_at_least( $this->min_php_version ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on PHP versions older than %s. Please contact your host and ask them to upgrade.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_php_version ) ); + } + if ( ! self::is_wp_at_least( $this->min_wp_version ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WordPress versions older than %s. Please update WordPress.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wp_version ) ); + } + if ( $this->min_wc_version !== null && $this->can_check_plugin_version() && ! self::is_wc_at_least( $this->min_wc_version ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WooCommerce versions older than %s. Please update WooCommerce.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wc_version ) ); + } + if ( $this->min_openssl_version !== null && ! self::is_open_ssl_at_least( $this->min_openssl_version ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without OpenSSL module version at least %s. Please update OpenSSL module.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), + '0x' . dechex( $this->min_openssl_version ) ) ); + } + if ( false !== $this->check_minimum_required_plugin_version( $this->minimum_required_plugin_version ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __('The “%s” plugin requires at least %s version to work correctly with WooCommerce. Please update it', $this->get_text_domain() ), + esc_html( $this->plugin_name ), $this->minimum_required_plugin_version ) ); + } + + $notices = $this->append_plugin_require_notices( $notices ); + $notices = $this->append_module_require_notices( $notices ); + $notices = $this->append_settings_require_notices( $notices ); + + return $notices; + } + + /** + * @param $min_version + * + * @return mixed + */ + public static function is_php_at_least( $min_version ) { + return version_compare( PHP_VERSION, $min_version, '>=' ); + } + + /** + * Prepares message in html format + * + * @param string $message + * + * @return string + */ + protected function prepare_notice_message( $message ) { + return '<div class="error"><p>' . $message . '</p></div>'; + } + + public function get_text_domain() { + return $this->text_domain; + } + + /** + * @param string $min_version + * + * @return bool + */ + public static function is_wp_at_least( $min_version ) { + return version_compare( get_bloginfo( 'version' ), $min_version, '>=' ); + } + + /** + * Are plugins loaded so we can check the version + * + * @return bool + */ + private function can_check_plugin_version() { + return did_action( 'plugins_loaded' ) > 0; + } + + /** + * Checks if plugin is active and have designated version. Needs to be enabled in deferred way. + * + * @param string $min_version + * + * @return bool + */ + public static function is_wc_at_least( $min_version ) { + return defined( 'WC_VERSION' ) && + version_compare( WC_VERSION, $min_version, '>=' ); + } + + /** + * Checks if ssl version is valid + * + * @param int $required_version Version in hex. Version 9.6 is 0x000906000 + * + * @return bool + * @see https://www.openssl.org/docs/man1.1.0/crypto/OPENSSL_VERSION_NUMBER.html + * + */ + public static function is_open_ssl_at_least( $required_version ) { + return defined( 'OPENSSL_VERSION_NUMBER' ) && OPENSSL_VERSION_NUMBER > (int) $required_version; + } + + /** + * @param $required_minimum_version + * + * @return bool + */ + public function check_minimum_required_plugin_version( $required_minimum_version ) { + if ( null === $this->get_current_plugin_version() ) { + return false; + } + + return $this->get_current_plugin_version() > $required_minimum_version; + } + + /** + * @return array + */ + private function get_existing_plugins() { + if( ! function_exists( 'get_plugins' ) ) { + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + } + + return ( get_plugins() ? get_plugins() : [] ); + } + + /** + * @return mixed|null + */ + public function get_current_plugin_version() + { + $version = null; + if (! empty( $this->get_existing_plugins() ) ) { + foreach ( $this->get_existing_plugins() as $plugin ) { + if ( $plugin['Name'] === $this->plugin_name ) { + $version = $plugin['Version']; + } } - - if ( $notice !== null ) { - $notices[] = $notice; + } + + return $version; + } + + /** + * @return string + */ + private function notice_or_error() + { + $class = true == $this->force_plugin_update ? 'error' : 'notice notice-warning is-dismissible'; + + return $class; + } + + /** + * @param array $notices + * + * @return array + */ + private function append_plugin_require_notices( $notices ) { + if ( count( $this->plugin_require ) > 0 ) { + foreach ( $this->plugin_require as $plugin_name => $plugin_info ) { + $notice = null; + if ( isset( $plugin_info['repository_url'] ) ) { + $notice = $this->prepare_plugin_repository_require_notice( $plugin_info ); + } elseif ( ! self::is_wp_plugin_active( $plugin_name ) ) { + $notice = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s active. Please install and activate %s plugin.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), + esc_html( basename( $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ] ) ), + esc_html( basename( $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ] ) ) ) ); + } + + if ( $notice !== null ) { + $notices[] = $notice; + } } } + + return $notices; } - - return $notices; - } - - /** - * Prepares WP install url and injects info about plugin to the WP update engine. - * - * @param array $plugin_info - * - * @return string - */ - private function prepare_plugin_repository_install_url( $plugin_info ) { - $slug = basename( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ); - $install_url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ); - if ( function_exists( 'wp_nonce_url' ) && function_exists( 'wp_create_nonce' ) ) { - $install_url = wp_nonce_url( $install_url, 'install-plugin_' . $slug ); - } - add_filter( 'plugins_api', function ( $api, $action, $args ) use ( $plugin_info, $slug ) { - if ( 'plugin_information' !== $action || - false !== $api || - ! isset( $args->slug ) || - $slug !== $args->slug - ) { - return $api; + + /** + * Prepares WP install url and injects info about plugin to the WP update engine. + * + * @param array $plugin_info + * + * @return string + */ + private function prepare_plugin_repository_install_url( $plugin_info ) { + $slug = basename( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ); + $install_url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ); + if ( function_exists( 'wp_nonce_url' ) && function_exists( 'wp_create_nonce' ) ) { + $install_url = wp_nonce_url( $install_url, 'install-plugin_' . $slug ); } - - $api = new stdClass(); - $api->name = $plugin_info['nice_name']; // self in closures requires 5.4 - $api->version = $plugin_info['version']; // self in closures requires 5.4 - $api->download_link = esc_url( $plugin_info['repository_url'] ); // self in closures requires 5.4 - - return $api; - }, 10, 3 ); - - return $install_url; - } - - /** - * @param array $plugin_info Internal required plugin info data. - * - * @return string|null Return null if no notice is needed. - */ - private function prepare_plugin_repository_require_notice( $plugin_info ) { - $name = $plugin_info[ self::PLUGIN_INFO_KEY_NAME ]; - $nice_name = $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ]; - - if ( ! self::is_wp_plugin_active( $name ) ) { - if ( ! self::is_wp_plugin_installed( $name ) ) { - $install_url = $this->prepare_plugin_repository_install_url( $plugin_info ); - - return $this->prepare_notice_message( sprintf( wp_kses( __( 'The “%s” plugin requires free %s plugin. <a href="%s">Install %s →</a>', + add_filter( 'plugins_api', function ( $api, $action, $args ) use ( $plugin_info, $slug ) { + if ( 'plugin_information' !== $action || + false !== $api || + ! isset( $args->slug ) || + $slug !== $args->slug + ) { + return $api; + } + + $api = new stdClass(); + $api->name = $plugin_info['nice_name']; // self in closures requires 5.4 + $api->version = $plugin_info['version']; // self in closures requires 5.4 + $api->download_link = esc_url( $plugin_info['repository_url'] ); // self in closures requires 5.4 + + return $api; + }, 10, 3 ); + + return $install_url; + } + + /** + * @param array $plugin_info Internal required plugin info data. + * + * @return string|null Return null if no notice is needed. + */ + private function prepare_plugin_repository_require_notice( $plugin_info ) { + $name = $plugin_info[ self::PLUGIN_INFO_KEY_NAME ]; + $nice_name = $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ]; + + if ( ! self::is_wp_plugin_active( $name ) ) { + if ( ! self::is_wp_plugin_installed( $name ) ) { + $install_url = $this->prepare_plugin_repository_install_url( $plugin_info ); + + return $this->prepare_notice_message( sprintf( wp_kses( __( 'The “%s” plugin requires free %s plugin. <a href="%s">Install %s →</a>', + $this->get_text_domain() ), array( 'a' => array( 'href' => array() ) ) ), + $this->plugin_name, $nice_name, esc_url( $install_url ), $nice_name ) ); + } + $activate_url = 'plugins.php?action=activate&plugin=' . urlencode( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ) . '&plugin_status=all&paged=1&s'; + if ( function_exists( 'wp_create_nonce' ) ) { + $activate_url .= '&_wpnonce=' . urlencode( wp_create_nonce( 'activate-plugin_' . $name ) ); + } + + return $this->prepare_notice_message( sprintf( wp_kses( __( 'The “%s” plugin requires activating %s plugin. <a href="%s">Activate %s →</a>', $this->get_text_domain() ), array( 'a' => array( 'href' => array() ) ) ), - $this->plugin_name, $nice_name, esc_url( $install_url ), $nice_name ) ); + $this->plugin_name, $nice_name, esc_url( admin_url( $activate_url ) ), $nice_name ) ); } - $activate_url = 'plugins.php?action=activate&plugin=' . urlencode( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ) . '&plugin_status=all&paged=1&s'; - if ( function_exists( 'wp_create_nonce' ) ) { - $activate_url .= '&_wpnonce=' . urlencode( wp_create_nonce( 'activate-plugin_' . $name ) ); + + return null; + } + + /** + * Checks if plugin is active. Needs to be used in deferred way. + * + * @param string $plugin_file + * + * @return bool + */ + public static function is_wp_plugin_active( $plugin_file ) { + $active_plugins = (array) get_option( 'active_plugins', array() ); + + if ( is_multisite() ) { + $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); } - - return $this->prepare_notice_message( sprintf( wp_kses( __( 'The “%s” plugin requires activating %s plugin. <a href="%s">Activate %s →</a>', - $this->get_text_domain() ), array( 'a' => array( 'href' => array() ) ) ), - $this->plugin_name, $nice_name, esc_url( admin_url( $activate_url ) ), $nice_name ) ); + + return in_array( $plugin_file, $active_plugins ) || array_key_exists( $plugin_file, $active_plugins ); } - - return null; - } - - /** - * Checks if plugin is active. Needs to be used in deferred way. - * - * @param string $plugin_file - * - * @return bool - */ - public static function is_wp_plugin_active( $plugin_file ) { - $active_plugins = (array) get_option( 'active_plugins', array() ); - - if ( is_multisite() ) { - $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); - } - - return in_array( $plugin_file, $active_plugins ) || array_key_exists( $plugin_file, $active_plugins ); - } - - /** - * Checks if plugin is installed. Needs to be enabled in deferred way. - * - * @param string $plugin_file - * - * @return bool - */ - public static function is_wp_plugin_installed( $plugin_file ) { - if ( ! function_exists( 'get_plugins' ) ) { - require_once ABSPATH . '/wp-admin/includes/plugin.php'; + + /** + * Checks if plugin is installed. Needs to be enabled in deferred way. + * + * @param string $plugin_file + * + * @return bool + */ + public static function is_wp_plugin_installed( $plugin_file ) { + if ( ! function_exists( 'get_plugins' ) ) { + require_once ABSPATH . '/wp-admin/includes/plugin.php'; + } + + return array_key_exists( $plugin_file, get_plugins() ); } - - return array_key_exists( $plugin_file, get_plugins() ); - } - - /** - * @param array $notices - * - * @return array - */ - private function append_module_require_notices( $notices ) { - if ( count( $this->module_require ) > 0 ) { - foreach ( $this->module_require as $module_name => $nice_module_name ) { - if ( ! self::is_module_active( $module_name ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php module installed. Please contact your host and ask them to install %s.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), - esc_html( basename( $nice_module_name ) ), esc_html( basename( $nice_module_name ) ) ) ); + + /** + * @param array $notices + * + * @return array + */ + private function append_module_require_notices( $notices ) { + if ( count( $this->module_require ) > 0 ) { + foreach ( $this->module_require as $module_name => $nice_module_name ) { + if ( ! self::is_module_active( $module_name ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php module installed. Please contact your host and ask them to install %s.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), + esc_html( basename( $nice_module_name ) ), esc_html( basename( $nice_module_name ) ) ) ); + } } } + + return $notices; } - - return $notices; - } - - /** - * @param string $name - * - * @return bool - */ - public static function is_module_active( $name ) { - return extension_loaded( $name ); - } - - /** - * @param array $notices - * - * @return array - */ - private function append_settings_require_notices( $notices ) { - if ( count( $this->setting_require ) > 0 ) { - foreach ( $this->setting_require as $setting => $value ) { - if ( ! self::is_setting_set( $setting, $value ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php setting set to %s. Please contact your host and ask them to set %s.', - $this->get_text_domain() ), esc_html( $this->plugin_name ), - esc_html( basename( $setting ) ), - esc_html( basename( $value ) ), esc_html( basename( $setting ) ) ) ); + + /** + * @param string $name + * + * @return bool + */ + public static function is_module_active( $name ) { + return extension_loaded( $name ); + } + + /** + * @param array $notices + * + * @return array + */ + private function append_settings_require_notices( $notices ) { + if ( count( $this->setting_require ) > 0 ) { + foreach ( $this->setting_require as $setting => $value ) { + if ( ! self::is_setting_set( $setting, $value ) ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php setting set to %s. Please contact your host and ask them to set %s.', + $this->get_text_domain() ), esc_html( $this->plugin_name ), + esc_html( basename( $setting ) ), + esc_html( basename( $value ) ), esc_html( basename( $setting ) ) ) ); + } } } + + return $notices; } - - return $notices; - } - - /** - * @param string $name - * @param mixed $value - * - * @return bool - */ - public static function is_setting_set( $name, $value ) { - return ini_get( $name ) === (string) $value; - } - - /** - * @return void - * - * @deprecated use render_notices or disable_plugin - */ - public function disable_plugin_render_notice() { - add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) ); - } - - /** - * Renders requirement notices in admin panel - * - * @return void - */ - public function render_notices() { - add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) ); - } - - /** - * Renders requirement notices in admin panel - * - * @return void - */ - public function disable_plugin() { - add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_deactivate_action' ) ); - } - - /** - * @return void - * @internal Do not use as public. Public only for wp action. - * - */ - public function handle_deactivate_action() { - if ( isset( $this->plugin_file ) ) { - deactivate_plugins( plugin_basename( $this->plugin_file ) ); + + /** + * @param string $name + * @param mixed $value + * + * @return bool + */ + public static function is_setting_set( $name, $value ) { + return ini_get( $name ) === (string) $value; } - } - - /** - * Should be called as WordPress action - * - * @return void - * @internal Do not use as public. Public only for wp action. - * - */ - public function handle_render_notices_action() { - foreach ( $this->notices as $notice ) { - echo $notice; + + /** + * @return void + * + * @deprecated use render_notices or disable_plugin + */ + public function disable_plugin_render_notice() { + add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) ); + } + + /** + * Renders requirement notices in admin panel + * + * @return void + */ + public function render_notices() { + add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) ); + } + + /** + * Renders requirement notices in admin panel + * + * @return void + */ + public function disable_plugin() { + add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_deactivate_action' ) ); + } + + /** + * @return void + * @internal Do not use as public. Public only for wp action. + * + */ + public function handle_deactivate_action() { + if ( isset( $this->plugin_file ) ) { + deactivate_plugins( plugin_basename( $this->plugin_file ) ); + } + } + + /** + * Should be called as WordPress action + * + * @return void + * @internal Do not use as public. Public only for wp action. + * + */ + public function handle_render_notices_action() { + foreach ( $this->notices as $notice ) { + echo $notice; + } } } - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/Basic_Requirement_Checker_Factory.php b/src/Basic_Requirement_Checker_Factory.php index 5c691b679b8f52844f78e3af1d63a5ec64074689..e46cf86895d0a04bfb3e0fae31b37135db490393 100644 --- a/src/Basic_Requirement_Checker_Factory.php +++ b/src/Basic_Requirement_Checker_Factory.php @@ -19,13 +19,14 @@ class WPDesk_Basic_Requirement_Checker_Factory { * * @param string $plugin_file * @param string $plugin_name + * @param string $minimum_required_plugin_version * @param string|null $text_domain Text domain to use. If null try to use library text domain. * * @return WPDesk_Requirement_Checker */ - public function create_requirement_checker( $plugin_file, $plugin_name, $text_domain = null ) { + public function create_requirement_checker( $plugin_file, $plugin_name, $text_domain = null, $minimum_required_plugin_version ) { return new WPDesk_Basic_Requirement_Checker( $plugin_file, $plugin_name, - $this->initialize_translations( $text_domain ), null, null ); + $this->initialize_translations( $text_domain ), null, null, $minimum_required_plugin_version ); } /** @@ -44,7 +45,8 @@ class WPDesk_Basic_Requirement_Checker_Factory { $plugin_name, $this->initialize_translations( $text_domain ), $requirements['php'], - $requirements['wp'] + $requirements['wp'], + $requirements['minimum_required_plugin_version'] ); if ( isset( $requirements['plugins'] ) ) { diff --git a/src/Requirement_Checker.php b/src/Requirement_Checker.php index aa472cad4e5c02e9ad38aded009b296d1c370f87..8b036d9031ce013a79d33d8426bdd8ecea16600b 100644 --- a/src/Requirement_Checker.php +++ b/src/Requirement_Checker.php @@ -32,7 +32,20 @@ interface WPDesk_Requirement_Checker * @return $this */ public function set_min_openssl_require($version); - + + /** + * @param $minimum_require_plugin_version + * + * @return $this + */ + public function set_minimum_require_plugin_version($minimum_require_plugin_version); + + /** + * @param $force_plugin_update + * + * @return $this + */ + public function force_plugin_update($force_plugin_update); /** * @param string $plugin_name * @param string $nice_plugin_name Nice plugin name for better looks in notice diff --git a/tests/unit/Test_Basic_Requirement_Checker.php b/tests/unit/Test_Basic_Requirement_Checker.php index 552b31fbbff8e58fc1d9ee8a26eb7f8fe3912e69..499fe5102b21a37fafb564586ba50e088c75508a 100644 --- a/tests/unit/Test_Basic_Requirement_Checker.php +++ b/tests/unit/Test_Basic_Requirement_Checker.php @@ -14,6 +14,8 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { const ALWAYS_VALID_WP_VERSION = '4.0'; const HOOK_TYPE_ACTION = 'action'; + + const MINIMUM_REQUIRED_PLUGIN_VERSION = '1.0'; public function setUp() { WP_Mock::setUp(); @@ -48,6 +50,8 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { $this->expectOutputRegex( "/PHP/" ); $requirements->handle_render_notices_action(); } + + /** * @param string $php @@ -57,7 +61,7 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { */ private function create_requirements_for_php_wp( $php, $wp ) { return new WPDesk_Basic_Requirement_Checker( self::RANDOM_PLUGIN_FILE, self::RANDOM_PLUGIN_NAME, - self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp ); + self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp, self::MINIMUM_REQUIRED_PLUGIN_VERSION ); } public function test_wp_version_check() { @@ -76,6 +80,21 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { $requirements->handle_render_notices_action(); } + public function test_minimum_plugin_version_check() { + $minimum_plugin_version_fail = '0.1'; + + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + $this->assertTrue( $requirements->are_requirements_met(), 'Minimum plugin version is ok' ); + $requirements->set_minimum_require_plugin_version( $minimum_plugin_version_fail ); + $this->assertFalse( $requirements->are_requirements_met(), + 'Failed as minimum required plugin version should be ' . $minimum_plugin_version_fail ); + + $this->expectOutputRegex( "/Plugin/" ); + $requirements->handle_render_notices_action(); + } + /** * @requires extension curl */