includes/mail-tag.php000064400000010142147206643610010565 0ustar00tag = $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; } } includes/css/styles-rtl.css000064400000000230147206643610012002 0ustar00.wpcf7-not-valid-tip { direction: rtl; } .use-floating-validation-tip .wpcf7-not-valid-tip { right: 1em; } .wpcf7-list-item { margin: 0 1em 0 0; } includes/css/styles.css000064400000005516147206643610011217 0ustar00.wpcf7 .screen-reader-response { position: absolute; overflow: hidden; clip: rect(1px, 1px, 1px, 1px); clip-path: inset(50%); height: 1px; width: 1px; margin: -1px; padding: 0; border: 0; word-wrap: normal !important; } .wpcf7 form .wpcf7-response-output { margin: 2em 0.5em 1em; padding: 0.2em 1em; border: 2px solid #00a0d2; /* Blue */ } .wpcf7 form.init .wpcf7-response-output, .wpcf7 form.resetting .wpcf7-response-output, .wpcf7 form.submitting .wpcf7-response-output { display: none; } .wpcf7 form.sent .wpcf7-response-output { border-color: #46b450; /* Green */ } .wpcf7 form.failed .wpcf7-response-output, .wpcf7 form.aborted .wpcf7-response-output { border-color: #dc3232; /* Red */ } .wpcf7 form.spam .wpcf7-response-output { border-color: #f56e28; /* Orange */ } .wpcf7 form.invalid .wpcf7-response-output, .wpcf7 form.unaccepted .wpcf7-response-output, .wpcf7 form.payment-required .wpcf7-response-output { border-color: #ffb900; /* Yellow */ } .wpcf7-form-control-wrap { position: relative; } .wpcf7-not-valid-tip { color: #dc3232; /* Red */ font-size: 1em; font-weight: normal; display: block; } .use-floating-validation-tip .wpcf7-not-valid-tip { position: relative; top: -2ex; left: 1em; z-index: 100; border: 1px solid #dc3232; background: #fff; padding: .2em .8em; width: 24em; } .wpcf7-list-item { display: inline-block; margin: 0 0 0 1em; } .wpcf7-list-item-label::before, .wpcf7-list-item-label::after { content: " "; } .wpcf7-spinner { visibility: hidden; display: inline-block; background-color: #23282d; /* Dark Gray 800 */ opacity: 0.75; width: 24px; height: 24px; border: none; border-radius: 100%; padding: 0; margin: 0 24px; position: relative; } form.submitting .wpcf7-spinner { visibility: visible; } .wpcf7-spinner::before { content: ''; position: absolute; background-color: #fbfbfc; /* Light Gray 100 */ top: 4px; left: 4px; width: 6px; height: 6px; border: none; border-radius: 100%; transform-origin: 8px 8px; animation-name: spin; animation-duration: 1000ms; animation-timing-function: linear; animation-iteration-count: infinite; } @media (prefers-reduced-motion: reduce) { .wpcf7-spinner::before { animation-name: blink; animation-duration: 2000ms; } } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes blink { from { opacity: 0; } 50% { opacity: 1; } to { opacity: 0; } } .wpcf7 [inert] { opacity: 0.5; } .wpcf7 input[type="file"] { cursor: pointer; } .wpcf7 input[type="file"]:disabled { cursor: default; } .wpcf7 .wpcf7-submit:disabled { cursor: not-allowed; } .wpcf7 input[type="url"], .wpcf7 input[type="email"], .wpcf7 input[type="tel"] { direction: ltr; } .wpcf7-reflection > output { display: list-item; list-style: none; } .wpcf7-reflection > output[hidden] { display: none; } includes/contact-form.php000064400000075126147206643610011503 0ustar00 array( 'name' => __( 'Contact Forms', 'contact-form-7' ), 'singular_name' => __( 'Contact Form', 'contact-form-7' ), ), 'rewrite' => false, 'query_var' => false, 'public' => false, 'capability_type' => 'page', 'capabilities' => array( 'edit_post' => 'wpcf7_edit_contact_form', 'read_post' => 'wpcf7_read_contact_form', 'delete_post' => 'wpcf7_delete_contact_form', 'edit_posts' => 'wpcf7_edit_contact_forms', 'edit_others_posts' => 'wpcf7_edit_contact_forms', 'publish_posts' => 'wpcf7_edit_contact_forms', 'read_private_posts' => 'wpcf7_edit_contact_forms', ), ) ); } /** * Retrieves contact form data that match given conditions. * * @param string|array $args Optional. Arguments to be passed to WP_Query. * @return array Array of WPCF7_ContactForm objects. */ public static function find( $args = '' ) { $defaults = array( 'post_status' => 'any', 'posts_per_page' => -1, 'offset' => 0, 'orderby' => 'ID', 'order' => 'ASC', ); $args = wp_parse_args( $args, $defaults ); $args['post_type'] = self::post_type; $q = new WP_Query(); $posts = $q->query( $args ); self::$found_items = $q->found_posts; $objs = array(); foreach ( (array) $posts as $post ) { $objs[] = new self( $post ); } return $objs; } /** * Returns a contact form data filled by default template contents. * * @param string|array $options Optional. Contact form options. * @return WPCF7_ContactForm A new contact form object. */ public static function get_template( $options = '' ) { $options = wp_parse_args( $options, array( 'locale' => null, 'title' => __( 'Untitled', 'contact-form-7' ), ) ); if ( ! isset( $options['locale'] ) ) { $options['locale'] = determine_locale(); } $callback = static function ( $options ) { $contact_form = new self; $contact_form->title = $options['title']; $contact_form->locale = $options['locale']; $properties = $contact_form->get_properties(); foreach ( $properties as $key => $value ) { $default_template = WPCF7_ContactFormTemplate::get_default( $key ); if ( isset( $default_template ) ) { $properties[$key] = $default_template; } } $contact_form->properties = $properties; return $contact_form; }; $contact_form = wpcf7_switch_locale( $options['locale'], $callback, $options ); self::$current = apply_filters( 'wpcf7_contact_form_default_pack', $contact_form, $options ); return self::$current; } /** * Creates a WPCF7_ContactForm object and sets it as the current instance. * * @param WPCF7_ContactForm|WP_Post|int $post Object or post ID. * @return WPCF7_ContactForm|null Contact form object. Null if unset. */ public static function get_instance( $post ) { $contact_form = null; if ( $post instanceof self ) { $contact_form = $post; } elseif ( ! empty( $post ) ) { $post = get_post( $post ); if ( isset( $post ) and self::post_type === get_post_type( $post ) ) { $contact_form = new self( $post ); } } return self::$current = $contact_form; } /** * Generates a "unit-tag" for the given contact form ID. * * @return string Unit-tag. */ private static function generate_unit_tag( $id = 0 ) { static $global_count = 0; $global_count += 1; if ( in_the_loop() ) { $unit_tag = sprintf( 'wpcf7-f%1$d-p%2$d-o%3$d', absint( $id ), get_the_ID(), $global_count ); } else { $unit_tag = sprintf( 'wpcf7-f%1$d-o%2$d', absint( $id ), $global_count ); } return $unit_tag; } /** * Constructor. */ private function __construct( $post = null ) { $post = get_post( $post ); if ( $post and self::post_type === get_post_type( $post ) ) { $this->id = $post->ID; $this->name = $post->post_name; $this->title = $post->post_title; $this->locale = get_post_meta( $post->ID, '_locale', true ); $this->hash = get_post_meta( $post->ID, '_hash', true ); $this->construct_properties( $post ); $this->upgrade(); } else { $this->construct_properties(); } do_action( 'wpcf7_contact_form', $this ); } /** * Magic method for property overloading. */ public function __get( $name ) { $message = __( '%1$s property of a WPCF7_ContactForm object is no longer accessible. Use %2$s method instead.', 'contact-form-7' ); if ( 'id' === $name ) { wp_trigger_error( '', sprintf( $message, 'id', 'id()' ), E_USER_DEPRECATED ); return $this->id; } elseif ( 'title' === $name ) { wp_trigger_error( '', sprintf( $message, 'title', 'title()' ), E_USER_DEPRECATED ); return $this->title; } elseif ( $prop = $this->prop( $name ) ) { wp_trigger_error( '', sprintf( $message, $name, 'prop(\'' . $name . '\')' ), E_USER_DEPRECATED ); return $prop; } } /** * Returns true if this contact form is not yet saved to the database. */ public function initial() { return empty( $this->id ); } /** * Constructs contact form properties. This is called only once * from the constructor. */ private function construct_properties( $post = null ) { $builtin_properties = array( 'form' => '', 'mail' => array(), 'mail_2' => array(), 'messages' => array(), 'additional_settings' => '', ); $properties = apply_filters( 'wpcf7_pre_construct_contact_form_properties', $builtin_properties, $this ); // Filtering out properties with invalid name $properties = array_filter( $properties, static function ( $key ) { $sanitized_key = sanitize_key( $key ); return $key === $sanitized_key; }, ARRAY_FILTER_USE_KEY ); foreach ( $properties as $name => $val ) { $prop = $this->retrieve_property( $name ); if ( isset( $prop ) ) { $properties[$name] = $prop; } } $this->properties = $properties; foreach ( $properties as $name => $val ) { $properties[$name] = apply_filters( "wpcf7_contact_form_property_{$name}", $val, $this ); } $this->properties = $properties; $properties = (array) apply_filters( 'wpcf7_contact_form_properties', $properties, $this ); $this->properties = $properties; } /** * Retrieves contact form property of the specified name from the database. * * @param string $name Property name. * @return array|string|null Property value. Null if property does not exist. */ private function retrieve_property( $name ) { $property = null; if ( ! $this->initial() ) { $post_id = $this->id; if ( metadata_exists( 'post', $post_id, '_' . $name ) ) { $property = get_post_meta( $post_id, '_' . $name, true ); } elseif ( metadata_exists( 'post', $post_id, $name ) ) { $property = get_post_meta( $post_id, $name, true ); } } return $property; } /** * Returns the value for the given property name. * * @param string $name Property name. * @return array|string|null Property value. Null if property does not exist. */ public function prop( $name ) { $props = $this->get_properties(); return isset( $props[$name] ) ? $props[$name] : null; } /** * Returns all the properties. * * @return array This contact form's properties. */ public function get_properties() { return (array) $this->properties; } /** * Updates properties. * * @param array $properties New properties. */ public function set_properties( $properties ) { $defaults = $this->get_properties(); $properties = wp_parse_args( $properties, $defaults ); $properties = array_intersect_key( $properties, $defaults ); $this->properties = $properties; } /** * Returns ID of this contact form. * * @return int The ID. */ public function id() { return $this->id; } /** * Returns unit-tag for this contact form. * * @return string Unit-tag. */ public function unit_tag() { return $this->unit_tag; } /** * Returns name (slug) of this contact form. * * @return string Name. */ public function name() { return $this->name; } /** * Returns title of this contact form. * * @return string Title. */ public function title() { return $this->title; } /** * Set a title for this contact form. * * @param string $title Title. */ public function set_title( $title ) { $title = strip_tags( $title ); $title = trim( $title ); if ( '' === $title ) { $title = __( 'Untitled', 'contact-form-7' ); } $this->title = $title; } /** * Returns the locale code of this contact form. * * @return string Locale code. Empty string if no valid locale is set. */ public function locale() { if ( wpcf7_is_valid_locale( $this->locale ) ) { return $this->locale; } else { return ''; } } /** * Sets a locale for this contact form. * * @param string $locale Locale code. */ public function set_locale( $locale ) { $locale = trim( $locale ); if ( wpcf7_is_valid_locale( $locale ) ) { $this->locale = $locale; } else { $this->locale = 'en_US'; } } /** * Retrieves the random hash string tied to this contact form. * * @param int $length Length of hash string. * @return string Hash string unique to this contact form. */ public function hash( $length = 7 ) { return substr( $this->hash, 0, absint( $length ) ); } /** * Returns the specified shortcode attribute value. * * @param string $name Shortcode attribute name. * @return string|null Attribute value. Null if the attribute does not exist. */ public function shortcode_attr( $name ) { if ( isset( $this->shortcode_atts[$name] ) ) { return (string) $this->shortcode_atts[$name]; } } /** * Returns true if this contact form is identical to the submitted one. */ public function is_posted() { if ( ! WPCF7_Submission::get_instance() ) { return false; } if ( empty( $_POST['_wpcf7_unit_tag'] ) ) { return false; } return $this->unit_tag() === $_POST['_wpcf7_unit_tag']; } /** * Generates HTML that represents a form. * * @param string|array $options Optional. Form options. * @return string HTML output. */ public function form_html( $options = '' ) { $options = wp_parse_args( $options, array( 'html_id' => '', 'html_name' => '', 'html_title' => '', 'html_class' => '', 'output' => 'form', ) ); $this->shortcode_atts = $options; if ( 'raw_form' == $options['output'] ) { return sprintf( '
%s
', esc_html( $this->prop( 'form' ) ) ); } if ( $this->is_true( 'subscribers_only' ) and ! current_user_can( 'wpcf7_submit', $this->id() ) ) { $notice = __( "This contact form is available only for logged in users.", 'contact-form-7' ); $notice = sprintf( '

%s

', esc_html( $notice ) ); return apply_filters( 'wpcf7_subscribers_only_notice', $notice, $this ); } $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( '

%1$s %2$s

', 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 ) ) { $lang_tag = $matches[1]; } $html = "\n" . sprintf( '
', wpcf7_format_atts( array( 'class' => 'wpcf7 no-js', 'id' => $this->unit_tag(), ( get_option( 'html_type' ) == 'text/html' ) ? 'lang' : 'xml:lang' => $lang_tag, 'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr', 'data-wpcf7-id' => $this->id(), ) ) ); $html .= "\n" . $this->screen_reader_response() . "\n"; $id_attr = apply_filters( 'wpcf7_form_id_attr', preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_id'] ) ); $name_attr = apply_filters( 'wpcf7_form_name_attr', preg_replace( '/[^A-Za-z0-9:._-]/', '', $options['html_name'] ) ); $title_attr = apply_filters( 'wpcf7_form_title_attr', $options['html_title'] ); $class = 'wpcf7-form'; if ( $this->is_posted() ) { $submission = WPCF7_Submission::get_instance(); $data_status_attr = $this->form_status_class_name( $submission->get_status() ); $class .= sprintf( ' %s', $data_status_attr ); } else { $data_status_attr = 'init'; $class .= ' init'; } if ( $options['html_class'] ) { $class .= ' ' . $options['html_class']; } if ( $this->in_demo_mode() ) { $class .= ' demo'; } $class = explode( ' ', $class ); $class = array_map( 'sanitize_html_class', $class ); $class = array_filter( $class ); $class = array_unique( $class ); $class = implode( ' ', $class ); $class = apply_filters( 'wpcf7_form_class_attr', $class ); $enctype = wpcf7_enctype_value( apply_filters( 'wpcf7_form_enctype', '' ) ); $autocomplete = apply_filters( 'wpcf7_form_autocomplete', '' ); $atts = array( 'action' => esc_url( $action_url ), 'method' => 'post', 'class' => ( '' !== $class ) ? $class : null, 'id' => ( '' !== $id_attr ) ? $id_attr : null, 'name' => ( '' !== $name_attr ) ? $name_attr : null, 'aria-label' => ( '' !== $title_attr ) ? $title_attr : __( 'Contact form', 'contact-form-7' ), 'enctype' => ( '' !== $enctype ) ? $enctype : null, 'autocomplete' => ( '' !== $autocomplete ) ? $autocomplete : null, 'novalidate' => true, 'data-status' => $data_status_attr, ); $atts += (array) apply_filters( 'wpcf7_form_additional_atts', array() ); $html .= sprintf( '
', wpcf7_format_atts( $atts ) ) . "\n"; $html .= $this->form_hidden_fields(); $html .= $this->form_elements(); if ( ! $this->responses_count ) { $html .= $this->form_response_output(); } $html .= "\n" . '
'; $html .= "\n" . '
'; return $html . "\n"; } /** * Returns the class name that matches the given form status. */ private function form_status_class_name( $status ) { switch ( $status ) { case 'init': $class = 'init'; break; case 'validation_failed': $class = 'invalid'; break; case 'acceptance_missing': $class = 'unaccepted'; break; case 'spam': $class = 'spam'; break; case 'aborted': $class = 'aborted'; break; case 'mail_sent': $class = 'sent'; break; case 'mail_failed': $class = 'failed'; break; default: $class = sprintf( 'custom-%s', preg_replace( '/[^0-9a-z]+/i', '-', $status ) ); } return $class; } /** * Returns a set of hidden fields. */ private function form_hidden_fields() { $hidden_fields = array( '_wpcf7' => $this->id(), '_wpcf7_version' => WPCF7_VERSION, '_wpcf7_locale' => $this->locale(), '_wpcf7_unit_tag' => $this->unit_tag(), '_wpcf7_container_post' => 0, '_wpcf7_posted_data_hash' => '', ); if ( in_the_loop() ) { $hidden_fields['_wpcf7_container_post'] = (int) get_the_ID(); } if ( $this->nonce_is_active() and is_user_logged_in() ) { $hidden_fields['_wpnonce'] = wpcf7_create_nonce(); } $hidden_fields += (array) apply_filters( 'wpcf7_form_hidden_fields', array() ); $content = ''; foreach ( $hidden_fields as $name => $value ) { $content .= sprintf( '', esc_attr( $name ), esc_attr( $value ) ) . "\n"; } return '
' . "\n" . $content . '
' . "\n"; } /** * Returns the visible response output for a form submission. */ public function form_response_output() { $status = 'init'; $class = 'wpcf7-response-output'; $content = ''; if ( $this->is_posted() ) { // Post response output for non-AJAX $submission = WPCF7_Submission::get_instance(); $status = $submission->get_status(); $content = $submission->get_response(); } $atts = array( 'class' => trim( $class ), 'aria-hidden' => 'true', ); $output = sprintf( '
%2$s
', wpcf7_format_atts( $atts ), esc_html( $content ) ); $output = apply_filters( 'wpcf7_form_response_output', $output, $class, $content, $this, $status ); $this->responses_count += 1; return $output; } /** * Returns the response output that is only accessible from screen readers. */ public function screen_reader_response() { $primary_response = ''; $validation_errors = array(); if ( $this->is_posted() ) { // Post response output for non-AJAX $submission = WPCF7_Submission::get_instance(); $primary_response = $submission->get_response(); if ( $invalid_fields = $submission->get_invalid_fields() ) { foreach ( (array) $invalid_fields as $name => $field ) { $list_item = esc_html( $field['reason'] ); if ( $field['idref'] ) { $list_item = sprintf( '%2$s', esc_attr( $field['idref'] ), $list_item ); } $validation_error_id = wpcf7_get_validation_error_reference( $name, $this->unit_tag() ); if ( $validation_error_id ) { $list_item = sprintf( '
  • %2$s
  • ', esc_attr( $validation_error_id ), $list_item ); $validation_errors[] = $list_item; } } } } $primary_response = sprintf( '

    %s

    ', esc_html( $primary_response ) ); $validation_errors = sprintf( '', implode( "\n", $validation_errors ) ); $output = sprintf( '
    %1$s %2$s
    ', $primary_response, $validation_errors ); return $output; } /** * Returns a validation error for the specified input field. * * @param string $name Input field name. */ public function validation_error( $name ) { $error = ''; if ( $this->is_posted() ) { $submission = WPCF7_Submission::get_instance(); if ( $invalid_field = $submission->get_invalid_field( $name ) ) { $error = trim( $invalid_field['reason'] ); } } if ( ! $error ) { return $error; } $atts = array( 'class' => 'wpcf7-not-valid-tip', 'aria-hidden' => 'true', ); $error = sprintf( '%2$s', wpcf7_format_atts( $atts ), esc_html( $error ) ); return apply_filters( 'wpcf7_validation_error', $error, $name, $this ); } /** * Replaces all form-tags in the form template with corresponding HTML. * * @return string Replaced form content. */ public function replace_all_form_tags() { $manager = WPCF7_FormTagsManager::get_instance(); $form = $this->prop( 'form' ); if ( wpcf7_autop_or_not() ) { $form = $manager->replace_with_placeholders( $form ); $form = wpcf7_autop( $form ); $form = $manager->restore_from_placeholders( $form ); } $form = $manager->replace_all( $form ); $this->scanned_form_tags = $manager->get_scanned_tags(); return $form; } /** * Replaces all form-tags in the form template with corresponding HTML. * * @deprecated 4.6 Use replace_all_form_tags() * * @return string Replaced form content. */ public function form_do_shortcode() { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_ContactForm::replace_all_form_tags' ); return $this->replace_all_form_tags(); } /** * Scans form-tags from the form template. * * @param string|array|null $cond Optional. Filters. Default null. * @return array Form-tags matching the given filter conditions. */ public function scan_form_tags( $cond = null ) { $manager = WPCF7_FormTagsManager::get_instance(); if ( empty( $this->scanned_form_tags ) ) { $this->scanned_form_tags = $manager->scan( $this->prop( 'form' ) ); } $tags = $this->scanned_form_tags; return $manager->filter( $tags, $cond ); } /** * Scans form-tags from the form template. * * @deprecated 4.6 Use scan_form_tags() * * @param string|array|null $cond Optional. Filters. Default null. * @return array Form-tags matching the given filter conditions. */ public function form_scan_shortcode( $cond = null ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_ContactForm::scan_form_tags' ); return $this->scan_form_tags( $cond ); } /** * Replaces all form-tags in the form template with corresponding HTML. * * @return string Replaced form content. wpcf7_form_elements filters applied. */ public function form_elements() { return apply_filters( 'wpcf7_form_elements', $this->replace_all_form_tags() ); } /** * Collects mail-tags available for this contact form. * * @param string|array $options Optional. Search options. * @return array Mail-tag names. */ public function collect_mail_tags( $options = '' ) { $manager = WPCF7_FormTagsManager::get_instance(); $options = wp_parse_args( $options, array( 'include' => array(), 'exclude' => $manager->collect_tag_types( 'not-for-mail' ), ) ); $tags = $this->scan_form_tags(); $mailtags = array(); foreach ( (array) $tags as $tag ) { $type = $tag->basetype; if ( empty( $type ) ) { continue; } elseif ( ! empty( $options['include'] ) ) { if ( ! in_array( $type, $options['include'] ) ) { continue; } } elseif ( ! empty( $options['exclude'] ) ) { if ( in_array( $type, $options['exclude'] ) ) { continue; } } $mailtags[] = $tag->name; } $mailtags = array_unique( $mailtags ); $mailtags = array_filter( $mailtags ); $mailtags = array_values( $mailtags ); return apply_filters( 'wpcf7_collect_mail_tags', $mailtags, $options, $this ); } /** * Prints a mail-tag suggestion list. * * @param string $template_name Optional. Mail template name. Default 'mail'. */ public function suggest_mail_tags( $template_name = 'mail' ) { $mail = wp_parse_args( $this->prop( $template_name ), array( 'active' => false, 'recipient' => '', 'sender' => '', 'subject' => '', 'body' => '', 'additional_headers' => '', 'attachments' => '', 'use_html' => false, 'exclude_blank' => false, ) ); $mail = array_filter( $mail ); foreach ( (array) $this->collect_mail_tags() as $mail_tag ) { $pattern = sprintf( '/\[(_[a-z]+_)?%s([ \t]+[^]]+)?\]/', preg_quote( $mail_tag, '/' ) ); $used = preg_grep( $pattern, $mail ); echo sprintf( '[%2$s]', 'mailtag code ' . ( $used ? 'used' : 'unused' ), esc_html( $mail_tag ) ); } } /** * Submits this contact form. * * @param string|array $options Optional. Submission options. Default empty. * @return array Result of submission. */ public function submit( $options = '' ) { $options = wp_parse_args( $options, array( 'skip_mail' => ( $this->in_demo_mode() || $this->is_true( 'skip_mail' ) || ! empty( $this->skip_mail ) ), ) ); if ( $this->is_true( 'subscribers_only' ) and ! current_user_can( 'wpcf7_submit', $this->id() ) ) { $result = array( 'contact_form_id' => $this->id(), 'status' => 'error', 'message' => __( "This contact form is available only for logged in users.", 'contact-form-7' ), ); return $result; } $submission = WPCF7_Submission::get_instance( $this, array( 'skip_mail' => $options['skip_mail'], ) ); $result = array( 'contact_form_id' => $this->id(), ); $result += $submission->get_result(); if ( $this->in_demo_mode() ) { $result['demo_mode'] = true; } do_action( 'wpcf7_submit', $this, $result ); return $result; } /** * Returns message used for given status. * * @param string $status Status. * @param bool $filter Optional. Whether filters are applied. Default true. * @return string Message. */ public function message( $status, $filter = true ) { $messages = $this->prop( 'messages' ); $message = isset( $messages[$status] ) ? $messages[$status] : ''; if ( $filter ) { $message = $this->filter_message( $message, $status ); } return $message; } /** * Filters a message. * * @param string $message Message to filter. * @param string $status Optional. Status. Default empty. * @return string Filtered message. */ public function filter_message( $message, $status = '' ) { $message = wpcf7_mail_replace_tags( $message ); $message = apply_filters( 'wpcf7_display_message', $message, $status ); $message = wp_strip_all_tags( $message ); return $message; } /** * Returns the additional setting value searched by name. * * @param string $name Name of setting. * @return string Additional setting value. */ public function pref( $name ) { $settings = $this->additional_setting( $name ); if ( $settings ) { return $settings[0]; } } /** * Returns additional setting values searched by name. * * @param string $name Name of setting. * @param int $max Maximum result item count. * @return array Additional setting values. */ public function additional_setting( $name, $max = 1 ) { $settings = (array) explode( "\n", $this->prop( 'additional_settings' ) ); $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/'; $count = 0; $values = array(); foreach ( $settings as $setting ) { if ( preg_match( $pattern, $setting, $matches ) ) { if ( $matches[1] != $name ) { continue; } if ( ! $max or $count < (int) $max ) { $values[] = trim( $matches[2] ); $count += 1; } } } return $values; } /** * Returns true if the specified setting has a truthy string value. * * @param string $name Name of setting. * @return bool True if the setting value is 'on', 'true', or '1'. */ public function is_true( $name ) { return in_array( $this->pref( $name ), array( 'on', 'true', '1' ), true ); } /** * Returns true if this contact form is in the demo mode. */ public function in_demo_mode() { return $this->is_true( 'demo_mode' ); } /** * Returns true if nonce is active for this contact form. */ public function nonce_is_active() { $is_active = WPCF7_VERIFY_NONCE; if ( $this->is_true( 'subscribers_only' ) ) { $is_active = true; } return (bool) apply_filters( 'wpcf7_verify_nonce', $is_active, $this ); } /** * Returns true if the specified setting has a falsey string value. * * @param string $name Name of setting. * @return bool True if the setting value is 'off', 'false', or '0'. */ public function is_false( $name ) { return in_array( $this->pref( $name ), array( 'off', 'false', '0' ), true ); } /** * Upgrades this contact form properties. */ private function upgrade() { $mail = $this->prop( 'mail' ); if ( is_array( $mail ) and ! isset( $mail['recipient'] ) ) { $mail['recipient'] = get_option( 'admin_email' ); } $this->properties['mail'] = $mail; $messages = $this->prop( 'messages' ); if ( is_array( $messages ) ) { foreach ( wpcf7_messages() as $key => $arr ) { if ( ! isset( $messages[$key] ) ) { $messages[$key] = $arr['default']; } } } $this->properties['messages'] = $messages; } /** * Stores this contact form properties to the database. * * @return int The post ID on success. The value 0 on failure. */ public function save() { $title = wp_slash( $this->title ); $props = wp_slash( $this->get_properties() ); $post_content = implode( "\n", wpcf7_array_flatten( $props ) ); if ( $this->initial() ) { $post_id = wp_insert_post( array( 'post_type' => self::post_type, 'post_status' => 'publish', 'post_title' => $title, 'post_content' => trim( $post_content ), ) ); } else { $post_id = wp_update_post( array( 'ID' => (int) $this->id, 'post_status' => 'publish', 'post_title' => $title, 'post_content' => trim( $post_content ), ) ); } if ( $post_id ) { foreach ( $props as $prop => $value ) { update_post_meta( $post_id, '_' . $prop, wpcf7_normalize_newline_deep( $value ) ); } if ( wpcf7_is_valid_locale( $this->locale ) ) { update_post_meta( $post_id, '_locale', $this->locale ); } add_post_meta( $post_id, '_hash', wpcf7_generate_contact_form_hash( $post_id ), true // Unique ); if ( $this->initial() ) { $this->id = $post_id; do_action( 'wpcf7_after_create', $this ); } else { do_action( 'wpcf7_after_update', $this ); } do_action( 'wpcf7_after_save', $this ); } return $post_id; } /** * Makes a copy of this contact form. * * @return WPCF7_ContactForm New contact form object. */ public function copy() { $new = new self; $new->title = $this->title . '_copy'; $new->locale = $this->locale; $new->properties = $this->properties; return apply_filters( 'wpcf7_copy', $new, $this ); } /** * Deletes this contact form. * * @return bool True if deletion succeeded, false otherwise. */ public function delete() { if ( $this->initial() ) { return false; } if ( wp_delete_post( $this->id, true ) ) { $this->id = 0; return true; } return false; } /** * Returns a WordPress shortcode for this contact form. */ public function shortcode( $options = '' ) { $options = wp_parse_args( $options, array( 'use_old_format' => false ) ); $title = str_replace( array( '"', '[', ']' ), '', $this->title ); if ( $options['use_old_format'] ) { $old_unit_id = (int) get_post_meta( $this->id, '_old_cf7_unit_id', true ); if ( $old_unit_id ) { $shortcode = sprintf( '[contact-form %1$d "%2$s"]', $old_unit_id, $title ); } else { $shortcode = ''; } } else { $shortcode = sprintf( '[contact-form-7 id="%1$s" title="%2$s"]', $this->hash(), $title ); } return apply_filters( 'wpcf7_contact_form_shortcode', $shortcode, $options, $this ); } } includes/pipe.php000064400000005104147206643610010031 0ustar00before = $this->after = trim( $text ); } else { $this->before = trim( substr( $text, 0, $pipe_pos ) ); $this->after = trim( substr( $text, $pipe_pos + 1 ) ); } } } /** * Class representing a list of pipes. */ class WPCF7_Pipes { private $pipes = array(); public function __construct( array $texts = null ) { foreach ( (array) $texts as $text ) { $this->add_pipe( $text ); } } private function add_pipe( $text ) { $pipe = new WPCF7_Pipe( $text ); $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', ) ); foreach ( $this->pipes as $pipe ) { $before_canonical = wpcf7_canonicalize( $pipe->before, array( 'strto' => 'as-is', ) ); if ( $input_canonical === $before_canonical ) { return $pipe->after; } } return $input; } public function collect_befores() { $befores = array(); foreach ( $this->pipes as $pipe ) { $befores[] = $pipe->before; } return $befores; } public function collect_afters() { $afters = array(); foreach ( $this->pipes as $pipe ) { $afters[] = $pipe->after; } return $afters; } public function zero() { return empty( $this->pipes ); } public function random_pipe() { if ( $this->zero() ) { return null; } return $this->pipes[array_rand( $this->pipes )]; } public function to_array() { return array_map( static function ( WPCF7_Pipe $pipe ) { return array( $pipe->before, $pipe->after, ); }, $this->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(); } } includes/contact-form-functions.php000064400000024401147206643610013477 0ustar00postmeta WHERE meta_key = '_old_cf7_unit_id'" . $wpdb->prepare( " AND meta_value = %d", $old_id ); if ( $new_id = $wpdb->get_var( $q ) ) { return wpcf7_contact_form( $new_id ); } } /** * Searches for a contact form by a hash string. * * @param string $hash Part of a hash string. * @return WPCF7_ContactForm Contact form object. */ function wpcf7_get_contact_form_by_hash( $hash ) { global $wpdb; $hash = trim( $hash ); if ( strlen( $hash ) < 7 ) { return null; } $like = $wpdb->esc_like( $hash ) . '%'; $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_hash'" . $wpdb->prepare( " AND meta_value LIKE %s", $like ); if ( $post_id = $wpdb->get_var( $q ) ) { return wpcf7_contact_form( $post_id ); } } /** * Searches for a contact form by title. * * @param string $title Title of contact form. * @return WPCF7_ContactForm|null Contact form object if found, null otherwise. */ function wpcf7_get_contact_form_by_title( $title ) { if ( ! is_string( $title ) or '' === $title ) { return null; } $contact_forms = WPCF7_ContactForm::find( array( 'title' => $title, 'posts_per_page' => 1, ) ); if ( $contact_forms ) { return wpcf7_contact_form( reset( $contact_forms ) ); } } /** * Wrapper function of WPCF7_ContactForm::get_current(). * * @return WPCF7_ContactForm Contact form object. */ function wpcf7_get_current_contact_form() { if ( $current = WPCF7_ContactForm::get_current() ) { return $current; } } /** * Returns true if it is in the state that a non-Ajax submission is accepted. */ function wpcf7_is_posted() { if ( ! $contact_form = wpcf7_get_current_contact_form() ) { return false; } return $contact_form->is_posted(); } /** * Retrieves the user input value through a non-Ajax submission. * * @param string $name Name of form control. * @param string $default_value Optional default value. * @return string The user input value through the form-control. */ function wpcf7_get_hangover( $name, $default_value = null ) { if ( ! wpcf7_is_posted() ) { return $default_value; } $submission = WPCF7_Submission::get_instance(); if ( ! $submission or $submission->is( 'mail_sent' ) ) { return $default_value; } return isset( $_POST[$name] ) ? wp_unslash( $_POST[$name] ) : $default_value; } /** * Retrieves an HTML snippet of validation error on the given form control. * * @param string $name Name of form control. * @return string Validation error message in a form of HTML snippet. */ function wpcf7_get_validation_error( $name ) { if ( ! $contact_form = wpcf7_get_current_contact_form() ) { return ''; } return $contact_form->validation_error( $name ); } /** * Returns a reference key to a validation error message. * * @param string $name Name of form control. * @param string $unit_tag Optional. Unit tag of the contact form. * @return string Reference key code. */ function wpcf7_get_validation_error_reference( $name, $unit_tag = '' ) { if ( '' === $unit_tag ) { $contact_form = wpcf7_get_current_contact_form(); if ( $contact_form and $contact_form->validation_error( $name ) ) { $unit_tag = $contact_form->unit_tag(); } else { return null; } } return preg_replace( '/[^0-9a-z_-]+/i', '', sprintf( '%1$s-ve-%2$s', $unit_tag, $name ) ); } /** * Retrieves a message for the given status. */ function wpcf7_get_message( $status ) { if ( ! $contact_form = wpcf7_get_current_contact_form() ) { return ''; } return $contact_form->message( $status ); } /** * Returns a class names list for a form-tag of the specified type. * * @param string $type Form-tag type. * @param string $default_classes Optional default classes. * @return string Whitespace-separated list of class names. */ function wpcf7_form_controls_class( $type, $default_classes = '' ) { $type = trim( $type ); if ( is_string( $default_classes ) ) { $default_classes = explode( ' ', $default_classes ); } $classes = array( 'wpcf7-form-control', sprintf( 'wpcf7-%s', rtrim( $type, '*' ) ), ); if ( str_ends_with( $type, '*' ) ) { $classes[] = 'wpcf7-validates-as-required'; } $classes = array_merge( $classes, $default_classes ); $classes = array_filter( array_unique( $classes ) ); return implode( ' ', $classes ); } /** * Callback function for the contact-form-7 shortcode. */ function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) { if ( is_feed() ) { return '[contact-form-7]'; } if ( 'contact-form-7' === $code ) { $atts = shortcode_atts( array( 'id' => '', 'title' => '', 'html_id' => '', 'html_name' => '', 'html_title' => '', 'html_class' => '', 'output' => 'form', ), $atts, 'wpcf7' ); $id = trim( $atts['id'] ); $title = trim( $atts['title'] ); $contact_form = wpcf7_get_contact_form_by_hash( $id ); if ( ! $contact_form ) { $contact_form = wpcf7_contact_form( $id ); } if ( ! $contact_form ) { $contact_form = wpcf7_get_contact_form_by_title( $title ); } } else { if ( is_string( $atts ) ) { $atts = explode( ' ', $atts, 2 ); } $id = (int) array_shift( $atts ); $contact_form = wpcf7_get_contact_form_by_old_id( $id ); } if ( ! $contact_form ) { return sprintf( '

    %1$s %2$s

    ', esc_html( __( 'Error:', 'contact-form-7' ) ), esc_html( __( "Contact form not found.", 'contact-form-7' ) ) ); } $callback = static function ( $contact_form, $atts ) { return $contact_form->form_html( $atts ); }; $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( $data = '', $context = 'save' ) { $data = wp_parse_args( $data, array( 'id' => -1, 'title' => null, 'locale' => null, 'form' => null, 'mail' => null, 'mail_2' => null, 'messages' => null, 'additional_settings' => null, ) ); $data = wp_unslash( $data ); $data['id'] = (int) $data['id']; if ( -1 == $data['id'] ) { $contact_form = WPCF7_ContactForm::get_template(); } else { $contact_form = wpcf7_contact_form( $data['id'] ); } if ( empty( $contact_form ) ) { return false; } if ( null !== $data['title'] ) { $contact_form->set_title( $data['title'] ); } if ( null !== $data['locale'] ) { $contact_form->set_locale( $data['locale'] ); } $properties = array(); if ( null !== $data['form'] ) { $properties['form'] = wpcf7_sanitize_form( $data['form'] ); } if ( null !== $data['mail'] ) { $properties['mail'] = wpcf7_sanitize_mail( $data['mail'] ); $properties['mail']['active'] = true; } if ( null !== $data['mail_2'] ) { $properties['mail_2'] = wpcf7_sanitize_mail( $data['mail_2'] ); } if ( null !== $data['messages'] ) { $properties['messages'] = wpcf7_sanitize_messages( $data['messages'] ); } if ( null !== $data['additional_settings'] ) { $properties['additional_settings'] = wpcf7_sanitize_additional_settings( $data['additional_settings'] ); } $contact_form->set_properties( $properties ); do_action( 'wpcf7_save_contact_form', $contact_form, $data, $context ); if ( 'save' == $context ) { $contact_form->save(); } return $contact_form; } /** * Sanitizes the form property data. */ function wpcf7_sanitize_form( $input, $default_template = '' ) { if ( null === $input ) { return $default_template; } $output = trim( $input ); if ( ! current_user_can( 'unfiltered_html' ) ) { $output = wpcf7_kses( $output, 'form' ); } return $output; } /** * Sanitizes the mail property data. */ function wpcf7_sanitize_mail( $input, $defaults = array() ) { $input = wp_parse_args( $input, array( 'active' => false, 'subject' => '', 'sender' => '', 'recipient' => '', 'body' => '', 'additional_headers' => '', 'attachments' => '', 'use_html' => false, 'exclude_blank' => false, ) ); $input = wp_parse_args( $input, $defaults ); $output = array(); $output['active'] = (bool) $input['active']; $output['subject'] = trim( $input['subject'] ); $output['sender'] = trim( $input['sender'] ); $output['recipient'] = trim( $input['recipient'] ); $output['body'] = trim( $input['body'] ); if ( ! current_user_can( 'unfiltered_html' ) ) { $output['body'] = wpcf7_kses( $output['body'], 'mail' ); } $output['additional_headers'] = ''; $headers = str_replace( "\r\n", "\n", $input['additional_headers'] ); $headers = explode( "\n", $headers ); foreach ( $headers as $header ) { $header = trim( $header ); if ( '' !== $header ) { $output['additional_headers'] .= $header . "\n"; } } $output['additional_headers'] = trim( $output['additional_headers'] ); $output['attachments'] = trim( $input['attachments'] ); $output['use_html'] = (bool) $input['use_html']; $output['exclude_blank'] = (bool) $input['exclude_blank']; return $output; } /** * Sanitizes the messages property data. */ function wpcf7_sanitize_messages( $input, $defaults = array() ) { $output = array(); foreach ( wpcf7_messages() as $key => $val ) { if ( isset( $input[$key] ) ) { $output[$key] = trim( $input[$key] ); } elseif ( isset( $defaults[$key] ) ) { $output[$key] = $defaults[$key]; } } return $output; } /** * Sanitizes the additional settings property data. */ function wpcf7_sanitize_additional_settings( $input, $default_template = '' ) { if ( null === $input ) { return $default_template; } $output = trim( $input ); return $output; } /** * Generates a random hash string for a contact form. * * @param int $post_id Post ID. * @return string SHA-1 hash. */ function wpcf7_generate_contact_form_hash( $post_id ) { return sha1( implode( '|', array( get_current_user_id(), $post_id, time(), home_url(), ) ) ); } includes/shortcodes.php000064400000005226147206643610011256 0ustar00get_scanned_tags(); } public function add_shortcode( $tag, $callback, $has_name = false ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_FormTagsManager::add' ); return self::$form_tags_manager->add( $tag, $callback, $has_name ); } public function remove_shortcode( $tag ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_FormTagsManager::remove' ); return self::$form_tags_manager->remove( $tag ); } public function normalize_shortcode( $content ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_FormTagsManager::normalize' ); return self::$form_tags_manager->normalize( $content ); } public function do_shortcode( $content, $exec = true ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_FormTagsManager::replace_all' ); if ( $exec ) { return self::$form_tags_manager->replace_all( $content ); } else { return self::$form_tags_manager->scan( $content ); } } public function scan_shortcode( $content ) { wpcf7_deprecated_function( __METHOD__, '4.6', 'WPCF7_FormTagsManager::scan' ); return self::$form_tags_manager->scan( $content ); } } class WPCF7_Shortcode extends WPCF7_FormTag { public function __construct( $tag ) { wpcf7_deprecated_function( 'WPCF7_Shortcode', '4.6', 'WPCF7_FormTag' ); parent::__construct( $tag ); } } includes/config-validator/messages.php000064400000002230147206643610014130 0ustar00contact_form->prop( 'messages' ); if ( ! $messages ) { return; } 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 ); 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' ); } } } } /** * Detects errors of HTML uses in a message. * * @link https://contactform7.com/configuration-errors/html-in-message/ */ public function detect_html_in_message( $section, $content ) { $stripped = wp_strip_all_tags( $content ); if ( $stripped !== $content ) { return true; } return false; } } includes/config-validator/additional-settings.php000064400000001306147206643610016272 0ustar00supports( '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' ); } } } } includes/config-validator/actions.php000064400000001203147206643610013760 0ustar00 '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(); } } } includes/config-validator/mail.php000064400000034377147206643610013264 0ustar00 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_callback( $matches ) { // 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(); $example_email = 'example@example.com'; $example_text = 'example'; $example_blank = ''; // 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; } 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; } } /** * Runs error detection for the mail sections. */ public function validate_mail( $template = 'mail' ) { if ( $this->contact_form->is_true( 'demo_mode' ) or $this->contact_form->is_true( 'skip_mail' ) ) { return; } $components = (array) $this->contact_form->prop( $template ); if ( ! $components ) { return; } if ( 'mail' !== $template and empty( $components['active'] ) ) { return; } $components = wp_parse_args( $components, array( 'subject' => '', 'sender' => '', 'recipient' => '', 'additional_headers' => '', 'body' => '', 'attachments' => '', ) ); $this->validate_mail_subject( $template, $components['subject'] ); $this->validate_mail_sender( $template, $components['sender'] ); $this->validate_mail_recipient( $template, $components['recipient'] ); $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' ); } } if ( $this->supports( 'email_not_in_site_domain' ) ) { $this->remove_error( $section, 'email_not_in_site_domain' ); if ( ! $this->has_error( $section, 'invalid_mailbox_syntax' ) ) { $sender = $this->replace_mail_tags( $content ); $sender = wpcf7_strip_newline( $sender ); 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' ), ) ); } } } } /** * Runs error detection for the mail recipient section. */ public function validate_mail_recipient( $template, $content ) { $section = sprintf( '%s.recipient', $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' ); } } 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; } $is_valid_header = preg_match( '/^([0-9A-Za-z-]+):(.*)$/', $header, $matches ); 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 ( $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' ); } } 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' ); } } 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' ); } } } /** * 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( array( 'type' => array( 'file', 'file*' ) ) ); foreach ( $tags as $tag ) { $name = $tag->name; if ( ! str_contains( $content, "[{$name}]" ) ) { continue; } $limit = (int) $tag->get_limit_option(); if ( empty( $attachables[$name] ) or $attachables[$name] < $limit ) { $attachables[$name] = $limit; } } $total_size = array_sum( $attachables ); foreach ( explode( "\n", $content ) as $line ) { $line = trim( $line ); if ( '' === $line or str_starts_with( $line, '[' ) ) { continue; } 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( $section, 'attachments_overweight', array( 'message' => __( "The total size of attachment files is too large.", 'contact-form-7' ), ) ); } else { $this->remove_error( $section, 'attachments_overweight' ); } } } /** * Detects errors of invalid mailbox syntax. * * @link https://contactform7.com/configuration-errors/invalid-mailbox-syntax/ */ 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 true; } return false; } /** * Detects errors of empty message fields. * * @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 true; } return false; } /** * Detects errors of nonexistent attachment files. * * @link https://contactform7.com/configuration-errors/file-not-found/ */ public function detect_file_not_found( $section, $content ) { $path = path_join( WP_CONTENT_DIR, $content ); if ( ! is_readable( $path ) or ! is_file( $path ) ) { return true; } return false; } /** * Detects errors of attachment files out of the content directory. * * @link https://contactform7.com/configuration-errors/file-not-in-content-dir/ */ public function detect_file_not_in_content_dir( $section, $content ) { $path = path_join( WP_CONTENT_DIR, $content ); if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) { 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; } } includes/config-validator/form.php000064400000016456147206643610013303 0ustar00contact_form->prop( '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 PHP’s maximum acceptable size.", 'contact-form-7' ), ) ); } else { $this->remove_error( $section, 'upload_filesize_overlimit' ); } } } /** * Detects errors of multiple form controls in a single label. * * @link https://contactform7.com/configuration-errors/multiple-controls-in-label/ */ public function detect_multiple_controls_in_label( $section, $content ) { $pattern = '%(.+?)%s'; if ( preg_match_all( $pattern, $content, $matches ) ) { $form_tags_manager = WPCF7_FormTagsManager::get_instance(); foreach ( $matches[1] as $insidelabel ) { $tags = $form_tags_manager->scan( $insidelabel ); $fields_count = 0; foreach ( $tags as $tag ) { $is_multiple_controls_container = wpcf7_form_tag_supports( $tag->type, 'multiple-controls-container' ); $is_zero_controls_container = wpcf7_form_tag_supports( $tag->type, 'zero-controls-container' ); if ( $is_multiple_controls_container ) { $fields_count += count( $tag->values ); if ( $tag->has_option( 'free_text' ) ) { $fields_count += 1; } } elseif ( $is_zero_controls_container ) { $fields_count += 0; } elseif ( ! empty( $tag->name ) ) { $fields_count += 1; } if ( 1 < $fields_count ) { return true; } } } } return false; } /** * Detects errors of unavailable form-tag names. * * @link https://contactform7.com/configuration-errors/unavailable-names/ */ public function detect_unavailable_names( $section, $content ) { $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type', 'embed', ); $form_tags_manager = WPCF7_FormTagsManager::get_instance(); $ng_named_tags = $form_tags_manager->filter( $content, array( 'name' => $public_query_vars, ) ); $ng_names = array(); foreach ( $ng_named_tags as $tag ) { $ng_names[] = sprintf( '"%s"', $tag->name ); } if ( $ng_names ) { return array_unique( $ng_names ); } return false; } /** * Detects errors of unavailable HTML elements. * * @link https://contactform7.com/configuration-errors/unavailable-html-elements/ */ public function detect_unavailable_html_elements( $section, $content ) { $pattern = '%(?:]|)%i'; if ( preg_match( $pattern, $content ) ) { return true; } return false; } /** * Detects errors of dots in form-tag names. * * @link https://contactform7.com/configuration-errors/dots-in-names/ */ public function detect_dots_in_names( $section, $content ) { $form_tags_manager = WPCF7_FormTagsManager::get_instance(); $tags = $form_tags_manager->filter( $content, array( 'feature' => 'name-attr', ) ); foreach ( $tags as $tag ) { if ( str_contains( $tag->raw_name, '.' ) ) { return true; } } return false; } /** * Detects errors of colons in form-tag names. * * @link https://contactform7.com/configuration-errors/colons-in-names/ */ public function detect_colons_in_names( $section, $content ) { $form_tags_manager = WPCF7_FormTagsManager::get_instance(); $tags = $form_tags_manager->filter( $content, array( 'feature' => 'name-attr', ) ); foreach ( $tags as $tag ) { if ( str_contains( $tag->raw_name, ':' ) ) { return true; } } return false; } /** * Detects errors of uploadable file size overlimit. * * @link https://contactform7.com/configuration-errors/upload-filesize-overlimit */ public function detect_upload_filesize_overlimit( $section, $content ) { $upload_max_filesize = ini_get( 'upload_max_filesize' ); if ( ! $upload_max_filesize ) { return false; } $upload_max_filesize = strtolower( $upload_max_filesize ); $upload_max_filesize = trim( $upload_max_filesize ); if ( ! preg_match( '/^(\d+)([kmg]?)$/', $upload_max_filesize, $matches ) ) { return false; } if ( 'k' === $matches[2] ) { $upload_max_filesize = (int) $matches[1] * KB_IN_BYTES; } elseif ( 'm' === $matches[2] ) { $upload_max_filesize = (int) $matches[1] * MB_IN_BYTES; } elseif ( 'g' === $matches[2] ) { $upload_max_filesize = (int) $matches[1] * GB_IN_BYTES; } else { $upload_max_filesize = (int) $matches[1]; } $form_tags_manager = WPCF7_FormTagsManager::get_instance(); $tags = $form_tags_manager->filter( $content, array( 'basetype' => 'file', ) ); foreach ( $tags as $tag ) { if ( $upload_max_filesize < $tag->get_limit_option() ) { return true; } } return false; } } includes/config-validator/validator.php000064400000020137147206643610014314 0ustar00 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']; } } /** * Returns the contact form object that is tied to this validator. */ public function contact_form() { return $this->contact_form; } /** * Returns true if no error has been detected. */ public function is_valid() { return ! $this->count_errors(); } /** * 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( $options = '' ) { $options = wp_parse_args( $options, array( 'section' => '', 'code' => '', ) ); $count = 0; foreach ( $this->errors as $key => $errors ) { if ( preg_match( '/^mail_[0-9]+\.(.*)$/', $key, $matches ) ) { $key = sprintf( 'mail.%s', $matches[1] ); } if ( $options['section'] and $key !== $options['section'] and preg_replace( '/\..*$/', '', $key, 1 ) !== $options['section'] ) { continue; } foreach ( $errors as $error ) { if ( empty( $error ) ) { continue; } if ( $options['code'] and $error['code'] !== $options['code'] ) { continue; } $count += 1; } } return $count; } /** * Collects messages for detected errors. */ public function collect_error_messages() { $error_messages = array(); foreach ( $this->errors as $section => $errors ) { $error_messages[$section] = array(); foreach ( $errors as $error ) { if ( empty( $error['args']['message'] ) ) { $message = $this->get_default_message( $error['code'] ); } elseif ( empty( $error['args']['params'] ) ) { $message = $error['args']['message']; } else { $message = $this->build_message( $error['args']['message'], $error['args']['params'] ); } $link = ''; if ( ! empty( $error['args']['link'] ) ) { $link = $error['args']['link']; } $error_messages[$section][] = array( 'message' => $message, 'link' => esc_url( $link ), ); } } return $error_messages; } /** * Builds an error message by replacing placeholders. */ public function build_message( $message, $params = '' ) { $params = wp_parse_args( $params, array() ); foreach ( $params as $key => $val ) { if ( ! preg_match( '/^[0-9A-Za-z_]+$/', $key ) ) { // invalid key continue; } $placeholder = '%' . $key . '%'; if ( false !== stripos( $message, $placeholder ) ) { $message = str_ireplace( $placeholder, $val, $message ); } } return $message; } /** * Returns a default message that is used when the message for the error * is not specified. */ public function get_default_message( $code = '' ) { return __( "Configuration error is detected.", 'contact-form-7' ); } /** * 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. * * @param string $section The section where the error detected. * @param string $code The unique code of the error. * @param string|array $args Optional options for the error. */ public function add_error( $section, $code, $args = '' ) { $args = wp_parse_args( $args, array( 'message' => '', 'params' => array(), ) ); $available_error_codes = (array) apply_filters( 'wpcf7_config_validator_available_error_codes', self::error_codes, $this->contact_form ); if ( ! in_array( $code, $available_error_codes, true ) ) { return false; } if ( ! isset( $args['link'] ) ) { $args['link'] = self::get_doc_link( $code ); } if ( ! isset( $this->errors[$section] ) ) { $this->errors[$section] = array(); } $this->errors[$section][] = array( 'code' => $code, 'args' => $args, ); return true; } /** * Removes an error. * * @param string $section The section where the error detected. * @param string $code The unique code of the error. */ public function remove_error( $section, $code ) { if ( empty( $this->errors[$section] ) ) { return; } foreach ( (array) $this->errors[$section] as $key => $error ) { if ( isset( $error['code'] ) and $error['code'] === $code ) { unset( $this->errors[$section][$key] ); } } if ( empty( $this->errors[$section] ) ) { unset( $this->errors[$section] ); } } /** * The main validation runner. * * @return bool True if there is no error detected. */ public function validate() { $this->validate_form(); $this->validate_mail( 'mail' ); $this->validate_mail( 'mail_2' ); $this->validate_messages(); $this->validate_additional_settings(); do_action( 'wpcf7_config_validator_validate', $this ); return $this->is_valid(); } /** * Saves detected errors as a post meta data. */ public function save() { if ( $this->contact_form->initial() ) { return; } delete_post_meta( $this->contact_form->id(), '_config_validation' ); if ( $this->errors ) { update_post_meta( $this->contact_form->id(), '_config_validation', $this->errors ); } } /** * Restore errors from the database. */ public function restore() { $config_errors = get_post_meta( $this->contact_form->id(), '_config_validation', true ); foreach ( (array) $config_errors as $section => $errors ) { if ( empty( $errors ) ) { continue; } foreach ( (array) $errors as $error ) { if ( ! empty( $error['code'] ) ) { $code = $error['code']; $args = isset( $error['args'] ) ? $error['args'] : ''; $this->add_error( $section, $code, $args ); } } } } } includes/file.php000064400000024236147206643610010022 0ustar00 false, 'filetypes' => '', 'limit' => MB_IN_BYTES, ) ); foreach ( array( 'name', 'size', 'tmp_name', 'error' ) as $key ) { if ( ! isset( $file[$key] ) ) { $file[$key] = array(); } } $names = wpcf7_array_flatten( $file['name'] ); $sizes = wpcf7_array_flatten( $file['size'] ); $tmp_names = wpcf7_array_flatten( $file['tmp_name'] ); $errors = wpcf7_array_flatten( $file['error'] ); foreach ( $errors as $error ) { if ( ! empty( $error ) and UPLOAD_ERR_NO_FILE !== $error ) { return new WP_Error( 'wpcf7_upload_failed_php_error', wpcf7_get_message( 'upload_failed_php_error' ) ); } } if ( isset( $options['schema'] ) and isset( $options['name'] ) ) { $context = array( 'file' => true, 'field' => $options['name'], ); foreach ( $options['schema']->validate( $context ) as $result ) { if ( is_wp_error( $result ) ) { return $result; } } } // Move uploaded file to tmp dir $uploads_dir = wpcf7_upload_tmp_dir(); $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir ); $uploaded_files = array(); foreach ( $names as $key => $name ) { $tmp_name = $tmp_names[$key]; if ( empty( $tmp_name ) or ! is_uploaded_file( $tmp_name ) ) { continue; } $filename = $name; $filename = wpcf7_canonicalize( $filename, array( 'strto' => 'as-is' ) ); $filename = wpcf7_antiscript_file_name( $filename ); $filename = apply_filters( 'wpcf7_upload_file_name', $filename, $name, $options ); $filename = wp_unique_filename( $uploads_dir, $filename ); $new_file = path_join( $uploads_dir, $filename ); if ( false === @move_uploaded_file( $tmp_name, $new_file ) ) { return new WP_Error( 'wpcf7_upload_failed', wpcf7_get_message( 'upload_failed' ) ); } // Make sure the uploaded file is only readable for the owner process chmod( $new_file, 0400 ); $uploaded_files[] = $new_file; } return $uploaded_files; } add_filter( 'wpcf7_messages', 'wpcf7_file_messages', 10, 1 ); /** * A wpcf7_messages filter callback that adds messages for * file-uploading fields. */ function wpcf7_file_messages( $messages ) { return array_merge( $messages, array( 'upload_failed' => array( 'description' => __( "Uploading a file fails for any reason", 'contact-form-7' ), 'default' => __( "There was an unknown error uploading the file.", 'contact-form-7' ), ), 'upload_file_type_invalid' => array( 'description' => __( "Uploaded file is not allowed for file type", 'contact-form-7' ), 'default' => __( "You are not allowed to upload files of this type.", 'contact-form-7' ), ), 'upload_file_too_large' => array( 'description' => __( "Uploaded file is too large", 'contact-form-7' ), 'default' => __( "The uploaded file is too large.", 'contact-form-7' ), ), 'upload_failed_php_error' => array( 'description' => __( "Uploading a file fails for PHP error", 'contact-form-7' ), 'default' => __( "There was an error uploading the file.", 'contact-form-7' ), ), ) ); } add_filter( 'wpcf7_form_enctype', 'wpcf7_file_form_enctype_filter', 10, 1 ); /** * A wpcf7_form_enctype filter callback that sets the enctype attribute * to multipart/form-data if the form has file-uploading fields. */ function wpcf7_file_form_enctype_filter( $enctype ) { $multipart = (bool) wpcf7_scan_form_tags( array( 'feature' => 'file-uploading', ) ); if ( $multipart ) { $enctype = 'multipart/form-data'; } return $enctype; } /** * Converts a MIME type string to an array of corresponding file extensions. * * @param string $mime MIME type. * Wildcard (*) is available for the subtype part. * @return array Corresponding file extensions. */ function wpcf7_convert_mime_to_ext( $mime ) { static $mime_types = array(); $mime_types = wp_get_mime_types(); $results = array(); if ( preg_match( '%^([a-z]+)/([*]|[a-z0-9.+-]+)$%i', $mime, $matches ) ) { foreach ( $mime_types as $key => $val ) { if ( '*' === $matches[2] and str_starts_with( $val, $matches[1] . '/' ) or $val === $matches[0] ) { $results = array_merge( $results, explode( '|', $key ) ); } } } $results = array_unique( $results ); $results = array_filter( $results ); $results = array_values( $results ); return $results; } /** * Returns a formatted list of acceptable filetypes. * * @param string|array $types Optional. Array of filetypes. * @param string $format Optional. Pre-defined format designator. * @return string Formatted list of acceptable filetypes. */ function wpcf7_acceptable_filetypes( $types = 'default', $format = 'regex' ) { if ( 'default' === $types or empty( $types ) ) { $types = array( 'audio/*', 'video/*', 'image/*', ); } else { $types = array_map( static function ( $type ) { if ( is_string( $type ) ) { return preg_split( '/[\s|,]+/', strtolower( $type ) ); } }, (array) $types ); $types = wpcf7_array_flatten( $types ); $types = array_filter( array_unique( $types ) ); } if ( 'attr' === $format or 'attribute' === $format ) { $types = array_map( static function ( $type ) { if ( false === strpos( $type, '/' ) ) { return sprintf( '.%s', trim( $type, '.' ) ); } elseif ( preg_match( '%^([a-z]+)/[*]$%i', $type, $matches ) ) { if ( in_array( $matches[1], array( 'audio', 'video', 'image' ) ) ) { return $type; } else { return ''; } } elseif ( wpcf7_convert_mime_to_ext( $type ) ) { return $type; } }, $types ); $types = array_filter( $types ); return implode( ',', $types ); } elseif ( 'regex' === $format ) { $types = array_map( static function ( $type ) { if ( false === strpos( $type, '/' ) ) { return preg_quote( trim( $type, '.' ) ); } elseif ( $type = wpcf7_convert_mime_to_ext( $type ) ) { return $type; } }, $types ); $types = wpcf7_array_flatten( $types ); $types = array_filter( array_unique( $types ) ); return implode( '|', $types ); } return ''; } add_action( 'wpcf7_init', 'wpcf7_init_uploads', 10, 0 ); /** * Initializes the temporary directory for uploaded files. */ function wpcf7_init_uploads() { $dir = wpcf7_upload_tmp_dir(); if ( is_dir( $dir ) and is_writable( $dir ) ) { $htaccess_file = path_join( $dir, '.htaccess' ); if ( file_exists( $htaccess_file ) ) { list( $first_line_comment ) = (array) file( $htaccess_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); if ( '# Apache 2.4+' === $first_line_comment ) { return; } } if ( $handle = @fopen( $htaccess_file, 'w' ) ) { fwrite( $handle, "# Apache 2.4+\n" ); fwrite( $handle, "\n" ); fwrite( $handle, " Require all denied\n" ); fwrite( $handle, "\n" ); fwrite( $handle, "\n" ); fwrite( $handle, "# Apache 2.2\n" ); fwrite( $handle, "\n" ); fwrite( $handle, " Deny from all\n" ); fwrite( $handle, "\n" ); fclose( $handle ); } } } /** * Creates a child directory with a randomly generated name. * * @param string $dir The parent directory path. * @return string The child directory path if created, otherwise the parent. */ function wpcf7_maybe_add_random_dir( $dir ) { do { $rand_max = mt_getrandmax(); $rand = zeroise( mt_rand( 0, $rand_max ), strlen( $rand_max ) ); $dir_new = path_join( $dir, $rand ); } while ( file_exists( $dir_new ) ); if ( wp_mkdir_p( $dir_new ) ) { return $dir_new; } return $dir; } /** * Returns the directory path for uploaded files. * * @return string Directory path. */ function wpcf7_upload_tmp_dir() { if ( defined( 'WPCF7_UPLOADS_TMP_DIR' ) ) { $dir = path_join( WP_CONTENT_DIR, WPCF7_UPLOADS_TMP_DIR ); wp_mkdir_p( $dir ); if ( wpcf7_is_file_path_in_content_dir( $dir ) ) { return $dir; } } $dir = path_join( wpcf7_upload_dir( 'dir' ), 'wpcf7_uploads' ); wp_mkdir_p( $dir ); return $dir; } add_action( 'shutdown', 'wpcf7_cleanup_upload_files', 20, 0 ); /** * Cleans up files in the temporary directory for uploaded files. * * @param int $seconds Files older than this are removed. Default 60. * @param int $max Maximum number of files to be removed in a function call. * Default 100. */ function wpcf7_cleanup_upload_files( $seconds = 60, $max = 100 ) { $dir = trailingslashit( wpcf7_upload_tmp_dir() ); if ( ! is_dir( $dir ) or ! is_readable( $dir ) or ! wp_is_writable( $dir ) ) { return; } $seconds = absint( $seconds ); $max = absint( $max ); $count = 0; if ( $handle = opendir( $dir ) ) { while ( false !== ( $file = readdir( $handle ) ) ) { if ( '.' == $file or '..' == $file or '.htaccess' == $file ) { continue; } $mtime = @filemtime( path_join( $dir, $file ) ); if ( $mtime and time() < $mtime + $seconds ) { // less than $seconds old continue; } wpcf7_rmdir_p( path_join( $dir, $file ) ); $count += 1; if ( $max <= $count ) { break; } } closedir( $handle ); } } add_action( 'wpcf7_admin_warnings', 'wpcf7_file_display_warning_message', 10, 3 ); /** * Displays warning messages about file-uploading fields. */ function wpcf7_file_display_warning_message( $page, $action, $object ) { if ( $object instanceof WPCF7_ContactForm ) { $contact_form = $object; } else { return; } $has_tags = (bool) $contact_form->scan_form_tags( array( 'feature' => 'file-uploading', ) ); if ( ! $has_tags ) { return; } $uploads_dir = wpcf7_upload_tmp_dir(); if ( ! is_dir( $uploads_dir ) or ! wp_is_writable( $uploads_dir ) ) { $message = sprintf( /* translators: %s: the path of the temporary folder */ __( 'This contact form has file uploading fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.', 'contact-form-7' ), $uploads_dir ); wp_admin_notice( esc_html( $message ), 'type=warning' ); } } includes/contact-form-template.php000064400000013302147206643610013300 0ustar00 %2$s [text* your-name autocomplete:name] [submit "%6$s"]', __( '(optional)', 'contact-form-7' ), __( 'Your name', 'contact-form-7' ), __( 'Your email', 'contact-form-7' ), __( 'Subject', 'contact-form-7' ), __( 'Your message', 'contact-form-7' ), __( 'Submit', 'contact-form-7' ) ); return trim( $template ); } public static function mail() { $template = array( 'subject' => sprintf( /* translators: 1: blog name, 2: [your-subject] */ _x( '%1$s "%2$s"', 'mail subject', 'contact-form-7' ), '[_site_title]', '[your-subject]' ), 'sender' => sprintf( '%s <%s>', '[_site_title]', self::from_email() ), 'body' => sprintf( /* translators: %s: [your-name] [your-email] */ __( 'From: %s', 'contact-form-7' ), '[your-name] [your-email]' ) . "\n" . sprintf( /* translators: %s: [your-subject] */ __( 'Subject: %s', 'contact-form-7' ), '[your-subject]' ) . "\n\n" . __( 'Message Body:', 'contact-form-7' ) . "\n" . '[your-message]' . "\n\n" . '-- ' . "\n" . sprintf( /* translators: 1: blog name, 2: blog URL */ __( 'This is a notification that a contact form was submitted on your website (%1$s %2$s).', 'contact-form-7' ), '[_site_title]', '[_site_url]' ), 'recipient' => '[_site_admin_email]', 'additional_headers' => 'Reply-To: [your-email]', 'attachments' => '', 'use_html' => 0, 'exclude_blank' => 0, ); return $template; } public static function mail_2() { $template = array( 'active' => false, 'subject' => sprintf( /* translators: 1: blog name, 2: [your-subject] */ _x( '%1$s "%2$s"', 'mail subject', 'contact-form-7' ), '[_site_title]', '[your-subject]' ), 'sender' => sprintf( '%s <%s>', '[_site_title]', self::from_email() ), 'body' => __( 'Message Body:', 'contact-form-7' ) . "\n" . '[your-message]' . "\n\n" . '-- ' . "\n" . sprintf( /* translators: 1: blog name, 2: blog URL */ __( '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]' ), 'recipient' => '[your-email]', 'additional_headers' => sprintf( 'Reply-To: %s', '[_site_admin_email]' ), 'attachments' => '', 'use_html' => 0, 'exclude_blank' => 0, ); return $template; } public static function from_email() { $admin_email = get_option( 'admin_email' ); if ( wpcf7_is_localhost() ) { return $admin_email; } $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); $sitename = strtolower( $sitename ); if ( 'www.' === substr( $sitename, 0, 4 ) ) { $sitename = substr( $sitename, 4 ); } if ( strpbrk( $admin_email, '@' ) === '@' . $sitename ) { return $admin_email; } return 'wordpress@' . $sitename; } public static function messages() { $messages = array(); foreach ( wpcf7_messages() as $key => $arr ) { $messages[$key] = $arr['default']; } return $messages; } } function wpcf7_messages() { $messages = array( 'mail_sent_ok' => array( 'description' => __( "Sender's message was sent successfully", 'contact-form-7' ), 'default' => __( "Thank you for your message. It has been sent.", 'contact-form-7' ), ), 'mail_sent_ng' => array( 'description' => __( "Sender's message failed to send", 'contact-form-7' ), 'default' => __( "There was an error trying to send your message. Please try again later.", 'contact-form-7' ), ), 'validation_error' => array( 'description' => __( "Validation errors occurred", 'contact-form-7' ), 'default' => __( "One or more fields have an error. Please check and try again.", 'contact-form-7' ), ), 'spam' => array( 'description' => __( "Submission was referred to as spam", 'contact-form-7' ), 'default' => __( "There was an error trying to send your message. Please try again later.", 'contact-form-7' ), ), 'accept_terms' => array( 'description' => __( "There are terms that the sender must accept", 'contact-form-7' ), 'default' => __( "You must accept the terms and conditions before sending your message.", 'contact-form-7' ), ), 'invalid_required' => array( 'description' => __( "There is a field that the sender must fill in", 'contact-form-7' ), 'default' => __( "Please fill out this field.", 'contact-form-7' ), ), 'invalid_too_long' => array( 'description' => __( "There is a field with input that is longer than the maximum allowed length", 'contact-form-7' ), 'default' => __( "This field has a too long input.", 'contact-form-7' ), ), 'invalid_too_short' => array( 'description' => __( "There is a field with input that is shorter than the minimum allowed length", 'contact-form-7' ), 'default' => __( "This field has a too short input.", 'contact-form-7' ), ), ); return apply_filters( 'wpcf7_messages', $messages ); } includes/block-editor/index.js000064400000011762147206643610012415 0ustar00(()=>{"use strict";var t={n:e=>{var l=e&&e.__esModule?()=>e.default:()=>e;return t.d(l,{a:l}),l},d:(e,l)=>{for(var a in l)t.o(l,a)&&!t.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:l[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const e=window.wp.i18n,l=window.wp.blocks,a=window.wp.blockEditor,r=window.ReactJSXRuntime,o=(0,r.jsxs)("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 242.5 239.46",children:[(0,r.jsx)("defs",{children:(0,r.jsx)("clipPath",{id:"clip-path",transform:"translate(1.72)",children:(0,r.jsx)("circle",{className:"cls-1",cx:"119.73",cy:"119.73",r:"116.15",fill:"none"})})}),(0,r.jsx)("g",{id:"Layer_2","data-name":"Layer 2",children:(0,r.jsxs)("g",{id:"Layer_1","data-name":"Layer 1",children:[(0,r.jsxs)("g",{className:"cls-2",clipPath:"url(#clip-path)",children:[(0,r.jsx)("circle",{className:"cls-3",cx:"121.45",cy:"119.73",r:"116.15",fill:"#33c6f4"}),(0,r.jsx)("path",{className:"cls-4",d:"M239.32,167.79c-53.41-24-108.37-91.46-113-94.55s-10.84.77-10.84.77c-3.87-6.19-10.06.77-10.06.77C76.77,123.55.14,170.11.14,170.11S36.94,237.79,122,237.79C208.48,237.79,239.32,167.79,239.32,167.79Z",transform:"translate(1.72)",fill:"#1b447e"}),(0,r.jsx)("path",{className:"cls-5",d:"M67.48,116.58s15.48-7,12.38,4.65-15.48,28.64-11.61,29.41S83,140.58,86.06,142.12s5.42.78,3.87,6.2-3.1,9.29,0,9.29,5.42-7,9.29-13.94,10.06-3.87,12.38-1.55,9.29,15.49,14.71,13.94,8.51-8.52,6.19-24,1.55-20.12,1.55-20.12,4.64-2.32,13.16,8.51,24,27.09,26.31,26.32-10.83-17.8-7.74-19.35,15.48,2.32,21.68,7.74c0,0,2.12,8.87,2.12.36L126.31,73.24,115.47,74l-10.06.77S80.64,111.94,67.48,116.58Z",transform:"translate(1.72)",fill:"#fff"}),(0,r.jsx)("path",{className:"cls-6",d:"M239.32,170.11c-53.41-24-108.37-93.78-113-96.87s-10.84.77-10.84.77c-3.87-6.19-10.06.77-10.06.77C76.77,123.55.14,170.11.14,170.11",transform:"translate(1.72)",fill:"none",stroke:"#221e1f",strokeMiterlimit:"10",strokeWidth:"8px"})]}),(0,r.jsx)("circle",{className:"cls-6",cx:"121.45",cy:"119.73",r:"116.15",fill:"none",stroke:"#1b447e",strokeMiterlimit:"10",strokeWidth:"8px"})]})})]}),s=window.wp.element,c=window.wp.components,n=window.wp.apiFetch;var i=t.n(n);const m=window.wp.url,h=async t=>i()({path:(0,m.addQueryArgs)("/contact-form-7/v1/contact-forms",{posts_per_page:20,orderby:"modified",order:"DESC",...t})}).then((t=>t)),d=t=>{let e="[contact-form-7]";return t.hash?e=e.replace(/\]$/,` id="${t.hash}"]`):t.id&&(e=e.replace(/\]$/,` id="${t.id}"]`)),t.title&&(e=e.replace(/\]$/,` title="${t.title}"]`)),t.htmlId&&(e=e.replace(/\]$/,` html_id="${t.htmlId}"]`)),t.htmlName&&(e=e.replace(/\]$/,` html_name="${t.htmlName}"]`)),t.htmlTitle&&(e=e.replace(/\]$/,` html_title="${t.htmlTitle}"]`)),t.htmlClass&&(e=e.replace(/\]$/,` html_class="${t.htmlClass}"]`)),"raw_form"===t.output&&(e=e.replace(/\]$/,` output="${t.output}"]`)),e},p=t=>{const e=ajaxurl.replace(/\/admin-ajax\.php$/,"/admin.php");return(0,m.addQueryArgs)(e,{page:"wpcf7",post:t.id,action:"edit"})},f={from:[],to:[{type:"block",blocks:["core/shortcode"],transform:t=>{const e=d(t);return(0,l.createBlock)("core/shortcode",{text:e})}}]};(0,l.registerBlockType)("contact-form-7/contact-form-selector",{icon:o,transforms:f,edit:function({attributes:t,setAttributes:l}){const o=t=>t.reduce(((t,e)=>t.set(e.id,e)),new Map),[n,i]=(0,s.useState)((()=>o([])));return(0,s.useEffect)((()=>{h().then((t=>{i(o(t))}))}),[]),(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(a.InspectorControls,{children:[t.id&&(0,r.jsx)(c.PanelBody,{title:t.title,children:(0,r.jsx)(c.ExternalLink,{href:p(t),children:(0,e.__)("Edit this contact form","contact-form-7")})}),t.id&&(0,r.jsxs)(c.PanelBody,{title:(0,e.__)("Form attributes","contact-form-7"),initialOpen:!1,children:[(0,r.jsx)(c.TextControl,{label:(0,e.__)("ID","contact-form-7"),value:t.htmlId,onChange:t=>l({htmlId:t}),help:(0,e.__)("Used for the id attribute value of the form element.","contact-form-7")}),(0,r.jsx)(c.TextControl,{label:(0,e.__)("Name","contact-form-7"),value:t.htmlName,onChange:t=>l({htmlName:t}),help:(0,e.__)("Used for the name attribute value of the form element.","contact-form-7")}),(0,r.jsx)(c.TextControl,{label:(0,e.__)("Title","contact-form-7"),value:t.htmlTitle,onChange:t=>l({htmlTitle:t}),help:(0,e.__)("Used for the aria-label attribute value of the form element.","contact-form-7")}),(0,r.jsx)(c.TextControl,{label:(0,e.__)("Class","contact-form-7"),value:t.htmlClass,onChange:t=>l({htmlClass:t}),help:(0,e.__)("Used for the class attribute value of the form element.","contact-form-7")})]})]}),(0,r.jsx)("div",{...(0,a.useBlockProps)({className:"components-placeholder",style:{marginTop:"28px",marginBottom:"28px"}}),children:(0,r.jsx)(c.ComboboxControl,{label:(0,e.__)("Select a contact form:","contact-form-7"),options:(t=>{const e=[];for(const[l,a]of t)e.push({value:l,label:a.title});return e})(n),value:t.id,onChange:t=>l({id:parseInt(t),hash:n.get(parseInt(t))?.hash,title:n.get(parseInt(t))?.title}),onFilterValueChange:t=>{h({search:t}).then((t=>{i(o(t))}))}})})]})},save:({attributes:t})=>{const e=d(t);return(0,r.jsx)("div",{...a.useBlockProps.save(),children:e})}})})();includes/block-editor/index.asset.php000064400000000334147206643610013677 0ustar00 array( 'react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-url', ), 'version' => WPCF7_VERSION, ); includes/block-editor/block.json000064400000001363147206643610012731 0ustar00{ "$schema": "https://schemas.wp.org/trunk/block.json", "apiVersion": 3, "name": "contact-form-7/contact-form-selector", "title": "Contact Form 7", "category": "widgets", "description": "Insert a contact form you have created with Contact Form 7.", "keywords": [ "form" ], "textdomain": "contact-form-7", "attributes": { "id": { "type": "integer" }, "hash": { "type": "string" }, "title": { "type": "string" }, "htmlId": { "type": "string" }, "htmlName": { "type": "string" }, "htmlTitle": { "type": "string" }, "htmlClass": { "type": "string" }, "output": { "enum": [ "form", "raw_form" ], "default": "form" } }, "editorScript": [ "file:./index.js", "contact-form-7-block-editor" ] } includes/block-editor/block-editor.php000064400000000301147206643610014022 0ustar00 __( 'Spam protection', 'contact-form-7' ), 'email_marketing' => __( 'Email marketing', 'contact-form-7' ), 'payments' => __( 'Payments', 'contact-form-7' ), ); } /** * Returns the singleton instance of this class. * * @return WPCF7_Integration The instance. */ public static function get_instance() { if ( empty( self::$instance ) ) { self::$instance = new self; self::$instance->categories = self::get_builtin_categories(); } return self::$instance; } /** * Adds a service to the services list. */ public function add_service( $name, WPCF7_Service $service ) { $name = sanitize_key( $name ); if ( empty( $name ) or isset( $this->services[$name] ) ) { return false; } $this->services[$name] = $service; } /** * Adds a service category to the categories list. */ public function add_category( $name, $title ) { $name = sanitize_key( $name ); if ( empty( $name ) or isset( $this->categories[$name] ) ) { return false; } $this->categories[$name] = $title; } /** * Returns true if a service with the name exists in the services list. * * @param string $name The name of service to search. */ public function service_exists( $name = '' ) { if ( '' == $name ) { return (bool) count( $this->services ); } else { return isset( $this->services[$name] ); } } /** * Returns a service object with the name. * * @param string $name The name of service. * @return WPCF7_Service|bool The service object if it exists, * false otherwise. */ public function get_service( $name ) { if ( $this->service_exists( $name ) ) { return $this->services[$name]; } else { return false; } } /** * Prints services list. */ public function list_services( $args = '' ) { $args = wp_parse_args( $args, array( 'include' => array(), ) ); $singular = false; $services = (array) $this->services; if ( ! empty( $args['include'] ) ) { $services = array_intersect_key( $services, array_flip( (array) $args['include'] ) ); if ( 1 == count( $services ) ) { $singular = true; } } if ( empty( $services ) ) { return; } $action = wpcf7_current_action(); foreach ( $services as $name => $service ) { $cats = array_intersect_key( $this->categories, array_flip( $service->get_categories() ) ); ?>
    icon(); ?>

    get_title() ); ?>


    link(); ?>

    display( $action ); } else { $service->display(); } ?>
    refresh_token ); } protected function save_data() { } protected function reset_data() { } protected function get_redirect_uri() { return admin_url(); } protected function menu_page_url( $args = '' ) { return menu_page_url( 'wpcf7-integration', false ); } public function load( $action = '' ) { if ( 'auth_redirect' === $action ) { $code = $_GET['code'] ?? ''; if ( $code ) { $this->request_token( $code ); } if ( ! empty( $this->access_token ) ) { $message = 'success'; } else { $message = 'failed'; } wp_safe_redirect( $this->menu_page_url( array( 'action' => 'setup', 'message' => $message, ) ) ); exit(); } } protected function authorize( $scope = '' ) { $endpoint = add_query_arg( array( 'response_type' => 'code', 'client_id' => $this->client_id, 'redirect_uri' => urlencode( $this->get_redirect_uri() ), 'scope' => $scope, ), $this->authorization_endpoint ); if ( wp_redirect( sanitize_url( $endpoint ) ) ) { exit(); } } protected function get_http_authorization_header( $scheme = 'basic' ) { $scheme = strtolower( trim( $scheme ) ); switch ( $scheme ) { case 'bearer': return sprintf( 'Bearer %s', $this->access_token ); case 'basic': default: return sprintf( 'Basic %s', base64_encode( $this->client_id . ':' . $this->client_secret ) ); } } protected function request_token( $authorization_code ) { $endpoint = add_query_arg( array( 'code' => $authorization_code, 'redirect_uri' => urlencode( $this->get_redirect_uri() ), 'grant_type' => 'authorization_code', ), $this->token_endpoint ); $request = array( 'headers' => array( 'Authorization' => $this->get_http_authorization_header( 'basic' ), ), ); $response = wp_remote_post( sanitize_url( $endpoint ), $request ); $response_code = (int) wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); $response_body = json_decode( $response_body, true ); if ( WP_DEBUG and 400 <= $response_code ) { $this->log( $endpoint, $request, $response ); } if ( 401 == $response_code ) { // Unauthorized $this->access_token = null; $this->refresh_token = null; } else { if ( isset( $response_body['access_token'] ) ) { $this->access_token = $response_body['access_token']; } else { $this->access_token = null; } if ( isset( $response_body['refresh_token'] ) ) { $this->refresh_token = $response_body['refresh_token']; } else { $this->refresh_token = null; } } $this->save_data(); return $response; } protected function refresh_token() { $endpoint = add_query_arg( array( 'refresh_token' => $this->refresh_token, 'grant_type' => 'refresh_token', ), $this->token_endpoint ); $request = array( 'headers' => array( 'Authorization' => $this->get_http_authorization_header( 'basic' ), ), ); $response = wp_remote_post( sanitize_url( $endpoint ), $request ); $response_code = (int) wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); $response_body = json_decode( $response_body, true ); if ( WP_DEBUG and 400 <= $response_code ) { $this->log( $endpoint, $request, $response ); } if ( 401 == $response_code ) { // Unauthorized $this->access_token = null; $this->refresh_token = null; } else { if ( isset( $response_body['access_token'] ) ) { $this->access_token = $response_body['access_token']; } else { $this->access_token = null; } if ( isset( $response_body['refresh_token'] ) ) { $this->refresh_token = $response_body['refresh_token']; } } $this->save_data(); return $response; } protected function remote_request( $url, $request = array() ) { static $refreshed = false; $request = wp_parse_args( $request, array() ); $request['headers'] = array_merge( $request['headers'], array( 'Authorization' => $this->get_http_authorization_header( 'bearer' ), ) ); $response = wp_remote_request( sanitize_url( $url ), $request ); if ( 401 === wp_remote_retrieve_response_code( $response ) and ! $refreshed ) { $this->refresh_token(); $refreshed = true; $response = $this->remote_request( $url, $request ); } return $response; } protected function log( $url, $request, $response ) { wpcf7_log_remote_request( $url, $request, $response ); } } includes/capabilities.php000064400000001502147206643610011523 0ustar00 WPCF7_ADMIN_READ_WRITE_CAPABILITY, 'wpcf7_edit_contact_forms' => WPCF7_ADMIN_READ_WRITE_CAPABILITY, 'wpcf7_read_contact_form' => WPCF7_ADMIN_READ_CAPABILITY, 'wpcf7_read_contact_forms' => WPCF7_ADMIN_READ_CAPABILITY, 'wpcf7_delete_contact_form' => WPCF7_ADMIN_READ_WRITE_CAPABILITY, 'wpcf7_delete_contact_forms' => WPCF7_ADMIN_READ_WRITE_CAPABILITY, 'wpcf7_manage_integration' => 'manage_options', 'wpcf7_submit' => 'read', ); $meta_caps = apply_filters( 'wpcf7_map_meta_cap', $meta_caps ); $caps = array_diff( $caps, array_keys( $meta_caps ) ); if ( isset( $meta_caps[$cap] ) ) { $caps[] = $meta_caps[$cap]; } return $caps; } includes/swv/schema-holder.php000064400000001726147206643610012434 0ustar00schema ) ) { return $this->schema; } $schema = new WPCF7_SWV_Schema( array( 'locale' => isset( $this->locale ) ? $this->locale : '', ) ); do_action( 'wpcf7_swv_create_schema', $schema, $this ); return $this->schema = $schema; } /** * Validates form inputs based on the schema and given context. */ public function validate_schema( $context, WPCF7_Validation $validity ) { $schema = $this->get_schema(); foreach ( $schema->validate( $context ) as $result ) { if ( is_wp_error( $result ) ) { $rule = $result->get_error_data(); $field = $rule->get_property( 'field' ); if ( isset( $field ) and $validity->is_valid( $field ) ) { $validity->invalidate( $field, $result ); } } } } } includes/swv/php/abstract-rules.php000064400000006407147206643610013444 0ustar00properties = 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, ) ); } } includes/swv/php/rules/minnumber.php000064400000001416147206643610013632 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/maxitems.php000064400000001321147206643610013460 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/tel.php000064400000001151147206643610012416 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_tel( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/number.php000064400000001162147206643610013124 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_number( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/minlength.php000064400000001442147206643610013622 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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(); } } } includes/swv/php/rules/file.php000064400000002437147206643610012561 0ustar00get_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; } } includes/swv/php/rules/any.php000064400000000767147206643610012435 0ustar00rules() as $rule ) { if ( $rule->matches( $context ) ) { $result = $rule->validate( $context ); if ( ! is_wp_error( $result ) ) { return true; } } } return $this->create_error(); } } includes/swv/php/rules/required.php000064400000001112147206643610013447 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); if ( empty( $input ) ) { return $this->create_error(); } return true; } } includes/swv/php/rules/url.php000064400000001151147206643610012434 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_url( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/enum.php000064400000001671147206643610012605 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/maxfilesize.php000064400000001305147206643610014153 0ustar00get_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; } } includes/swv/php/rules/maxlength.php000064400000001442147206643610013624 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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(); } } } includes/swv/php/rules/mindate.php000064400000001366147206643610013263 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/minfilesize.php000064400000001305147206643610014151 0ustar00get_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; } } includes/swv/php/rules/requiredfile.php000064400000001126147206643610014314 0ustar00get_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; } } includes/swv/php/rules/all.php000064400000001114147206643610012401 0ustar00rules() 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; } } includes/swv/php/rules/date.php000064400000001154147206643610012552 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_date( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/email.php000064400000001157147206643610012727 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_email( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/maxdate.php000064400000001366147206643610013265 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/time.php000064400000001154147206643610012573 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); foreach ( $input as $i ) { if ( ! wpcf7_is_time( $i ) ) { return $this->create_error(); } } return true; } } includes/swv/php/rules/minitems.php000064400000001321147206643610013456 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/stepnumber.php000064400000001671147206643610014025 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $input ); $input = wpcf7_exclude_blank( $input ); $base = floatval( $this->get_property( 'base' ) ); $interval = floatval( $this->get_property( 'interval' ) ); if ( ! ( 0 < $interval ) ) { return true; } foreach ( $input as $i ) { $remainder = fmod( floatval( $i ) - $base, $interval ); if ( 0.0 === round( abs( $remainder ), 6 ) or 0.0 === round( abs( $remainder - $interval ), 6 ) ) { continue; } return $this->create_error(); } return true; } } includes/swv/php/rules/maxnumber.php000064400000001416147206643610013634 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/php/rules/dayofweek.php000064400000002026147206643610013612 0ustar00get_default_input(); $input = wpcf7_array_flatten( $input ); $input = wpcf7_strip_whitespaces( $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; } } includes/swv/script-loader.php000064400000001024147206643610012460 0ustar00 array(), 'version' => WPCF7_VERSION, ) ); wp_register_script( 'swv', wpcf7_plugin_url( 'includes/swv/js/index.js' ), $assets['dependencies'], $assets['version'], array( 'in_footer' => true ) ); }, 10, 0 ); includes/swv/swv.php000064400000010224147206643610010531 0ustar00 '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', 'stepnumber' => 'Contactable\SWV\StepNumberRule', 'all' => 'Contactable\SWV\AllRule', 'any' => 'Contactable\SWV\AnyRule', ); return apply_filters( 'wpcf7_swv_available_rules', $rules ); } add_action( 'wpcf7_init', 'wpcf7_swv_load_rules', 10, 0 ); /** * Loads SWV fules. */ function wpcf7_swv_load_rules() { $rules = wpcf7_swv_available_rules(); foreach ( array_keys( $rules ) as $rule ) { $file = sprintf( '%s.php', $rule ); $path = path_join( WPCF7_PLUGIN_DIR . '/includes/swv/php/rules', $file ); if ( file_exists( $path ) ) { include_once $path; } } } /** * Creates an SWV rule object. * * @param string $rule_name Rule name. * @param string|array $properties Optional. Rule properties. * @return \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(); if ( isset( $rules[$rule_name] ) ) { return new $rules[$rule_name]( $properties ); } } /** * Returns an associative array of JSON Schema for Contact Form 7 SWV. */ function wpcf7_swv_get_meta_schema() { return array( '$schema' => 'https://json-schema.org/draft/2020-12/schema', 'title' => 'Contact Form 7 SWV', 'description' => 'Contact Form 7 SWV meta-schema', 'type' => 'object', 'properties' => array( 'version' => array( 'type' => 'string', ), 'locale' => array( 'type' => 'string', ), 'rules' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'properties' => array( 'rule' => array( 'type' => 'string', 'enum' => array_keys( wpcf7_swv_available_rules() ), ), 'field' => array( 'type' => 'string', 'pattern' => '^[A-Za-z][-A-Za-z0-9_:]*$', ), 'error' => array( 'type' => 'string', ), 'accept' => array( 'type' => 'array', 'items' => array( 'type' => 'string', ), ), 'base' => array( 'type' => 'string', ), 'interval' => array( 'type' => 'number', 'minimum' => 0, ), 'threshold' => array( 'type' => 'string', ), ), 'required' => array( 'rule' ), ), ), ), ); } /** * The schema class as a composite rule. */ class WPCF7_SWV_Schema extends \Contactable\SWV\CompositeRule { /** * The human-readable version of the schema. */ const version = 'Contact Form 7 SWV Schema 2024-10'; /** * 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 ); } } } } includes/swv/js/index.js000064400000030340147206643610011263 0ustar00(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function i(t){if(this.formData={},this.tree={},!(t instanceof FormData))return this;this.formData=t;const e=()=>{const t=new Map;return t.largestIndex=0,t.set=function(e,i){""===e?e=t.largestIndex++:/^[0-9]+$/.test(e)&&(e=parseInt(e),t.largestIndex<=e&&(t.largestIndex=e+1)),Map.prototype.set.call(t,e,i)},t};this.tree=e();const i=/^(?[a-z][-a-z0-9_:]*)(?(?:\[(?:[a-z][-a-z0-9_:]*|[0-9]*)\])*)/i;for(const[t,s]of this.formData){const o=t.match(i);if(o)if(""===o.groups.array)this.tree.set(o.groups.name,s);else{const t=[...o.groups.array.matchAll(/\[([a-z][-a-z0-9_:]*|[0-9]*)\]/gi)].map((([t,e])=>e));t.unshift(o.groups.name);const i=t.pop();t.reduce(((t,i)=>{if(/^[0-9]+$/.test(i)&&(i=parseInt(i)),t.get(i)instanceof Map)return t.get(i);const s=e();return t.set(i,s),s}),this.tree).set(i,s)}}}t.r(e),t.d(e,{all:()=>D,any:()=>M,date:()=>m,dayofweek:()=>u,email:()=>r,enum:()=>h,file:()=>d,maxdate:()=>z,maxfilesize:()=>j,maxitems:()=>v,maxlength:()=>x,maxnumber:()=>y,mindate:()=>A,minfilesize:()=>$,minitems:()=>w,minlength:()=>g,minnumber:()=>b,number:()=>c,required:()=>n,requiredfile:()=>a,stepnumber:()=>I,tel:()=>l,time:()=>f,url:()=>p}),i.prototype.entries=function(){return this.tree.entries()},i.prototype.get=function(t){return this.tree.get(t)},i.prototype.getAll=function(t){if(!this.has(t))return[];const e=t=>{const i=[];if(t instanceof Map)for(const[s,o]of t)i.push(...e(o));else""!==t&&i.push(t);return i};return e(this.get(t))},i.prototype.has=function(t){return this.tree.has(t)},i.prototype.keys=function(){return this.tree.keys()},i.prototype.values=function(){return this.tree.values()};const s=i;function o({rule:t,field:e,error:i,...s}){this.rule=t,this.field=e,this.error=i,this.properties=s}const n=function(t){if(0===t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).length)throw new o(this)},a=function(t){if(0===t.getAll(this.field).length)throw new o(this)},r=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>{if(t.length<6)return!1;if(-1===t.indexOf("@",1))return!1;if(t.indexOf("@")!==t.lastIndexOf("@"))return!1;const[e,i]=t.split("@",2);if(!/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/.test(e))return!1;if(/\.{2,}/.test(i))return!1;if(/(?:^[ \t\n\r\0\x0B.]|[ \t\n\r\0\x0B.]$)/.test(i))return!1;const s=i.split(".");if(s.length<2)return!1;for(const t of s){if(/(?:^[ \t\n\r\0\x0B-]|[ \t\n\r\0\x0B-]$)/.test(t))return!1;if(!/^[a-z0-9-]+$/i.test(t))return!1}return!0})))throw new o(this)},p=function(t){const e=t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t));if(!e.every((t=>{try{return(t=>-1!==["http","https","ftp","ftps","mailto","news","irc","irc6","ircs","gopher","nntp","feed","telnet","mms","rtsp","sms","svn","tel","fax","xmpp","webcal","urn"].indexOf(t))(new URL(t).protocol.replace(/:$/,""))}catch{return!1}})))throw new o(this)},l=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>(((t=(t=t.replace(/[#*].*$/,"")).replaceAll(/[()/.*#\s-]+/g,"")).startsWith("+")||t.startsWith("00"))&&(t=`+${t.replace(/^[+0]+/,"")}`),!!/^[+]?[0-9]+$/.test(t)&&6t.trim())).filter((t=>""!==t)).every((t=>!!/^[-]?[0-9]+(?:[eE][+-]?[0-9]+)?$/.test(t)||!!/^[-]?(?:[0-9]+)?[.][0-9]+(?:[eE][+-]?[0-9]+)?$/.test(t))))throw new o(this)},m=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>{if(!/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test(t))return!1;const e=new Date(t);return!Number.isNaN(e.valueOf())})))throw new o(this)},f=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>{const e=t.match(/^([0-9]{2})\:([0-9]{2})(?:\:([0-9]{2}))?$/);if(!e)return!1;const i=parseInt(e[1]),s=parseInt(e[2]),o=e[3]?parseInt(e[3]):0;return 0<=i&&i<=23&&0<=s&&s<=59&&0<=o&&o<=59})))throw new o(this)},d=function(t){if(!t.getAll(this.field).every((t=>t instanceof File&&this.accept?.some((e=>/^\.[a-z0-9]+$/i.test(e)?t.name.toLowerCase().endsWith(e.toLowerCase()):(t=>{const e=[],i=t.match(/^(?[a-z]+)\/(?[*]|[a-z0-9.+-]+)$/i);if(i){const t=i.groups.toplevel.toLowerCase(),s=i.groups.sub.toLowerCase();for(const[o,n]of(()=>{const t=new Map;return t.set("jpg|jpeg|jpe","image/jpeg"),t.set("gif","image/gif"),t.set("png","image/png"),t.set("bmp","image/bmp"),t.set("tiff|tif","image/tiff"),t.set("webp","image/webp"),t.set("ico","image/x-icon"),t.set("heic","image/heic"),t.set("asf|asx","video/x-ms-asf"),t.set("wmv","video/x-ms-wmv"),t.set("wmx","video/x-ms-wmx"),t.set("wm","video/x-ms-wm"),t.set("avi","video/avi"),t.set("divx","video/divx"),t.set("flv","video/x-flv"),t.set("mov|qt","video/quicktime"),t.set("mpeg|mpg|mpe","video/mpeg"),t.set("mp4|m4v","video/mp4"),t.set("ogv","video/ogg"),t.set("webm","video/webm"),t.set("mkv","video/x-matroska"),t.set("3gp|3gpp","video/3gpp"),t.set("3g2|3gp2","video/3gpp2"),t.set("txt|asc|c|cc|h|srt","text/plain"),t.set("csv","text/csv"),t.set("tsv","text/tab-separated-values"),t.set("ics","text/calendar"),t.set("rtx","text/richtext"),t.set("css","text/css"),t.set("htm|html","text/html"),t.set("vtt","text/vtt"),t.set("dfxp","application/ttaf+xml"),t.set("mp3|m4a|m4b","audio/mpeg"),t.set("aac","audio/aac"),t.set("ra|ram","audio/x-realaudio"),t.set("wav","audio/wav"),t.set("ogg|oga","audio/ogg"),t.set("flac","audio/flac"),t.set("mid|midi","audio/midi"),t.set("wma","audio/x-ms-wma"),t.set("wax","audio/x-ms-wax"),t.set("mka","audio/x-matroska"),t.set("rtf","application/rtf"),t.set("js","application/javascript"),t.set("pdf","application/pdf"),t.set("swf","application/x-shockwave-flash"),t.set("class","application/java"),t.set("tar","application/x-tar"),t.set("zip","application/zip"),t.set("gz|gzip","application/x-gzip"),t.set("rar","application/rar"),t.set("7z","application/x-7z-compressed"),t.set("exe","application/x-msdownload"),t.set("psd","application/octet-stream"),t.set("xcf","application/octet-stream"),t.set("doc","application/msword"),t.set("pot|pps|ppt","application/vnd.ms-powerpoint"),t.set("wri","application/vnd.ms-write"),t.set("xla|xls|xlt|xlw","application/vnd.ms-excel"),t.set("mdb","application/vnd.ms-access"),t.set("mpp","application/vnd.ms-project"),t.set("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document"),t.set("docm","application/vnd.ms-word.document.macroEnabled.12"),t.set("dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template"),t.set("dotm","application/vnd.ms-word.template.macroEnabled.12"),t.set("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),t.set("xlsm","application/vnd.ms-excel.sheet.macroEnabled.12"),t.set("xlsb","application/vnd.ms-excel.sheet.binary.macroEnabled.12"),t.set("xltx","application/vnd.openxmlformats-officedocument.spreadsheetml.template"),t.set("xltm","application/vnd.ms-excel.template.macroEnabled.12"),t.set("xlam","application/vnd.ms-excel.addin.macroEnabled.12"),t.set("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation"),t.set("pptm","application/vnd.ms-powerpoint.presentation.macroEnabled.12"),t.set("ppsx","application/vnd.openxmlformats-officedocument.presentationml.slideshow"),t.set("ppsm","application/vnd.ms-powerpoint.slideshow.macroEnabled.12"),t.set("potx","application/vnd.openxmlformats-officedocument.presentationml.template"),t.set("potm","application/vnd.ms-powerpoint.template.macroEnabled.12"),t.set("ppam","application/vnd.ms-powerpoint.addin.macroEnabled.12"),t.set("sldx","application/vnd.openxmlformats-officedocument.presentationml.slide"),t.set("sldm","application/vnd.ms-powerpoint.slide.macroEnabled.12"),t.set("onetoc|onetoc2|onetmp|onepkg","application/onenote"),t.set("oxps","application/oxps"),t.set("xps","application/vnd.ms-xpsdocument"),t.set("odt","application/vnd.oasis.opendocument.text"),t.set("odp","application/vnd.oasis.opendocument.presentation"),t.set("ods","application/vnd.oasis.opendocument.spreadsheet"),t.set("odg","application/vnd.oasis.opendocument.graphics"),t.set("odc","application/vnd.oasis.opendocument.chart"),t.set("odb","application/vnd.oasis.opendocument.database"),t.set("odf","application/vnd.oasis.opendocument.formula"),t.set("wp|wpd","application/wordperfect"),t.set("key","application/vnd.apple.keynote"),t.set("numbers","application/vnd.apple.numbers"),t.set("pages","application/vnd.apple.pages"),t})())("*"===s&&n.startsWith(t+"/")||n===i[0])&&e.push(...o.split("|"))}return e})(e).some((e=>(e="."+e.trim(),t.name.toLowerCase().endsWith(e.toLowerCase())))))))))throw new o(this)},h=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>this.accept?.some((e=>t===String(e))))))throw new o(this)},u=function(t){if(!t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).every((t=>{const e=0===(i=new Date(t).getDay())?7:i;var i;return this.accept?.some((t=>e===parseInt(t)))})))throw new o(this)},w=function(t){if(t.getAll(this.field).map((t=>t.trim())).filter((t=>""!==t)).lengtht.trim())).filter((t=>""!==t));if(parseInt(this.threshold)t.trim())).filter((t=>""!==t));let i=0;if(e.forEach((t=>{"string"==typeof t&&(i+=t.length)})),0!==i&&it.trim())).filter((t=>""!==t));let i=0;if(e.forEach((t=>{"string"==typeof t&&(i+=t.length)})),parseInt(this.threshold)t.trim())).filter((t=>""!==t)).every((t=>!(parseFloat(t)t.trim())).filter((t=>""!==t)).every((t=>!(parseFloat(this.threshold)t.trim())).filter((t=>""!==t)).every((t=>!(/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test(t)&&/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test(this.threshold)&&tt.trim())).filter((t=>""!==t)).every((t=>!(/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test(t)&&/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test(this.threshold)&&this.threshold{t instanceof File&&(i+=t.size)})),i{t instanceof File&&(i+=t.size)})),parseInt(this.threshold)t.trim())).filter((t=>""!==t)),i=parseFloat(this.base),s=parseFloat(this.interval);if(!(0{const e=(parseFloat(t)-i)%s;return"0.000000"===Math.abs(e).toFixed(6)||"0.000000"===Math.abs(e-s).toFixed(6)})))throw new o(this)},O=({ruleObj:t,options:i})=>{const{rule:s,...o}=t;return"function"==typeof e[s]&&("function"!=typeof e[s].matches||e[s].matches(o,i))},E=({ruleObj:t,formDataTree:i,options:s})=>{const{rule:o}=t;e[o].call(t,i,s)},k=[],F=t=>[...k].reduce(((t,e)=>i=>e(i,t)),t),D=function(t,e={}){const i=(this.rules??[]).filter((t=>O({ruleObj:t,options:e}))),s=F(E);if(!i.every((i=>{try{s({ruleObj:i,formDataTree:t,options:e})}catch(t){if(!(t instanceof o))throw t;if(void 0!==t.error)throw t;return!1}return!0})))throw new o(this)},M=function(t,e={}){const i=(this.rules??[]).filter((t=>O({ruleObj:t,options:e}))),s=F(E);if(!i.some((i=>{try{s({ruleObj:i,formDataTree:t,options:e})}catch(t){if(!(t instanceof o))throw t;return!1}return!0})))throw new o(this)};var L;window.swv={validators:e,validate:(t,e,i={})=>{const n=(t.rules??[]).filter((t=>O({ruleObj:t,options:i})));if(!n.length)return new Map;const a=F(E),r=new s(e),p=n.reduce(((t,e)=>{try{a({ruleObj:e,formDataTree:r,options:i})}catch(e){if(!(e instanceof o))throw e;if(void 0!==e.field&&!t.has(e.field)&&void 0!==e.error)return t.set(e.field,e)}return t}),new Map);for(const t of r.keys())p.has(t)||p.set(t,{validInputs:r.getAll(t)});return p},use:t=>{k.push(t)},...null!==(L=window.swv)&&void 0!==L?L:{}}})();includes/swv/js/index.asset.php000064400000000121147206643610012546 0ustar00 array(), 'version' => WPCF7_VERSION, ); includes/pocket-holder.php000064400000000505147206643610011634 0ustar00pocket[$key] ) ) { return $this->pocket[$key]; } } public function push( $key, $value ) { $this->pocket[$key] = $value; } } includes/upgrade.php000064400000005777147206643610010543 0ustar00 'any', 'posts_per_page' => -1, ) ); foreach ( $posts as $post ) { $post_id = $post->id(); // Delete the old post meta for config-validation results. delete_post_meta( $post_id, '_config_errors' ); // Add the contact form hash. add_post_meta( $post_id, '_hash', wpcf7_generate_contact_form_hash( $post_id ), true // Unique ); } } add_action( 'wpcf7_upgrade', 'wpcf7_convert_to_cpt', 10, 2 ); /** * Converts old data in the dedicated database table to custom posts. * * @since 3.0.0 `wpcf7_contact_form` CPT is introduced. */ function wpcf7_convert_to_cpt( $new_ver, $old_ver ) { global $wpdb; if ( ! version_compare( $old_ver, '3.0-dev', '<' ) ) { return; } $old_rows = array(); $table_name = $wpdb->prefix . "contact_form_7"; if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) ) { $old_rows = $wpdb->get_results( "SELECT * FROM $table_name" ); } elseif ( $opt = get_option( 'wpcf7' ) and ! empty( $opt['contact_forms'] ) ) { foreach ( (array) $opt['contact_forms'] as $key => $value ) { $old_rows[] = (object) array_merge( $value, array( 'cf7_unit_id' => $key ) ); } } foreach ( (array) $old_rows as $row ) { $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'" . $wpdb->prepare( " AND meta_value = %d", $row->cf7_unit_id ); if ( $wpdb->get_var( $q ) ) { continue; } $postarr = array( 'post_type' => 'wpcf7_contact_form', 'post_status' => 'publish', 'post_title' => maybe_unserialize( $row->title ), ); $post_id = wp_insert_post( $postarr ); if ( $post_id ) { update_post_meta( $post_id, '_old_cf7_unit_id', $row->cf7_unit_id ); $metas = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings', ); foreach ( $metas as $meta ) { update_post_meta( $post_id, '_' . $meta, wpcf7_normalize_newline_deep( maybe_unserialize( $row->{$meta} ) ) ); } } } } add_action( 'wpcf7_upgrade', 'wpcf7_prepend_underscore', 10, 2 ); /** * Prepends an underscore to post meta keys. */ function wpcf7_prepend_underscore( $new_ver, $old_ver ) { if ( version_compare( $old_ver, '3.0-dev', '<' ) ) { return; } if ( ! version_compare( $old_ver, '3.3-dev', '<' ) ) { return; } $posts = WPCF7_ContactForm::find( array( 'post_status' => 'any', 'posts_per_page' => -1, ) ); foreach ( $posts as $post ) { $props = $post->get_properties(); foreach ( $props as $prop => $value ) { if ( metadata_exists( 'post', $post->id(), '_' . $prop ) ) { continue; } update_post_meta( $post->id(), '_' . $prop, $value ); delete_post_meta( $post->id(), $prop ); } } } includes/controller.php000064400000006305147206643610011263 0ustar00submit(); } } } /** * Registers main scripts and styles. */ add_action( 'wp_enqueue_scripts', static function () { $assets = include( wpcf7_plugin_path( 'includes/js/index.asset.php' ) ); $assets = wp_parse_args( $assets, array( 'dependencies' => array(), 'version' => WPCF7_VERSION, ) ); wp_register_script( 'contact-form-7', wpcf7_plugin_url( 'includes/js/index.js' ), array_merge( $assets['dependencies'], array( 'swv' ) ), $assets['version'], array( 'in_footer' => true ) ); wp_set_script_translations( 'contact-form-7', 'contact-form-7' ); wp_register_script( 'contact-form-7-html5-fallback', wpcf7_plugin_url( 'includes/js/html5-fallback.js' ), array( 'jquery-ui-datepicker' ), WPCF7_VERSION, array( 'in_footer' => true ) ); if ( wpcf7_load_js() ) { wpcf7_enqueue_scripts(); } wp_register_style( 'contact-form-7', wpcf7_plugin_url( 'includes/css/styles.css' ), array(), WPCF7_VERSION, 'all' ); wp_register_style( 'contact-form-7-rtl', wpcf7_plugin_url( 'includes/css/styles-rtl.css' ), array( 'contact-form-7' ), WPCF7_VERSION, 'all' ); wp_register_style( 'jquery-ui-smoothness', wpcf7_plugin_url( 'includes/js/jquery-ui/themes/smoothness/jquery-ui.min.css' ), array(), '1.12.1', 'screen' ); if ( wpcf7_load_css() ) { wpcf7_enqueue_styles(); } }, 10, 0 ); /** * Enqueues scripts. */ function wpcf7_enqueue_scripts() { wp_enqueue_script( 'contact-form-7' ); $wpcf7_obj = array( 'api' => array( 'root' => sanitize_url( get_rest_url() ), 'namespace' => 'contact-form-7/v1', ), ); if ( defined( 'WP_CACHE' ) and WP_CACHE ) { $wpcf7_obj = array_merge( $wpcf7_obj, array( 'cached' => 1, ) ); } wp_add_inline_script( 'contact-form-7', sprintf( 'var wpcf7 = %s;', wp_json_encode( $wpcf7_obj, JSON_PRETTY_PRINT ) ), 'before' ); do_action( 'wpcf7_enqueue_scripts' ); } /** * Returns true if the main script is enqueued. */ function wpcf7_script_is() { return wp_script_is( 'contact-form-7' ); } /** * Enqueues styles. */ function wpcf7_enqueue_styles() { wp_enqueue_style( 'contact-form-7' ); if ( wpcf7_is_rtl() ) { wp_enqueue_style( 'contact-form-7-rtl' ); } do_action( 'wpcf7_enqueue_styles' ); } /** * Returns true if the main stylesheet is enqueued. */ function wpcf7_style_is() { return wp_style_is( 'contact-form-7' ); } add_action( 'wp_enqueue_scripts', 'wpcf7_html5_fallback', 20, 0 ); /** * Enqueues scripts and styles for the HTML5 fallback. */ function wpcf7_html5_fallback() { if ( ! wpcf7_support_html5_fallback() ) { return; } if ( wpcf7_script_is() ) { wp_enqueue_script( 'contact-form-7-html5-fallback' ); } if ( wpcf7_style_is() ) { wp_enqueue_style( 'jquery-ui-smoothness' ); } } includes/html-formatter.php000064400000040511147206643610012042 0ustar00options = wp_parse_args( $options, array( 'auto_br' => true, 'auto_indent' => true, ) ); } /** * Separates the given text into chunks of HTML. Each chunk must be an * associative array that includes 'position', 'type', and 'content' keys. * * @param string $input Text to be separated into chunks. * @return iterable Iterable of chunks. */ public function separate_into_chunks( $input ) { $input_bytelength = strlen( $input ); $position = 0; while ( $position < $input_bytelength ) { $next_tag = preg_match( '/(?:|<(?:\/?)[a-z].*?>)/is', $input, $matches, PREG_OFFSET_CAPTURE, $position ); if ( ! $next_tag ) { yield array( 'position' => $position, 'type' => self::text, 'content' => substr( $input, $position ), ); break; } $next_tag = $matches[0][0]; $next_tag_position = $matches[0][1]; if ( $position < $next_tag_position ) { yield array( 'position' => $position, 'type' => self::text, 'content' => substr( $input, $position, $next_tag_position - $position ), ); } if ( ' $next_tag_position, 'type' => $next_tag_type, 'content' => substr( $input, $next_tag_position, strlen( $next_tag ) ), ); $position = $next_tag_position + strlen( $next_tag ); } } /** * Normalizes content in each chunk. This may change the type and position * of the chunk. * * @param iterable $chunks The original chunks. * @return iterable Normalized chunks. */ public function pre_format( $chunks ) { $position = 0; foreach ( $chunks as $chunk ) { $chunk['position'] = $position; // Standardize newline characters to "\n". $chunk['content'] = str_replace( array( "\r\n", "\r" ), "\n", $chunk['content'] ); if ( $chunk['type'] === self::start_tag ) { list( $chunk['content'] ) = self::normalize_start_tag( $chunk['content'] ); // Replace
    by a line break. if ( $this->options['auto_br'] and preg_match( '/^$/i', $chunk['content'] ) ) { $chunk['type'] = self::text; $chunk['content'] = "\n"; } } yield $chunk; $position = self::calc_next_position( $chunk ); } } /** * Concatenates neighboring text chunks to create a single chunk. * * @param iterable $chunks The original chunks. * @return iterable Processed chunks. */ public function concatenate_texts( $chunks ) { $position = 0; $text_left = null; foreach ( $chunks as $chunk ) { $chunk['position'] = $position; if ( $chunk['type'] === self::text ) { if ( isset( $text_left ) ) { $text_left['content'] .= $chunk['content']; } else { $text_left = $chunk; } continue; } if ( isset( $text_left ) ) { yield $text_left; $chunk['position'] = self::calc_next_position( $text_left ); $text_left = null; } yield $chunk; $position = self::calc_next_position( $chunk ); } if ( isset( $text_left ) ) { yield $text_left; } } /** * Outputs formatted HTML based on the given chunks. * * @param iterable $chunks The original chunks. * @return string Formatted HTML. */ public function format( $chunks ) { $chunks = $this->pre_format( $chunks ); $chunks = $this->concatenate_texts( $chunks ); $this->output = ''; $this->stacked_elements = array(); foreach ( $chunks as $chunk ) { if ( $chunk['type'] === self::text ) { $this->append_text( $chunk['content'] ); } if ( $chunk['type'] === self::start_tag ) { $this->start_tag( $chunk['content'] ); } if ( $chunk['type'] === self::end_tag ) { $this->end_tag( $chunk['content'] ); } if ( $chunk['type'] === self::comment ) { $this->append_comment( $chunk['content'] ); } } // Close all remaining tags. $this->close_all_tags(); return $this->output; } /** * Appends a text node content to the output property. * * @param string $content Text node content. */ public function append_text( $content ) { if ( $this->is_inside( array( 'pre', 'template' ) ) ) { $this->output .= $content; return; } if ( empty( $this->stacked_elements ) or $this->has_parent( 'p' ) or $this->has_parent( self::p_parent_elements ) ) { // Close

    if the content starts with multiple line breaks. if ( preg_match( '/^\s*\n\s*\n\s*/', $content ) ) { $this->end_tag( 'p' ); } // Split up the contents into paragraphs, separated by double line breaks. $paragraphs = preg_split( '/\s*\n\s*\n\s*/', $content ); $paragraphs = array_filter( $paragraphs, static function ( $paragraph ) { return '' !== trim( $paragraph ); } ); $paragraphs = array_values( $paragraphs ); if ( $paragraphs ) { if ( $this->is_inside( 'p' ) ) { $paragraph = array_shift( $paragraphs ); $paragraph = self::normalize_paragraph( $paragraph, $this->options['auto_br'] ); $this->output .= $paragraph; } foreach ( $paragraphs as $paragraph ) { $this->start_tag( 'p' ); $paragraph = ltrim( $paragraph ); $paragraph = self::normalize_paragraph( $paragraph, $this->options['auto_br'] ); $this->output .= $paragraph; } } // Close

    if the content ends with multiple line breaks. if ( preg_match( '/\s*\n\s*\n\s*$/', $content ) ) { $this->end_tag( 'p' ); } // Cases where the content is a single line break. if ( preg_match( '/^\s*\n\s*$/', $content ) ) { $auto_br = $this->options['auto_br'] && $this->is_inside( 'p' ); $content = self::normalize_paragraph( $content, $auto_br ); $this->output .= $content; } } else { $auto_br = $this->options['auto_br'] && $this->has_parent( self::br_parent_elements ); $content = self::normalize_paragraph( $content, $auto_br ); $this->output .= $content; } } /** * Appends a start tag to the output property. * * @param string $tag A start tag. */ public function start_tag( $tag ) { list( $tag, $tag_name ) = self::normalize_start_tag( $tag ); if ( in_array( $tag_name, self::p_child_elements ) ) { if ( ! $this->is_inside( 'p' ) and ! $this->is_inside( self::p_child_elements ) and ! $this->has_parent( self::p_nonparent_elements ) ) { // Open

    if it does not exist. $this->start_tag( 'p' ); } } elseif ( 'p' === $tag_name or in_array( $tag_name, self::p_parent_elements ) or in_array( $tag_name, self::p_nonparent_elements ) ) { // Close

    if it exists. $this->end_tag( 'p' ); } if ( 'dd' === $tag_name or 'dt' === $tag_name ) { // Close

    and
    if closing tag is omitted. $this->end_tag( 'dd' ); $this->end_tag( 'dt' ); } if ( 'li' === $tag_name ) { // Close
  • if closing tag is omitted. $this->end_tag( 'li' ); } if ( 'optgroup' === $tag_name ) { // Close if closing tag is omitted. $this->end_tag( 'option' ); $this->end_tag( 'optgroup' ); } if ( 'option' === $tag_name ) { // Close