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 +412 -40 4.7.0 → 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
@@ -121,10 +153,17 @@
121 153 // add_action( 'admin_init', [$this, 'notices'], 9 );
122 154 add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
123 155
124 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' ) );
125 163 add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
126 164 add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
165 + add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) );
127 166 add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) );
128 167
129 168 // $this->container->get( SetupWizard::class )->init();
130 169
@@ -136,8 +175,10 @@
136 175 add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
137 176 add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
138 177 add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
139 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 );
140 181
141 182 /**
142 183 * Remove Comments Column from List Table.
143 184 */
@@ -442,8 +483,26 @@
442 483 )
443 484 );
444 485 }
445 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 +
446 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>';
447 506 $_summer_campaign_notice = array(
448 507 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
449 508 'html' => $summer_campaign_message,
@@ -458,10 +517,11 @@
458 517 ),
459 518 'maybe_later' => array(
460 519 'label' => __( 'I’ll Grab It Later', 'betterdocs' ),
461 520 'attributes' => array(
462 - 'target' => '_blank',
463 - 'data-later' => true,
521 + 'target' => '_blank',
522 + // A promo dismissal is permanent: no 15-day re-show.
523 + 'data-dismiss' => true,
464 524 'class' => 'dismiss-btn',
465 525 ),
466 526 ),
467 527 ),
@@ -473,11 +533,11 @@
473 533 array(
474 534 'start' => $notices->time(),
475 535 'recurrence' => false,
476 536 'dismissible' => true,
477 - 'refresh' => BETTERDOCS_VERSION,
478 537 'expire' => strtotime( '11:59:59pm June 25, 2026' ),
479 538 'display_if' => ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ),
539 + 'screens' => $betterdocs_screens,
480 540 )
481 541 );
482 542
483 543 self::$cache_bank->create_account( $notices );
@@ -483,22 +543,9 @@
483 543 self::$cache_bank->create_account( $notices );
484 544 self::$cache_bank->calculate_deposits( $notices );
485 545 if ( method_exists( self::$cache_bank, 'clear_notices_in_' ) ) {
486 546 self::$cache_bank->clear_notices_in_(
487 - array(
488 - 'toplevel_page_betterdocs-dashboard',
489 - 'admin_page_betterdocs-admin',
490 - 'betterdocs_page_betterdocs-admin',
491 - 'betterdocs_page_betterdocs-settings',
492 - 'betterdocs_page_betterdocs-faq',
493 - 'betterdocs_page_betterdocs-analytics',
494 - 'betterdocs_page_betterdocs-glossaries',
495 - 'betterdocs_page_betterdocs-ai-chatbot',
496 - 'betterdocs_page_betterdocs-doc-categories',
497 - 'betterdocs_page_betterdocs-doc-tags',
498 - 'edit-doc_category',
499 - 'edit-doc_tag',
500 - ),
547 + $betterdocs_screens,
501 548 $notices,
502 549 true
503 550 );
504 551 }
@@ -562,8 +609,9 @@
562 609 'betterdocs-settings',
563 610 'betterdocs-admin',
564 611 'betterdocs-dashboard',
565 612 'betterdocs-analytics',
613 + 'betterdocs-content-iq',
566 614 'betterdocs-glossaries',
567 615 'betterdocs-faq',
568 616 'betterdocs-doc-categories',
569 617 'betterdocs-doc-tags',
@@ -570,8 +618,15 @@
570 618 'edit-doc_category',
571 619 'edit-doc_tag',
572 620 'edit-knowledge_base',
573 621 'betterdocs-ai-chatbot',
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',
574 629 ) );
575 630
576 631 if ( in_array( $current_screen_id, $registered_screens ) ) {
577 632 $classes .= ' betterdocs-admin ';
@@ -831,15 +886,34 @@
831 886 'dashboard_mode' => get_option( 'dashboard_mode' ),
832 887 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
833 888 'betterdocs_pro_version' => betterdocs()->pro_version(),
834 889 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
835 - 'disabled_embed_model_option' => get_option( 'disabled_embed_model_option' ),
836 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(),
837 897 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
838 898 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
839 899 'current_admin_language' => Helper::get_current_admin_language(),
840 900 'is_multilingual' => Helper::is_multilingual_active(),
841 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 + ),
842 916 )
843 917 );
844 918
845 919 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
@@ -870,10 +944,11 @@
870 944
871 945 // Get settings and remove unnecessary keys
872 946 $betterdocs_settings = get_option( 'betterdocs_settings', false );
873 947 if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
874 - unset( $betterdocs_settings['ai_autowrite_api_key'] );
875 - unset( $betterdocs_settings['ai_chatbot_api_key'] );
948 + foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
949 + unset( $betterdocs_settings[ $sensitive_key ] );
950 + }
876 951 }
877 952
878 953 betterdocs()->assets->localize(
879 954 'betterdocs-admin-faq',
@@ -886,24 +961,10 @@
886 961 'betterdocs_settings' => $betterdocs_settings,
887 962 )
888 963 );
889 964
890 - // Glossaries Related Localization
891 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
892 -
893 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
894 -
895 - betterdocs()->assets->localize(
896 - 'betterdocs-admin-glossaries',
897 - 'betterdocsGlossary',
898 - array(
899 - 'dir_url' => BETTERDOCS_ABSURL,
900 - 'rest_url' => esc_url_raw( rest_url() ),
901 - 'free_version' => betterdocs()->version,
902 - 'nonce' => wp_create_nonce( 'wp_rest' ),
903 - 'betterdocs_settings' => $betterdocs_settings,
904 - )
905 - );
965 + // Glossaries is Pro — Pro's Core\Glossaries::enqueue() owns that bundle
966 + // and both of its localized objects (`betterdocs`, `betterdocsGlossary`).
906 967 }
907 968
908 969 /**
909 970 * All admin pages header
@@ -960,8 +1021,13 @@
960 1021
961 1022 // Always register both UI endpoints
962 1023 $this->register_modern_ui_fallback();
963 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 +
964 1030 foreach ( $this->menu_list() as $key => $value ) {
965 1031 if ( 'betterdocs' === $key ) {
966 1032 $callable = 'add_menu_page';
967 1033 $value = wp_parse_args( $value, $default_args );
@@ -990,10 +1056,203 @@
990 1056 }
991 1057 ++$_menu_position;
992 1058 }
993 1059 }
1060 +
1061 + $this->paint_mcp_badge();
994 1062 }
995 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 +
996 1255 private function register_modern_ui_fallback() {
997 1256 // Add the submenu with valid parent slug
998 1257 add_submenu_page(
999 1258 'betterdocs', // Valid parent slug
@@ -1114,8 +1373,18 @@
1114 1373 'output',
1115 1374 ),
1116 1375 $parent_slug
1117 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 + ),
1118 1387 'analytics' => $this->normalize_menu(
1119 1388 __( 'Analytics', 'betterdocs' ),
1120 1389 'betterdocs-analytics',
1121 1390 'read_docs_analytics',
@@ -1124,8 +1393,18 @@
1124 1393 'output',
1125 1394 ),
1126 1395 $parent_slug
1127 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 + ),
1128 1407 'faq' => $this->normalize_menu(
1129 1408 __( 'FAQ Builder', 'betterdocs' ),
1130 1409 'betterdocs-faq',
1131 1410 'read_faq_builder',
@@ -1136,13 +1415,25 @@
1136 1415 $parent_slug
1137 1416 ),
1138 1417 );
1139 1418
1140 - 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 ) ) {
1141 1432 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1142 1433 __( 'Glossaries', 'betterdocs' ),
1143 1434 'betterdocs-glossaries',
1144 - 'read_docs_analytics',
1435 + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1145 1436 array(
1146 1437 $this,
1147 1438 'output',
1148 1439 ),
@@ -1149,8 +1440,24 @@
1149 1440 $parent_slug
1150 1441 );
1151 1442 }
1152 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 +
1153 1460 if ( ! betterdocs()->is_chatbot_active() ) {
1154 1461 $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1155 1462 __( 'AI Chatbot', 'betterdocs' ),
1156 1463 'betterdocs-ai-chatbot',
@@ -1165,8 +1472,70 @@
1165 1472
1166 1473 return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1167 1474 }
1168 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 +
1169 1538 public function add_custom_classes_to_menu_items() {
1170 1539 global $menu, $submenu;
1171 1540
1172 1541 $menu_items = array(
@@ -1175,12 +1544,15 @@
1175 1544 'betterdocs_page_add_new' => 'betterdocs-add-new',
1176 1545 'betterdocs-doc-categories' => 'betterdocs-categories',
1177 1546 'betterdocs-doc-tags' => 'betterdocs-tags',
1178 1547 'betterdocs-settings' => 'betterdocs-settings',
1548 + 'betterdocs-mcp' => 'betterdocs-mcp',
1179 1549 'betterdocs-analytics' => 'betterdocs-analytics',
1550 + 'betterdocs-content-iq' => 'betterdocs-content-iq',
1180 1551 'betterdocs-faq' => 'betterdocs-faq',
1181 1552 'betterdocs-glossaries' => 'betterdocs-glossaries',
1182 1553 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1554 + 'betterdocs-api-docs' => 'betterdocs-api-docs',
1183 1555 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1184 1556 );
1185 1557
1186 1558 foreach ( $menu as &$item ) {