This commit is contained in:
2026-03-11 15:57:27 +01:00
parent 481271c972
commit b4b460fd21
10775 changed files with 2071579 additions and 26409 deletions

View File

@@ -7,80 +7,7 @@ add_action(
);
function wpcf7_init_block_editor_assets() {
$assets = array();
$asset_file = wpcf7_plugin_path(
'includes/block-editor/index.asset.php'
);
if ( file_exists( $asset_file ) ) {
$assets = include( $asset_file );
}
$assets = wp_parse_args( $assets, array(
'dependencies' => array(
'wp-api-fetch',
'wp-block-editor',
'wp-blocks',
'wp-components',
'wp-element',
'wp-i18n',
'wp-url',
),
'version' => WPCF7_VERSION,
) );
wp_register_script(
'contact-form-7-block-editor',
wpcf7_plugin_url( 'includes/block-editor/index.js' ),
$assets['dependencies'],
$assets['version']
);
wp_set_script_translations(
'contact-form-7-block-editor',
'contact-form-7'
);
register_block_type(
wpcf7_plugin_path( 'includes/block-editor' ),
array(
'editor_script' => 'contact-form-7-block-editor',
)
);
}
add_action(
'enqueue_block_editor_assets',
'wpcf7_enqueue_block_editor_assets',
10, 0
);
function wpcf7_enqueue_block_editor_assets() {
$contact_forms = array_map(
static function ( $contact_form ) {
return array(
'id' => $contact_form->id(),
'hash' => $contact_form->hash(),
'slug' => $contact_form->name(),
'title' => $contact_form->title(),
'locale' => $contact_form->locale(),
);
},
WPCF7_ContactForm::find( array(
'posts_per_page' => 20,
'orderby' => 'modified',
'order' => 'DESC',
) )
);
wp_add_inline_script(
'contact-form-7-block-editor',
sprintf(
'window.wpcf7 = {contactForms:%s};',
json_encode( $contact_forms )
),
'before'
wpcf7_plugin_path( 'includes/block-editor' )
);
}

View File

@@ -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",
@@ -34,5 +34,5 @@
"default": "form"
}
},
"editorScript": "file:./index.js"
"editorScript": [ "file:./index.js", "contact-form-7-block-editor" ]
}

View File

