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 '