*/ class Aitoce_Agents_Page extends Aitoce_Agent_Settings { /** * Handles admin notifications. * @var Aitoce_Admin_Notifications */ private $notify; /** * Handles admin notifications. * @var Aitoce_Agent */ private $agent; public $account_membership; /** * Constructor. * Initializes notification and options handler objects. */ public function __construct() { $this->notify = new Aitoce_Admin_Notifications(); } public function aitoce_add_agents_menu(): void { global $nimbot_agents_page; $nimbot_agents_page = add_submenu_page( parent_slug: AITOCE_MENU_SLUG, page_title: __('Agents', 'ai-tool-center'), menu_title: __('Agents', 'ai-tool-center'), capability: 'manage_options', menu_slug: AITOCE_MENU_SLUG_AGENTS, callback: [$this, 'aitoce_render_agent_page'] ); add_action("load-$nimbot_agents_page", [$this, "aitoce_agent_list_screen_options"]); } // add screen options public function aitoce_agent_list_screen_options($submitted = '') { // phpcs:disable WordPress.Security.NonceVerification if (!empty($submitted) || (!isset($_GET['action']) || ($_GET['action'] !== 'add_agent' && $_GET['action'] !== 'edit_agent'))) { global $nimbot_agents_page; global $table; $screen = get_current_screen(); // get out of here if we are not on our settings page if (!is_object($screen) || $screen->id != $nimbot_agents_page) return; $args = [ 'label' => __('Elements per page', 'ai-tool-center'), 'default' => 5, 'option' => 'elements_per_page' ]; add_screen_option('per_page', $args); $table = new Aitoce_Agent(); } } // add screen options public function aitoce_render_agent_page() { // phpcs:disable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage // phpcs:disable WordPress.DB global $wpdb; $api_call = new Aitoce_Api; $account_membership = $api_call->aitoce_get_account(); $this->notify->aitoce_display_admin_errors(); // Creating an instance $args = [ 'plural' => 'Agents', 'singular' => 'Agent', 'ajax' => false, 'screen' => null, ]; $table = new Aitoce_Agent($args); if (isset($_GET['action']) && $_GET['action'] === 'add_agent') { if(isset($_POST['submit'])){ // Check and serialize inputs $clean_data = $this->aitoce_sanitize_post_data($account_membership); // If errors display them and display add form if(!$clean_data){ $this->aitoce_display_agent_add($account_membership); }else{ // No errors enter into database // Get correct Model switch ($this->connection['aitoce_agent_provider']) { case 'claude': $agent_interface = $this->connection['aitoce_agent_claude_interface']; $agent_model = $this->connection['aitoce_agent_claude_model']; $agent_url = $this->claude_models[$agent_model]['url']; break; case 'custom': $agent_interface = $this->connection['aitoce_agent_custom_interface']; $agent_model = $this->connection['aitoce_agent_custom_model']; $agent_url = $this->connection['aitoce_agent_url']; break; default: $agent_interface = $this->connection['aitoce_agent_openai_interface']; $agent_model = $this->connection['aitoce_agent_openai_model']; $agent_url = $this->openai_models[$agent_model]['url']; break; } // Table name with WP prefix $table_name = esc_sql($wpdb->prefix . AITOCE_AGENT_TABLE); $inserted = $wpdb->insert( $table_name, [ 'agent_name' => $this->connection['aitoce_agent_name'], 'agent_description' => $this->connection['aitoce_agent_description'], 'agent_provider' => $this->connection['aitoce_agent_provider'], 'agent_custom_interface' => $agent_interface, 'agent_url' => $agent_url, 'agent_api_key' => $this->connection['aitoce_agent_api_key'], 'agent_model' => $agent_model, 'agent_role' => $this->rag['aitoce_agent_role'], 'agent_retention' => $this->rag['aitoce_agent_retention'], 'agent_transcript' => $this->rag['aitoce_agent_transcript'], 'agent_pages' => $this->rag['aitoce_agent_pages'], 'agent_posts' => $this->rag['aitoce_agent_posts'], 'agent_styling' => wp_json_encode($this->styling), 'created_at' => current_time('mysql', 1) ], [ '%s', // agent_name '%s', // agent_description '%s', // agent_provider '%s', // agent_custom_interface '%s', // agent_url '%s', // agent_api_key '%s', // agent_model '%s', // agent_role '%s', // agent_retention '%s', // agent_transcript '%s', // agent_pages '%s', // agent_posts '%s', // agent_styling '%s', // created_at ] ); // If successful return to agent list else add agent form if($inserted){ $type = 'success'; $message = __( 'Added new Agent.', 'ai-tool-center' ); add_settings_error( 'aitoce-admin-notice', esc_attr( 'settings_updated' ), $message, $type ); }else{ $type = 'error'; $message = __( 'Error inserting data into database!', 'ai-tool-center' ); add_settings_error( 'aitoce-admin-notice', esc_attr( 'settings_updated' ), $message, $type ); } $this->aitoce_display_agent_add($account_membership); } }else{ $this->aitoce_display_agent_add($account_membership); } } elseif (isset($_GET['action']) && $_GET['action'] === 'edit_agent') { // Check nonce if (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'edit_agent_wpnonce')) { return; } $agent_id = isset($_GET['element']) ? absint($_GET['element'] ?? ''):''; if(isset($_POST['submit'])){ // Check and serialize inputs $clean_data = $this->aitoce_sanitize_post_data($account_membership); // If errors display them and display add form if(!$clean_data){ $this->aitoce_display_agent_edit($account_membership); }else{ // Get correct Model switch ($this->connection['aitoce_agent_provider']) { case 'claude': $agent_interface = $this->connection['aitoce_agent_claude_interface']; $agent_model = $this->connection['aitoce_agent_claude_model']; $agent_url = $this->claude_models[$agent_model]['url']; break; case 'custom': $agent_interface = $this->connection['aitoce_agent_custom_interface']; $agent_model = $this->connection['aitoce_agent_custom_model']; $agent_url = $this->connection['aitoce_agent_url']; break; default: $agent_interface = $this->connection['aitoce_agent_openai_interface']; $agent_model = $this->connection['aitoce_agent_openai_model']; $agent_url = $this->openai_models[$agent_model]['url']; break; } // Table name with WP prefix $table_name = esc_sql($wpdb->prefix . AITOCE_AGENT_TABLE); // Update agent in database $inserted = $wpdb->update( $table_name, [ 'agent_name' => $this->connection['aitoce_agent_name'], 'agent_description' => $this->connection['aitoce_agent_description'], 'agent_provider' => $this->connection['aitoce_agent_provider'], 'agent_custom_interface' => $agent_interface, 'agent_url' => $agent_url, 'agent_api_key' => $this->connection['aitoce_agent_api_key'], 'agent_model' => $agent_model, 'agent_role' => $this->rag['aitoce_agent_role'], 'agent_retention' => $this->rag['aitoce_agent_retention'], 'agent_transcript' => $this->rag['aitoce_agent_transcript'], 'agent_pages' => $this->rag['aitoce_agent_pages'], 'agent_posts' => $this->rag['aitoce_agent_posts'], 'agent_styling' => wp_json_encode($this->styling) ], ['agent_id' => $agent_id], [ '%s', // agent_name '%s', // agent_description '%s', // agent_provider '%s', // agent_custom_interface '%s', // agent_url '%s', // agent_api_key '%s', // agent_model '%s', // agent_role '%s', // agent_retention '%s', // agent_transcript '%s', // agent_pages '%s', // agent_posts '%s', // agent_styling '%s', // created_at ] ); $type = 'success'; $message = 'Agent ID:'.esc_attr($agent_id).' Updated.'; add_settings_error( 'aitoce-admin-notice', esc_attr( 'settings_updated' ), $message, $type ); $this->aitoce_display_agent_edit($account_membership); } }else{ // Load Agent data from database $mysql_agent_data = $this->aitoce_get_agent_data($agent_id); // Output Agent data into form if($mysql_agent_data){ $this->aitoce_load_mysql_into_form($mysql_agent_data); $this->aitoce_display_agent_edit($account_membership); } } } else { $table->aitoce_display_agent_list(); } } private function aitoce_redirect($url) { $string = ""; echo wp_kses($string, [ 'script' => [ 'type' => [], ] ]); } // To show bulk action dropdown public function aitoce_display_agent_add($account_membership) { // phpcs:disable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage echo '

AI Tool Center Icon AI Tool Center Nimbot Head

'; // Display errors settings_errors(); echo '

Add Agent

'; $this->aitoce_get_connection_form(); $this->aitoce_get_rag_form($account_membership['label']); $this->aitoce_get_styling_form(); echo ''; } // To show bulk action dropdown public function aitoce_display_agent_edit($account_membership) { // phpcs:disable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage echo '

AI Tool Center Icon AI Tool Center Nimbot Head

'; // Display errors settings_errors(); echo '

Edit Agent

'; $this->aitoce_get_connection_form(); $this->aitoce_get_rag_form($account_membership['label']); $this->aitoce_get_styling_form(); echo ' '; } private function aitoce_get_connection_form(){ echo ' '; $submit_text = (isset($_GET['action']) && $_GET['action'] === 'add_agent')? 'Add New':'Update'; echo '

'; } private function aitoce_get_rag_form($account_membership){ if(empty($this->rag['aitoce_agent_retention'])){ $this->rag['aitoce_agent_retention'] = $this->rag_defaults[$account_membership]['aitoce_agent_retention']['default']; } if(empty($this->rag['aitoce_agent_transcript']) && $this->rag['aitoce_agent_transcript'] !== '0'){ $this->rag['aitoce_agent_transcript'] = $this->rag_defaults[$account_membership]['aitoce_agent_transcript']['default']; } echo '

RAG

'; $submit_text = (isset($_GET['action']) && $_GET['action'] === 'add_agent')? 'Add New':'Update'; echo '

'; } private function aitoce_get_styling_form(){ echo '

Styling

Background Style

Client Message Style

NimBot Reply Style

Client Input Style

Button Style

'; $submit_text = (isset($_GET['action']) && $_GET['action'] === 'add_agent')? 'Add New':'Update'; echo '

'; wp_nonce_field("add_agent_wpnonce", "_wpnonce"); echo '
'; } }