'text', 'input_name' => 'name', 'phone' => 'phone', 'input_email' => 'email', 'input_number' => 'number', 'textarea' => 'textarea', ); /** @var array entries already handled on this request */ private $done = array(); public function key() { return 'ff'; } public function name() { return 'Fluent Forms'; } public function active() { return defined( 'FLUENTFORM' ) || function_exists( 'wpFluentForm' ); } public function forms() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Fluent Forms' own table, read on the settings page only. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, title, form_fields FROM %i ORDER BY title', $wpdb->prefix . 'fluentform_forms' ) ); $forms = array(); foreach ( (array) $rows as $row ) { $data = json_decode( (string) $row->form_fields, true ); $fields = array(); $this->collect( (array) ( $data['fields'] ?? array() ), $fields ); $forms[ (string) $row->id ] = array( 'title' => (string) $row->title, 'fields' => $fields, ); } return $forms; } public function listen() { add_action( 'fluentform/submission_inserted', array( $this, 'on_submit' ), 20, 3 ); add_action( 'fluentform_submission_inserted', array( $this, 'on_submit' ), 20, 3 ); } /** * @param int $entry_id * @param array $data Submitted values by field name (Name fields as an array of parts). * @param object|array $form The form (id, title). */ public function on_submit( $entry_id, $data, $form ) { $form = (object) $form; $form_id = (string) ( $form->id ?? '' ); $once = $form_id . ':' . $entry_id; if ( '' === $form_id || isset( $this->done[ $once ] ) ) { return; } $this->done[ $once ] = true; $answers = array(); foreach ( (array) $data as $key => $value ) { if ( '_' === substr( (string) $key, 0, 1 ) ) { continue; // _wp_http_referer, _fluentform_*_fluentformnonce … } // Name / address parts read as one text, separated by spaces. $answers[ (string) $key ] = is_array( $value ) && array_keys( $value ) !== range( 0, count( $value ) - 1 ) ? trim( implode( ' ', array_filter( array_map( array( $this, 'text' ), $value ), 'strlen' ) ) ) : $this->text( $value ); } Beon_Notify_Forms::submitted( $this->key(), $form_id, $answers, (string) ( $form->title ?? '' ), $data ); } private function collect( array $elements, array &$fields ) { foreach ( $elements as $el ) { if ( ! is_array( $el ) ) { continue; } $element = (string) ( $el['element'] ?? '' ); // Containers hold columns, each with its own fields. foreach ( (array) ( $el['columns'] ?? array() ) as $column ) { $this->collect( (array) ( $column['fields'] ?? array() ), $fields ); } $name = (string) ( $el['attributes']['name'] ?? '' ); if ( '' === $name || in_array( $element, self::SKIP, true ) ) { continue; } $label = (string) ( $el['settings']['label'] ?? '' ); if ( '' === $label ) { $label = (string) ( $el['settings']['admin_field_label'] ?? $name ); } $fields[] = array( 'key' => $name, 'label' => wp_strip_all_tags( $label ) ?: $name, 'type' => self::TYPES[ $element ] ?? $element, ); } } }