PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
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 3.4.2 3.5.0 All 201 releases
← All changes | includes/Core/Admin.php +403 -24 4.6.1 → 4.9.3 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'] ) );
@@ -480,8 +533,9 @@
480 533 'betterdocs_page_betterdocs-faq',
481 534 'betterdocs_page_betterdocs-analytics',
482 535 'betterdocs_page_betterdocs-glossaries',
483 536 'betterdocs_page_betterdocs-ai-chatbot',
537 + 'betterdocs_page_betterdocs-api-docs',
484 538 'betterdocs_page_betterdocs-doc-categories',
485 539 'betterdocs_page_betterdocs-doc-tags',
486 540 'edit-doc_category',
487 541 'edit-doc_tag',
@@ -550,8 +604,9 @@
550 604 'betterdocs-settings',
551 605 'betterdocs-admin',
552 606 'betterdocs-dashboard',
553 607 'betterdocs-analytics',
608 + 'betterdocs-content-iq',
554 609 'betterdocs-glossaries',
555 610 'betterdocs-faq',
556 611 'betterdocs-doc-categories',
557 612 'betterdocs-doc-tags',
@@ -558,8 +613,15 @@
558 613 'edit-doc_category',
559 614 'edit-doc_tag',
560 615 'edit-knowledge_base',
561 616 'betterdocs-ai-chatbot',
617 + 'betterdocs-api-docs',
618 + // Without this the MCP screen never receives `betterdocs-admin`, and
619 + // the design tokens' dark-mode overrides — which are declared on
620 + // `.betterdocs-admin.betterdocs-dark-mode` — can never apply there:
621 + // the switcher in the header flips the cookie and the page stays
622 + // light. @since 4.9.0
623 + 'betterdocs-mcp',
562 624 ) );
563 625
564 626 if ( in_array( $current_screen_id, $registered_screens ) ) {
565 627 $classes .= ' betterdocs-admin ';
@@ -819,15 +881,34 @@
819 881 'dashboard_mode' => get_option( 'dashboard_mode' ),
820 882 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
821 883 'betterdocs_pro_version' => betterdocs()->pro_version(),
822 884 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
823 - 'disabled_embed_model_option' => get_option( 'disabled_embed_model_option' ),
824 885 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
886 + 'api_docs_teaser' => betterdocs()->show_api_docs_teaser(),
887 + 'glossaries_teaser' => betterdocs()->show_glossary_teaser(),
888 + 'glossaries_needs_pro_update' => betterdocs()->glossaries_needs_pro_update(),
889 + 'glossaries_min_pro_version' => betterdocs()->glossaries_min_pro_version(),
890 + 'glossaries_pro_update_url' => self_admin_url( 'plugins.php' ),
891 + 'content_intelligence_teaser' => betterdocs()->show_content_intelligence_teaser(),
825 892 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
826 893 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
827 894 'current_admin_language' => Helper::get_current_admin_language(),
828 895 'is_multilingual' => Helper::is_multilingual_active(),
829 896 'languages' => Helper::get_admin_languages(),
897 + /**
898 + * MCP page bootstrap. `abilities_api_available` decides whether the
899 + * page offers a connection at all: without the Abilities API there
900 + * is no tool catalog, so an AI client would connect and find
901 + * nothing. `enabled` is only the initial paint — the toggle owns
902 + * the value from then on.
903 + *
904 + * @since 4.9.0
905 + */
906 + 'mcp' => array(
907 + 'abilities_api_available' => function_exists( 'wp_register_ability' ),
908 + 'enabled' => (bool) betterdocs()->settings->get( 'enable_mcp', false ),
909 + 'rest' => 'betterdocs/v1',
910 + ),
830 911 )
831 912 );
832 913
833 914 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
@@ -858,10 +939,11 @@
858 939
859 940 // Get settings and remove unnecessary keys
860 941 $betterdocs_settings = get_option( 'betterdocs_settings', false );
861 942 if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
862 - unset( $betterdocs_settings['ai_autowrite_api_key'] );
863 - unset( $betterdocs_settings['ai_chatbot_api_key'] );
943 + foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
944 + unset( $betterdocs_settings[ $sensitive_key ] );
945 + }
864 946 }
865 947
866 948 betterdocs()->assets->localize(
867 949 'betterdocs-admin-faq',
@@ -874,24 +956,10 @@
874 956 'betterdocs_settings' => $betterdocs_settings,
875 957 )
876 958 );
877 959
878 - // Glossaries Related Localization
879 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
880 -
881 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
882 -
883 - betterdocs()->assets->localize(
884 - 'betterdocs-admin-glossaries',
885 - 'betterdocsGlossary',
886 - array(
887 - 'dir_url' => BETTERDOCS_ABSURL,
888 - 'rest_url' => esc_url_raw( rest_url() ),
889 - 'free_version' => betterdocs()->version,
890 - 'nonce' => wp_create_nonce( 'wp_rest' ),
891 - 'betterdocs_settings' => $betterdocs_settings,
892 - )
893 - );
960 + // Glossaries is Pro — Pro's Core\Glossaries::enqueue() owns that bundle
961 + // and both of its localized objects (`betterdocs`, `betterdocsGlossary`).
894 962 }
895 963
896 964 /**
897 965 * All admin pages header
@@ -948,8 +1016,13 @@
948 1016
949 1017 // Always register both UI endpoints
950 1018 $this->register_modern_ui_fallback();
951 1019
1020 + // The one-time MCP discovery badge (ADR-063). Decided once, here, and
1021 + // remembered for `mcp_badge_styles()`: the pill's markup and the pill's
1022 + // stylesheet have to be printed on the same requests as each other.
1023 + $this->mcp_badge = self::should_flag_mcp();
1024 +
952 1025 foreach ( $this->menu_list() as $key => $value ) {
953 1026 if ( 'betterdocs' === $key ) {
954 1027 $callable = 'add_menu_page';
955 1028 $value = wp_parse_args( $value, $default_args );
@@ -978,10 +1051,203 @@
978 1051 }
979 1052 ++$_menu_position;
980 1053 }
981 1054 }
1055 +
1056 + $this->paint_mcp_badge();
982 1057 }
983 1058
1059 + /**
1060 + * Append the discovery badge to the registered menu titles.
1061 + *
1062 + * **After** registration, editing `$menu` / `$submenu` in place — the same
1063 + * shape `add_custom_classes_to_menu_items()` uses — and never by passing a
1064 + * decorated title to `add_menu_page()`. That distinction is not cosmetic:
1065 + * core stores `sanitize_title( $menu_title )` as `$admin_page_hooks[ $slug ]`
1066 + * (`wp-admin/includes/plugin.php:1397`) and builds every child page's hook
1067 + * suffix from it (`get_plugin_page_hookname()`), so markup in the parent's
1068 + * title renames `betterdocs_page_betterdocs-mcp` — and the MCP screen, whose
1069 + * asset enqueue is keyed on that exact suffix, silently loads no React
1070 + * bundle at all. Measured: the first visit came back 102,513 bytes with no
1071 + * `dashboard.js`, against 489,272 bytes once the badge had cleared.
1072 + *
1073 + * Titles are only ever appended to, never rebuilt: the menu list is filtered
1074 + * (`betterdocs_admin_menu`), so whatever a filter put in a title survives.
1075 + *
1076 + * @return void
1077 + * @since 4.9.0
1078 + */
1079 + private function paint_mcp_badge() {
1080 + if ( ! $this->mcp_badge ) {
1081 + return;
1082 + }
1083 +
1084 + global $menu, $submenu;
1085 +
1086 + if ( is_array( $menu ) ) {
1087 + foreach ( $menu as &$item ) {
1088 + if ( isset( $item[2] ) && $this->slug === $item[2] ) {
1089 + $item[0] .= self::mcp_parent_bubble();
1090 + break;
1091 + }
1092 + }
1093 + unset( $item );
1094 + }
1095 +
1096 + if ( isset( $submenu[ $this->slug ] ) && is_array( $submenu[ $this->slug ] ) ) {
1097 + foreach ( $submenu[ $this->slug ] as &$sub_item ) {
1098 + if ( isset( $sub_item[2] ) && 'betterdocs-content-iq' === $sub_item[2] ) {
1099 + $sub_item[0] .= self::mcp_submenu_pill();
1100 + break;
1101 + }
1102 + }
1103 + unset( $sub_item );
1104 + }
1105 + }
1106 +
1107 + /**
1108 + * Whether the current user should see the one-time MCP discovery badge.
1109 + *
1110 + * True only for a user who can actually reach the screen and has never
1111 + * opened it. The capability is the one the MCP menu item is already
1112 + * registered with (`manage_options`) rather than a second, re-derived rule —
1113 + * so the badge can never advertise a page its reader cannot open.
1114 + *
1115 + * @return bool
1116 + * @since 4.9.0
1117 + */
1118 + public static function should_flag_mcp() {
1119 + if ( ! current_user_can( 'manage_options' ) ) {
1120 + return false;
1121 + }
1122 +
1123 + $user_id = get_current_user_id();
1124 +
1125 + if ( ! $user_id ) {
1126 + return false;
1127 + }
1128 +
1129 + // `get_user_meta( …, true )` answers '' for a key that is not there, and
1130 + // the value written is always `time()` — so an empty string is the only
1131 + // shape "never opened" takes.
1132 + return '' === get_user_meta( $user_id, self::MCP_SEEN_META, true );
1133 + }
1134 +
1135 + /**
1136 + * WordPress' own update-count bubble, for the BetterDocs parent menu item.
1137 + *
1138 + * Core's markup on purpose: the red bubble, its position and its dark-mode
1139 + * colours are already in `wp-admin`'s stylesheet, so this needs no CSS of
1140 + * ours and cannot drift from the Plugins/Updates bubbles beside it.
1141 + *
1142 + * @return string
1143 + * @since 4.9.0
1144 + */
1145 + private static function mcp_parent_bubble() {
1146 + return ' <span class="update-plugins count-1"><span class="update-count">1</span></span>';
1147 + }
1148 +
1149 + /**
1150 + * The green "New" pill for the MCP submenu item.
1151 + *
1152 + * Core has no submenu-badge markup, so this one is ours — styled by
1153 + * `mcp_badge_styles()`.
1154 + *
1155 + * @return string
1156 + * @since 4.9.0
1157 + */
1158 + private static function mcp_submenu_pill() {
1159 + return ' <span class="bd-menu-pill">' . esc_html__( 'New', 'betterdocs' ) . '</span>';
1160 + }
1161 +
1162 + /**
1163 + * Whether this request is the MCP screen being opened by someone who can
1164 + * open it.
1165 + *
1166 + * The whole of the clearing decision, in one static so it can be pinned by
1167 + * a test. It reads the **page slug** rather than an admin hook suffix
1168 + * because the suffix is derived state: core builds it from
1169 + * `sanitize_title()` of the *parent* menu title (`get_plugin_page_hookname()`),
1170 + * that title is filtered (`betterdocs_admin_menu`), and the parent slug is
1171 + * spelled two ways in this class already. `?page=betterdocs-mcp` is the one
1172 + * thing that identifies this screen on every install (ADR-065).
1173 + *
1174 + * The capability is `manage_options`, the same one the MCP menu item is
1175 + * registered with and the same one {@see self::should_flag_mcp()} gates on:
1176 + * nothing may be written for a user who cannot reach the page.
1177 + *
1178 + * @return bool
1179 + * @since 4.9.0
1180 + */
1181 + public static function is_mcp_screen_request() {
1182 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection; see mark_mcp_seen().
1183 + $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
1184 +
1185 + if ( 'betterdocs-content-iq' !== $page ) {
1186 + return false;
1187 + }
1188 +
1189 + return current_user_can( 'manage_options' );
1190 + }
1191 +
1192 + /**
1193 + * Record that this user has now seen the MCP screen.
1194 + *
1195 + * Bound to `admin_init` — which fires for every admin request — and gated on
1196 + * the page slug, rather than to `load-{$hook_suffix}` for the one suffix
1197 + * `add_submenu_page()` happened to return. `admin_menu` has already run by
1198 + * the time `admin_init` fires (measured), so the badge is still painted on
1199 + * *this* request and is gone from the next admin page — that is expected and
1200 + * correct. Do not add JavaScript to strip it mid-request.
1201 + *
1202 + * **No nonce, on purpose.** A nonce protects a state change an attacker
1203 + * could make a logged-in administrator perform unknowingly. The only state
1204 + * here is "this administrator has now been shown the MCP screen once", it is
1205 + * written for the current user alone, it holds no attacker-chosen value, and
1206 + * the worst a forged request can achieve is hiding a discovery badge from
1207 + * the person it was drawn for. A nonce on a plain page view would also have
1208 + * to survive the menu link, which carries none.
1209 + *
1210 + * @return void
1211 + * @since 4.9.0
1212 + */
1213 + public function mark_mcp_seen() {
1214 + if ( ! self::is_mcp_screen_request() ) {
1215 + return;
1216 + }
1217 +
1218 + $user_id = get_current_user_id();
1219 +
1220 + if ( ! $user_id ) {
1221 + return;
1222 + }
1223 +
1224 + update_user_meta( $user_id, self::MCP_SEEN_META, time() );
1225 + }
1226 +
1227 + /**
1228 + * The handful of declarations the "New" pill needs, inline, and only while
1229 + * it is being shown.
1230 + *
1231 + * The menu is read from the WordPress Dashboard, and `styles()` above
1232 + * early-returns on non-BetterDocs screens — so `admin/css/dashboard.css` is
1233 + * not loaded where this pill is seen. Loading the whole BetterDocs admin
1234 + * stylesheet globally, or shipping a stylesheet file for nine declarations,
1235 + * both cost far more than printing them here. The accent is written out
1236 + * rather than taken from `--base-color-700`: that token lives in
1237 + * `dashboard.css`, which is exactly the file that is not loaded here.
1238 + *
1239 + * @return void
1240 + * @since 4.9.0
1241 + */
1242 + public function mcp_badge_styles() {
1243 + if ( ! $this->mcp_badge ) {
1244 + return;
1245 + }
1246 +
1247 + 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";
1248 + }
1249 +
984 1250 private function register_modern_ui_fallback() {
985 1251 // Add the submenu with valid parent slug
986 1252 add_submenu_page(
987 1253 'betterdocs', // Valid parent slug
@@ -1102,8 +1368,18 @@
1102 1368 'output',
1103 1369 ),
1104 1370 $parent_slug
1105 1371 ),
1372 + 'mcp' => $this->normalize_menu(
1373 + __( 'MCP', 'betterdocs' ),
1374 + 'betterdocs-mcp',
1375 + 'manage_options',
1376 + array(
1377 + $this,
1378 + 'output',
1379 + ),
1380 + $parent_slug
1381 + ),
1106 1382 'analytics' => $this->normalize_menu(
1107 1383 __( 'Analytics', 'betterdocs' ),
1108 1384 'betterdocs-analytics',
1109 1385 'read_docs_analytics',
@@ -1112,8 +1388,18 @@
1112 1388 'output',
1113 1389 ),
1114 1390 $parent_slug
1115 1391 ),
1392 + 'content_intelligence' => $this->normalize_menu(
1393 + __( 'Content IQ', 'betterdocs' ),
1394 + 'betterdocs-content-iq',
1395 + 'read_docs_analytics',
1396 + array(
1397 + $this,
1398 + 'output',
1399 + ),
1400 + $parent_slug
1401 + ),
1116 1402 'faq' => $this->normalize_menu(
1117 1403 __( 'FAQ Builder', 'betterdocs' ),
1118 1404 'betterdocs-faq',
1119 1405 'read_faq_builder',
@@ -1124,13 +1410,25 @@
1124 1410 $parent_slug
1125 1411 ),
1126 1412 );
1127 1413
1128 - if ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) {
1414 + // Content Intelligence ships in Pro, which overwrites that same key in place.
1415 + // Unlike API Docs it has to sit directly after Analytics, and `menus()` walks
1416 + // this array in insertion order — so the slot is declared inside the literal
1417 + // above and only withdrawn here. Appending it after the fact, api_docs-style,
1418 + // would park it at the bottom of the menu.
1419 + if ( ! ( betterdocs()->show_content_intelligence_teaser() || betterdocs()->has_content_intelligence() ) ) {
1420 + unset( $betterdocs_admin_pages['content_intelligence'] );
1421 + }
1422 +
1423 + // Glossaries is Pro. Reserve this same 'glossaries' slot for Free's locked
1424 + // teaser so the item keeps this position; once Pro is active the real
1425 + // screen overwrites the key in place (same pattern as API Docs below).
1426 + if ( betterdocs()->show_glossary_teaser() || ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) ) {
1129 1427 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1130 1428 __( 'Glossaries', 'betterdocs' ),
1131 1429 'betterdocs-glossaries',
1132 - 'read_docs_analytics',
1430 + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1133 1431 array(
1134 1432 $this,
1135 1433 'output',
1136 1434 ),
@@ -1137,8 +1435,24 @@
1137 1435 $parent_slug
1138 1436 );
1139 1437 }
1140 1438
1439 + // API Docs ships in Pro, which overwrites this same key in place — declaring
1440 + // the slot here is what keeps the item in this position. Without Pro it
1441 + // holds Free's locked teaser instead.
1442 + if ( betterdocs()->show_api_docs_teaser() || betterdocs()->has_api_docs() ) {
1443 + $betterdocs_admin_pages['api_docs'] = $this->normalize_menu(
1444 + __( 'API Docs', 'betterdocs' ),
1445 + 'betterdocs-api-docs',
1446 + apply_filters( 'betterdocs_api_ref_capability', 'manage_options' ),
1447 + array(
1448 + $this,
1449 + 'output',
1450 + ),
1451 + $parent_slug
1452 + );
1453 + }
1454 +
1141 1455 if ( ! betterdocs()->is_chatbot_active() ) {
1142 1456 $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1143 1457 __( 'AI Chatbot', 'betterdocs' ),
1144 1458 'betterdocs-ai-chatbot',
@@ -1153,8 +1467,70 @@
1153 1467
1154 1468 return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1155 1469 }
1156 1470
1471 + /**
1472 + * Put the BetterDocs submenu in a deliberate order.
1473 + *
1474 + * Order used to be an accident of *when* each item was added: Free declares
1475 + * most of them inline, and reserves in-place slots for `glossaries` /
1476 + * `api_docs` so Pro can overwrite the key without moving it. Anything added
1477 + * purely through this filter, though, could only land at the end — which is
1478 + * why Multiple KB (Pro, priority 100) and AI Chatbot Logs sat after
1479 + * everything else regardless of where they belong.
1480 + *
1481 + * Sorting here, at priority 999, fixes that for every source at once: Free's
1482 + * own entries, Pro's, and any add-on's. Knowledge Base now follows Tags (it
1483 + * is the third taxonomy-ish thing, so it belongs with Categories and Tags
1484 + * rather than past Analytics), and API Docs follows Knowledge Base.
1485 + *
1486 + * Keys not listed keep their relative order and are appended, so an add-on
1487 + * that registers something unknown to this list is never dropped.
1488 + *
1489 + * @param array $pages Menu pages keyed by slug id.
1490 + * @return array
1491 + */
1492 + public function order_admin_menu( $pages ) {
1493 + if ( ! is_array( $pages ) ) {
1494 + return $pages;
1495 + }
1496 +
1497 + $order = array(
1498 + 'betterdocs',
1499 + 'dashboard',
1500 + 'all_docs',
1501 + 'add_new',
1502 + 'categories',
1503 + 'tags',
1504 + 'multiple_kb',
1505 + 'api_docs',
1506 + 'settings',
1507 + 'mcp',
1508 + 'analytics',
1509 + // Content IQ reads as a second Analytics screen, so it has to stay
1510 + // pinned directly behind it. Without this entry it would fall into
1511 + // the unknown-key bucket below and be appended to the bottom of the
1512 + // menu — the exact placement the menu literal avoids by declaring
1513 + // the slot inline rather than filtering it in api_docs-style.
1514 + 'content_intelligence',
1515 + 'faq',
1516 + 'glossaries',
1517 + 'ai_chatbot',
1518 + 'ai_chatbot_logs',
1519 + );
1520 +
1521 + $ordered = array();
1522 + foreach ( $order as $key ) {
1523 + if ( array_key_exists( $key, $pages ) ) {
1524 + $ordered[ $key ] = $pages[ $key ];
1525 + unset( $pages[ $key ] );
1526 + }
1527 + }
1528 +
1529 + // `$pages` now holds only unknown keys, still in their original order.
1530 + return array_merge( $ordered, $pages );
1531 + }
1532 +
1157 1533 public function add_custom_classes_to_menu_items() {
1158 1534 global $menu, $submenu;
1159 1535
1160 1536 $menu_items = array(
@@ -1163,12 +1539,15 @@
1163 1539 'betterdocs_page_add_new' => 'betterdocs-add-new',
1164 1540 'betterdocs-doc-categories' => 'betterdocs-categories',
1165 1541 'betterdocs-doc-tags' => 'betterdocs-tags',
1166 1542 'betterdocs-settings' => 'betterdocs-settings',
1543 + 'betterdocs-mcp' => 'betterdocs-mcp',
1167 1544 'betterdocs-analytics' => 'betterdocs-analytics',
1545 + 'betterdocs-content-iq' => 'betterdocs-content-iq',
1168 1546 'betterdocs-faq' => 'betterdocs-faq',
1169 1547 'betterdocs-glossaries' => 'betterdocs-glossaries',
1170 1548 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1549 + 'betterdocs-api-docs' => 'betterdocs-api-docs',
1171 1550 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1172 1551 );
1173 1552
1174 1553 foreach ( $menu as &$item ) {