is_woocommerce_admin_page( $hook ); } /** * Detects all major WooCommerce admin pages. */ private function is_woocommerce_admin_page( $hook ) { // WooCommerce core admin page slugs (page= query param). $wc_pages = array( 'wc-admin', 'wc-reports', 'wc-settings', 'wc-status', 'wc-addons', 'woocommerce', 'woocommerce-marketing', ); // Plugin's own page. $plugin_pages = array( 'wc-ai-content-generator' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( $current_page && ( in_array( $current_page, $wc_pages, true ) || in_array( $current_page, $plugin_pages, true ) ) ) { return true; } // Hook-based detection for WC post-type screens. $wc_hooks = array( 'post.php', 'post-new.php', // product/order edit 'edit.php', // product/order/coupon lists ); if ( in_array( $hook, $wc_hooks, true ) ) { $screen = get_current_screen(); if ( $screen && in_array( $screen->post_type, array( 'product', 'shop_order', 'shop_coupon' ), true ) ) { return true; } } // WooCommerce Analytics / Reports pages. if ( $hook === 'woocommerce_page_wc-admin' ) { return true; } // Any admin page starting with woocommerce. if ( strpos( $hook, 'woocommerce' ) !== false ) { return true; } // Any admin page of webtoffee. if ( strpos( $hook, 'wt' ) !== false ) { return true; } // WooCommerce customer list (users.php with role filter). // phpcs:ignore WordPress.Security.NonceVerification.Recommended $role = isset( $_GET['role'] ) ? sanitize_text_field( wp_unslash( $_GET['role'] ) ) : ''; if ( $hook === 'users.php' && $role === 'customer' ) { return true; } return false; } public function enqueue_assets( $hook ) { if ( ! $this->should_show( $hook ) ) { return; } wp_enqueue_style( 'wtofe-chatbot-admin', WTOFE_AI_PLUGIN_URL . 'public/modules/chatbot/assets/css/chatbot.css', array(), WTOFE_AI_VERSION ); // Admin-specific overrides. wp_add_inline_style( 'wtofe-chatbot-admin', ' /* Nudge bubble above WP admin bar if present */ #wtofe-chatbot-fab { bottom: 28px; right: 28px; z-index: 100000; } #wtofe-chatbot-window { bottom: 98px; right: 28px; z-index: 100001; } /* Action result banners */ .wtofe-action-result { margin-top:10px; padding:8px 12px; border-radius:8px; font-size:13px; line-height:1.5; } .wtofe-action-success { background:#d1fae5; color:#065f46; border:1px solid #6ee7b7; } .wtofe-action-error { background:#fee2e2; color:#991b1b; border:1px solid #fca5a5; } .wtofe-action-result a{ color:inherit; text-decoration:underline; } /* Bubble rendered HTML */ .wtofe-bubble-bot ul { margin:6px 0 6px 16px; padding:0; list-style:disc; } .wtofe-bubble-bot li { margin-bottom:3px; } .wtofe-bubble-bot p { margin:4px 0; } .wtofe-bubble-bot a { color:#4f46e5; text-decoration:underline; } .wtofe-bubble-bot strong { font-weight:600; } /* CSV import button */ #wtofe-chatbot-import-btn { width:36px; height:36px; border-radius:50%; background:transparent; color:#6b7280; border:1px solid #d1d5db; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; transition:background .15s,color .15s; padding:0; } #wtofe-chatbot-import-btn:hover { background:#f3f4f6; color:#374151; } #wtofe-chatbot-import-btn.wtofe-importing { opacity:.5; cursor:not-allowed; } ' ); wp_enqueue_script( 'wtofe-chatbot-admin', WTOFE_AI_PLUGIN_URL . 'public/modules/chatbot/assets/js/chatbot.js', array( 'jquery' ), WTOFE_AI_VERSION, true ); $chatbot_name = get_option( 'wtofe_chatbot_name', 'Store Assistant' ); wp_localize_script( 'wtofe-chatbot-admin', 'wtofeChatbot', array( 'restUrl' => esc_url_raw( set_url_scheme( rest_url( 'wtofe-ai/v1/chatbot' ), is_ssl() ? 'https' : 'http' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'ajaxUrl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ), 'importNonce' => wp_create_nonce( 'wtofe_chatbot_import_nonce' ), 'importPluginActive' => WTOFE_Chatbot_Module_Import::is_dependency_active(), 'importPluginInstallUrl' => esc_url_raw( admin_url( 'plugin-install.php?s=product-import-export-for-woo&tab=search&type=term' ) ), 'welcomeMsg' => __( 'Hi! Ask me to create products, update orders, generate content, manage coupons, and more.', 'ai-content-generator-for-woocommerce' ), 'placeholder' => __( 'Create a product, update order #123, generate description…', 'ai-content-generator-for-woocommerce' ), 'chatbotName' => $chatbot_name, 'isAdmin' => true, 'isGuest' => false, 'i18n' => array( 'sending' => __( 'Working…', 'ai-content-generator-for-woocommerce' ), 'error' => __( 'Something went wrong. Please try again.', 'ai-content-generator-for-woocommerce' ), 'clearChat' => __( 'Clear chat', 'ai-content-generator-for-woocommerce' ), 'closeChat' => __( 'Close chat', 'ai-content-generator-for-woocommerce' ), 'typeHere' => __( 'Create a product, update order #123, generate description…', 'ai-content-generator-for-woocommerce' ), ), ) ); } public function render_bubble( ) { $hook = ''; // Not needed for render — should_show() already ran in enqueue. // Check the option directly since $hook isn't available here. // The admin bubble is independent of the frontend "Enable Chatbot" toggle. if ( ! (bool) get_option( 'wtofe_chatbot_admin_bubble', true ) ) return; if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'manage_options' ) ) return; // Only render if the script was enqueued (meaning should_show() returned true). if ( ! wp_script_is( 'wtofe-chatbot-admin', 'enqueued' ) ) return; $chatbot_name = esc_html( get_option( 'wtofe_chatbot_name', 'Store Assistant' ) ); $avatar_url = esc_url( get_option( 'wtofe_chatbot_avatar_url', '' ) ); $primary_color = esc_attr( get_option( 'wtofe_chatbot_primary_color', '#000000' ) ); $bubble_color = esc_attr( get_option( 'wtofe_chatbot_bubble_color', '#000000' ) ); $text_color = esc_attr( get_option( 'wtofe_chatbot_text_color', '#ffffff' ) ); $is_admin_bubble = true; require WTOFE_AI_PLUGIN_DIR . 'public/modules/chatbot/chatbot-bubble.php'; } }