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 +965 -344 4.4.1 → 4.9.3 View file →
@@ -1,15 +1,20 @@
1 1 <?php
2 +namespace WPDeveloper\BetterDocs\Core;
2 3
3 -namespace WPDeveloper\BetterDocs\Core;
4 +if ( ! defined( 'ABSPATH' ) ) {
5 + exit;
6 +}
4 7
8 +
5 9 use Exception;
6 10 use PriyoMukul\WPNotice\Notices;
11 +use WPDeveloper\BetterDocs\Admin\NoticePointers;
7 12 use WPDeveloper\BetterDocs\Utils\Base;
8 13 use PriyoMukul\WPNotice\Utils\CacheBank;
9 14 use WPDeveloper\BetterDocs\Utils\Helper;
10 15 use WPDeveloper\BetterDocs\Utils\Enqueue;
11 -use WPDeveloper\BetterDocs\Utils\Insights;
16 +use WPDeveloper\BetterDocs\Insights\Insights;
12 17 use PriyoMukul\WPNotice\Utils\NoticeRemover;
13 18 use WPDeveloper\BetterDocs\Core\PluginInstaller;
14 19 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
15 20
@@ -14,13 +19,48 @@
14 19 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
15 20
16 21 class Admin extends Base {
17 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 + /**
18 57 * @var CacheBank
19 58 */
20 59 private static $cache_bank;
21 60 /**
22 61 * Admin Root Menu Slug
62 + *
23 63 * @var string
24 64 */
25 65 private $slug = 'betterdocs-dashboard';
26 66 /**
@@ -52,21 +92,22 @@
52 92 private $kbmigration;
53 93
54 94 /**
55 95 * Enqueue
96 + *
56 97 * @var Enqueue
57 98 */
58 99 private $assets;
59 100
60 101 // modules
61 - protected $installer;
102 + protected $installer;
62 103
63 104 /**
64 105 * FAQBuilder
106 + *
65 107 * @var FAQBuilder
66 108 */
67 109 private $faq_builder;
68 - private $glossaries;
69 110
70 111 public function __construct( Container $container, PostType $type, Enqueue $assets, Settings $settings, KBMigration $kbmigration ) {
71 112 $this->container = $container;
72 113 $this->assets = $assets;
@@ -73,17 +114,26 @@
73 114 $this->settings = $settings;
74 115 $this->kbmigration = $kbmigration;
75 116 $this->slug = 'betterdocs-dashboard';
76 117
77 - add_action( 'init', [ $type, 'register' ], 9 );
78 - add_action('rest_api_init', [$this, 'order_terms_in_wp_terms_admin_table']);
118 + add_action( 'init', array( $type, 'register' ), 9 );
119 + add_action( 'rest_api_init', array( $this, 'order_terms_in_wp_terms_admin_table' ) );
79 120
80 121 $type->init();
81 122 $type->admin_init();
82 123
83 124 $this->faq_builder = $this->container->get( FAQBuilder::class );
84 - $this->glossaries = $this->container->get( Glossaries::class );
85 125
126 + /**
127 + * Register usage tracking (including the daily `put_do_weekly_action` cron
128 + * handler) on every request — WP-Cron runs with is_admin() === false, so
129 + * this MUST sit above the admin guard or the cron send never fires. The
130 + * admin-only UI hooks inside Insights::init() (deactivation form, footer
131 + * scripts, plugin_action_links) are context-specific and simply never run
132 + * outside wp-admin.
133 + */
134 + $this->plugin_insights();
135 +
86 136 if ( ! is_admin() ) {
87 137 return;
88 138 }
89 139
@@ -88,41 +138,59 @@
88 138 }
89 139
90 140 $this->installer = new PluginInstaller();
91 141
92 - $this->plugin_insights();
93 - add_action( 'admin_notices', [ $this, 'compatibility_notices' ] );
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 );
94 153 // add_action( 'admin_init', [$this, 'notices'], 9 );
95 - add_filter( 'admin_init', [ $this, 'save_admin_page' ], 99 );
154 + add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
96 155
97 - add_action( 'admin_menu', [ $this, 'menus' ] );
98 - add_action( 'admin_menu', [ $this, 'reset_submenu' ] );
99 - add_action( 'admin_head', [ $this, 'add_custom_classes_to_menu_items' ] );
100 - add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, [ $this, 'insert_plugin_links' ] );
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' ) );
163 + add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
164 + add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
165 + add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) );
166 + add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) );
101 167
102 168 // $this->container->get( SetupWizard::class )->init();
103 169
104 - add_action( 'admin_enqueue_scripts', [ $this, 'styles' ] );
105 - add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] );
106 - //add_action( 'betterdocs_listing_header', [ $this, 'header' ], 10, 1 );
107 - add_action( 'admin_bar_menu', [ $this, 'toolbar_menu' ], 32 );
170 + add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
171 + add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
172 + // add_action( 'betterdocs_listing_header', [ $this, 'header' ], 10, 1 );
173 + add_action( 'admin_bar_menu', array( $this, 'toolbar_menu' ), 32 );
108 174
109 - add_filter( 'admin_body_class', [ $this, 'body_classes' ] );
110 - add_filter( 'parent_file', [ $type, 'highlight_admin_menu' ] );
111 - add_filter( 'submenu_file', [ $type, 'highlight_admin_submenu' ], 10, 2 );
112 - add_filter( 'betterdocs_admin_menu', [ $this, 'quick_setup_menu' ], 10, 1 );
175 + add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
176 + add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
177 + add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
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 );
113 181
114 182 /**
115 183 * Remove Comments Column from List Table.
116 184 */
117 - add_filter( 'manage_docs_posts_columns', [ $this, 'set_custom_edit_action_columns' ] );
118 - add_filter( 'manage_docs_posts_custom_column', [ $this, 'manage_custom_columns' ], 10, 2 );
185 + add_filter( 'manage_docs_posts_columns', array( $this, 'set_custom_edit_action_columns' ) );
186 + add_filter( 'manage_docs_posts_custom_column', array( $this, 'manage_custom_columns' ), 10, 2 );
119 187
120 188 /**
121 189 * Add New Column
122 190 */
123 - add_filter( 'manage_users_columns', [ $this, 'add_users_total_docs_column' ], 10, 1 );
124 - add_filter( 'manage_users_custom_column', [ $this, 'popular_users_docs_data' ], 10, 3 );
191 + add_filter( 'manage_users_columns', array( $this, 'add_users_total_docs_column' ), 10, 1 );
192 + add_filter( 'manage_users_custom_column', array( $this, 'popular_users_docs_data' ), 10, 3 );
125 193 if ( is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
126 194 add_action( 'admin_footer-plugins.php', array( $this, 'disable_deactivation' ) );
127 195 }
128 196
@@ -127,9 +195,9 @@
127 195 }
128 196
129 197 if ( $this->settings->get( 'enable_estimated_reading_time' ) ) {
130 198 // Hook into unified metabox instead of creating separate metabox
131 - add_action( 'betterdocs_reading_time_tab_content', [ $this, 'render_estimated_time_markup' ] );
199 + add_action( 'betterdocs_reading_time_tab_content', array( $this, 'render_estimated_time_markup' ) );
132 200 }
133 201
134 202 self::$cache_bank = CacheBank::get_instance();
135 203
@@ -140,17 +208,28 @@
140 208 $this->notices();
141 209 } catch ( Exception $e ) {
142 210 unset( $e );
143 211 }
212 +
213 + // Initialize Black Friday Pointer
214 + $this->init_black_friday_pointer();
215 +
216 + // Register AJAX handler for pointer dismissal
217 + add_action( 'wp_ajax_betterdocs_dismiss_black_friday_pointer', array( $this, 'ajax_dismiss_black_friday_pointer' ) );
144 218 }
145 219
146 220 public function order_terms_in_wp_terms_admin_table() {
147 - //order the terms correctly to be shown on the admin panel categories menu with betterdocs order
148 - add_action('rest_insert_doc_category', function( $term, $request, $bool ) {
149 - $max_order = Helper::get_max_doc_category_order_from_term_meta() ?? 0;
150 - $next_order = $max_order + 1;
151 - update_term_meta( $term->term_id, 'doc_category_order', $next_order );
152 - }, 10, 3);
221 + // order the terms correctly to be shown on the admin panel categories menu with betterdocs order
222 + add_action(
223 + 'rest_insert_doc_category',
224 + function ( $term, $request, $bool ) {
225 + $max_order = Helper::get_max_doc_category_order_from_term_meta() ?? 0;
226 + $next_order = $max_order + 1;
227 + update_term_meta( $term->term_id, 'doc_category_order', $next_order );
228 + },
229 + 10,
230 + 3
231 + );
153 232 }
154 233
155 234 public function disable_deactivation() {
156 235 $tooltip_text = esc_html__( 'Deactivate BetterDocs Pro First', 'betterdocs' );
@@ -197,17 +276,17 @@
197 276 <?php
198 277 }
199 278
200 279 public function add_users_total_docs_column( $columns ) {
201 - $new_column = [
202 - 'docs' => __( 'Docs', 'betterdocs' )
203 - ];
280 + $new_column = array(
281 + 'docs' => __( 'Docs', 'betterdocs' ),
282 + );
204 283 $columns = array_merge( $columns, $new_column );
205 284 return $columns;
206 285 }
207 286
208 287 public function popular_users_docs_data( $output, $column_name, $user_id ) {
209 - if ( $column_name == 'docs' ) {
288 + if ( 'docs' == $column_name ) {
210 289 $total_count = count_user_posts( $user_id, 'docs', true );
211 290 return '<a href="edit.php?post_type=docs&author=' . $user_id . '" class="edit"><span aria-hidden="true">' . $total_count . '</span></a>';
212 291 }
213 292 return $output;
@@ -221,13 +300,15 @@
221 300 if ( betterdocs()->is_pro_active() ) {
222 301 $plugins = Helper::get_plugins();
223 302 $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php'];
224 303
225 - 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', '>=' ) ) {
226 307 return;
227 308 }
228 309
229 - betterdocs()->views->get( 'admin/notices/compatibility', [ 'version' => $plugin_data['Version'] ] );
310 + betterdocs()->views->get( 'admin/notices/compatibility', array( 'version' => $plugin_data['Version'] ) );
230 311 }
231 312 }
232 313
233 314 public function plugin_insights( $prevent_init = false ) {
@@ -232,20 +313,20 @@
232 313
233 314 public function plugin_insights( $prevent_init = false ) {
234 315 $this->insights = Insights::get_instance(
235 316 BETTERDOCS_PLUGIN_FILE,
236 - [
317 + array(
237 318 'opt_in' => true,
238 319 'goodbye_form' => true,
239 - 'item_id' => 'c7b16777b4f1b83f6083'
240 - ]
320 + 'item_id' => 'c7b16777b4f1b83f6083',
321 + )
241 322 );
242 323
243 324 $this->insights->set_notice_options(
244 - [
325 + array(
245 326 'notice' => __( 'Want to help make <strong>BetterDocs</strong> even more awesome? You can get a <strong>10% discount coupon</strong> for Premium extensions if you allow us to track the usage.', 'betterdocs' ),
246 - 'extra_notice' => __( 'We collect non-sensitive diagnostic data and plugin usage information. Your site URL, WordPress & PHP version, plugins & themes and email address to send you the discount coupon. This data lets us make sure this plugin always stays compatible with the most popular plugins and themes. No spam, I promise.', 'betterdocs' )
247 - ]
327 + 'extra_notice' => __( 'We collect non-sensitive diagnostic data and plugin usage information. Your site URL, WordPress & PHP version, plugins & themes and email address to send you the discount coupon. This data lets us make sure this plugin always stays compatible with the most popular plugins and themes. No spam, I promise.', 'betterdocs' ),
328 + )
248 329 );
249 330
250 331 if ( ! $prevent_init ) {
251 332 $this->insights->init();
@@ -261,76 +342,77 @@
261 342 * @throws Exception
262 343 */
263 344 public function notices() {
264 345 $notices = new Notices(
265 - [
346 + array(
266 347 'id' => 'betterdocs',
267 348 'storage_key' => 'notices',
268 349 'lifetime' => 3,
269 350 'stylesheet_url' => $this->assets->asset_url( 'admin/css/notices.css' ),
270 351 'styles' => $this->assets->asset_url( 'admin/css/notices.css' ),
271 - 'priority' => 4
272 - ]
352 + 'priority' => 4,
353 + )
273 354 );
274 355
275 356 /**
276 - * Review Notice
277 - * @var mixed $message
278 - */
357 + * Review Notice
358 + *
359 + * @var mixed $message
360 + */
279 361
280 362 $message = __( 'We hope you\'re enjoying BetterDocs! Could you please do us a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?', 'betterdocs' );
281 363
282 - $_review_notice = [
364 + $_review_notice = array(
283 365 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
284 366 'html' => '<p>' . $message . '</p>',
285 - 'links' => [
286 - 'later' => [
367 + 'links' => array(
368 + 'later' => array(
287 369 'link' => 'https://wordpress.org/plugins/betterdocs/#reviews',
288 370 'target' => '_blank',
289 371 'label' => __( 'Sure, you deserve it!', 'betterdocs' ),
290 - 'icon_class' => 'dashicons dashicons-external'
291 - ],
292 - 'allready' => [
372 + 'icon_class' => 'dashicons dashicons-external',
373 + ),
374 + 'allready' => array(
293 375 'label' => __( 'I already did', 'betterdocs' ),
294 376 'icon_class' => 'dashicons dashicons-smiley',
295 - 'attributes' => [
296 - 'data-dismiss' => true
297 - ]
298 - ],
299 - 'maybe_later' => [
377 + 'attributes' => array(
378 + 'data-dismiss' => true,
379 + ),
380 + ),
381 + 'maybe_later' => array(
300 382 'label' => __( 'Maybe Later', 'betterdocs' ),
301 383 'icon_class' => 'dashicons dashicons-calendar-alt',
302 - 'attributes' => [
384 + 'attributes' => array(
303 385 'data-later' => true,
304 - 'class' => 'dismiss-btn'
305 - ]
306 - ],
307 - 'support' => [
386 + 'class' => 'dismiss-btn',
387 + ),
388 + ),
389 + 'support' => array(
308 390 'link' => 'https://wpdeveloper.com/support',
309 - 'attributes' => [
310 - 'target' => '_blank'
311 - ],
391 + 'attributes' => array(
392 + 'target' => '_blank',
393 + ),
312 394 'label' => __( 'I need help', 'betterdocs' ),
313 - 'icon_class' => 'dashicons dashicons-sos'
314 - ],
315 - 'never_show_again' => [
395 + 'icon_class' => 'dashicons dashicons-sos',
396 + ),
397 + 'never_show_again' => array(
316 398 'label' => __( 'Never show again', 'betterdocs' ),
317 399 'icon_class' => 'dashicons dashicons-dismiss',
318 - 'attributes' => [
319 - 'data-dismiss' => true
320 - ]
321 - ]
322 - ]
323 - ];
400 + 'attributes' => array(
401 + 'data-dismiss' => true,
402 + ),
403 + ),
404 + ),
405 + );
324 406
325 407 $notices->add(
326 408 'review',
327 409 $_review_notice,
328 - [
410 + array(
329 411 'start' => $notices->strtotime( '+10 days' ),
330 412 'recurrence' => 30,
331 - 'dismissible' => true
332 - ]
413 + 'dismissible' => true,
414 + )
333 415 );
334 416
335 417 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
336 418 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
@@ -335,9 +417,9 @@
335 417 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
336 418 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
337 419
338 420 $message = sprintf(
339 - /* translators: %s is the name of the existing knowledge base plugin. */
421 + /* translators: %s is the name of the existing knowledge base plugin. */
340 422 __( 'Already using %s? Power up your Knowledge Base by migrating all your docs and settings to BetterDocs with just 1 click.', 'betterdocs' ),
341 423 esc_html( $plugin_name )
342 424 );
343 425
@@ -347,50 +429,51 @@
347 429 esc_url( admin_url( 'admin.php?page=betterdocs-settings&tab=tab-migration' ) ),
348 430 esc_html__( 'Start Migration', 'betterdocs' )
349 431 );
350 432
351 - $_migration_notice = [
433 + $_migration_notice = array(
352 434 'thumbnail' => '',
353 435 'html' => $migration_message,
354 - 'links' => [
355 - 'maybe_later' => [
436 + 'links' => array(
437 + 'maybe_later' => array(
356 438 'label' => __( 'Maybe Later', 'betterdocs' ),
357 439 'icon_class' => 'dashicons dashicons-calendar-alt',
358 - 'attributes' => [
440 + 'attributes' => array(
359 441 'data-later' => true,
360 - 'class' => 'dismiss-btn'
361 - ]
362 - ],
363 - 'never_show_again' => [
442 + 'class' => 'dismiss-btn',
443 + ),
444 + ),
445 + 'never_show_again' => array(
364 446 'label' => __( 'Never show again', 'betterdocs' ),
365 447 'icon_class' => 'dashicons dashicons-dismiss',
366 - 'attributes' => [
367 - 'data-dismiss' => true
368 - ]
369 - ]
370 - ]
371 - ];
448 + 'attributes' => array(
449 + 'data-dismiss' => true,
450 + ),
451 + ),
452 + ),
453 + );
372 454
373 455 $notices->add(
374 456 'migration',
375 457 $_migration_notice,
376 - [
458 + array(
377 459 'start' => $notices->time(),
378 460 'recurrence' => false,
379 - 'dismissible' => true
380 - ]
461 + 'dismissible' => true,
462 + )
381 463 );
382 464 }
383 465
384 466 /**
467 + *
385 468 * Opt-In Notice
386 469 */
387 470 $allow_tracking = get_option( 'wpins_allow_tracking' );
388 - if ( $this->insights != null && ! isset( $allow_tracking['betterdocs'] ) ) {
471 + if ( null != $this->insights && ! isset( $allow_tracking['betterdocs'] ) ) {
389 472 $notices->add(
390 473 'opt_in',
391 - [ $this->insights, 'notice' ],
392 - [
474 + array( $this->insights, 'notice' ),
475 + array(
393 476 'classes' => 'updated put-dismiss-notice',
394 477 'start' => $notices->time(),
395 478 'refresh' => BETTERDOCS_VERSION,
396 479 'dismissible' => true,
@@ -395,48 +478,48 @@
395 478 'refresh' => BETTERDOCS_VERSION,
396 479 'dismissible' => true,
397 480 'do_action' => 'wpdeveloper_notice_clicked_for_betterdocs',
398 481 'display_if' => ! function_exists( 'betterdocs_pro' ),
399 - 'screens' => [ 'dashboard' ]
400 - ]
482 + 'screens' => array( 'dashboard' ),
483 + )
401 484 );
402 485 }
403 486
404 - $spring_campaign_message = '<div class="betterdocs-spring-notice-body"><p style="margin-top: 0; margin-bottom: 0;">🌸 <strong>Spring Savings:</strong> Build AI-powered Knowledge Bases & FAQs to empower support and improve user experience – now <strong>Flat 25% OFF!</strong> ⚡️</p></div>';
405 - $_spring_campaign_notice = [
487 + $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>';
488 + $_summer_campaign_notice = array(
406 489 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
407 - 'html' => $spring_campaign_message,
408 - 'links' => [
409 - 'support' => [
410 - 'link' => 'https://betterdocs.co/spring2026-admin-notice',
411 - 'attributes' => [
490 + 'html' => $summer_campaign_message,
491 + 'links' => array(
492 + 'support' => array(
493 + 'link' => 'https://betterdocs.co/summer2026-admin-notice',
494 + 'attributes' => array(
412 495 'target' => '_blank',
413 496 'class' => 'offer-button',
414 - ],
497 + ),
415 498 'label' => __( 'Upgrade To PRO Now', 'betterdocs' ),
416 - ],
417 - 'maybe_later' => [
418 - 'label' => __( 'I\'ll Grab It Later', 'betterdocs' ),
419 - 'attributes' => [
499 + ),
500 + 'maybe_later' => array(
501 + 'label' => __( 'I’ll Grab It Later', 'betterdocs' ),
502 + 'attributes' => array(
420 503 'target' => '_blank',
421 504 'data-later' => true,
422 505 'class' => 'dismiss-btn',
423 - ],
424 - ],
425 - ],
426 - ];
506 + ),
507 + ),
508 + ),
509 + );
427 510
428 511 $notices->add(
429 - 'spring-campaign-26',
430 - $_spring_campaign_notice,
431 - [
512 + 'summer-campaign-26',
513 + $_summer_campaign_notice,
514 + array(
432 515 'start' => $notices->time(),
433 516 'recurrence' => false,
434 517 'dismissible' => true,
435 518 'refresh' => BETTERDOCS_VERSION,
436 - 'expire' => strtotime( '11:59:59pm May 31, 2026' ),
519 + 'expire' => strtotime( '11:59:59pm June 25, 2026' ),
437 520 'display_if' => ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ),
438 - ]
521 + )
439 522 );
440 523
441 524 self::$cache_bank->create_account( $notices );
442 525 self::$cache_bank->calculate_deposits( $notices );
@@ -441,9 +524,9 @@
441 524 self::$cache_bank->create_account( $notices );
442 525 self::$cache_bank->calculate_deposits( $notices );
443 526 if ( method_exists( self::$cache_bank, 'clear_notices_in_' ) ) {
444 527 self::$cache_bank->clear_notices_in_(
445 - [
528 + array(
446 529 'toplevel_page_betterdocs-dashboard',
447 530 'admin_page_betterdocs-admin',
448 531 'betterdocs_page_betterdocs-admin',
449 532 'betterdocs_page_betterdocs-settings',
@@ -450,11 +533,14 @@
450 533 'betterdocs_page_betterdocs-faq',
451 534 'betterdocs_page_betterdocs-analytics',
452 535 'betterdocs_page_betterdocs-glossaries',
453 536 'betterdocs_page_betterdocs-ai-chatbot',
537 + 'betterdocs_page_betterdocs-api-docs',
538 + 'betterdocs_page_betterdocs-doc-categories',
539 + 'betterdocs_page_betterdocs-doc-tags',
454 540 'edit-doc_category',
455 - 'edit-doc_tag'
456 - ],
541 + 'edit-doc_tag',
542 + ),
457 543 $notices,
458 544 true
459 545 );
460 546 }
@@ -459,31 +545,93 @@
459 545 );
460 546 }
461 547 }
462 548
549 + /**
550 + * Resolve the admin dark-mode preference.
551 + *
552 + * The mode switcher stores the choice in a client cookie (no DB write, shared
553 + * across every admin screen). Fall back to the legacy
554 + * `betterdocs_settings['dark_mode']` value for installs that set it before this
555 + * change and haven't toggled since.
556 + *
557 + * @return bool
558 + */
559 + public function is_dark_mode() {
560 + if ( isset( $_COOKIE['betterdocs_admin_dark_mode'] ) ) {
561 + return '1' === $_COOKIE['betterdocs_admin_dark_mode']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
562 + }
563 +
564 + $saved = get_option( 'betterdocs_settings', array() );
565 + return ! empty( $saved['dark_mode'] );
566 + }
567 +
568 + /**
569 + * Whether the knowledge base is genuinely empty (no doc categories and no
570 + * non-trash docs). Localized to the admin so the All Docs panel can render a
571 + * skeleton shaped like the "No Category Found" empty card on first paint —
572 + * instead of a category/docs skeleton it would immediately replace — without
573 + * waiting for the REST fetch to reveal the count.
574 + *
575 + * @return bool
576 + */
577 + public function kb_is_empty() {
578 + $cats = wp_count_terms( array( 'taxonomy' => 'doc_category', 'hide_empty' => false ) );
579 + $cats = is_wp_error( $cats ) ? 0 : (int) $cats;
580 + if ( $cats > 0 ) {
581 + return false;
582 + }
583 +
584 + $counts = (array) wp_count_posts( 'docs' );
585 + $total = 0;
586 + foreach ( array( 'publish', 'future', 'draft', 'pending', 'private' ) as $status ) {
587 + $total += isset( $counts[ $status ] ) ? (int) $counts[ $status ] : 0;
588 + }
589 +
590 + return 0 === $total;
591 + }
592 +
463 593 public function body_classes( $classes ) {
464 - $saved_settings = get_option( 'betterdocs_settings', false );
465 - $dark_mode = isset( $saved_settings['dark_mode'] ) ? $saved_settings['dark_mode'] : false;
466 - $dark_mode = ! empty( $dark_mode ) ? boolval( $dark_mode ) : false;
594 + $dark_mode = $this->is_dark_mode();
467 595 $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', str_replace( 'admin_page_', '', get_current_screen()->id ) ) ) : '';
468 - $registered_screens = [
469 - 'betterdocs-settings',
470 - 'betterdocs-admin',
471 - 'betterdocs-dashboard',
472 - 'betterdocs-analytics',
473 - 'betterdocs-glossaries',
474 - 'betterdocs-faq',
475 - 'edit-doc_category',
476 - 'edit-doc_tag',
477 - 'edit-knowledge_base',
478 - 'betterdocs-ai-chatbot'
479 - ];
596 + /**
597 + * Filter the list of (prefix-stripped) screen ids that receive the
598 + * `betterdocs-admin` body class (and dark-mode class). Pro/add-ons can
599 + * register their own React admin pages, e.g. the Knowledge Base page.
600 + *
601 + * @param string[] $registered_screens Screen ids with the page prefix removed.
602 + */
603 + $registered_screens = apply_filters( 'betterdocs_admin_screen_slugs', array(
604 + 'betterdocs-settings',
605 + 'betterdocs-admin',
606 + 'betterdocs-dashboard',
607 + 'betterdocs-analytics',
608 + 'betterdocs-content-iq',
609 + 'betterdocs-glossaries',
610 + 'betterdocs-faq',
611 + 'betterdocs-doc-categories',
612 + 'betterdocs-doc-tags',
613 + 'edit-doc_category',
614 + 'edit-doc_tag',
615 + 'edit-knowledge_base',
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',
624 + ) );
480 625
481 - if( in_array( $current_screen_id, $registered_screens ) ) {
482 - $classes .= ' betterdocs-admin ';
483 - }
626 + if ( in_array( $current_screen_id, $registered_screens ) ) {
627 + $classes .= ' betterdocs-admin ';
628 + }
484 629
485 - if ( $dark_mode === true && in_array( $current_screen_id, $registered_screens ) ) {
630 + // Dark mode also applies on the Quick Setup wizard, whose self-scoped chrome
631 + // keys off `.betterdocs_page_betterdocs-setup.betterdocs-dark-mode`.
632 + $dark_screens = array_merge( $registered_screens, array( 'betterdocs-setup' ) );
633 + if ( $dark_mode && in_array( $current_screen_id, $dark_screens, true ) ) {
486 634 $classes .= ' betterdocs-dark-mode ';
487 635 }
488 636
489 637 return $classes;
@@ -498,11 +646,11 @@
498 646 * @since 1.0.0
499 647 */
500 648 public function set_custom_edit_action_columns( $columns ) {
501 649 unset( $columns['comments'] );
502 - $new_columns = [];
650 + $new_columns = array();
503 651 foreach ( $columns as $key => $value ) {
504 - if ( $key == 'date' ) {
652 + if ( 'date' == $key ) {
505 653 $new_columns['betterdocs_word_count'] = __( 'Word Count', 'betterdocs' ); // put the tags column before it
506 654 $new_columns['betterdocs_reaction'] = __( 'Reactions', 'betterdocs' );
507 655 }
508 656 $new_columns[ $key ] = $value;
@@ -514,26 +662,29 @@
514 662 public function manage_custom_columns( $column, $post_id ) {
515 663 global $wpdb;
516 664 switch ( $column ) {
517 665 case 'betterdocs_word_count':
518 - $content_without_html_tags = trim( strip_tags( get_post_field( 'post_content', $post_id ) ) );
666 + $content_without_html_tags = trim( wp_strip_all_tags( get_post_field( 'post_content', $post_id ) ) );
519 667 preg_match_all( '/<[^>]*>|[\p{L}\p{M}]+/u', $content_without_html_tags, $matches );
520 - $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( [] );
668 + $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( array() );
521 669 $word_count = $total_words;
522 670 echo '<span>' . esc_html( intval( $word_count ) ) . '</span>';
523 671 break;
524 672 case 'betterdocs_reaction':
525 - $where = "WHERE post_id='" . esc_sql( $post_id ) . "'";
673 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- per-post analytics aggregation rendered in admin list table; cache would mask live reactions.
526 674 $analytics = $wpdb->get_results(
527 - "SELECT
528 - sum(impressions) as totalViews,
529 - sum(unique_visit) as totalUniqueViews,
530 - sum(happy + sad + normal) as totalReactions,
531 - sum(happy) as totalHappy,
532 - sum(normal) as totalNormal,
533 - sum(sad) as totalSad
534 - FROM {$wpdb->prefix}betterdocs_analytics
535 - $where"
675 + $wpdb->prepare(
676 + "SELECT
677 + sum(impressions) as totalViews,
678 + sum(unique_visit) as totalUniqueViews,
679 + sum(happy + sad + normal) as totalReactions,
680 + sum(happy) as totalHappy,
681 + sum(normal) as totalNormal,
682 + sum(sad) as totalSad
683 + FROM {$wpdb->prefix}betterdocs_analytics
684 + WHERE post_id = %d",
685 + $post_id
686 + )
536 687 );
537 688
538 689 echo '<ul class="reactions-count">
539 690 <li>
@@ -589,22 +740,22 @@
589 740 * @return void
590 741 * @since 1.0.0
591 742 */
592 743 public function styles( $hook ) {
593 - $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', [], 'all' );
744 + $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', array(), 'all' );
594 745
595 746 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
596 747 return;
597 748 }
598 749
599 - $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', [], 'all' );
600 - $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', [], 'all' );
601 - $this->assets->enqueue( 'betterdocs-old', 'admin/css/betterdocs.css', [], 'all' );
750 + $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', array(), 'all' );
751 + $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', array(), 'all' );
752 + $this->assets->enqueue( 'betterdocs-old', 'admin/css/betterdocs.css', array(), 'all' );
602 753
603 754 /**
604 - * This scripts enqueued for Dashboard App.
605 - */
606 - $this->assets->enqueue( 'betterdocs', 'admin/css/dashboard.css', [ 'betterdocs-old' ], '', BETTERDOCS_VERSION );
755 + * This scripts enqueued for Dashboard App.
756 + */
757 + $this->assets->enqueue( 'betterdocs', 'admin/css/dashboard.css', array( 'betterdocs-old' ), '', BETTERDOCS_VERSION );
607 758 $this->assets->enqueue( 'betterdocs-icons', 'admin/btd-icon/style.css' );
608 759 }
609 760
610 761 /**
@@ -615,26 +766,70 @@
615 766 * @return void
616 767 * @since 1.0.0
617 768 */
618 769 public function scripts( $hook ) {
619 - if ( ( $hook === 'edit.php' ) && get_post_type() == 'docs' ) {
770 + // Classic-UI screens that should offer a "Switch to BetterDocs UI"
771 + // button: All Docs, FAQ list, FAQ groups, Product FAQ groups,
772 + // Doc Categories, Doc Tags. Maps each to the React admin page to
773 + // return to; $switch_args carries extra query args (e.g. the FAQ
774 + // Builder tab) appended to the React page URL.
775 + $switch_page = '';
776 + $switch_args = array();
777 + if ( 'edit.php' === $hook && 'docs' === get_post_type() ) {
778 + $switch_page = 'betterdocs-admin';
779 + } elseif ( 'edit.php' === $hook && 'betterdocs_faq' === get_post_type() ) {
780 + $switch_page = 'betterdocs-faq';
781 + } elseif ( 'edit-tags.php' === $hook ) {
782 + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
783 + $taxonomy = $screen && ! empty( $screen->taxonomy )
784 + ? $screen->taxonomy
785 + : ( isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
786 + if ( 'betterdocs_faq_category' === $taxonomy ) {
787 + $switch_page = 'betterdocs-faq';
788 + } elseif ( 'betterdocs_product_faq_category' === $taxonomy ) {
789 + // Product FAQ groups live on the FAQ Builder's WooCommerce tab.
790 + $switch_page = 'betterdocs-faq';
791 + $switch_args = array( 'faq_tab' => 'woocommerce' );
792 + } elseif ( 'doc_category' === $taxonomy ) {
793 + $switch_page = 'betterdocs-doc-categories';
794 + } elseif ( 'doc_tag' === $taxonomy ) {
795 + $switch_page = 'betterdocs-doc-tags';
796 + }
797 + }
798 +
799 + /**
800 + * Allow Pro/add-ons to map their own classic-UI taxonomy screens to a
801 + * React admin page for the "Switch to BetterDocs UI" button — e.g. the
802 + * Knowledge Base taxonomy, which only exists when Pro is active.
803 + *
804 + * @param string $switch_page React page slug, or '' for no switcher.
805 + * @param string $hook Current admin page hook.
806 + */
807 + $switch_page = apply_filters( 'betterdocs_classic_switch_page', $switch_page, $hook );
808 +
809 + if ( $switch_page ) {
620 810 $this->assets->enqueue(
621 811 'betterdocs-switcher',
622 812 'admin/js/switcher.js',
623 - [
624 - 'jquery'
625 - ]
813 + array(
814 + 'jquery',
815 + )
626 816 );
627 817
628 818 $this->assets->localize(
629 819 'betterdocs-switcher',
630 820 'betterdocsSwitcher',
631 - [
821 + array(
632 822 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
823 + 'page' => $switch_page,
824 + 'url' => add_query_arg(
825 + array_merge( array( 'page' => $switch_page ), $switch_args ),
826 + admin_url( 'admin.php' )
827 + ),
633 828 'site_address' => get_bloginfo( 'url' ),
634 829 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
635 - 'betterdocs_pro_version' => betterdocs()->pro_version()
636 - ]
830 + 'betterdocs_pro_version' => betterdocs()->pro_version(),
831 + )
637 832 );
638 833
639 834 return;
640 835 }
@@ -644,104 +839,127 @@
644 839 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
645 840 return;
646 841 }
647 842
648 - wp_enqueue_media(); // load early to fix problems with media upload issues on settings for wordpress 6.0.9
843 + wp_enqueue_media(); // load early to fix problems with media upload issues on settings for WordPress 6.0.9
649 844 $this->assets->register( 'betterdocs-admin', 'admin/js/dashboard.js' );
650 845
651 846 $saved_settings = get_option( 'betterdocs_settings', false );
652 - $dark_mode = $saved_settings['dark_mode'] ?? false;
653 - $dark_mode = ! empty( $dark_mode ) && boolval( $dark_mode );
847 + $dark_mode = $this->is_dark_mode();
654 848 $this->assets->localize(
655 849 'betterdocs-admin',
656 850 'betterdocs_admin',
657 - [
851 + array(
658 852 'ajaxurl' => admin_url( 'admin-ajax.php' ),
659 853 'doc_cat_order_nonce' => wp_create_nonce( 'doc_cat_order_nonce' ),
660 854 'knowledge_base_order_nonce' => wp_create_nonce( 'knowledge_base_order_nonce' ),
661 - 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing
662 - 'per_page_id' => 'edit_doc_category_per_page',
663 - 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
664 - 'dark_mode' => $dark_mode,
665 - 'text' => __( 'Copied!', 'betterdocs' ),
666 - 'test_report' => __( 'Test Report!', 'betterdocs' ),
667 - 'sending' => __( 'Sending...', 'betterdocs' ),
668 - 'dir_url' => BETTERDOCS_ABSURL,
669 - 'rest_url' => esc_url_raw( rest_url() ),
670 - 'free_version' => betterdocs()->version,
671 - 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
672 - 'nonce' => wp_create_nonce( 'wp_rest' ),
673 - 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
674 - 'count_all_docs' => array_sum((array) wp_count_posts('docs')),
675 - 'count_all_faq' => array_sum((array) wp_count_posts('betterdocs_faq')),
676 - 'count_new_docs' => count(get_option('saved_docs_post_ids', [])) + count(get_option('betterdocs_ai_chatbot_error_posts', [])),
677 - 'admin_url' => admin_url(),
678 - 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
679 - 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
680 - 'previewMode' => betterdocs()->settings->get( 'ia_enable_preview', false ),
681 - 'dashboard_mode' => get_option( 'dashboard_mode' ),
682 - 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
683 - 'betterdocs_pro_version' => betterdocs()->pro_version(),
684 - 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
685 - 'disabled_embed_model_option' => get_option('disabled_embed_model_option'),
686 - 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
687 - 'total_doc_category_terms' => wp_count_terms( 'doc_category'),
688 - 'github_oauth_nonce' => wp_create_nonce( 'betterdocs_github_oauth_nonce' ),
689 - 'git_settings' => [
690 - 'git_repository_url' => betterdocs()->settings->get_raw_field( 'git_repository_url', '' ),
691 - 'git_branch' => betterdocs()->settings->get_raw_field( 'git_branch', '' ),
692 - 'git_docs_directory' => betterdocs()->settings->get_raw_field( 'git_docs_directory', '' ),
693 - 'git_provider' => betterdocs()->settings->get_raw_field( 'git_provider', 'github' ),
694 - ],
695 - ]
855 + 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- pagination read from URL.
856 + 'per_page_id' => 'edit_doc_category_per_page',
857 + 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
858 + 'dark_mode' => $dark_mode,
859 + 'kb_is_empty' => $this->kb_is_empty(),
860 + 'text' => __( 'Copied!', 'betterdocs' ),
861 + 'test_report' => __( 'Test Report!', 'betterdocs' ),
862 + 'sending' => __( 'Sending...', 'betterdocs' ),
863 + 'dir_url' => BETTERDOCS_ABSURL,
864 + 'rest_url' => esc_url_raw( rest_url() ),
865 + 'free_version' => betterdocs()->version,
866 + 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
867 + 'ai_sample_docs' => array(
868 + 'enabled' => (bool) betterdocs()->settings->get( 'enable_ai_sample_docs', true ),
869 + 'rest_base' => esc_url_raw( get_rest_url( null, '/betterdocs/v1/sample-docs' ) ),
870 + ),
871 + 'nonce' => wp_create_nonce( 'wp_rest' ),
872 + 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
873 + 'count_all_docs' => array_sum( (array) wp_count_posts( 'docs' ) ),
874 + 'count_all_faq' => array_sum( (array) wp_count_posts( 'betterdocs_faq' ) ),
875 + 'faq_order' => get_option( 'betterdocs_faq_order', 'default' ),
876 + 'count_new_docs' => $this->get_not_synced_docs_count(),
877 + 'admin_url' => admin_url(),
878 + 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
879 + 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
880 + 'previewMode' => betterdocs()->settings->get( 'ia_enable_preview', false ),
881 + 'dashboard_mode' => get_option( 'dashboard_mode' ),
882 + 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
883 + 'betterdocs_pro_version' => betterdocs()->pro_version(),
884 + 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
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(),
892 + 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
893 + 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
894 + 'current_admin_language' => Helper::get_current_admin_language(),
895 + 'is_multilingual' => Helper::is_multilingual_active(),
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 + ),
911 + )
696 912 );
697 913
698 914 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
699 915 if ( ! wp_script_is( 'wp-date', 'registered' ) ) {
700 - $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', [] );
916 + $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', array() );
701 917 }
702 918 wp_enqueue_script( 'betterdocs-admin' );
703 919
704 920 /**
705 - * Duplicate Codes Need to Be Removed From Here Onwards
706 - */
921 + * Duplicate Codes Need to Be Removed From Here Onwards
922 + */
707 923
708 - //FAQ Builder Related Localization
924 + // FAQ Builder Related Localization
709 925 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/css/faq.css' );
710 926 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/js/faq.js' );
711 927
928 + // Load the classic editor (TinyMCE + QuickTags) so the FAQ rich-text editor can mount via wp.editor.initialize().
929 + if ( function_exists( 'wp_enqueue_editor' ) ) {
930 + wp_enqueue_editor();
931 + }
932 + if ( function_exists( 'wp_enqueue_media' ) ) {
933 + wp_enqueue_media();
934 + }
935 +
712 936 // removing emoji support
713 937 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
714 938 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
715 939
940 + // Get settings and remove unnecessary keys
941 + $betterdocs_settings = get_option( 'betterdocs_settings', false );
942 + if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
943 + foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
944 + unset( $betterdocs_settings[ $sensitive_key ] );
945 + }
946 + }
947 +
716 948 betterdocs()->assets->localize(
717 949 'betterdocs-admin-faq',
718 950 'betterdocsFaq',
719 - [
951 + array(
720 952 'dir_url' => BETTERDOCS_ABSURL,
721 953 'rest_url' => esc_url_raw( rest_url() ),
722 954 'free_version' => betterdocs()->version,
723 955 'nonce' => wp_create_nonce( 'wp_rest' ),
724 - 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
725 - ]
956 + 'betterdocs_settings' => $betterdocs_settings,
957 + )
726 958 );
727 959
728 - //Glossaries Related Localization
729 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
730 -
731 - betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
732 -
733 - betterdocs()->assets->localize(
734 - 'betterdocs-admin-glossaries',
735 - 'betterdocsGlossary',
736 - [
737 - 'dir_url' => BETTERDOCS_ABSURL,
738 - 'rest_url' => esc_url_raw( rest_url() ),
739 - 'free_version' => betterdocs()->version,
740 - 'nonce' => wp_create_nonce( 'wp_rest' ),
741 - 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
742 - ]
743 - );
960 + // Glossaries is Pro — Pro's Core\Glossaries::enqueue() owns that bundle
961 + // and both of its localized objects (`betterdocs`, `betterdocsGlossary`).
744 962 }
745 963
746 964 /**
747 965 * All admin pages header
@@ -749,31 +967,31 @@
749 967 * @return void
750 968 * @since 1.0.0
751 969 */
752 970 public function header( $admin_tab_name ) {
753 - $quick_links = [
971 + $quick_links = array(
754 972 'switch_view' => sprintf(
755 973 '<a href="%s" class="betterdocs-button betterdocs-button-secondary">%s</a>',
756 974 add_query_arg(
757 - [
975 + array(
758 976 'post_type' => 'docs',
759 - 'bdocs_view' => 'classic'
760 - ],
977 + 'bdocs_view' => 'classic',
978 + ),
761 979 'edit.php'
762 980 ),
763 981 __( 'Switch to Classic UI', 'betterdocs' )
764 982 ),
765 - 'add_new_doc' => sprintf( '<a href="%s" class="betterdocs-button betterdocs-button-primary">%s</a>', add_query_arg( [ 'post_type' => 'docs' ], 'post-new.php' ), __( 'Add New Doc', 'betterdocs' ) )
766 - ];
983 + 'add_new_doc' => sprintf( '<a href="%s" class="betterdocs-button betterdocs-button-primary">%s</a>', add_query_arg( array( 'post_type' => 'docs' ), 'post-new.php' ), __( 'Add New Doc', 'betterdocs' ) ),
984 + );
767 985
768 986 $quick_links = apply_filters( 'betterdocs_quick_links', $quick_links );
769 987
770 988 betterdocs()->views->get(
771 989 'admin/header',
772 - [
990 + array(
773 991 'quick_links' => $quick_links,
774 - 'active_tab' => $admin_tab_name
775 - ]
992 + 'active_tab' => $admin_tab_name,
993 + )
776 994 );
777 995 }
778 996
779 997 /**
@@ -782,17 +1000,17 @@
782 1000 * @return void
783 1001 * @since 1.0.0
784 1002 */
785 1003 public function menus() {
786 - $default_args = [
1004 + $default_args = array(
787 1005 'page_title' => 'BetterDocs',
788 1006 'menu_title' => 'BetterDocs',
789 - 'capability' => 'edit_docs', // Unified capability
1007 + 'capability' => 'edit_docs', // Unified capability
790 1008 'menu_slug' => $this->slug,
791 - 'callback' => [ $this, 'output' ],
1009 + 'callback' => array( $this, 'output' ),
792 1010 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
793 - 'position' => 5
794 - ];
1011 + 'position' => 5,
1012 + );
795 1013
796 1014 $_menu_position = 5;
797 1015 global $submenu;
798 1016
@@ -798,24 +1016,29 @@
798 1016
799 1017 // Always register both UI endpoints
800 1018 $this->register_modern_ui_fallback();
801 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 +
802 1025 foreach ( $this->menu_list() as $key => $value ) {
803 - if ( $key === 'betterdocs' ) {
1026 + if ( 'betterdocs' === $key ) {
804 1027 $callable = 'add_menu_page';
805 1028 $value = wp_parse_args( $value, $default_args );
806 1029 call_user_func_array( $callable, $value );
807 1030 } else {
808 - $is_core_page = strpos($value['menu_slug'], '?') !== false;
1031 + $is_core_page = strpos( $value['menu_slug'], '?' ) !== false;
809 1032
810 - if ($is_core_page) {
1033 + if ( $is_core_page ) {
811 1034 // Add classic UI directly
812 - $submenu[$this->slug][] = [
1035 + $submenu[ $this->slug ][] = array(
813 1036 $value['menu_title'],
814 1037 $value['capability'],
815 1038 $value['menu_slug'],
816 - $value['page_title']
817 - ];
1039 + $value['page_title'],
1040 + );
818 1041 } else {
819 1042 // Add modern UI through WordPress API
820 1043 add_submenu_page(
821 1044 $this->slug,
@@ -825,30 +1048,223 @@
825 1048 $value['menu_slug'],
826 1049 $value['callback']
827 1050 );
828 1051 }
829 - $_menu_position++;
1052 + ++$_menu_position;
830 1053 }
831 1054 }
1055 +
1056 + $this->paint_mcp_badge();
832 1057 }
833 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 +
834 1250 private function register_modern_ui_fallback() {
835 1251 // Add the submenu with valid parent slug
836 1252 add_submenu_page(
837 1253 'betterdocs', // Valid parent slug
838 - __('All Docs', 'betterdocs'),
1254 + __( 'All Docs', 'betterdocs' ),
839 1255 '', // Empty menu title hides it
840 1256 'edit_docs',
841 1257 'betterdocs-admin',
842 - [ $this, 'output' ]
1258 + array( $this, 'output' )
843 1259 );
844 1260
845 1261 // Hide the menu item from appearing in the admin sidebar
846 1262 global $submenu;
847 - if (isset($submenu['betterdocs'])) {
848 - foreach ($submenu['betterdocs'] as $key => $item) {
849 - if ($item[2] === 'betterdocs-admin') {
850 - unset($submenu['betterdocs'][$key]);
1263 + if ( isset( $submenu['betterdocs'] ) ) {
1264 + foreach ( $submenu['betterdocs'] as $key => $item ) {
1265 + if ( 'betterdocs-admin' === $item[2] ) {
1266 + unset( $submenu['betterdocs'][ $key ] );
851 1267 break;
852 1268 }
853 1269 }
854 1270 }
@@ -861,17 +1277,17 @@
861 1277 * @since 1.0.0
862 1278 */
863 1279 public function output() {
864 1280 if ( betterdocs()->is_pro_active()
865 - && version_compare( betterdocs()->pro_version(), '3.3.4', '<=' )
866 - && get_current_screen()->id == 'betterdocs_page_betterdocs-analytics' ) {
1281 + && version_compare( betterdocs()->pro_version(), '3.3.4', '<=' )
1282 + && get_current_screen()->id == 'betterdocs_page_betterdocs-analytics' ) {
867 1283 betterdocs_pro()->views->get( 'admin/analytics-pro' );
868 1284 } else {
869 1285 betterdocs()->views->get(
870 1286 'admin/main',
871 - [
872 - 'admin_ui' => 'dnd'
873 - ]
1287 + array(
1288 + 'admin_ui' => 'dnd',
1289 + )
874 1290 );
875 1291 }
876 1292 }
877 1293
@@ -884,11 +1300,10 @@
884 1300 * @param array $callback
885 1301 *
886 1302 * @return array
887 1303 * @since 2.5.0
888 - *
889 1304 */
890 - private function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null, $optional = [] ) {
1305 + private function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null, $optional = array() ) {
891 1306 return Helper::normalize_menu( $title, $slug, $cap, $callback, $optional );
892 1307 }
893 1308
894 1309 /**
@@ -897,34 +1312,34 @@
897 1312 * @return array
898 1313 * @since 1.0.0
899 1314 */
900 1315 private function menu_list() {
901 - $parent_slug = [];
1316 + $parent_slug = array();
902 1317
903 - $betterdocs_admin_pages = [
904 - 'betterdocs' => [
1318 + $betterdocs_admin_pages = array(
1319 + 'betterdocs' => array(
905 1320 'menu_slug' => $this->slug,
906 1321 'page_title' => 'BetterDocs',
907 1322 'menu_title' => 'BetterDocs',
908 1323 'capability' => 'edit_docs',
909 - 'callback' => [ $this, 'output' ],
1324 + 'callback' => array( $this, 'output' ),
910 1325 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
911 - 'position' => 5
912 - ],
913 - 'dashboard' => $this->normalize_menu(
1326 + 'position' => 5,
1327 + ),
1328 + 'dashboard' => $this->normalize_menu(
914 1329 __( 'Dashboard', 'betterdocs' ),
915 1330 'betterdocs-dashboard',
916 1331 'edit_docs',
917 - [
1332 + array(
918 1333 $this,
919 - 'output'
920 - ]
1334 + 'output',
1335 + )
921 1336 ),
922 1337 'all_docs' => $this->normalize_menu(
923 1338 __( 'All Docs', 'betterdocs' ),
924 1339 $this->ui_slug(),
925 1340 'edit_docs',
926 - [ $this, 'output' ],
1341 + array( $this, 'output' ),
927 1342 $parent_slug
928 1343 ),
929 1344 'add_new' => $this->normalize_menu(
930 1345 __( 'Add New', 'betterdocs' ),
@@ -931,95 +1346,210 @@
931 1346 'post-new.php?post_type=docs'
932 1347 ),
933 1348 'categories' => $this->normalize_menu(
934 1349 __( 'Categories', 'betterdocs' ),
935 - 'edit-tags.php?taxonomy=doc_category&post_type=docs',
936 - 'manage_doc_terms'
1350 + 'betterdocs-doc-categories',
1351 + 'manage_doc_terms',
1352 + array( $this, 'output' ),
1353 + $parent_slug
937 1354 ),
938 1355 'tags' => $this->normalize_menu(
939 1356 __( 'Tags', 'betterdocs' ),
940 - 'edit-tags.php?taxonomy=doc_tag&post_type=docs',
941 - 'manage_doc_terms'
1357 + 'betterdocs-doc-tags',
1358 + 'manage_doc_terms',
1359 + array( $this, 'output' ),
1360 + $parent_slug
942 1361 ),
943 1362 'settings' => $this->normalize_menu(
944 1363 __( 'Settings', 'betterdocs' ),
945 1364 'betterdocs-settings',
946 1365 'edit_docs_settings',
947 - [
1366 + array(
948 1367 $this,
949 - 'output'
950 - ],
1368 + 'output',
1369 + ),
951 1370 $parent_slug
952 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 + ),
953 1382 'analytics' => $this->normalize_menu(
954 1383 __( 'Analytics', 'betterdocs' ),
955 1384 'betterdocs-analytics',
956 1385 'read_docs_analytics',
957 - [
1386 + array(
958 1387 $this,
959 - 'output'
960 - ],
1388 + 'output',
1389 + ),
961 1390 $parent_slug
962 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 + ),
963 1402 'faq' => $this->normalize_menu(
964 1403 __( 'FAQ Builder', 'betterdocs' ),
965 1404 'betterdocs-faq',
966 1405 'read_faq_builder',
967 - [
1406 + array(
968 1407 $this,
969 - 'output'
970 - ],
1408 + 'output',
1409 + ),
971 1410 $parent_slug
972 - )
973 - ];
1411 + ),
1412 + );
974 1413
975 - 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 ) ) {
976 1427 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
977 1428 __( 'Glossaries', 'betterdocs' ),
978 1429 'betterdocs-glossaries',
979 - 'read_docs_analytics',
980 - [
1430 + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1431 + array(
981 1432 $this,
982 - 'output'
983 - ],
1433 + 'output',
1434 + ),
984 1435 $parent_slug
985 1436 );
986 1437 }
987 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 + }
988 1454
989 - if (!betterdocs()->is_chatbot_active()) {
990 - $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
991 - __('AI Chatbot', 'betterdocs'),
992 - 'betterdocs-ai-chatbot',
993 - 'edit_docs_settings',
994 - [
1455 + if ( ! betterdocs()->is_chatbot_active() ) {
1456 + $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1457 + __( 'AI Chatbot', 'betterdocs' ),
1458 + 'betterdocs-ai-chatbot',
1459 + 'edit_docs_settings',
1460 + array(
995 1461 $this,
996 - 'output'
997 - ],
998 - $parent_slug
999 - );
1000 - }
1462 + 'output',
1463 + ),
1464 + $parent_slug
1465 + );
1466 + }
1001 1467
1002 - return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, [$this, 'output'], $parent_slug);
1468 + return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1469 + }
1003 1470
1004 - }
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 + }
1005 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 +
1006 1533 public function add_custom_classes_to_menu_items() {
1007 1534 global $menu, $submenu;
1008 1535
1009 - $menu_items = [
1536 + $menu_items = array(
1010 1537 'betterdocs' => 'betterdocs',
1011 1538 'betterdocs_page_all_docs' => 'betterdocs-all-docs',
1012 1539 'betterdocs_page_add_new' => 'betterdocs-add-new',
1013 - 'edit-tags.php?taxonomy=doc_category&post_type=docs' => 'betterdocs-categories',
1014 - 'edit-tags.php?taxonomy=doc_tag&post_type=docs' => 'betterdocs-tags',
1540 + 'betterdocs-doc-categories' => 'betterdocs-categories',
1541 + 'betterdocs-doc-tags' => 'betterdocs-tags',
1015 1542 'betterdocs-settings' => 'betterdocs-settings',
1543 + 'betterdocs-mcp' => 'betterdocs-mcp',
1016 1544 'betterdocs-analytics' => 'betterdocs-analytics',
1545 + 'betterdocs-content-iq' => 'betterdocs-content-iq',
1017 1546 'betterdocs-faq' => 'betterdocs-faq',
1018 1547 'betterdocs-glossaries' => 'betterdocs-glossaries',
1019 1548 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1020 - 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb'
1021 - ];
1549 + 'betterdocs-api-docs' => 'betterdocs-api-docs',
1550 + 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1551 + );
1022 1552
1023 1553 foreach ( $menu as &$item ) {
1024 1554 if ( isset( $menu_items[ $item[2] ] ) ) {
1025 1555 if ( ! isset( $item[4] ) ) {
@@ -1049,12 +1579,12 @@
1049 1579 $menus['quick_setup'] = $this->normalize_menu(
1050 1580 __( 'Quick Setup', 'betterdocs' ),
1051 1581 'betterdocs-setup',
1052 1582 'delete_users',
1053 - [
1583 + array(
1054 1584 $this->container->get( SetupWizard::class ),
1055 - 'views'
1056 - ]
1585 + 'views',
1586 + )
1057 1587 );
1058 1588 }
1059 1589
1060 1590 return $menus;
@@ -1062,8 +1592,12 @@
1062 1592
1063 1593 public function insert_plugin_links( $links ) {
1064 1594 $links[] = '<a href="admin.php?page=betterdocs-settings">' . __( 'Settings', 'betterdocs' ) . '</a>';
1065 1595
1596 + if ( ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
1597 + $links[] = '<a href="https://betterdocs.co/upgrade-to-pro-plugins-wp" target="_blank" style="color: #000; font-weight: bold;">' . __( 'Upgrade to Pro', 'betterdocs' ) . '</a>';
1598 + }
1599 +
1066 1600 return $links;
1067 1601 }
1068 1602
1069 1603 public function toolbar_menu( $admin_bar ) {
@@ -1098,14 +1632,14 @@
1098 1632
1099 1633 $encyclopedia_url = home_url( $slug );
1100 1634
1101 1635 $admin_bar->add_node(
1102 - [
1636 + array(
1103 1637 'parent' => 'site-name',
1104 1638 'id' => 'view-docs',
1105 1639 'title' => __( 'Visit Documentation', 'betterdocs' ),
1106 - 'href' => $docs_url
1107 - ]
1640 + 'href' => $docs_url,
1641 + )
1108 1642 );
1109 1643
1110 1644 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1111 1645
@@ -1110,14 +1644,14 @@
1110 1644 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1111 1645
1112 1646 if ( $is_enable_encyclopedia && betterdocs()->is_pro_active() ) {
1113 1647 $admin_bar->add_node(
1114 - [
1648 + array(
1115 1649 'parent' => 'site-name',
1116 1650 'id' => 'view-encyclopedia',
1117 1651 'title' => __( 'Visit Encyclopedia', 'betterdocs' ),
1118 - 'href' => $encyclopedia_url
1119 - ]
1652 + 'href' => $encyclopedia_url,
1653 + )
1120 1654 );
1121 1655 }
1122 1656 }
1123 1657
@@ -1124,14 +1658,20 @@
1124 1658 /**
1125 1659 * Save last visited admin ui
1126 1660 *
1127 1661 * @since 3.0.1
1128 - *
1129 1662 */
1130 1663 public function save_admin_page() {
1131 - if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'docs' && isset( $_GET['bdocs_view'] ) && $_GET['bdocs_view'] === 'classic' ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1664 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1665 + $post_type = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : '';
1666 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1667 + $bdocs_view = isset( $_GET['bdocs_view'] ) ? sanitize_text_field( wp_unslash( $_GET['bdocs_view'] ) ) : '';
1668 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1669 + $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
1670 +
1671 + if ( 'docs' === $post_type && 'classic' === $bdocs_view ) {
1132 1672 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'classic_ui' );
1133 - } elseif ( isset( $_GET['page'] ) && $_GET['page'] === 'betterdocs-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1673 + } elseif ( 'betterdocs-admin' === $page ) {
1134 1674 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'modern_ui' );
1135 1675 }
1136 1676 }
1137 1677
@@ -1141,18 +1681,20 @@
1141 1681 * @return string
1142 1682 * @since 3.0.1
1143 1683 */
1144 1684 public function ui_slug() {
1145 - $last_visited = get_user_meta(get_current_user_id(), 'last_visited_docs_admin_page', true);
1146 - $docs_exist = get_posts([
1147 - 'post_type' => 'docs',
1148 - 'post_status' => 'any',
1149 - 'numberposts' => 1
1150 - ]);
1685 + $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1686 + $docs_exist = get_posts(
1687 + array(
1688 + 'post_type' => 'docs',
1689 + 'post_status' => 'any',
1690 + 'numberposts' => 1,
1691 + )
1692 + );
1151 1693
1152 - return ($last_visited === 'modern_ui' || empty($docs_exist))
1153 - ? 'betterdocs-admin'
1154 - : 'edit.php?post_type=docs&bdocs_view=classic';
1694 + return ( 'modern_ui' === $last_visited || empty( $docs_exist ) )
1695 + ? 'betterdocs-admin'
1696 + : 'edit.php?post_type=docs&bdocs_view=classic';
1155 1697 }
1156 1698
1157 1699 /**
1158 1700 * Resets a duplicate submenu in WordPress if the parent main menu and the first submenu permalink are not the same.
@@ -1162,9 +1704,9 @@
1162 1704 */
1163 1705 public function reset_submenu() {
1164 1706 global $submenu;
1165 1707
1166 - $docs = get_posts( [ 'post_type' => 'docs' ] );
1708 + $docs = get_posts( array( 'post_type' => 'docs' ) );
1167 1709 if ( count( $docs ) == 0 ) {
1168 1710 return;
1169 1711 }
1170 1712
@@ -1169,10 +1711,89 @@
1169 1711 }
1170 1712
1171 1713 $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1172 1714
1173 - if ( $last_visited === 'classic_ui' && isset( $submenu['betterdocs-admin'] ) && in_array( 'betterdocs-admin', $submenu['betterdocs-admin'][0] ) ) {
1715 + if ( 'classic_ui' === $last_visited && isset( $submenu['betterdocs-admin'] ) && in_array( 'betterdocs-admin', $submenu['betterdocs-admin'][0] ) ) {
1174 1716 unset( $submenu['betterdocs-admin'][0] );
1175 1717 $submenu['betterdocs-admin'] = array_values( $submenu['betterdocs-admin'] );
1176 1718 }
1719 + }
1720 +
1721 + /**
1722 + * Initialize Black Friday Pointer
1723 + *
1724 + * @return void
1725 + * @since 3.7.0
1726 + */
1727 + private function init_black_friday_pointer() {
1728 + // Only initialize if conditions are met
1729 + if ( NoticePointers::should_display_notice() ) {
1730 + new NoticePointers();
1731 + }
1732 + }
1733 +
1734 + /**
1735 + * AJAX handler for dismissing Black Friday pointer
1736 + *
1737 + * @return void
1738 + * @since 3.7.0
1739 + */
1740 + public function ajax_dismiss_black_friday_pointer() {
1741 + // Verify nonce
1742 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
1743 + if ( ! wp_verify_nonce( $nonce, 'betterdocs_dismiss_pointer' ) ) {
1744 + wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'betterdocs' ) ) );
1745 + }
1746 +
1747 + // Check if user has permission
1748 + if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'edit_docs' ) ) {
1749 + wp_send_json_error( array( 'message' => __( 'Permission denied', 'betterdocs' ) ) );
1750 + }
1751 +
1752 + // Get the introduction key
1753 + $introduction_key = isset( $_POST['introduction_key'] ) ? sanitize_text_field( wp_unslash( $_POST['introduction_key'] ) ) : '';
1754 +
1755 + if ( empty( $introduction_key ) ) {
1756 + wp_send_json_error( array( 'message' => __( 'Invalid introduction key', 'betterdocs' ) ) );
1757 + }
1758 +
1759 + // Set the introduction as viewed
1760 + NoticePointers::set_introduction_viewed( $introduction_key );
1761 +
1762 + // Clear the priority option so other plugins can set their priority
1763 + delete_option( '_wpdeveloper_plugin_pointer_priority' );
1764 +
1765 + wp_send_json_success( array( 'message' => __( 'Pointer dismissed successfully', 'betterdocs' ) ) );
1766 + }
1767 +
1768 + /**
1769 + * Get count of docs that are not yet synced
1770 + * Similar to Helper::get_not_synced_post_ids() in betterdocs-ai-chatbot plugin
1771 + *
1772 + * @return int
1773 + * @since 3.7.0
1774 + */
1775 + private function get_not_synced_docs_count() {
1776 + $new_post_ids = get_option( 'saved_docs_post_ids', array() );
1777 + $error_posts_data = get_option( 'betterdocs_ai_chatbot_error_posts', array() );
1778 +
1779 + // Extract post IDs from error posts (handle both old and new formats)
1780 + $error_post_ids = array();
1781 + if ( is_array( $error_posts_data ) && ! empty( $error_posts_data ) ) {
1782 + foreach ( $error_posts_data as $key => $value ) {
1783 + if ( is_numeric( $key ) && is_numeric( $value ) ) {
1784 + // Old format: numeric key with post ID as value
1785 + $error_post_ids[] = $value;
1786 + } elseif ( is_numeric( $key ) && is_array( $value ) && isset( $value['post_id'] ) ) {
1787 + // New format: post ID as key with structured data
1788 + $error_post_ids[] = $key;
1789 + } elseif ( is_numeric( $key ) ) {
1790 + // New format: post ID as key
1791 + $error_post_ids[] = $key;
1792 + }
1793 + }
1794 + }
1795 +
1796 + // Merge both arrays and remove duplicates, then count
1797 + return count( array_unique( array_merge( $new_post_ids, $error_post_ids ) ) );
1177 1798 }
1178 1799 }