prefix . 'ai_app_onsite_model_settings'; $existing_settings = $wpdb->get_row("SELECT * FROM $table_name_settings"); if ($existing_settings) { $wpdb->update( $table_name_settings, array( 'model' => $model, 'temperature' => $temperature, 'f_penalty' => $f_penalty, 'p_penalty' => $p_penalty, 'max_tokens' => $max_tokens, 'top_p' => $top_p ), array('id' => $existing_settings->id), array('%s', '%f', '%f', '%f', '%d', '%f'), ); $existing_settings = $wpdb->get_row("SELECT * FROM $table_name_settings"); // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error updating data: ' . $wpdb->last_error); } else { $results = array('status' => '200', 'message' => 'Settings saved successfully', 'data' => $existing_settings); // Return success message for insert wp_send_json_success($results); } } else { // Insert new row $wpdb->insert( $table_name_settings, array( 'app_id' => 1, 'model' => $model, 'temperature' => $temperature, 'f_penalty' => $f_penalty, 'p_penalty' => $p_penalty, 'max_tokens' => $max_tokens, 'top_p' => $top_p ), array('%d', '%s', '%d', '%d', '%d', '%d', '%d') ); } $existing_settings = $wpdb->get_row("SELECT * FROM $table_name_settings"); // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error saving data: ' . $wpdb->last_error); } else { $results = array('status' => '200', 'message' => 'Settings saved successfully', 'data' => $existing_settings); // Return success message for insert wp_send_json_success($results); } } function ai_app_onsite_get_model_settings() { global $wpdb; $table_name_settings = $wpdb->prefix . 'ai_app_onsite_model_settings'; // Check if the table exists if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_settings'") != $table_name_settings) { die('Table does not exist.'); } // Check if the table has rows $rows_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name_settings"); if ($rows_count == 0) { $results = array('status' => '404', 'message' => 'No rows found in the table.'); // Return the data wp_send_json($results); die(); } // Fetch the data $results = $wpdb->get_results("SELECT * FROM $table_name_settings", ARRAY_A); // Return the data wp_send_json($results); // Don't forget to exit exit; } function ai_app_onsite_get_openapi_data() { global $wpdb; $table_name_settings = $wpdb->prefix . 'ai_app_onsite_model_settings'; // Check if the table exists if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_settings'") != $table_name_settings) { die('Table does not exist.'); } // Check if the table has rows $rows_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name_settings"); if ($rows_count == 0) { $results = array('status' => '404', 'message' => 'No rows found in the table.'); // Return the data wp_send_json($results); die(); } // Fetch the data $results = $wpdb->get_results("SELECT openai_key, key_status FROM $table_name_settings", ARRAY_A); // Return the data wp_send_json($results); // Don't forget to exit exit; } function ai_app_onsite_save_openai_key_callback() { global $wpdb; if ( ! isset($_POST['action']) || ! isset($_POST['api_Key_nonce']) || // Check if the nonce field is set ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['api_Key_nonce'])), 'ai_app_onsite_model_settings_nonce') ) { wp_send_json_error('Invalid nonce. Please refresh the page and try again.'); return; } // Extract individual settings values openai-iv $openai_key = isset($_POST['api_key']) ? sanitize_text_field(wp_unslash($_POST['api_key'])) : ''; $api_status = isset($_POST['api_status']) ? sanitize_text_field(wp_unslash($_POST['api_status'])) : ''; $engine_model = isset($_POST['engine_model']) ? sanitize_text_field(wp_unslash($_POST['engine_model'])) : ''; // Check if settings already exist in the database $table_name_settings = $wpdb->prefix . 'ai_app_onsite_model_settings'; $existing_settings = $wpdb->get_row("SELECT * FROM $table_name_settings"); if ($existing_settings) { $wpdb->update( $table_name_settings, array( 'openai_key' => $openai_key, 'key_status' => $api_status, ), array('id' => $existing_settings->id), array('%s', '%s'), ); // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error updating data: ' . $wpdb->last_error); } else { // Return success message for update wp_send_json_success('Settings updated successfully.'); } } else { // Insert new row $wpdb->insert( $table_name_settings, array( 'app_id' => 1, 'openai_key' => $openai_key, 'key_status' => $api_status, 'model' => $engine_model, 'temperature' => '0.7', 'f_penalty' => '0', 'p_penalty' => '0', 'max_tokens' => '3000', 'top_p' => '1', ), array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ); // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error saving data: ' . $wpdb->last_error); } else { // Return success message for insert wp_send_json_success('Settings saved successfully.'); } } } } new AI_APP_ONSITE_Model_Settings_Handler(); // Instantiate the class }