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