page_slug ); add_settings_field( 'enable_widget', __( 'Enable Widget', 'aica-smart-ai-commerce-assistant' ), array( $this, 'checkbox_field_callback' ), $this->page_slug, 'aica_general_section', array( 'label_for' => 'enable_widget', 'description' => __( 'Enable the AI assistant widget on frontend', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'widget_position', __( 'Widget Position', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_general_section', array( 'label_for' => 'widget_position', 'options' => array( 'bottom-right' => __( 'Bottom Right', 'aica-smart-ai-commerce-assistant' ), 'bottom-left' => __( 'Bottom Left', 'aica-smart-ai-commerce-assistant' ), 'top-right' => __( 'Top Right', 'aica-smart-ai-commerce-assistant' ), 'top-left' => __( 'Top Left', 'aica-smart-ai-commerce-assistant' ), ), ) ); // Default Theme add_settings_field( 'default_theme', __( 'Default Theme', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), // ← CORRETTO $this->page_slug, // ← CORRETTO 'aica_general_section', // ← CORRETTO array( 'label_for' => 'default_theme', 'options' => array( 'auto' => __( 'Auto (System preference)', 'aica-smart-ai-commerce-assistant' ), 'light' => __( 'Light', 'aica-smart-ai-commerce-assistant' ), 'dark' => __( 'Dark', 'aica-smart-ai-commerce-assistant' ), ), 'description' => __( 'Choose the default theme for the widget. Users can still toggle between themes using the button in the widget header.', 'aica-smart-ai-commerce-assistant' ), ) ); // AI Provider Settings Section add_settings_section( 'aica_ai_section', __( 'AI Provider Settings', 'aica-smart-ai-commerce-assistant' ), array( $this, 'ai_section_callback' ), $this->page_slug ); add_settings_field( 'ai_provider', __( 'AI Provider', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'ai_provider', 'options' => array( 'openai' => __( 'OpenAI (ChatGPT)', 'aica-smart-ai-commerce-assistant' ), 'anthropic' => __( 'Anthropic (Claude)', 'aica-smart-ai-commerce-assistant' ), 'google' => __( 'Google (Gemini)', 'aica-smart-ai-commerce-assistant' ), 'openrouter' => __( 'OpenRouter (Multiple Models)', 'aica-smart-ai-commerce-assistant' ), ), 'description' => __( 'Choose your preferred AI provider', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'openai_api_key', __( 'OpenAI API Key', 'aica-smart-ai-commerce-assistant' ), array( $this, 'password_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'openai_api_key', 'placeholder' => 'sk-...', 'description' => sprintf( /* translators: %s: OpenAI link */ __( 'Need an API Key? Get one here: %s', 'aica-smart-ai-commerce-assistant' ), 'OpenAI Platform' ), 'show_if' => 'ai_provider', 'show_if_value' => 'openai', ) ); add_settings_field( 'openai_model', __( 'OpenAI Model', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'openai_model', 'options' => array( 'gpt-4o' => 'GPT-4o (Best quality)', 'gpt-4o-mini' => 'GPT-4o Mini (Balanced)', 'gpt-3.5-turbo' => 'GPT-3.5 Turbo (Fast & cheap)', ), 'show_if' => 'ai_provider', 'show_if_value' => 'openai', ) ); add_settings_field( 'anthropic_api_key', __( 'Anthropic API Key', 'aica-smart-ai-commerce-assistant' ), array( $this, 'password_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'anthropic_api_key', 'placeholder' => 'sk-ant-...', 'description' => sprintf( /* translators: %s: Anthropic link */ __( 'Need an API Key? Get one here: %s', 'aica-smart-ai-commerce-assistant' ), 'Anthropic Console' ), 'show_if' => 'ai_provider', 'show_if_value' => 'anthropic', ) ); add_settings_field( 'anthropic_model', __( 'Claude Model', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'anthropic_model', 'options' => array( 'claude-3-5-sonnet-20241022' => 'Claude 3.5 Sonnet (Best)', 'claude-3-5-haiku-20241022' => 'Claude 3.5 Haiku (Fast)', 'claude-3-opus-20240229' => 'Claude 3 Opus (Legacy)', ), 'show_if' => 'ai_provider', 'show_if_value' => 'anthropic', ) ); add_settings_field( 'google_api_key', __( 'Google API Key', 'aica-smart-ai-commerce-assistant' ), array( $this, 'password_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'google_api_key', 'description' => sprintf( /* translators: %s: Google link */ __( 'Need an API Key? Get one here: %s', 'aica-smart-ai-commerce-assistant' ), 'Google AI Studio' ), 'show_if' => 'ai_provider', 'show_if_value' => 'google', ) ); add_settings_field( 'google_model', __( 'Gemini Model', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'google_model', 'options' => array( 'gemini-2.5-flash' => 'Gemini 2.5 Flash (Recommended)', 'gemini-2.0-flash' => 'Gemini 2.0 Flash', 'gemini-flash-latest' => 'Gemini Flash Latest', 'gemini-2.5-pro' => 'Gemini 2.5 Pro (Best Quality)', 'gemini-pro-latest' => 'Gemini Pro Latest', 'gemini-2.0-flash-lite' => 'Gemini 2.0 Flash Lite (Fastest)', ), 'show_if' => 'ai_provider', 'show_if_value' => 'google', ) ); add_settings_field( 'openrouter_api_key', __( 'OpenRouter API Key', 'aica-smart-ai-commerce-assistant' ), array( $this, 'password_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'openrouter_api_key', 'description' => sprintf( /* translators: %s: OpenRouter link */ __( 'Need an API Key? Get one here: %s', 'aica-smart-ai-commerce-assistant' ), 'OpenRouter' ), 'show_if' => 'ai_provider', 'show_if_value' => 'openrouter', ) ); add_settings_field( 'openrouter_model', __( 'Model', 'aica-smart-ai-commerce-assistant' ), array( $this, 'text_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'openrouter_model', 'placeholder' => 'anthropic/claude-3.5-sonnet', 'description' => __( 'Enter model ID from OpenRouter', 'aica-smart-ai-commerce-assistant' ), 'show_if' => 'ai_provider', 'show_if_value' => 'openrouter', ) ); add_settings_field( 'temperature', __( 'Temperature', 'aica-smart-ai-commerce-assistant' ), array( $this, 'number_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'temperature', 'min' => 0, 'max' => 2, 'step' => 0.1, 'description' => __( 'Controls randomness (0 = focused, 2 = creative)', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'max_tokens', __( 'Max Tokens', 'aica-smart-ai-commerce-assistant' ), array( $this, 'number_field_callback' ), $this->page_slug, 'aica_ai_section', array( 'label_for' => 'max_tokens', 'min' => 100, 'max' => 4000, 'step' => 100, 'description' => __( 'Maximum response length', 'aica-smart-ai-commerce-assistant' ), ) ); // RAG Settings Section add_settings_section( 'aica_rag_section', __( 'RAG & Product Search Settings', 'aica-smart-ai-commerce-assistant' ), array( $this, 'rag_section_callback' ), $this->page_slug ); add_settings_field( 'enable_embeddings', __( 'Enable Embeddings', 'aica-smart-ai-commerce-assistant' ), array( $this, 'checkbox_field_callback' ), $this->page_slug, 'aica_rag_section', array( 'label_for' => 'enable_embeddings', 'description' => __( 'Use AI embeddings for better product search (requires API credits)', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'embedding_model', __( 'Embedding Model', 'aica-smart-ai-commerce-assistant' ), array( $this, 'select_field_callback' ), $this->page_slug, 'aica_rag_section', array( 'label_for' => 'embedding_model', 'options' => array( 'text-embedding-3-small' => 'OpenAI text-embedding-3-small (Cheap)', 'text-embedding-3-large' => 'OpenAI text-embedding-3-large (Best)', 'text-embedding-ada-002' => 'OpenAI ada-002 (Legacy)', ), ) ); add_settings_field( 'max_products_per_response', __( 'Max Products per Response', 'aica-smart-ai-commerce-assistant' ), array( $this, 'number_field_callback' ), $this->page_slug, 'aica_rag_section', array( 'label_for' => 'max_products_per_response', 'min' => 1, 'max' => 20, 'step' => 1, 'description' => __( 'Maximum number of products to show in chat', 'aica-smart-ai-commerce-assistant' ), ) ); // Appearance Settings Section add_settings_section( 'aica_appearance_section', __( 'Appearance Settings', 'aica-smart-ai-commerce-assistant' ), array( $this, 'appearance_section_callback' ), $this->page_slug ); add_settings_field( 'primary_color', __( 'Primary Color', 'aica-smart-ai-commerce-assistant' ), array( $this, 'color_field_callback' ), $this->page_slug, 'aica_appearance_section', array( 'label_for' => 'primary_color', ) ); add_settings_field( 'widget_title', __( 'Widget Title', 'aica-smart-ai-commerce-assistant' ), array( $this, 'text_field_callback' ), $this->page_slug, 'aica_appearance_section', array( 'label_for' => 'widget_title', 'placeholder' => __( 'AI Assistant', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'greeting_message', __( 'Greeting Message', 'aica-smart-ai-commerce-assistant' ), array( $this, 'textarea_field_callback' ), $this->page_slug, 'aica_appearance_section', array( 'label_for' => 'greeting_message', 'placeholder' => __( 'Hi! How can I help you today?', 'aica-smart-ai-commerce-assistant' ), 'rows' => 3, ) ); // Features Section add_settings_section( 'aica_features_section', __( 'Features', 'aica-smart-ai-commerce-assistant' ), null, $this->page_slug ); add_settings_field( 'enable_product_recommendations', __( 'Product Recommendations', 'aica-smart-ai-commerce-assistant' ), array( $this, 'checkbox_field_callback' ), $this->page_slug, 'aica_features_section', array( 'label_for' => 'enable_product_recommendations', 'description' => __( 'Allow AI to recommend products', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'enable_cart_actions', __( 'Cart Actions', 'aica-smart-ai-commerce-assistant' ), array( $this, 'checkbox_field_callback' ), $this->page_slug, 'aica_features_section', array( 'label_for' => 'enable_cart_actions', 'description' => __( 'Allow adding/removing products via chat', 'aica-smart-ai-commerce-assistant' ), ) ); // Rate Limiting Section add_settings_section( 'aica_rate_limit_section', __( 'Rate Limiting', 'aica-smart-ai-commerce-assistant' ), array( $this, 'rate_limit_section_callback' ), $this->page_slug ); add_settings_field( 'rate_limit_enabled', __( 'Enable Rate Limiting', 'aica-smart-ai-commerce-assistant' ), array( $this, 'checkbox_field_callback' ), $this->page_slug, 'aica_rate_limit_section', array( 'label_for' => 'rate_limit_enabled', 'description' => __( 'Prevent spam and control API costs', 'aica-smart-ai-commerce-assistant' ), ) ); add_settings_field( 'rate_limit_messages', __( 'Max Messages per Hour', 'aica-smart-ai-commerce-assistant' ), array( $this, 'number_field_callback' ), $this->page_slug, 'aica_rate_limit_section', array( 'label_for' => 'rate_limit_messages', 'min' => 10, 'max' => 1000, 'step' => 10, ) ); } /** * Section callbacks */ public function general_section_callback() { echo '

' . esc_html__( 'Configure general plugin settings', 'aica-smart-ai-commerce-assistant' ) . '

'; } public function ai_section_callback() { echo '

' . esc_html__( 'Choose and configure your AI provider', 'aica-smart-ai-commerce-assistant' ) . '

'; } public function rag_section_callback() { echo '

' . esc_html__( 'Configure RAG (Retrieval-Augmented Generation) for better product search', 'aica-smart-ai-commerce-assistant' ) . '

'; } public function appearance_section_callback() { echo '

' . esc_html__( 'Customize the widget appearance', 'aica-smart-ai-commerce-assistant' ) . '

'; } public function rate_limit_section_callback() { echo '

' . esc_html__( 'Protect against spam and control API usage', 'aica-smart-ai-commerce-assistant' ) . '

'; } /** * Field callbacks */ public function text_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : ''; $placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : ''; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), esc_attr( $value ), esc_attr( $placeholder ) ); if ( isset( $args['description'] ) ) { printf( '

%s

', wp_kses_post( $args['description'] ) ); } } public function password_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : ''; $placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : ''; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), esc_attr( $value ), esc_attr( $placeholder ) ); if ( isset( $args['description'] ) ) { printf( '

%s

', wp_kses_post( $args['description'] ) ); } } public function textarea_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : ''; $rows = isset( $args['rows'] ) ? $args['rows'] : 5; $placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : ''; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), esc_attr( $rows ), esc_attr( $placeholder ), esc_textarea( $value ) ); } public function checkbox_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $checked = isset( $settings[ $args['label_for'] ] ) && $settings[ $args['label_for'] ] === 'yes'; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), checked( $checked, true, false ) ); if ( isset( $args['description'] ) ) { printf( '

%s

', esc_html( $args['description'] ) ); } } public function select_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : ''; printf( ''; if ( isset( $args['description'] ) ) { printf( '

%s

', wp_kses_post( $args['description'] ) ); } } public function number_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : ''; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), esc_attr( $value ), esc_attr( $args['min'] ), esc_attr( $args['max'] ), esc_attr( $args['step'] ) ); if ( isset( $args['description'] ) ) { printf( '

%s

', esc_html( $args['description'] ) ); } } public function color_field_callback( $args ) { $settings = get_option( 'aica_settings', array() ); $value = isset( $settings[ $args['label_for'] ] ) ? $settings[ $args['label_for'] ] : '#0066FF'; printf( '', esc_attr( $args['label_for'] ), esc_attr( $args['label_for'] ), esc_attr( $value ) ); } /** * Sanitize settings * * @param array $input Settings input * @return array */ public function sanitize_settings( $input ) { $sanitized = array(); // Text fields $text_fields = array( 'widget_title', 'openai_model', 'anthropic_model', 'google_model', 'openrouter_model', 'embedding_model' ); foreach ( $text_fields as $field ) { if ( isset( $input[ $field ] ) ) { $sanitized[ $field ] = sanitize_text_field( $input[ $field ] ); } } // Password fields (API keys) $password_fields = array( 'openai_api_key', 'anthropic_api_key', 'google_api_key', 'openrouter_api_key' ); foreach ( $password_fields as $field ) { if ( isset( $input[ $field ] ) ) { $sanitized[ $field ] = sanitize_text_field( $input[ $field ] ); } } // Textarea if ( isset( $input['greeting_message'] ) ) { $sanitized['greeting_message'] = sanitize_textarea_field( $input['greeting_message'] ); } // Checkboxes $checkbox_fields = array( 'enable_widget', 'enable_embeddings', 'enable_product_recommendations', 'enable_cart_actions', 'rate_limit_enabled' ); foreach ( $checkbox_fields as $field ) { $sanitized[ $field ] = isset( $input[ $field ] ) && $input[ $field ] === 'yes' ? 'yes' : 'no'; } // Select fields $select_fields = array( 'ai_provider', 'widget_position', 'default_theme' ); foreach ( $select_fields as $field ) { if ( isset( $input[ $field ] ) ) { $sanitized[ $field ] = sanitize_text_field( $input[ $field ] ); } } // Number fields if ( isset( $input['temperature'] ) ) { $sanitized['temperature'] = floatval( $input['temperature'] ); } if ( isset( $input['max_tokens'] ) ) { $sanitized['max_tokens'] = intval( $input['max_tokens'] ); } if ( isset( $input['max_products_per_response'] ) ) { $sanitized['max_products_per_response'] = intval( $input['max_products_per_response'] ); } if ( isset( $input['rate_limit_messages'] ) ) { $sanitized['rate_limit_messages'] = intval( $input['rate_limit_messages'] ); } // Color if ( isset( $input['primary_color'] ) ) { $sanitized['primary_color'] = sanitize_hex_color( $input['primary_color'] ); } return $sanitized; } /** * Display admin notices * * @return void */ public function admin_notices() { $settings = get_option( 'aica_settings', array() ); $provider = $settings['ai_provider'] ?? ''; // Check if API key is set $api_key_field = ''; switch ( $provider ) { case 'openai': $api_key_field = 'openai_api_key'; break; case 'anthropic': $api_key_field = 'anthropic_api_key'; break; case 'google': $api_key_field = 'google_api_key'; break; case 'openrouter': $api_key_field = 'openrouter_api_key'; break; } if ( $api_key_field && empty( $settings[ $api_key_field ] ) ) { $screen = get_current_screen(); if ( $screen && strpos( $screen->id, 'aica-smart-ai-commerce-assistant' ) !== false ) { ?>

⚙️

page_slug ] ) ) { foreach ( (array) $wp_settings_sections[ $this->page_slug ] as $section ) { ?>
" . esc_html( $section['title'] ) . ""; } if ( $section['callback'] ) { call_user_func( $section['callback'], $section ); } if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $this->page_slug ] ) || ! isset( $wp_settings_fields[ $this->page_slug ][ $section['id'] ] ) ) { continue; } ?> page_slug, $section['id'] ); ?>