get_posted_data()` - field name => * value; checkbox and acceptance fields are arrays (empty when unticked). * - `WPCF7_ContactForm::find()` / `get_instance( $id )` and * `scan_form_tags()` describe the forms. An acceptance field's wording is * its content (or first value); with the `invert` option, ticking it * means "no". * * @package Emails404 */ namespace Emails404\Providers; if ( ! defined( 'ABSPATH' ) ) { exit; } use Emails404\Form_Connections; use Emails404\Form_Source_Interface; class Contact_Form_7_Provider implements Form_Source_Interface { const SLUG = 'contact_form_7'; public function get_slug() { return self::SLUG; } public function get_label() { return 'Contact Form 7'; } public function is_available() { return class_exists( 'WPCF7_ContactForm' ); } public function register_hooks() { add_action( 'wpcf7_mail_sent', array( $this, 'handle_submission' ), 10, 1 ); add_action( 'wpcf7_mail_failed', array( $this, 'handle_submission' ), 10, 1 ); } public function list_forms() { if ( ! $this->is_available() ) { return array(); } $forms = array(); foreach ( \WPCF7_ContactForm::find() as $contact_form ) { $forms[] = self::describe( $contact_form ); } return $forms; } public function describe_form( $form_id ) { if ( ! $this->is_available() || ! ctype_digit( (string) $form_id ) ) { return null; } $contact_form = \WPCF7_ContactForm::get_instance( (int) $form_id ); return $contact_form ? self::describe( $contact_form ) : null; } /** @param \WPCF7_ContactForm $contact_form */ public function handle_submission( $contact_form ) { if ( ! is_object( $contact_form ) || ! method_exists( $contact_form, 'id' ) || ! class_exists( 'WPCF7_Submission' ) ) { return; } $submission = \WPCF7_Submission::get_instance(); if ( ! $submission ) { return; } Form_Connections::receive( $this, (string) $contact_form->id(), (array) $submission->get_posted_data(), wp_generate_uuid4(), self::describe( $contact_form ) ); } /** @param \WPCF7_ContactForm $contact_form */ public static function describe( $contact_form ) { return array( 'id' => (string) $contact_form->id(), 'title' => (string) $contact_form->title(), 'fields' => self::fields_from_tags( $contact_form->scan_form_tags() ), ); } /** * @param array $tags WPCF7_FormTag objects. * @return array */ public static function fields_from_tags( array $tags ) { $fields = array(); $seen = array(); foreach ( $tags as $tag ) { $name = isset( $tag->name ) ? (string) $tag->name : ''; if ( '' === $name || isset( $seen[ $name ] ) ) { continue; } $seen[ $name ] = true; $basetype = isset( $tag->basetype ) ? (string) $tag->basetype : ''; $first_value = isset( $tag->values ) && is_array( $tag->values ) && $tag->values ? (string) reset( $tag->values ) : ''; $first_label = isset( $tag->labels ) && is_array( $tag->labels ) && $tag->labels ? (string) reset( $tag->labels ) : $first_value; if ( 'email' === $basetype ) { $fields[] = array( 'key' => $name, 'label' => $name, 'kind' => Form_Connections::KIND_EMAIL ); } elseif ( 'acceptance' === $basetype ) { $content = isset( $tag->content ) && '' !== trim( (string) $tag->content ) ? (string) $tag->content : $first_value; $fields[] = array( 'key' => $name, 'label' => trim( wp_strip_all_tags( $content ) ), 'kind' => Form_Connections::KIND_CONSENT, 'invert' => is_object( $tag ) && method_exists( $tag, 'has_option' ) && $tag->has_option( 'invert' ), ); } elseif ( 'checkbox' === $basetype ) { $fields[] = array( 'key' => $name, 'label' => '' !== $first_label ? $first_label : $name, 'kind' => Form_Connections::KIND_CONSENT ); } elseif ( 'text' === $basetype ) { $fields[] = array( 'key' => $name, 'label' => $name, 'kind' => Form_Connections::kind_from_words( $name, '' ) ); } } return $fields; } }