PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.2
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 3.5.1 All 200 releases
← All changes | includes/Core/Admin.php +362 -2 4.8.24.9.2 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 /**
@@ -121,10 +155,17 @@
121 155 // add_action( 'admin_init', [$this, 'notices'], 9 );
122 156 add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
123 157
124 158 add_action( 'admin_menu', array( $this, 'menus' ) );
159 + // The badge's clear runs on `admin_init` — a hook that fires for every
160 + // admin request — and identifies the screen by its page slug, rather
161 + // than on `load-{$hook_suffix}` (ADR-065). `admin_init` fires *after*
162 + // `admin_menu`, measured on the rig, so the badge is still painted on
163 + // the request that opens the screen exactly as before.
164 + add_action( 'admin_init', array( $this, 'mark_mcp_seen' ) );
125 165 add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
126 166 add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
167 + add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) );
127 168 add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) );
128 169
129 170 // $this->container->get( SetupWizard::class )->init();
130 171
@@ -136,8 +177,10 @@
136 177 add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
137 178 add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
138 179 add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
139 180 add_filter( 'betterdocs_admin_menu', array( $this, 'quick_setup_menu' ), 10, 1 );
181 + // Runs last so it also orders items Pro/add-ons append through this filter.
182 + add_filter( 'betterdocs_admin_menu', array( $this, 'order_admin_menu' ), 999, 1 );
140 183
141 184 /**
142 185 * Remove Comments Column from List Table.
143 186 */
@@ -563,8 +606,9 @@
563 606 'betterdocs-settings',
564 607 'betterdocs-admin',
565 608 'betterdocs-dashboard',
566 609 'betterdocs-analytics',
610 + 'betterdocs-content-iq',
567 611 'betterdocs-glossaries',
568 612 'betterdocs-faq',
569 613 'betterdocs-doc-categories',
570 614 'betterdocs-doc-tags',
@@ -572,8 +616,14 @@
572 616 'edit-doc_tag',
573 617 'edit-knowledge_base',
574 618 'betterdocs-ai-chatbot',
575 619 'betterdocs-api-docs',
620 + // Without this the MCP screen never receives `betterdocs-admin`, and
621 + // the design tokens' dark-mode overrides — which are declared on
622 + // `.betterdocs-admin.betterdocs-dark-mode` — can never apply there:
623 + // the switcher in the header flips the cookie and the page stays
624 + // light. @since 4.9.0
625 + 'betterdocs-mcp',
576 626 ) );
577 627
578 628 if ( in_array( $current_screen_id, $registered_screens ) ) {
579 629 $classes .= ' betterdocs-admin ';
@@ -835,13 +885,29 @@
835 885 'betterdocs_pro_version' => betterdocs()->pro_version(),
836 886 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
837 887 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
838 888 'api_docs_teaser' => betterdocs()->show_api_docs_teaser(),
889 + 'glossaries_teaser' => betterdocs()->show_glossary_teaser(),
890 + 'content_intelligence_teaser' => betterdocs()->show_content_intelligence_teaser(),
839 891 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
840 892 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
841 893 'current_admin_language' => Helper::get_current_admin_language(),
842 894 'is_multilingual' => Helper::is_multilingual_active(),
843 895 'languages' => Helper::get_admin_languages(),
896 + /**
897 + * MCP page bootstrap. `abilities_api_available` decides whether the
898 + * page offers a connection at all: without the Abilities API there
899 + * is no tool catalog, so an AI client would connect and find
900 + * nothing. `enabled` is only the initial paint — the toggle owns
901 + * the value from then on.
902 + *
903 + * @since 4.9.0
904 + */
905 + 'mcp' => array(
906 + 'abilities_api_available' => function_exists( 'wp_register_ability' ),
907 + 'enabled' => (bool) betterdocs()->settings->get( 'enable_mcp', false ),
908 + 'rest' => 'betterdocs/v1',
909 + ),
844 910 )
845 911 );
846 912
847 913 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
@@ -963,8 +1029,13 @@
963 1029
964 1030 // Always register both UI endpoints
965 1031 $this->register_modern_ui_fallback();
966 1032
1033 + // The one-time MCP discovery badge (ADR-063). Decided once, here, and
1034 + // remembered for `mcp_badge_styles()`: the pill's markup and the pill's
1035 + // stylesheet have to be printed on the same requests as each other.
1036 + $this->mcp_badge = self::should_flag_mcp();
1037 +
967 1038 foreach ( $this->menu_list() as $key => $value ) {
968 1039 if ( 'betterdocs' === $key ) {
969 1040 $callable = 'add_menu_page';
970 1041 $value = wp_parse_args( $value, $default_args );
@@ -993,10 +1064,203 @@
993 1064 }
994 1065 ++$_menu_position;
995 1066 }
996 1067 }
1068 +
1069 + $this->paint_mcp_badge();
997 1070 }
998 1071
1072 + /**
1073 + * Append the discovery badge to the registered menu titles.
1074 + *
1075 + * **After** registration, editing `$menu` / `$submenu` in place — the same
1076 + * shape `add_custom_classes_to_menu_items()` uses — and never by passing a
1077 + * decorated title to `add_menu_page()`. That distinction is not cosmetic:
1078 + * core stores `sanitize_title( $menu_title )` as `$admin_page_hooks[ $slug ]`
1079 + * (`wp-admin/includes/plugin.php:1397`) and builds every child page's hook
1080 + * suffix from it (`get_plugin_page_hookname()`), so markup in the parent's
1081 + * title renames `betterdocs_page_betterdocs-mcp` — and the MCP screen, whose
1082 + * asset enqueue is keyed on that exact suffix, silently loads no React
1083 + * bundle at all. Measured: the first visit came back 102,513 bytes with no
1084 + * `dashboard.js`, against 489,272 bytes once the badge had cleared.
1085 + *
1086 + * Titles are only ever appended to, never rebuilt: the menu list is filtered
1087 + * (`betterdocs_admin_menu`), so whatever a filter put in a title survives.
1088 + *
1089 + * @return void
1090 + * @since 4.9.0
1091 + */
1092 + private function paint_mcp_badge() {
1093 + if ( ! $this->mcp_badge ) {
1094 + return;
1095 + }
1096 +
1097 + global $menu, $submenu;
1098 +
1099 + if ( is_array( $menu ) ) {
1100 + foreach ( $menu as &$item ) {
1101 + if ( isset( $item[2] ) && $this->slug === $item[2] ) {
1102 + $item[0] .= self::mcp_parent_bubble();
1103 + break;
1104 + }
1105 + }
1106 + unset( $item );
1107 + }
1108 +
1109 + if ( isset( $submenu[ $this->slug ] ) && is_array( $submenu[ $this->slug ] ) ) {
1110 + foreach ( $submenu[ $this->slug ] as &$sub_item ) {
1111 + if ( isset( $sub_item[2] ) && 'betterdocs-content-iq' === $sub_item[2] ) {
1112 + $sub_item[0] .= self::mcp_submenu_pill();
1113 + break;
1114 + }
1115 + }
1116 + unset( $sub_item );
1117 + }
1118 + }
1119 +
1120 + /**
1121 + * Whether the current user should see the one-time MCP discovery badge.
1122 + *
1123 + * True only for a user who can actually reach the screen and has never
1124 + * opened it. The capability is the one the MCP menu item is already
1125 + * registered with (`manage_options`) rather than a second, re-derived rule —
1126 + * so the badge can never advertise a page its reader cannot open.
1127 + *
1128 + * @return bool
1129 + * @since 4.9.0
1130 + */
1131 + public static function should_flag_mcp() {
1132 + if ( ! current_user_can( 'manage_options' ) ) {
1133 + return false;
1134 + }
1135 +
1136 + $user_id = get_current_user_id();
1137 +
1138 + if ( ! $user_id ) {
1139 + return false;
1140 + }
1141 +
1142 + // `get_user_meta( …, true )` answers '' for a key that is not there, and
1143 + // the value written is always `time()` — so an empty string is the only
1144 + // shape "never opened" takes.
1145 + return '' === get_user_meta( $user_id, self::MCP_SEEN_META, true );
1146 + }
1147 +
1148 + /**
1149 + * WordPress' own update-count bubble, for the BetterDocs parent menu item.
1150 + *
1151 + * Core's markup on purpose: the red bubble, its position and its dark-mode
1152 + * colours are already in `wp-admin`'s stylesheet, so this needs no CSS of
1153 + * ours and cannot drift from the Plugins/Updates bubbles beside it.
1154 + *
1155 + * @return string
1156 + * @since 4.9.0
1157 + */
1158 + private static function mcp_parent_bubble() {
1159 + return ' <span class="update-plugins count-1"><span class="update-count">1</span></span>';
1160 + }
1161 +
1162 + /**
1163 + * The green "New" pill for the MCP submenu item.
1164 + *
1165 + * Core has no submenu-badge markup, so this one is ours — styled by
1166 + * `mcp_badge_styles()`.
1167 + *
1168 + * @return string
1169 + * @since 4.9.0
1170 + */
1171 + private static function mcp_submenu_pill() {
1172 + return ' <span class="bd-menu-pill">' . esc_html__( 'New', 'betterdocs' ) . '</span>';
1173 + }
1174 +
1175 + /**
1176 + * Whether this request is the MCP screen being opened by someone who can
1177 + * open it.
1178 + *
1179 + * The whole of the clearing decision, in one static so it can be pinned by
1180 + * a test. It reads the **page slug** rather than an admin hook suffix
1181 + * because the suffix is derived state: core builds it from
1182 + * `sanitize_title()` of the *parent* menu title (`get_plugin_page_hookname()`),
1183 + * that title is filtered (`betterdocs_admin_menu`), and the parent slug is
1184 + * spelled two ways in this class already. `?page=betterdocs-mcp` is the one
1185 + * thing that identifies this screen on every install (ADR-065).
1186 + *
1187 + * The capability is `manage_options`, the same one the MCP menu item is
1188 + * registered with and the same one {@see self::should_flag_mcp()} gates on:
1189 + * nothing may be written for a user who cannot reach the page.
1190 + *
1191 + * @return bool
1192 + * @since 4.9.0
1193 + */
1194 + public static function is_mcp_screen_request() {
1195 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection; see mark_mcp_seen().
1196 + $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
1197 +
1198 + if ( 'betterdocs-content-iq' !== $page ) {
1199 + return false;
1200 + }
1201 +
1202 + return current_user_can( 'manage_options' );
1203 + }
1204 +
1205 + /**
1206 + * Record that this user has now seen the MCP screen.
1207 + *
1208 + * Bound to `admin_init` — which fires for every admin request — and gated on
1209 + * the page slug, rather than to `load-{$hook_suffix}` for the one suffix
1210 + * `add_submenu_page()` happened to return. `admin_menu` has already run by
1211 + * the time `admin_init` fires (measured), so the badge is still painted on
1212 + * *this* request and is gone from the next admin page — that is expected and
1213 + * correct. Do not add JavaScript to strip it mid-request.
1214 + *
1215 + * **No nonce, on purpose.** A nonce protects a state change an attacker
1216 + * could make a logged-in administrator perform unknowingly. The only state
1217 + * here is "this administrator has now been shown the MCP screen once", it is
1218 + * written for the current user alone, it holds no attacker-chosen value, and
1219 + * the worst a forged request can achieve is hiding a discovery badge from
1220 + * the person it was drawn for. A nonce on a plain page view would also have
1221 + * to survive the menu link, which carries none.
1222 + *
1223 + * @return void
1224 + * @since 4.9.0
1225 + */
1226 + public function mark_mcp_seen() {
1227 + if ( ! self::is_mcp_screen_request() ) {
1228 + return;
1229 + }
1230 +
1231 + $user_id = get_current_user_id();
1232 +
1233 + if ( ! $user_id ) {
1234 + return;
1235 + }
1236 +
1237 + update_user_meta( $user_id, self::MCP_SEEN_META, time() );
1238 + }
1239 +
1240 + /**
1241 + * The handful of declarations the "New" pill needs, inline, and only while
1242 + * it is being shown.
1243 + *
1244 + * The menu is read from the WordPress Dashboard, and `styles()` above
1245 + * early-returns on non-BetterDocs screens — so `admin/css/dashboard.css` is
1246 + * not loaded where this pill is seen. Loading the whole BetterDocs admin
1247 + * stylesheet globally, or shipping a stylesheet file for nine declarations,
1248 + * both cost far more than printing them here. The accent is written out
1249 + * rather than taken from `--base-color-700`: that token lives in
1250 + * `dashboard.css`, which is exactly the file that is not loaded here.
1251 + *
1252 + * @return void
1253 + * @since 4.9.0
1254 + */
1255 + public function mcp_badge_styles() {
1256 + if ( ! $this->mcp_badge ) {
1257 + return;
1258 + }
1259 +
1260 + 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";
1261 + }
1262 +
999 1263 private function register_modern_ui_fallback() {
1000 1264 // Add the submenu with valid parent slug
1001 1265 add_submenu_page(
1002 1266 'betterdocs', // Valid parent slug
@@ -1117,8 +1381,18 @@
1117 1381 'output',
1118 1382 ),
1119 1383 $parent_slug
1120 1384 ),
1385 + 'mcp' => $this->normalize_menu(
1386 + __( 'MCP', 'betterdocs' ),
1387 + 'betterdocs-mcp',
1388 + 'manage_options',
1389 + array(
1390 + $this,
1391 + 'output',
1392 + ),
1393 + $parent_slug
1394 + ),
1121 1395 'analytics' => $this->normalize_menu(
1122 1396 __( 'Analytics', 'betterdocs' ),
1123 1397 'betterdocs-analytics',
1124 1398 'read_docs_analytics',
@@ -1127,8 +1401,18 @@
1127 1401 'output',
1128 1402 ),
1129 1403 $parent_slug
1130 1404 ),
1405 + 'content_intelligence' => $this->normalize_menu(
1406 + __( 'Content IQ', 'betterdocs' ),
1407 + 'betterdocs-content-iq',
1408 + 'read_docs_analytics',
1409 + array(
1410 + $this,
1411 + 'output',
1412 + ),
1413 + $parent_slug
1414 + ),
1131 1415 'faq' => $this->normalize_menu(
1132 1416 __( 'FAQ Builder', 'betterdocs' ),
1133 1417 'betterdocs-faq',
1134 1418 'read_faq_builder',
@@ -1139,13 +1423,25 @@
1139 1423 $parent_slug
1140 1424 ),
1141 1425 );
1142 1426
1143 - if ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) {
1427 + // Content Intelligence ships in Pro, which overwrites that same key in place.
1428 + // Unlike API Docs it has to sit directly after Analytics, and `menus()` walks
1429 + // this array in insertion order — so the slot is declared inside the literal
1430 + // above and only withdrawn here. Appending it after the fact, api_docs-style,
1431 + // would park it at the bottom of the menu.
1432 + if ( ! ( betterdocs()->show_content_intelligence_teaser() || betterdocs()->has_content_intelligence() ) ) {
1433 + unset( $betterdocs_admin_pages['content_intelligence'] );
1434 + }
1435 +
1436 + // Glossaries is Pro. Reserve this same 'glossaries' slot for Free's locked
1437 + // teaser so the item keeps this position; once Pro is active the real
1438 + // screen overwrites the key in place (same pattern as API Docs below).
1439 + if ( betterdocs()->show_glossary_teaser() || ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) ) {
1144 1440 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1145 1441 __( 'Glossaries', 'betterdocs' ),
1146 1442 'betterdocs-glossaries',
1147 - 'read_docs_analytics',
1443 + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1148 1444 array(
1149 1445 $this,
1150 1446 'output',
1151 1447 ),
@@ -1184,8 +1480,70 @@
1184 1480
1185 1481 return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1186 1482 }
1187 1483
1484 + /**
1485 + * Put the BetterDocs submenu in a deliberate order.
1486 + *
1487 + * Order used to be an accident of *when* each item was added: Free declares
1488 + * most of them inline, and reserves in-place slots for `glossaries` /
1489 + * `api_docs` so Pro can overwrite the key without moving it. Anything added
1490 + * purely through this filter, though, could only land at the end — which is
1491 + * why Multiple KB (Pro, priority 100) and AI Chatbot Logs sat after
1492 + * everything else regardless of where they belong.
1493 + *
1494 + * Sorting here, at priority 999, fixes that for every source at once: Free's
1495 + * own entries, Pro's, and any add-on's. Knowledge Base now follows Tags (it
1496 + * is the third taxonomy-ish thing, so it belongs with Categories and Tags
1497 + * rather than past Analytics), and API Docs follows Knowledge Base.
1498 + *
1499 + * Keys not listed keep their relative order and are appended, so an add-on
1500 + * that registers something unknown to this list is never dropped.
1501 + *
1502 + * @param array $pages Menu pages keyed by slug id.
1503 + * @return array
1504 + */
1505 + public function order_admin_menu( $pages ) {
1506 + if ( ! is_array( $pages ) ) {
1507 + return $pages;
1508 + }
1509 +
1510 + $order = array(
1511 + 'betterdocs',
1512 + 'dashboard',
1513 + 'all_docs',
1514 + 'add_new',
1515 + 'categories',
1516 + 'tags',
1517 + 'multiple_kb',
1518 + 'api_docs',
1519 + 'settings',
1520 + 'mcp',
1521 + 'analytics',
1522 + // Content IQ reads as a second Analytics screen, so it has to stay
1523 + // pinned directly behind it. Without this entry it would fall into
1524 + // the unknown-key bucket below and be appended to the bottom of the
1525 + // menu — the exact placement the menu literal avoids by declaring
1526 + // the slot inline rather than filtering it in api_docs-style.
1527 + 'content_intelligence',
1528 + 'faq',
1529 + 'glossaries',
1530 + 'ai_chatbot',
1531 + 'ai_chatbot_logs',
1532 + );
1533 +
1534 + $ordered = array();
1535 + foreach ( $order as $key ) {
1536 + if ( array_key_exists( $key, $pages ) ) {
1537 + $ordered[ $key ] = $pages[ $key ];
1538 + unset( $pages[ $key ] );
1539 + }
1540 + }
1541 +
1542 + // `$pages` now holds only unknown keys, still in their original order.
1543 + return array_merge( $ordered, $pages );
1544 + }
1545 +
1188 1546 public function add_custom_classes_to_menu_items() {
1189 1547 global $menu, $submenu;
1190 1548
1191 1549 $menu_items = array(
@@ -1194,9 +1552,11 @@
1194 1552 'betterdocs_page_add_new' => 'betterdocs-add-new',
1195 1553 'betterdocs-doc-categories' => 'betterdocs-categories',
1196 1554 'betterdocs-doc-tags' => 'betterdocs-tags',
1197 1555 'betterdocs-settings' => 'betterdocs-settings',
1556 + 'betterdocs-mcp' => 'betterdocs-mcp',
1198 1557 'betterdocs-analytics' => 'betterdocs-analytics',
1558 + 'betterdocs-content-iq' => 'betterdocs-content-iq',
1199 1559 'betterdocs-faq' => 'betterdocs-faq',
1200 1560 'betterdocs-glossaries' => 'betterdocs-glossaries',
1201 1561 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1202 1562 'betterdocs-api-docs' => 'betterdocs-api-docs',