rebase from live enviornment

This commit is contained in:
Rachit Bhargava
2024-01-09 22:14:20 -05:00
parent ff0b49a046
commit 3a22fcaa4a
15968 changed files with 2344674 additions and 45234 deletions

View File

@@ -0,0 +1,6 @@
<?php
return array(
'dependencies' => array(),
'version' => WPCF7_VERSION,
);

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_DateRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,48 @@
<?php
class WPCF7_SWV_DayofweekRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_dayofweek',
$this->get_property( 'error' )
);
}
}
}
return true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_EmailRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,43 @@
<?php
class WPCF7_SWV_EnumRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 );
foreach ( $input as $i ) {
if ( ! in_array( $i, $acceptable_values, true ) ) {
return new WP_Error( 'wpcf7_invalid_enum',
$this->get_property( 'error' )
);
}
}
return true;
}
}

View File

@@ -0,0 +1,63 @@
<?php
class WPCF7_SWV_FileRule extends WPCF7_SWV_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 = isset( $_FILES[$field]['name'] ) ? $_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 new WP_Error( 'wpcf7_invalid_file',
$this->get_property( '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 true;
}
}

View File

@@ -0,0 +1,42 @@
<?php
class WPCF7_SWV_MaxDateRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_maxdate',
$this->get_property( 'error' )
);
}
}
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
class WPCF7_SWV_MaxFileSizeRule extends WPCF7_SWV_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 = isset( $_FILES[$field]['size'] ) ? $_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 new WP_Error( 'wpcf7_invalid_maxfilesize',
$this->get_property( 'error' )
);
}
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
class WPCF7_SWV_MaxItemsRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_maxitems',
$this->get_property( 'error' )
);
}
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
class WPCF7_SWV_MaxLengthRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_maxlength',
$this->get_property( 'error' )
);
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
class WPCF7_SWV_MaxNumberRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_maxnumber',
$this->get_property( 'error' )
);
}
}
return true;
}
}

View File

@@ -0,0 +1,42 @@
<?php
class WPCF7_SWV_MinDateRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_mindate',
$this->get_property( 'error' )
);
}
}
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
class WPCF7_SWV_MinFileSizeRule extends WPCF7_SWV_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 = isset( $_FILES[$field]['size'] ) ? $_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 new WP_Error( 'wpcf7_invalid_minfilesize',
$this->get_property( 'error' )
);
}
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
class WPCF7_SWV_MinItemsRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_minitems',
$this->get_property( 'error' )
);
}
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
class WPCF7_SWV_MinLengthRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_minlength',
$this->get_property( 'error' )
);
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
class WPCF7_SWV_MinNumberRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 new WP_Error( 'wpcf7_invalid_minnumber',
$this->get_property( 'error' )
);
}
}
return true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_NumberRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_RequiredRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,37 @@
<?php
class WPCF7_SWV_RequiredFileRule extends WPCF7_SWV_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 = isset( $_FILES[$field]['tmp_name'] )
? $_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 true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_TelRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_TimeRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
class WPCF7_SWV_URLRule extends WPCF7_SWV_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 ) {
$field = $this->get_property( 'field' );
$input = isset( $_POST[$field] ) ? $_POST[$field] : '';
$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 true;
}
}

View File

@@ -0,0 +1,57 @@
<?php
trait WPCF7_SWV_SchemaHolder {
protected $schema;
/**
* Retrieves SWV schema for this holder object (contact form).
*
* @return WPCF7_SWV_Schema The schema object.
*/
public function get_schema() {
if ( isset( $this->schema ) ) {
return $this->schema;
}
$schema = new WPCF7_SWV_Schema( array(
'locale' => isset( $this->locale ) ? $this->locale : '',
) );
do_action( 'wpcf7_swv_create_schema', $schema, $this );
return $this->schema = $schema;
}
/**
* 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;
}
if ( $rule instanceof WPCF7_SWV_CompositeRule ) {
foreach ( $rule->rules() as $child_rule ) {
call_user_func( $callback, $child_rule );
}
} else {
$field = $rule->get_property( 'field' );
if ( $validity->is_valid( $field ) ) {
$result = $rule->validate( $context );
if ( is_wp_error( $result ) ) {
$validity->invalidate( $field, $result );
}
}
}
};
call_user_func( $callback, $this->get_schema() );
}
}

View File

@@ -0,0 +1,26 @@
<?php
add_action(
'wp_enqueue_scripts',
static function () {
$assets = array();
$asset_file = wpcf7_plugin_path( 'includes/swv/js/index.asset.php' );
if ( file_exists( $asset_file ) ) {
$assets = include( $asset_file );
}
$assets = wp_parse_args( $assets, array(
'dependencies' => array(),
'version' => WPCF7_VERSION,
) );
wp_register_script( 'swv',
wpcf7_plugin_url( 'includes/swv/js/index.js' ),
$assets['dependencies'],
$assets['version'],
true
);
},
10, 0
);

View File

@@ -0,0 +1,295 @@
<?php
/**
* Schema-Woven Validation API
*/
require_once WPCF7_PLUGIN_DIR . '/includes/swv/schema-holder.php';
require_once WPCF7_PLUGIN_DIR . '/includes/swv/script-loader.php';
/**
* Returns an associative array of SWV rules.
*/
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',
);
return apply_filters( 'wpcf7_swv_available_rules', $rules );
}
add_action( 'wpcf7_init', 'wpcf7_swv_load_rules', 10, 0 );
/**
* Loads SWV fules.
*/
function wpcf7_swv_load_rules() {
$rules = wpcf7_swv_available_rules();
foreach ( array_keys( $rules ) as $rule ) {
$file = sprintf( '%s.php', $rule );
$path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/rules', $file );
if ( file_exists( $path ) ) {
include_once $path;
}
}
}
/**
* Creates an SWV rule object.
*
* @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.
*/
function wpcf7_swv_create_rule( $rule_name, $properties = '' ) {
$rules = wpcf7_swv_available_rules();
if ( isset( $rules[$rule_name] ) ) {
return new $rules[$rule_name]( $properties );
}
}
/**
* Returns an associative array of JSON Schema for Contact Form 7 SWV.
*/
function wpcf7_swv_get_meta_schema() {
return array(
'$schema' => 'https://json-schema.org/draft/2020-12/schema',
'title' => 'Contact Form 7 SWV',
'description' => 'Contact Form 7 SWV meta-schema',
'type' => 'object',
'properties' => array(
'version' => array(
'type' => 'string',
),
'locale' => array(
'type' => 'string',
),
'rules' => array(
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'rule' => array(
'type' => 'string',
'enum' => array_keys( wpcf7_swv_available_rules() ),
),
'field' => array(
'type' => 'string',
'pattern' => '^[A-Za-z][-A-Za-z0-9_:]*$',
),
'error' => array(
'type' => 'string',
),
'accept' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'threshold' => array(
'type' => 'string',
),
),
'required' => array( 'rule' ),
),
),
),
);
}
/**
* 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 {
const version = 'Contact Form 7 SWV Schema 2023-07';
public function __construct( $properties = '' ) {
$this->properties = wp_parse_args( $properties, array(
'version' => self::version,
) );
}
}