PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.9
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.9
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 +288 -963 4.9.13.8.9 View file →
@@ -1,62 +1,28 @@
1 1 <?php
2 +
2 3 namespace WPDeveloper\BetterDocs\Core;
3 4
4 -if ( ! defined( 'ABSPATH' ) ) {
5 - exit;
6 -}
7 -
8 -
9 5 use Exception;
10 6 use PriyoMukul\WPNotice\Notices;
11 -use WPDeveloper\BetterDocs\Admin\NoticePointers;
12 7 use WPDeveloper\BetterDocs\Utils\Base;
13 8 use PriyoMukul\WPNotice\Utils\CacheBank;
14 9 use WPDeveloper\BetterDocs\Utils\Helper;
15 10 use WPDeveloper\BetterDocs\Utils\Enqueue;
16 -use WPDeveloper\BetterDocs\Insights\Insights;
11 +use WPDeveloper\BetterDocs\Utils\Insights;
17 12 use PriyoMukul\WPNotice\Utils\NoticeRemover;
18 -use WPDeveloper\BetterDocs\Core\PluginInstaller;
19 13 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
20 14
21 15 class Admin extends Base {
22 16 /**
23 - * Per-user flag recording that this administrator has opened the MCP screen.
24 - *
25 - * Stores the timestamp of the first visit, but only its **presence** is read:
26 - * absent means "this user has not seen MCP yet", which is what puts the
27 - * one-time discovery badge on the menu (ADR-063). Per user on purpose — two
28 - * administrators each get their own first look, and neither clears the other's.
29 - *
30 - * @var string
31 - * @since 4.9.0
32 - */
33 - const MCP_SEEN_META = 'betterdocs_mcp_seen';
34 -
35 - /**
36 - * Whether this request painted the MCP discovery badge onto the menu.
37 - *
38 - * Decided once in `menus()` (on `admin_menu`) and read again in
39 - * `mcp_badge_styles()` (on `admin_head`), rather than re-deciding, because the
40 - * two must agree: on the request that opens the MCP screen the badge is still
41 - * painted while the meta is already written, and re-deciding at `admin_head`
42 - * would leave that one painted pill unstyled.
43 - *
44 - * @var bool
45 - * @since 4.9.0
46 - */
47 - private $mcp_badge = false;
48 -
49 - /**
50 17 * @var CacheBank
51 18 */
52 19 private static $cache_bank;
53 20 /**
54 21 * Admin Root Menu Slug
55 - *
56 22 * @var string
57 23 */
58 - private $slug = 'betterdocs-dashboard';
24 + private $slug = 'betterdocs-admin';
59 25 /**
60 26 * Insights
61 27 *
62 28 * @var Insights
@@ -85,19 +51,14 @@
85 51 private $kbmigration;
86 52
87 53 /**
88 54 * Enqueue
89 - *
90 55 * @var Enqueue
91 56 */
92 57 private $assets;
93 58
94 - // modules
95 - protected $installer;
96 -
97 59 /**
98 60 * FAQBuilder
99 - *
100 61 * @var FAQBuilder
101 62 */
102 63 private $faq_builder;
103 64 private $glossaries;
@@ -106,12 +67,11 @@
106 67 $this->container = $container;
107 68 $this->assets = $assets;
108 69 $this->settings = $settings;
109 70 $this->kbmigration = $kbmigration;
110 - $this->slug = 'betterdocs-dashboard';
71 + $this->slug = 'betterdocs-admin';
111 72
112 - add_action( 'init', array( $type, 'register' ), 9 );
113 - add_action( 'rest_api_init', array( $this, 'order_terms_in_wp_terms_admin_table' ) );
73 + add_action( 'init', [ $type, 'register' ], 9 );
114 74
115 75 $type->init();
116 76 $type->admin_init();
117 77
@@ -117,80 +77,51 @@
117 77
118 78 $this->faq_builder = $this->container->get( FAQBuilder::class );
119 79 $this->glossaries = $this->container->get( Glossaries::class );
120 80
121 - /**
122 - * Register usage tracking (including the daily `put_do_weekly_action` cron
123 - * handler) on every request — WP-Cron runs with is_admin() === false, so
124 - * this MUST sit above the admin guard or the cron send never fires. The
125 - * admin-only UI hooks inside Insights::init() (deactivation form, footer
126 - * scripts, plugin_action_links) are context-specific and simply never run
127 - * outside wp-admin.
128 - */
129 - $this->plugin_insights();
130 -
131 81 if ( ! is_admin() ) {
132 82 return;
133 83 }
134 84
135 - $this->installer = new PluginInstaller();
136 -
137 - add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
138 - // The WPNotice CacheBank wipes all admin_notices at priority 10 on BetterDocs
139 - // screens, so the hook above never renders inside the BetterDocs panels.
140 - // Re-add the compatibility notice after that wipe (in_admin_header, priority
141 - // 999) so it shows on the panels like the review / license notices.
142 - add_action( 'in_admin_header', function () {
143 - $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
144 - if ( $screen && betterdocs()->is_betterdocs_screen( $screen->id ) ) {
145 - add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
146 - }
147 - }, 999 );
85 + $this->plugin_insights();
86 + add_action( 'admin_notices', [ $this, 'compatibility_notices' ] );
148 87 // add_action( 'admin_init', [$this, 'notices'], 9 );
149 - add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
88 + add_filter( 'admin_init', [ $this, 'save_admin_page' ], 99 );
150 89
151 - add_action( 'admin_menu', array( $this, 'menus' ) );
152 - // The badge's clear runs on `admin_init` — a hook that fires for every
153 - // admin request — and identifies the screen by its page slug, rather
154 - // than on `load-{$hook_suffix}` (ADR-065). `admin_init` fires *after*
155 - // `admin_menu`, measured on the rig, so the badge is still painted on
156 - // the request that opens the screen exactly as before.
157 - add_action( 'admin_init', array( $this, 'mark_mcp_seen' ) );
158 - add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
159 - add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
160 - add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) );
161 - add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) );
90 + add_action( 'admin_menu', [ $this, 'menus' ] );
91 + add_action( 'admin_menu', [ $this, 'reset_submenu' ] );
92 + add_action( 'admin_head', [ $this, 'add_custom_classes_to_menu_items' ] );
93 + add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, [ $this, 'insert_plugin_links' ] );
162 94
163 95 // $this->container->get( SetupWizard::class )->init();
164 96
165 - add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
166 - add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
167 - // add_action( 'betterdocs_listing_header', [ $this, 'header' ], 10, 1 );
168 - add_action( 'admin_bar_menu', array( $this, 'toolbar_menu' ), 32 );
97 + add_action( 'admin_enqueue_scripts', [ $this, 'styles' ] );
98 + add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] );
99 + //add_action( 'betterdocs_listing_header', [ $this, 'header' ], 10, 1 );
100 + add_action( 'admin_bar_menu', [ $this, 'toolbar_menu' ], 32 );
169 101
170 - add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
171 - add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
172 - add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
173 - add_filter( 'betterdocs_admin_menu', array( $this, 'quick_setup_menu' ), 10, 1 );
102 + add_filter( 'admin_body_class', [ $this, 'body_classes' ] );
103 + add_filter( 'parent_file', [ $type, 'highlight_admin_menu' ] );
104 + add_filter( 'submenu_file', [ $type, 'highlight_admin_submenu' ], 10, 2 );
105 + add_filter( 'betterdocs_admin_menu', [ $this, 'quick_setup_menu' ], 10, 1 );
174 106
175 107 /**
176 108 * Remove Comments Column from List Table.
177 109 */
178 - add_filter( 'manage_docs_posts_columns', array( $this, 'set_custom_edit_action_columns' ) );
179 - add_filter( 'manage_docs_posts_custom_column', array( $this, 'manage_custom_columns' ), 10, 2 );
110 + add_filter( 'manage_docs_posts_columns', [ $this, 'set_custom_edit_action_columns' ] );
111 + add_filter( 'manage_docs_posts_custom_column', [ $this, 'manage_custom_columns' ], 10, 2 );
180 112
181 113 /**
182 114 * Add New Column
183 115 */
184 - add_filter( 'manage_users_columns', array( $this, 'add_users_total_docs_column' ), 10, 1 );
185 - add_filter( 'manage_users_custom_column', array( $this, 'popular_users_docs_data' ), 10, 3 );
116 + add_filter( 'manage_users_columns', [ $this, 'add_users_total_docs_column' ], 10, 1 );
117 + add_filter( 'manage_users_custom_column', [ $this, 'popular_users_docs_data' ], 10, 3 );
186 118 if ( is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
187 119 add_action( 'admin_footer-plugins.php', array( $this, 'disable_deactivation' ) );
188 120 }
189 121
190 122 if ( $this->settings->get( 'enable_estimated_reading_time' ) ) {
191 - // Hook into unified metabox instead of creating separate metabox
192 - add_action( 'betterdocs_reading_time_tab_content', array( $this, 'render_estimated_time_markup' ) );
123 + add_action( 'add_meta_boxes', [ $this, 'reading_meta_box_' ], 10 );
193 124 }
194 125
195 126 self::$cache_bank = CacheBank::get_instance();
196 127
@@ -201,30 +132,10 @@
201 132 $this->notices();
202 133 } catch ( Exception $e ) {
203 134 unset( $e );
204 135 }
205 -
206 - // Initialize Black Friday Pointer
207 - $this->init_black_friday_pointer();
208 -
209 - // Register AJAX handler for pointer dismissal
210 - add_action( 'wp_ajax_betterdocs_dismiss_black_friday_pointer', array( $this, 'ajax_dismiss_black_friday_pointer' ) );
211 136 }
212 137
213 - public function order_terms_in_wp_terms_admin_table() {
214 - // order the terms correctly to be shown on the admin panel categories menu with betterdocs order
215 - add_action(
216 - 'rest_insert_doc_category',
217 - function ( $term, $request, $bool ) {
218 - $max_order = Helper::get_max_doc_category_order_from_term_meta() ?? 0;
219 - $next_order = $max_order + 1;
220 - update_term_meta( $term->term_id, 'doc_category_order', $next_order );
221 - },
222 - 10,
223 - 3
224 - );
225 - }
226 -
227 138 public function disable_deactivation() {
228 139 $tooltip_text = esc_html__( 'Deactivate BetterDocs Pro First', 'betterdocs' );
229 140 ?>
230 141 <style type="text/css">
@@ -269,17 +180,17 @@
269 180 <?php
270 181 }
271 182
272 183 public function add_users_total_docs_column( $columns ) {
273 - $new_column = array(
274 - 'docs' => __( 'Docs', 'betterdocs' ),
275 - );
184 + $new_column = [
185 + 'docs' => __( 'Docs', 'betterdocs' )
186 + ];
276 187 $columns = array_merge( $columns, $new_column );
277 188 return $columns;
278 189 }
279 190
280 191 public function popular_users_docs_data( $output, $column_name, $user_id ) {
281 - if ( 'docs' == $column_name ) {
192 + if ( $column_name == 'docs' ) {
282 193 $total_count = count_user_posts( $user_id, 'docs', true );
283 194 return '<a href="edit.php?post_type=docs&author=' . $user_id . '" class="edit"><span aria-hidden="true">' . $total_count . '</span></a>';
284 195 }
285 196 return $output;
@@ -284,8 +195,20 @@
284 195 }
285 196 return $output;
286 197 }
287 198
199 + public function reading_meta_box_() {
200 + add_meta_box(
201 + 'betterdocs_estimated_time_metabox',
202 + __( 'Estimated Reading Time', 'betterdocs' ),
203 + [
204 + $this,
205 + 'render_estimated_time_markup'
206 + ],
207 + 'docs'
208 + );
209 + }
210 +
288 211 public function render_estimated_time_markup() {
289 212 betterdocs()->views->get( 'admin/metabox/estimated-reading-box' );
290 213 }
291 214
@@ -293,15 +216,13 @@
293 216 if ( betterdocs()->is_pro_active() ) {
294 217 $plugins = Helper::get_plugins();
295 218 $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php'];
296 219
297 - // Require the paired Pro release: the Analytics UI is version-coupled to
298 - // Pro's advanced modules, so an older Pro renders a broken/partial panel.
299 - if ( isset( $plugin_data['Version'] ) && version_compare( $plugin_data['Version'], '4.0.0', '>=' ) ) {
220 + if ( isset( $plugin_data['Version'] ) && version_compare( $plugin_data['Version'], '2.5.0', '>=' ) ) {
300 221 return;
301 222 }
302 223
303 - betterdocs()->views->get( 'admin/notices/compatibility', array( 'version' => $plugin_data['Version'] ) );
224 + betterdocs()->views->get( 'admin/notices/compatibility', [ 'version' => $plugin_data['Version'] ] );
304 225 }
305 226 }
306 227
307 228 public function plugin_insights( $prevent_init = false ) {
@@ -306,20 +227,20 @@
306 227
307 228 public function plugin_insights( $prevent_init = false ) {
308 229 $this->insights = Insights::get_instance(
309 230 BETTERDOCS_PLUGIN_FILE,
310 - array(
231 + [
311 232 'opt_in' => true,
312 233 'goodbye_form' => true,
313 - 'item_id' => 'c7b16777b4f1b83f6083',
314 - )
234 + 'item_id' => 'c7b16777b4f1b83f6083'
235 + ]
315 236 );
316 237
317 238 $this->insights->set_notice_options(
318 - array(
239 + [
319 240 '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' ),
320 - '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' ),
321 - )
241 + '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' )
242 + ]
322 243 );
323 244
324 245 if ( ! $prevent_init ) {
325 246 $this->insights->init();
@@ -335,77 +256,76 @@
335 256 * @throws Exception
336 257 */
337 258 public function notices() {
338 259 $notices = new Notices(
339 - array(
260 + [
340 261 'id' => 'betterdocs',
341 262 'storage_key' => 'notices',
342 263 'lifetime' => 3,
343 264 'stylesheet_url' => $this->assets->asset_url( 'admin/css/notices.css' ),
344 265 'styles' => $this->assets->asset_url( 'admin/css/notices.css' ),
345 - 'priority' => 4,
346 - )
266 + 'priority' => 4
267 + ]
347 268 );
348 269
349 270 /**
350 - * Review Notice
351 - *
352 - * @var mixed $message
353 - */
271 + * Review Notice
272 + * @var mixed $message
273 + */
354 274
355 275 $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' );
356 276
357 - $_review_notice = array(
277 + $_review_notice = [
358 278 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
359 279 'html' => '<p>' . $message . '</p>',
360 - 'links' => array(
361 - 'later' => array(
280 + 'links' => [
281 + 'later' => [
362 282 'link' => 'https://wordpress.org/plugins/betterdocs/#reviews',
363 283 'target' => '_blank',
364 284 'label' => __( 'Sure, you deserve it!', 'betterdocs' ),
365 - 'icon_class' => 'dashicons dashicons-external',
366 - ),
367 - 'allready' => array(
285 + 'icon_class' => 'dashicons dashicons-external'
286 + ],
287 + 'allready' => [
368 288 'label' => __( 'I already did', 'betterdocs' ),
369 289 'icon_class' => 'dashicons dashicons-smiley',
370 - 'attributes' => array(
371 - 'data-dismiss' => true,
372 - ),
373 - ),
374 - 'maybe_later' => array(
290 + 'attributes' => [
291 + 'data-dismiss' => true
292 + ]
293 + ],
294 + 'maybe_later' => [
375 295 'label' => __( 'Maybe Later', 'betterdocs' ),
376 296 'icon_class' => 'dashicons dashicons-calendar-alt',
377 - 'attributes' => array(
297 + 'attributes' => [
378 298 'data-later' => true,
379 - 'class' => 'dismiss-btn',
380 - ),
381 - ),
382 - 'support' => array(
299 + 'class' => 'dismiss-btn'
300 + ]
301 + ],
302 + 'support' => [
383 303 'link' => 'https://wpdeveloper.com/support',
384 - 'attributes' => array(
385 - 'target' => '_blank',
386 - ),
304 + 'attributes' => [
305 + 'target' => '_blank'
306 + ],
387 307 'label' => __( 'I need help', 'betterdocs' ),
388 - 'icon_class' => 'dashicons dashicons-sos',
389 - ),
390 - 'never_show_again' => array(
308 + 'icon_class' => 'dashicons dashicons-sos'
309 + ],
310 + 'never_show_again' => [
391 311 'label' => __( 'Never show again', 'betterdocs' ),
392 312 'icon_class' => 'dashicons dashicons-dismiss',
393 - 'attributes' => array(
394 - 'data-dismiss' => true,
395 - ),
396 - ),
397 - ),
398 - );
313 + 'attributes' => [
314 + 'data-dismiss' => true
315 + ]
316 + ]
317 + ]
318 + ];
399 319
400 320 $notices->add(
401 321 'review',
402 322 $_review_notice,
403 - array(
323 + [
404 324 'start' => $notices->strtotime( '+10 days' ),
405 325 'recurrence' => 30,
406 - 'dismissible' => true,
407 - )
326 + 'dismissible' => true
327 + ]
408 328 );
409 329
410 330 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
411 331 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
@@ -410,9 +330,9 @@
410 330 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
411 331 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
412 332
413 333 $message = sprintf(
414 - /* translators: %s is the name of the existing knowledge base plugin. */
334 + /* translators: %s is the name of the existing knowledge base plugin. */
415 335 __( 'Already using %s? Power up your Knowledge Base by migrating all your docs and settings to BetterDocs with just 1 click.', 'betterdocs' ),
416 336 esc_html( $plugin_name )
417 337 );
418 338
@@ -422,51 +342,50 @@
422 342 esc_url( admin_url( 'admin.php?page=betterdocs-settings&tab=tab-migration' ) ),
423 343 esc_html__( 'Start Migration', 'betterdocs' )
424 344 );
425 345
426 - $_migration_notice = array(
346 + $_migration_notice = [
427 347 'thumbnail' => '',
428 348 'html' => $migration_message,
429 - 'links' => array(
430 - 'maybe_later' => array(
349 + 'links' => [
350 + 'maybe_later' => [
431 351 'label' => __( 'Maybe Later', 'betterdocs' ),
432 352 'icon_class' => 'dashicons dashicons-calendar-alt',
433 - 'attributes' => array(
353 + 'attributes' => [
434 354 'data-later' => true,
435 - 'class' => 'dismiss-btn',
436 - ),
437 - ),
438 - 'never_show_again' => array(
355 + 'class' => 'dismiss-btn'
356 + ]
357 + ],
358 + 'never_show_again' => [
439 359 'label' => __( 'Never show again', 'betterdocs' ),
440 360 'icon_class' => 'dashicons dashicons-dismiss',
441 - 'attributes' => array(
442 - 'data-dismiss' => true,
443 - ),
444 - ),
445 - ),
446 - );
361 + 'attributes' => [
362 + 'data-dismiss' => true
363 + ]
364 + ]
365 + ]
366 + ];
447 367
448 368 $notices->add(
449 369 'migration',
450 370 $_migration_notice,
451 - array(
371 + [
452 372 'start' => $notices->time(),
453 373 'recurrence' => false,
454 - 'dismissible' => true,
455 - )
374 + 'dismissible' => true
375 + ]
456 376 );
457 377 }
458 378
459 379 /**
460 - *
461 380 * Opt-In Notice
462 381 */
463 382 $allow_tracking = get_option( 'wpins_allow_tracking' );
464 - if ( null != $this->insights && ! isset( $allow_tracking['betterdocs'] ) ) {
383 + if ( $this->insights != null && ! isset( $allow_tracking['betterdocs'] ) ) {
465 384 $notices->add(
466 385 'opt_in',
467 - array( $this->insights, 'notice' ),
468 - array(
386 + [ $this->insights, 'notice' ],
387 + [
469 388 'classes' => 'updated put-dismiss-notice',
470 389 'start' => $notices->time(),
471 390 'refresh' => BETTERDOCS_VERSION,
472 391 'dismissible' => true,
@@ -471,57 +390,19 @@
471 390 'refresh' => BETTERDOCS_VERSION,
472 391 'dismissible' => true,
473 392 'do_action' => 'wpdeveloper_notice_clicked_for_betterdocs',
474 393 'display_if' => ! function_exists( 'betterdocs_pro' ),
475 - 'screens' => array( 'dashboard' ),
476 - )
394 + 'screens' => [ 'dashboard' ]
395 + ]
477 396 );
478 397 }
479 398
480 - $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>';
481 - $_summer_campaign_notice = array(
482 - 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
483 - 'html' => $summer_campaign_message,
484 - 'links' => array(
485 - 'support' => array(
486 - 'link' => 'https://betterdocs.co/summer2026-admin-notice',
487 - 'attributes' => array(
488 - 'target' => '_blank',
489 - 'class' => 'offer-button',
490 - ),
491 - 'label' => __( 'Upgrade To PRO Now', 'betterdocs' ),
492 - ),
493 - 'maybe_later' => array(
494 - 'label' => __( 'I’ll Grab It Later', 'betterdocs' ),
495 - 'attributes' => array(
496 - 'target' => '_blank',
497 - 'data-later' => true,
498 - 'class' => 'dismiss-btn',
499 - ),
500 - ),
501 - ),
502 - );
503 -
504 - $notices->add(
505 - 'summer-campaign-26',
506 - $_summer_campaign_notice,
507 - array(
508 - 'start' => $notices->time(),
509 - 'recurrence' => false,
510 - 'dismissible' => true,
511 - 'refresh' => BETTERDOCS_VERSION,
512 - 'expire' => strtotime( '11:59:59pm June 25, 2026' ),
513 - 'display_if' => ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ),
514 - )
515 - );
516 -
517 399 self::$cache_bank->create_account( $notices );
518 400 self::$cache_bank->calculate_deposits( $notices );
519 401 if ( method_exists( self::$cache_bank, 'clear_notices_in_' ) ) {
520 402 self::$cache_bank->clear_notices_in_(
521 - array(
522 - 'toplevel_page_betterdocs-dashboard',
523 - 'admin_page_betterdocs-admin',
403 + [
404 + 'toplevel_page_betterdocs-admin',
524 405 'betterdocs_page_betterdocs-admin',
525 406 'betterdocs_page_betterdocs-settings',
526 407 'betterdocs_page_betterdocs-faq',
527 408 'betterdocs_page_betterdocs-analytics',
@@ -526,14 +407,11 @@
526 407 'betterdocs_page_betterdocs-faq',
527 408 'betterdocs_page_betterdocs-analytics',
528 409 'betterdocs_page_betterdocs-glossaries',
529 410 'betterdocs_page_betterdocs-ai-chatbot',
530 - 'betterdocs_page_betterdocs-api-docs',
531 - 'betterdocs_page_betterdocs-doc-categories',
532 - 'betterdocs_page_betterdocs-doc-tags',
533 411 'edit-doc_category',
534 - 'edit-doc_tag',
535 - ),
412 + 'edit-doc_tag'
413 + ],
536 414 $notices,
537 415 true
538 416 );
539 417 }
@@ -538,92 +416,30 @@
538 416 );
539 417 }
540 418 }
541 419
542 - /**
543 - * Resolve the admin dark-mode preference.
544 - *
545 - * The mode switcher stores the choice in a client cookie (no DB write, shared
546 - * across every admin screen). Fall back to the legacy
547 - * `betterdocs_settings['dark_mode']` value for installs that set it before this
548 - * change and haven't toggled since.
549 - *
550 - * @return bool
551 - */
552 - public function is_dark_mode() {
553 - if ( isset( $_COOKIE['betterdocs_admin_dark_mode'] ) ) {
554 - return '1' === $_COOKIE['betterdocs_admin_dark_mode']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
555 - }
556 -
557 - $saved = get_option( 'betterdocs_settings', array() );
558 - return ! empty( $saved['dark_mode'] );
559 - }
560 -
561 - /**
562 - * Whether the knowledge base is genuinely empty (no doc categories and no
563 - * non-trash docs). Localized to the admin so the All Docs panel can render a
564 - * skeleton shaped like the "No Category Found" empty card on first paint —
565 - * instead of a category/docs skeleton it would immediately replace — without
566 - * waiting for the REST fetch to reveal the count.
567 - *
568 - * @return bool
569 - */
570 - public function kb_is_empty() {
571 - $cats = wp_count_terms( array( 'taxonomy' => 'doc_category', 'hide_empty' => false ) );
572 - $cats = is_wp_error( $cats ) ? 0 : (int) $cats;
573 - if ( $cats > 0 ) {
574 - return false;
575 - }
576 -
577 - $counts = (array) wp_count_posts( 'docs' );
578 - $total = 0;
579 - foreach ( array( 'publish', 'future', 'draft', 'pending', 'private' ) as $status ) {
580 - $total += isset( $counts[ $status ] ) ? (int) $counts[ $status ] : 0;
581 - }
582 -
583 - return 0 === $total;
584 - }
585 -
586 420 public function body_classes( $classes ) {
587 - $dark_mode = $this->is_dark_mode();
588 - $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', str_replace( 'admin_page_', '', get_current_screen()->id ) ) ) : '';
589 - /**
590 - * Filter the list of (prefix-stripped) screen ids that receive the
591 - * `betterdocs-admin` body class (and dark-mode class). Pro/add-ons can
592 - * register their own React admin pages, e.g. the Knowledge Base page.
593 - *
594 - * @param string[] $registered_screens Screen ids with the page prefix removed.
595 - */
596 - $registered_screens = apply_filters( 'betterdocs_admin_screen_slugs', array(
597 - 'betterdocs-settings',
598 - 'betterdocs-admin',
599 - 'betterdocs-dashboard',
600 - 'betterdocs-analytics',
601 - 'betterdocs-glossaries',
602 - 'betterdocs-faq',
603 - 'betterdocs-doc-categories',
604 - 'betterdocs-doc-tags',
605 - 'edit-doc_category',
606 - 'edit-doc_tag',
607 - 'edit-knowledge_base',
608 - 'betterdocs-ai-chatbot',
609 - 'betterdocs-api-docs',
610 - // Without this the MCP screen never receives `betterdocs-admin`, and
611 - // the design tokens' dark-mode overrides — which are declared on
612 - // `.betterdocs-admin.betterdocs-dark-mode` — can never apply there:
613 - // the switcher in the header flips the cookie and the page stays
614 - // light. @since 4.9.0
615 - 'betterdocs-mcp',
616 - ) );
421 + $saved_settings = get_option( 'betterdocs_settings', false );
422 + $dark_mode = isset( $saved_settings['dark_mode'] ) ? $saved_settings['dark_mode'] : false;
423 + $dark_mode = ! empty( $dark_mode ) ? boolval( $dark_mode ) : false;
424 + $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', get_current_screen()->id ) ) : '';
425 + $registered_screens = [
426 + 'betterdocs-settings',
427 + 'betterdocs-admin',
428 + 'betterdocs-analytics',
429 + 'betterdocs-glossaries',
430 + 'betterdocs-faq',
431 + 'edit-doc_category',
432 + 'edit-doc_tag',
433 + 'edit-knowledge_base',
434 + 'betterdocs-ai-chatbot'
435 + ];
617 436
618 - if ( in_array( $current_screen_id, $registered_screens ) ) {
619 - $classes .= ' betterdocs-admin ';
620 - }
437 + if( in_array( $current_screen_id, $registered_screens ) ) {
438 + $classes .= ' betterdocs-admin ';
439 + }
621 440
622 - // Dark mode also applies on the Quick Setup wizard, whose self-scoped chrome
623 - // keys off `.betterdocs_page_betterdocs-setup.betterdocs-dark-mode`.
624 - $dark_screens = array_merge( $registered_screens, array( 'betterdocs-setup' ) );
625 - if ( $dark_mode && in_array( $current_screen_id, $dark_screens, true ) ) {
441 + if ( $dark_mode === true && in_array( $current_screen_id, $registered_screens ) ) {
626 442 $classes .= ' betterdocs-dark-mode ';
627 443 }
628 444
629 445 return $classes;
@@ -638,11 +454,11 @@
638 454 * @since 1.0.0
639 455 */
640 456 public function set_custom_edit_action_columns( $columns ) {
641 457 unset( $columns['comments'] );
642 - $new_columns = array();
458 + $new_columns = [];
643 459 foreach ( $columns as $key => $value ) {
644 - if ( 'date' == $key ) {
460 + if ( $key == 'date' ) {
645 461 $new_columns['betterdocs_word_count'] = __( 'Word Count', 'betterdocs' ); // put the tags column before it
646 462 $new_columns['betterdocs_reaction'] = __( 'Reactions', 'betterdocs' );
647 463 }
648 464 $new_columns[ $key ] = $value;
@@ -654,29 +470,26 @@
654 470 public function manage_custom_columns( $column, $post_id ) {
655 471 global $wpdb;
656 472 switch ( $column ) {
657 473 case 'betterdocs_word_count':
658 - $content_without_html_tags = trim( wp_strip_all_tags( get_post_field( 'post_content', $post_id ) ) );
474 + $content_without_html_tags = trim( strip_tags( get_post_field( 'post_content', $post_id ) ) );
659 475 preg_match_all( '/<[^>]*>|[\p{L}\p{M}]+/u', $content_without_html_tags, $matches );
660 - $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( array() );
476 + $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( [] );
661 477 $word_count = $total_words;
662 478 echo '<span>' . esc_html( intval( $word_count ) ) . '</span>';
663 479 break;
664 480 case 'betterdocs_reaction':
665 - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- per-post analytics aggregation rendered in admin list table; cache would mask live reactions.
481 + $where = "WHERE post_id='" . esc_sql( $post_id ) . "'";
666 482 $analytics = $wpdb->get_results(
667 - $wpdb->prepare(
668 - "SELECT
669 - sum(impressions) as totalViews,
670 - sum(unique_visit) as totalUniqueViews,
671 - sum(happy + sad + normal) as totalReactions,
672 - sum(happy) as totalHappy,
673 - sum(normal) as totalNormal,
674 - sum(sad) as totalSad
675 - FROM {$wpdb->prefix}betterdocs_analytics
676 - WHERE post_id = %d",
677 - $post_id
678 - )
483 + "SELECT
484 + sum(impressions) as totalViews,
485 + sum(unique_visit) as totalUniqueViews,
486 + sum(happy + sad + normal) as totalReactions,
487 + sum(happy) as totalHappy,
488 + sum(normal) as totalNormal,
489 + sum(sad) as totalSad
490 + FROM {$wpdb->prefix}betterdocs_analytics
491 + $where"
679 492 );
680 493
681 494 echo '<ul class="reactions-count">
682 495 <li>
@@ -732,22 +545,22 @@
732 545 * @return void
733 546 * @since 1.0.0
734 547 */
735 548 public function styles( $hook ) {
736 - $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', array(), 'all' );
549 + $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', [], 'all' );
737 550
738 551 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
739 552 return;
740 553 }
741 554
742 - $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', array(), 'all' );
743 - $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', array(), 'all' );
744 - $this->assets->enqueue( 'betterdocs-old', 'admin/css/betterdocs.css', array(), 'all' );
555 + $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', [], 'all' );
556 + $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', [], 'all' );
557 + $this->assets->enqueue( 'betterdocs-old', 'admin/css/betterdocs.css', [], 'all' );
745 558
746 559 /**
747 - * This scripts enqueued for Dashboard App.
748 - */
749 - $this->assets->enqueue( 'betterdocs', 'admin/css/dashboard.css', array( 'betterdocs-old' ), '', BETTERDOCS_VERSION );
560 + * This scripts enqueued for Dashboard App.
561 + */
562 + $this->assets->enqueue( 'betterdocs', 'admin/css/dashboard.css', [ 'betterdocs-old' ] );
750 563 $this->assets->enqueue( 'betterdocs-icons', 'admin/btd-icon/style.css' );
751 564 }
752 565
753 566 /**
@@ -758,70 +571,26 @@
758 571 * @return void
759 572 * @since 1.0.0
760 573 */
761 574 public function scripts( $hook ) {
762 - // Classic-UI screens that should offer a "Switch to BetterDocs UI"
763 - // button: All Docs, FAQ list, FAQ groups, Product FAQ groups,
764 - // Doc Categories, Doc Tags. Maps each to the React admin page to
765 - // return to; $switch_args carries extra query args (e.g. the FAQ
766 - // Builder tab) appended to the React page URL.
767 - $switch_page = '';
768 - $switch_args = array();
769 - if ( 'edit.php' === $hook && 'docs' === get_post_type() ) {
770 - $switch_page = 'betterdocs-admin';
771 - } elseif ( 'edit.php' === $hook && 'betterdocs_faq' === get_post_type() ) {
772 - $switch_page = 'betterdocs-faq';
773 - } elseif ( 'edit-tags.php' === $hook ) {
774 - $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
775 - $taxonomy = $screen && ! empty( $screen->taxonomy )
776 - ? $screen->taxonomy
777 - : ( isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
778 - if ( 'betterdocs_faq_category' === $taxonomy ) {
779 - $switch_page = 'betterdocs-faq';
780 - } elseif ( 'betterdocs_product_faq_category' === $taxonomy ) {
781 - // Product FAQ groups live on the FAQ Builder's WooCommerce tab.
782 - $switch_page = 'betterdocs-faq';
783 - $switch_args = array( 'faq_tab' => 'woocommerce' );
784 - } elseif ( 'doc_category' === $taxonomy ) {
785 - $switch_page = 'betterdocs-doc-categories';
786 - } elseif ( 'doc_tag' === $taxonomy ) {
787 - $switch_page = 'betterdocs-doc-tags';
788 - }
789 - }
790 -
791 - /**
792 - * Allow Pro/add-ons to map their own classic-UI taxonomy screens to a
793 - * React admin page for the "Switch to BetterDocs UI" button — e.g. the
794 - * Knowledge Base taxonomy, which only exists when Pro is active.
795 - *
796 - * @param string $switch_page React page slug, or '' for no switcher.
797 - * @param string $hook Current admin page hook.
798 - */
799 - $switch_page = apply_filters( 'betterdocs_classic_switch_page', $switch_page, $hook );
800 -
801 - if ( $switch_page ) {
575 + if ( ( $hook === 'edit.php' ) && get_post_type() == 'docs' ) {
802 576 $this->assets->enqueue(
803 577 'betterdocs-switcher',
804 578 'admin/js/switcher.js',
805 - array(
806 - 'jquery',
807 - )
579 + [
580 + 'jquery'
581 + ]
808 582 );
809 583
810 584 $this->assets->localize(
811 585 'betterdocs-switcher',
812 586 'betterdocsSwitcher',
813 - array(
587 + [
814 588 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
815 - 'page' => $switch_page,
816 - 'url' => add_query_arg(
817 - array_merge( array( 'page' => $switch_page ), $switch_args ),
818 - admin_url( 'admin.php' )
819 - ),
820 589 'site_address' => get_bloginfo( 'url' ),
821 590 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
822 - 'betterdocs_pro_version' => betterdocs()->pro_version(),
823 - )
591 + 'betterdocs_pro_version' => betterdocs()->pro_version()
592 + ]
824 593 );
825 594
826 595 return;
827 596 }
@@ -831,122 +600,78 @@
831 600 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
832 601 return;
833 602 }
834 603
835 - wp_enqueue_media(); // load early to fix problems with media upload issues on settings for WordPress 6.0.9
836 604 $this->assets->register( 'betterdocs-admin', 'admin/js/dashboard.js' );
837 605
838 606 $saved_settings = get_option( 'betterdocs_settings', false );
839 - $dark_mode = $this->is_dark_mode();
607 + $dark_mode = $saved_settings['dark_mode'] ?? false;
608 + $dark_mode = ! empty( $dark_mode ) && boolval( $dark_mode );
840 609 $this->assets->localize(
841 610 'betterdocs-admin',
842 611 'betterdocs_admin',
843 - array(
612 + [
844 613 'ajaxurl' => admin_url( 'admin-ajax.php' ),
845 614 'doc_cat_order_nonce' => wp_create_nonce( 'doc_cat_order_nonce' ),
846 615 'knowledge_base_order_nonce' => wp_create_nonce( 'knowledge_base_order_nonce' ),
847 - 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- pagination read from URL.
848 - 'per_page_id' => 'edit_doc_category_per_page',
849 - 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
850 - 'dark_mode' => $dark_mode,
851 - 'kb_is_empty' => $this->kb_is_empty(),
852 - 'text' => __( 'Copied!', 'betterdocs' ),
853 - 'test_report' => __( 'Test Report!', 'betterdocs' ),
854 - 'sending' => __( 'Sending...', 'betterdocs' ),
855 - 'dir_url' => BETTERDOCS_ABSURL,
856 - 'rest_url' => esc_url_raw( rest_url() ),
857 - 'free_version' => betterdocs()->version,
858 - 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
859 - 'ai_sample_docs' => array(
860 - 'enabled' => (bool) betterdocs()->settings->get( 'enable_ai_sample_docs', true ),
861 - 'rest_base' => esc_url_raw( get_rest_url( null, '/betterdocs/v1/sample-docs' ) ),
862 - ),
863 - 'nonce' => wp_create_nonce( 'wp_rest' ),
864 - 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
865 - 'count_all_docs' => array_sum( (array) wp_count_posts( 'docs' ) ),
866 - 'count_all_faq' => array_sum( (array) wp_count_posts( 'betterdocs_faq' ) ),
867 - 'faq_order' => get_option( 'betterdocs_faq_order', 'default' ),
868 - 'count_new_docs' => $this->get_not_synced_docs_count(),
869 - 'admin_url' => admin_url(),
870 - 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
871 - 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
872 - 'previewMode' => betterdocs()->settings->get( 'ia_enable_preview', false ),
873 - 'dashboard_mode' => get_option( 'dashboard_mode' ),
874 - 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
875 - 'betterdocs_pro_version' => betterdocs()->pro_version(),
876 - 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
877 - 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
878 - 'api_docs_teaser' => betterdocs()->show_api_docs_teaser(),
879 - 'glossaries_teaser' => betterdocs()->show_glossary_teaser(),
880 - 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
881 - 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
882 - 'current_admin_language' => Helper::get_current_admin_language(),
883 - 'is_multilingual' => Helper::is_multilingual_active(),
884 - 'languages' => Helper::get_admin_languages(),
885 - /**
886 - * MCP page bootstrap. `abilities_api_available` decides whether the
887 - * page offers a connection at all: without the Abilities API there
888 - * is no tool catalog, so an AI client would connect and find
889 - * nothing. `enabled` is only the initial paint — the toggle owns
890 - * the value from then on.
891 - *
892 - * @since 4.9.0
893 - */
894 - 'mcp' => array(
895 - 'abilities_api_available' => function_exists( 'wp_register_ability' ),
896 - 'enabled' => (bool) betterdocs()->settings->get( 'enable_mcp', false ),
897 - 'rest' => 'betterdocs/v1',
898 - ),
899 - )
616 + 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing
617 + 'per_page_id' => 'edit_doc_category_per_page',
618 + 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
619 + 'dark_mode' => $dark_mode,
620 + 'text' => __( 'Copied!', 'betterdocs' ),
621 + 'test_report' => __( 'Test Report!', 'betterdocs' ),
622 + 'sending' => __( 'Sending...', 'betterdocs' ),
623 + 'dir_url' => BETTERDOCS_ABSURL,
624 + 'rest_url' => esc_url_raw( rest_url() ),
625 + 'free_version' => betterdocs()->version,
626 + 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
627 + 'nonce' => wp_create_nonce( 'wp_rest' ),
628 + 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
629 + 'count_all_docs' => wp_count_posts('docs')->publish,
630 + 'count_new_docs' => count(get_option('saved_docs_post_ids', [])),
631 + 'admin_url' => admin_url(),
632 + 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
633 + 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
634 + 'previewMode' => betterdocs()->settings->get( 'ia_enable_preview', false ),
635 + 'dashboard_mode' => get_option( 'dashboard_mode' ),
636 + 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
637 + 'betterdocs_pro_version' => betterdocs()->pro_version(),
638 + 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
639 + 'disabled_embed_model_option'=> get_option('disabled_embed_model_option'),
640 + ]
900 641 );
901 642
902 643 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
903 644 if ( ! wp_script_is( 'wp-date', 'registered' ) ) {
904 - $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', array() );
645 + $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', [] );
905 646 }
906 647 wp_enqueue_script( 'betterdocs-admin' );
907 648
908 649 /**
909 - * Duplicate Codes Need to Be Removed From Here Onwards
910 - */
650 + * Duplicate Codes Need to Be Removed From Here Onwards
651 + */
911 652
912 - // FAQ Builder Related Localization
653 + //FAQ Builder Related Localization
913 654 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/css/faq.css' );
914 655 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/js/faq.js' );
915 656
916 - // Load the classic editor (TinyMCE + QuickTags) so the FAQ rich-text editor can mount via wp.editor.initialize().
917 - if ( function_exists( 'wp_enqueue_editor' ) ) {
918 - wp_enqueue_editor();
919 - }
920 - if ( function_exists( 'wp_enqueue_media' ) ) {
921 - wp_enqueue_media();
922 - }
923 -
924 657 // removing emoji support
925 658 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
926 659 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
927 660
928 - // Get settings and remove unnecessary keys
929 - $betterdocs_settings = get_option( 'betterdocs_settings', false );
930 - if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
931 - foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
932 - unset( $betterdocs_settings[ $sensitive_key ] );
933 - }
934 - }
935 -
936 661 betterdocs()->assets->localize(
937 662 'betterdocs-admin-faq',
938 663 'betterdocsFaq',
939 - array(
664 + [
940 665 'dir_url' => BETTERDOCS_ABSURL,
941 666 'rest_url' => esc_url_raw( rest_url() ),
942 667 'free_version' => betterdocs()->version,
943 668 'nonce' => wp_create_nonce( 'wp_rest' ),
944 - 'betterdocs_settings' => $betterdocs_settings,
945 - )
669 + 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
670 + ]
946 671 );
947 672
948 - // Glossaries Related Localization
673 + //Glossaries Related Localization
949 674 betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
950 675
951 676 betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
952 677
@@ -952,15 +677,15 @@
952 677
953 678 betterdocs()->assets->localize(
954 679 'betterdocs-admin-glossaries',
955 680 'betterdocsGlossary',
956 - array(
681 + [
957 682 'dir_url' => BETTERDOCS_ABSURL,
958 683 'rest_url' => esc_url_raw( rest_url() ),
959 684 'free_version' => betterdocs()->version,
960 685 'nonce' => wp_create_nonce( 'wp_rest' ),
961 - 'betterdocs_settings' => $betterdocs_settings,
962 - )
686 + 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
687 + ]
963 688 );
964 689 }
965 690
966 691 /**
@@ -969,31 +694,31 @@
969 694 * @return void
970 695 * @since 1.0.0
971 696 */
972 697 public function header( $admin_tab_name ) {
973 - $quick_links = array(
698 + $quick_links = [
974 699 'switch_view' => sprintf(
975 700 '<a href="%s" class="betterdocs-button betterdocs-button-secondary">%s</a>',
976 701 add_query_arg(
977 - array(
702 + [
978 703 'post_type' => 'docs',
979 - 'bdocs_view' => 'classic',
980 - ),
704 + 'bdocs_view' => 'classic'
705 + ],
981 706 'edit.php'
982 707 ),
983 708 __( 'Switch to Classic UI', 'betterdocs' )
984 709 ),
985 - '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' ) ),
986 - );
710 + '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' ) )
711 + ];
987 712
988 713 $quick_links = apply_filters( 'betterdocs_quick_links', $quick_links );
989 714
990 715 betterdocs()->views->get(
991 716 'admin/header',
992 - array(
717 + [
993 718 'quick_links' => $quick_links,
994 - 'active_tab' => $admin_tab_name,
995 - )
719 + 'active_tab' => $admin_tab_name
720 + ]
996 721 );
997 722 }
998 723
999 724 /**
@@ -1001,279 +726,41 @@
1001 726 *
1002 727 * @return void
1003 728 * @since 1.0.0
1004 729 */
730 +
1005 731 public function menus() {
1006 - $default_args = array(
732 + $default_args = [
1007 733 'page_title' => 'BetterDocs',
1008 734 'menu_title' => 'BetterDocs',
1009 - 'capability' => 'edit_docs', // Unified capability
735 + 'capability' => 'edit_posts',
1010 736 'menu_slug' => $this->slug,
1011 - 'callback' => array( $this, 'output' ),
737 + 'callback' => [ $this, 'output' ],
1012 738 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
1013 - 'position' => 5,
1014 - );
739 + 'position' => 5
740 + ];
1015 741
1016 742 $_menu_position = 5;
1017 - global $submenu;
1018 743
1019 - // Always register both UI endpoints
1020 - $this->register_modern_ui_fallback();
1021 -
1022 - // The one-time MCP discovery badge (ADR-063). Decided once, here, and
1023 - // remembered for `mcp_badge_styles()`: the pill's markup and the pill's
1024 - // stylesheet have to be printed on the same requests as each other.
1025 - $this->mcp_badge = self::should_flag_mcp();
1026 -
1027 744 foreach ( $this->menu_list() as $key => $value ) {
1028 - if ( 'betterdocs' === $key ) {
745 + if ( $key === 'betterdocs' ) {
1029 746 $callable = 'add_menu_page';
1030 747 $value = wp_parse_args( $value, $default_args );
1031 - call_user_func_array( $callable, $value );
1032 748 } else {
1033 - $is_core_page = strpos( $value['menu_slug'], '?' ) !== false;
1034 -
1035 - if ( $is_core_page ) {
1036 - // Add classic UI directly
1037 - $submenu[ $this->slug ][] = array(
1038 - $value['menu_title'],
1039 - $value['capability'],
1040 - $value['menu_slug'],
1041 - $value['page_title'],
1042 - );
1043 - } else {
1044 - // Add modern UI through WordPress API
1045 - add_submenu_page(
1046 - $this->slug,
1047 - $value['page_title'],
1048 - $value['menu_title'],
1049 - $value['capability'],
1050 - $value['menu_slug'],
1051 - $value['callback']
1052 - );
1053 - }
1054 - ++$_menu_position;
749 + $callable = 'add_submenu_page';
750 + $default_args['callback'] = '';
751 + $default_args['position'] = $_menu_position;
752 + unset( $default_args['icon_url'] );
753 + $value = wp_parse_args( $value, array_merge( [ 'parent_slug' => $this->slug ], $default_args ) );
1055 754 }
1056 - }
1057 755
1058 - $this->paint_mcp_badge();
1059 - }
756 + ++$_menu_position;
1060 757
1061 - /**
1062 - * Append the discovery badge to the registered menu titles.
1063 - *
1064 - * **After** registration, editing `$menu` / `$submenu` in place — the same
1065 - * shape `add_custom_classes_to_menu_items()` uses — and never by passing a
1066 - * decorated title to `add_menu_page()`. That distinction is not cosmetic:
1067 - * core stores `sanitize_title( $menu_title )` as `$admin_page_hooks[ $slug ]`
1068 - * (`wp-admin/includes/plugin.php:1397`) and builds every child page's hook
1069 - * suffix from it (`get_plugin_page_hookname()`), so markup in the parent's
1070 - * title renames `betterdocs_page_betterdocs-mcp` — and the MCP screen, whose
1071 - * asset enqueue is keyed on that exact suffix, silently loads no React
1072 - * bundle at all. Measured: the first visit came back 102,513 bytes with no
1073 - * `dashboard.js`, against 489,272 bytes once the badge had cleared.
1074 - *
1075 - * Titles are only ever appended to, never rebuilt: the menu list is filtered
1076 - * (`betterdocs_admin_menu`), so whatever a filter put in a title survives.
1077 - *
1078 - * @return void
1079 - * @since 4.9.0
1080 - */
1081 - private function paint_mcp_badge() {
1082 - if ( ! $this->mcp_badge ) {
1083 - return;
758 + call_user_func_array( $callable, $value );
1084 759 }
1085 -
1086 - global $menu, $submenu;
1087 -
1088 - if ( is_array( $menu ) ) {
1089 - foreach ( $menu as &$item ) {
1090 - if ( isset( $item[2] ) && $this->slug === $item[2] ) {
1091 - $item[0] .= self::mcp_parent_bubble();
1092 - break;
1093 - }
1094 - }
1095 - unset( $item );
1096 - }
1097 -
1098 - if ( isset( $submenu[ $this->slug ] ) && is_array( $submenu[ $this->slug ] ) ) {
1099 - foreach ( $submenu[ $this->slug ] as &$sub_item ) {
1100 - if ( isset( $sub_item[2] ) && 'betterdocs-mcp' === $sub_item[2] ) {
1101 - $sub_item[0] .= self::mcp_submenu_pill();
1102 - break;
1103 - }
1104 - }
1105 - unset( $sub_item );
1106 - }
1107 760 }
1108 761
1109 762 /**
1110 - * Whether the current user should see the one-time MCP discovery badge.
1111 - *
1112 - * True only for a user who can actually reach the screen and has never
1113 - * opened it. The capability is the one the MCP menu item is already
1114 - * registered with (`manage_options`) rather than a second, re-derived rule —
1115 - * so the badge can never advertise a page its reader cannot open.
1116 - *
1117 - * @return bool
1118 - * @since 4.9.0
1119 - */
1120 - public static function should_flag_mcp() {
1121 - if ( ! current_user_can( 'manage_options' ) ) {
1122 - return false;
1123 - }
1124 -
1125 - $user_id = get_current_user_id();
1126 -
1127 - if ( ! $user_id ) {
1128 - return false;
1129 - }
1130 -
1131 - // `get_user_meta( …, true )` answers '' for a key that is not there, and
1132 - // the value written is always `time()` — so an empty string is the only
1133 - // shape "never opened" takes.
1134 - return '' === get_user_meta( $user_id, self::MCP_SEEN_META, true );
1135 - }
1136 -
1137 - /**
1138 - * WordPress' own update-count bubble, for the BetterDocs parent menu item.
1139 - *
1140 - * Core's markup on purpose: the red bubble, its position and its dark-mode
1141 - * colours are already in `wp-admin`'s stylesheet, so this needs no CSS of
1142 - * ours and cannot drift from the Plugins/Updates bubbles beside it.
1143 - *
1144 - * @return string
1145 - * @since 4.9.0
1146 - */
1147 - private static function mcp_parent_bubble() {
1148 - return ' <span class="update-plugins count-1"><span class="update-count">1</span></span>';
1149 - }
1150 -
1151 - /**
1152 - * The green "New" pill for the MCP submenu item.
1153 - *
1154 - * Core has no submenu-badge markup, so this one is ours — styled by
1155 - * `mcp_badge_styles()`.
1156 - *
1157 - * @return string
1158 - * @since 4.9.0
1159 - */
1160 - private static function mcp_submenu_pill() {
1161 - return ' <span class="bd-menu-pill">' . esc_html__( 'New', 'betterdocs' ) . '</span>';
1162 - }
1163 -
1164 - /**
1165 - * Whether this request is the MCP screen being opened by someone who can
1166 - * open it.
1167 - *
1168 - * The whole of the clearing decision, in one static so it can be pinned by
1169 - * a test. It reads the **page slug** rather than an admin hook suffix
1170 - * because the suffix is derived state: core builds it from
1171 - * `sanitize_title()` of the *parent* menu title (`get_plugin_page_hookname()`),
1172 - * that title is filtered (`betterdocs_admin_menu`), and the parent slug is
1173 - * spelled two ways in this class already. `?page=betterdocs-mcp` is the one
1174 - * thing that identifies this screen on every install (ADR-065).
1175 - *
1176 - * The capability is `manage_options`, the same one the MCP menu item is
1177 - * registered with and the same one {@see self::should_flag_mcp()} gates on:
1178 - * nothing may be written for a user who cannot reach the page.
1179 - *
1180 - * @return bool
1181 - * @since 4.9.0
1182 - */
1183 - public static function is_mcp_screen_request() {
1184 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection; see mark_mcp_seen().
1185 - $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
1186 -
1187 - if ( 'betterdocs-mcp' !== $page ) {
1188 - return false;
1189 - }
1190 -
1191 - return current_user_can( 'manage_options' );
1192 - }
1193 -
1194 - /**
1195 - * Record that this user has now seen the MCP screen.
1196 - *
1197 - * Bound to `admin_init` — which fires for every admin request — and gated on
1198 - * the page slug, rather than to `load-{$hook_suffix}` for the one suffix
1199 - * `add_submenu_page()` happened to return. `admin_menu` has already run by
1200 - * the time `admin_init` fires (measured), so the badge is still painted on
1201 - * *this* request and is gone from the next admin page — that is expected and
1202 - * correct. Do not add JavaScript to strip it mid-request.
1203 - *
1204 - * **No nonce, on purpose.** A nonce protects a state change an attacker
1205 - * could make a logged-in administrator perform unknowingly. The only state
1206 - * here is "this administrator has now been shown the MCP screen once", it is
1207 - * written for the current user alone, it holds no attacker-chosen value, and
1208 - * the worst a forged request can achieve is hiding a discovery badge from
1209 - * the person it was drawn for. A nonce on a plain page view would also have
1210 - * to survive the menu link, which carries none.
1211 - *
1212 - * @return void
1213 - * @since 4.9.0
1214 - */
1215 - public function mark_mcp_seen() {
1216 - if ( ! self::is_mcp_screen_request() ) {
1217 - return;
1218 - }
1219 -
1220 - $user_id = get_current_user_id();
1221 -
1222 - if ( ! $user_id ) {
1223 - return;
1224 - }
1225 -
1226 - update_user_meta( $user_id, self::MCP_SEEN_META, time() );
1227 - }
1228 -
1229 - /**
1230 - * The handful of declarations the "New" pill needs, inline, and only while
1231 - * it is being shown.
1232 - *
1233 - * The menu is read from the WordPress Dashboard, and `styles()` above
1234 - * early-returns on non-BetterDocs screens — so `admin/css/dashboard.css` is
1235 - * not loaded where this pill is seen. Loading the whole BetterDocs admin
1236 - * stylesheet globally, or shipping a stylesheet file for nine declarations,
1237 - * both cost far more than printing them here. The accent is written out
1238 - * rather than taken from `--base-color-700`: that token lives in
1239 - * `dashboard.css`, which is exactly the file that is not loaded here.
1240 - *
1241 - * @return void
1242 - * @since 4.9.0
1243 - */
1244 - public function mcp_badge_styles() {
1245 - if ( ! $this->mcp_badge ) {
1246 - return;
1247 - }
1248 -
1249 - 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";
1250 - }
1251 -
1252 - private function register_modern_ui_fallback() {
1253 - // Add the submenu with valid parent slug
1254 - add_submenu_page(
1255 - 'betterdocs', // Valid parent slug
1256 - __( 'All Docs', 'betterdocs' ),
1257 - '', // Empty menu title hides it
1258 - 'edit_docs',
1259 - 'betterdocs-admin',
1260 - array( $this, 'output' )
1261 - );
1262 -
1263 - // Hide the menu item from appearing in the admin sidebar
1264 - global $submenu;
1265 - if ( isset( $submenu['betterdocs'] ) ) {
1266 - foreach ( $submenu['betterdocs'] as $key => $item ) {
1267 - if ( 'betterdocs-admin' === $item[2] ) {
1268 - unset( $submenu['betterdocs'][ $key ] );
1269 - break;
1270 - }
1271 - }
1272 - }
1273 - }
1274 -
1275 - /**
1276 763 * BetterDocs Admin Page Output
1277 764 *
1278 765 * @return void
1279 766 * @since 1.0.0
@@ -1279,17 +766,17 @@
1279 766 * @since 1.0.0
1280 767 */
1281 768 public function output() {
1282 769 if ( betterdocs()->is_pro_active()
1283 - && version_compare( betterdocs()->pro_version(), '3.3.4', '<=' )
1284 - && get_current_screen()->id == 'betterdocs_page_betterdocs-analytics' ) {
770 + && version_compare( betterdocs()->pro_version(), '3.3.4', '<=' )
771 + && get_current_screen()->id == 'betterdocs_page_betterdocs-analytics' ) {
1285 772 betterdocs_pro()->views->get( 'admin/analytics-pro' );
1286 773 } else {
1287 774 betterdocs()->views->get(
1288 775 'admin/main',
1289 - array(
1290 - 'admin_ui' => 'dnd',
1291 - )
776 + [
777 + 'admin_ui' => 'dnd'
778 + ]
1292 779 );
1293 780 }
1294 781 }
1295 782
@@ -1302,10 +789,11 @@
1302 789 * @param array $callback
1303 790 *
1304 791 * @return array
1305 792 * @since 2.5.0
793 + *
1306 794 */
1307 - private function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null, $optional = array() ) {
795 + private function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null, $optional = [] ) {
1308 796 return Helper::normalize_menu( $title, $slug, $cap, $callback, $optional );
1309 797 }
1310 798
1311 799 /**
@@ -1314,162 +802,89 @@
1314 802 * @return array
1315 803 * @since 1.0.0
1316 804 */
1317 805 private function menu_list() {
1318 - $parent_slug = array();
806 + $parent_slug = [];
1319 807
1320 - $betterdocs_admin_pages = array(
1321 - 'betterdocs' => array(
808 + $betterdocs_admin_pages = [
809 + 'betterdocs' => [
1322 810 'menu_slug' => $this->slug,
1323 811 'page_title' => 'BetterDocs',
1324 812 'menu_title' => 'BetterDocs',
1325 813 'capability' => 'edit_docs',
1326 - 'callback' => array( $this, 'output' ),
814 + 'callback' => [ $this, 'output' ],
1327 815 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
1328 - 'position' => 5,
1329 - ),
1330 - 'dashboard' => $this->normalize_menu(
1331 - __( 'Dashboard', 'betterdocs' ),
1332 - 'betterdocs-dashboard',
1333 - 'edit_docs',
1334 - array(
1335 - $this,
1336 - 'output',
1337 - )
1338 - ),
1339 - 'all_docs' => $this->normalize_menu(
1340 - __( 'All Docs', 'betterdocs' ),
1341 - $this->ui_slug(),
1342 - 'edit_docs',
1343 - array( $this, 'output' ),
1344 - $parent_slug
1345 - ),
1346 - 'add_new' => $this->normalize_menu(
1347 - __( 'Add New', 'betterdocs' ),
1348 - 'post-new.php?post_type=docs'
1349 - ),
1350 - 'categories' => $this->normalize_menu(
1351 - __( 'Categories', 'betterdocs' ),
1352 - 'betterdocs-doc-categories',
1353 - 'manage_doc_terms',
1354 - array( $this, 'output' ),
1355 - $parent_slug
1356 - ),
1357 - 'tags' => $this->normalize_menu(
1358 - __( 'Tags', 'betterdocs' ),
1359 - 'betterdocs-doc-tags',
1360 - 'manage_doc_terms',
1361 - array( $this, 'output' ),
1362 - $parent_slug
1363 - ),
816 + 'position' => 5
817 + ],
818 + 'all_docs' => $this->normalize_menu( __( 'All Docs', 'betterdocs' ), $this->ui_slug() ),
819 + 'add_new' => $this->normalize_menu( __( 'Add New', 'betterdocs' ), 'post-new.php?post_type=docs' ),
820 + 'categories' => $this->normalize_menu( __( 'Categories', 'betterdocs' ), 'edit-tags.php?taxonomy=doc_category&post_type=docs', 'manage_doc_terms' ),
821 + 'tags' => $this->normalize_menu( __( 'Tags', 'betterdocs' ), 'edit-tags.php?taxonomy=doc_tag&post_type=docs', 'manage_doc_terms' ),
1364 822 'settings' => $this->normalize_menu(
1365 823 __( 'Settings', 'betterdocs' ),
1366 824 'betterdocs-settings',
1367 825 'edit_docs_settings',
1368 - array(
826 + [
1369 827 $this,
1370 - 'output',
1371 - ),
828 + 'output'
829 + ],
1372 830 $parent_slug
1373 831 ),
1374 - 'mcp' => $this->normalize_menu(
1375 - __( 'MCP', 'betterdocs' ),
1376 - 'betterdocs-mcp',
1377 - 'manage_options',
1378 - array(
1379 - $this,
1380 - 'output',
1381 - ),
1382 - $parent_slug
1383 - ),
1384 832 'analytics' => $this->normalize_menu(
1385 833 __( 'Analytics', 'betterdocs' ),
1386 834 'betterdocs-analytics',
1387 835 'read_docs_analytics',
1388 - array(
836 + [
1389 837 $this,
1390 - 'output',
1391 - ),
838 + 'output'
839 + ],
1392 840 $parent_slug
1393 841 ),
1394 842 'faq' => $this->normalize_menu(
1395 843 __( 'FAQ Builder', 'betterdocs' ),
1396 844 'betterdocs-faq',
1397 - 'read_faq_builder',
1398 - array(
845 + 'read_docs_analytics',
846 + [
1399 847 $this,
1400 - 'output',
1401 - ),
848 + 'output'
849 + ],
1402 850 $parent_slug
1403 - ),
1404 - );
851 + )
852 + ];
1405 853
1406 - // Glossaries is Pro. Reserve this same 'glossaries' slot for Free's locked
1407 - // teaser so the item keeps this position; once Pro is active the real
1408 - // screen overwrites the key in place (same pattern as API Docs below).
1409 - if ( betterdocs()->show_glossary_teaser() || ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) ) {
854 + if ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) {
1410 855 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1411 856 __( 'Glossaries', 'betterdocs' ),
1412 857 'betterdocs-glossaries',
1413 - betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics',
1414 - array(
858 + 'read_docs_analytics',
859 + [
1415 860 $this,
1416 - 'output',
1417 - ),
861 + 'output'
862 + ],
1418 863 $parent_slug
1419 864 );
1420 865 }
1421 866
1422 - // API Docs ships in Pro, which overwrites this same key in place — declaring
1423 - // the slot here is what keeps the item in this position. Without Pro it
1424 - // holds Free's locked teaser instead.
1425 - if ( betterdocs()->show_api_docs_teaser() || betterdocs()->has_api_docs() ) {
1426 - $betterdocs_admin_pages['api_docs'] = $this->normalize_menu(
1427 - __( 'API Docs', 'betterdocs' ),
1428 - 'betterdocs-api-docs',
1429 - apply_filters( 'betterdocs_api_ref_capability', 'manage_options' ),
1430 - array(
1431 - $this,
1432 - 'output',
1433 - ),
1434 - $parent_slug
1435 - );
1436 - }
867 + return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, [$this, 'output'], $parent_slug);
1437 868
1438 - if ( ! betterdocs()->is_chatbot_active() ) {
1439 - $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1440 - __( 'AI Chatbot', 'betterdocs' ),
1441 - 'betterdocs-ai-chatbot',
1442 - 'edit_docs_settings',
1443 - array(
1444 - $this,
1445 - 'output',
1446 - ),
1447 - $parent_slug
1448 - );
1449 - }
869 + }
1450 870
1451 - return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1452 - }
1453 -
1454 871 public function add_custom_classes_to_menu_items() {
1455 872 global $menu, $submenu;
1456 873
1457 - $menu_items = array(
874 + $menu_items = [
1458 875 'betterdocs' => 'betterdocs',
1459 876 'betterdocs_page_all_docs' => 'betterdocs-all-docs',
1460 877 'betterdocs_page_add_new' => 'betterdocs-add-new',
1461 - 'betterdocs-doc-categories' => 'betterdocs-categories',
1462 - 'betterdocs-doc-tags' => 'betterdocs-tags',
878 + 'edit-tags.php?taxonomy=doc_category&post_type=docs' => 'betterdocs-categories',
879 + 'edit-tags.php?taxonomy=doc_tag&post_type=docs' => 'betterdocs-tags',
1463 880 'betterdocs-settings' => 'betterdocs-settings',
1464 - 'betterdocs-mcp' => 'betterdocs-mcp',
1465 881 'betterdocs-analytics' => 'betterdocs-analytics',
1466 882 'betterdocs-faq' => 'betterdocs-faq',
1467 883 'betterdocs-glossaries' => 'betterdocs-glossaries',
1468 884 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1469 - 'betterdocs-api-docs' => 'betterdocs-api-docs',
1470 - 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1471 - );
885 + 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb'
886 + ];
1472 887
1473 888 foreach ( $menu as &$item ) {
1474 889 if ( isset( $menu_items[ $item[2] ] ) ) {
1475 890 if ( ! isset( $item[4] ) ) {
@@ -1499,12 +914,12 @@
1499 914 $menus['quick_setup'] = $this->normalize_menu(
1500 915 __( 'Quick Setup', 'betterdocs' ),
1501 916 'betterdocs-setup',
1502 917 'delete_users',
1503 - array(
918 + [
1504 919 $this->container->get( SetupWizard::class ),
1505 - 'views',
1506 - )
920 + 'views'
921 + ]
1507 922 );
1508 923 }
1509 924
1510 925 return $menus;
@@ -1512,12 +927,8 @@
1512 927
1513 928 public function insert_plugin_links( $links ) {
1514 929 $links[] = '<a href="admin.php?page=betterdocs-settings">' . __( 'Settings', 'betterdocs' ) . '</a>';
1515 930
1516 - if ( ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
1517 - $links[] = '<a href="https://betterdocs.co/upgrade-to-pro-plugins-wp" target="_blank" style="color: #000; font-weight: bold;">' . __( 'Upgrade to Pro', 'betterdocs' ) . '</a>';
1518 - }
1519 -
1520 931 return $links;
1521 932 }
1522 933
1523 934 public function toolbar_menu( $admin_bar ) {
@@ -1544,22 +955,17 @@
1544 955 }
1545 956
1546 957 $slug = $this->settings->get( 'encyclopedia_root_slug' );
1547 958
1548 - global $wp_rewrite;
1549 - if ( $wp_rewrite->using_index_permalinks() ) {
1550 - $slug = $wp_rewrite->index . '/' . $slug;
1551 - }
1552 -
1553 959 $encyclopedia_url = home_url( $slug );
1554 960
1555 961 $admin_bar->add_node(
1556 - array(
962 + [
1557 963 'parent' => 'site-name',
1558 964 'id' => 'view-docs',
1559 965 'title' => __( 'Visit Documentation', 'betterdocs' ),
1560 - 'href' => $docs_url,
1561 - )
966 + 'href' => $docs_url
967 + ]
1562 968 );
1563 969
1564 970 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1565 971
@@ -1564,14 +970,14 @@
1564 970 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1565 971
1566 972 if ( $is_enable_encyclopedia && betterdocs()->is_pro_active() ) {
1567 973 $admin_bar->add_node(
1568 - array(
974 + [
1569 975 'parent' => 'site-name',
1570 976 'id' => 'view-encyclopedia',
1571 977 'title' => __( 'Visit Encyclopedia', 'betterdocs' ),
1572 - 'href' => $encyclopedia_url,
1573 - )
978 + 'href' => $encyclopedia_url
979 + ]
1574 980 );
1575 981 }
1576 982 }
1577 983
@@ -1578,20 +984,14 @@
1578 984 /**
1579 985 * Save last visited admin ui
1580 986 *
1581 987 * @since 3.0.1
988 + *
1582 989 */
1583 990 public function save_admin_page() {
1584 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1585 - $post_type = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : '';
1586 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1587 - $bdocs_view = isset( $_GET['bdocs_view'] ) ? sanitize_text_field( wp_unslash( $_GET['bdocs_view'] ) ) : '';
1588 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1589 - $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
1590 -
1591 - if ( 'docs' === $post_type && 'classic' === $bdocs_view ) {
991 + if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'docs' && isset( $_GET['bdocs_view'] ) && $_GET['bdocs_view'] === 'classic' ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1592 992 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'classic_ui' );
1593 - } elseif ( 'betterdocs-admin' === $page ) {
993 + } elseif ( isset( $_GET['page'] ) && $_GET['page'] === 'betterdocs-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1594 994 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'modern_ui' );
1595 995 }
1596 996 }
1597 997
@@ -1602,19 +1002,23 @@
1602 1002 * @since 3.0.1
1603 1003 */
1604 1004 public function ui_slug() {
1605 1005 $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1606 - $docs_exist = get_posts(
1607 - array(
1006 + $docs = get_posts(
1007 + [
1608 1008 'post_type' => 'docs',
1609 1009 'post_status' => 'any',
1610 - 'numberposts' => 1,
1611 - )
1010 + 'numberposts' => 2
1011 + ]
1612 1012 );
1613 1013
1614 - return ( 'modern_ui' === $last_visited || empty( $docs_exist ) )
1615 - ? 'betterdocs-admin'
1616 - : 'edit.php?post_type=docs&bdocs_view=classic';
1014 + if ( $last_visited === 'modern_ui' || count( $docs ) == 0 ) {
1015 + $slug = 'betterdocs-admin';
1016 + } else {
1017 + $slug = admin_url( 'edit.php?post_type=docs&bdocs_view=classic' );
1018 + }
1019 +
1020 + return $slug;
1617 1021 }
1618 1022
1619 1023 /**
1620 1024 * Resets a duplicate submenu in WordPress if the parent main menu and the first submenu permalink are not the same.
@@ -1624,9 +1028,9 @@
1624 1028 */
1625 1029 public function reset_submenu() {
1626 1030 global $submenu;
1627 1031
1628 - $docs = get_posts( array( 'post_type' => 'docs' ) );
1032 + $docs = get_posts( [ 'post_type' => 'docs' ] );
1629 1033 if ( count( $docs ) == 0 ) {
1630 1034 return;
1631 1035 }
1632 1036
@@ -1631,89 +1035,10 @@
1631 1035 }
1632 1036
1633 1037 $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1634 1038
1635 - if ( 'classic_ui' === $last_visited && isset( $submenu['betterdocs-admin'] ) && in_array( 'betterdocs-admin', $submenu['betterdocs-admin'][0] ) ) {
1039 + if ( $last_visited === 'classic_ui' && isset( $submenu['betterdocs-admin'] ) && in_array( 'betterdocs-admin', $submenu['betterdocs-admin'][0] ) ) {
1636 1040 unset( $submenu['betterdocs-admin'][0] );
1637 1041 $submenu['betterdocs-admin'] = array_values( $submenu['betterdocs-admin'] );
1638 1042 }
1639 - }
1640 -
1641 - /**
1642 - * Initialize Black Friday Pointer
1643 - *
1644 - * @return void
1645 - * @since 3.7.0
1646 - */
1647 - private function init_black_friday_pointer() {
1648 - // Only initialize if conditions are met
1649 - if ( NoticePointers::should_display_notice() ) {
1650 - new NoticePointers();
1651 - }
1652 - }
1653 -
1654 - /**
1655 - * AJAX handler for dismissing Black Friday pointer
1656 - *
1657 - * @return void
1658 - * @since 3.7.0
1659 - */
1660 - public function ajax_dismiss_black_friday_pointer() {
1661 - // Verify nonce
1662 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
1663 - if ( ! wp_verify_nonce( $nonce, 'betterdocs_dismiss_pointer' ) ) {
1664 - wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'betterdocs' ) ) );
1665 - }
1666 -
1667 - // Check if user has permission
1668 - if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'edit_docs' ) ) {
1669 - wp_send_json_error( array( 'message' => __( 'Permission denied', 'betterdocs' ) ) );
1670 - }
1671 -
1672 - // Get the introduction key
1673 - $introduction_key = isset( $_POST['introduction_key'] ) ? sanitize_text_field( wp_unslash( $_POST['introduction_key'] ) ) : '';
1674 -
1675 - if ( empty( $introduction_key ) ) {
1676 - wp_send_json_error( array( 'message' => __( 'Invalid introduction key', 'betterdocs' ) ) );
1677 - }
1678 -
1679 - // Set the introduction as viewed
1680 - NoticePointers::set_introduction_viewed( $introduction_key );
1681 -
1682 - // Clear the priority option so other plugins can set their priority
1683 - delete_option( '_wpdeveloper_plugin_pointer_priority' );
1684 -
1685 - wp_send_json_success( array( 'message' => __( 'Pointer dismissed successfully', 'betterdocs' ) ) );
1686 - }
1687 -
1688 - /**
1689 - * Get count of docs that are not yet synced
1690 - * Similar to Helper::get_not_synced_post_ids() in betterdocs-ai-chatbot plugin
1691 - *
1692 - * @return int
1693 - * @since 3.7.0
1694 - */
1695 - private function get_not_synced_docs_count() {
1696 - $new_post_ids = get_option( 'saved_docs_post_ids', array() );
1697 - $error_posts_data = get_option( 'betterdocs_ai_chatbot_error_posts', array() );
1698 -
1699 - // Extract post IDs from error posts (handle both old and new formats)
1700 - $error_post_ids = array();
1701 - if ( is_array( $error_posts_data ) && ! empty( $error_posts_data ) ) {
1702 - foreach ( $error_posts_data as $key => $value ) {
1703 - if ( is_numeric( $key ) && is_numeric( $value ) ) {
1704 - // Old format: numeric key with post ID as value
1705 - $error_post_ids[] = $value;
1706 - } elseif ( is_numeric( $key ) && is_array( $value ) && isset( $value['post_id'] ) ) {
1707 - // New format: post ID as key with structured data
1708 - $error_post_ids[] = $key;
1709 - } elseif ( is_numeric( $key ) ) {
1710 - // New format: post ID as key
1711 - $error_post_ids[] = $key;
1712 - }
1713 - }
1714 - }
1715 -
1716 - // Merge both arrays and remove duplicates, then count
1717 - return count( array_unique( array_merge( $new_post_ids, $error_post_ids ) ) );
1718 1043 }
1719 1044 }