maybe_create_table(); } /** * Create database table if it doesn't exist */ private function maybe_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'awke_form_entries'; // Drop the existing table if it exists $wpdb->query("DROP TABLE IF EXISTS $table_name"); // Create new table with updated structure $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, form_id varchar(50) NOT NULL, form_name varchar(255) NOT NULL, field_data longtext NOT NULL, user_ip varchar(100) NOT NULL, user_agent text NOT NULL, submitted_at datetime NOT NULL, status varchar(20) NOT NULL DEFAULT 'unread', PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } /** * Handle form submission */ public function handle_form_submission() { // Verify nonce if (!isset($_POST['awke_nonce']) || !wp_verify_nonce($_POST['awke_nonce'], 'awke_contact_form_nonce')) { wp_send_json_error([ 'message' => esc_html__('Security check failed. Please refresh the page and try again.', 'advanced-widget-kit-for-elementor') ]); wp_die(); } // Verify AJAX request if (!wp_doing_ajax()) { wp_send_json_error([ 'message' => esc_html__('Invalid request method.', 'advanced-widget-kit-for-elementor') ]); wp_die(); } // Get form data $form_id = isset($_POST['form_id']) ? sanitize_text_field($_POST['form_id']) : ''; $form_name = isset($_POST['form_name']) ? sanitize_text_field($_POST['form_name']) : 'Contact Form'; // Format fields data $fields = []; foreach ($_POST as $key => $value) { if (!in_array($key, ['action', 'form_id', 'awke_nonce', '_wp_http_referer'])) { $fields[] = [ 'label' => ucfirst(str_replace(['_', '-'], ' ', $key)), 'value' => is_array($value) ? implode(', ', $value) : sanitize_text_field($value), 'type' => 'text' ]; } } // Add form name to fields $fields[] = [ 'label' => 'Form name', 'value' => $form_name, 'type' => 'text' ]; // Validate required fields $errors = $this->validate_fields($fields); if (!empty($errors)) { wp_send_json_error(['message' => $errors]); } // Save to database $entry_id = $this->save_form_entry($form_id, $fields); if (!$entry_id) { global $wpdb; wp_send_json_error([ 'message' => esc_html__('Failed to save form submission.', 'advanced-widget-kit-for-elementor'), 'debug' => [ 'last_error' => $wpdb->last_error, 'last_query' => $wpdb->last_query, 'form_id' => $form_id, 'form_name' => $form_name ] ]); wp_die(); } // Send email notifications $this->send_notifications($form_id, $fields); wp_send_json_success([ 'message' => esc_html__('Thank you for your message. We will contact you soon.', 'advanced-widget-kit-for-elementor'), 'entry_id' => $entry_id ]); } /** * Validate form fields */ private function validate_fields($fields) { $errors = []; foreach ($fields as $field_data) { $value = $field_data['value']; $label = $field_data['label']; // Required field validation (all fields are required by default in this version) if (empty($value)) { $errors[] = sprintf( esc_html__('%s is required.', 'advanced-widget-kit-for-elementor'), $label ); } // Email validation (based on field name) if (stripos($label, 'email') !== false && !empty($value)) { if (!is_email($value)) { $errors[] = sprintf( esc_html__('Please enter a valid email address for %s.', 'advanced-widget-kit-for-elementor'), $label ); } } // Phone validation (based on field name) if ((stripos($label, 'phone') !== false || stripos($label, 'tel') !== false) && !empty($value)) { if (!preg_match('/^\+?[\d\s-()]{10,}$/', $value)) { $errors[] = sprintf( esc_html__('Please enter a valid phone number for %s.', 'advanced-widget-kit-for-elementor'), $label ); } } } return $errors; } /** * Save form entry to database */ private function save_form_entry($form_id, $fields) { global $wpdb; // Extract form name from fields $form_name = ''; foreach ($fields as $field) { if ($field['label'] === 'Form name') { $form_name = $field['value']; break; } } $table_name = $wpdb->prefix . 'awke_form_entries'; // Ensure the table exists $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name; if (!$table_exists) { $this->maybe_create_table(); } $data = array( 'form_id' => $form_id, 'form_name' => $form_name, 'field_data' => wp_json_encode($fields), 'user_ip' => $this->get_user_ip(), 'user_agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']), 'submitted_at' => current_time('mysql'), 'status' => 'unread' ); $format = array( '%s', // form_id '%s', // form_name '%s', // field_data '%s', // user_ip '%s', // user_agent '%s', // submitted_at '%s' // status ); $result = $wpdb->insert($table_name, $data, $format); return $result ? $wpdb->insert_id : false; } /** * Send email notifications */ private function send_notifications($form_id, $fields) { // Get form settings $widget_data = get_post_meta($form_id, '_elementor_data', true); $widget_data = json_decode($widget_data, true); // Build email content $content = '
| %s | %s |
|---|