init = $init; $this->data_builder = $data_builder; } /** * Set script translations for a given script handle. * * Enables JavaScript i18n via @wordpress/i18n for the specified script. * * @since 2.5.12 * @param string $handle Script handle to set translations for. */ private function set_script_translations( string $handle ): void { wp_set_script_translations( $handle, 'ai-product-tools', AIPT_PLUGIN_DIR . 'languages' ); } /** * Enqueue assets for hook * * @param string $hook WordPress hook name. */ public function enqueueForHook( string $hook ): void { $screen = get_current_screen(); $is_plugin_page = ( 'toplevel_page_ai-product-tools' === $hook || strpos( $hook, 'page_aipt-' ) !== false || ( 'post.php' === $hook || 'post-new.php' === $hook ) && $screen && $screen->post_type === 'product' ); if ( ! $is_plugin_page ) { return; } $this->enqueueBaseAssets( $hook ); $this->enqueueNotificationManager(); $this->enqueueMetaboxAssets( $hook, $screen ); $this->enqueueSettingsAssets( $hook, $screen ); $this->enqueueToolkitAssets( $hook, $screen ); $this->enqueueDescriptionGeneratorAssets( $hook ); $this->enqueueFaqGeneratorAssets( $hook ); $this->enqueueTitleGeneratorAssets( $hook ); $this->enqueueAutomationJobsAssets( $hook ); $this->enqueueStoreIndexAssets( $hook ); $this->enqueueChatbotAdminAssets( $hook ); $this->enqueueSeoMetaGeneratorAssets( $hook ); $this->enqueueTaxonomyGeneratorAssets( $hook ); $this->enqueueImageAltGeneratorAssets( $hook ); $this->enqueueProductEditorAssets( $hook ); $this->enqueueContentHistoryAssets( $hook ); $this->enqueueContentTemplatesAssets( $hook ); $this->enqueueMcpAgentsAssets( $hook ); } /** * Enqueue base assets (vendors, commons, main styles) * * Main styles are always loaded - Freemius opt-in isolation is handled * via CSS rules (all: revert) in tailwind.css instead of PHP gating. * * @param string $hook WordPress hook name. */ private function enqueueBaseAssets( string $hook ): void { if ( 'toplevel_page_ai-product-tools' === $hook || strpos( $hook, 'page_aipt-advanced-bulk-generator' ) !== false || strpos( $hook, 'page_aipt-title-generator' ) !== false || strpos( $hook, 'page_aipt-faq-generator' ) !== false ) { if ( function_exists( 'wp_enqueue_editor' ) ) { wp_enqueue_editor(); wp_enqueue_media(); } } if ( strpos( $hook, 'page_aipt-chatbot' ) !== false ) { wp_enqueue_media(); } wp_enqueue_style( 'aipt-main-styles', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'css/commons.css' ), array(), $this->init->get_asset_version( 'css/commons.css' ) ); $vendor_js = $this->init->get_asset_path( 'js/vendors.js' ); if ( $vendor_js ) { wp_enqueue_script( 'aipt-vendors', AIPT_PLUGIN_URL . $vendor_js, array(), $this->init->get_asset_version( 'js/vendors.js' ), true ); } $commons_js = $this->init->get_asset_path( 'js/commons.js' ); if ( $commons_js ) { wp_enqueue_script( 'aipt-commons', AIPT_PLUGIN_URL . $commons_js, array( 'aipt-vendors', 'wp-i18n' ), $this->init->get_asset_version( 'js/commons.js' ), true ); $this->set_script_translations( 'aipt-commons' ); } } /** * Enqueue notification manager assets */ private function enqueueNotificationManager(): void { $this->init->enqueue_virtual_style( 'aipt-notification-manager', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-notification-manager', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/notification-manager.js' ), array( 'aipt-commons', 'react-jsx-runtime', 'wp-element', 'wp-i18n' ), $this->init->get_asset_version( 'js/notification-manager.js' ), true ); $this->set_script_translations( 'aipt-notification-manager' ); } /** * Enqueue metabox assets * * @param string $hook WordPress hook name. * @param object $screen Current screen object. */ private function enqueueMetaboxAssets( string $hook, $screen ): void { if ( ! current_user_can( 'use_aipt_tools' ) ) { return; } if ( 'post.php' === $hook || 'post-new.php' === $hook ) { if ( $screen && $screen->post_type === 'product' ) { $this->init->enqueue_virtual_style( 'aipt-metabox', array( 'aipt-main-styles' ) ); wp_add_inline_style( 'aipt-metabox', self::METABOX_CHROME_CSS ); wp_enqueue_script( 'aipt-metabox', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/metabox.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/metabox.js' ), true ); $this->set_script_translations( 'aipt-metabox' ); $metabox_data = $this->data_builder->buildMetaboxData(); wp_localize_script( 'aipt-metabox', 'aiptData', $metabox_data ); } } } /** * Enqueue settings page assets * * @param string $hook WordPress hook name. * @param object $screen Current screen object. */ private function enqueueSettingsAssets( string $hook, $screen ): void { if ( $screen && ( strpos( $hook, '_page_aipt-settings' ) !== false ) ) { $this->init->enqueue_virtual_style( 'aipt-settings', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-settings', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/settings.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/settings.js' ), true ); $this->set_script_translations( 'aipt-settings' ); $settings_data = $this->data_builder->buildSettingsData(); wp_localize_script( 'aipt-settings', 'aiptData', $settings_data ); } } /** * Enqueue toolkit page assets * * @param string $hook WordPress hook name. * @param object $screen Current screen object. */ private function enqueueToolkitAssets( string $hook, $screen ): void { if ( 'toplevel_page_ai-product-tools' === $hook && ( ! $screen || $screen->id === 'toplevel_page_ai-product-tools' ) ) { $toolkit_css = $this->init->get_asset_path( 'css/toolkit.css' ); if ( file_exists( AIPT_PLUGIN_DIR . $toolkit_css ) ) { wp_enqueue_style( 'aipt-toolkit', AIPT_PLUGIN_URL . $toolkit_css, array( 'aipt-main-styles' ), $this->init->get_asset_version( 'css/toolkit.css' ) ); } else { $this->init->enqueue_virtual_style( 'aipt-toolkit', array( 'aipt-main-styles' ) ); } wp_enqueue_script( 'aipt-toolkit', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/toolkit.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/toolkit.js' ), true ); $this->set_script_translations( 'aipt-toolkit' ); $toolkit_data = $this->data_builder->buildToolkitData(); wp_localize_script( 'aipt-toolkit', 'aiptData', $toolkit_data ); } } /** * Enqueue description generator assets * * @param string $hook WordPress hook name. */ private function enqueueDescriptionGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-advanced-bulk-generator' ) !== false ) { $this->enqueueContentHistoryStyles(); $this->init->enqueue_virtual_style( 'aipt-description-generator', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-description-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/description-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/description-generator.js' ), true ); $this->set_script_translations( 'aipt-description-generator' ); $desc_data = $this->data_builder->buildDescriptionGeneratorData(); wp_localize_script( 'aipt-description-generator', 'aiptData', $desc_data ); } } /** * Enqueue FAQ generator assets * * @param string $hook WordPress hook name. */ private function enqueueFaqGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-faq-generator' ) !== false ) { $this->enqueueContentHistoryStyles(); $this->init->enqueue_virtual_style( 'aipt-faq-generator', array( 'aipt-main-styles' ) ); wp_enqueue_style( 'aipt-faq-display', AIPT_PLUGIN_URL . 'dist/css/faq-display.css', array(), $this->init->get_asset_version( 'css/faq-display.css' ) ); \AIPT\Api\BulkGenerator\FAQ\FAQFrontendRenderer::styles_enqueued(); wp_enqueue_script( 'aipt-faq-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/faq-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/faq-generator.js' ), true ); $this->set_script_translations( 'aipt-faq-generator' ); $faq_data = $this->data_builder->buildFAQGeneratorData(); wp_localize_script( 'aipt-faq-generator', 'aiptData', $faq_data ); } } /** * Enqueue title generator assets * * @param string $hook WordPress hook name. */ private function enqueueTitleGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-title-generator' ) !== false ) { $this->enqueueContentHistoryStyles(); $this->init->enqueue_virtual_style( 'aipt-title-generator', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-title-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/title-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/title-generator.js' ), true ); $this->set_script_translations( 'aipt-title-generator' ); $title_data = $this->data_builder->buildTitleGeneratorData(); wp_localize_script( 'aipt-title-generator', 'aiptData', $title_data ); } } /** * Enqueue automation jobs assets * * @param string $hook WordPress hook name. */ private function enqueueAutomationJobsAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-automation-jobs' ) !== false ) { $this->init->enqueue_virtual_style( 'aipt-automation-jobs', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-automation-jobs', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/automation-jobs.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/automation-jobs.js' ), true ); $this->set_script_translations( 'aipt-automation-jobs' ); $jobs_data = $this->data_builder->buildAutomationJobsData(); wp_localize_script( 'aipt-automation-jobs', 'aiptData', $jobs_data ); } } /** * Enqueue AI Store Index assets * * @param string $hook WordPress hook name. */ private function enqueueStoreIndexAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-store-index' ) !== false ) { $this->init->enqueue_virtual_style( 'aipt-store-index', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-store-index', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/store-index.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/store-index.js' ), true ); $this->set_script_translations( 'aipt-store-index' ); $index_data = $this->data_builder->buildStoreIndexData(); wp_localize_script( 'aipt-store-index', 'aiptData', $index_data ); } } /** * Enqueue AI Shop Assistant admin assets * * @param string $hook WordPress hook name. */ private function enqueueChatbotAdminAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-chatbot' ) !== false ) { $this->init->enqueue_virtual_style( 'aipt-chatbot-admin', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-chatbot-admin', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/chatbot-admin.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/chatbot-admin.js' ), true ); $this->set_script_translations( 'aipt-chatbot-admin' ); $chatbot_data = $this->data_builder->buildChatbotAdminData(); wp_localize_script( 'aipt-chatbot-admin', 'aiptChatbotAdminData', $chatbot_data ); wp_localize_script( 'aipt-chatbot-admin', 'aiptData', array_intersect_key( $chatbot_data, array_flip( array( 'nonce', 'ajaxurl', 'adminUrl', 'restUrl', 'version', 'locale', 'fs' ) ) ) ); } } /** * Enqueue SEO Meta Generator assets * * @param string $hook WordPress hook name. */ private function enqueueSeoMetaGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-seo-meta-generator' ) !== false ) { $this->enqueueContentHistoryStyles(); $this->init->enqueue_virtual_style( 'aipt-seo-meta-generator', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-seo-meta-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/seo-meta-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/seo-meta-generator.js' ), true ); $this->set_script_translations( 'aipt-seo-meta-generator' ); $seo_data = $this->data_builder->buildSeoMetaGeneratorData(); wp_localize_script( 'aipt-seo-meta-generator', 'aiptData', $seo_data ); } } /** * Enqueue Image Alt Text Generator admin assets. * * Loads the virtual stylesheet and JS bundle plus its localised aiptData * payload when the current admin page hook matches the Image Alt * Generator slug. * * @since 2.6.0 * * @param string $hook Current admin page hook suffix. * @return void */ private function enqueueImageAltGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-image-alt' ) !== false ) { $this->enqueueContentHistoryStyles(); $this->init->enqueue_virtual_style( 'aipt-image-alt-generator', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-image-alt-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/image-alt-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/image-alt-generator.js' ), true ); $this->set_script_translations( 'aipt-image-alt-generator' ); $data = $this->data_builder->buildImageAltGeneratorData(); wp_localize_script( 'aipt-image-alt-generator', 'aiptData', $data ); } } private function enqueueTaxonomyGeneratorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-taxonomy-generator' ) !== false ) { $this->init->enqueue_virtual_style( 'aipt-taxonomy-generator', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-taxonomy-generator', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/taxonomy-generator.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/taxonomy-generator.js' ), true ); $this->set_script_translations( 'aipt-taxonomy-generator' ); $taxonomy_data = $this->data_builder->buildTaxonomyGeneratorData(); wp_localize_script( 'aipt-taxonomy-generator', 'aiptData', $taxonomy_data ); } } /** * Enqueue AI Bulk Product Editor assets * * @param string $hook WordPress hook name. */ private function enqueueProductEditorAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-product-editor' ) !== false ) { wp_enqueue_style( 'aipt-product-editor', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'css/product-editor.css' ), array( 'aipt-main-styles' ), $this->init->get_asset_version( 'css/product-editor.css' ) ); wp_enqueue_script( 'aipt-product-editor', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/product-editor.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/product-editor.js' ), true ); $this->set_script_translations( 'aipt-product-editor' ); $product_editor_data = $this->data_builder->buildProductEditorData(); wp_localize_script( 'aipt-product-editor', 'aiptData', $product_editor_data ); } } /** * Enqueue the Content History design system stylesheet * (.aipt-ch__* structure + --ch-* custom properties). * * Shared by the History hub itself and by the generator surfaces that embed * HistoryPanel / RestoreAction inline (Description, FAQ, Title, SEO Meta, * Image Alt legacy) -- everything else renders it as unstyled divs without * this. Call from a page-specific enqueue method, never unconditionally -- * see design audit finding S9 for why it used to load everywhere. */ private function enqueueContentHistoryStyles(): void { wp_enqueue_style( 'aipt-content-history-styles', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'css/content-history.css' ), array( 'aipt-main-styles' ), $this->init->get_asset_version( 'css/content-history.css' ) ); } /** * Enqueue Content History assets * * @param string $hook WordPress hook name. */ private function enqueueContentHistoryAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-history' ) === false ) { return; } $this->enqueueContentHistoryStyles(); wp_enqueue_script( 'aipt-content-history', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/content-history.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/content-history.js' ), true ); $this->set_script_translations( 'aipt-content-history' ); wp_localize_script( 'aipt-content-history', 'aiptData', $this->data_builder->buildContentHistoryData() ); } /** * Enqueue Content templates page assets * * @param string $hook WordPress hook name. */ private function enqueueContentTemplatesAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-content-templates' ) === false ) { return; } $this->init->enqueue_virtual_style( 'aipt-content-templates', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-content-templates', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/content-templates.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/content-templates.js' ), true ); $this->set_script_translations( 'aipt-content-templates' ); wp_localize_script( 'aipt-content-templates', 'aiptData', $this->data_builder->buildContentStructureData() ); } /** * Enqueue AI Agents page assets * * @param string $hook WordPress hook name. */ private function enqueueMcpAgentsAssets( string $hook ): void { if ( strpos( $hook, 'page_aipt-mcp-agents' ) === false ) { return; } $this->init->enqueue_virtual_style( 'aipt-mcp-agents', array( 'aipt-main-styles' ) ); wp_enqueue_script( 'aipt-mcp-agents', AIPT_PLUGIN_URL . $this->init->get_asset_path( 'js/mcp-agents.js' ), array( 'aipt-commons', 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'aipt-notification-manager' ), $this->init->get_asset_version( 'js/mcp-agents.js' ), true ); $this->set_script_translations( 'aipt-mcp-agents' ); wp_localize_script( 'aipt-mcp-agents', 'aiptData', $this->data_builder->buildMcpAgentsData() ); } /** * Enqueue AI Shop Assistant widget assets for frontend * * @since 2.5.0 */ public function enqueueChatbotWidgetAssets(): void { $settings = get_option( 'aipt_chatbot_settings', array() ); $is_enabled = isset( $settings['general']['enabled'] ) ? (bool) $settings['general']['enabled'] : false; if ( ! $is_enabled ) { return; } if ( ! $this->init->should_show_chatbot_on_page( $settings ) ) { return; } $css_path = $this->init->get_asset_path( 'css/chatbot-widget.css' ); if ( $css_path ) { wp_enqueue_style( 'aipt-chatbot-widget', AIPT_PLUGIN_URL . $css_path, array(), $this->init->get_asset_version( 'css/chatbot-widget.css' ) ); } $js_path = $this->init->get_asset_path( 'js/chatbot-widget.js' ); if ( $js_path ) { wp_enqueue_script( 'aipt-chatbot-widget', AIPT_PLUGIN_URL . $js_path, array(), $this->init->get_asset_version( 'js/chatbot-widget.js' ), true ); $widget_data = $this->data_builder->buildChatbotWidgetData(); $widget_data['ajaxUrl'] = admin_url( 'admin-ajax.php' ); wp_localize_script( 'aipt-chatbot-widget', 'aiptChatbotWidgetData', $widget_data ); } } }