Plugin Updates
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
require_once WPCF7_PLUGIN_DIR . '/includes/swv/schema-holder.php';
|
||||
require_once WPCF7_PLUGIN_DIR . '/includes/swv/script-loader.php';
|
||||
require_once WPCF7_PLUGIN_DIR . '/includes/swv/php/abstract-rules.php';
|
||||
|
||||
|
||||
/**
|
||||
@@ -12,27 +13,29 @@ require_once WPCF7_PLUGIN_DIR . '/includes/swv/script-loader.php';
|
||||
*/
|
||||
function wpcf7_swv_available_rules() {
|
||||
$rules = array(
|
||||
'required' => 'WPCF7_SWV_RequiredRule',
|
||||
'requiredfile' => 'WPCF7_SWV_RequiredFileRule',
|
||||
'email' => 'WPCF7_SWV_EmailRule',
|
||||
'url' => 'WPCF7_SWV_URLRule',
|
||||
'tel' => 'WPCF7_SWV_TelRule',
|
||||
'number' => 'WPCF7_SWV_NumberRule',
|
||||
'date' => 'WPCF7_SWV_DateRule',
|
||||
'time' => 'WPCF7_SWV_TimeRule',
|
||||
'file' => 'WPCF7_SWV_FileRule',
|
||||
'enum' => 'WPCF7_SWV_EnumRule',
|
||||
'dayofweek' => 'WPCF7_SWV_DayofweekRule',
|
||||
'minitems' => 'WPCF7_SWV_MinItemsRule',
|
||||
'maxitems' => 'WPCF7_SWV_MaxItemsRule',
|
||||
'minlength' => 'WPCF7_SWV_MinLengthRule',
|
||||
'maxlength' => 'WPCF7_SWV_MaxLengthRule',
|
||||
'minnumber' => 'WPCF7_SWV_MinNumberRule',
|
||||
'maxnumber' => 'WPCF7_SWV_MaxNumberRule',
|
||||
'mindate' => 'WPCF7_SWV_MinDateRule',
|
||||
'maxdate' => 'WPCF7_SWV_MaxDateRule',
|
||||
'minfilesize' => 'WPCF7_SWV_MinFileSizeRule',
|
||||
'maxfilesize' => 'WPCF7_SWV_MaxFileSizeRule',
|
||||
'required' => 'Contactable\SWV\RequiredRule',
|
||||
'requiredfile' => 'Contactable\SWV\RequiredFileRule',
|
||||
'email' => 'Contactable\SWV\EmailRule',
|
||||
'url' => 'Contactable\SWV\URLRule',
|
||||
'tel' => 'Contactable\SWV\TelRule',
|
||||
'number' => 'Contactable\SWV\NumberRule',
|
||||
'date' => 'Contactable\SWV\DateRule',
|
||||
'time' => 'Contactable\SWV\TimeRule',
|
||||
'file' => 'Contactable\SWV\FileRule',
|
||||
'enum' => 'Contactable\SWV\EnumRule',
|
||||
'dayofweek' => 'Contactable\SWV\DayofweekRule',
|
||||
'minitems' => 'Contactable\SWV\MinItemsRule',
|
||||
'maxitems' => 'Contactable\SWV\MaxItemsRule',
|
||||
'minlength' => 'Contactable\SWV\MinLengthRule',
|
||||
'maxlength' => 'Contactable\SWV\MaxLengthRule',
|
||||
'minnumber' => 'Contactable\SWV\MinNumberRule',
|
||||
'maxnumber' => 'Contactable\SWV\MaxNumberRule',
|
||||
'mindate' => 'Contactable\SWV\MinDateRule',
|
||||
'maxdate' => 'Contactable\SWV\MaxDateRule',
|
||||
'minfilesize' => 'Contactable\SWV\MinFileSizeRule',
|
||||
'maxfilesize' => 'Contactable\SWV\MaxFileSizeRule',
|
||||
'all' => 'Contactable\SWV\AllRule',
|
||||
'any' => 'Contactable\SWV\AnyRule',
|
||||
);
|
||||
|
||||
return apply_filters( 'wpcf7_swv_available_rules', $rules );
|
||||
@@ -49,7 +52,7 @@ function wpcf7_swv_load_rules() {
|
||||
|
||||
foreach ( array_keys( $rules ) as $rule ) {
|
||||
$file = sprintf( '%s.php', $rule );
|
||||
$path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/rules', $file );
|
||||
$path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/php/rules', $file );
|
||||
|
||||
if ( file_exists( $path ) ) {
|
||||
include_once $path;
|
||||
@@ -63,7 +66,7 @@ function wpcf7_swv_load_rules() {
|
||||
*
|
||||
* @param string $rule_name Rule name.
|
||||
* @param string|array $properties Optional. Rule properties.
|
||||
* @return WPCF7_SWV_Rule|null The rule object, or null if it failed.
|
||||
* @return Rule|null The rule object, or null if it failed.
|
||||
*/
|
||||
function wpcf7_swv_create_rule( $rule_name, $properties = '' ) {
|
||||
$rules = wpcf7_swv_available_rules();
|
||||
@@ -124,172 +127,38 @@ function wpcf7_swv_get_meta_schema() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The base class of SWV rules.
|
||||
*/
|
||||
abstract class WPCF7_SWV_Rule {
|
||||
|
||||
protected $properties = array();
|
||||
|
||||
public function __construct( $properties = '' ) {
|
||||
$this->properties = wp_parse_args( $properties, array() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this rule matches the given context.
|
||||
*
|
||||
* @param array $context Context.
|
||||
*/
|
||||
public function matches( $context ) {
|
||||
$field = $this->get_property( 'field' );
|
||||
|
||||
if ( ! empty( $context['field'] ) ) {
|
||||
if ( $field and ! in_array( $field, (array) $context['field'], true ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates with this rule's logic.
|
||||
*
|
||||
* @param array $context Context.
|
||||
*/
|
||||
public function validate( $context ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the properties to an array.
|
||||
*
|
||||
* @return array Array of properties.
|
||||
*/
|
||||
public function to_array() {
|
||||
$properties = (array) $this->properties;
|
||||
|
||||
if ( defined( 'static::rule_name' ) and static::rule_name ) {
|
||||
$properties = array( 'rule' => static::rule_name ) + $properties;
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the property value specified by the given property name.
|
||||
*
|
||||
* @param string $name Property name.
|
||||
* @return mixed Property value.
|
||||
*/
|
||||
public function get_property( $name ) {
|
||||
if ( isset( $this->properties[$name] ) ) {
|
||||
return $this->properties[$name];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The base class of SWV composite rules.
|
||||
*/
|
||||
abstract class WPCF7_SWV_CompositeRule extends WPCF7_SWV_Rule {
|
||||
|
||||
protected $rules = array();
|
||||
|
||||
|
||||
/**
|
||||
* Adds a sub-rule to this composite rule.
|
||||
*
|
||||
* @param WPCF7_SWV_Rule $rule Sub-rule to be added.
|
||||
*/
|
||||
public function add_rule( $rule ) {
|
||||
if ( $rule instanceof WPCF7_SWV_Rule ) {
|
||||
$this->rules[] = $rule;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an iterator of sub-rules.
|
||||
*/
|
||||
public function rules() {
|
||||
foreach ( $this->rules as $rule ) {
|
||||
yield $rule;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this rule matches the given context.
|
||||
*
|
||||
* @param array $context Context.
|
||||
*/
|
||||
public function matches( $context ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates with this rule's logic.
|
||||
*
|
||||
* @param array $context Context.
|
||||
*/
|
||||
public function validate( $context ) {
|
||||
foreach ( $this->rules() as $rule ) {
|
||||
if ( $rule->matches( $context ) ) {
|
||||
$result = $rule->validate( $context );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the properties to an array.
|
||||
*
|
||||
* @return array Array of properties.
|
||||
*/
|
||||
public function to_array() {
|
||||
$rules_arrays = array_map(
|
||||
static function ( $rule ) {
|
||||
return $rule->to_array();
|
||||
},
|
||||
$this->rules
|
||||
);
|
||||
|
||||
return array_merge(
|
||||
parent::to_array(),
|
||||
array(
|
||||
'rules' => $rules_arrays,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The schema class as a composite rule.
|
||||
*/
|
||||
class WPCF7_SWV_Schema extends WPCF7_SWV_CompositeRule {
|
||||
class WPCF7_SWV_Schema extends \Contactable\SWV\CompositeRule {
|
||||
|
||||
const version = 'Contact Form 7 SWV Schema 2023-07';
|
||||
/**
|
||||
* The human-readable version of the schema.
|
||||
*/
|
||||
const version = 'Contact Form 7 SWV Schema 2024-02';
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct( $properties = '' ) {
|
||||
$this->properties = wp_parse_args( $properties, array(
|
||||
'version' => self::version,
|
||||
) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates with this schema.
|
||||
*
|
||||
* @param array $context Context.
|
||||
*/
|
||||
public function validate( $context ) {
|
||||
foreach ( $this->rules() as $rule ) {
|
||||
if ( $rule->matches( $context ) ) {
|
||||
yield $rule->validate( $context );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user