array( 'type' => 'boolean', 'default' => false ), 'wtofe_chatbot_name' => array( 'type' => 'string', 'default' => 'Store Assistant', 'sanitize' => 'sanitize_text_field' ), 'wtofe_chatbot_avatar_url' => array( 'type' => 'string', 'default' => '', 'sanitize' => 'esc_url_raw' ), 'wtofe_chatbot_primary_color' => array( 'type' => 'string', 'default' => '#000000', 'sanitize' => 'sanitize_hex_color' ), 'wtofe_chatbot_bubble_color' => array( 'type' => 'string', 'default' => '#000000', 'sanitize' => 'sanitize_hex_color' ), 'wtofe_chatbot_text_color' => array( 'type' => 'string', 'default' => '#ffffff', 'sanitize' => 'sanitize_hex_color' ), 'wtofe_chatbot_welcome_message' => array( 'type' => 'string', 'default' => 'Hi! How can I help you today?', 'sanitize' => 'sanitize_textarea_field' ), 'wtofe_chatbot_placeholder' => array( 'type' => 'string', 'default' => 'Ask me anything about the store…', 'sanitize' => 'sanitize_text_field' ), 'wtofe_chatbot_model' => array( 'type' => 'string', 'default' => '', 'sanitize' => 'sanitize_text_field' ), 'wtofe_ai_max_tokens_chatbot' => array( 'type' => 'integer', 'default' => 4000, 'sanitize' => function( $val ) { $v = absint( $val ); return ( $v >= 100 && $v <= 32000 ) ? $v : 4000; } ), 'wtofe_ai_temperature_chatbot' => array( 'type' => 'number', 'default' => 1.0, 'sanitize' => function( $val ) { return min( 2.0, max( 0.0, (float) $val ) ); } ), 'wtofe_chatbot_pages' => array( 'type' => 'array', 'default' => array('product', wc_get_page_id( 'shop' )), 'sanitize' => array( $this, 'sanitize_page_ids' ) ), 'wtofe_chatbot_max_history_days'=> array( 'type' => 'integer', 'default' => 90, 'sanitize' => 'absint' ), 'wtofe_chatbot_allow_guests' => array( 'type' => 'boolean', 'default' => false ), 'wtofe_chatbot_admin_bubble' => array( 'type' => 'boolean', 'default' => true ), ); foreach ( $fields as $option => $cfg ) { $args = array( 'type' => $cfg['type'], 'default' => $cfg['default'], 'sanitize_callback' => isset( $cfg['sanitize'] ) ? $cfg['sanitize'] : null, ); if ( 'boolean' === $cfg['type'] ) { $args['sanitize_callback'] = 'rest_sanitize_boolean'; } register_setting( 'wtofe_chatbot_settings', $option, $args ); } } public function sanitize_page_ids( $val ) { if ( ! is_array( $val ) ) { $val = explode( ',', (string) $val ); } $allowed_slugs = array( 'shop', 'product', 'cart', 'checkout', 'myaccount' ); $sanitized = array(); foreach ( array_filter( $val ) as $item ) { if ( in_array( $item, $allowed_slugs, true ) ) { // Keep WC virtual page slugs as strings. $sanitized[] = $item; } else { // Real WP page — store as positive integer. $id = absint( $item ); $sanitized[] = $id; } } return $sanitized; } // ------------------------------------------------------------------------- // Settings App Page // ------------------------------------------------------------------------- /** * Adds the "Chatbot" page to the settings app. It saves into its own * option group (wtofe_chatbot_settings) with its own button. * * @param array $sections * @return array */ public function add_settings_app_section( $sections ) { $text_models = WTOFE_Settings_App::get_models()['text_generation'] ?? array(); // If the plugin's own OpenAI key is set, always offer OpenAI models even without a connector. $fallback_models = null; if ( (bool) get_option( 'wtofe_use_custom_api_key', false ) && '' !== trim( (string) get_option( 'wtofe_chatgpt_api_key', '' ) ) && empty( $text_models['openai'] ) ) { $fallback_models = array( 'label' => 'OpenAI (Custom Key)', 'models' => array( array( 'id' => 'gpt-4o', 'label' => 'GPT-4o' ), array( 'id' => 'gpt-4o-mini', 'label' => 'GPT-4o Mini' ), array( 'id' => 'gpt-4-turbo', 'label' => 'GPT-4 Turbo' ), array( 'id' => 'gpt-3.5-turbo', 'label' => 'GPT-3.5 Turbo' ), ), ); } // All published pages and posts for page selection. $pages = array(); foreach ( get_posts( array( 'post_type' => array( 'page', 'post' ), 'posts_per_page' => -1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ) ) as $post ) { $pages[] = array( 'id' => $post->ID, 'title' => $post->post_title ); } $sections['chatbot'] = array( 'order' => 50, 'group' => 'chatbot', 'values' => array( 'wtofe_chatbot_enabled' => (bool) get_option( 'wtofe_chatbot_enabled', false ), 'wtofe_chatbot_allow_guests' => (bool) get_option( 'wtofe_chatbot_allow_guests', false ), 'wtofe_chatbot_admin_bubble' => (bool) get_option( 'wtofe_chatbot_admin_bubble', true ), 'wtofe_chatbot_name' => (string) get_option( 'wtofe_chatbot_name', 'Store Assistant' ), 'wtofe_chatbot_avatar_url' => (string) get_option( 'wtofe_chatbot_avatar_url', '' ), 'wtofe_chatbot_primary_color' => (string) get_option( 'wtofe_chatbot_primary_color', '#000000' ), 'wtofe_chatbot_bubble_color' => (string) get_option( 'wtofe_chatbot_bubble_color', '#000000' ), 'wtofe_chatbot_text_color' => (string) get_option( 'wtofe_chatbot_text_color', '#ffffff' ), 'wtofe_chatbot_welcome_message' => (string) get_option( 'wtofe_chatbot_welcome_message', 'Hi! How can I help you today?' ), 'wtofe_chatbot_placeholder' => (string) get_option( 'wtofe_chatbot_placeholder', 'Ask me anything about the store…' ), 'wtofe_chatbot_model' => WTOFE_Settings_App::selectable_model( get_option( 'wtofe_chatbot_model', '' ), 'text_generation', $fallback_models ? array( $fallback_models ) : array() ), 'wtofe_ai_max_tokens_chatbot' => (int) get_option( 'wtofe_ai_max_tokens_chatbot', 4000 ), 'wtofe_ai_temperature_chatbot' => (float) get_option( 'wtofe_ai_temperature_chatbot', 1.0 ), 'wtofe_chatbot_pages' => array_values( (array) get_option( 'wtofe_chatbot_pages', array( 'product', wc_get_page_id( 'shop' ) ) ) ), 'wtofe_chatbot_max_history_days' => absint( get_option( 'wtofe_chatbot_max_history_days', 90 ) ), ), 'data' => array( 'pages' => $pages, 'fallbackModels' => $fallback_models, 'historyNonce' => wp_create_nonce( 'wtofe_chatbot_admin_nonce' ), ), ); return $sections; } // ------------------------------------------------------------------------- // AJAX Handlers // ------------------------------------------------------------------------- public function ajax_clear_history() { check_ajax_referer( 'wtofe_chatbot_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( 'Insufficient permissions.' ); } $user_id = isset( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0; $before_date = isset( $_POST['before_date'] ) ? sanitize_text_field( wp_unslash( $_POST['before_date'] ) ) : ''; AI_Content_Generator_Chatbot_DB::clear_history_before( $user_id, $before_date ); wp_send_json_success( array( 'message' => __( 'Chat history cleared.', 'ai-content-generator-for-woocommerce' ) ) ); } /** * AJAX: one page of users with chat history, for the settings app's * history table. */ public function ajax_get_history_users() { check_ajax_referer( 'wtofe_chatbot_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( 'Insufficient permissions.' ); } $page = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; $result = AI_Content_Generator_Chatbot_DB::get_users_with_history( self::HISTORY_PER_PAGE, $page ); $users = array(); foreach ( $result['users'] as $row ) { $user = get_userdata( $row['user_id'] ); $users[] = array( 'user_id' => absint( $row['user_id'] ), 'name' => $user ? $user->display_name : __( 'User', 'ai-content-generator-for-woocommerce' ), 'display' => $user ? $user->display_name . ' (' . $user->user_email . ')' : __( 'Deleted User', 'ai-content-generator-for-woocommerce' ) . ' #' . absint( $row['user_id'] ), 'message_count' => absint( $row['message_count'] ), 'last_message' => $row['last_message'], ); } wp_send_json_success( array( 'users' => $users, 'total' => (int) $result['total'], 'perPage' => self::HISTORY_PER_PAGE, ) ); } public function ajax_get_user_history() { check_ajax_referer( 'wtofe_chatbot_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( 'Insufficient permissions.' ); } $user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0; $date_from = isset( $_GET['date_from'] ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : ''; $date_to = isset( $_GET['date_to'] ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : ''; $history = AI_Content_Generator_Chatbot_DB::get_user_history_admin( $user_id, $date_from, $date_to ); // Admin is already permission-checked above (manage_woocommerce), so // treat as a trusted viewer regardless of whether the placeholder was // originally generated in an admin or customer context — this lets // the admin panel keep showing working links for any customer's past // orders too, without needing that customer to be logged in. if ( class_exists( 'WTOFE_Chatbot_Module_Print' ) ) { foreach ( $history as &$entry ) { if ( ! empty( $entry['bot_response'] ) ) { $entry['bot_response'] = WTOFE_Chatbot_Module_Print::resolve_placeholders( $entry['bot_response'], true, get_current_user_id() ); } } unset( $entry ); } wp_send_json_success( array( 'history' => $history ) ); } }