init = $init; } /** * Register admin menu pages * * Creates main plugin menu and all submenu pages. */ public function registerMenus(): void { $icon_base64 = $this->getCachedIcon(); add_menu_page( __( 'AI Product Tools', 'ai-product-tools' ), __( 'AI Product Tools', 'ai-product-tools' ), 'use_aipt_tools', 'ai-product-tools', array( $this, 'renderToolkitPage' ), $icon_base64, 56 ); add_submenu_page( 'ai-product-tools', __( 'AI Toolkit', 'ai-product-tools' ), __( 'AI Toolkit', 'ai-product-tools' ), 'use_aipt_tools', 'ai-product-tools', array( $this, 'renderToolkitPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Content Templates', 'ai-product-tools' ), __( 'Content Templates', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-content-templates', array( $this, 'renderContentTemplatesPage' ) ); add_submenu_page( 'ai-product-tools', __( 'AI Bulk Product Editor', 'ai-product-tools' ), __( 'AI Bulk Product Editor', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-product-editor', array( $this, 'renderProductEditorPage' ) ); $automation_label = $this->getAutomationSurfaceLabel(); add_submenu_page( 'ai-product-tools', $automation_label, $automation_label, 'manage_woocommerce', 'aipt-automation-jobs', array( $this, 'renderAutomationJobsPage' ) ); add_submenu_page( 'ai-product-tools', __( 'AI Agents', 'ai-product-tools' ), __( 'AI Agents', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-mcp-agents', array( $this, 'renderMcpAgentsPage' ) ); add_submenu_page( 'ai-product-tools', __( 'AI Store Index', 'ai-product-tools' ), __( 'AI Store Index', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-store-index', array( $this, 'renderStoreIndexPage' ) ); add_submenu_page( 'ai-product-tools', __( 'AI Shop Assistant', 'ai-product-tools' ), __( 'AI Shop Assistant', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-chatbot', array( $this, 'renderChatbotPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Content Bank', 'ai-product-tools' ), __( 'Content Bank', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-history', array( $this, 'renderContentHistoryPage' ) ); $standard_redirect_hook = add_submenu_page( 'ai-product-tools', __( 'Standard Bulk Generator', 'ai-product-tools' ), __( 'Standard Bulk Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-standard-bulk-generator', array( $this, 'redirectStandardBulkGeneratorPage' ) ); if ( $standard_redirect_hook ) { add_action( 'load-' . $standard_redirect_hook, array( $this, 'redirectStandardBulkGeneratorPage' ) ); } add_submenu_page( 'ai-product-tools', __( 'Bulk Description Generator', 'ai-product-tools' ), __( 'Bulk Description Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-advanced-bulk-generator', array( $this, 'renderBulkGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Bulk FAQ Generator', 'ai-product-tools' ), __( 'Bulk FAQ Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-faq-generator', array( $this, 'renderFaqGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'AI Bulk Taxonomy Generator', 'ai-product-tools' ), __( 'AI Bulk Taxonomy Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-taxonomy-generator', array( $this, 'renderTaxonomyGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Bulk Title Generator', 'ai-product-tools' ), __( 'Bulk Title Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-title-generator', array( $this, 'renderTitleGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Bulk SEO Meta Generator', 'ai-product-tools' ), __( 'Bulk SEO Meta Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-seo-meta-generator', array( $this, 'renderSeoMetaGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Bulk Image Alt Generator', 'ai-product-tools' ), __( 'Bulk Image Alt Generator', 'ai-product-tools' ), 'manage_woocommerce', 'aipt-image-alt-generator', array( $this, 'renderImageAltGeneratorPage' ) ); add_submenu_page( 'ai-product-tools', __( 'Settings', 'ai-product-tools' ), __( 'Settings', 'ai-product-tools' ), 'manage_aipt_settings', 'aipt-settings', array( $this, 'renderSettingsPage' ) ); } /** * Admin page slugs of the six generator pages hidden from the WP admin * left nav. * * Each page stays registered via its own add_submenu_page() call in * {@see registerMenus()} so the slug keeps routing at its existing URL; * only the admin-menu ROW is hidden, with CSS (see * {@see hideGeneratorMenuItems()}) — never remove_submenu_page(). An * empirical probe against real WordPress core * (wp-admin/includes/menu.php) found that removing a live submenu row * makes user_can_access_admin_page() recompute the page's hook as * `admin_page_{slug}` once its parent no longer resolves, which never * matches the hook WordPress actually registered for it — a hard 403 for * every user, administrators included, on direct navigation. * {@see mapGeneratorPagesToSubmenuFile()} keeps the still-visible * "AI Toolkit" row highlighted while one of these six is open. * * Derived from {@see Generators::ADMIN_SLUGS} (via {@see Generators::all()} * and {@see Generators::adminSlug()}) rather than a second hand-typed * list: that registry is already the documented single source of truth * for this exact six-slug map, consumed the same way by * LocalizationDataBuilder and ContentStructureDataBuilder. * * @return string[] */ private function hiddenGeneratorSlugs(): array { return array_map( array( Generators::class, 'adminSlug' ), Generators::all() ); } /** * Admin page slug of the retired Standard Bulk Generator page, hidden * from the WP admin left nav the same way as {@see hiddenGeneratorSlugs()}'s * six rows: stays add_submenu_page()'d in {@see registerMenus()} (so the * slug keeps routing and its load-{hook} redirect keeps firing for old * links) and only its row is hidden here, never remove_submenu_page(). * * @return string */ private function retiredStandardBulkGeneratorSlug(): string { return 'aipt-standard-bulk-generator'; } /** * Hide the six generator rows, plus the retired Standard Bulk Generator * row, from the WP admin left nav. * * The pages themselves stay fully registered and reachable at their * existing URLs (see {@see hiddenGeneratorSlugs()} and * {@see retiredStandardBulkGeneratorSlug()}); only the row is hidden, * with a small inline stylesheet scoped to #adminmenu, following * {@see \AIPT\Admin\NoticeManager::hide_notices_on_plugin_pages()}'s * admin_head + raw ' . "\n"; } /** * Keep the "AI Toolkit" row highlighted while a hidden generator page * is open. * * The six pages stay registered (see {@see hiddenGeneratorSlugs()}), so * WordPress's own submenu_file resolution already finds a real match for * them; this filter only redirects that match to the still-visible * parent row so the CSS-hidden child does not leave the WP admin left * nav with nothing highlighted. * * @param string $submenu_file The submenu slug WordPress would otherwise highlight. * @return string */ public function mapGeneratorPagesToSubmenuFile( $submenu_file ) { $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( in_array( $page, $this->hiddenGeneratorSlugs(), true ) ) { return 'ai-product-tools'; } return $submenu_file; } /** * Resolve the automation surface label for the current entitlement. * * The free build caps automation at one job, `once`/manual cadence and a * single content type per job, so it is presented as "Scheduled Bulk Runs". * Business raises all three ceilings and keeps the "Automation" name. * * Filter-mediated (G9): the always-shipped code carries NO license check. A * premium service (RecurringScheduleService) registers * `aipt_automation_surface_is_business` and returns the Business entitlement; * on a free build the filter is unregistered so the false default yields the * free label. Mirrors `getAutomationSurfaceName()` in * `src/Components/AutomationJobs/businessScope.tsx`. * * @return string Localized menu label. */ private function getAutomationSurfaceLabel(): string { $is_business = (bool) apply_filters( 'aipt_automation_surface_is_business', false ); return $is_business ? __( 'Automation', 'ai-product-tools' ) : __( 'Scheduled Bulk Runs', 'ai-product-tools' ); } /** * Get cached SVG icon * * Loads and caches the plugin menu icon. * * @return string Base64-encoded SVG or dashicon fallback. */ private function getCachedIcon(): string { if ( $this->icon_cache === null ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; $icon_svg = ''; if ( $wp_filesystem ) { $icon_svg = $wp_filesystem->get_contents( AIPT_PLUGIN_DIR . 'dist/img/aipt-icon.svg' ); } $this->icon_cache = $icon_svg ? 'data:image/svg+xml;base64,' . base64_encode( $icon_svg ) : 'dashicons-admin-generic'; } return $this->icon_cache; } /** * Render Toolkit page */ public function renderToolkitPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Settings page */ public function renderSettingsPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Advanced Bulk Generator page */ public function renderBulkGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Redirect the retired Standard Bulk Generator slug to the merged Advanced page. * * Wired on the page's `load-{hook}` action (see {@see registerMenus()}) so the * redirect runs before any admin HTML is emitted; wp_safe_redirect() cannot set * the Location header from the later page-render callback. */ public function redirectStandardBulkGeneratorPage(): void { wp_safe_redirect( admin_url( 'admin.php?page=aipt-advanced-bulk-generator' ) ); exit; } /** * Render FAQ Generator page */ public function renderFaqGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Title Generator page */ public function renderTitleGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Automation Jobs page */ public function renderAutomationJobsPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render AI Store Index page */ public function renderStoreIndexPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render AI Shop Assistant page */ public function renderChatbotPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Bulk SEO Meta Generator page */ public function renderSeoMetaGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Taxonomy Generator page */ public function renderTaxonomyGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Bulk Image Alt Generator page */ public function renderImageAltGeneratorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render AI Bulk Product Editor page */ public function renderProductEditorPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Content History page */ public function renderContentHistoryPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render Content templates page */ public function renderContentTemplatesPage(): void { echo '
'; echo '
'; echo '
'; } /** * Render AI Agents page */ public function renderMcpAgentsPage(): void { echo '
'; echo '
'; echo '
'; } /** * Surface the AI Bulk Product Editor under the WooCommerce Products menu. * * The page is registered canonically in {@see registerMenus()} under the * plugin's own menu; this adds a second, WooCommerce-native entry point * (the "Products → …" location merchants expect) that links to the same * page. Hooked on `admin_menu` at a late priority so the Products menu * already exists when this runs. * * The entry is a plain link (a URL slug and no callback), not a second * registration of the `aipt-product-editor` slug: with the slug under two * menus, WordPress opens the first one that lists it (Products) while it * marks the row current under the plugin menu, so the left nav showed * Products open with nothing highlighted. A `parent_file` filter cannot * correct that, because menu-header.php re-runs get_admin_page_parent() * after the filter and overwrites the parent of such a plugin page. */ public function registerProductsSubmenu(): void { add_submenu_page( 'edit.php?post_type=product', __( 'AI Bulk Product Editor', 'ai-product-tools' ), __( 'AI Bulk Product Editor', 'ai-product-tools' ), 'manage_woocommerce', 'admin.php?page=aipt-product-editor' ); } /** * Add an "AI Bulk Product Editor" shortcut to the top admin bar. * * Only rendered for users who can reach the page. Links to the canonical * admin.php URL so it resolves regardless of which menu parent is active. * * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance. */ public function registerAdminBarNode( \WP_Admin_Bar $wp_admin_bar ): void { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $wp_admin_bar->add_node( array( 'id' => 'aipt-product-editor', 'title' => __( 'AI Bulk Product Editor', 'ai-product-tools' ), 'href' => admin_url( 'admin.php?page=aipt-product-editor' ), 'meta' => array( 'title' => __( 'Open the AI Bulk Product Editor', 'ai-product-tools' ), ), ) ); } }