@@ -2,6 +2,7 @@
return array(
'dependencies' => array(
'react',
'wp-api-fetch',
'wp-block-editor',
'wp-blocks',

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,27 @@
<?php
add_action(
'wpcf7_update_option',
'wpcf7_config_validator_update_option',
10, 3
);
/**
* Runs bulk validation after the reCAPTCHA integration option is updated.
*/
function wpcf7_config_validator_update_option( $name, $value, $old_option ) {
if ( 'recaptcha' === $name ) {
$contact_forms = WPCF7_ContactForm::find();
$options = array(
'include' => 'unsafe_email_without_protection',
);
foreach ( $contact_forms as $contact_form ) {
$config_validator = new WPCF7_ConfigValidator( $contact_form, $options );
$config_validator->restore();
$config_validator->validate();
$config_validator->save();
}
}
}

View File

@@ -6,17 +6,22 @@ trait WPCF7_ConfigValidator_AdditionalSettings {
* Runs error detection for the additional settings section.
*/
public function validate_additional_settings() {
$deprecated_settings_used =
$this->contact_form->additional_setting( 'on_sent_ok' ) ||
$this->contact_form->additional_setting( 'on_submit' );
$section = 'additional_settings.body';
if ( $deprecated_settings_used ) {
return $this->add_error( 'additional_settings.body',
'deprecated_settings',
array(
'message' => __( "Deprecated settings are used.", 'contact-form-7' ),
)
);
if ( $this->supports( 'deprecated_settings' ) ) {
$deprecated_settings_used =
$this->contact_form->additional_setting( 'on_sent_ok' ) ||
$this->contact_form->additional_setting( 'on_submit' );
if ( $deprecated_settings_used ) {
$this->add_error( $section, 'deprecated_settings',
array(
'message' => __( "Deprecated settings are used.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'deprecated_settings' );
}
}
}

View File

@@ -8,12 +8,83 @@ trait WPCF7_ConfigValidator_Form {
public function validate_form() {
$section = 'form.body';
$form = $this->contact_form->prop( 'form' );
$this->detect_multiple_controls_in_label( $section, $form );
$this->detect_unavailable_names( $section, $form );
$this->detect_unavailable_html_elements( $section, $form );
$this->detect_dots_in_names( $section, $form );
$this->detect_colons_in_names( $section, $form );
$this->detect_upload_filesize_overlimit( $section, $form );
if ( $this->supports( 'multiple_controls_in_label' ) ) {
if ( $this->detect_multiple_controls_in_label( $section, $form ) ) {
$this->add_error( $section, 'multiple_controls_in_label',
array(
'message' => __( "Multiple form controls are in a single label element.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'multiple_controls_in_label' );
}
}
if ( $this->supports( 'unavailable_names' ) ) {
$ng_names = $this->detect_unavailable_names( $section, $form );
if ( $ng_names ) {
$this->add_error( $section, 'unavailable_names',
array(
'message' =>
/* translators: %names%: a list of form control names */
__( "Unavailable names (%names%) are used for form controls.", 'contact-form-7' ),
'params' => array( 'names' => implode( ', ', $ng_names ) ),
)
);
} else {
$this->remove_error( $section, 'unavailable_names' );
}
}
if ( $this->supports( 'unavailable_html_elements' ) ) {
if ( $this->detect_unavailable_html_elements( $section, $form ) ) {
$this->add_error( $section, 'unavailable_html_elements',
array(
'message' => __( "Unavailable HTML elements are used in the form template.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'unavailable_html_elements' );
}
}
if ( $this->supports( 'dots_in_names' ) ) {
if ( $this->detect_dots_in_names( $section, $form ) ) {
$this->add_error( $section, 'dots_in_names',
array(
'message' => __( "Dots are used in form-tag names.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'dots_in_names' );
}
}
if ( $this->supports( 'colons_in_names' ) ) {
if ( $this->detect_colons_in_names( $section, $form ) ) {
$this->add_error( $section, 'colons_in_names',
array(
'message' => __( "Colons are used in form-tag names.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'colons_in_names' );
}
}
if ( $this->supports( 'upload_filesize_overlimit' ) ) {
if ( $this->detect_upload_filesize_overlimit( $section, $form ) ) {
$this->add_error( $section, 'upload_filesize_overlimit',
array(
'message' => __( "Uploadable file size exceeds PHPs maximum acceptable size.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'upload_filesize_overlimit' );
}
}
}
@@ -54,12 +125,7 @@ trait WPCF7_ConfigValidator_Form {
}
if ( 1 < $fields_count ) {
return $this->add_error( $section,
'multiple_controls_in_label',
array(
'message' => __( "Multiple form controls are in a single label element.", 'contact-form-7' ),
)
);
return true;
}
}
}
@@ -98,17 +164,7 @@ trait WPCF7_ConfigValidator_Form {
}
if ( $ng_names ) {
$ng_names = array_unique( $ng_names );
return $this->add_error( $section,
'unavailable_names',
array(
'message' =>
/* translators: %names%: a list of form control names */
__( "Unavailable names (%names%) are used for form controls.", 'contact-form-7' ),
'params' => array( 'names' => implode( ', ', $ng_names ) ),
)
);
return array_unique( $ng_names );
}
return false;
@@ -124,12 +180,7 @@ trait WPCF7_ConfigValidator_Form {
$pattern = '%(?:<form[\s\t>]|</form>)%i';
if ( preg_match( $pattern, $content ) ) {
return $this->add_error( $section,
'unavailable_html_elements',
array(
'message' => __( "Unavailable HTML elements are used in the form template.", 'contact-form-7' ),
)
);
return true;
}
return false;
@@ -150,12 +201,7 @@ trait WPCF7_ConfigValidator_Form {
foreach ( $tags as $tag ) {
if ( str_contains( $tag->raw_name, '.' ) ) {
return $this->add_error( $section,
'dots_in_names',
array(
'message' => __( "Dots are used in form-tag names.", 'contact-form-7' ),
)
);
return true;
}
}
@@ -177,12 +223,7 @@ trait WPCF7_ConfigValidator_Form {
foreach ( $tags as $tag ) {
if ( str_contains( $tag->raw_name, ':' ) ) {
return $this->add_error( $section,
'colons_in_names',
array(
'message' => __( "Colons are used in form-tag names.", 'contact-form-7' ),
)
);
return true;
}
}
@@ -227,12 +268,7 @@ trait WPCF7_ConfigValidator_Form {
foreach ( $tags as $tag ) {
if ( $upload_max_filesize < $tag->get_limit_option() ) {
return $this->add_error( $section,
'upload_filesize_overlimit',
array(
'message' => __( "Uploadable file size exceeds PHPs maximum acceptable size.", 'contact-form-7' ),
)
);
return true;
}
}

View File

@@ -2,11 +2,27 @@
trait WPCF7_ConfigValidator_Mail {
/**
* Replaces all mail-tags in the given content.
*/
public function replace_mail_tags( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'callback' =>
array( $this, 'replace_mail_tags_with_minimum_input_callback' ),
) );
$content = new WPCF7_MailTaggedText( $content, $options );
return $content->replace_tags();
}
/**
* Callback function for WPCF7_MailTaggedText. Replaces mail-tags with
* the most conservative inputs.
*/
public function replace_mail_tags_with_minimum_input( $matches ) {
public function replace_mail_tags_with_minimum_input_callback( $matches ) {
// allow [[foo]] syntax for escaping a tag
if ( $matches[1] === '[' and $matches[4] === ']' ) {
return substr( $matches[0], 1, -1 );
@@ -23,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;
}
}
@@ -124,109 +115,253 @@ trait WPCF7_ConfigValidator_Mail {
'attachments' => '',
) );
$callback = array( $this, 'replace_mail_tags_with_minimum_input' );
$subject = new WPCF7_MailTaggedText(
$components['subject'],
array( 'callback' => $callback )
$this->validate_mail_subject(
$template,
$components['subject']
);
$subject = $subject->replace_tags();
$subject = wpcf7_strip_newline( $subject );
$this->detect_maybe_empty( sprintf( '%s.subject', $template ), $subject );
$sender = new WPCF7_MailTaggedText(
$components['sender'],
array( 'callback' => $callback )
$this->validate_mail_sender(
$template,
$components['sender']
);
$sender = $sender->replace_tags();
$sender = wpcf7_strip_newline( $sender );
$invalid_mailbox = $this->detect_invalid_mailbox_syntax(
sprintf( '%s.sender', $template ),
$sender
$this->validate_mail_recipient(
$template,
$components['recipient']
);
if ( ! $invalid_mailbox and ! wpcf7_is_email_in_site_domain( $sender ) ) {
$this->add_error( sprintf( '%s.sender', $template ),
'email_not_in_site_domain',
array(
'message' => __( "Sender email address does not belong to the site domain.", 'contact-form-7' ),
)
);
$this->validate_mail_additional_headers(
$template,
$components['additional_headers']
);
$this->validate_mail_body(
$template,
$components['body']
);
$this->validate_mail_attachments(
$template,
$components['attachments']
);
}
/**
* Runs error detection for the mail subject section.
*/
public function validate_mail_subject( $template, $content ) {
$section = sprintf( '%s.subject', $template );
if ( $this->supports( 'maybe_empty' ) ) {
if ( $this->detect_maybe_empty( $section, $content ) ) {
$this->add_error( $section, 'maybe_empty',
array(
'message' => __( "There is a possible empty field.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'maybe_empty' );
}
}
}
/**
* Runs error detection for the mail sender section.
*/
public function validate_mail_sender( $template, $content ) {
$section = sprintf( '%s.sender', $template );
if ( $this->supports( 'invalid_mailbox_syntax' ) ) {
if ( $this->detect_invalid_mailbox_syntax( $section, $content ) ) {
$this->add_error( $section, 'invalid_mailbox_syntax',
array(
'message' => __( "Invalid mailbox syntax is used.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'invalid_mailbox_syntax' );
}
}
$recipient = new WPCF7_MailTaggedText(
$components['recipient'],
array( 'callback' => $callback )
);
if ( $this->supports( 'email_not_in_site_domain' ) ) {
$this->remove_error( $section, 'email_not_in_site_domain' );
$recipient = $recipient->replace_tags();
$recipient = wpcf7_strip_newline( $recipient );
if ( ! $this->has_error( $section, 'invalid_mailbox_syntax' ) ) {
$sender = $this->replace_mail_tags( $content );
$sender = wpcf7_strip_newline( $sender );
$this->detect_invalid_mailbox_syntax(
sprintf( '%s.recipient', $template ),
$recipient
);
if ( ! wpcf7_is_email_in_site_domain( $sender ) ) {
$this->add_error( $section, 'email_not_in_site_domain',
array(
'message' => __( "Sender email address does not belong to the site domain.", 'contact-form-7' ),
)
);
}
}
}
}
$additional_headers = new WPCF7_MailTaggedText(
$components['additional_headers'],
array( 'callback' => $callback )
);
$additional_headers = $additional_headers->replace_tags();
$additional_headers = explode( "\n", $additional_headers );
$mailbox_header_types = array( 'reply-to', 'cc', 'bcc' );
$invalid_mail_header_exists = false;
/**
* Runs error detection for the mail recipient section.
*/
public function validate_mail_recipient( $template, $content ) {
$section = sprintf( '%s.recipient', $template );
foreach ( $additional_headers as $header ) {
if ( $this->supports( 'invalid_mailbox_syntax' ) ) {
if ( $this->detect_invalid_mailbox_syntax( $section, $content ) ) {
$this->add_error( $section, 'invalid_mailbox_syntax',
array(
'message' => __( "Invalid mailbox syntax is used.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'invalid_mailbox_syntax' );
}
}
if ( $this->supports( 'unsafe_email_without_protection' ) ) {
$this->remove_error( $section, 'unsafe_email_without_protection' );
if ( ! $this->has_error( $section, 'invalid_mailbox_syntax' ) ) {
if (
$this->detect_unsafe_email_without_protection( $section, $content )
) {
$this->add_error( $section, 'unsafe_email_without_protection',
array(
'message' => __( "Unsafe email config is used without sufficient protection.", 'contact-form-7' ),
)
);
}
}
}
}
/**
* Runs error detection for the mail additional headers section.
*/
public function validate_mail_additional_headers( $template, $content ) {
$section = sprintf( '%s.additional_headers', $template );
$invalid_mail_headers = array();
$invalid_mailbox_fields = array();
$unsafe_email_fields = array();
foreach ( explode( "\n", $content ) as $header ) {
$header = trim( $header );
if ( '' === $header ) {
continue;
}
if ( ! preg_match( '/^([0-9A-Za-z-]+):(.*)$/', $header, $matches ) ) {
$invalid_mail_header_exists = true;
} else {
$header_name = $matches[1];
$header_value = trim( $matches[2] );
$is_valid_header = preg_match(
'/^([0-9A-Za-z-]+):(.*)$/',
$header,
$matches
);
if ( in_array( strtolower( $header_name ), $mailbox_header_types )
and '' !== $header_value ) {
$this->detect_invalid_mailbox_syntax(
sprintf( '%s.additional_headers', $template ),
$header_value,
array(
'message' => __( "Invalid mailbox syntax is used in the %name% field.", 'contact-form-7' ),
'params' => array( 'name' => $header_name )
)
);
}
if ( ! $is_valid_header ) {
$invalid_mail_headers[] = $header;
continue;
}
$header_name = $matches[1];
$header_value = trim( $matches[2] );
if (
in_array(
strtolower( $header_name ), array( 'reply-to', 'cc', 'bcc' )
) and
'' !== $header_value and
$this->detect_invalid_mailbox_syntax( $section, $header_value )
) {
$invalid_mailbox_fields[] = $header_name;
continue;
}
if (
in_array( strtolower( $header_name ), array( 'cc', 'bcc' ) ) and
$this->detect_unsafe_email_without_protection( $section, $header_value )
) {
$unsafe_email_fields[] = $header_name;
}
}
if ( $invalid_mail_header_exists ) {
$this->add_error( sprintf( '%s.additional_headers', $template ),
'invalid_mail_header',
array(
'message' => __( "There are invalid mail header fields.", 'contact-form-7' ),
)
);
if ( $this->supports( 'invalid_mail_header' ) ) {
if ( ! empty( $invalid_mail_headers ) ) {
$this->add_error( $section, 'invalid_mail_header',
array(
'message' => __( "There are invalid mail header fields.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'invalid_mail_header' );
}
}
$body = new WPCF7_MailTaggedText(
$components['body'],
array( 'callback' => $callback )
);
if ( $this->supports( 'invalid_mailbox_syntax' ) ) {
if ( ! empty( $invalid_mailbox_fields ) ) {
foreach ( $invalid_mailbox_fields as $header_name ) {
$this->add_error( $section, 'invalid_mailbox_syntax',
array(
'message' => __( "Invalid mailbox syntax is used in the %name% field.", 'contact-form-7' ),
'params' => array( 'name' => $header_name ),
)
);
}
} else {
$this->remove_error( $section, 'invalid_mailbox_syntax' );
}
}
$body = $body->replace_tags();
if ( $this->supports( 'unsafe_email_without_protection' ) ) {
if ( ! empty( $unsafe_email_fields ) ) {
$this->add_error( $section, 'unsafe_email_without_protection',
array(
'message' => __( "Unsafe email config is used without sufficient protection.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'unsafe_email_without_protection' );
}
}
}
$this->detect_maybe_empty( sprintf( '%s.body', $template ), $body );
if ( '' !== $components['attachments'] ) {
/**
* Runs error detection for the mail body section.
*/
public function validate_mail_body( $template, $content ) {
$section = sprintf( '%s.body', $template );
if ( $this->supports( 'maybe_empty' ) ) {
if ( $this->detect_maybe_empty( $section, $content ) ) {
$this->add_error( $section, 'maybe_empty',
array(
'message' => __( "There is a possible empty field.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'maybe_empty' );
}
}
}
/**
* Runs error detection for the mail attachments section.
*/
public function validate_mail_attachments( $template, $content ) {
$section = sprintf( '%s.attachments', $template );
$total_size = 0;
$files_not_found = array();
$files_out_of_content = array();
if ( '' !== $content ) {
$attachables = array();
$tags = $this->contact_form->scan_form_tags(
@@ -236,7 +371,7 @@ trait WPCF7_ConfigValidator_Mail {
foreach ( $tags as $tag ) {
$name = $tag->name;
if ( ! str_contains( $components['attachments'], "[{$name}]" ) ) {
if ( ! str_contains( $content, "[{$name}]" ) ) {
continue;
}
@@ -249,41 +384,61 @@ trait WPCF7_ConfigValidator_Mail {
$total_size = array_sum( $attachables );
$has_file_not_found = false;
$has_file_not_in_content_dir = false;
foreach ( explode( "\n", $components['attachments'] ) as $line ) {
foreach ( explode( "\n", $content ) as $line ) {
$line = trim( $line );
if ( '' === $line or str_starts_with( $line, '[' ) ) {
continue;
}
$has_file_not_found = $this->detect_file_not_found(
sprintf( '%s.attachments', $template ), $line
);
if ( ! $has_file_not_found and ! $has_file_not_in_content_dir ) {
$has_file_not_in_content_dir = $this->detect_file_not_in_content_dir(
sprintf( '%s.attachments', $template ), $line
);
}
if ( ! $has_file_not_found ) {
$path = path_join( WP_CONTENT_DIR, $line );
if ( $this->detect_file_not_found( $section, $line ) ) {
$files_not_found[] = $line;
} elseif ( $this->detect_file_not_in_content_dir( $section, $line ) ) {
$files_out_of_content[] = $line;
} else {
$total_size += (int) @filesize( $path );
}
}
}
if ( $this->supports( 'file_not_found' ) ) {
if ( ! empty( $files_not_found ) ) {
foreach ( $files_not_found as $line ) {
$this->add_error( $section, 'file_not_found',
array(
'message' => __( "Attachment file does not exist at %path%.", 'contact-form-7' ),
'params' => array( 'path' => $line ),
)
);
}
} else {
$this->remove_error( $section, 'file_not_found' );
}
}
if ( $this->supports( 'file_not_in_content_dir' ) ) {
if ( ! empty( $files_out_of_content ) ) {
$this->add_error( $section, 'file_not_in_content_dir',
array(
'message' => __( "It is not allowed to use files outside the wp-content directory.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'file_not_in_content_dir' );
}
}
if ( $this->supports( 'attachments_overweight' ) ) {
$max = 25 * MB_IN_BYTES; // 25 MB
if ( $max < $total_size ) {
$this->add_error( sprintf( '%s.attachments', $template ),
'attachments_overweight',
$this->add_error( $section, 'attachments_overweight',
array(
'message' => __( "The total size of attachment files is too large.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'attachments_overweight' );
}
}
}
@@ -294,17 +449,12 @@ trait WPCF7_ConfigValidator_Mail {
*
* @link https://contactform7.com/configuration-errors/invalid-mailbox-syntax/
*/
public function detect_invalid_mailbox_syntax( $section, $content, $args = '' ) {
$args = wp_parse_args( $args, array(
'message' => __( "Invalid mailbox syntax is used.", 'contact-form-7' ),
'params' => array(),
) );
public function detect_invalid_mailbox_syntax( $section, $content ) {
$content = $this->replace_mail_tags( $content );
$content = wpcf7_strip_newline( $content );
if ( ! wpcf7_is_mailbox_list( $content ) ) {
return $this->add_error( $section,
'invalid_mailbox_syntax',
$args
);
return true;
}
return false;
@@ -317,13 +467,11 @@ trait WPCF7_ConfigValidator_Mail {
* @link https://contactform7.com/configuration-errors/maybe-empty/
*/
public function detect_maybe_empty( $section, $content ) {
$content = $this->replace_mail_tags( $content );
$content = wpcf7_strip_newline( $content );
if ( '' === $content ) {
return $this->add_error( $section,
'maybe_empty',
array(
'message' => __( "There is a possible empty field.", 'contact-form-7' ),
)
);
return true;
}
return false;
@@ -339,13 +487,7 @@ trait WPCF7_ConfigValidator_Mail {
$path = path_join( WP_CONTENT_DIR, $content );
if ( ! is_readable( $path ) or ! is_file( $path ) ) {
return $this->add_error( $section,
'file_not_found',
array(
'message' => __( "Attachment file does not exist at %path%.", 'contact-form-7' ),
'params' => array( 'path' => $content ),
)
);
return true;
}
return false;
@@ -361,12 +503,73 @@ trait WPCF7_ConfigValidator_Mail {
$path = path_join( WP_CONTENT_DIR, $content );
if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) {
return $this->add_error( $section,
'file_not_in_content_dir',
array(
'message' => __( "It is not allowed to use files outside the wp-content directory.", 'contact-form-7' ),
)
);
return true;
}
return false;
}
/**
* Detects errors of that unsafe email config is used without
* sufficient protection.
*
* @link https://contactform7.com/configuration-errors/unsafe-email-without-protection/
*/
public function detect_unsafe_email_without_protection( $section, $content ) {
static $is_recaptcha_active = null;
if ( null === $is_recaptcha_active ) {
$is_recaptcha_active = call_user_func( function () {
$service = WPCF7_RECAPTCHA::get_instance();
return $service->is_active();
} );
}
if ( $is_recaptcha_active ) {
return false;
}
$example_email = 'user-specified@example.com';
// Replace mail-tags connected to an email type form-tag first.
$content = $this->replace_mail_tags( $content, array(
'callback' => function ( $matches ) use ( $example_email ) {
// allow [[foo]] syntax for escaping a tag
if ( $matches[1] === '[' and $matches[4] === ']' ) {
return substr( $matches[0], 1, -1 );
}
$tag = $matches[0];
$tagname = $matches[2];
$values = $matches[3];
$mail_tag = new WPCF7_MailTag( $tag, $tagname, $values );
$field_name = $mail_tag->field_name();
$form_tags = $this->contact_form->scan_form_tags(
array( 'name' => $field_name )
);
if ( $form_tags ) {
$form_tag = new WPCF7_FormTag( $form_tags[0] );
if ( 'email' === $form_tag->basetype ) {
return $example_email;
}
}
return $tag;
},
) );
// Replace remaining mail-tags.
$content = $this->replace_mail_tags( $content );
$content = wpcf7_strip_newline( $content );
if ( str_contains( $content, $example_email ) ) {
return true;
}
return false;

View File

@@ -12,14 +12,27 @@ trait WPCF7_ConfigValidator_Messages {
return;
}
if ( isset( $messages['captcha_not_match'] )
and ! wpcf7_use_really_simple_captcha() ) {
if (
isset( $messages['captcha_not_match'] ) and
! wpcf7_use_really_simple_captcha()
) {
unset( $messages['captcha_not_match'] );
}
foreach ( $messages as $key => $message ) {
$section = sprintf( 'messages.%s', $key );
$this->detect_html_in_message( $section, $message );
if ( $this->supports( 'html_in_message' ) ) {
if ( $this->detect_html_in_message( $section, $message ) ) {
$this->add_error( $section, 'html_in_message',
array(
'message' => __( "HTML tags are used in a message.", 'contact-form-7' ),
)
);
} else {
$this->remove_error( $section, 'html_in_message' );
}
}
}
}
@@ -32,13 +45,8 @@ trait WPCF7_ConfigValidator_Messages {
public function detect_html_in_message( $section, $content ) {
$stripped = wp_strip_all_tags( $content );
if ( $stripped != $content ) {
return $this->add_error( $section,
'html_in_message',
array(
'message' => __( "HTML tags are used in a message.", 'contact-form-7' ),
)
);
if ( $stripped !== $content ) {
return true;
}
return false;

View File

@@ -4,6 +4,7 @@ require_once path_join( __DIR__, 'form.php' );
require_once path_join( __DIR__, 'mail.php' );
require_once path_join( __DIR__, 'messages.php' );
require_once path_join( __DIR__, 'additional-settings.php' );
require_once path_join( __DIR__, 'actions.php' );
/**
@@ -13,15 +14,10 @@ require_once path_join( __DIR__, 'additional-settings.php' );
*/
class WPCF7_ConfigValidator {
use WPCF7_ConfigValidator_Form;
use WPCF7_ConfigValidator_Mail;
use WPCF7_ConfigValidator_Messages;
use WPCF7_ConfigValidator_AdditionalSettings;
/**
* The plugin version in which important updates happened last time.
*/
const last_important_update = '5.6.1';
const last_important_update = '5.8.1';
const error_codes = array(
'maybe_empty',
@@ -39,10 +35,18 @@ class WPCF7_ConfigValidator {
'dots_in_names',
'colons_in_names',
'upload_filesize_overlimit',
'unsafe_email_without_protection',
);
use WPCF7_ConfigValidator_Form;
use WPCF7_ConfigValidator_Mail;
use WPCF7_ConfigValidator_Messages;
use WPCF7_ConfigValidator_AdditionalSettings;
private $contact_form;
private $errors = array();
private $include;
private $exclude;
/**
@@ -66,8 +70,21 @@ class WPCF7_ConfigValidator {
/**
* Constructor.
*/
public function __construct( WPCF7_ContactForm $contact_form ) {
public function __construct( WPCF7_ContactForm $contact_form, $options = '' ) {
$options = wp_parse_args( $options, array(
'include' => null,
'exclude' => null,
) );
$this->contact_form = $contact_form;
if ( isset( $options['include'] ) ) {
$this->include = (array) $options['include'];
}
if ( isset( $options['exclude'] ) ) {
$this->exclude = (array) $options['exclude'];
}
}
@@ -87,11 +104,29 @@ class WPCF7_ConfigValidator {
}
/**
* Returns true if the given error code is supported by this instance.
*/
public function supports( $error_code ) {
if ( isset( $this->include ) ) {
$supported_codes = array_intersect( self::error_codes, $this->include );
} else {
$supported_codes = self::error_codes;
}
if ( isset( $this->exclude ) ) {
$supported_codes = array_diff( $supported_codes, $this->exclude );
}
return in_array( $error_code, $supported_codes, true );
}
/**
* Counts detected errors.
*/
public function count_errors( $args = '' ) {
$args = wp_parse_args( $args, array(
public function count_errors( $options = '' ) {
$options = wp_parse_args( $options, array(
'section' => '',
'code' => '',
) );
@@ -103,9 +138,9 @@ class WPCF7_ConfigValidator {
$key = sprintf( 'mail.%s', $matches[1] );
}
if ( $args['section']
and $key !== $args['section']
and preg_replace( '/\..*$/', '', $key, 1 ) !== $args['section'] ) {
if ( $options['section']
and $key !== $options['section']
and preg_replace( '/\..*$/', '', $key, 1 ) !== $options['section'] ) {
continue;
}
@@ -114,7 +149,7 @@ class WPCF7_ConfigValidator {
continue;
}
if ( $args['code'] and $error['code'] !== $args['code'] ) {
if ( $options['code'] and $error['code'] !== $options['code'] ) {
continue;
}
@@ -195,6 +230,27 @@ class WPCF7_ConfigValidator {
}
/**
* Returns true if the specified section has the specified error.
*
* @param string $section The section where the error detected.
* @param string $code The unique code of the error.
*/
public function has_error( $section, $code ) {
if ( empty( $this->errors[$section] ) ) {
return false;
}
foreach ( (array) $this->errors[$section] as $error ) {
if ( isset( $error['code'] ) and $error['code'] === $code ) {
return true;
}
}
return false;
}
/**
* Adds a validation error.
*
@@ -264,8 +320,6 @@ class WPCF7_ConfigValidator {
* @return bool True if there is no error detected.
*/
public function validate() {
$this->errors = array();
$this->validate_form();
$this->validate_mail( 'mail' );
$this->validate_mail( 'mail_2' );

View File

@@ -221,7 +221,7 @@ function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) {
return '[contact-form-7]';
}
if ( 'contact-form-7' == $code ) {
if ( 'contact-form-7' === $code ) {
$atts = shortcode_atts(
array(
'id' => '',
@@ -269,19 +269,23 @@ function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) {
return $contact_form->form_html( $atts );
};
return wpcf7_switch_locale(
$output = wpcf7_switch_locale(
$contact_form->locale(),
$callback,
$contact_form, $atts
);
do_action( 'wpcf7_shortcode_callback', $contact_form, $atts );
return $output;
}
/**
* Saves the contact form data.
*/
function wpcf7_save_contact_form( $args = '', $context = 'save' ) {
$args = wp_parse_args( $args, array(
function wpcf7_save_contact_form( $data = '', $context = 'save' ) {
$data = wp_parse_args( $data, array(
'id' => -1,
'title' => null,
'locale' => null,
@@ -292,56 +296,56 @@ function wpcf7_save_contact_form( $args = '', $context = 'save' ) {
'additional_settings' => null,
) );
$args = wp_unslash( $args );
$data = wp_unslash( $data );
$args['id'] = (int) $args['id'];
$data['id'] = (int) $data['id'];
if ( -1 == $args['id'] ) {
if ( -1 == $data['id'] ) {
$contact_form = WPCF7_ContactForm::get_template();
} else {
$contact_form = wpcf7_contact_form( $args['id'] );
$contact_form = wpcf7_contact_form( $data['id'] );
}
if ( empty( $contact_form ) ) {
return false;
}
if ( null !== $args['title'] ) {
$contact_form->set_title( $args['title'] );
if ( null !== $data['title'] ) {
$contact_form->set_title( $data['title'] );
}
if ( null !== $args['locale'] ) {
$contact_form->set_locale( $args['locale'] );
if ( null !== $data['locale'] ) {
$contact_form->set_locale( $data['locale'] );
}
$properties = array();
if ( null !== $args['form'] ) {
$properties['form'] = wpcf7_sanitize_form( $args['form'] );
if ( null !== $data['form'] ) {
$properties['form'] = wpcf7_sanitize_form( $data['form'] );
}
if ( null !== $args['mail'] ) {
$properties['mail'] = wpcf7_sanitize_mail( $args['mail'] );
if ( null !== $data['mail'] ) {
$properties['mail'] = wpcf7_sanitize_mail( $data['mail'] );
$properties['mail']['active'] = true;
}
if ( null !== $args['mail_2'] ) {
$properties['mail_2'] = wpcf7_sanitize_mail( $args['mail_2'] );
if ( null !== $data['mail_2'] ) {
$properties['mail_2'] = wpcf7_sanitize_mail( $data['mail_2'] );
}
if ( null !== $args['messages'] ) {
$properties['messages'] = wpcf7_sanitize_messages( $args['messages'] );
if ( null !== $data['messages'] ) {
$properties['messages'] = wpcf7_sanitize_messages( $data['messages'] );
}
if ( null !== $args['additional_settings'] ) {
if ( null !== $data['additional_settings'] ) {
$properties['additional_settings'] = wpcf7_sanitize_additional_settings(
$args['additional_settings']
$data['additional_settings']
);
}
$contact_form->set_properties( $properties );
do_action( 'wpcf7_save_contact_form', $contact_form, $args, $context );
do_action( 'wpcf7_save_contact_form', $contact_form, $data, $context );
if ( 'save' == $context ) {
$contact_form->save();

View File

@@ -74,7 +74,7 @@ class WPCF7_ContactFormTemplate {
. '-- ' . "\n"
. sprintf(
/* translators: 1: blog name, 2: blog URL */
__( 'This e-mail was sent from a contact form on %1$s (%2$s)', 'contact-form-7' ),
__( 'This is a notification that a contact form was submitted on your website (%1$s %2$s).', 'contact-form-7' ),
'[_site_title]',
'[_site_url]'
),
@@ -108,7 +108,7 @@ class WPCF7_ContactFormTemplate {
. '-- ' . "\n"
. sprintf(
/* translators: 1: blog name, 2: blog URL */
__( 'This e-mail was sent from a contact form on %1$s (%2$s)', 'contact-form-7' ),
__( 'This email is a receipt for your contact form submission on our website (%1$s %2$s) in which your email address was used. If that was not you, please ignore this message.', 'contact-form-7' ),
'[_site_title]',
'[_site_url]'
),

View File

@@ -3,6 +3,7 @@
class WPCF7_ContactForm {
use WPCF7_SWV_SchemaHolder;
use WPCF7_PipesHolder;
const post_type = 'wpcf7_contact_form';
@@ -104,23 +105,23 @@ class WPCF7_ContactForm {
/**
* Returns a contact form data filled by default template contents.
*
* @param string|array $args Optional. Contact form options.
* @param string|array $options Optional. Contact form options.
* @return WPCF7_ContactForm A new contact form object.
*/
public static function get_template( $args = '' ) {
$args = wp_parse_args( $args, array(
public static function get_template( $options = '' ) {
$options = wp_parse_args( $options, array(
'locale' => null,
'title' => __( 'Untitled', 'contact-form-7' ),
) );
if ( ! isset( $args['locale'] ) ) {
$args['locale'] = determine_locale();
if ( ! isset( $options['locale'] ) ) {
$options['locale'] = determine_locale();
}
$callback = static function ( $args ) {
$callback = static function ( $options ) {
$contact_form = new self;
$contact_form->title = $args['title'];
$contact_form->locale = $args['locale'];
$contact_form->title = $options['title'];
$contact_form->locale = $options['locale'];
$properties = $contact_form->get_properties();
@@ -138,13 +139,13 @@ class WPCF7_ContactForm {
};
$contact_form = wpcf7_switch_locale(
$args['locale'],
$options['locale'],
$callback,
$args
$options
);
self::$current = apply_filters( 'wpcf7_contact_form_default_pack',
$contact_form, $args
$contact_form, $options
);
return self::$current;
@@ -516,11 +517,11 @@ class WPCF7_ContactForm {
/**
* Generates HTML that represents a form.
*
* @param string|array $args Optional. Form options.
* @param string|array $options Optional. Form options.
* @return string HTML output.
*/
public function form_html( $args = '' ) {
$args = wp_parse_args( $args, array(
public function form_html( $options = '' ) {
$options = wp_parse_args( $options, array(
'html_id' => '',
'html_name' => '',
'html_title' => '',
@@ -528,9 +529,9 @@ class WPCF7_ContactForm {
'output' => 'form',
) );
$this->shortcode_atts = $args;
$this->shortcode_atts = $options;
if ( 'raw_form' == $args['output'] ) {
if ( 'raw_form' == $options['output'] ) {
return sprintf(
'<pre class="wpcf7-raw-form"><code>%s</code></pre>',
esc_html( $this->prop( 'form' ) )
@@ -554,6 +555,28 @@ class WPCF7_ContactForm {
$this->unit_tag = self::generate_unit_tag( $this->id );
$action_url = wpcf7_get_request_uri();
if ( $frag = strstr( $action_url, '#' ) ) {
$action_url = substr( $action_url, 0, -strlen( $frag ) );
}
$action_url .= '#' . $this->unit_tag();
$action_url = apply_filters( 'wpcf7_form_action_url', $action_url );
if (
str_starts_with( $action_url, '//' ) or
! str_starts_with( $action_url, '/' ) and
! str_starts_with( $action_url, home_url() )
) {
return sprintf(
'<p class="wpcf7-invalid-action-url"><strong>%1$s</strong> %2$s</p>',
esc_html( __( 'Error:', 'contact-form-7' ) ),
esc_html( __( "Invalid action URL is detected.", 'contact-form-7' ) )
);
}
$lang_tag = str_replace( '_', '-', $this->locale );
if ( preg_match( '/^([a-z]+-[a-z]+)-/i', $lang_tag, $matches ) ) {
@@ -572,25 +595,15 @@ class WPCF7_ContactForm {
$html .= "\n" . $this->screen_reader_response() . "\n";
$url = wpcf7_get_request_uri();
if ( $frag = strstr( $url, '#' ) ) {
$url = substr( $url, 0, -strlen( $frag ) );
}
$url .= '#' . $this->unit_tag();
$url = apply_filters( 'wpcf7_form_action_url', $url );
$id_attr = apply_filters( 'wpcf7_form_id_attr',
preg_replace( '/[^A-Za-z0-9:._-]/', '', $args['html_id'] )
preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_id'] )
);
$name_attr = apply_filters( 'wpcf7_form_name_attr',
preg_replace( '/[^A-Za-z0-9:._-]/', '', $args['html_name'] )
preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_name'] )
);
$title_attr = apply_filters( 'wpcf7_form_title_attr', $args['html_title'] );
$title_attr = apply_filters( 'wpcf7_form_title_attr', $options['html_title'] );
$class = 'wpcf7-form';
@@ -607,8 +620,8 @@ class WPCF7_ContactForm {
$class .= ' init';
}
if ( $args['html_class'] ) {
$class .= ' ' . $args['html_class'];
if ( $options['html_class'] ) {
$class .= ' ' . $options['html_class'];
}
if ( $this->in_demo_mode() ) {
@@ -626,7 +639,7 @@ class WPCF7_ContactForm {
$autocomplete = apply_filters( 'wpcf7_form_autocomplete', '' );
$atts = array(
'action' => esc_url( $url ),
'action' => esc_url( $action_url ),
'method' => 'post',
'class' => ( '' !== $class ) ? $class : null,
'id' => ( '' !== $id_attr ) ? $id_attr : null,
@@ -951,13 +964,13 @@ class WPCF7_ContactForm {
/**
* Collects mail-tags available for this contact form.
*
* @param string|array $args Optional. Search options.
* @param string|array $options Optional. Search options.
* @return array Mail-tag names.
*/
public function collect_mail_tags( $args = '' ) {
public function collect_mail_tags( $options = '' ) {
$manager = WPCF7_FormTagsManager::get_instance();
$args = wp_parse_args( $args, array(
$options = wp_parse_args( $options, array(
'include' => array(),
'exclude' => $manager->collect_tag_types( 'not-for-mail' ),
) );
@@ -970,12 +983,12 @@ class WPCF7_ContactForm {
if ( empty( $type ) ) {
continue;
} elseif ( ! empty( $args['include'] ) ) {
if ( ! in_array( $type, $args['include'] ) ) {
} elseif ( ! empty( $options['include'] ) ) {
if ( ! in_array( $type, $options['include'] ) ) {
continue;
}
} elseif ( ! empty( $args['exclude'] ) ) {
if ( in_array( $type, $args['exclude'] ) ) {
} elseif ( ! empty( $options['exclude'] ) ) {
if ( in_array( $type, $options['exclude'] ) ) {
continue;
}
}
@@ -987,7 +1000,7 @@ class WPCF7_ContactForm {
$mailtags = array_filter( $mailtags );
$mailtags = array_values( $mailtags );
return apply_filters( 'wpcf7_collect_mail_tags', $mailtags, $args, $this );
return apply_filters( 'wpcf7_collect_mail_tags', $mailtags, $options, $this );
}
@@ -1033,11 +1046,11 @@ class WPCF7_ContactForm {
/**
* Submits this contact form.
*
* @param string|array $args Optional. Submission options. Default empty.
* @param string|array $options Optional. Submission options. Default empty.
* @return array Result of submission.
*/
public function submit( $args = '' ) {
$args = wp_parse_args( $args, array(
public function submit( $options = '' ) {
$options = wp_parse_args( $options, array(
'skip_mail' =>
( $this->in_demo_mode()
|| $this->is_true( 'skip_mail' )
@@ -1059,7 +1072,7 @@ class WPCF7_ContactForm {
}
$submission = WPCF7_Submission::get_instance( $this, array(
'skip_mail' => $args['skip_mail'],
'skip_mail' => $options['skip_mail'],
) );
$result = array(
@@ -1330,14 +1343,14 @@ class WPCF7_ContactForm {
/**
* Returns a WordPress shortcode for this contact form.
*/
public function shortcode( $args = '' ) {
$args = wp_parse_args( $args, array(
public function shortcode( $options = '' ) {
$options = wp_parse_args( $options, array(
'use_old_format' => false
) );
$title = str_replace( array( '"', '[', ']' ), '', $this->title );
if ( $args['use_old_format'] ) {
if ( $options['use_old_format'] ) {
$old_unit_id = (int) get_post_meta( $this->id, '_old_cf7_unit_id', true );
if ( $old_unit_id ) {
@@ -1358,7 +1371,7 @@ class WPCF7_ContactForm {
}
return apply_filters( 'wpcf7_contact_form_shortcode',
$shortcode, $args, $this
$shortcode, $options, $this
);
}
}

View File

@@ -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() ) {

View File

@@ -140,6 +140,10 @@ form.submitting .wpcf7-spinner {
}
}
.wpcf7 [inert] {
opacity: 0.5;
}
.wpcf7 input[type="file"] {
cursor: pointer;
}

View File

@@ -4,11 +4,11 @@
* Validates uploaded files and moves them to the temporary directory.
*
* @param array $file An item of `$_FILES`.
* @param string|array $args Optional. Arguments to control behavior.
* @param string|array $options Optional. Options to control behavior.
* @return array|WP_Error Array of file paths, or WP_Error if validation fails.
*/
function wpcf7_unship_uploaded_file( $file, $args = '' ) {
$args = wp_parse_args( $args, array(
function wpcf7_unship_uploaded_file( $file, $options = '' ) {
$options = wp_parse_args( $options, array(
'required' => false,
'filetypes' => '',
'limit' => MB_IN_BYTES,
@@ -33,14 +33,16 @@ function wpcf7_unship_uploaded_file( $file, $args = '' ) {
}
}
if ( isset( $args['schema'] ) and isset( $args['name'] ) ) {
$result = $args['schema']->validate( array(
if ( isset( $options['schema'] ) and isset( $options['name'] ) ) {
$context = array(
'file' => true,
'field' => $args['name'],
) );
'field' => $options['name'],
);
if ( is_wp_error( $result ) ) {
return $result;
foreach ( $options['schema']->validate( $context ) as $result ) {
if ( is_wp_error( $result ) ) {
return $result;
}
}
}
@@ -62,7 +64,7 @@ function wpcf7_unship_uploaded_file( $file, $args = '' ) {
$filename = wpcf7_antiscript_file_name( $filename );
$filename = apply_filters( 'wpcf7_upload_file_name',
$filename, $name, $args
$filename, $name, $options
);
$filename = wp_unique_filename( $uploads_dir, $filename );

View File

@@ -410,7 +410,7 @@ class WPCF7_FormTag implements ArrayAccess {
if ( $contact_form = WPCF7_ContactForm::get_current() ) {
$val = $contact_form->shortcode_attr( $this->name );
if ( strlen( $val ) ) {
if ( isset( $val ) and strlen( $val ) ) {
if ( $args['multiple'] ) {
$values[] = $val;
} else {

View File

@@ -191,19 +191,19 @@ function wpcf7_strip_newline( $text ) {
* Canonicalizes text.
*
* @param string $text Input text.
* @param string|array|object $args Options.
* @param string|array|object $options Options.
* @return string Canonicalized text.
*/
function wpcf7_canonicalize( $text, $args = '' ) {
function wpcf7_canonicalize( $text, $options = '' ) {
// for back-compat
if ( is_string( $args ) and '' !== $args
and false === strpos( $args, '=' ) ) {
$args = array(
'strto' => $args,
if ( is_string( $options ) and '' !== $options
and false === strpos( $options, '=' ) ) {
$options = array(
'strto' => $options,
);
}
$args = wp_parse_args( $args, array(
$options = wp_parse_args( $options, array(
'strto' => 'lower',
'strip_separators' => false,
) );
@@ -229,19 +229,19 @@ function wpcf7_canonicalize( $text, $args = '' ) {
$text = mb_convert_kana( $text, 'asKV', $charset );
}
if ( $args['strip_separators'] ) {
if ( $options['strip_separators'] ) {
$text = preg_replace( '/[\r\n\t ]+/', '', $text );
} else {
$text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
}
if ( 'lower' == $args['strto'] ) {
if ( 'lower' == $options['strto'] ) {
if ( function_exists( 'mb_strtolower' ) ) {
$text = mb_strtolower( $text, $charset );
} else {
$text = strtolower( $text );
}
} elseif ( 'upper' == $args['strto'] ) {
} elseif ( 'upper' == $options['strto'] ) {
if ( function_exists( 'mb_strtoupper' ) ) {
$text = mb_strtoupper( $text, $charset );
} else {
@@ -261,7 +261,7 @@ function wpcf7_canonicalize( $text, $args = '' ) {
* @return string Sanitized unit-tag.
*/
function wpcf7_sanitize_unit_tag( $tag ) {
$tag = preg_replace( '/[^A-Za-z0-9_-]/', '', $tag );
$tag = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $tag );
return $tag;
}
@@ -275,6 +275,17 @@ function wpcf7_sanitize_unit_tag( $tag ) {
function wpcf7_antiscript_file_name( $filename ) {
$filename = wp_basename( $filename );
// Apply part of protection logic from sanitize_file_name().
$filename = str_replace(
array(
'?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"',
'&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}',
'%', '+', '', '«', '»', '”', '“', chr( 0 )
),
'',
$filename
);
$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
$filename = preg_replace( '/[\pC\pZ]+/iu', '', $filename );
@@ -522,7 +533,13 @@ function wpcf7_format_atts( $atts ) {
}
static $boolean_attributes = array(
'checked', 'disabled', 'multiple', 'readonly', 'required', 'selected',
'checked',
'disabled',
'inert',
'multiple',
'readonly',
'required',
'selected',
);
if ( in_array( $name, $boolean_attributes ) and '' === $value ) {

View File

@@ -147,11 +147,11 @@ function wpcf7_exclude_blank( $input ) {
* Creates a comma-separated list from a multi-dimensional array.
*
* @param mixed $input Array or item of array.
* @param string|array $args Optional. Output options.
* @param string|array $options Optional. Output options.
* @return string Comma-separated list.
*/
function wpcf7_flat_join( $input, $args = '' ) {
$args = wp_parse_args( $args, array(
function wpcf7_flat_join( $input, $options = '' ) {
$options = wp_parse_args( $options, array(
'separator' => ', ',
) );
@@ -164,7 +164,7 @@ function wpcf7_flat_join( $input, $args = '' ) {
}
}
return implode( $args['separator'], $output );
return implode( $options['separator'], $output );
}
@@ -210,10 +210,14 @@ function wpcf7_validate_configuration() {
/**
* Returns true if wpcf7_autop() is applied to form content.
* Returns true if wpcf7_autop() is applied.
*/
function wpcf7_autop_or_not() {
return (bool) apply_filters( 'wpcf7_autop_or_not', WPCF7_AUTOP );
function wpcf7_autop_or_not( $options = '' ) {
$options = wp_parse_args( $options, array(
'for' => 'form',
) );
return (bool) apply_filters( 'wpcf7_autop_or_not', WPCF7_AUTOP, $options );
}
@@ -238,16 +242,16 @@ function wpcf7_load_css() {
*
* @param string $url Link URL.
* @param string $anchor_text Anchor label text.
* @param string|array $args Optional. Link options.
* @param string|array $atts Optional. HTML attributes.
* @return string Formatted anchor element.
*/
function wpcf7_link( $url, $anchor_text, $args = '' ) {
$args = wp_parse_args( $args, array(
function wpcf7_link( $url, $anchor_text, $atts = '' ) {
$atts = wp_parse_args( $atts, array(
'id' => null,
'class' => null,
) );
$atts = array_merge( $args, array(
$atts = array_merge( $atts, array(
'href' => esc_url( $url ),
) );
@@ -267,6 +271,7 @@ function wpcf7_get_request_uri() {
if ( empty( $request_uri ) ) {
$request_uri = add_query_arg( array() );
$request_uri = '/' . ltrim( $request_uri, '/' );
}
return sanitize_url( $request_uri );
@@ -289,22 +294,20 @@ function wpcf7_register_post_types() {
/**
* Returns the version string of this plugin.
*
* @param string|array $args Optional. Output options.
* @param string|array $options Optional. Output options.
* @return string Version string.
*/
function wpcf7_version( $args = '' ) {
$defaults = array(
function wpcf7_version( $options = '' ) {
$options = wp_parse_args( $options, array(
'limit' => -1,
'only_major' => false,
);
) );
$args = wp_parse_args( $args, $defaults );
if ( $args['only_major'] ) {
$args['limit'] = 2;
if ( $options['only_major'] ) {
$options['limit'] = 2;
}
$args['limit'] = (int) $args['limit'];
$options['limit'] = (int) $options['limit'];
$ver = WPCF7_VERSION;
$ver = strtr( $ver, '_-+', '...' );
@@ -313,8 +316,8 @@ function wpcf7_version( $args = '' ) {
$ver = trim( $ver, '.' );
$ver = explode( '.', $ver );
if ( -1 < $args['limit'] ) {
$ver = array_slice( $ver, 0, $args['limit'] );
if ( -1 < $options['limit'] ) {
$ver = array_slice( $ver, 0, $options['limit'] );
}
$ver = implode( '.', $ver );
@@ -428,15 +431,15 @@ function wpcf7_rmdir_p( $dir ) {
*
* @link https://developer.wordpress.org/reference/functions/_http_build_query/
*
* @param array $args URL query parameters.
* @param array $data URL query parameters.
* @param string $key Optional. If specified, used to prefix key name.
* @return string Query string.
*/
function wpcf7_build_query( $args, $key = '' ) {
function wpcf7_build_query( $data, $key = '' ) {
$sep = '&';
$ret = array();
foreach ( (array) $args as $k => $v ) {
foreach ( (array) $data as $k => $v ) {
$k = urlencode( $k );
if ( ! empty( $key ) ) {

View File

@@ -95,8 +95,8 @@ class WPCF7_HTMLFormatter {
/**
* Constructor.
*/
public function __construct( $args = '' ) {
$this->options = wp_parse_args( $args, array(
public function __construct( $options = '' ) {
$this->options = wp_parse_args( $options, array(
'auto_br' => true,
'auto_indent' => true,
) );

View File

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

View 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;
}
}

View File

@@ -6,7 +6,7 @@ add_filter( 'wpcf7_mail_html_body', 'wpcf7_mail_html_body_autop', 10, 1 );
* Filter callback that applies auto-p to HTML email message body.
*/
function wpcf7_mail_html_body_autop( $body ) {
if ( wpcf7_autop_or_not() ) {
if ( wpcf7_autop_or_not( array( 'for' => 'mail' ) ) ) {
$body = wpcf7_autop( $body );
}
@@ -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;
}
@@ -281,17 +331,17 @@ class WPCF7_Mail {
/**
* Replaces mail-tags within the given text.
*/
public function replace_tags( $content, $args = '' ) {
if ( true === $args ) {
$args = array( 'html' => true );
public function replace_tags( $content, $options = '' ) {
if ( true === $options ) {
$options = array( 'html' => true );
}
$args = wp_parse_args( $args, array(
$options = wp_parse_args( $options, array(
'html' => false,
'exclude_blank' => false,
) );
return wpcf7_mail_replace_tags( $content, $args );
return wpcf7_mail_replace_tags( $content, $options );
}
@@ -341,18 +391,18 @@ class WPCF7_Mail {
* Replaces all mail-tags within the given text content.
*
* @param string $content Text including mail-tags.
* @param string|array $args Optional. Output options.
* @param string|array $options Optional. Output options.
* @return string Result of replacement.
*/
function wpcf7_mail_replace_tags( $content, $args = '' ) {
$args = wp_parse_args( $args, array(
function wpcf7_mail_replace_tags( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'exclude_blank' => false,
) );
if ( is_array( $content ) ) {
foreach ( $content as $key => $value ) {
$content[$key] = wpcf7_mail_replace_tags( $value, $args );
$content[$key] = wpcf7_mail_replace_tags( $value, $options );
}
return $content;
@@ -361,10 +411,10 @@ function wpcf7_mail_replace_tags( $content, $args = '' ) {
$content = explode( "\n", $content );
foreach ( $content as $num => $line ) {
$line = new WPCF7_MailTaggedText( $line, $args );
$line = new WPCF7_MailTaggedText( $line, $options );
$replaced = $line->replace_tags();
if ( $args['exclude_blank'] ) {
if ( $options['exclude_blank'] ) {
$replaced_tags = $line->get_replaced_tags();
if ( empty( $replaced_tags )
@@ -427,17 +477,17 @@ class WPCF7_MailTaggedText {
/**
* The constructor method.
*/
public function __construct( $content, $args = '' ) {
$args = wp_parse_args( $args, array(
public function __construct( $content, $options = '' ) {
$options = wp_parse_args( $options, array(
'html' => false,
'callback' => null,
) );
$this->html = (bool) $args['html'];
$this->html = (bool) $options['html'];
if ( null !== $args['callback']
and is_callable( $args['callback'] ) ) {
$this->callback = $args['callback'];
if ( null !== $options['callback']
and is_callable( $options['callback'] ) ) {
$this->callback = $options['callback'];
} elseif ( $this->html ) {
$this->callback = array( $this, 'replace_tags_callback_html' );
} else {
@@ -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;
}
}

View File

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

View File

@@ -355,6 +355,13 @@ class WPCF7_REST_Controller {
$request->get_param( '_wpcf7_unit_tag' )
);
if ( empty( $unit_tag ) ) {
return new WP_Error( 'wpcf7_unit_tag_not_found',
__( "There is no valid unit tag.", 'contact-form-7' ),
array( 'status' => 400 )
);
}
$result = $item->submit();
$response = array_merge( $result, array(

View File

@@ -28,10 +28,10 @@ class WPCF7_Submission {
/**
* Returns the singleton instance of this class.
*/
public static function get_instance( $contact_form = null, $args = '' ) {
public static function get_instance( $contact_form = null, $options = '' ) {
if ( $contact_form instanceof WPCF7_ContactForm ) {
if ( empty( self::$instance ) ) {
self::$instance = new self( $contact_form, $args );
self::$instance = new self( $contact_form, $options );
self::$instance->proceed();
return self::$instance;
} else {
@@ -58,13 +58,21 @@ class WPCF7_Submission {
/**
* Constructor.
*/
private function __construct( WPCF7_ContactForm $contact_form, $args = '' ) {
$args = wp_parse_args( $args, array(
private function __construct( WPCF7_ContactForm $contact_form, $options = '' ) {
$options = wp_parse_args( $options, array(
'skip_mail' => false,
) );
$this->contact_form = $contact_form;
$this->skip_mail = (bool) $args['skip_mail'];
$this->skip_mail = (bool) $options['skip_mail'];
}
/**
* Destructor.
*/
public function __destruct() {
$this->remove_uploaded_files();
}
@@ -124,8 +132,6 @@ class WPCF7_Submission {
do_action( 'wpcf7_mail_failed', $contact_form );
}
}
$this->remove_uploaded_files();
};
wpcf7_switch_locale( $this->contact_form->locale(), $callback );
@@ -202,15 +208,15 @@ class WPCF7_Submission {
/**
* Adds items to the array of submission result properties.
*
* @param string|array|object $args Value to add to result properties.
* @param string|array|object $data Value to add to result properties.
* @return array Added result properties.
*/
public function add_result_props( $args = '' ) {
$args = wp_parse_args( $args, array() );
public function add_result_props( $data = '' ) {
$data = wp_parse_args( $data, array() );
$this->result_props = array_merge( $this->result_props, $args );
$this->result_props = array_merge( $this->result_props, $data );
return $args;
return $data;
}
@@ -253,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;
}
@@ -279,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;
}
@@ -289,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;
@@ -336,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;
@@ -371,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_form_tag_supports( $tag->type, 'selectable-values' ) ) {
$value = ( '' === $value ) ? array() : (array) $value;
if ( WPCF7_USE_PIPE
and $pipes instanceof WPCF7_Pipes
and ! $pipes->zero() ) {
if ( is_array( $value_orig ) ) {
$value = array();
if ( WPCF7_USE_PIPE ) {
$pipes = $this->contact_form->get_pipes( $tag->name );
foreach ( $value_orig as $v ) {
$value[] = $pipes->do_pipe( $v );
}
} else {
$value = $pipes->do_pipe( $value_orig );
$value = array_map( static function ( $value ) use ( $pipes ) {
return $pipes->do_pipe( $value );
}, $value );
}
}
if ( wpcf7_form_tag_supports( $type, 'selectable-values' ) ) {
$value = (array) $value;
if ( $tag->has_option( 'free_text' )
and isset( $posted_data[$name . '_free_text'] ) ) {
$last_val = array_pop( $value );
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 = 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;
}
}
@@ -589,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 ) ) {
@@ -724,13 +667,13 @@ class WPCF7_Submission {
*
* @link https://contactform7.com/2019/05/31/why-is-this-message-marked-spam/
*/
public function add_spam_log( $args = '' ) {
$args = wp_parse_args( $args, array(
public function add_spam_log( $data = '' ) {
$data = wp_parse_args( $data, array(
'agent' => '',
'reason' => '',
) );
$this->spam_log[] = $args;
$this->spam_log[] = $data;
}
@@ -752,7 +695,7 @@ class WPCF7_Submission {
return true;
}
$nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
$nonce = $_POST['_wpnonce'] ?? '';
return wpcf7_verify_nonce( $nonce );
}
@@ -891,7 +834,7 @@ class WPCF7_Submission {
$file = $_FILES[$tag->name];
$args = array(
$options = array(
'tag' => $tag,
'name' => $tag->name,
'required' => $tag->is_required(),
@@ -900,7 +843,7 @@ class WPCF7_Submission {
'schema' => $this->contact_form->get_schema(),
);
$new_files = wpcf7_unship_uploaded_file( $file, $args );
$new_files = wpcf7_unship_uploaded_file( $file, $options );
if ( is_wp_error( $new_files ) ) {
$result->invalidate( $tag, $new_files );

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,191 @@
<?php
namespace Contactable\SWV;
use WP_Error;
/**
* The base class of SWV rules.
*/
abstract class Rule {
protected $properties = array();
public function __construct( $properties = '' ) {
$this->properties = wp_parse_args( $properties, array() );
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches( $context ) {
$field = $this->get_property( 'field' );
if ( ! empty( $context['field'] ) ) {
if ( $field and ! in_array( $field, (array) $context['field'], true ) ) {
return false;
}
}
return true;
}
/**
* Validates with this rule's logic.
*
* @param array $context Context.
*/
public function validate( $context ) {
return true;
}
/**
* Converts the properties to an array.
*
* @return array Array of properties.
*/
public function to_array() {
$properties = (array) $this->properties;
if ( defined( 'static::rule_name' ) and static::rule_name ) {
$properties = array( 'rule' => static::rule_name ) + $properties;
}
return $properties;
}
/**
* Returns the property value specified by the given property name.
*
* @param string $name Property name.
* @return mixed Property value.
*/
public function get_property( $name ) {
if ( isset( $this->properties[$name] ) ) {
return $this->properties[$name];
}
}
/**
* Returns the default user input value from $_POST.
*
* @return mixed Default user input value.
*/
public function get_default_input() {
$field = $this->get_property( 'field' );
if ( isset( $_POST[$field] ) ) {
return wp_unslash( $_POST[$field] );
}
return '';
}
/**
* Creates an error object. Returns false if the error property is omitted.
*/
protected function create_error() {
$error_code = defined( 'static::rule_name' )
? sprintf( 'swv_%s', static::rule_name )
: 'swv';
return new WP_Error(
$error_code,
(string) $this->get_property( 'error' ),
$this
);
}
}
/**
* The base class of SWV composite rules.
*/
abstract class CompositeRule extends Rule {
protected $rules = array();
/**
* Adds a sub-rule to this composite rule.
*
* @param Rule $rule Sub-rule to be added.
*/
public function add_rule( $rule ) {
if ( $rule instanceof Rule ) {
$this->rules[] = $rule;
}
}
/**
* Returns an iterator of sub-rules.
*/
public function rules() {
foreach ( $this->rules as $rule ) {
yield $rule;
}
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches( $context ) {
return true;
}
/**
* Validates with this rule's logic.
*
* @param array $context Context.
*/
public function validate( $context ) {
foreach ( $this->rules() as $rule ) {
if ( $rule->matches( $context ) ) {
$result = $rule->validate( $context );
if ( is_wp_error( $result ) ) {
return $result;
}
}
}
return true;
}
/**
* Converts the properties to an array.
*
* @return array Array of properties.
*/
public function to_array() {
$rules_arrays = array_map(
static function ( $rule ) {
return $rule->to_array();
},
$this->rules
);
return array_merge(
parent::to_array(),
array(
'rules' => $rules_arrays,
)
);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,6 +5,7 @@
require_once WPCF7_PLUGIN_DIR . '/includes/swv/schema-holder.php';
require_once WPCF7_PLUGIN_DIR . '/includes/swv/script-loader.php';
require_once WPCF7_PLUGIN_DIR . '/includes/swv/php/abstract-rules.php';
/**
@@ -12,27 +13,29 @@ require_once WPCF7_PLUGIN_DIR . '/includes/swv/script-loader.php';
*/
function wpcf7_swv_available_rules() {
$rules = array(
'required' => 'WPCF7_SWV_RequiredRule',
'requiredfile' => 'WPCF7_SWV_RequiredFileRule',
'email' => 'WPCF7_SWV_EmailRule',
'url' => 'WPCF7_SWV_URLRule',
'tel' => 'WPCF7_SWV_TelRule',
'number' => 'WPCF7_SWV_NumberRule',
'date' => 'WPCF7_SWV_DateRule',
'time' => 'WPCF7_SWV_TimeRule',
'file' => 'WPCF7_SWV_FileRule',
'enum' => 'WPCF7_SWV_EnumRule',
'dayofweek' => 'WPCF7_SWV_DayofweekRule',
'minitems' => 'WPCF7_SWV_MinItemsRule',
'maxitems' => 'WPCF7_SWV_MaxItemsRule',
'minlength' => 'WPCF7_SWV_MinLengthRule',
'maxlength' => 'WPCF7_SWV_MaxLengthRule',
'minnumber' => 'WPCF7_SWV_MinNumberRule',
'maxnumber' => 'WPCF7_SWV_MaxNumberRule',
'mindate' => 'WPCF7_SWV_MinDateRule',
'maxdate' => 'WPCF7_SWV_MaxDateRule',
'minfilesize' => 'WPCF7_SWV_MinFileSizeRule',
'maxfilesize' => 'WPCF7_SWV_MaxFileSizeRule',
'required' => 'Contactable\SWV\RequiredRule',
'requiredfile' => 'Contactable\SWV\RequiredFileRule',
'email' => 'Contactable\SWV\EmailRule',
'url' => 'Contactable\SWV\URLRule',
'tel' => 'Contactable\SWV\TelRule',
'number' => 'Contactable\SWV\NumberRule',
'date' => 'Contactable\SWV\DateRule',
'time' => 'Contactable\SWV\TimeRule',
'file' => 'Contactable\SWV\FileRule',
'enum' => 'Contactable\SWV\EnumRule',
'dayofweek' => 'Contactable\SWV\DayofweekRule',
'minitems' => 'Contactable\SWV\MinItemsRule',
'maxitems' => 'Contactable\SWV\MaxItemsRule',
'minlength' => 'Contactable\SWV\MinLengthRule',
'maxlength' => 'Contactable\SWV\MaxLengthRule',
'minnumber' => 'Contactable\SWV\MinNumberRule',
'maxnumber' => 'Contactable\SWV\MaxNumberRule',
'mindate' => 'Contactable\SWV\MinDateRule',
'maxdate' => 'Contactable\SWV\MaxDateRule',
'minfilesize' => 'Contactable\SWV\MinFileSizeRule',
'maxfilesize' => 'Contactable\SWV\MaxFileSizeRule',
'all' => 'Contactable\SWV\AllRule',
'any' => 'Contactable\SWV\AnyRule',
);
return apply_filters( 'wpcf7_swv_available_rules', $rules );
@@ -49,7 +52,7 @@ function wpcf7_swv_load_rules() {
foreach ( array_keys( $rules ) as $rule ) {
$file = sprintf( '%s.php', $rule );
$path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/rules', $file );
$path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/php/rules', $file );
if ( file_exists( $path ) ) {
include_once $path;
@@ -63,7 +66,7 @@ function wpcf7_swv_load_rules() {
*
* @param string $rule_name Rule name.
* @param string|array $properties Optional. Rule properties.
* @return WPCF7_SWV_Rule|null The rule object, or null if it failed.
* @return \Contactable\SWV\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 );
}
}
}
}

View File

@@ -253,18 +253,46 @@ function wpcf7_is_email_in_site_domain( $email ) {
* false otherwise.
*/
function wpcf7_is_file_path_in_content_dir( $path ) {
if ( $real_path = realpath( $path ) ) {
$path = $real_path;
} else {
if ( ! is_string( $path ) or '' === $path ) {
return false;
}
if ( 0 === strpos( $path, realpath( WP_CONTENT_DIR ) ) ) {
$callback = static function ( $path, $dir ) {
if ( $real_path = realpath( $path ) ) {
$path = $real_path;
} else {
return false;
}
if ( $real_dir = realpath( $dir ) ) {
$dir = trailingslashit( $real_dir );
} else {
return false;
}
return str_starts_with(
wp_normalize_path( $path ),
wp_normalize_path( $dir )
);
};
if (
call_user_func( $callback, $path, WP_CONTENT_DIR )
) {
return true;
}
if ( defined( 'UPLOADS' )
and 0 === strpos( $path, realpath( ABSPATH . UPLOADS ) ) ) {
if (
defined( 'UPLOADS' ) and
call_user_func( $callback, $path, ABSPATH . UPLOADS )
) {
return true;
}
if (
defined( 'WP_TEMP_DIR' ) and
call_user_func( $callback, $path, WP_TEMP_DIR )
) {
return true;
}