Plugin Updates
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 2,
|
||||
"apiVersion": 3,
|
||||
"name": "contact-form-7/contact-form-selector",
|
||||
"title": "Contact Form 7",
|
||||
"category": "widgets",
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -39,74 +39,49 @@ trait WPCF7_ConfigValidator_Mail {
|
||||
$example_text = 'example';
|
||||
$example_blank = '';
|
||||
|
||||
$form_tags = $this->contact_form->scan_form_tags(
|
||||
array( 'name' => $field_name )
|
||||
);
|
||||
// for back-compat
|
||||
$field_name = preg_replace( '/^wpcf7\./', '_', $field_name );
|
||||
|
||||
if ( $form_tags ) {
|
||||
$form_tag = new WPCF7_FormTag( $form_tags[0] );
|
||||
if ( '_site_admin_email' === $field_name ) {
|
||||
return get_bloginfo( 'admin_email', 'raw' );
|
||||
|
||||
$is_required = $form_tag->is_required() || 'radio' === $form_tag->type;
|
||||
} elseif ( '_user_agent' === $field_name ) {
|
||||
return $example_text;
|
||||
|
||||
if ( ! $is_required ) {
|
||||
return $example_blank;
|
||||
}
|
||||
} elseif ( '_user_email' === $field_name ) {
|
||||
return $this->contact_form->is_true( 'subscribers_only' )
|
||||
? $example_email
|
||||
: $example_blank;
|
||||
|
||||
if ( wpcf7_form_tag_supports( $form_tag->type, 'selectable-values' ) ) {
|
||||
if ( $form_tag->pipes instanceof WPCF7_Pipes ) {
|
||||
if ( $mail_tag->get_option( 'do_not_heat' ) ) {
|
||||
$before_pipes = $form_tag->pipes->collect_befores();
|
||||
$last_item = array_pop( $before_pipes );
|
||||
} else {
|
||||
$after_pipes = $form_tag->pipes->collect_afters();
|
||||
$last_item = array_pop( $after_pipes );
|
||||
}
|
||||
} else {
|
||||
$last_item = array_pop( $form_tag->values );
|
||||
}
|
||||
} elseif ( str_starts_with( $field_name, '_user_' ) ) {
|
||||
return $this->contact_form->is_true( 'subscribers_only' )
|
||||
? $example_text
|
||||
: $example_blank;
|
||||
|
||||
if ( $last_item and wpcf7_is_mailbox_list( $last_item ) ) {
|
||||
return $example_email;
|
||||
} else {
|
||||
return $example_text;
|
||||
}
|
||||
}
|
||||
} elseif ( str_starts_with( $field_name, '_' ) ) {
|
||||
return str_ends_with( $field_name, '_email' )
|
||||
? $example_email
|
||||
: $example_text;
|
||||
|
||||
if ( 'email' === $form_tag->basetype ) {
|
||||
return $example_email;
|
||||
} else {
|
||||
return $example_text;
|
||||
}
|
||||
|
||||
} else { // maybe special mail tag
|
||||
// for back-compat
|
||||
$field_name = preg_replace( '/^wpcf7\./', '_', $field_name );
|
||||
|
||||
if ( '_site_admin_email' === $field_name ) {
|
||||
return get_bloginfo( 'admin_email', 'raw' );
|
||||
|
||||
} elseif ( '_user_agent' === $field_name ) {
|
||||
return $example_text;
|
||||
|
||||
} elseif ( '_user_email' === $field_name ) {
|
||||
return $this->contact_form->is_true( 'subscribers_only' )
|
||||
? $example_email
|
||||
: $example_blank;
|
||||
|
||||
} elseif ( str_starts_with( $field_name, '_user_' ) ) {
|
||||
return $this->contact_form->is_true( 'subscribers_only' )
|
||||
? $example_text
|
||||
: $example_blank;
|
||||
|
||||
} elseif ( str_starts_with( $field_name, '_' ) ) {
|
||||
return str_ends_with( $field_name, '_email' )
|
||||
? $example_email
|
||||
: $example_text;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $tag;
|
||||
static $opcalcset = array();
|
||||
|
||||
if ( ! isset( $opcalcset[$this->contact_form->id()] ) ) {
|
||||
$opcalcset[$this->contact_form->id()] =
|
||||
new WPCF7_MailTag_OutputCalculator( $this->contact_form );
|
||||
}
|
||||
|
||||
$opcalc = $opcalcset[$this->contact_form->id()];
|
||||
$op = $opcalc->calc_output( $mail_tag );
|
||||
|
||||
if ( WPCF7_MailTag_OutputCalculator::email === $op ) {
|
||||
return $example_email;
|
||||
} elseif ( ! ( WPCF7_MailTag_OutputCalculator::blank & $op ) ) {
|
||||
return $example_text;
|
||||
} else {
|
||||
return $example_blank;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
class WPCF7_ContactForm {
|
||||
|
||||
use WPCF7_SWV_SchemaHolder;
|
||||
use WPCF7_PipesHolder;
|
||||
|
||||
const post_type = 'wpcf7_contact_form';
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ add_action(
|
||||
array( 'swv' )
|
||||
),
|
||||
$assets['version'],
|
||||
true
|
||||
array( 'in_footer' => true )
|
||||
);
|
||||
|
||||
wp_register_script(
|
||||
@@ -62,7 +62,7 @@ add_action(
|
||||
wpcf7_plugin_url( 'includes/js/html5-fallback.js' ),
|
||||
array( 'jquery-ui-datepicker' ),
|
||||
WPCF7_VERSION,
|
||||
true
|
||||
array( 'in_footer' => true )
|
||||
);
|
||||
|
||||
if ( wpcf7_load_js() ) {
|
||||
|
||||
@@ -34,13 +34,15 @@ function wpcf7_unship_uploaded_file( $file, $args = '' ) {
|
||||
}
|
||||
|
||||
if ( isset( $args['schema'] ) and isset( $args['name'] ) ) {
|
||||
$result = $args['schema']->validate( array(
|
||||
$context = array(
|
||||
'file' => true,
|
||||
'field' => $args['name'],
|
||||
) );
|
||||
);
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
foreach ( $args['schema']->validate( $context ) as $result ) {
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -141,6 +141,8 @@ class WPCF7_Integration {
|
||||
<h2 class="title"><?php echo esc_html( $service->get_title() ); ?></h2>
|
||||
<div class="infobox">
|
||||
<?php echo esc_html( implode( ', ', $cats ) ); ?>
|
||||
<br />
|
||||
<?php $service->link(); ?>
|
||||
</div>
|
||||
<br class="clear" />
|
||||
|
||||
@@ -247,8 +249,8 @@ class WPCF7_Service_OAuth2 extends WPCF7_Service {
|
||||
|
||||
|
||||
public function load( $action = '' ) {
|
||||
if ( 'auth_redirect' == $action ) {
|
||||
$code = isset( $_GET['code'] ) ? $_GET['code'] : '';
|
||||
if ( 'auth_redirect' === $action ) {
|
||||
$code = $_GET['code'] ?? '';
|
||||
|
||||
if ( $code ) {
|
||||
$this->request_token( $code );
|
||||
|
||||
File diff suppressed because one or more lines are too long
197
wp/wp-content/plugins/contact-form-7/includes/mail-tag.php
Normal file
197
wp/wp-content/plugins/contact-form-7/includes/mail-tag.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class that represents a mail-tag.
|
||||
*/
|
||||
class WPCF7_MailTag {
|
||||
|
||||
private $tag;
|
||||
private $tagname = '';
|
||||
private $name = '';
|
||||
private $options = array();
|
||||
private $values = array();
|
||||
private $form_tag = null;
|
||||
|
||||
|
||||
/**
|
||||
* The constructor method.
|
||||
*/
|
||||
public function __construct( $tag, $tagname, $values ) {
|
||||
$this->tag = $tag;
|
||||
$this->name = $this->tagname = $tagname;
|
||||
|
||||
$this->options = array(
|
||||
'do_not_heat' => false,
|
||||
'format' => '',
|
||||
);
|
||||
|
||||
if ( ! empty( $values ) ) {
|
||||
preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
|
||||
$this->values = wpcf7_strip_quote_deep( $matches[0] );
|
||||
}
|
||||
|
||||
if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
|
||||
$this->name = trim( $matches[1] );
|
||||
$this->options['do_not_heat'] = true;
|
||||
}
|
||||
|
||||
if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
|
||||
$this->name = trim( $matches[1] );
|
||||
$this->options['format'] = $this->values[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name part of this mail-tag.
|
||||
*/
|
||||
public function tag_name() {
|
||||
return $this->tagname;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the form field name corresponding to this mail-tag.
|
||||
*/
|
||||
public function field_name() {
|
||||
return strtr( $this->name, '.', '_' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the specified option.
|
||||
*/
|
||||
public function get_option( $option ) {
|
||||
return $this->options[$option];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the values part of this mail-tag.
|
||||
*/
|
||||
public function values() {
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the WPCF7_FormTag object that corresponds to this mail-tag.
|
||||
*/
|
||||
public function corresponding_form_tag() {
|
||||
if ( $this->form_tag instanceof WPCF7_FormTag ) {
|
||||
return $this->form_tag;
|
||||
}
|
||||
|
||||
if ( $submission = WPCF7_Submission::get_instance() ) {
|
||||
$contact_form = $submission->get_contact_form();
|
||||
$tags = $contact_form->scan_form_tags( array(
|
||||
'name' => $this->field_name(),
|
||||
'feature' => '! zero-controls-container',
|
||||
) );
|
||||
|
||||
if ( $tags ) {
|
||||
$this->form_tag = $tags[0];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->form_tag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
use Contactable\SWV;
|
||||
|
||||
/**
|
||||
* Mail-tag output calculator.
|
||||
*/
|
||||
class WPCF7_MailTag_OutputCalculator {
|
||||
|
||||
const email = 0b100;
|
||||
const text = 0b010;
|
||||
const blank = 0b001;
|
||||
|
||||
private $contact_form;
|
||||
|
||||
public function __construct( WPCF7_ContactForm $contact_form ) {
|
||||
$this->contact_form = $contact_form;
|
||||
}
|
||||
|
||||
public function calc_output( WPCF7_MailTag $mail_tag ) {
|
||||
return $this->calc_swv_result(
|
||||
$mail_tag,
|
||||
$this->contact_form->get_schema()
|
||||
);
|
||||
}
|
||||
|
||||
private function calc_swv_result( WPCF7_MailTag $mail_tag, SWV\Rule $rule ) {
|
||||
|
||||
if ( $rule instanceof SWV\AnyRule ) {
|
||||
$result = 0b000;
|
||||
|
||||
foreach ( $rule->rules() as $child_rule ) {
|
||||
$result |= $this->calc_swv_result( $mail_tag, $child_rule );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( $rule instanceof SWV\CompositeRule ) {
|
||||
$result = 0b111;
|
||||
|
||||
foreach ( $rule->rules() as $child_rule ) {
|
||||
$result &= $this->calc_swv_result( $mail_tag, $child_rule );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
$field_prop = $rule->get_property( 'field' );
|
||||
|
||||
if ( empty( $field_prop ) or $field_prop !== $mail_tag->field_name() ) {
|
||||
return self::email | self::text | self::blank;
|
||||
}
|
||||
|
||||
if ( $rule instanceof SWV\RequiredRule ) {
|
||||
return ~ self::blank;
|
||||
}
|
||||
|
||||
if ( $rule instanceof SWV\EmailRule ) {
|
||||
return self::email | self::blank;
|
||||
}
|
||||
|
||||
if ( $rule instanceof SWV\EnumRule ) {
|
||||
$acceptable_values = (array) $rule->get_property( 'accept' );
|
||||
$acceptable_values = array_map( 'strval', $acceptable_values );
|
||||
$acceptable_values = array_filter( $acceptable_values );
|
||||
$acceptable_values = array_unique( $acceptable_values );
|
||||
|
||||
if ( ! $mail_tag->get_option( 'do_not_heat' ) ) {
|
||||
$pipes = $this->contact_form->get_pipes(
|
||||
$mail_tag->field_name()
|
||||
);
|
||||
|
||||
$acceptable_values = array_map(
|
||||
static function ( $val ) use ( $pipes ) {
|
||||
return $pipes->do_pipe( $val );
|
||||
},
|
||||
$acceptable_values
|
||||
);
|
||||
}
|
||||
|
||||
$email_values = array_filter(
|
||||
$acceptable_values,
|
||||
'wpcf7_is_mailbox_list'
|
||||
);
|
||||
|
||||
if ( count( $email_values ) === count( $acceptable_values ) ) {
|
||||
return self::email | self::blank;
|
||||
} else {
|
||||
return self::email | self::text | self::blank;
|
||||
}
|
||||
}
|
||||
|
||||
return self::email | self::text | self::blank;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ class WPCF7_Mail {
|
||||
private $name = '';
|
||||
private $locale = '';
|
||||
private $template = array();
|
||||
private $component = '';
|
||||
private $use_html = false;
|
||||
private $exclude_blank = false;
|
||||
|
||||
@@ -36,6 +37,35 @@ class WPCF7_Mail {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the email template currently processed.
|
||||
*
|
||||
* Expected output: 'mail' or 'mail_2'
|
||||
*/
|
||||
public static function get_current_template_name() {
|
||||
$current = self::get_current();
|
||||
|
||||
if ( $current instanceof self ) {
|
||||
return $current->get_template_name();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the email template component currently processed.
|
||||
*
|
||||
* Expected output: 'recipient', 'sender', 'subject',
|
||||
* 'additional_headers', 'body', or 'attachments'
|
||||
*/
|
||||
public static function get_current_component_name() {
|
||||
$current = self::get_current();
|
||||
|
||||
if ( $current instanceof self ) {
|
||||
return $current->get_component_name();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Composes and sends email based on the specified template.
|
||||
*
|
||||
@@ -86,6 +116,22 @@ class WPCF7_Mail {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the email template. A wrapper method of name().
|
||||
*/
|
||||
public function get_template_name() {
|
||||
return $this->name();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the email template component currently processed.
|
||||
*/
|
||||
public function get_component_name() {
|
||||
return $this->component;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a component from the email template.
|
||||
*
|
||||
@@ -95,8 +141,10 @@ class WPCF7_Mail {
|
||||
* @return string The text representation of the email component.
|
||||
*/
|
||||
public function get( $component, $replace_tags = false ) {
|
||||
$use_html = ( $this->use_html && 'body' == $component );
|
||||
$exclude_blank = ( $this->exclude_blank && 'body' == $component );
|
||||
$this->component = $component;
|
||||
|
||||
$use_html = ( $this->use_html && 'body' === $component );
|
||||
$exclude_blank = ( $this->exclude_blank && 'body' === $component );
|
||||
|
||||
$template = $this->template;
|
||||
$component = isset( $template[$component] ) ? $template[$component] : '';
|
||||
@@ -127,6 +175,8 @@ class WPCF7_Mail {
|
||||
}
|
||||
}
|
||||
|
||||
$this->component = '';
|
||||
|
||||
return $component;
|
||||
}
|
||||
|
||||
@@ -502,9 +552,7 @@ class WPCF7_MailTaggedText {
|
||||
: null;
|
||||
|
||||
if ( $mail_tag->get_option( 'do_not_heat' ) ) {
|
||||
$submitted = isset( $_POST[$field_name] )
|
||||
? wp_unslash( $_POST[$field_name] )
|
||||
: '';
|
||||
$submitted = wp_unslash( $_POST[$field_name] ?? '' );
|
||||
}
|
||||
|
||||
$replaced = $submitted;
|
||||
@@ -514,8 +562,12 @@ class WPCF7_MailTaggedText {
|
||||
$replaced = $this->format( $replaced, $format );
|
||||
}
|
||||
|
||||
$separator = ( 'body' === WPCF7_Mail::get_current_component_name() )
|
||||
? wp_get_list_item_separator()
|
||||
: ', ';
|
||||
|
||||
$replaced = wpcf7_flat_join( $replaced, array(
|
||||
'separator' => wp_get_list_item_separator(),
|
||||
'separator' => $separator,
|
||||
) );
|
||||
|
||||
if ( $html ) {
|
||||
@@ -578,103 +630,3 @@ class WPCF7_MailTaggedText {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class that represents a mail-tag.
|
||||
*/
|
||||
class WPCF7_MailTag {
|
||||
|
||||
private $tag;
|
||||
private $tagname = '';
|
||||
private $name = '';
|
||||
private $options = array();
|
||||
private $values = array();
|
||||
private $form_tag = null;
|
||||
|
||||
|
||||
/**
|
||||
* The constructor method.
|
||||
*/
|
||||
public function __construct( $tag, $tagname, $values ) {
|
||||
$this->tag = $tag;
|
||||
$this->name = $this->tagname = $tagname;
|
||||
|
||||
$this->options = array(
|
||||
'do_not_heat' => false,
|
||||
'format' => '',
|
||||
);
|
||||
|
||||
if ( ! empty( $values ) ) {
|
||||
preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
|
||||
$this->values = wpcf7_strip_quote_deep( $matches[0] );
|
||||
}
|
||||
|
||||
if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
|
||||
$this->name = trim( $matches[1] );
|
||||
$this->options['do_not_heat'] = true;
|
||||
}
|
||||
|
||||
if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
|
||||
$this->name = trim( $matches[1] );
|
||||
$this->options['format'] = $this->values[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name part of this mail-tag.
|
||||
*/
|
||||
public function tag_name() {
|
||||
return $this->tagname;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the form field name corresponding to this mail-tag.
|
||||
*/
|
||||
public function field_name() {
|
||||
return strtr( $this->name, '.', '_' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the specified option.
|
||||
*/
|
||||
public function get_option( $option ) {
|
||||
return $this->options[$option];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the values part of this mail-tag.
|
||||
*/
|
||||
public function values() {
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the WPCF7_FormTag object that corresponds to this mail-tag.
|
||||
*/
|
||||
public function corresponding_form_tag() {
|
||||
if ( $this->form_tag instanceof WPCF7_FormTag ) {
|
||||
return $this->form_tag;
|
||||
}
|
||||
|
||||
if ( $submission = WPCF7_Submission::get_instance() ) {
|
||||
$contact_form = $submission->get_contact_form();
|
||||
$tags = $contact_form->scan_form_tags( array(
|
||||
'name' => $this->field_name(),
|
||||
'feature' => '! zero-controls-container',
|
||||
) );
|
||||
|
||||
if ( $tags ) {
|
||||
$this->form_tag = $tags[0];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->form_tag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ class WPCF7_Pipes {
|
||||
|
||||
private $pipes = array();
|
||||
|
||||
public function __construct( array $texts ) {
|
||||
foreach ( $texts as $text ) {
|
||||
public function __construct( array $texts = null ) {
|
||||
foreach ( (array) $texts as $text ) {
|
||||
$this->add_pipe( $text );
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,10 @@ class WPCF7_Pipes {
|
||||
$this->pipes[] = $pipe;
|
||||
}
|
||||
|
||||
public function merge( self $another ) {
|
||||
$this->pipes = array_merge( $this->pipes, $another->pipes );
|
||||
}
|
||||
|
||||
public function do_pipe( $input ) {
|
||||
$input_canonical = wpcf7_canonicalize( $input, array(
|
||||
'strto' => 'as-is',
|
||||
@@ -109,3 +113,37 @@ class WPCF7_Pipes {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Trait for classes that hold cross-tag WPCF7_Pipes object.
|
||||
*/
|
||||
trait WPCF7_PipesHolder {
|
||||
|
||||
protected $pipes;
|
||||
|
||||
public function get_pipes( $field_name ) {
|
||||
if ( isset( $this->pipes[$field_name] ) ) {
|
||||
return $this->pipes[$field_name];
|
||||
}
|
||||
|
||||
$result = new WPCF7_Pipes;
|
||||
|
||||
$tags = $this->scan_form_tags( array(
|
||||
'name' => $field_name,
|
||||
) );
|
||||
|
||||
foreach ( $tags as $tag ) {
|
||||
if ( $tag->pipes instanceof WPCF7_Pipes ) {
|
||||
$result->merge( $tag->pipes );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->pipes[$field_name] = $result;
|
||||
}
|
||||
|
||||
public function scan_form_tags() {
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -259,11 +259,7 @@ class WPCF7_Submission {
|
||||
* or false when no invalid field.
|
||||
*/
|
||||
public function get_invalid_field( $name ) {
|
||||
if ( isset( $this->invalid_fields[$name] ) ) {
|
||||
return $this->invalid_fields[$name];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return $this->invalid_fields[$name] ?? false;
|
||||
}
|
||||
|
||||
|
||||
@@ -285,9 +281,7 @@ class WPCF7_Submission {
|
||||
* null otherwise.
|
||||
*/
|
||||
public function get_meta( $name ) {
|
||||
if ( isset( $this->meta[$name] ) ) {
|
||||
return $this->meta[$name];
|
||||
}
|
||||
return $this->meta[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
@@ -295,38 +289,16 @@ class WPCF7_Submission {
|
||||
* Collects meta information about this submission.
|
||||
*/
|
||||
private function setup_meta_data() {
|
||||
$timestamp = time();
|
||||
|
||||
$remote_ip = $this->get_remote_ip_addr();
|
||||
|
||||
$remote_port = isset( $_SERVER['REMOTE_PORT'] )
|
||||
? (int) $_SERVER['REMOTE_PORT'] : '';
|
||||
|
||||
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] )
|
||||
? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
|
||||
|
||||
$url = $this->get_request_url();
|
||||
|
||||
$unit_tag = isset( $_POST['_wpcf7_unit_tag'] )
|
||||
? wpcf7_sanitize_unit_tag( $_POST['_wpcf7_unit_tag'] ) : '';
|
||||
|
||||
$container_post_id = isset( $_POST['_wpcf7_container_post'] )
|
||||
? (int) $_POST['_wpcf7_container_post'] : 0;
|
||||
|
||||
$current_user_id = get_current_user_id();
|
||||
|
||||
$do_not_store = $this->contact_form->is_true( 'do_not_store' );
|
||||
|
||||
$this->meta = array(
|
||||
'timestamp' => $timestamp,
|
||||
'remote_ip' => $remote_ip,
|
||||
'remote_port' => $remote_port,
|
||||
'user_agent' => $user_agent,
|
||||
'url' => $url,
|
||||
'unit_tag' => $unit_tag,
|
||||
'container_post_id' => $container_post_id,
|
||||
'current_user_id' => $current_user_id,
|
||||
'do_not_store' => $do_not_store,
|
||||
'timestamp' => time(),
|
||||
'remote_ip' => $this->get_remote_ip_addr(),
|
||||
'remote_port' => $_SERVER['REMOTE_PORT'] ?? '',
|
||||
'user_agent' => substr( $_SERVER['HTTP_USER_AGENT'] ?? '', 0, 254 ),
|
||||
'url' => $this->get_request_url(),
|
||||
'unit_tag' => wpcf7_sanitize_unit_tag( $_POST['_wpcf7_unit_tag'] ?? '' ),
|
||||
'container_post_id' => absint( $_POST['_wpcf7_container_post'] ?? 0 ),
|
||||
'current_user_id' => get_current_user_id(),
|
||||
'do_not_store' => $this->contact_form->is_true( 'do_not_store' ),
|
||||
);
|
||||
|
||||
return $this->meta;
|
||||
@@ -342,11 +314,7 @@ class WPCF7_Submission {
|
||||
*/
|
||||
public function get_posted_data( $name = '' ) {
|
||||
if ( ! empty( $name ) ) {
|
||||
if ( isset( $this->posted_data[$name] ) ) {
|
||||
return $this->posted_data[$name];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return $this->posted_data[$name] ?? null;
|
||||
}
|
||||
|
||||
return $this->posted_data;
|
||||
@@ -377,86 +345,56 @@ class WPCF7_Submission {
|
||||
* Constructs posted data property based on user input values.
|
||||
*/
|
||||
private function setup_posted_data() {
|
||||
$posted_data = array_filter( (array) $_POST, static function ( $key ) {
|
||||
return '_' !== substr( $key, 0, 1 );
|
||||
}, ARRAY_FILTER_USE_KEY );
|
||||
$posted_data = array_filter(
|
||||
(array) $_POST,
|
||||
static function ( $key ) {
|
||||
return ! str_starts_with( $key, '_' );
|
||||
},
|
||||
ARRAY_FILTER_USE_KEY
|
||||
);
|
||||
|
||||
$posted_data = wp_unslash( $posted_data );
|
||||
$posted_data = $this->sanitize_posted_data( $posted_data );
|
||||
|
||||
$tags = $this->contact_form->scan_form_tags();
|
||||
$tags = $this->contact_form->scan_form_tags( array(
|
||||
'feature' => array(
|
||||
'name-attr',
|
||||
'! not-for-mail',
|
||||
),
|
||||
) );
|
||||
|
||||
foreach ( (array) $tags as $tag ) {
|
||||
if ( empty( $tag->name ) ) {
|
||||
continue;
|
||||
$tags = array_reduce( $tags, static function ( $carry, $tag ) {
|
||||
if ( $tag->name and ! isset( $carry[$tag->name] ) ) {
|
||||
$carry[$tag->name] = $tag;
|
||||
}
|
||||
|
||||
$type = $tag->type;
|
||||
$name = $tag->name;
|
||||
$pipes = $tag->pipes;
|
||||
return $carry;
|
||||
}, array() );
|
||||
|
||||
$value_orig = $value = '';
|
||||
foreach ( $tags as $tag ) {
|
||||
$value_orig = $value = $posted_data[$tag->name] ?? '';
|
||||
|
||||
if ( isset( $posted_data[$name] ) ) {
|
||||
$value_orig = $value = $posted_data[$name];
|
||||
}
|
||||
|
||||
if ( WPCF7_USE_PIPE
|
||||
and $pipes instanceof WPCF7_Pipes
|
||||
and ! $pipes->zero() ) {
|
||||
if ( is_array( $value_orig ) ) {
|
||||
$value = array();
|
||||
|
||||
foreach ( $value_orig as $v ) {
|
||||
$value[] = $pipes->do_pipe( $v );
|
||||
}
|
||||
} else {
|
||||
$value = $pipes->do_pipe( $value_orig );
|
||||
}
|
||||
}
|
||||
|
||||
if ( wpcf7_form_tag_supports( $type, 'selectable-values' ) ) {
|
||||
if ( wpcf7_form_tag_supports( $tag->type, 'selectable-values' ) ) {
|
||||
$value = (array) $value;
|
||||
|
||||
if ( $tag->has_option( 'free_text' )
|
||||
and isset( $posted_data[$name . '_free_text'] ) ) {
|
||||
$last_val = array_pop( $value );
|
||||
if ( WPCF7_USE_PIPE ) {
|
||||
$pipes = $this->contact_form->get_pipes( $tag->name );
|
||||
|
||||
list( $tied_item ) = array_slice(
|
||||
WPCF7_USE_PIPE ? $tag->pipes->collect_afters() : $tag->values,
|
||||
-1, 1
|
||||
);
|
||||
|
||||
list( $last_val, $tied_item ) = array_map(
|
||||
static function ( $item ) {
|
||||
return wpcf7_canonicalize( $item, array(
|
||||
'strto' => 'as-is',
|
||||
) );
|
||||
},
|
||||
array( $last_val, $tied_item )
|
||||
);
|
||||
|
||||
if ( $last_val === $tied_item ) {
|
||||
$value[] = sprintf( '%s %s',
|
||||
$last_val,
|
||||
$posted_data[$name . '_free_text']
|
||||
);
|
||||
} else {
|
||||
$value[] = $last_val;
|
||||
}
|
||||
|
||||
unset( $posted_data[$name . '_free_text'] );
|
||||
$value = array_map( static function ( $value ) use ( $pipes ) {
|
||||
return $pipes->do_pipe( $value );
|
||||
}, $value );
|
||||
}
|
||||
}
|
||||
|
||||
$value = apply_filters( "wpcf7_posted_data_{$type}", $value,
|
||||
$value_orig, $tag
|
||||
$value = apply_filters( "wpcf7_posted_data_{$tag->type}",
|
||||
$value,
|
||||
$value_orig,
|
||||
$tag
|
||||
);
|
||||
|
||||
$posted_data[$name] = $value;
|
||||
$posted_data[$tag->name] = $value;
|
||||
|
||||
if ( $tag->has_option( 'consent_for:storage' )
|
||||
and empty( $posted_data[$name] ) ) {
|
||||
if ( $tag->has_option( 'consent_for:storage' ) and empty( $value ) ) {
|
||||
$this->meta['do_not_store'] = true;
|
||||
}
|
||||
}
|
||||
@@ -595,8 +533,7 @@ class WPCF7_Submission {
|
||||
$home_url = untrailingslashit( home_url() );
|
||||
|
||||
if ( self::is_restful() ) {
|
||||
$referer = isset( $_SERVER['HTTP_REFERER'] )
|
||||
? trim( $_SERVER['HTTP_REFERER'] ) : '';
|
||||
$referer = trim( $_SERVER['HTTP_REFERER'] ?? '' );
|
||||
|
||||
if ( $referer
|
||||
and 0 === strpos( $referer, $home_url ) ) {
|
||||
@@ -758,7 +695,7 @@ class WPCF7_Submission {
|
||||
return true;
|
||||
}
|
||||
|
||||
$nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
|
||||
$nonce = $_POST['_wpnonce'] ?? '';
|
||||
|
||||
return wpcf7_verify_nonce( $nonce );
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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