array( 'your-name', 'name', 'first-name', 'firstname' ), 'email' => array( 'your-email', 'email', 'email1' ), 'tel' => array( 'tel', 'phone', 'mobile', 'mobilephone' ), 'textarea' => array( 'your-message', 'message', 'summary' ), 'select' => array( 'source', 'your-source' ), ); /** * Hooks sending into Contact Form 7. */ public static function register() { add_action( 'wpcf7_mail_sent', array( __CLASS__, 'send' ) ); } /** * Sends a submission whose mail Contact Form 7 has just sent, when the form is set up for it. * * @param WPCF7_ContactForm $contact_form The submitted form. */ public static function send( $contact_form ) { $form_id = (int) $contact_form->id(); $endpoint = AFCFLD_Form_Settings::get_endpoint( $form_id ); $mapping = AFCFLD_Form_Settings::get_mapping( $form_id ); if ( ! AFCFLD_Form_Settings::is_enabled( $form_id ) || '' === $endpoint || ! $mapping ) { return; } // Demo mode only skips the mail, so Contact Form 7 still reports it as sent; a test must not become a lead. if ( $contact_form->in_demo_mode() ) { return; } /** * Filters whether a submission goes to Lead Docket, for example to keep a staging site from creating leads. * * @param bool $send Whether to send. Default true. * @param WPCF7_ContactForm $contact_form The submitted form. */ if ( ! apply_filters( 'afcfld_should_send', true, $contact_form ) ) { return; } $submission = WPCF7_Submission::get_instance(); if ( ! $submission ) { return; } $payload = array(); foreach ( $mapping as $field => $template ) { $payload[ $field ] = self::fill( $template, $submission ); } // Lead Docket allows texting when the field is missing, so an unticked consent box has to send false. $consent_field = AFCFLD_Form_Settings::get_consent_field( $form_id ); $texting_field = AFCFLD_Form_Settings::get_texting_field( $form_id ); if ( '' !== $consent_field && ! array_key_exists( $texting_field, $payload ) && $contact_form->scan_form_tags( array( 'name' => $consent_field ) ) ) { $payload[ $texting_field ] = self::is_ticked( $submission, $consent_field ) ? 'true' : 'false'; } $submitted_at = (int) $submission->get_meta( 'timestamp' ); // Fields of the mapping keep their own values. $payload += array( 'formId' => (string) $form_id, 'formTitle' => $contact_form->title(), 'submittedAt' => gmdate( 'c', $submitted_at ? $submitted_at : time() ), ); /** * Filters the fields sent to Lead Docket. * * @param array $payload Lead Docket field names and values. * @param WPCF7_ContactForm $contact_form The submitted form. * @param WPCF7_Submission $submission The submission. */ $payload = apply_filters( 'afcfld_payload', $payload, $contact_form, $submission ); /** * Filters the arguments of the request to Lead Docket, such as its timeout. * * @param array $args Arguments for wp_safe_remote_post(). * @param WPCF7_ContactForm $contact_form The submitted form. */ $args = apply_filters( 'afcfld_request_args', array( 'timeout' => 20, 'body' => $payload, ), $contact_form ); $response = wp_safe_remote_post( $endpoint, $args ); AFCFLD_Form_Settings::add_log_entry( $form_id, array( 'time' => time(), 'code' => is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ), 'error' => is_wp_error( $response ) ? $response->get_error_message() : '', ) ); /** * Fires after a submission was sent to Lead Docket, whatever the outcome. * * @param array|WP_Error $response The response, or the error that stopped the request. * @param array $payload The fields that were sent. * @param WPCF7_ContactForm $contact_form The submitted form. */ do_action( 'afcfld_response', $response, $payload, $contact_form ); } /** * Replaces the tags in a mapping value with the submission's values. * * @param string $template The mapping value, such as "[first-name] [last-name]". * @param WPCF7_Submission $submission The submission. * @return string */ public static function fill( $template, $submission ) { $posted = (array) $submission->get_posted_data(); $value = preg_replace_callback( self::TAG_PATTERN, static function ( $matches ) use ( $posted, $submission ) { return self::tag_value( trim( $matches[1] ), $matches[0], $posted, $submission ); }, (string) $template ); return trim( (string) $value ); } /** * The value of one tag. * * @param string $name The tag's name, such as "your-email". * @param string $tag The tag as written, brackets included. * @param array $posted The submission's posted data. * @param WPCF7_Submission $submission The submission. * @return string */ private static function tag_value( $name, $tag, $posted, $submission ) { if ( 'page-url' === $name ) { return self::page_url( $submission ); } // Contact Form 7's own special tags, such as [_raw_your-field], [_post_url] or [_remote_ip]. if ( 0 === strpos( $name, '_' ) ) { $value = wpcf7_mail_replace_tags( $tag ); return $value === $tag ? '' : (string) $value; } $fields = isset( self::SHORTCUTS[ $name ] ) ? self::SHORTCUTS[ $name ] : array(); $fields[] = $name; foreach ( $fields as $field ) { if ( isset( $posted[ $field ] ) ) { $value = $posted[ $field ]; return is_array( $value ) ? implode( ', ', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) ) : (string) $value; } } return ''; } /** * Whether a checkbox was ticked: Contact Form 7 posts a ticked box's value and leaves an unticked one empty. * * @param WPCF7_Submission $submission The submission. * @param string $field The checkbox's name. * @return bool */ private static function is_ticked( $submission, $field ) { $value = $submission->get_posted_data( $field ); $value = is_array( $value ) ? implode( '', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) ) : (string) $value; return '' !== trim( $value ); } /** * The address of the page the form was sent from, without its query string. * * @param WPCF7_Submission $submission The submission. * @return string */ private static function page_url( $submission ) { $post_id = (int) $submission->get_meta( 'container_post_id' ); $url = $post_id ? get_permalink( $post_id ) : ''; // Contact Form 7 knows the post only for forms in the main content. Otherwise it keeps the referring // address, or its own REST address when the browser sent no referrer. if ( ! $url ) { $url = preg_replace( '/[?#].*$/', '', (string) $submission->get_meta( 'url' ) ); if ( false !== strpos( $url, 'contact-form-7/v1' ) ) { $url = ''; } } return $url ? $url : home_url( '/' ); } }