get( 'form_settings' ); $form_id = $form_settings['id'] ?? 'unknown'; $form_name = $form_settings['form_name'] ?? 'Contact Form'; // Get the raw form field values. $raw_fields = $record->get( 'fields' ); // Build a clean associative array: field_id => value. $fields = array(); foreach ( $raw_fields as $field_id => $field_data ) { $fields[ $field_id ] = $field_data['value'] ?? ''; } // Get source page URL. $referrer = isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; // Build the API payload. $payload = array( 'action' => 'submit', 'form_id' => $form_id, 'form_name' => $form_name, 'fields' => $fields, 'source_url' => $referrer, 'source_ip' => a1tools_get_client_ip(), 'form_secret' => $form_secret, 'crm_company_id' => (int) get_option( 'a1tools_form_crm_company_id', 0 ), 'crm_franchise_id' => (int) get_option( 'a1tools_form_crm_franchise_id', 0 ), ); // API endpoint. $api_url = 'https://tools.a-1chimney.com/api/sunday/forms.php?action=submit'; /** * Filter the A1 Tools form submission API URL. * * @since 1.5.0 * @param string $api_url The API URL. */ $api_url = apply_filters( 'a1tools_form_api_url', $api_url ); // Defer the API call to PHP shutdown so it doesn't interfere with // Elementor's form processing pipeline (MailChimp, email, etc.). // The AJAX response is sent to the browser first, then our request fires. add_action( 'shutdown', function() use ( $api_url, $payload, $form_secret ) { $response = wp_remote_post( $api_url, array( 'timeout' => 15, 'httpversion' => '1.1', 'blocking' => false, 'headers' => array( 'Content-Type' => 'application/json', 'X-Form-Secret' => $form_secret, ), 'body' => wp_json_encode( $payload ), 'sslverify' => true, ) ); if ( is_wp_error( $response ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( '[A1 Tools] Form submission API error: ' . $response->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- guarded by WP_DEBUG } } }); } // Only register the form hook when form forwarding is enabled. // This prevents any interference with other Elementor form actions (e.g., MailChimp). if ( get_option( 'a1tools_form_enabled', false ) ) { add_action( 'elementor_pro/forms/new_record', 'a1tools_handle_elementor_form_submission', 99, 2 ); } /** * Get client IP address, handling proxies. * * @since 1.5.0 * @return string Client IP. */ function a1tools_get_client_ip() { if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $ips = explode( ',', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ); return trim( $ips[0] ); } if ( ! empty( $_SERVER['HTTP_X_REAL_IP'] ) ) { return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); } return isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '0.0.0.0'; }