Plugin Updates

This commit is contained in:
Tony Volpe
2024-03-19 15:33:31 +00:00
parent ff5b56dc44
commit 3a70a6e4bf
317 changed files with 8178 additions and 2933 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,191 @@
<?php
namespace Contactable\SWV;
use WP_Error;
/**
* The base class of SWV rules.
*/
abstract class 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];
}
}
/**
* Returns the default user input value from $_POST.
*
* @return mixed Default user input value.
*/
public function get_default_input() {
$field = $this->get_property( 'field' );
if ( isset( $_POST[$field] ) ) {
return wp_unslash( $_POST[$field] );
}
return '';
}
/**
* Creates an error object. Returns false if the error property is omitted.
*/
protected function create_error() {
$error_code = defined( 'static::rule_name' )
? sprintf( 'swv_%s', static::rule_name )
: 'swv';
return new WP_Error(
$error_code,
(string) $this->get_property( 'error' ),
$this
);
}
}
/**
* The base class of SWV composite rules.
*/
abstract class CompositeRule extends Rule {
protected $rules = array();
/**
* Adds a sub-rule to this composite rule.
*
* @param Rule $rule Sub-rule to be added.
*/
public function add_rule( $rule ) {
if ( $rule instanceof 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,
)
);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Contactable\SWV;
class AllRule extends CompositeRule {
const rule_name = 'all';
public function matches( $context ) {
if ( false === parent::matches( $context ) ) {
return false;
}
return true;
}
public function validate( $context ) {
foreach ( $this->rules() as $rule ) {
if ( $rule->matches( $context ) ) {
$result = $rule->validate( $context );
if ( is_wp_error( $result ) ) {
if ( $result->get_error_message() ) {
return $result;
} else {
return $this->create_error();
}
}
}
}
return true;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Contactable\SWV;
class AnyRule extends CompositeRule {
const rule_name = 'any';
public function matches( $context ) {
if ( false === parent::matches( $context ) ) {
return false;
}
return true;
}
public function validate( $context ) {
foreach ( $this->rules() as $rule ) {
if ( $rule->matches( $context ) ) {
$result = $rule->validate( $context );
if ( ! is_wp_error( $result ) ) {
return true;
}
}
}
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_DateRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class DateRule extends Rule {
const rule_name = 'date';
@@ -17,16 +19,13 @@ class WPCF7_SWV_DateRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_date( $i ) ) {
return new WP_Error( 'wpcf7_invalid_date',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_DayofweekRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class DayofweekRule extends Rule {
const rule_name = 'dayofweek';
@@ -17,10 +19,7 @@ class WPCF7_SWV_DayofweekRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -35,9 +34,7 @@ class WPCF7_SWV_DayofweekRule extends WPCF7_SWV_Rule {
$dow = (int) $datetime->format( 'N' );
if ( ! in_array( $dow, $acceptable_values, true ) ) {
return new WP_Error( 'wpcf7_invalid_dayofweek',
$this->get_property( 'error' )
);
return $this->create_error();
}
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_EmailRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class EmailRule extends Rule {
const rule_name = 'email';
@@ -17,16 +19,13 @@ class WPCF7_SWV_EmailRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_email( $i ) ) {
return new WP_Error( 'wpcf7_invalid_email',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_EnumRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class EnumRule extends Rule {
const rule_name = 'enum';
@@ -17,23 +19,23 @@ class WPCF7_SWV_EnumRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
$acceptable_values = (array) $this->get_property( 'accept' );
$acceptable_values = array_map( 'strval', $acceptable_values );
$acceptable_values = array_filter( $acceptable_values );
$acceptable_values = array_unique( $acceptable_values );
$acceptable_values = array_filter( $acceptable_values,
static function ( $val ) {
return '' !== $val;
}
);
foreach ( $input as $i ) {
if ( ! in_array( $i, $acceptable_values, true ) ) {
return new WP_Error( 'wpcf7_invalid_enum',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_FileRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class FileRule extends Rule {
const rule_name = 'file';
@@ -18,7 +20,7 @@ class WPCF7_SWV_FileRule extends WPCF7_SWV_Rule {
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_FILES[$field]['name'] ) ? $_FILES[$field]['name'] : '';
$input = $_FILES[$field]['name'] ?? '';
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -43,17 +45,13 @@ class WPCF7_SWV_FileRule extends WPCF7_SWV_Rule {
$last_period_pos = strrpos( $i, '.' );
if ( false === $last_period_pos ) { // no period
return new WP_Error( 'wpcf7_invalid_file',
$this->get_property( 'error' )
);
return $this->create_error();
}
$suffix = strtolower( substr( $i, $last_period_pos ) );
if ( ! in_array( $suffix, $acceptable_filetypes, true ) ) {
return new WP_Error( 'wpcf7_invalid_file',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MaxDateRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MaxDateRule extends Rule {
const rule_name = 'maxdate';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MaxDateRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -30,9 +31,7 @@ class WPCF7_SWV_MaxDateRule extends WPCF7_SWV_Rule {
foreach ( $input as $i ) {
if ( wpcf7_is_date( $i ) and $threshold < $i ) {
return new WP_Error( 'wpcf7_invalid_maxdate',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MaxFileSizeRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MaxFileSizeRule extends Rule {
const rule_name = 'maxfilesize';
@@ -18,7 +20,7 @@ class WPCF7_SWV_MaxFileSizeRule extends WPCF7_SWV_Rule {
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_FILES[$field]['size'] ) ? $_FILES[$field]['size'] : '';
$input = $_FILES[$field]['size'] ?? '';
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -29,9 +31,7 @@ class WPCF7_SWV_MaxFileSizeRule extends WPCF7_SWV_Rule {
$threshold = $this->get_property( 'threshold' );
if ( $threshold < array_sum( $input ) ) {
return new WP_Error( 'wpcf7_invalid_maxfilesize',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MaxItemsRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MaxItemsRule extends Rule {
const rule_name = 'maxitems';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MaxItemsRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -29,9 +30,7 @@ class WPCF7_SWV_MaxItemsRule extends WPCF7_SWV_Rule {
}
if ( (int) $threshold < count( $input ) ) {
return new WP_Error( 'wpcf7_invalid_maxitems',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MaxLengthRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MaxLengthRule extends Rule {
const rule_name = 'maxlength';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MaxLengthRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -37,9 +38,7 @@ class WPCF7_SWV_MaxLengthRule extends WPCF7_SWV_Rule {
if ( $total <= $threshold ) {
return true;
} else {
return new WP_Error( 'wpcf7_invalid_maxlength',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MaxNumberRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MaxNumberRule extends Rule {
const rule_name = 'maxnumber';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MaxNumberRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -30,9 +31,7 @@ class WPCF7_SWV_MaxNumberRule extends WPCF7_SWV_Rule {
foreach ( $input as $i ) {
if ( wpcf7_is_number( $i ) and (float) $threshold < (float) $i ) {
return new WP_Error( 'wpcf7_invalid_maxnumber',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MinDateRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MinDateRule extends Rule {
const rule_name = 'mindate';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MinDateRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -30,9 +31,7 @@ class WPCF7_SWV_MinDateRule extends WPCF7_SWV_Rule {
foreach ( $input as $i ) {
if ( wpcf7_is_date( $i ) and $i < $threshold ) {
return new WP_Error( 'wpcf7_invalid_mindate',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MinFileSizeRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MinFileSizeRule extends Rule {
const rule_name = 'minfilesize';
@@ -18,7 +20,7 @@ class WPCF7_SWV_MinFileSizeRule extends WPCF7_SWV_Rule {
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_FILES[$field]['size'] ) ? $_FILES[$field]['size'] : '';
$input = $_FILES[$field]['size'] ?? '';
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -29,9 +31,7 @@ class WPCF7_SWV_MinFileSizeRule extends WPCF7_SWV_Rule {
$threshold = $this->get_property( 'threshold' );
if ( array_sum( $input ) < $threshold ) {
return new WP_Error( 'wpcf7_invalid_minfilesize',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MinItemsRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MinItemsRule extends Rule {
const rule_name = 'minitems';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MinItemsRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -29,9 +30,7 @@ class WPCF7_SWV_MinItemsRule extends WPCF7_SWV_Rule {
}
if ( count( $input ) < (int) $threshold ) {
return new WP_Error( 'wpcf7_invalid_minitems',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MinLengthRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MinLengthRule extends Rule {
const rule_name = 'minlength';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MinLengthRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -37,9 +38,7 @@ class WPCF7_SWV_MinLengthRule extends WPCF7_SWV_Rule {
if ( $threshold <= $total ) {
return true;
} else {
return new WP_Error( 'wpcf7_invalid_minlength',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_MinNumberRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class MinNumberRule extends Rule {
const rule_name = 'minnumber';
@@ -17,8 +19,7 @@ class WPCF7_SWV_MinNumberRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
@@ -30,9 +31,7 @@ class WPCF7_SWV_MinNumberRule extends WPCF7_SWV_Rule {
foreach ( $input as $i ) {
if ( wpcf7_is_number( $i ) and (float) $i < (float) $threshold ) {
return new WP_Error( 'wpcf7_invalid_minnumber',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_NumberRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class NumberRule extends Rule {
const rule_name = 'number';
@@ -17,16 +19,13 @@ class WPCF7_SWV_NumberRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_number( $i ) ) {
return new WP_Error( 'wpcf7_invalid_number',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_RequiredRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class RequiredRule extends Rule {
const rule_name = 'required';
@@ -17,17 +19,12 @@ class WPCF7_SWV_RequiredRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
if ( empty( $input ) ) {
return new WP_Error( 'wpcf7_invalid_required',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_RequiredFileRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class RequiredFileRule extends Rule {
const rule_name = 'requiredfile';
@@ -18,17 +20,12 @@ class WPCF7_SWV_RequiredFileRule extends WPCF7_SWV_Rule {
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_FILES[$field]['tmp_name'] )
? $_FILES[$field]['tmp_name'] : '';
$input = $_FILES[$field]['tmp_name'] ?? '';
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
if ( empty( $input ) ) {
return new WP_Error( 'wpcf7_invalid_requiredfile',
$this->get_property( 'error' )
);
return $this->create_error();
}
return true;

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_TelRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class TelRule extends Rule {
const rule_name = 'tel';
@@ -17,16 +19,13 @@ class WPCF7_SWV_TelRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_tel( $i ) ) {
return new WP_Error( 'wpcf7_invalid_tel',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_TimeRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class TimeRule extends Rule {
const rule_name = 'time';
@@ -17,16 +19,13 @@ class WPCF7_SWV_TimeRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_time( $i ) ) {
return new WP_Error( 'wpcf7_invalid_time',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -1,6 +1,8 @@
<?php
class WPCF7_SWV_URLRule extends WPCF7_SWV_Rule {
namespace Contactable\SWV;
class URLRule extends Rule {
const rule_name = 'url';
@@ -17,16 +19,13 @@ class WPCF7_SWV_URLRule extends WPCF7_SWV_Rule {
}
public function validate( $context ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$input = $this->get_default_input();
$input = wpcf7_array_flatten( $input );
$input = wpcf7_exclude_blank( $input );
foreach ( $input as $i ) {
if ( ! wpcf7_is_url( $i ) ) {
return new WP_Error( 'wpcf7_invalid_url',
$this->get_property( 'error' )
);
return $this->create_error();
}
}

View File

@@ -29,29 +29,18 @@ trait WPCF7_SWV_SchemaHolder {
* Validates form inputs based on the schema and given context.
*/
public function validate_schema( $context, WPCF7_Validation $validity ) {
$callback = static function ( $rule ) use ( &$callback, $context, $validity ) {
if ( ! $rule->matches( $context ) ) {
return;
}
$schema = $this->get_schema();
if ( $rule instanceof WPCF7_SWV_CompositeRule ) {
foreach ( $rule->rules() as $child_rule ) {
call_user_func( $callback, $child_rule );
}
} else {
foreach ( $schema->validate( $context ) as $result ) {
if ( is_wp_error( $result ) ) {
$rule = $result->get_error_data();
$field = $rule->get_property( 'field' );
if ( $validity->is_valid( $field ) ) {
$result = $rule->validate( $context );
if ( is_wp_error( $result ) ) {
$validity->invalidate( $field, $result );
}
if ( isset( $field ) and $validity->is_valid( $field ) ) {
$validity->invalidate( $field, $result );
}
}
};
call_user_func( $callback, $this->get_schema() );
}
}
}

View File

@@ -19,7 +19,7 @@ add_action(
wpcf7_plugin_url( 'includes/swv/js/index.js' ),
$assets['dependencies'],
$assets['version'],
true
array( 'in_footer' => true )
);
},
10, 0

View File

@@ -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 );
}
}
}
}