prefix . 'tt24_forms'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching $form_data = $wpdb->get_row( $wpdb->prepare( "SELECT title, form_settings FROM {$table_forms} WHERE id = %d", $form_id ) ); // phpcs:enable if ( ! $form_data ) { return false; } $form_title = $form_data->title ? $form_data->title : esc_html__( 'Unrestricted Form', '24tt-unrestricted-forms' ); $site_name = get_bloginfo( 'name' ); // Securely decode the JSON settings $parsed_data = json_decode( $form_data->form_settings, true ); $settings = isset( $parsed_data['settings'] ) ? $parsed_data['settings'] : array(); $layout = isset( $parsed_data['layout'] ) ? $parsed_data['layout'] : (is_array($parsed_data) ? $parsed_data : array()); // 1. BUILD THE TRANSLATION DICTIONARY // We map the ugly database keys to the beautiful human labels! $field_dictionary = array(); if ( is_array( $layout ) ) { foreach ( $layout as $field ) { if ( isset( $field['name'] ) && isset( $field['label'] ) ) { $field_dictionary[ $field['name'] ] = $field['label']; } } } // 2. Build the HTML Data Table $fields_html = ''; $fields_html .= ''; $reply_to = ''; // We will hunt for an email to use as a fallback $ignored_fields = array( 'action', 'tt24_form_id', 'tt24_submit_nonce', '_wp_http_referer' ); foreach ( $post_data as $key => $value ) { if ( in_array( $key, $ignored_fields, true ) ) { continue; } // TRANSLATION MAGIC: Look up the beautiful label, fallback to a cleaned-up key if not found if ( isset( $field_dictionary[ $key ] ) ) { $display_key = $field_dictionary[ $key ]; } else { $display_key = ucwords( str_replace( array( '_', '-' ), ' ', sanitize_text_field( $key ) ) ); } if ( is_array( $value ) ) { $display_value = implode( ', ', array_map( 'sanitize_textarea_field', wp_unslash( $value ) ) ); } else { $display_value = sanitize_textarea_field( wp_unslash( $value ) ); } $fields_html .= ''; $fields_html .= ''; $fields_html .= ''; $fields_html .= ''; // SMART HUNT: Find the first valid email address in the submission if ( empty( $reply_to ) && is_email( $display_value ) ) { $reply_to = sanitize_email( $display_value ); } } $fields_html .= '
' . esc_html( $display_key ) . '' . nl2br( esc_html( $display_value ) ) . '
'; // 3. PROCESS ADMIN NOTIFICATION $admin_enabled = isset($settings['admin_email_enable']) ? filter_var($settings['admin_email_enable'], FILTER_VALIDATE_BOOLEAN) : true; if ( $admin_enabled ) { $to_email = !empty($settings['admin_email_to']) ? $settings['admin_email_to'] : '{admin_email}'; $to_email = str_replace('{admin_email}', get_option('admin_email'), $to_email); $to_email = apply_filters( 'tt24_forms_notification_email', $to_email, $form_id ); $subject = !empty($settings['admin_email_subject']) ? $settings['admin_email_subject'] : sprintf( esc_html__( '[%1$s] New Submission: %2$s', '24tt-unrestricted-forms' ), $site_name, $form_title ); $subject = str_replace('{form_title}', $form_title, $subject); $message_body = !empty($settings['admin_email_msg']) ? $settings['admin_email_msg'] : esc_html__( "You have received a new form submission. Here are the details:\n\n{all_fields}", '24tt-unrestricted-forms' ); $message_html = nl2br( esc_html( $message_body ) ); $message_html = str_replace( '{all_fields}', $fields_html, $message_html ); $final_admin_msg = self::get_email_template($form_title, $message_html, $site_name); $headers = array('Content-Type: text/html; charset=UTF-8'); if ( ! empty( $reply_to ) ) { $headers[] = 'Reply-To: ' . $reply_to; } wp_mail( $to_email, $subject, $final_admin_msg, $headers ); } // 4. PROCESS USER AUTO-RESPONDER $user_enabled = isset($settings['user_email_enable']) ? filter_var($settings['user_email_enable'], FILTER_VALIDATE_BOOLEAN) : false; if ( $user_enabled ) { $user_email_address = ''; // Try to use the specific field mapped in the settings if ( !empty($settings['user_email_field']) && isset($post_data[$settings['user_email_field']]) ) { $user_email_address = sanitize_email( wp_unslash( $post_data[$settings['user_email_field']] ) ); } // SMART FALLBACK: If the mapped field failed, use the email we hunted down earlier! if ( empty($user_email_address) || !is_email($user_email_address) ) { $user_email_address = $reply_to; } // Only send if we definitively found an email address if ( is_email( $user_email_address ) ) { $user_subject = !empty($settings['user_email_subject']) ? $settings['user_email_subject'] : esc_html__('Thank you for your submission', '24tt-unrestricted-forms'); $user_subject = str_replace('{form_title}', $form_title, $user_subject); $user_message_body = !empty($settings['user_email_msg']) ? $settings['user_email_msg'] : esc_html__("Hi there,\n\nThank you for contacting us! We have received your information and will be in touch shortly.", '24tt-unrestricted-forms'); $user_message_html = nl2br( esc_html( $user_message_body ) ); $user_message_html = str_replace( '{all_fields}', $fields_html, $user_message_html ); $final_user_msg = self::get_email_template($form_title, $user_message_html, $site_name); $headers_user = array('Content-Type: text/html; charset=UTF-8'); // Reply-To for User goes to Admin $admin_reply_to = get_option('admin_email'); if (!empty($settings['admin_email_to']) && is_email($settings['admin_email_to'])) { $admin_reply_to = sanitize_email($settings['admin_email_to']); } $headers_user[] = 'Reply-To: ' . $admin_reply_to; wp_mail( $user_email_address, $user_subject, $final_user_msg, $headers_user ); } } return true; } /** * Helper method to wrap email content in a clean, responsive HTML template. */ private static function get_email_template($title, $content, $site_name) { $html = ''; $html .= '
'; $html .= '

' . esc_html( $title ) . '

'; $html .= '
'; $html .= $content; $html .= '

'; $html .= sprintf( esc_html__( 'This email was generated automatically by 24TT Unrestricted Forms on %s.', '24tt-unrestricted-forms' ), esc_html( $site_name ) ); $html .= '

'; $html .= '
'; return $html; } }