&$item ) { $item['field_label'] = isset( $item['field_label'] ) ? sanitize_text_field( $item['field_label'] ) : ''; $item['field_id'] = isset( $item['field_id'] ) ? sanitize_text_field( $item['field_id'] ) : ''; $item['field_sample_text'] = isset( $item['field_sample_text'] ) ? sanitize_text_field( $item['field_sample_text'] ) : ''; $item['field_creator'] = isset( $item['field_creator'] ) ? sanitize_text_field( $item['field_creator'] ) : ''; $item['field_maxchar'] = isset( $item['field_maxchar'] ) ? sanitize_text_field( $item['field_maxchar'] ) : ''; $item['field_uniqueid'] = isset( $item['field_uniqueid'] ) ? sanitize_text_field( $item['field_uniqueid'] ) : ''; $item['app_width_figure'] = isset( $item['app_width_figure'] ) ? sanitize_text_field( $item['app_width_figure'] ) : ''; $item['field_dropdown_options'] = isset( $item['field_dropdown_options'] ) ? sanitize_text_field( $item['field_dropdown_options'] ) : ''; // Validate field_label length (DB column is VARCHAR 300) if ( mb_strlen( $item['field_label'] ) > 300 ) { wp_send_json_error( 'Field label for field #' . ( $idx + 1 ) . ' exceeds 300 characters (currently ' . mb_strlen( $item['field_label'] ) . '). Please shorten it.' ); } // Validate field_sample_text length (DB column is VARCHAR 500) if ( mb_strlen( $item['field_sample_text'] ) > 500 ) { wp_send_json_error( 'Sample text for field #' . ( $idx + 1 ) . ' exceeds 500 characters (currently ' . mb_strlen( $item['field_sample_text'] ) . '). Please shorten it.' ); } } unset( $item ); // break reference // Table name for field selector settings $table_name_settings = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; $max_field_option = $wpdb->get_var("SELECT MAX(field_position) FROM $table_name_settings WHERE app_id = 1"); // Iterate through form data and insert/update settings foreach ($form_data_array as $index => $formData) { // Check if settings already exist for this field $existing_settings = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name_settings WHERE field_uniqueid = %s", $formData['field_uniqueid'])); // echo 'existing_settings = {{'.$formData['field_label'].'}}'; $field_label = $formData['field_label']; // Replace underscores with spaces $field_label = str_replace(' ', '_', $field_label); // Convert the first letter to lowercase $field_label = strtolower($field_label); // Enclose the key in curly braces $field_id = $formData['field_id']; $replaceField_id = str_replace(' ', '-', $field_id); // Convert the first letter to lowercase $formField_id = strtolower($replaceField_id); $field_tag = '{{' . $formField_id . '}}'; if (isset($formData['field_dropdown_options'])) { // Split the string into an array using '|' as a delimiter $field_dropdown_options = explode('|', $formData['field_dropdown_options']); } else { $field_dropdown_options = []; } // Serialize the array to store in the database $field_dropdown_options = serialize($field_dropdown_options); $field_position = isset($formData['field_order']) ? (int) $formData['field_order'] : (int) $index; // If settings already exist, update the row; otherwise, insert a new row if ($existing_settings) { // Prepare data for update $update_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formField_id, 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_creator'] == 'dropdown' ? '' : $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $field_position, 'field_tag' => $field_tag, // 'created_at' => current_time('mysql'), 'updated_at' => gmdate("Y-m-d H:i:s") ); if ($existing_settings->field_tag !== $field_tag) { // Update references in prompt editor table (only prompt and prompt_html columns) $this->update_prompt_references('prompt', $existing_settings->field_tag, $field_tag, 1); $this->update_prompt_references('prompt_html', $existing_settings->field_tag, $field_tag, 1); } // Update existing row $wpdb->update( $table_name_settings, $update_data, array('id' => $existing_settings->id), array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s'), array('%d') // where format ); } else { $field_position = isset($formData['field_order']) ? (int) $formData['field_order'] : ((int) $max_field_option + 1); // Prepare data for insertion $create_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formData['field_id'], 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_creator'] == 'dropdown' ? '' : $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $field_position, 'field_tag' => $field_tag, 'created_at' => gmdate("Y-m-d H:i:s"), 'updated_at' => gmdate("Y-m-d H:i:s") ); // Insert new row $wpdb->insert( $table_name_settings, $create_data, array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s') ); } // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error savingdf data: ' . $wpdb->last_error); } } // If reached here, data saved successfully aiapp_invalidate_frontend_cache( 'Form fields updated' ); wp_send_json_success('Settings saved successfully.'); } /** * Update references of an old field tag to a new one inside the prompt editor table. * * @param string $column The column name to update ('prompt' or 'prompt_html'). * @param string $current The current field tag value (e.g., '{{old-tag}}'). * @param string $expected The new field tag value (e.g., '{{new-tag}}'). * @param int $app_id The app ID for scoping the update. * @return int|false Number of rows affected, or false on error. */ public function update_prompt_references($column, $current, $expected, $app_id = 1) { global $wpdb; // Ensure the table name is accessible from the class $table_name_prompt_editor = $this->table_name_prompt_editor ?? $wpdb->prefix . 'ai_app_onsite_prompt_editor'; // Validate column name to prevent SQL injection $allowed_columns = ['prompt', 'prompt_html']; if (!in_array($column, $allowed_columns, true)) { return false; // invalid column } // Prepare safe LIKE pattern $like = '%' . $wpdb->esc_like($current) . '%'; // Build and execute the query $sql = $wpdb->prepare( "UPDATE {$table_name_prompt_editor} SET {$column} = REPLACE(COALESCE({$column}, ''), %s, %s) WHERE {$column} LIKE %s AND app_id = %d", $current, $expected, $like, (int) $app_id ); return $wpdb->query($sql); } public function ai_app_onsite_read_field_selector_callback() { global $wpdb; $table_name = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; $table_name_prompt_editor = $wpdb->prefix . 'ai_app_onsite_prompt_editor'; // Fetch data from the database $results = $wpdb->get_results("SELECT * FROM $table_name WHERE app_id = 1 ORDER BY field_position ASC", ARRAY_A); // HTML content string to store the generated HTML $html_content = ''; // Loop through each record and generate HTML content $ctr = 0; $app_width = $results[0]['app_width'] ?? '70'; foreach ($results as $record) { // Sync the field tag value in the database $field_id = strtolower(str_replace(' ', '-', trim($record['field_id']))); $expected = '{{' . $field_id . '}}'; $current = trim($record['field_tag']); if ($current !== $expected) { $wpdb->update( $table_name, ['field_tag' => $expected], ['id' => (int) $record['id']] ); // 2) Update references in prompt editor table (only prompt and prompt_html columns) $this->update_prompt_references('prompt', $current, $expected, 1); $this->update_prompt_references('prompt_html', $current, $expected, 1); } $html_content .= '