Skip to content
Snippets Groups Projects
Commit 8a219ac1 authored by vasili.guruli's avatar vasili.guruli
Browse files

Fixes with required minimum plugin version notice / Refaktor

parent b92e355d
No related branches found
No related tags found
2 merge requests!19Feature/minimum plugin version check demo1,!18Minimum required plugin version demo1
Pipeline #9625 passed with warnings
......@@ -15,6 +15,8 @@
const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name';
const PLUGIN_INFO_KEY_NAME = 'name';
const PLUGIN_INFO_VERSION = 'version';
const PLUGIN_INFO_REQUIRED_VERSION = 'required_version';
/** @var string */
protected $plugin_name;
......@@ -38,10 +40,6 @@
protected $notices;
/** @var @string */
private $text_domain;
/** @var string */
private $minimum_required_plugin_version;
/** @var string */
private $force_plugin_update;
/**
* @param string $plugin_file
......@@ -49,15 +47,11 @@
* @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 ) {
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->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 );
......@@ -112,36 +106,18 @@
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
* @param string $plugin_require_version required plugin minimum version
*
* @return $this
*/
public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) {
public function add_plugin_require( $plugin_name, $nice_plugin_name = null, $plugin_require_version = 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
self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name,
self::PLUGIN_INFO_VERSION => $plugin_require_version === null ? '0.0' : $plugin_require_version,
);
return $this;
......@@ -228,14 +204,11 @@
$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 );
$notices = $this->check_minimum_require_plugins_version_and_display_notices( $notices );
return $notices;
}
......@@ -308,54 +281,53 @@
}
/**
* @param $required_minimum_version
* @param $notices
*
* @return bool
* @return array
*/
public function check_minimum_required_plugin_version( $required_minimum_version ) {
if ( null === $this->get_current_plugin_version() ) {
return false;
private function check_minimum_require_plugins_version_and_display_notices( $notices ) {
if ( $this->require_plugins() > 0 ) {
foreach ( $this->require_plugins() as $plugin ) {
if ( $plugin[ ucfirst( self::PLUGIN_INFO_VERSION ) ] < $plugin[ self::PLUGIN_INFO_REQUIRED_VERSION ] ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin requires at least %s version of %s to work correctly. Please update it', $this->get_text_domain() ),
esc_html( $this->plugin_name ), $plugin[ self::PLUGIN_INFO_REQUIRED_VERSION ], $plugin[ ucfirst( self::PLUGIN_INFO_KEY_NAME ) ] ) );
}
}
}
return $this->get_current_plugin_version() > $required_minimum_version;
return $notices;
}
/**
* Check the plugins directory and retrieve all required plugin files with plugin data.
*
* @return array
*/
private function get_existing_plugins() {
public function require_plugins() {
$require_plugins = array();
if ( file_exists( ABSPATH . '/wp-admin/includes/plugin.php' ) ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$get_existing_plugins = ( get_plugins() ? get_plugins() : array() );
return ( get_plugins() ? get_plugins() : [] );
if ( ! empty( $this->plugin_require ) ) {
foreach ( $this->plugin_require as $plugin ) {
if ( ! isset( $plugin[ self::PLUGIN_INFO_VERSION ] ) ) {
unset( $this->plugin_require[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ] );
} else {
if ( self::is_wp_plugin_active( $plugin[ self::PLUGIN_INFO_KEY_NAME ] ) ) {
$require_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ] = $get_existing_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ];
$require_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ][ self::PLUGIN_INFO_REQUIRED_VERSION ] = $plugin[ self::PLUGIN_INFO_VERSION ];
}
/**
* @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'];
}
}
}
return $version;
}
/**
* @return string
*/
private function notice_or_error()
{
$class = true == $this->force_plugin_update ? 'error' : 'notice notice-warning is-dismissible';
return $class;
return $require_plugins;
}
/**
......
......@@ -19,14 +19,13 @@ 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, $minimum_required_plugin_version ) {
public function create_requirement_checker( $plugin_file, $plugin_name, $text_domain = null ) {
return new WPDesk_Basic_Requirement_Checker( $plugin_file, $plugin_name,
$this->initialize_translations( $text_domain ), null, null, $minimum_required_plugin_version );
$this->initialize_translations( $text_domain ), null, null );
}
/**
......@@ -45,13 +44,12 @@ class WPDesk_Basic_Requirement_Checker_Factory {
$plugin_name,
$this->initialize_translations( $text_domain ),
$requirements['php'],
$requirements['wp'],
$requirements['minimum_required_plugin_version']
$requirements['wp']
);
if ( isset( $requirements['plugins'] ) ) {
foreach ( $requirements['plugins'] as $requirement ) {
$requirements_checker->add_plugin_require( $requirement['name'], $requirement['nice_name'] );
$requirements_checker->add_plugin_require( $requirement['name'], $requirement['nice_name'], $requirement['version'] );
}
}
......
......@@ -33,19 +33,6 @@ interface WPDesk_Requirement_Checker
*/
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
......
......@@ -15,8 +15,6 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase {
const HOOK_TYPE_ACTION = 'action';
const MINIMUM_REQUIRED_PLUGIN_VERSION = '1.0';
public function setUp() {
WP_Mock::setUp();
......@@ -61,7 +59,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::MINIMUM_REQUIRED_PLUGIN_VERSION );
self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp );
}
public function test_wp_version_check() {
......@@ -80,21 +78,6 @@ 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
*/
......@@ -211,9 +194,15 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase {
$requirements->add_plugin_repository_require( $activated_plugin_name, $random_version );
$this->assertTrue( $requirements->are_requirements_met(), "Should be met for activated plugin" );
$requirements->add_plugin_require( $activated_plugin_name, $random_version );
$this->assertTrue( $requirements->are_requirements_met(), 'Should be met for required plugins' );
$requirements->add_plugin_repository_require( $not_activated_plugin_name, $random_version );
$this->assertFalse( $requirements->are_requirements_met(), "Should NOT be met for only installed plugin" );
$requirements->add_plugin_require( $not_activated_plugin_name, $random_version );
$this->assertFalse( $requirements->are_requirements_met(), "Should NOT be met for only installed plugin" );
$this->expectOutputRegex( "/Activate $not_activated_plugin_name/" );
$requirements->handle_render_notices_action();
......
......@@ -11,6 +11,7 @@ class Test_Basic_Requirement_Checker_Factory extends PHPUnit\Framework\TestCase
array(
'name' => 'woocommerce/woocommerce.php',
'nice_name' => 'WooCommerce',
'version' => '1.0'
),
),
'repo_plugins' => array(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment