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 +286 -674 4.8.03.8.9 View file →
@@ -1,22 +1,16 @@
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 /**
@@ -24,12 +18,11 @@
24 18 */
25 19 private static $cache_bank;
26 20 /**
27 21 * Admin Root Menu Slug
28 - *
29 22 * @var string
30 23 */
31 - private $slug = 'betterdocs-dashboard';
24 + private $slug = 'betterdocs-admin';
32 25 /**
33 26 * Insights
34 27 *
35 28 * @var Insights
@@ -58,19 +51,14 @@
58 51 private $kbmigration;
59 52
60 53 /**
61 54 * Enqueue
62 - *
63 55 * @var Enqueue
64 56 */
65 57 private $assets;
66 58
67 - // modules
68 - protected $installer;
69 -
70 59 /**
71 60 * FAQBuilder
72 - *
73 61 * @var FAQBuilder
74 62 */
75 63 private $faq_builder;
76 64 private $glossaries;
@@ -79,12 +67,11 @@
79 67 $this->container = $container;
80 68 $this->assets = $assets;
81 69 $this->settings = $settings;
82 70 $this->kbmigration = $kbmigration;
83 - $this->slug = 'betterdocs-dashboard';
71 + $this->slug = 'betterdocs-admin';
84 72
85 - add_action( 'init', array( $type, 'register' ), 9 );
86 - add_action( 'rest_api_init', array( $this, 'order_terms_in_wp_terms_admin_table' ) );
73 + add_action( 'init', [ $type, 'register' ], 9 );
87 74
88 75 $type->init();
89 76 $type->admin_init();
90 77
@@ -90,73 +77,51 @@
90 77
91 78 $this->faq_builder = $this->container->get( FAQBuilder::class );
92 79 $this->glossaries = $this->container->get( Glossaries::class );
93 80
94 - /**
95 - * Register usage tracking (including the daily `put_do_weekly_action` cron
96 - * handler) on every request — WP-Cron runs with is_admin() === false, so
97 - * this MUST sit above the admin guard or the cron send never fires. The
98 - * admin-only UI hooks inside Insights::init() (deactivation form, footer
99 - * scripts, plugin_action_links) are context-specific and simply never run
100 - * outside wp-admin.
101 - */
102 - $this->plugin_insights();
103 -
104 81 if ( ! is_admin() ) {
105 82 return;
106 83 }
107 84
108 - $this->installer = new PluginInstaller();
109 -
110 - add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
111 - // The WPNotice CacheBank wipes all admin_notices at priority 10 on BetterDocs
112 - // screens, so the hook above never renders inside the BetterDocs panels.
113 - // Re-add the compatibility notice after that wipe (in_admin_header, priority
114 - // 999) so it shows on the panels like the review / license notices.
115 - add_action( 'in_admin_header', function () {
116 - $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
117 - if ( $screen && betterdocs()->is_betterdocs_screen( $screen->id ) ) {
118 - add_action( 'admin_notices', array( $this, 'compatibility_notices' ) );
119 - }
120 - }, 999 );
85 + $this->plugin_insights();
86 + add_action( 'admin_notices', [ $this, 'compatibility_notices' ] );
121 87 // add_action( 'admin_init', [$this, 'notices'], 9 );
122 - add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 );
88 + add_filter( 'admin_init', [ $this, 'save_admin_page' ], 99 );
123 89
124 - add_action( 'admin_menu', array( $this, 'menus' ) );
125 - add_action( 'admin_menu', array( $this, 'reset_submenu' ) );
126 - add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) );
127 - 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' ] );
128 94
129 95 // $this->container->get( SetupWizard::class )->init();
130 96
131 - add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
132 - add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
133 - // add_action( 'betterdocs_listing_header', [ $this, 'header' ], 10, 1 );
134 - 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 );
135 101
136 - add_filter( 'admin_body_class', array( $this, 'body_classes' ) );
137 - add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) );
138 - add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 );
139 - 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 );
140 106
141 107 /**
142 108 * Remove Comments Column from List Table.
143 109 */
144 - add_filter( 'manage_docs_posts_columns', array( $this, 'set_custom_edit_action_columns' ) );
145 - 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 );
146 112
147 113 /**
148 114 * Add New Column
149 115 */
150 - add_filter( 'manage_users_columns', array( $this, 'add_users_total_docs_column' ), 10, 1 );
151 - 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 );
152 118 if ( is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
153 119 add_action( 'admin_footer-plugins.php', array( $this, 'disable_deactivation' ) );
154 120 }
155 121
156 122 if ( $this->settings->get( 'enable_estimated_reading_time' ) ) {
157 - // Hook into unified metabox instead of creating separate metabox
158 - add_action( 'betterdocs_reading_time_tab_content', array( $this, 'render_estimated_time_markup' ) );
123 + add_action( 'add_meta_boxes', [ $this, 'reading_meta_box_' ], 10 );
159 124 }
160 125
161 126 self::$cache_bank = CacheBank::get_instance();
162 127
@@ -167,30 +132,10 @@
167 132 $this->notices();
168 133 } catch ( Exception $e ) {
169 134 unset( $e );
170 135 }
171 -
172 - // Initialize Black Friday Pointer
173 - $this->init_black_friday_pointer();
174 -
175 - // Register AJAX handler for pointer dismissal
176 - add_action( 'wp_ajax_betterdocs_dismiss_black_friday_pointer', array( $this, 'ajax_dismiss_black_friday_pointer' ) );
177 136 }
178 137
179 - public function order_terms_in_wp_terms_admin_table() {
180 - // order the terms correctly to be shown on the admin panel categories menu with betterdocs order
181 - add_action(
182 - 'rest_insert_doc_category',
183 - function ( $term, $request, $bool ) {
184 - $max_order = Helper::get_max_doc_category_order_from_term_meta() ?? 0;
185 - $next_order = $max_order + 1;
186 - update_term_meta( $term->term_id, 'doc_category_order', $next_order );
187 - },
188 - 10,
189 - 3
190 - );
191 - }
192 -
193 138 public function disable_deactivation() {
194 139 $tooltip_text = esc_html__( 'Deactivate BetterDocs Pro First', 'betterdocs' );
195 140 ?>
196 141 <style type="text/css">
@@ -235,17 +180,17 @@
235 180 <?php
236 181 }
237 182
238 183 public function add_users_total_docs_column( $columns ) {
239 - $new_column = array(
240 - 'docs' => __( 'Docs', 'betterdocs' ),
241 - );
184 + $new_column = [
185 + 'docs' => __( 'Docs', 'betterdocs' )
186 + ];
242 187 $columns = array_merge( $columns, $new_column );
243 188 return $columns;
244 189 }
245 190
246 191 public function popular_users_docs_data( $output, $column_name, $user_id ) {
247 - if ( 'docs' == $column_name ) {
192 + if ( $column_name == 'docs' ) {
248 193 $total_count = count_user_posts( $user_id, 'docs', true );
249 194 return '<a href="edit.php?post_type=docs&author=' . $user_id . '" class="edit"><span aria-hidden="true">' . $total_count . '</span></a>';
250 195 }
251 196 return $output;
@@ -250,8 +195,20 @@
250 195 }
251 196 return $output;
252 197 }
253 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 +
254 211 public function render_estimated_time_markup() {
255 212 betterdocs()->views->get( 'admin/metabox/estimated-reading-box' );
256 213 }
257 214
@@ -259,15 +216,13 @@
259 216 if ( betterdocs()->is_pro_active() ) {
260 217 $plugins = Helper::get_plugins();
261 218 $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php'];
262 219
263 - // Require the paired Pro release: the Analytics UI is version-coupled to
264 - // Pro's advanced modules, so an older Pro renders a broken/partial panel.
265 - 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', '>=' ) ) {
266 221 return;
267 222 }
268 223
269 - betterdocs()->views->get( 'admin/notices/compatibility', array( 'version' => $plugin_data['Version'] ) );
224 + betterdocs()->views->get( 'admin/notices/compatibility', [ 'version' => $plugin_data['Version'] ] );
270 225 }
271 226 }
272 227
273 228 public function plugin_insights( $prevent_init = false ) {
@@ -272,20 +227,20 @@
272 227
273 228 public function plugin_insights( $prevent_init = false ) {
274 229 $this->insights = Insights::get_instance(
275 230 BETTERDOCS_PLUGIN_FILE,
276 - array(
231 + [
277 232 'opt_in' => true,
278 233 'goodbye_form' => true,
279 - 'item_id' => 'c7b16777b4f1b83f6083',
280 - )
234 + 'item_id' => 'c7b16777b4f1b83f6083'
235 + ]
281 236 );
282 237
283 238 $this->insights->set_notice_options(
284 - array(
239 + [
285 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' ),
286 - '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' ),
287 - )
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 + ]
288 243 );
289 244
290 245 if ( ! $prevent_init ) {
291 246 $this->insights->init();
@@ -301,77 +256,76 @@
301 256 * @throws Exception
302 257 */
303 258 public function notices() {
304 259 $notices = new Notices(
305 - array(
260 + [
306 261 'id' => 'betterdocs',
307 262 'storage_key' => 'notices',
308 263 'lifetime' => 3,
309 264 'stylesheet_url' => $this->assets->asset_url( 'admin/css/notices.css' ),
310 265 'styles' => $this->assets->asset_url( 'admin/css/notices.css' ),
311 - 'priority' => 4,
312 - )
266 + 'priority' => 4
267 + ]
313 268 );
314 269
315 270 /**
316 - * Review Notice
317 - *
318 - * @var mixed $message
319 - */
271 + * Review Notice
272 + * @var mixed $message
273 + */
320 274
321 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' );
322 276
323 - $_review_notice = array(
277 + $_review_notice = [
324 278 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
325 279 'html' => '<p>' . $message . '</p>',
326 - 'links' => array(
327 - 'later' => array(
280 + 'links' => [
281 + 'later' => [
328 282 'link' => 'https://wordpress.org/plugins/betterdocs/#reviews',
329 283 'target' => '_blank',
330 284 'label' => __( 'Sure, you deserve it!', 'betterdocs' ),
331 - 'icon_class' => 'dashicons dashicons-external',
332 - ),
333 - 'allready' => array(
285 + 'icon_class' => 'dashicons dashicons-external'
286 + ],
287 + 'allready' => [
334 288 'label' => __( 'I already did', 'betterdocs' ),
335 289 'icon_class' => 'dashicons dashicons-smiley',
336 - 'attributes' => array(
337 - 'data-dismiss' => true,
338 - ),
339 - ),
340 - 'maybe_later' => array(
290 + 'attributes' => [
291 + 'data-dismiss' => true
292 + ]
293 + ],
294 + 'maybe_later' => [
341 295 'label' => __( 'Maybe Later', 'betterdocs' ),
342 296 'icon_class' => 'dashicons dashicons-calendar-alt',
343 - 'attributes' => array(
297 + 'attributes' => [
344 298 'data-later' => true,
345 - 'class' => 'dismiss-btn',
346 - ),
347 - ),
348 - 'support' => array(
299 + 'class' => 'dismiss-btn'
300 + ]
301 + ],
302 + 'support' => [
349 303 'link' => 'https://wpdeveloper.com/support',
350 - 'attributes' => array(
351 - 'target' => '_blank',
352 - ),
304 + 'attributes' => [
305 + 'target' => '_blank'
306 + ],
353 307 'label' => __( 'I need help', 'betterdocs' ),
354 - 'icon_class' => 'dashicons dashicons-sos',
355 - ),
356 - 'never_show_again' => array(
308 + 'icon_class' => 'dashicons dashicons-sos'
309 + ],
310 + 'never_show_again' => [
357 311 'label' => __( 'Never show again', 'betterdocs' ),
358 312 'icon_class' => 'dashicons dashicons-dismiss',
359 - 'attributes' => array(
360 - 'data-dismiss' => true,
361 - ),
362 - ),
363 - ),
364 - );
313 + 'attributes' => [
314 + 'data-dismiss' => true
315 + ]
316 + ]
317 + ]
318 + ];
365 319
366 320 $notices->add(
367 321 'review',
368 322 $_review_notice,
369 - array(
323 + [
370 324 'start' => $notices->strtotime( '+10 days' ),
371 325 'recurrence' => 30,
372 - 'dismissible' => true,
373 - )
326 + 'dismissible' => true
327 + ]
374 328 );
375 329
376 330 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
377 331 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
@@ -376,9 +330,9 @@
376 330 if ( $this->kbmigration->existing_plugins && ! in_array( $this->kbmigration->existing_plugins[0][0], $this->kbmigration->migrated_plugins ) ) {
377 331 $plugin_name = '<strong>' . esc_html( $this->kbmigration->existing_plugins[0][1] ) . '</strong>';
378 332
379 333 $message = sprintf(
380 - /* translators: %s is the name of the existing knowledge base plugin. */
334 + /* translators: %s is the name of the existing knowledge base plugin. */
381 335 __( 'Already using %s? Power up your Knowledge Base by migrating all your docs and settings to BetterDocs with just 1 click.', 'betterdocs' ),
382 336 esc_html( $plugin_name )
383 337 );
384 338
@@ -388,51 +342,50 @@
388 342 esc_url( admin_url( 'admin.php?page=betterdocs-settings&tab=tab-migration' ) ),
389 343 esc_html__( 'Start Migration', 'betterdocs' )
390 344 );
391 345
392 - $_migration_notice = array(
346 + $_migration_notice = [
393 347 'thumbnail' => '',
394 348 'html' => $migration_message,
395 - 'links' => array(
396 - 'maybe_later' => array(
349 + 'links' => [
350 + 'maybe_later' => [
397 351 'label' => __( 'Maybe Later', 'betterdocs' ),
398 352 'icon_class' => 'dashicons dashicons-calendar-alt',
399 - 'attributes' => array(
353 + 'attributes' => [
400 354 'data-later' => true,
401 - 'class' => 'dismiss-btn',
402 - ),
403 - ),
404 - 'never_show_again' => array(
355 + 'class' => 'dismiss-btn'
356 + ]
357 + ],
358 + 'never_show_again' => [
405 359 'label' => __( 'Never show again', 'betterdocs' ),
406 360 'icon_class' => 'dashicons dashicons-dismiss',
407 - 'attributes' => array(
408 - 'data-dismiss' => true,
409 - ),
410 - ),
411 - ),
412 - );
361 + 'attributes' => [
362 + 'data-dismiss' => true
363 + ]
364 + ]
365 + ]
366 + ];
413 367
414 368 $notices->add(
415 369 'migration',
416 370 $_migration_notice,
417 - array(
371 + [
418 372 'start' => $notices->time(),
419 373 'recurrence' => false,
420 - 'dismissible' => true,
421 - )
374 + 'dismissible' => true
375 + ]
422 376 );
423 377 }
424 378
425 379 /**
426 - *
427 380 * Opt-In Notice
428 381 */
429 382 $allow_tracking = get_option( 'wpins_allow_tracking' );
430 - if ( null != $this->insights && ! isset( $allow_tracking['betterdocs'] ) ) {
383 + if ( $this->insights != null && ! isset( $allow_tracking['betterdocs'] ) ) {
431 384 $notices->add(
432 385 'opt_in',
433 - array( $this->insights, 'notice' ),
434 - array(
386 + [ $this->insights, 'notice' ],
387 + [
435 388 'classes' => 'updated put-dismiss-notice',
436 389 'start' => $notices->time(),
437 390 'refresh' => BETTERDOCS_VERSION,
438 391 'dismissible' => true,
@@ -437,57 +390,19 @@
437 390 'refresh' => BETTERDOCS_VERSION,
438 391 'dismissible' => true,
439 392 'do_action' => 'wpdeveloper_notice_clicked_for_betterdocs',
440 393 'display_if' => ! function_exists( 'betterdocs_pro' ),
441 - 'screens' => array( 'dashboard' ),
442 - )
394 + 'screens' => [ 'dashboard' ]
395 + ]
443 396 );
444 397 }
445 398
446 - $summer_campaign_message = '<div class="betterdocs-summer-notice-body"><p style="margin-top: 0; margin-bottom: 0;">🏖️ <strong>Summer Savings:</strong> Build AI-powered Knowledge Bases & FAQs to cut support tickets and improve user experience – now <strong>up to $100 OFF!</strong></p></div>';
447 - $_summer_campaign_notice = array(
448 - 'thumbnail' => $this->assets->icon( 'betterdocs-logo.svg', true ),
449 - 'html' => $summer_campaign_message,
450 - 'links' => array(
451 - 'support' => array(
452 - 'link' => 'https://betterdocs.co/summer2026-admin-notice',
453 - 'attributes' => array(
454 - 'target' => '_blank',
455 - 'class' => 'offer-button',
456 - ),
457 - 'label' => __( 'Upgrade To PRO Now', 'betterdocs' ),
458 - ),
459 - 'maybe_later' => array(
460 - 'label' => __( 'I’ll Grab It Later', 'betterdocs' ),
461 - 'attributes' => array(
462 - 'target' => '_blank',
463 - 'data-later' => true,
464 - 'class' => 'dismiss-btn',
465 - ),
466 - ),
467 - ),
468 - );
469 -
470 - $notices->add(
471 - 'summer-campaign-26',
472 - $_summer_campaign_notice,
473 - array(
474 - 'start' => $notices->time(),
475 - 'recurrence' => false,
476 - 'dismissible' => true,
477 - 'refresh' => BETTERDOCS_VERSION,
478 - 'expire' => strtotime( '11:59:59pm June 25, 2026' ),
479 - 'display_if' => ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ),
480 - )
481 - );
482 -
483 399 self::$cache_bank->create_account( $notices );
484 400 self::$cache_bank->calculate_deposits( $notices );
485 401 if ( method_exists( self::$cache_bank, 'clear_notices_in_' ) ) {
486 402 self::$cache_bank->clear_notices_in_(
487 - array(
488 - 'toplevel_page_betterdocs-dashboard',
489 - 'admin_page_betterdocs-admin',
403 + [
404 + 'toplevel_page_betterdocs-admin',
490 405 'betterdocs_page_betterdocs-admin',
491 406 'betterdocs_page_betterdocs-settings',
492 407 'betterdocs_page_betterdocs-faq',
493 408 'betterdocs_page_betterdocs-analytics',
@@ -492,13 +407,11 @@
492 407 'betterdocs_page_betterdocs-faq',
493 408 'betterdocs_page_betterdocs-analytics',
494 409 'betterdocs_page_betterdocs-glossaries',
495 410 'betterdocs_page_betterdocs-ai-chatbot',
496 - 'betterdocs_page_betterdocs-doc-categories',
497 - 'betterdocs_page_betterdocs-doc-tags',
498 411 'edit-doc_category',
499 - 'edit-doc_tag',
500 - ),
412 + 'edit-doc_tag'
413 + ],
501 414 $notices,
502 415 true
503 416 );
504 417 }
@@ -503,85 +416,30 @@
503 416 );
504 417 }
505 418 }
506 419
507 - /**
508 - * Resolve the admin dark-mode preference.
509 - *
510 - * The mode switcher stores the choice in a client cookie (no DB write, shared
511 - * across every admin screen). Fall back to the legacy
512 - * `betterdocs_settings['dark_mode']` value for installs that set it before this
513 - * change and haven't toggled since.
514 - *
515 - * @return bool
516 - */
517 - public function is_dark_mode() {
518 - if ( isset( $_COOKIE['betterdocs_admin_dark_mode'] ) ) {
519 - return '1' === $_COOKIE['betterdocs_admin_dark_mode']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
520 - }
521 -
522 - $saved = get_option( 'betterdocs_settings', array() );
523 - return ! empty( $saved['dark_mode'] );
524 - }
525 -
526 - /**
527 - * Whether the knowledge base is genuinely empty (no doc categories and no
528 - * non-trash docs). Localized to the admin so the All Docs panel can render a
529 - * skeleton shaped like the "No Category Found" empty card on first paint —
530 - * instead of a category/docs skeleton it would immediately replace — without
531 - * waiting for the REST fetch to reveal the count.
532 - *
533 - * @return bool
534 - */
535 - public function kb_is_empty() {
536 - $cats = wp_count_terms( array( 'taxonomy' => 'doc_category', 'hide_empty' => false ) );
537 - $cats = is_wp_error( $cats ) ? 0 : (int) $cats;
538 - if ( $cats > 0 ) {
539 - return false;
540 - }
541 -
542 - $counts = (array) wp_count_posts( 'docs' );
543 - $total = 0;
544 - foreach ( array( 'publish', 'future', 'draft', 'pending', 'private' ) as $status ) {
545 - $total += isset( $counts[ $status ] ) ? (int) $counts[ $status ] : 0;
546 - }
547 -
548 - return 0 === $total;
549 - }
550 -
551 420 public function body_classes( $classes ) {
552 - $dark_mode = $this->is_dark_mode();
553 - $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', str_replace( 'admin_page_', '', get_current_screen()->id ) ) ) : '';
554 - /**
555 - * Filter the list of (prefix-stripped) screen ids that receive the
556 - * `betterdocs-admin` body class (and dark-mode class). Pro/add-ons can
557 - * register their own React admin pages, e.g. the Knowledge Base page.
558 - *
559 - * @param string[] $registered_screens Screen ids with the page prefix removed.
560 - */
561 - $registered_screens = apply_filters( 'betterdocs_admin_screen_slugs', array(
562 - 'betterdocs-settings',
563 - 'betterdocs-admin',
564 - 'betterdocs-dashboard',
565 - 'betterdocs-analytics',
566 - 'betterdocs-glossaries',
567 - 'betterdocs-faq',
568 - 'betterdocs-doc-categories',
569 - 'betterdocs-doc-tags',
570 - 'edit-doc_category',
571 - 'edit-doc_tag',
572 - 'edit-knowledge_base',
573 - 'betterdocs-ai-chatbot',
574 - ) );
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 + ];
575 436
576 - if ( in_array( $current_screen_id, $registered_screens ) ) {
577 - $classes .= ' betterdocs-admin ';
578 - }
437 + if( in_array( $current_screen_id, $registered_screens ) ) {
438 + $classes .= ' betterdocs-admin ';
439 + }
579 440
580 - // Dark mode also applies on the Quick Setup wizard, whose self-scoped chrome
581 - // keys off `.betterdocs_page_betterdocs-setup.betterdocs-dark-mode`.
582 - $dark_screens = array_merge( $registered_screens, array( 'betterdocs-setup' ) );
583 - if ( $dark_mode && in_array( $current_screen_id, $dark_screens, true ) ) {
441 + if ( $dark_mode === true && in_array( $current_screen_id, $registered_screens ) ) {
584 442 $classes .= ' betterdocs-dark-mode ';
585 443 }
586 444
587 445 return $classes;
@@ -596,11 +454,11 @@
596 454 * @since 1.0.0
597 455 */
598 456 public function set_custom_edit_action_columns( $columns ) {
599 457 unset( $columns['comments'] );
600 - $new_columns = array();
458 + $new_columns = [];
601 459 foreach ( $columns as $key => $value ) {
602 - if ( 'date' == $key ) {
460 + if ( $key == 'date' ) {
603 461 $new_columns['betterdocs_word_count'] = __( 'Word Count', 'betterdocs' ); // put the tags column before it
604 462 $new_columns['betterdocs_reaction'] = __( 'Reactions', 'betterdocs' );
605 463 }
606 464 $new_columns[ $key ] = $value;
@@ -612,29 +470,26 @@
612 470 public function manage_custom_columns( $column, $post_id ) {
613 471 global $wpdb;
614 472 switch ( $column ) {
615 473 case 'betterdocs_word_count':
616 - $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 ) ) );
617 475 preg_match_all( '/<[^>]*>|[\p{L}\p{M}]+/u', $content_without_html_tags, $matches );
618 - $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( array() );
476 + $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( [] );
619 477 $word_count = $total_words;
620 478 echo '<span>' . esc_html( intval( $word_count ) ) . '</span>';
621 479 break;
622 480 case 'betterdocs_reaction':
623 - // 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 ) . "'";
624 482 $analytics = $wpdb->get_results(
625 - $wpdb->prepare(
626 - "SELECT
627 - sum(impressions) as totalViews,
628 - sum(unique_visit) as totalUniqueViews,
629 - sum(happy + sad + normal) as totalReactions,
630 - sum(happy) as totalHappy,
631 - sum(normal) as totalNormal,
632 - sum(sad) as totalSad
633 - FROM {$wpdb->prefix}betterdocs_analytics
634 - WHERE post_id = %d",
635 - $post_id
636 - )
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"
637 492 );
638 493
639 494 echo '<ul class="reactions-count">
640 495 <li>
@@ -690,22 +545,22 @@
690 545 * @return void
691 546 * @since 1.0.0
692 547 */
693 548 public function styles( $hook ) {
694 - $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', array(), 'all' );
549 + $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', [], 'all' );
695 550
696 551 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
697 552 return;
698 553 }
699 554
700 - $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', array(), 'all' );
701 - $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', array(), 'all' );
702 - $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' );
703 558
704 559 /**
705 - * This scripts enqueued for Dashboard App.
706 - */
707 - $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' ] );
708 563 $this->assets->enqueue( 'betterdocs-icons', 'admin/btd-icon/style.css' );
709 564 }
710 565
711 566 /**
@@ -716,70 +571,26 @@
716 571 * @return void
717 572 * @since 1.0.0
718 573 */
719 574 public function scripts( $hook ) {
720 - // Classic-UI screens that should offer a "Switch to BetterDocs UI"
721 - // button: All Docs, FAQ list, FAQ groups, Product FAQ groups,
722 - // Doc Categories, Doc Tags. Maps each to the React admin page to
723 - // return to; $switch_args carries extra query args (e.g. the FAQ
724 - // Builder tab) appended to the React page URL.
725 - $switch_page = '';
726 - $switch_args = array();
727 - if ( 'edit.php' === $hook && 'docs' === get_post_type() ) {
728 - $switch_page = 'betterdocs-admin';
729 - } elseif ( 'edit.php' === $hook && 'betterdocs_faq' === get_post_type() ) {
730 - $switch_page = 'betterdocs-faq';
731 - } elseif ( 'edit-tags.php' === $hook ) {
732 - $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
733 - $taxonomy = $screen && ! empty( $screen->taxonomy )
734 - ? $screen->taxonomy
735 - : ( isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
736 - if ( 'betterdocs_faq_category' === $taxonomy ) {
737 - $switch_page = 'betterdocs-faq';
738 - } elseif ( 'betterdocs_product_faq_category' === $taxonomy ) {
739 - // Product FAQ groups live on the FAQ Builder's WooCommerce tab.
740 - $switch_page = 'betterdocs-faq';
741 - $switch_args = array( 'faq_tab' => 'woocommerce' );
742 - } elseif ( 'doc_category' === $taxonomy ) {
743 - $switch_page = 'betterdocs-doc-categories';
744 - } elseif ( 'doc_tag' === $taxonomy ) {
745 - $switch_page = 'betterdocs-doc-tags';
746 - }
747 - }
748 -
749 - /**
750 - * Allow Pro/add-ons to map their own classic-UI taxonomy screens to a
751 - * React admin page for the "Switch to BetterDocs UI" button — e.g. the
752 - * Knowledge Base taxonomy, which only exists when Pro is active.
753 - *
754 - * @param string $switch_page React page slug, or '' for no switcher.
755 - * @param string $hook Current admin page hook.
756 - */
757 - $switch_page = apply_filters( 'betterdocs_classic_switch_page', $switch_page, $hook );
758 -
759 - if ( $switch_page ) {
575 + if ( ( $hook === 'edit.php' ) && get_post_type() == 'docs' ) {
760 576 $this->assets->enqueue(
761 577 'betterdocs-switcher',
762 578 'admin/js/switcher.js',
763 - array(
764 - 'jquery',
765 - )
579 + [
580 + 'jquery'
581 + ]
766 582 );
767 583
768 584 $this->assets->localize(
769 585 'betterdocs-switcher',
770 586 'betterdocsSwitcher',
771 - array(
587 + [
772 588 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
773 - 'page' => $switch_page,
774 - 'url' => add_query_arg(
775 - array_merge( array( 'page' => $switch_page ), $switch_args ),
776 - admin_url( 'admin.php' )
777 - ),
778 589 'site_address' => get_bloginfo( 'url' ),
779 590 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
780 - 'betterdocs_pro_version' => betterdocs()->pro_version(),
781 - )
591 + 'betterdocs_pro_version' => betterdocs()->pro_version()
592 + ]
782 593 );
783 594
784 595 return;
785 596 }
@@ -789,106 +600,78 @@
789 600 if ( ! betterdocs()->is_betterdocs_screen( $hook ) ) {
790 601 return;
791 602 }
792 603
793 - wp_enqueue_media(); // load early to fix problems with media upload issues on settings for WordPress 6.0.9
794 604 $this->assets->register( 'betterdocs-admin', 'admin/js/dashboard.js' );
795 605
796 606 $saved_settings = get_option( 'betterdocs_settings', false );
797 - $dark_mode = $this->is_dark_mode();
607 + $dark_mode = $saved_settings['dark_mode'] ?? false;
608 + $dark_mode = ! empty( $dark_mode ) && boolval( $dark_mode );
798 609 $this->assets->localize(
799 610 'betterdocs-admin',
800 611 'betterdocs_admin',
801 - array(
612 + [
802 613 'ajaxurl' => admin_url( 'admin-ajax.php' ),
803 614 'doc_cat_order_nonce' => wp_create_nonce( 'doc_cat_order_nonce' ),
804 615 'knowledge_base_order_nonce' => wp_create_nonce( 'knowledge_base_order_nonce' ),
805 - 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- pagination read from URL.
806 - 'per_page_id' => 'edit_doc_category_per_page',
807 - 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
808 - 'dark_mode' => $dark_mode,
809 - 'kb_is_empty' => $this->kb_is_empty(),
810 - 'text' => __( 'Copied!', 'betterdocs' ),
811 - 'test_report' => __( 'Test Report!', 'betterdocs' ),
812 - 'sending' => __( 'Sending...', 'betterdocs' ),
813 - 'dir_url' => BETTERDOCS_ABSURL,
814 - 'rest_url' => esc_url_raw( rest_url() ),
815 - 'free_version' => betterdocs()->version,
816 - 'generate_data_url' => get_rest_url( null, '/betterdocs/v1/create-sample-docs' ),
817 - 'ai_sample_docs' => array(
818 - 'enabled' => (bool) betterdocs()->settings->get( 'enable_ai_sample_docs', true ),
819 - 'rest_base' => esc_url_raw( get_rest_url( null, '/betterdocs/v1/sample-docs' ) ),
820 - ),
821 - 'nonce' => wp_create_nonce( 'wp_rest' ),
822 - 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ),
823 - 'count_all_docs' => array_sum( (array) wp_count_posts( 'docs' ) ),
824 - 'count_all_faq' => array_sum( (array) wp_count_posts( 'betterdocs_faq' ) ),
825 - 'faq_order' => get_option( 'betterdocs_faq_order', 'default' ),
826 - 'count_new_docs' => $this->get_not_synced_docs_count(),
827 - 'admin_url' => admin_url(),
828 - 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ),
829 - 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ),
830 - 'previewMode' => betterdocs()->settings->get( 'ia_enable_preview', false ),
831 - 'dashboard_mode' => get_option( 'dashboard_mode' ),
832 - 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(),
833 - 'betterdocs_pro_version' => betterdocs()->pro_version(),
834 - 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ),
835 - 'betterdocs_ChatBot_plugin' => is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' ),
836 - 'is_woocommerce_active' => class_exists( 'WooCommerce' ),
837 - 'total_doc_category_terms' => wp_count_terms( 'doc_category' ),
838 - 'current_admin_language' => Helper::get_current_admin_language(),
839 - 'is_multilingual' => Helper::is_multilingual_active(),
840 - 'languages' => Helper::get_admin_languages(),
841 - )
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 + ]
842 641 );
843 642
844 643 // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js
845 644 if ( ! wp_script_is( 'wp-date', 'registered' ) ) {
846 - $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', array() );
645 + $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', [] );
847 646 }
848 647 wp_enqueue_script( 'betterdocs-admin' );
849 648
850 649 /**
851 - * Duplicate Codes Need to Be Removed From Here Onwards
852 - */
650 + * Duplicate Codes Need to Be Removed From Here Onwards
651 + */
853 652
854 - // FAQ Builder Related Localization
653 + //FAQ Builder Related Localization
855 654 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/css/faq.css' );
856 655 betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/js/faq.js' );
857 656
858 - // Load the classic editor (TinyMCE + QuickTags) so the FAQ rich-text editor can mount via wp.editor.initialize().
859 - if ( function_exists( 'wp_enqueue_editor' ) ) {
860 - wp_enqueue_editor();
861 - }
862 - if ( function_exists( 'wp_enqueue_media' ) ) {
863 - wp_enqueue_media();
864 - }
865 -
866 657 // removing emoji support
867 658 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
868 659 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
869 660
870 - // Get settings and remove unnecessary keys
871 - $betterdocs_settings = get_option( 'betterdocs_settings', false );
872 - if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) {
873 - foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) {
874 - unset( $betterdocs_settings[ $sensitive_key ] );
875 - }
876 - }
877 -
878 661 betterdocs()->assets->localize(
879 662 'betterdocs-admin-faq',
880 663 'betterdocsFaq',
881 - array(
664 + [
882 665 'dir_url' => BETTERDOCS_ABSURL,
883 666 'rest_url' => esc_url_raw( rest_url() ),
884 667 'free_version' => betterdocs()->version,
885 668 'nonce' => wp_create_nonce( 'wp_rest' ),
886 - 'betterdocs_settings' => $betterdocs_settings,
887 - )
669 + 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
670 + ]
888 671 );
889 672
890 - // Glossaries Related Localization
673 + //Glossaries Related Localization
891 674 betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/css/faq.css' );
892 675
893 676 betterdocs()->assets->enqueue( 'betterdocs-admin-glossaries', 'admin/js/glossaries.js' );
894 677
@@ -894,15 +677,15 @@
894 677
895 678 betterdocs()->assets->localize(
896 679 'betterdocs-admin-glossaries',
897 680 'betterdocsGlossary',
898 - array(
681 + [
899 682 'dir_url' => BETTERDOCS_ABSURL,
900 683 'rest_url' => esc_url_raw( rest_url() ),
901 684 'free_version' => betterdocs()->version,
902 685 'nonce' => wp_create_nonce( 'wp_rest' ),
903 - 'betterdocs_settings' => $betterdocs_settings,
904 - )
686 + 'betterdocs_settings' => get_option( 'betterdocs_settings', false )
687 + ]
905 688 );
906 689 }
907 690
908 691 /**
@@ -911,31 +694,31 @@
911 694 * @return void
912 695 * @since 1.0.0
913 696 */
914 697 public function header( $admin_tab_name ) {
915 - $quick_links = array(
698 + $quick_links = [
916 699 'switch_view' => sprintf(
917 700 '<a href="%s" class="betterdocs-button betterdocs-button-secondary">%s</a>',
918 701 add_query_arg(
919 - array(
702 + [
920 703 'post_type' => 'docs',
921 - 'bdocs_view' => 'classic',
922 - ),
704 + 'bdocs_view' => 'classic'
705 + ],
923 706 'edit.php'
924 707 ),
925 708 __( 'Switch to Classic UI', 'betterdocs' )
926 709 ),
927 - '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' ) ),
928 - );
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 + ];
929 712
930 713 $quick_links = apply_filters( 'betterdocs_quick_links', $quick_links );
931 714
932 715 betterdocs()->views->get(
933 716 'admin/header',
934 - array(
717 + [
935 718 'quick_links' => $quick_links,
936 - 'active_tab' => $admin_tab_name,
937 - )
719 + 'active_tab' => $admin_tab_name
720 + ]
938 721 );
939 722 }
940 723
941 724 /**
@@ -943,77 +726,37 @@
943 726 *
944 727 * @return void
945 728 * @since 1.0.0
946 729 */
730 +
947 731 public function menus() {
948 - $default_args = array(
732 + $default_args = [
949 733 'page_title' => 'BetterDocs',
950 734 'menu_title' => 'BetterDocs',
951 - 'capability' => 'edit_docs', // Unified capability
735 + 'capability' => 'edit_posts',
952 736 'menu_slug' => $this->slug,
953 - 'callback' => array( $this, 'output' ),
737 + 'callback' => [ $this, 'output' ],
954 738 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
955 - 'position' => 5,
956 - );
739 + 'position' => 5
740 + ];
957 741
958 742 $_menu_position = 5;
959 - global $submenu;
960 743
961 - // Always register both UI endpoints
962 - $this->register_modern_ui_fallback();
963 -
964 744 foreach ( $this->menu_list() as $key => $value ) {
965 - if ( 'betterdocs' === $key ) {
745 + if ( $key === 'betterdocs' ) {
966 746 $callable = 'add_menu_page';
967 747 $value = wp_parse_args( $value, $default_args );
968 - call_user_func_array( $callable, $value );
969 748 } else {
970 - $is_core_page = strpos( $value['menu_slug'], '?' ) !== false;
971 -
972 - if ( $is_core_page ) {
973 - // Add classic UI directly
974 - $submenu[ $this->slug ][] = array(
975 - $value['menu_title'],
976 - $value['capability'],
977 - $value['menu_slug'],
978 - $value['page_title'],
979 - );
980 - } else {
981 - // Add modern UI through WordPress API
982 - add_submenu_page(
983 - $this->slug,
984 - $value['page_title'],
985 - $value['menu_title'],
986 - $value['capability'],
987 - $value['menu_slug'],
988 - $value['callback']
989 - );
990 - }
991 - ++$_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 ) );
992 754 }
993 - }
994 - }
995 755
996 - private function register_modern_ui_fallback() {
997 - // Add the submenu with valid parent slug
998 - add_submenu_page(
999 - 'betterdocs', // Valid parent slug
1000 - __( 'All Docs', 'betterdocs' ),
1001 - '', // Empty menu title hides it
1002 - 'edit_docs',
1003 - 'betterdocs-admin',
1004 - array( $this, 'output' )
1005 - );
756 + ++$_menu_position;
1006 757
1007 - // Hide the menu item from appearing in the admin sidebar
1008 - global $submenu;
1009 - if ( isset( $submenu['betterdocs'] ) ) {
1010 - foreach ( $submenu['betterdocs'] as $key => $item ) {
1011 - if ( 'betterdocs-admin' === $item[2] ) {
1012 - unset( $submenu['betterdocs'][ $key ] );
1013 - break;
1014 - }
1015 - }
758 + call_user_func_array( $callable, $value );
1016 759 }
1017 760 }
1018 761
1019 762 /**
@@ -1023,17 +766,17 @@
1023 766 * @since 1.0.0
1024 767 */
1025 768 public function output() {
1026 769 if ( betterdocs()->is_pro_active()
1027 - && version_compare( betterdocs()->pro_version(), '3.3.4', '<=' )
1028 - && 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' ) {
1029 772 betterdocs_pro()->views->get( 'admin/analytics-pro' );
1030 773 } else {
1031 774 betterdocs()->views->get(
1032 775 'admin/main',
1033 - array(
1034 - 'admin_ui' => 'dnd',
1035 - )
776 + [
777 + 'admin_ui' => 'dnd'
778 + ]
1036 779 );
1037 780 }
1038 781 }
1039 782
@@ -1046,10 +789,11 @@
1046 789 * @param array $callback
1047 790 *
1048 791 * @return array
1049 792 * @since 2.5.0
793 + *
1050 794 */
1051 - 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 = [] ) {
1052 796 return Helper::normalize_menu( $title, $slug, $cap, $callback, $optional );
1053 797 }
1054 798
1055 799 /**
@@ -1058,62 +802,32 @@
1058 802 * @return array
1059 803 * @since 1.0.0
1060 804 */
1061 805 private function menu_list() {
1062 - $parent_slug = array();
806 + $parent_slug = [];
1063 807
1064 - $betterdocs_admin_pages = array(
1065 - 'betterdocs' => array(
808 + $betterdocs_admin_pages = [
809 + 'betterdocs' => [
1066 810 'menu_slug' => $this->slug,
1067 811 'page_title' => 'BetterDocs',
1068 812 'menu_title' => 'BetterDocs',
1069 813 'capability' => 'edit_docs',
1070 - 'callback' => array( $this, 'output' ),
814 + 'callback' => [ $this, 'output' ],
1071 815 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
1072 - 'position' => 5,
1073 - ),
1074 - 'dashboard' => $this->normalize_menu(
1075 - __( 'Dashboard', 'betterdocs' ),
1076 - 'betterdocs-dashboard',
1077 - 'edit_docs',
1078 - array(
1079 - $this,
1080 - 'output',
1081 - )
1082 - ),
1083 - 'all_docs' => $this->normalize_menu(
1084 - __( 'All Docs', 'betterdocs' ),
1085 - $this->ui_slug(),
1086 - 'edit_docs',
1087 - array( $this, 'output' ),
1088 - $parent_slug
1089 - ),
1090 - 'add_new' => $this->normalize_menu(
1091 - __( 'Add New', 'betterdocs' ),
1092 - 'post-new.php?post_type=docs'
1093 - ),
1094 - 'categories' => $this->normalize_menu(
1095 - __( 'Categories', 'betterdocs' ),
1096 - 'betterdocs-doc-categories',
1097 - 'manage_doc_terms',
1098 - array( $this, 'output' ),
1099 - $parent_slug
1100 - ),
1101 - 'tags' => $this->normalize_menu(
1102 - __( 'Tags', 'betterdocs' ),
1103 - 'betterdocs-doc-tags',
1104 - 'manage_doc_terms',
1105 - array( $this, 'output' ),
1106 - $parent_slug
1107 - ),
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' ),
1108 822 'settings' => $this->normalize_menu(
1109 823 __( 'Settings', 'betterdocs' ),
1110 824 'betterdocs-settings',
1111 825 'edit_docs_settings',
1112 - array(
826 + [
1113 827 $this,
1114 - 'output',
1115 - ),
828 + 'output'
829 + ],
1116 830 $parent_slug
1117 831 ),
1118 832 'analytics' => $this->normalize_menu(
1119 833 __( 'Analytics', 'betterdocs' ),
@@ -1118,25 +832,25 @@
1118 832 'analytics' => $this->normalize_menu(
1119 833 __( 'Analytics', 'betterdocs' ),
1120 834 'betterdocs-analytics',
1121 835 'read_docs_analytics',
1122 - array(
836 + [
1123 837 $this,
1124 - 'output',
1125 - ),
838 + 'output'
839 + ],
1126 840 $parent_slug
1127 841 ),
1128 842 'faq' => $this->normalize_menu(
1129 843 __( 'FAQ Builder', 'betterdocs' ),
1130 844 'betterdocs-faq',
1131 - 'read_faq_builder',
1132 - array(
845 + 'read_docs_analytics',
846 + [
1133 847 $this,
1134 - 'output',
1135 - ),
848 + 'output'
849 + ],
1136 850 $parent_slug
1137 - ),
1138 - );
851 + )
852 + ];
1139 853
1140 854 if ( betterdocs()->is_pro_active() && betterdocs()->settings->get( 'enable_glossaries' ) == true ) {
1141 855 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1142 856 __( 'Glossaries', 'betterdocs' ),
@@ -1141,48 +855,36 @@
1141 855 $betterdocs_admin_pages['glossaries'] = $this->normalize_menu(
1142 856 __( 'Glossaries', 'betterdocs' ),
1143 857 'betterdocs-glossaries',
1144 858 'read_docs_analytics',
1145 - array(
859 + [
1146 860 $this,
1147 - 'output',
1148 - ),
861 + 'output'
862 + ],
1149 863 $parent_slug
1150 864 );
1151 865 }
1152 866
1153 - if ( ! betterdocs()->is_chatbot_active() ) {
1154 - $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu(
1155 - __( 'AI Chatbot', 'betterdocs' ),
1156 - 'betterdocs-ai-chatbot',
1157 - 'edit_docs_settings',
1158 - array(
1159 - $this,
1160 - 'output',
1161 - ),
1162 - $parent_slug
1163 - );
1164 - }
867 + return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, [$this, 'output'], $parent_slug);
1165 868
1166 - return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug );
1167 - }
869 + }
1168 870
1169 871 public function add_custom_classes_to_menu_items() {
1170 872 global $menu, $submenu;
1171 873
1172 - $menu_items = array(
874 + $menu_items = [
1173 875 'betterdocs' => 'betterdocs',
1174 876 'betterdocs_page_all_docs' => 'betterdocs-all-docs',
1175 877 'betterdocs_page_add_new' => 'betterdocs-add-new',
1176 - 'betterdocs-doc-categories' => 'betterdocs-categories',
1177 - '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',
1178 880 'betterdocs-settings' => 'betterdocs-settings',
1179 881 'betterdocs-analytics' => 'betterdocs-analytics',
1180 882 'betterdocs-faq' => 'betterdocs-faq',
1181 883 'betterdocs-glossaries' => 'betterdocs-glossaries',
1182 884 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot',
1183 - 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb',
1184 - );
885 + 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb'
886 + ];
1185 887
1186 888 foreach ( $menu as &$item ) {
1187 889 if ( isset( $menu_items[ $item[2] ] ) ) {
1188 890 if ( ! isset( $item[4] ) ) {
@@ -1212,12 +914,12 @@
1212 914 $menus['quick_setup'] = $this->normalize_menu(
1213 915 __( 'Quick Setup', 'betterdocs' ),
1214 916 'betterdocs-setup',
1215 917 'delete_users',
1216 - array(
918 + [
1217 919 $this->container->get( SetupWizard::class ),
1218 - 'views',
1219 - )
920 + 'views'
921 + ]
1220 922 );
1221 923 }
1222 924
1223 925 return $menus;
@@ -1225,12 +927,8 @@
1225 927
1226 928 public function insert_plugin_links( $links ) {
1227 929 $links[] = '<a href="admin.php?page=betterdocs-settings">' . __( 'Settings', 'betterdocs' ) . '</a>';
1228 930
1229 - if ( ! is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' ) ) {
1230 - $links[] = '<a href="https://betterdocs.co/upgrade-to-pro-plugins-wp" target="_blank" style="color: #000; font-weight: bold;">' . __( 'Upgrade to Pro', 'betterdocs' ) . '</a>';
1231 - }
1232 -
1233 931 return $links;
1234 932 }
1235 933
1236 934 public function toolbar_menu( $admin_bar ) {
@@ -1257,22 +955,17 @@
1257 955 }
1258 956
1259 957 $slug = $this->settings->get( 'encyclopedia_root_slug' );
1260 958
1261 - global $wp_rewrite;
1262 - if ( $wp_rewrite->using_index_permalinks() ) {
1263 - $slug = $wp_rewrite->index . '/' . $slug;
1264 - }
1265 -
1266 959 $encyclopedia_url = home_url( $slug );
1267 960
1268 961 $admin_bar->add_node(
1269 - array(
962 + [
1270 963 'parent' => 'site-name',
1271 964 'id' => 'view-docs',
1272 965 'title' => __( 'Visit Documentation', 'betterdocs' ),
1273 - 'href' => $docs_url,
1274 - )
966 + 'href' => $docs_url
967 + ]
1275 968 );
1276 969
1277 970 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1278 971
@@ -1277,14 +970,14 @@
1277 970 $is_enable_encyclopedia = betterdocs()->settings->get( 'enable_encyclopedia' );
1278 971
1279 972 if ( $is_enable_encyclopedia && betterdocs()->is_pro_active() ) {
1280 973 $admin_bar->add_node(
1281 - array(
974 + [
1282 975 'parent' => 'site-name',
1283 976 'id' => 'view-encyclopedia',
1284 977 'title' => __( 'Visit Encyclopedia', 'betterdocs' ),
1285 - 'href' => $encyclopedia_url,
1286 - )
978 + 'href' => $encyclopedia_url
979 + ]
1287 980 );
1288 981 }
1289 982 }
1290 983
@@ -1291,20 +984,14 @@
1291 984 /**
1292 985 * Save last visited admin ui
1293 986 *
1294 987 * @since 3.0.1
988 + *
1295 989 */
1296 990 public function save_admin_page() {
1297 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1298 - $post_type = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : '';
1299 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1300 - $bdocs_view = isset( $_GET['bdocs_view'] ) ? sanitize_text_field( wp_unslash( $_GET['bdocs_view'] ) ) : '';
1301 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen detection.
1302 - $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
1303 -
1304 - 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
1305 992 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'classic_ui' );
1306 - } elseif ( 'betterdocs-admin' === $page ) {
993 + } elseif ( isset( $_GET['page'] ) && $_GET['page'] === 'betterdocs-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1307 994 update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'modern_ui' );
1308 995 }
1309 996 }
1310 997
@@ -1315,19 +1002,23 @@
1315 1002 * @since 3.0.1
1316 1003 */
1317 1004 public function ui_slug() {
1318 1005 $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1319 - $docs_exist = get_posts(
1320 - array(
1006 + $docs = get_posts(
1007 + [
1321 1008 'post_type' => 'docs',
1322 1009 'post_status' => 'any',
1323 - 'numberposts' => 1,
1324 - )
1010 + 'numberposts' => 2
1011 + ]
1325 1012 );
1326 1013
1327 - return ( 'modern_ui' === $last_visited || empty( $docs_exist ) )
1328 - ? 'betterdocs-admin'
1329 - : '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;
1330 1021 }
1331 1022
1332 1023 /**
1333 1024 * Resets a duplicate submenu in WordPress if the parent main menu and the first submenu permalink are not the same.
@@ -1337,9 +1028,9 @@
1337 1028 */
1338 1029 public function reset_submenu() {
1339 1030 global $submenu;
1340 1031
1341 - $docs = get_posts( array( 'post_type' => 'docs' ) );
1032 + $docs = get_posts( [ 'post_type' => 'docs' ] );
1342 1033 if ( count( $docs ) == 0 ) {
1343 1034 return;
1344 1035 }
1345 1036
@@ -1344,89 +1035,10 @@
1344 1035 }
1345 1036
1346 1037 $last_visited = get_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', true );
1347 1038
1348 - 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] ) ) {
1349 1040 unset( $submenu['betterdocs-admin'][0] );
1350 1041 $submenu['betterdocs-admin'] = array_values( $submenu['betterdocs-admin'] );
1351 1042 }
1352 - }
1353 -
1354 - /**
1355 - * Initialize Black Friday Pointer
1356 - *
1357 - * @return void
1358 - * @since 3.7.0
1359 - */
1360 - private function init_black_friday_pointer() {
1361 - // Only initialize if conditions are met
1362 - if ( NoticePointers::should_display_notice() ) {
1363 - new NoticePointers();
1364 - }
1365 - }
1366 -
1367 - /**
1368 - * AJAX handler for dismissing Black Friday pointer
1369 - *
1370 - * @return void
1371 - * @since 3.7.0
1372 - */
1373 - public function ajax_dismiss_black_friday_pointer() {
1374 - // Verify nonce
1375 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
1376 - if ( ! wp_verify_nonce( $nonce, 'betterdocs_dismiss_pointer' ) ) {
1377 - wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'betterdocs' ) ) );
1378 - }
1379 -
1380 - // Check if user has permission
1381 - if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'edit_docs' ) ) {
1382 - wp_send_json_error( array( 'message' => __( 'Permission denied', 'betterdocs' ) ) );
1383 - }
1384 -
1385 - // Get the introduction key
1386 - $introduction_key = isset( $_POST['introduction_key'] ) ? sanitize_text_field( wp_unslash( $_POST['introduction_key'] ) ) : '';
1387 -
1388 - if ( empty( $introduction_key ) ) {
1389 - wp_send_json_error( array( 'message' => __( 'Invalid introduction key', 'betterdocs' ) ) );
1390 - }
1391 -
1392 - // Set the introduction as viewed
1393 - NoticePointers::set_introduction_viewed( $introduction_key );
1394 -
1395 - // Clear the priority option so other plugins can set their priority
1396 - delete_option( '_wpdeveloper_plugin_pointer_priority' );
1397 -
1398 - wp_send_json_success( array( 'message' => __( 'Pointer dismissed successfully', 'betterdocs' ) ) );
1399 - }
1400 -
1401 - /**
1402 - * Get count of docs that are not yet synced
1403 - * Similar to Helper::get_not_synced_post_ids() in betterdocs-ai-chatbot plugin
1404 - *
1405 - * @return int
1406 - * @since 3.7.0
1407 - */
1408 - private function get_not_synced_docs_count() {
1409 - $new_post_ids = get_option( 'saved_docs_post_ids', array() );
1410 - $error_posts_data = get_option( 'betterdocs_ai_chatbot_error_posts', array() );
1411 -
1412 - // Extract post IDs from error posts (handle both old and new formats)
1413 - $error_post_ids = array();
1414 - if ( is_array( $error_posts_data ) && ! empty( $error_posts_data ) ) {
1415 - foreach ( $error_posts_data as $key => $value ) {
1416 - if ( is_numeric( $key ) && is_numeric( $value ) ) {
1417 - // Old format: numeric key with post ID as value
1418 - $error_post_ids[] = $value;
1419 - } elseif ( is_numeric( $key ) && is_array( $value ) && isset( $value['post_id'] ) ) {
1420 - // New format: post ID as key with structured data
1421 - $error_post_ids[] = $key;
1422 - } elseif ( is_numeric( $key ) ) {
1423 - // New format: post ID as key
1424 - $error_post_ids[] = $key;
1425 - }
1426 - }
1427 - }
1428 -
1429 - // Merge both arrays and remove duplicates, then count
1430 - return count( array_unique( array_merge( $new_post_ids, $error_post_ids ) ) );
1431 1043 }
1432 1044 }