-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
677533e
commit 049c7fe
Showing
4 changed files
with
338 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
namespace Simple_Page_Ordering_Validator; | ||
|
||
class Validator { | ||
/** | ||
* Array of checks. | ||
* | ||
* @var array | ||
*/ | ||
private $checklist = array(); | ||
|
||
/** | ||
* Array of error messages | ||
* | ||
* @var string[] | ||
*/ | ||
private $messages = array(); | ||
|
||
/** | ||
* Constructor function. | ||
*/ | ||
public function __construct() { | ||
$this->checklist = array( | ||
'plugin_name' => array( | ||
'value' => '', | ||
'required' => true, | ||
), | ||
'php_min_required_version' => array( | ||
'value' => '', | ||
'required' => false, | ||
), | ||
'php_max_required_version' => array( | ||
'value' => '', | ||
'required' => false, | ||
), | ||
'wp_min_required_version' => array( | ||
'value' => '', | ||
'required' => false, | ||
), | ||
'wp_max_required_version' => array( | ||
'value' => '', | ||
'required' => false, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Sets the plugin name. | ||
* | ||
* @param string $value Plugin name. | ||
* @return Validator | ||
*/ | ||
public function set_plugin_name( $value = '' ) { | ||
$this->checklist['plugin_name']['value'] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the minimum PHP version supported by a plugin. | ||
* | ||
* @param string $value Minimum PHP version. | ||
* @return Validator | ||
*/ | ||
public function set_php_min_required_version( $value = '' ) { | ||
$this->checklist['php_min_required_version']['value'] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the maximum PHP version supported by a plugin. | ||
* | ||
* @param string $value Maximum PHP version. | ||
* @return Validator | ||
*/ | ||
public function set_php_max_required_version( $value = '' ) { | ||
$this->checklist['php_max_required_version']['value'] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the minimum WordPress version supported by a plugin. | ||
* | ||
* @param string $value Minimum WordPress version. | ||
* @return Validator | ||
*/ | ||
public function set_wordpress_min_required_version( $value = '' ) { | ||
$this->checklist['wp_min_required_version']['value'] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the maximum WordPress version supported by a plugin. | ||
* | ||
* @param string $value Maximum WordPress version. | ||
* @return Validator | ||
*/ | ||
public function set_wordpress_max_required_version( $value = '' ) { | ||
$this->checklist['wp_max_required_version']['value'] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns true if the plugin meets all compatibility checks, false otherwise. | ||
* | ||
* @return boolean | ||
*/ | ||
public function is_plugin_compatible() { | ||
foreach ( $this->checklist as $item_name => $item_details ) { | ||
if ( $item_details['required'] && empty( $item_details['value'] ) ) { | ||
return false; | ||
} | ||
|
||
switch ( $item_name ) { | ||
case 'php_min_required_version': | ||
if ( ! empty( $item_details['value'] ) && version_compare( phpversion(), $item_details['value'], '<' ) ) { | ||
$this->messages[] = sprintf( esc_html__( 'The minimum PHP version required is %s' ), $item_details['value'] ); | ||
} | ||
break; | ||
|
||
case 'php_max_required_version': | ||
if ( ! empty( $item_details['value'] ) && version_compare( phpversion(), $item_details['value'], '>' ) ) { | ||
$this->messages[] = sprintf( esc_html__( 'The maximum PHP version supported is %s' ), $item_details['value'] ); | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
if ( ! empty( $this->messages ) ) { | ||
add_action( 'admin_notices', array( $this, 'render_php_compat_error' ) ); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Renders the error messages as notice. | ||
*/ | ||
public function render_php_compat_error() { | ||
?> | ||
<div class="notice notice-error"> | ||
<p> | ||
<strong> | ||
<?php printf( esc_html__( '%s error:' ), $this->checklist['plugin_name']['value'] ); ?> | ||
</strong> | ||
</p> | ||
<?php if ( count( $this->messages ) > 1 ) : ?> | ||
<ul> | ||
<?php foreach ( $this->messages as $message ) : ?> | ||
<li><?php echo esc_html( $message ); ?></li> | ||
<?php endforeach; ?> | ||
</ul> | ||
<?php elseif ( 1 === count( $this->messages ) ): ?> | ||
<p> | ||
<?php echo esc_html( $this->messages[0] ); ?> | ||
</p> | ||
<?php endif; ?> | ||
</div> | ||
<?php | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters