settings['formName'] ) ) { return (string) $form->settings['formName']; } } return self::post_title( $form_id, 'forminator_forms' ); } /** * {@inheritDoc} */ public function register() { add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'capture' ), 10, 3 ); } /** * Stores the submission. * * @param object $entry Entry model. * @param int $form_id Form ID. * @param array $field_data Submitted fields: name, value. */ public function capture( $entry, $form_id, $field_data ) { $labels = $this->labels( $form_id ); $fields = array(); foreach ( (array) $field_data as $item ) { $name = (string) ( $item['name'] ?? '' ); if ( '' === $name || 0 === strpos( $name, '_' ) ) { continue; } $value = $item['value'] ?? ''; $type = (string) preg_replace( '/-\d+$/', '', $name ); // Name and address fields arrive as associative arrays, e.g. first-name / last-name. if ( is_array( $value ) && array_keys( $value ) !== range( 0, count( $value ) - 1 ) ) { foreach ( $value as $sub => $sub_value ) { $fields[] = array( 'label' => str_replace( '-', ' ', (string) $sub ), 'name' => (string) $sub, 'type' => '', 'value' => $sub_value, ); } continue; } $fields[] = array( 'label' => $labels[ $name ] ?? $name, 'name' => $name, 'type' => 'phone' === $type ? 'tel' : $type, 'value' => $value, ); } Capture::submission( self::key(), (string) $form_id, $this->form_name( $form_id ), $fields ); } /** * Field labels keyed by slug. * * @param int $form_id Form ID. * @return array */ private function labels( $form_id ) { $labels = array(); if ( ! class_exists( '\Forminator_API' ) ) { return $labels; } $form_fields = \Forminator_API::get_form_fields( $form_id ); if ( is_wp_error( $form_fields ) ) { return $labels; } foreach ( (array) $form_fields as $field ) { $data = is_object( $field ) && method_exists( $field, 'to_formatted_array' ) ? $field->to_formatted_array() : (array) $field; if ( ! empty( $data['element_id'] ) ) { $labels[ $data['element_id'] ] = $data['field_label'] ?? $data['element_id']; } } return $labels; } /** * Form title. * * @param int $form_id Form ID. * @return string */ private function form_name( $form_id ) { if ( class_exists( '\Forminator_API' ) ) { $form = \Forminator_API::get_form( $form_id ); if ( ! is_wp_error( $form ) && ! empty( $form->settings['formName'] ) ) { return (string) $form->settings['formName']; } } return (string) get_the_title( $form_id ); } }