PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 All 203 releases
← All changes | includes/Core/Admin.php +557 -54 4.5.6 → 4.9.5 View file →
@@ -19,8 +19,42 @@
19 19 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
20 20
21 21 class Admin extends Base {
22 22 /**
23 + * Per-user flag recording that this administrator has opened the Content IQ
24 + * screen — the discovery badge (ADR-063) now flags Content Intelligence, the
25 + * headline feature, rather than MCP.
26 + *
27 + * Stores the timestamp of the first visit, but only its **presence** is read:
28 + * absent means "this user has not seen Content IQ yet", which is what puts the
29 + * one-time discovery badge on the menu. Per user on purpose — two administrators
30 + * each get their own first look, and neither clears the other's. A deliberately
31 + * fresh meta key (not the old `betterdocs_mcp_seen`) so a user who already
32 + * dismissed the MCP badge still gets this one for the new feature.
33 + *
34 + * The private `*_mcp_*` helper names below are kept as-is to hold the diff to
35 + * the target slug + this key; they now paint the Content IQ item.
36 + *
37 + * @var string
38 + * @since 4.9.0
39 + */
40 + const MCP_SEEN_META = 'betterdocs_content_iq_seen';
41 +
42 + /**
43 + * Whether this request painted the MCP discovery badge onto the menu.
44 + *
45 + * Decided once in `menus()` (on `admin_menu`) and read again in
46 + * `mcp_badge_styles()` (on `admin_head`), rather than re-deciding, because the
47 + * two must agree: on the request that opens the MCP screen the badge is still
48 + * painted while the meta is already written, and re-deciding at `admin_head`
49 + * would leave that one painted pill unstyled.
50 + *
51 + * @var bool
52 + * @since 4.9.0
53 + */
54 + private $mcp_badge = false;
55 +
56 + /**
23 57 * @var CacheBank
24 58 */
25 59 private static $cache_bank;
26 60 /**
@@ -72,9 +106,8 @@
72 106 *
73 107 * @var FAQBuilder
74 108 */
75 109 private $faq_builder;
76 - private $glossaries;
77 110
78 111 public function __construct( Container $container, PostType $type, Enqueue $assets, Settings $settings, KBMigration $kbmigration ) {
79 112 $this->container = $container;
80 113 $this->assets = $assets;
@@ -88,9 +121,8 @@
88 121 $type->init();
89 122 $type->admin_init();
90 123
91 124 $this->faq_builder = $this->container->get( FAQBuilder::class );
92 - $this->glossaries = $this->container->get( Glossaries::class );
93 125
94 126 /**
95 127 * Register usage tracking (including the daily `put_do_weekly_action` cron
96 128 * handler) on every request — WP-Cron runs with is_admin() === false, so
@@ -107,14 +139,31 @@
107 139
108 140 $this->installer = new PluginInstaller();
109 141
110 142 add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
143 + // The WPNotice CacheBank wipes all admin_notices at priority 10 on BetterDocs
144 + // screens, so the hook above never renders inside the BetterDocs panels.
145 + // Re-add the compatibility notice after that wipe (in_admin_header, priority
146 + // 999) so it shows on the panels like the review / license notices.
147 + add_action( 'in_admin_header', function () {
148 + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
149 + if ( $screen && betterdocs()->is_betterdocs_screen( $screen->id ) ) {
150 + add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
151 + }
152 + }, 999 );
111 153 // add_action( 'admin_init', [$this, 'notices'], 9 );
112 154 add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
113 155
114 156 add_action( 'admin_menu', array( $this, 'menus' ) );
157 + // The badge's clear runs on `admin_init` — a hook that fires for every
158 + // admin request — and identifies the screen by its page slug, rather
159 + // than on `load-{$hook_suffix}` (ADR-065). `admin_init` fires *after*
160 + // `admin_menu`, measured on the rig, so the badge is still painted on
161 + // the request that opens the screen exactly as before.
162 + add_action( 'admin_init', array( $this, 'mark_mcp_seen' ) );
115 163 add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
116 164 add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
165 + add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) );
117 166 add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) );
118 167
119 168 // $this->container->get( SetupWizard::class )->init();
120 169
@@ -126,8 +175,10 @@
126 175 add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
127 176 add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
128 177 add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
129 178 add_filter( 'betterdocs_admin_menu', array( $this, 'quick_setup_menu' ), 10, 1 );
179 + // Runs last so it also orders items Pro/add-ons append through this filter.
180 + add_filter( 'betterdocs_admin_menu', array( $this, 'order_admin_menu' ), 999, 1 );
130 181
131 182 /**
132 183 * Remove Comments Column from List Table.
133 184 */
@@ -249,9 +300,11 @@
249 300 if ( betterdocs()->is_pro_active() ) {
250 301 $plugins = Helper::get_plugins();
251 302 $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php'];
252 303
253 - if ( isset( $plugin_data['Version'] ) && version_compare( $plugin_data['Version'], '2.5.0', '>=' ) ) {
304 + // Require the paired Pro release: the Analytics UI is version-coupled to
305 + // Pro's advanced modules, so an older Pro renders a broken/partial panel.
306 + if ( isset( $plugin_data['Version'] ) && version_compare( $plugin_data['Version'], '4.0.0', '>=' ) ) {
254 307 return;
255 308 }
256 309
257 310 betterdocs()->views->get( 'admin/notices/compatibility', array( 'version' => $plugin_data['Version'] ) );
@@ -430,8 +483,26 @@
430 483 )
431 484 );
432 485 }
433 486
487 + // BetterDocs' own admin screens. Promotional notices are limited to these
488 + // (WP.org guideline 11), never the whole of wp-admin.
489 + $betterdocs_screens = array(
490 + 'toplevel_page_betterdocs-dashboard',
491 + 'admin_page_betterdocs-admin',
492 + 'betterdocs_page_betterdocs-admin',
493 + 'betterdocs_page_betterdocs-settings',
494 + 'betterdocs_page_betterdocs-faq',
495 + 'betterdocs_page_betterdocs-analytics',
496 + 'betterdocs_page_betterdocs-glossaries',
497 + 'betterdocs_page_betterdocs-ai-chatbot',
498 + 'betterdocs_page_betterdocs-api-docs',
499 + 'betterdocs_page_betterdocs-doc-categories',
500 + 'betterdocs_page_betterdocs-doc-tags',
501 + 'edit-doc_category',
502 + 'edit-doc_tag',
503 + );
504 +
434 505 $summer_campaign_message = '<div class="betterdocs-summer-notice-body"><p style="margin-top: 0; margin-bottom: 0;">🏖️ <strong>Summer Savings:</strong> Build AI-powered Knowledge Bases & FAQs to cut support tickets and improve user experience – now <strong>up to $100 OFF!</strong></p></div>';
435 506 $_summer_campaign_notice = array(
436 507 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
437 508 'html' => $summer_campaign_message,
@@ -446,10 +517,11 @@
446 517 ),
447 518 'maybe_later' => array(
448 519 'label' => __( 'I’ll Grab It Later', 'betterdocs' ),
449 520 'attributes' => array(
450 - 'target' => '_blank',
451 - 'data-later' => true,
521 + 'target' => '_blank',
522 + // A promo dismissal is permanent: no 15-day re-show.
523 + 'data-dismiss' => true,
452 524 'class' => 'dismiss-btn',
453 525 ),
454 526 ),
455 527 ),
@@ -461,11 +533,11 @@
461 533 array(
462 534 'start' => $notices->time(),
463 535 'recurrence' => false,
464 536 'dismissible' => true,
465 - 'refresh' => BETTERDOCS_VERSION,
466 537 'expire' => strtotime( '11:59:59pm June 25, 2026' ),
467 538 'display_if' => ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ),
539 + 'screens' => $betterdocs_screens,
468 540 )
469 541 );
470 542
471 543 self::$cache_bank->create_account( $notices );
@@ -471,20 +543,9 @@
471 543 self::$cache_bank->create_account( $notices );
472 544 self::$cache_bank->calculate_deposits( $notices );
473 545 if ( method_exists( self::$cache_bank, 'clear_notices_in_' ) ) {
474 546 self::$cache_bank->clear_notices_in_(
475 - array(
476 - 'toplevel_page_betterdocs-dashboard',
477 - 'admin_page_betterdocs-admin',
478 - 'betterdocs_page_betterdocs-admin',
479 - 'betterdocs_page_betterdocs-settings',
480 - 'betterdocs_page_betterdocs-faq',
481 - 'betterdocs_page_betterdocs-analytics',
482 - 'betterdocs_page_betterdocs-glossaries',
483 - 'betterdocs_page_betterdocs-ai-chatbot',
484 - 'edit-doc_category',
485 - 'edit-doc_tag',
486 - ),
547 + $betterdocs_screens,
487 548 $notices,
488 549 true
489 550 );
490 551 }
@@ -489,31 +550,93 @@
489 550 );
490 551 }
491 552 }
492 553
554 + /**
555 + * Resolve the admin dark-mode preference.
556 + *
557 + * The mode switcher stores the choice in a client cookie (no DB write, shared
558 + * across every admin screen). Fall back to the legacy
559 + * `betterdocs_settings['dark_mode']` value for installs that set it before this
560 + * change and haven't toggled since.
561 + *
562 + * @return bool
563 + */
564 + public function is_dark_mode() {
565 + if ( isset( $_COOKIE['betterdocs_admin_dark_mode'] ) ) {
566 + return '1' === $_COOKIE['betterdocs_admin_dark_mode']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
567 + }
568 +
569 + $saved = get_option( 'betterdocs_settings', array() );
570 + return ! empty( $saved['dark_mode'] );
571 + }
572 +
573 + /**
574 + * Whether the knowledge base is genuinely empty (no doc categories and no
575 + * non-trash docs). Localized to the admin so the All Docs panel can render a
576 + * skeleton shaped like the "No Category Found" empty card on first paint —
577 + * instead of a category/docs skeleton it would immediately replace — without
578 + * waiting for the REST fetch to reveal the count.
579 + *
580 + * @return bool
581 + */
582 + public function kb_is_empty() {
583 + $cats = wp_count_terms( array( 'taxonomy' => 'doc_category', 'hide_empty' => false ) );
584 + $cats = is_wp_error( $cats ) ? 0 : (int) $cats;
585 + if ( $cats > 0 ) {
586 + return false;
587 + }
588 +
589 + $counts = (array) wp_count_posts( 'docs' );
590 + $total = 0;
591 + foreach ( array( 'publish', 'future', 'draft', 'pending', 'private' ) as $status ) {
592 + $total += isset( $counts[ $status ] ) ? (int) $counts[ $status ] : 0;
593 + }
594 +
595 + return 0 === $total;
596 + }
597 +
493 598 public function body_classes( $classes ) {
494 - $saved_settings = get_option( 'betterdocs_settings', false );
495 - $dark_mode = isset( $saved_settings['dark_mode'] ) ? $saved_settings['dark_mode'] : false;
496 - $dark_mode = ! empty( $dark_mode ) ? boolval( $dark_mode ) : false;
599 + $dark_mode = $this->is_dark_mode();
497 600 $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', str_replace( 'admin_page_', '', get_current_screen()->id ) ) ) : '';
498 - $registered_screens = array(
601 + /**
602 + * Filter the list of (prefix-stripped) screen ids that receive the
603 + * `betterdocs-admin` body class (and dark-mode class). Pro/add-ons can
604 + * register their own React admin pages, e.g. the Knowledge Base page.
605 + *
606 + * @param string[] $registered_screens Screen ids with the page prefix removed.
607 + */
608 + $registered_screens = apply_filters( 'betterdocs_admin_screen_slugs', array(
499 609 'betterdocs-settings',
500 610 'betterdocs-admin',
501 611 'betterdocs-dashboard',
502 612 'betterdocs-analytics',
613 + 'betterdocs-content-iq',
503 614 'betterdocs-glossaries',
504 615 'betterdocs-faq',
616 + 'betterdocs-doc-categories',
617 + 'betterdocs-doc-tags',
505 618 'edit-doc_category',
506 619 'edit-doc_tag',
507 620 'edit-knowledge_base',
508 621 'betterdocs-ai-chatbot',
509 - );
622 + 'betterdocs-api-docs',
623 + // Without this the MCP screen never receives `betterdocs-admin`, and
624 + // the design tokens' dark-mode overrides — which are declared on
625 + // `.betterdocs-admin.betterdocs-dark-mode` — can never apply there:
626 + // the switcher in the header flips the cookie and the page stays
627 + // light. @since 4.9.0
628 + 'betterdocs-mcp',
629 + ) );
510 630
511 631 if ( in_array( $current_screen_id, $registered_screens ) ) {
512 632 $classes .= ' betterdocs-admin ';
513 633 }
514 634
515 - if ( true === $dark_mode && in_array( $current_screen_id, $registered_screens ) ) {
635 + // Dark mode also applies on the Quick Setup wizard, whose self-scoped chrome
636 + // keys off `.betterdocs_page_betterdocs-setup.betterdocs-dark-mode`.
637 + $dark_screens = array_merge( $registered_screens, array( 'betterdocs-setup' ) );
638 + if ( $dark_mode && in_array( $current_screen_id, $dark_screens, true ) ) {
516 639 $classes .= ' betterdocs-dark-mode ';
517 640 }
518 641
519 642 return $classes;
@@ -648,9 +771,48 @@
648 771 * @return void
649 772 * @since 1.0.0
650 773 */
651 774 public function scripts( $hook ) {
652 - if ( ( 'edit.php' === $hook ) && get_post_type() == 'docs' ) {
775 + // Classic-UI screens that should offer a "Switch to BetterDocs UI"
776 + // button: All Docs, FAQ list, FAQ groups, Product FAQ groups,
777 + // Doc Categories, Doc Tags. Maps each to the React admin page to
778 + // return to; $switch_args carries extra query args (e.g. the FAQ
779 + // Builder tab) appended to the React page URL.
780 + $switch_page = '';
781 + $switch_args = array();
782 + if ( 'edit.php' === $hook && 'docs' === get_post_type() ) {
783 + $switch_page = 'betterdocs-admin';
784 + } elseif ( 'edit.php' === $hook && 'betterdocs_faq' === get_post_type() ) {
785 + $switch_page = 'betterdocs-faq';
786 + } elseif ( 'edit-tags.php' === $hook ) {
787 + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
788 + $taxonomy = $screen && ! empty( $screen->taxonomy )
789 + ? $screen->taxonomy
790 + : ( isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
791 + if ( 'betterdocs_faq_category' === $taxonomy ) {
792 + $switch_page = 'betterdocs-faq';
793 + } elseif ( 'betterdocs_product_faq_category' === $taxonomy ) {
794 + // Product FAQ groups live on the FAQ Builder's WooCommerce tab.
795 + $switch_page = 'betterdocs-faq';
796 + $switch_args = array( 'faq_tab' => 'woocommerce' );
797 + } elseif ( 'doc_category' === $taxonomy ) {
798 + $switch_page = 'betterdocs-doc-categories';
799 + } elseif ( 'doc_tag' === $taxonomy ) {
800 + $switch_page = 'betterdocs-doc-tags';
801 + }
802 + }
803 +
804 + /**
805 + * Allow Pro/add-ons to map their own classic-UI taxonomy screens to a
806 + * React admin page for the "Switch to BetterDocs UI" button — e.g. the
807 + * Knowledge Base taxonomy, which only exists when Pro is active.
808 + *
809 + * @param string $switch_page React page slug, or '' for no switcher.
810 + * @param string $hook Current admin page hook.
811 + */
812 + $switch_page = apply_filters( 'betterdocs_classic_switch_page', $switch_page, $hook );
813 +
814 + if ( $switch_page ) {
653 815 $this->assets->enqueue(
654 816 'betterdocs-switcher',
655 817 'admin/js/switcher.js',
656 818 array(
@@ -662,8 +824,13 @@
662 824 'betterdocs-switcher',
663 825 'betterdocsSwitcher',
664 826 array(
665 827 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
828 + 'page' => $switch_page,
829 + 'url' => add_query_arg(
830 + array_merge( array( 'page' => $switch_page ), $switch_args ),
831 + admin_url( 'admin.php' )
832 + ),
666 833 'site_address' => get_bloginfo( 'url' ),
667 834 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
668 835 'betterdocs_pro_version' => betterdocs()->pro_version(),
669 836 )
@@ -681,10 +848,9 @@
681 848 wp_enqueue_media(); // load early to fix problems with media upload issues on settings for WordPress 6.0.9
682 849 $this->assets->register( 'betterdocs-admin', 'admin/js/dashboard.js' );
683 850
684 851 $saved_settings = get_option( 'betterdocs_settings', false );
685 - $dark_mode = $saved_settings['dark_mode'] ?? false;
686 - $dark_mode = ! empty( $dark_mode ) && boolval( $dark_mode );
852 + $dark_mode = $this->is_dark_mode();
687 853 $this->assets->localize(
688 854 'betterdocs-admin',
689 855 'betterdocs_admin',
690 856 array(
@@ -694,8 +860,9 @@
694 860 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- pagination read from URL.
695 861 'per_page_id' => 'edit_doc_category_per_page',
696 862 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
697 863 'dark_mode' => $dark_mode,
864 + 'kb_is_empty' => $this->kb_is_empty(),
698 865 'text' => __( 'Copied!', 'betterdocs' ),
699 866 'test_report' => __( 'Test Report!', 'betterdocs' ),
700 867 'sending' => __( 'Sending...', 'betterdocs' ),
701 868 'dir_url' => BETTERDOCS_ABSURL,
@@ -701,12 +868,17 @@
701 868 'dir_url' => BETTERDOCS_ABSURL,
702 869 'rest_url' => esc_url_raw( rest_url() ),
703 870 'free_version' => betterdocs()->version,
704 871 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
872 + 'ai_sample_docs' => array(
873 + 'enabled' => (bool) betterdocs()->settings->get( 'enable_ai_sample_docs', true ),
874 + 'rest_base' => esc_url_raw( get_rest_url( null, '/betterdocs/v1/sample-docs' ) ),
875 + ),
705 876 'nonce' => wp_create_nonce( 'wp_rest' ),
706 877 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
707 878 'count_all_docs' => array_sum( (array) wp_count_posts( 'docs' ) ),
708 879 'count_all_faq' => array_sum( (array) wp_count_posts( 'betterdocs_faq' ) ),
880 + 'faq_order' => get_option( 'betterdocs_faq_order', 'default' ),
709 881 'count_new_docs' => $this->get_not_synced_docs_count(),
710 882 'admin_url' => admin_url(),
711 883 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
712 884 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
@@ -714,13 +886,34 @@
714 886 'dashboard_mode' => get_option( 'dashboard_mode' ),
715 887 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
716 888 'betterdocs_pro_version' => betterdocs()->pro_version(),
717 889 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
718 - 'disabled_embed_model_option' => get_option( 'disabled_embed_model_option' ),
719 890 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
891 + 'api_docs_teaser' => betterdocs()->show_api_docs_teaser(),
892 + 'glossaries_teaser' => betterdocs()->show_glossary_teaser(),
893 + 'glossaries_needs_pro_update' => betterdocs()->glossaries_needs_pro_update(),
894 + 'glossaries_min_pro_version' => betterdocs()->glossaries_min_pro_version(),
895 + 'glossaries_pro_update_url' => self_admin_url( 'plugins.php' ),
896 + 'content_intelligence_teaser' => betterdocs()->show_content_intelligence_teaser(),
897 + 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
720 898 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
721 899 'current_admin_language' => Helper::get_current_admin_language(),
722 900 'is_multilingual' => Helper::is_multilingual_active(),
901 + 'languages' => Helper::get_admin_languages(),
902 + /**
903 + * MCP page bootstrap. `abilities_api_available` decides whether the
904 + * page offers a connection at all: without the Abilities API there
905 + * is no tool catalog, so an AI client would connect and find
906 + * nothing. `enabled` is only the initial paint — the toggle owns
907 + * the value from then on.
908 + *
909 + * @since 4.9.0
910 + */
911 + 'mcp' => array(
912 + 'abilities_api_available' => function_exists( 'wp_register_ability' ),
913 + 'enabled' => (bool) betterdocs()->settings->get( 'enable_mcp', false ),
914 + 'rest' => 'betterdocs/v1',
915 + ),
723 916 )
724 917 );
725 918
726 919 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
@@ -736,8 +929,16 @@
736 929 // FAQ Builder Related Localization
737 930 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/css/faq.css' );
738 931 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/js/faq.js' );
739 932
933 + // Load the classic editor (TinyMCE + QuickTags) so the FAQ rich-text editor can mount via wp.editor.initialize().
934 + if ( function_exists( 'wp_enqueue_editor' ) ) {
935 + wp_enqueue_editor();
936 + }
937 + if ( function_exists( 'wp_enqueue_media' ) ) {
938 + wp_enqueue_media();
939 + }
940 +
740 941 // removing emoji support
741 942 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
742 943 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
743 944
@@ -743,10 +944,11 @@
743 944
744 945 // Get settings and remove unnecessary keys
745 946 $betterdocs_settings = get_option( 'betterdocs_settings', false );
746 947 if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
747 - unset( $betterdocs_settings['ai_autowrite_api_key'] );
748 - unset( $betterdocs_settings['ai_chatbot_api_key'] );
948 + foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
949 + unset( $betterdocs_settings[ $sensitive_key ] );
950 + }
749 951 }
750 952
751 953 betterdocs()->assets->localize(
752 954 'betterdocs-admin-faq',
@@ -759,24 +961,10 @@
759 961 'betterdocs_settings' => $betterdocs_settings,
760 962 )
761 963 );
762 964
763 - // Glossaries Related Localization
764 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
765 -
766 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
767 -
768 - betterdocs()->assets->localize(
769 - 'betterdocs-admin-glossaries',
770 - 'betterdocsGlossary',
771 - array(
772 - 'dir_url' => BETTERDOCS_ABSURL,
773 - 'rest_url' => esc_url_raw( rest_url() ),
774 - 'free_version' => betterdocs()->version,
775 - 'nonce' => wp_create_nonce( 'wp_rest' ),
776 - 'betterdocs_settings' => $betterdocs_settings,
777 - )
778 - );
965 + // Glossaries is Pro — Pro's Core\Glossaries::enqueue() owns that bundle
966 + // and both of its localized objects (`betterdocs`, `betterdocsGlossary`).
779 967 }
780 968
781 969 /**
782 970 * All admin pages header
@@ -833,8 +1021,13 @@
833 1021
834 1022 // Always register both UI endpoints
835 1023 $this->register_modern_ui_fallback();
836 1024
1025 + // The one-time MCP discovery badge (ADR-063). Decided once, here, and
1026 + // remembered for `mcp_badge_styles()`: the pill's markup and the pill's
1027 + // stylesheet have to be printed on the same requests as each other.
1028 + $this->mcp_badge = self::should_flag_mcp();
1029 +
837 1030 foreach ( $this->menu_list() as $key => $value ) {
838 1031 if ( 'betterdocs' === $key ) {
839 1032 $callable = 'add_menu_page';
840 1033 $value = wp_parse_args( $value, $default_args );
@@ -863,10 +1056,203 @@
863 1056 }
864 1057 ++$_menu_position;
865 1058 }
866 1059 }
1060 +
1061 + $this->paint_mcp_badge();
867 1062 }
868 1063
1064 + /**
1065 + * Append the discovery badge to the registered menu titles.
1066 + *
1067 + * **After** registration, editing `$menu` / `$submenu` in place — the same
1068 + * shape `add_custom_classes_to_menu_items()` uses — and never by passing a
1069 + * decorated title to `add_menu_page()`. That distinction is not cosmetic:
1070 + * core stores `sanitize_title( $menu_title )` as `$admin_page_hooks[ $slug ]`
1071 + * (`wp-admin/includes/plugin.php:1397`) and builds every child page's hook
1072 + * suffix from it (`get_plugin_page_hookname()`), so markup in the parent's
1073 + * title renames `betterdocs_page_betterdocs-mcp` — and the MCP screen, whose
1074 + * asset enqueue is keyed on that exact suffix, silently loads no React
1075 + * bundle at all. Measured: the first visit came back 102,513 bytes with no
1076 + * `dashboard.js`, against 489,272 bytes once the badge had cleared.
1077 + *
1078 + * Titles are only ever appended to, never rebuilt: the menu list is filtered
1079 + * (`betterdocs_admin_menu`), so whatever a filter put in a title survives.
1080 + *
1081 + * @return void
1082 + * @since 4.9.0
1083 + */
1084 + private function paint_mcp_badge() {
1085 + if ( ! $this->mcp_badge ) {
1086 + return;
1087 + }
1088 +
1089 + global $menu, $submenu;
1090 +
1091 + if ( is_array( $menu ) ) {
1092 + foreach ( $menu as &$item ) {
1093 + if ( isset( $item[2] ) && $this->slug === $item[2] ) {
1094 + $item[0] .= self::mcp_parent_bubble();
1095 + break;
1096 + }
1097 + }
1098 + unset( $item );
1099 + }
1100 +
1101 + if ( isset( $submenu[ $this->slug ] ) && is_array( $submenu[ $this->slug ] ) ) {
1102 + foreach ( $submenu[ $this->slug ] as &$sub_item ) {
1103 + if ( isset( $sub_item[2] ) && 'betterdocs-content-iq' === $sub_item[2] ) {
1104 + $sub_item[0] .= self::mcp_submenu_pill();
1105 + break;
1106 + }
1107 + }
1108 + unset( $sub_item );
1109 + }
1110 + }
1111 +
1112 + /**
1113 + * Whether the current user should see the one-time MCP discovery badge.
1114 + *
1115 + * True only for a user who can actually reach the screen and has never
1116 + * opened it. The capability is the one the MCP menu item is already
1117 + * registered with (`manage_options`) rather than a second, re-derived rule —
1118 + * so the badge can never advertise a page its reader cannot open.
1119 + *
1120 + * @return bool
1121 + * @since 4.9.0
1122 + */
1123 + public static function should_flag_mcp() {
1124 + if ( ! current_user_can( 'manage_options' ) ) {
1125 + return false;
1126 + }
1127 +
1128 + $user_id = get_current_user_id();
1129 +
1130 + if ( ! $user_id ) {
1131 + return false;
1132 + }
1133 +
1134 + // `get_user_meta( …, true )` answers '' for a key that is not there, and
1135 + // the value written is always `time()` — so an empty string is the only
1136 + // shape "never opened" takes.
1137 + return '' === get_user_meta( $user_id, self::MCP_SEEN_META, true );
1138 + }
1139 +
1140 + /**
1141 + * WordPress' own update-count bubble, for the BetterDocs parent menu item.
1142 + *
1143 + * Core's markup on purpose: the red bubble, its position and its dark-mode
1144 + * colours are already in `wp-admin`'s stylesheet, so this needs no CSS of
1145 + * ours and cannot drift from the Plugins/Updates bubbles beside it.
1146 + *
1147 + * @return string
1148 + * @since 4.9.0
1149 + */
1150 + private static function mcp_parent_bubble() {
1151 + return ' <span class="update-plugins count-1"><span class="update-count">1</span></span>';
1152 + }
1153 +
1154 + /**
1155 + * The green "New" pill for the MCP submenu item.
1156 + *
1157 + * Core has no submenu-badge markup, so this one is ours — styled by
1158 + * `mcp_badge_styles()`.
1159 + *
1160 + * @return string
1161 + * @since 4.9.0
1162 + */
1163 + private static function mcp_submenu_pill() {
1164 + return ' <span class="bd-menu-pill">' . esc_html__( 'New', 'betterdocs' ) . '</span>';
1165 + }
1166 +
1167 + /**
1168 + * Whether this request is the MCP screen being opened by someone who can
1169 + * open it.
1170 + *
1171 + * The whole of the clearing decision, in one static so it can be pinned by
1172 + * a test. It reads the **page slug** rather than an admin hook suffix
1173 + * because the suffix is derived state: core builds it from
1174 + * `sanitize_title()` of the *parent* menu title (`get_plugin_page_hookname()`),
1175 + * that title is filtered (`betterdocs_admin_menu`), and the parent slug is
1176 + * spelled two ways in this class already. `?page=betterdocs-mcp` is the one
1177 + * thing that identifies this screen on every install (ADR-065).
1178 + *
1179 + * The capability is `manage_options`, the same one the MCP menu item is
1180 + * registered with and the same one {@see self::should_flag_mcp()} gates on:
1181 + * nothing may be written for a user who cannot reach the page.
1182 + *
1183 + * @return bool
1184 + * @since 4.9.0
1185 + */
1186 + public static function is_mcp_screen_request() {
1187 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection; see mark_mcp_seen().
1188 + $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
1189 +
1190 + if ( 'betterdocs-content-iq' !== $page ) {
1191 + return false;
1192 + }
1193 +
1194 + return current_user_can( 'manage_options' );
1195 + }
1196 +
1197 + /**
1198 + * Record that this user has now seen the MCP screen.
1199 + *
1200 + * Bound to `admin_init` — which fires for every admin request — and gated on
1201 + * the page slug, rather than to `load-{$hook_suffix}` for the one suffix
1202 + * `add_submenu_page()` happened to return. `admin_menu` has already run by
1203 + * the time `admin_init` fires (measured), so the badge is still painted on
1204 + * *this* request and is gone from the next admin page — that is expected and
1205 + * correct. Do not add JavaScript to strip it mid-request.
1206 + *
1207 + * **No nonce, on purpose.** A nonce protects a state change an attacker
1208 + * could make a logged-in administrator perform unknowingly. The only state
1209 + * here is "this administrator has now been shown the MCP screen once", it is
1210 + * written for the current user alone, it holds no attacker-chosen value, and
1211 + * the worst a forged request can achieve is hiding a discovery badge from
1212 + * the person it was drawn for. A nonce on a plain page view would also have
1213 + * to survive the menu link, which carries none.
1214 + *
1215 + * @return void
1216 + * @since 4.9.0
1217 + */
1218 + public function mark_mcp_seen() {
1219 + if ( ! self::is_mcp_screen_request() ) {
1220 + return;
1221 + }
1222 +
1223 + $user_id = get_current_user_id();
1224 +
1225 + if ( ! $user_id ) {
1226 + return;
1227 + }
1228 +
1229 + update_user_meta( $user_id, self::MCP_SEEN_META, time() );
1230 + }
1231 +
1232 + /**
1233 + * The handful of declarations the "New" pill needs, inline, and only while
1234 + * it is being shown.
1235 + *
1236 + * The menu is read from the WordPress Dashboard, and `styles()` above
1237 + * early-returns on non-BetterDocs screens — so `admin/css/dashboard.css` is
1238 + * not loaded where this pill is seen. Loading the whole BetterDocs admin
1239 + * stylesheet globally, or shipping a stylesheet file for nine declarations,
1240 + * both cost far more than printing them here. The accent is written out
1241 + * rather than taken from `--base-color-700`: that token lives in
1242 + * `dashboard.css`, which is exactly the file that is not loaded here.
1243 + *
1244 + * @return void
1245 + * @since 4.9.0
1246 + */
1247 + public function mcp_badge_styles() {
1248 + if ( ! $this->mcp_badge ) {
1249 + return;
1250 + }
1251 +
1252 + echo '<style id="betterdocs-menu-pill">#adminmenu .bd-menu-pill{display:inline-block;background:#00b884;color:#fff;font-size:10px;text-transform:uppercase;line-height:1.6;padding:1px 6px;margin-left:6px;border-radius:9px;}</style>' . "\n";
1253 + }
1254 +
869 1255 private function register_modern_ui_fallback() {
870 1256 // Add the submenu with valid parent slug
871 1257 add_submenu_page(
872 1258 'betterdocs', // Valid parent slug
@@ -965,15 +1351,19 @@
965 1351 'post-new.php?post_type=docs'
966 1352 ),
967 1353 'categories' => $this->normalize_menu(
968 1354 __( 'Categories', 'betterdocs' ),
969 - 'edit-tags.php?taxonomy=doc_category&post_type=docs',
970 - 'manage_doc_terms'
1355 + 'betterdocs-doc-categories',
1356 + 'manage_doc_terms',
1357 + array( $this, 'output' ),
1358 + $parent_slug
971 1359 ),
972 1360 'tags' => $this->normalize_menu(
973 1361 __( 'Tags', 'betterdocs' ),
974 - 'edit-tags.php?taxonomy=doc_tag&post_type=docs',
975 - 'manage_doc_terms'
1362 + 'betterdocs-doc-tags',
1363 + 'manage_doc_terms',
1364 + array( $this, 'output' ),
1365 + $parent_slug
976 1366 ),
977 1367 'settings' => $this->normalize_menu(
978 1368 __( 'Settings', 'betterdocs' ),
979 1369 'betterdocs-settings',
@@ -983,8 +1373,18 @@
983 1373 'output',
984 1374 ),
985 1375 $parent_slug
986 1376 ),
1377 + 'mcp' => $this->normalize_menu(
1378 + __( 'MCP', 'betterdocs' ),
1379 + 'betterdocs-mcp',
1380 + 'manage_options',
1381 + array(
1382 + $this,
1383 + 'output',
1384 + ),
1385 + $parent_slug
1386 + ),
987 1387 'analytics' => $this->normalize_menu(
988 1388 __( 'Analytics', 'betterdocs' ),
989 1389 'betterdocs-analytics',
990 1390 'read_docs_analytics',
@@ -993,8 +1393,18 @@
993 1393 'output',
994 1394 ),
995 1395 $parent_slug
996 1396 ),
1397 + 'content_intelligence' => $this->normalize_menu(
1398 + __( 'Content IQ', 'betterdocs' ),
1399 + 'betterdocs-content-iq',
1400 + 'read_docs_analytics',
1401 + array(
1402 + $this,
1403 + 'output',
1404 + ),
1405 + $parent_slug
1406 + ),
997 1407 'faq' => $this->normalize_menu(
998 1408 __( 'FAQ Builder', 'betterdocs' ),
999 1409 'betterdocs-faq',
1000 1410 'read_faq_builder',
@@ -1005,13 +1415,25 @@
1005 1415 $parent_slug
1006 1416 ),
1007 1417 );
1008 1418
1009 - if ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) {
1419 + // Content Intelligence ships in Pro, which overwrites that same key in place.
1420 + // Unlike API Docs it has to sit directly after Analytics, and `menus()` walks
1421 + // this array in insertion order — so the slot is declared inside the literal
1422 + // above and only withdrawn here. Appending it after the fact, api_docs-style,
1423 + // would park it at the bottom of the menu.
1424 + if ( ! ( betterdocs()->show_content_intelligence_teaser() || betterdocs()->has_content_intelligence() ) ) {
1425 + unset( $betterdocs_admin_pages['content_intelligence'] );
1426 + }
1427 +
1428 + // Glossaries is Pro. Reserve this same 'glossaries' slot for Free's locked
1429 + // teaser so the item keeps this position; once Pro is active the real
1430 + // screen overwrites the key in place (same pattern as API Docs below).
1431 + if ( betterdocs()->show_glossary_teaser() || ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) ) {
1010 1432 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1011 1433 __( 'Glossaries', 'betterdocs' ),
1012 1434 'betterdocs-glossaries',
1013 - 'read_docs_analytics',
1435 + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1014 1436 array(
1015 1437 $this,
1016 1438 'output',
1017 1439 ),
@@ -1018,8 +1440,24 @@
1018 1440 $parent_slug
1019 1441 );
1020 1442 }
1021 1443
1444 + // API Docs ships in Pro, which overwrites this same key in place — declaring
1445 + // the slot here is what keeps the item in this position. Without Pro it
1446 + // holds Free's locked teaser instead.
1447 + if ( betterdocs()->show_api_docs_teaser() || betterdocs()->has_api_docs() ) {
1448 + $betterdocs_admin_pages['api_docs'] = $this->normalize_menu(
1449 + __( 'API Docs', 'betterdocs' ),
1450 + 'betterdocs-api-docs',
1451 + apply_filters( 'betterdocs_api_ref_capability', 'manage_options' ),
1452 + array(
1453 + $this,
1454 + 'output',
1455 + ),
1456 + $parent_slug
1457 + );
1458 + }
1459 +
1022 1460 if ( ! betterdocs()->is_chatbot_active() ) {
1023 1461 $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1024 1462 __( 'AI Chatbot', 'betterdocs' ),
1025 1463 'betterdocs-ai-chatbot',
@@ -1034,8 +1472,70 @@
1034 1472
1035 1473 return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1036 1474 }
1037 1475
1476 + /**
1477 + * Put the BetterDocs submenu in a deliberate order.
1478 + *
1479 + * Order used to be an accident of *when* each item was added: Free declares
1480 + * most of them inline, and reserves in-place slots for `glossaries` /
1481 + * `api_docs` so Pro can overwrite the key without moving it. Anything added
1482 + * purely through this filter, though, could only land at the end — which is
1483 + * why Multiple KB (Pro, priority 100) and AI Chatbot Logs sat after
1484 + * everything else regardless of where they belong.
1485 + *
1486 + * Sorting here, at priority 999, fixes that for every source at once: Free's
1487 + * own entries, Pro's, and any add-on's. Knowledge Base now follows Tags (it
1488 + * is the third taxonomy-ish thing, so it belongs with Categories and Tags
1489 + * rather than past Analytics), and API Docs follows Knowledge Base.
1490 + *
1491 + * Keys not listed keep their relative order and are appended, so an add-on
1492 + * that registers something unknown to this list is never dropped.
1493 + *
1494 + * @param array $pages Menu pages keyed by slug id.
1495 + * @return array
1496 + */
1497 + public function order_admin_menu( $pages ) {
1498 + if ( ! is_array( $pages ) ) {
1499 + return $pages;
1500 + }
1501 +
1502 + $order = array(
1503 + 'betterdocs',
1504 + 'dashboard',
1505 + 'all_docs',
1506 + 'add_new',
1507 + 'categories',
1508 + 'tags',
1509 + 'multiple_kb',
1510 + 'api_docs',
1511 + 'settings',
1512 + 'mcp',
1513 + 'analytics',
1514 + // Content IQ reads as a second Analytics screen, so it has to stay
1515 + // pinned directly behind it. Without this entry it would fall into
1516 + // the unknown-key bucket below and be appended to the bottom of the
1517 + // menu — the exact placement the menu literal avoids by declaring
1518 + // the slot inline rather than filtering it in api_docs-style.
1519 + 'content_intelligence',
1520 + 'faq',
1521 + 'glossaries',
1522 + 'ai_chatbot',
1523 + 'ai_chatbot_logs',
1524 + );
1525 +
1526 + $ordered = array();
1527 + foreach ( $order as $key ) {
1528 + if ( array_key_exists( $key, $pages ) ) {
1529 + $ordered[ $key ] = $pages[ $key ];
1530 + unset( $pages[ $key ] );
1531 + }
1532 + }
1533 +
1534 + // `$pages` now holds only unknown keys, still in their original order.
1535 + return array_merge( $ordered, $pages );
1536 + }
1537 +
1038 1538 public function add_custom_classes_to_menu_items() {
1039 1539 global $menu, $submenu;
1040 1540
1041 1541 $menu_items = array(
@@ -1041,15 +1541,18 @@
1041 1541 $menu_items = array(
1042 1542 'betterdocs' => 'betterdocs',
1043 1543 'betterdocs_page_all_docs' => 'betterdocs-all-docs',
1044 1544 'betterdocs_page_add_new' => 'betterdocs-add-new',
1045 - 'edit-tags.php?taxonomy=doc_category&post_type=docs' => 'betterdocs-categories',
1046 - 'edit-tags.php?taxonomy=doc_tag&post_type=docs' => 'betterdocs-tags',
1545 + 'betterdocs-doc-categories' => 'betterdocs-categories',
1546 + 'betterdocs-doc-tags' => 'betterdocs-tags',
1047 1547 'betterdocs-settings' => 'betterdocs-settings',
1548 + 'betterdocs-mcp' => 'betterdocs-mcp',
1048 1549 'betterdocs-analytics' => 'betterdocs-analytics',
1550 + 'betterdocs-content-iq' => 'betterdocs-content-iq',
1049 1551 'betterdocs-faq' => 'betterdocs-faq',
1050 1552 'betterdocs-glossaries' => 'betterdocs-glossaries',
1051 1553 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1554 + 'betterdocs-api-docs' => 'betterdocs-api-docs',
1052 1555 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1053 1556 );
1054 1557
1055 1558 foreach ( $menu as &$item ) {