[ label, detection callback ]. */ public static function integrations() { return array( 'cf7' => array( 'Contact Form 7', static function () { return defined( 'WPCF7_VERSION' ); } ), 'wpforms' => array( 'WPForms', static function () { return defined( 'WPFORMS_VERSION' ); } ), 'gravity' => array( 'Gravity Forms', static function () { return class_exists( 'GFForms' ); } ), 'ninja' => array( 'Ninja Forms', static function () { return class_exists( 'Ninja_Forms' ); } ), 'elementor' => array( 'Elementor Pro Forms', static function () { return defined( 'ELEMENTOR_PRO_VERSION' ); } ), 'fluent' => array( 'Fluent Forms', static function () { return defined( 'FLUENTFORM' ) || defined( 'FLUENTFORM_VERSION' ); } ), 'formidable' => array( 'Formidable Forms', static function () { return class_exists( 'FrmAppHelper' ); } ), 'nodebook' => array( __( 'Nodebook form (shortcode)', 'nodebook' ), '__return_true' ), ); } /** Integration keys without labels: safe before translations load (WordPress 6.7+ warns otherwise). */ public static function keys() { return array( 'cf7', 'wpforms', 'gravity', 'ninja', 'elementor', 'fluent', 'formidable', 'nodebook' ); } public static function enabled( $key ) { return in_array( $key, (array) Nodebook_Settings::get( 'forms' ), true ); } public static function init() { if ( self::enabled( 'cf7' ) ) { add_action( 'wpcf7_submit', array( __CLASS__, 'cf7' ), 10, 2 ); } if ( self::enabled( 'wpforms' ) ) { add_action( 'wpforms_process_complete', array( __CLASS__, 'wpforms' ), 10, 4 ); } if ( self::enabled( 'gravity' ) ) { add_action( 'gform_after_submission', array( __CLASS__, 'gravity' ), 10, 2 ); } if ( self::enabled( 'ninja' ) ) { add_action( 'ninja_forms_after_submission', array( __CLASS__, 'ninja' ) ); } if ( self::enabled( 'elementor' ) ) { add_action( 'elementor_pro/forms/new_record', array( __CLASS__, 'elementor' ), 10, 2 ); } if ( self::enabled( 'fluent' ) ) { add_action( 'fluentform/submission_inserted', array( __CLASS__, 'fluent' ), 10, 3 ); } if ( self::enabled( 'formidable' ) ) { add_action( 'frm_after_create_entry', array( __CLASS__, 'formidable' ), 30, 2 ); } } private static function safe( callable $fn ) { try { $fn(); } catch ( \Throwable $e ) { // Never break a visitor's form submission because of the CRM. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-only diagnostics. error_log( 'Nodebook: ' . $e->getMessage() ); } } } /* Contact Form 7 ----------------------------------------------------- */ public static function cf7( $form, $result ) { if ( empty( $result['status'] ) || ! in_array( $result['status'], array( 'mail_sent', 'mail_failed' ), true ) ) { return; } self::safe( static function () use ( $form ) { $submission = class_exists( 'WPCF7_Submission' ) ? WPCF7_Submission::get_instance() : null; if ( ! $submission ) { return; } $posted = (array) $submission->get_posted_data(); $types = array(); $labels = array(); foreach ( (array) $form->scan_form_tags() as $tag ) { if ( ! empty( $tag->name ) ) { $types[ $tag->name ] = $tag->basetype; // An acceptance box's own text ("Yes, send me the newsletter") is its label and the consent wording. if ( 'acceptance' === $tag->basetype && ! empty( $tag->content ) ) { $labels[ $tag->name ] = trim( wp_strip_all_tags( (string) $tag->content ) ); } } } $fields = array(); foreach ( $posted as $key => $value ) { if ( 0 === strpos( (string) $key, '_' ) ) { continue; } $fields[] = array( 'label' => $labels[ $key ] ?? ucwords( str_replace( array( 'your-', '-', '_' ), array( '', ' ', ' ' ), (string) $key ) ), 'key' => (string) $key, 'type' => isset( $types[ $key ] ) ? $types[ $key ] : '', 'value' => $value, ); } Nodebook_Capture::from_fields( $fields, 'Contact Form 7', $form->title(), $form->id() ); } ); } /* WPForms ------------------------------------------------------------ */ public static function wpforms( $fields, $entry, $form_data, $entry_id ) { self::safe( static function () use ( $fields, $form_data ) { $list = array(); foreach ( (array) $fields as $f ) { $type = isset( $f['type'] ) ? $f['type'] : ''; if ( 'name' === $type && ( ! empty( $f['first'] ) || ! empty( $f['last'] ) ) ) { $list[] = array( 'label' => 'First name', 'key' => 'first', 'type' => 'text', 'value' => $f['first'] ?? '' ); $list[] = array( 'label' => 'Last name', 'key' => 'last', 'type' => 'text', 'value' => $f['last'] ?? '' ); continue; } $list[] = array( 'label' => $f['name'] ?? '', 'key' => (string) ( $f['id'] ?? '' ), 'type' => $type, 'value' => $f['value'] ?? '', ); } Nodebook_Capture::from_fields( $list, 'WPForms', $form_data['settings']['form_title'] ?? '', (int) ( $form_data['id'] ?? 0 ) ); } ); } /* Gravity Forms ------------------------------------------------------ */ public static function gravity( $entry, $form ) { self::safe( static function () use ( $entry, $form ) { $list = array(); foreach ( (array) ( $form['fields'] ?? array() ) as $field ) { $type = is_object( $field ) ? $field->type : ( $field['type'] ?? '' ); $label = is_object( $field ) ? $field->label : ( $field['label'] ?? '' ); $inputs = is_object( $field ) ? $field->inputs : ( $field['inputs'] ?? null ); $fid = is_object( $field ) ? $field->id : ( $field['id'] ?? 0 ); if ( is_array( $inputs ) && $inputs ) { foreach ( $inputs as $in ) { $val = $entry[ (string) $in['id'] ] ?? ''; if ( '' === $val ) { continue; } $list[] = array( 'label' => ( 'name' === $type || 'address' === $type ) ? ( $in['label'] ?? '' ) : $label . ' ' . ( $in['label'] ?? '' ), 'key' => (string) $in['id'], 'type' => 'checkbox' === $type ? 'checkbox' : '', 'value' => $val, ); } continue; } $list[] = array( 'label' => $label, 'key' => (string) $fid, 'type' => 'phone' === $type ? 'tel' : $type, 'value' => $entry[ (string) $fid ] ?? '', ); } Nodebook_Capture::from_fields( $list, 'Gravity Forms', $form['title'] ?? '', (int) ( $form['id'] ?? 0 ) ); } ); } /* Ninja Forms -------------------------------------------------------- */ public static function ninja( $form_data ) { self::safe( static function () use ( $form_data ) { $list = array(); foreach ( (array) ( $form_data['fields'] ?? array() ) as $f ) { $list[] = array( 'label' => $f['label'] ?? '', 'key' => $f['key'] ?? '', 'type' => $f['type'] ?? '', 'value' => $f['value'] ?? '', ); } Nodebook_Capture::from_fields( $list, 'Ninja Forms', $form_data['settings']['title'] ?? '', (int) ( $form_data['form_id'] ?? 0 ) ); } ); } /* Elementor Pro ------------------------------------------------------ */ public static function elementor( $record, $handler ) { self::safe( static function () use ( $record ) { $list = array(); foreach ( (array) $record->get( 'fields' ) as $id => $f ) { $list[] = array( 'label' => $f['title'] ?? '', 'key' => (string) $id, 'type' => $f['type'] ?? '', 'value' => $f['value'] ?? '', ); } Nodebook_Capture::from_fields( $list, 'Elementor', (string) $record->get_form_settings( 'form_name' ), 0 ); } ); } /* Fluent Forms ------------------------------------------------------- */ /** Fluent Forms: field name => [ label, type ], read from the form definition (falls back to the name). */ private static function fluent_labels( $form ) { $out = array(); $schema = is_object( $form ) && ! empty( $form->form_fields ) ? json_decode( (string) $form->form_fields, true ) : array(); $walk = static function ( $fields ) use ( &$walk, &$out ) { foreach ( (array) $fields as $f ) { if ( ! is_array( $f ) ) { continue; } if ( ! empty( $f['columns'] ) ) { foreach ( (array) $f['columns'] as $col ) { $walk( $col['fields'] ?? array() ); } } $name = $f['attributes']['name'] ?? ''; $label = $f['settings']['label'] ?? ''; $label = '' !== (string) $label ? $label : ( $f['settings']['admin_field_label'] ?? '' ); $type = str_replace( 'input_', '', (string) ( $f['element'] ?? '' ) ); if ( '' !== (string) $name ) { $out[ $name ] = array( wp_strip_all_tags( (string) $label ), $type ); } // Name fields hold first / middle / last as sub-fields. foreach ( (array) ( $f['fields'] ?? array() ) as $sub => $sf ) { if ( is_array( $sf ) && '' !== (string) $name ) { $out[ $name . '.' . $sub ] = array( wp_strip_all_tags( (string) ( $sf['settings']['label'] ?? '' ) ), $sub ); } } } }; $walk( $schema['fields'] ?? array() ); return $out; } public static function fluent( $entry_id, $form_data, $form ) { self::safe( static function () use ( $form_data, $form ) { $labels = self::fluent_labels( $form ); $label = static function ( $key, $fallback ) use ( $labels ) { return isset( $labels[ $key ] ) && '' !== $labels[ $key ][0] ? $labels[ $key ][0] : str_replace( '_', ' ', (string) $fallback ); }; $type = static function ( $key ) use ( $labels ) { $t = isset( $labels[ $key ] ) ? $labels[ $key ][1] : ''; return in_array( $t, array( 'email', 'phone', 'textarea' ), true ) ? ( 'phone' === $t ? 'tel' : $t ) : ''; }; $list = array(); foreach ( (array) $form_data as $key => $value ) { if ( 0 === strpos( (string) $key, '_' ) || in_array( $key, array( '__fluent_form_embded_post_id', 'g-recaptcha-response', 'h-captcha-response', 'cf-turnstile-response' ), true ) ) { continue; } if ( is_array( $value ) && array_keys( $value ) !== range( 0, count( $value ) - 1 ) ) { foreach ( $value as $sub => $sv ) { $list[] = array( // Keep the sub-field name ("first name") so names are split correctly. 'label' => str_replace( '_', ' ', (string) $sub ), 'key' => $key . '.' . $sub, 'type' => '', 'value' => $sv, ); } continue; } $list[] = array( 'label' => $label( $key, $key ), 'key' => (string) $key, 'type' => $type( $key ), 'value' => $value, ); } Nodebook_Capture::from_fields( $list, 'Fluent Forms', is_object( $form ) ? (string) $form->title : '', is_object( $form ) ? (int) $form->id : 0 ); } ); } /* Formidable --------------------------------------------------------- */ public static function formidable( $entry_id, $form_id ) { self::safe( static function () use ( $entry_id, $form_id ) { if ( ! class_exists( 'FrmEntry' ) || ! class_exists( 'FrmField' ) ) { return; } $entry = FrmEntry::getOne( $entry_id, true ); $fields = FrmField::get_all_for_form( $form_id ); $list = array(); foreach ( (array) $fields as $f ) { $list[] = array( 'label' => $f->name, 'key' => $f->field_key, 'type' => $f->type, 'value' => isset( $entry->metas[ $f->id ] ) ? $entry->metas[ $f->id ] : '', ); } $title = class_exists( 'FrmForm' ) ? FrmForm::getName( $form_id ) : ''; Nodebook_Capture::from_fields( $list, 'Formidable Forms', (string) $title, (int) $form_id ); } ); } }