Plugin Updates
This commit is contained in:
@@ -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,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class DateRule extends Rule {
|
||||
|
||||
const rule_name = 'date';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class DayofweekRule extends Rule {
|
||||
|
||||
const rule_name = 'dayofweek';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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( 'intval', $acceptable_values );
|
||||
$acceptable_values = array_filter( $acceptable_values );
|
||||
$acceptable_values = array_unique( $acceptable_values );
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
if ( wpcf7_is_date( $i ) ) {
|
||||
$datetime = date_create_immutable( $i, wp_timezone() );
|
||||
$dow = (int) $datetime->format( 'N' );
|
||||
|
||||
if ( ! in_array( $dow, $acceptable_values, true ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class EmailRule extends Rule {
|
||||
|
||||
const rule_name = 'email';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class EnumRule extends Rule {
|
||||
|
||||
const rule_name = 'enum';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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_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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class FileRule extends Rule {
|
||||
|
||||
const rule_name = 'file';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['file'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$field = $this->get_property( 'field' );
|
||||
$input = $_FILES[$field]['name'] ?? '';
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$acceptable_filetypes = array();
|
||||
|
||||
foreach ( (array) $this->get_property( 'accept' ) as $accept ) {
|
||||
if ( preg_match( '/^\.[a-z0-9]+$/i', $accept ) ) {
|
||||
$acceptable_filetypes[] = strtolower( $accept );
|
||||
} else {
|
||||
foreach ( wpcf7_convert_mime_to_ext( $accept ) as $ext ) {
|
||||
$acceptable_filetypes[] = sprintf(
|
||||
'.%s',
|
||||
strtolower( trim( $ext, ' .' ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$acceptable_filetypes = array_unique( $acceptable_filetypes );
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
$last_period_pos = strrpos( $i, '.' );
|
||||
|
||||
if ( false === $last_period_pos ) { // no period
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
$suffix = strtolower( substr( $i, $last_period_pos ) );
|
||||
|
||||
if ( ! in_array( $suffix, $acceptable_filetypes, true ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MaxDateRule extends Rule {
|
||||
|
||||
const rule_name = 'maxdate';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_date( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
if ( wpcf7_is_date( $i ) and $threshold < $i ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MaxFileSizeRule extends Rule {
|
||||
|
||||
const rule_name = 'maxfilesize';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['file'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$field = $this->get_property( 'field' );
|
||||
$input = $_FILES[$field]['size'] ?? '';
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( $threshold < array_sum( $input ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MaxItemsRule extends Rule {
|
||||
|
||||
const rule_name = 'maxitems';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_number( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( (int) $threshold < count( $input ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MaxLengthRule extends Rule {
|
||||
|
||||
const rule_name = 'maxlength';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$total = 0;
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
$total += wpcf7_count_code_units( $i );
|
||||
}
|
||||
|
||||
$threshold = (int) $this->get_property( 'threshold' );
|
||||
|
||||
if ( $total <= $threshold ) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MaxNumberRule extends Rule {
|
||||
|
||||
const rule_name = 'maxnumber';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_number( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
if ( wpcf7_is_number( $i ) and (float) $threshold < (float) $i ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MinDateRule extends Rule {
|
||||
|
||||
const rule_name = 'mindate';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_date( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
if ( wpcf7_is_date( $i ) and $i < $threshold ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MinFileSizeRule extends Rule {
|
||||
|
||||
const rule_name = 'minfilesize';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['file'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$field = $this->get_property( 'field' );
|
||||
$input = $_FILES[$field]['size'] ?? '';
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( array_sum( $input ) < $threshold ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MinItemsRule extends Rule {
|
||||
|
||||
const rule_name = 'minitems';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_number( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( count( $input ) < (int) $threshold ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MinLengthRule extends Rule {
|
||||
|
||||
const rule_name = 'minlength';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$total = 0;
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
$total += wpcf7_count_code_units( $i );
|
||||
}
|
||||
|
||||
$threshold = (int) $this->get_property( 'threshold' );
|
||||
|
||||
if ( $threshold <= $total ) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class MinNumberRule extends Rule {
|
||||
|
||||
const rule_name = 'minnumber';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
$threshold = $this->get_property( 'threshold' );
|
||||
|
||||
if ( ! wpcf7_is_number( $threshold ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $input as $i ) {
|
||||
if ( wpcf7_is_number( $i ) and (float) $i < (float) $threshold ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class NumberRule extends Rule {
|
||||
|
||||
const rule_name = 'number';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class RequiredRule extends Rule {
|
||||
|
||||
const rule_name = 'required';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$input = $this->get_default_input();
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class RequiredFileRule extends Rule {
|
||||
|
||||
const rule_name = 'requiredfile';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['file'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$field = $this->get_property( 'field' );
|
||||
$input = $_FILES[$field]['tmp_name'] ?? '';
|
||||
$input = wpcf7_array_flatten( $input );
|
||||
$input = wpcf7_exclude_blank( $input );
|
||||
|
||||
if ( empty( $input ) ) {
|
||||
return $this->create_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class TelRule extends Rule {
|
||||
|
||||
const rule_name = 'tel';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class TimeRule extends Rule {
|
||||
|
||||
const rule_name = 'time';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Contactable\SWV;
|
||||
|
||||
class URLRule extends Rule {
|
||||
|
||||
const rule_name = 'url';
|
||||
|
||||
public function matches( $context ) {
|
||||
if ( false === parent::matches( $context ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $context['text'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate( $context ) {
|
||||
$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 $this->create_error();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user