Skip to content

Commit

Permalink
update composer package
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Oct 9, 2023
1 parent 677533e commit 049c7fe
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 12 deletions.
169 changes: 169 additions & 0 deletions 10up-lib/wp-compat-validation-tool/src/Validator.php
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
}
}
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
},
"platform-check": false
},
"repositories": [
{
Expand All @@ -38,12 +40,15 @@
],
"scripts": {
"post-install-cmd": [
"./vendor/10up/wp-compat-validation-tool/replace-namespace.sh Simple_Page_Ordering_Validator",
"composer dump-autoload"
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh Simple_Page_Ordering_Validator"
],
"post-update-cmd": [
"./vendor/10up/wp-compat-validation-tool/replace-namespace.sh Simple_Page_Ordering_Validator",
"composer dump-autoload"
"./10up-lib/wp-compat-validation-tool/replace-namespace.sh Simple_Page_Ordering_Validator"
]
},
"extra": {
"installer-paths": {
"./{$name}/": ["10up/wp-compat-validation-tool"]
}
}
}
160 changes: 156 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions simple-page-ordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* @package simple-page-ordering
*/

if ( ! is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
if ( ! is_readable( __DIR__ . '/10up-lib/wp-compat-validation-tool/src/Validator.php' ) ) {
return;
}

require_once 'vendor/autoload.php';
require_once '10up-lib/wp-compat-validation-tool/src/Validator.php';

$compat_checker = new \Simple_Page_Ordering_Validator\Validator();
$compat_checker
Expand Down

0 comments on commit 049c7fe

Please sign in to comment.