| @@ -1,8 +1,12 @@ | ||
| 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; |
| 7 | 11 | use WPDeveloper\BetterDocs\Admin\NoticePointers; |
| 8 | 12 | use WPDeveloper\BetterDocs\Utils\Base; |
| @@ -8,9 +12,9 @@ | ||
| 8 | 12 | use WPDeveloper\BetterDocs\Utils\Base; |
| 9 | 13 | use PriyoMukul\WPNotice\Utils\CacheBank; |
| 10 | 14 | use WPDeveloper\BetterDocs\Utils\Helper; |
| 11 | 15 | use WPDeveloper\BetterDocs\Utils\Enqueue; |
| 12 | -use WPDeveloper\BetterDocs\Utils\Insights; | |
| 16 | +use WPDeveloper\BetterDocs\Insights\Insights; | |
| 13 | 17 | use PriyoMukul\WPNotice\Utils\NoticeRemover; |
| 14 | 18 | use WPDeveloper\BetterDocs\Core\PluginInstaller; |
| 15 | 19 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 16 | 20 | |
| @@ -15,8 +19,42 @@ | ||
| 15 | 19 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 16 | 20 | |
| 17 | 21 | class Admin extends Base { |
| 18 | 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 | + /** | |
| 19 | 57 | * @var CacheBank |
| 20 | 58 | */ |
| 21 | 59 | private static $cache_bank; |
| 22 | 60 | /** |
| @@ -86,8 +124,18 @@ | ||
| 86 | 124 | |
| 87 | 125 | $this->faq_builder = $this->container->get( FAQBuilder::class ); |
| 88 | 126 | $this->glossaries = $this->container->get( Glossaries::class ); |
| 89 | 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 | + | |
| 90 | 138 | if ( ! is_admin() ) { |
| 91 | 139 | return; |
| 92 | 140 | } |
| 93 | 141 | |
| @@ -92,16 +140,32 @@ | ||
| 92 | 140 | } |
| 93 | 141 | |
| 94 | 142 | $this->installer = new PluginInstaller(); |
| 95 | 143 | |
| 96 | - $this->plugin_insights(); | |
| 97 | 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 ); | |
| 98 | 155 | // add_action( 'admin_init', [$this, 'notices'], 9 ); |
| 99 | 156 | add_filter( 'admin_init', array( $this, 'save_admin_page' ), 99 ); |
| 100 | 157 | |
| 101 | 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' ) ); | |
| 102 | 165 | add_action( 'admin_menu', array( $this, 'reset_submenu' ) ); |
| 103 | 166 | add_action( 'admin_head', array( $this, 'add_custom_classes_to_menu_items' ) ); |
| 167 | + add_action( 'admin_head', array( $this, 'mcp_badge_styles' ) ); | |
| 104 | 168 | add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, array( $this, 'insert_plugin_links' ) ); |
| 105 | 169 | |
| 106 | 170 | // $this->container->get( SetupWizard::class )->init(); |
| 107 | 171 | |
| @@ -113,8 +177,10 @@ | ||
| 113 | 177 | add_filter( 'admin_body_class', array( $this, 'body_classes' ) ); |
| 114 | 178 | add_filter( 'parent_file', array( $type, 'highlight_admin_menu' ) ); |
| 115 | 179 | add_filter( 'submenu_file', array( $type, 'highlight_admin_submenu' ), 10, 2 ); |
| 116 | 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 ); | |
| 117 | 183 | |
| 118 | 184 | /** |
| 119 | 185 | * Remove Comments Column from List Table. |
| 120 | 186 | */ |
| @@ -236,9 +302,11 @@ | ||
| 236 | 302 | if ( betterdocs()->is_pro_active() ) { |
| 237 | 303 | $plugins = Helper::get_plugins(); |
| 238 | 304 | $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php']; |
| 239 | 305 | |
| 240 | - 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', '>=' ) ) { | |
| 241 | 309 | return; |
| 242 | 310 | } |
| 243 | 311 | |
| 244 | 312 | betterdocs()->views->get( 'admin/notices/compatibility', array( 'version' => $plugin_data['Version'] ) ); |
| @@ -467,8 +535,11 @@ | ||
| 467 | 535 | 'betterdocs_page_betterdocs-faq', |
| 468 | 536 | 'betterdocs_page_betterdocs-analytics', |
| 469 | 537 | 'betterdocs_page_betterdocs-glossaries', |
| 470 | 538 | 'betterdocs_page_betterdocs-ai-chatbot', |
| 539 | + 'betterdocs_page_betterdocs-api-docs', | |
| 540 | + 'betterdocs_page_betterdocs-doc-categories', | |
| 541 | + 'betterdocs_page_betterdocs-doc-tags', | |
| 471 | 542 | 'edit-doc_category', |
| 472 | 543 | 'edit-doc_tag', |
| 473 | 544 | ), |
| 474 | 545 | $notices, |
| @@ -476,31 +547,93 @@ | ||
| 476 | 547 | ); |
| 477 | 548 | } |
| 478 | 549 | } |
| 479 | 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 | + | |
| 480 | 595 | public function body_classes( $classes ) { |
| 481 | - $saved_settings = get_option( 'betterdocs_settings', false ); | |
| 482 | - $dark_mode = isset( $saved_settings['dark_mode'] ) ? $saved_settings['dark_mode'] : false; | |
| 483 | - $dark_mode = ! empty( $dark_mode ) ? boolval( $dark_mode ) : false; | |
| 596 | + $dark_mode = $this->is_dark_mode(); | |
| 484 | 597 | $current_screen_id = get_current_screen() != null ? str_replace( 'betterdocs_page_', '', str_replace( 'toplevel_page_', '', str_replace( 'admin_page_', '', get_current_screen()->id ) ) ) : ''; |
| 485 | - $registered_screens = array( | |
| 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( | |
| 486 | 606 | 'betterdocs-settings', |
| 487 | 607 | 'betterdocs-admin', |
| 488 | 608 | 'betterdocs-dashboard', |
| 489 | 609 | 'betterdocs-analytics', |
| 610 | + 'betterdocs-content-iq', | |
| 490 | 611 | 'betterdocs-glossaries', |
| 491 | 612 | 'betterdocs-faq', |
| 613 | + 'betterdocs-doc-categories', | |
| 614 | + 'betterdocs-doc-tags', | |
| 492 | 615 | 'edit-doc_category', |
| 493 | 616 | 'edit-doc_tag', |
| 494 | 617 | 'edit-knowledge_base', |
| 495 | 618 | 'betterdocs-ai-chatbot', |
| 496 | - ); | |
| 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 | + ) ); | |
| 497 | 627 | |
| 498 | 628 | if ( in_array( $current_screen_id, $registered_screens ) ) { |
| 499 | 629 | $classes .= ' betterdocs-admin '; |
| 500 | 630 | } |
| 501 | 631 | |
| 502 | - if ( true === $dark_mode && 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 ) ) { | |
| 503 | 636 | $classes .= ' betterdocs-dark-mode '; |
| 504 | 637 | } |
| 505 | 638 | |
| 506 | 639 | return $classes; |
| @@ -531,9 +664,9 @@ | ||
| 531 | 664 | public function manage_custom_columns( $column, $post_id ) { |
| 532 | 665 | global $wpdb; |
| 533 | 666 | switch ( $column ) { |
| 534 | 667 | case 'betterdocs_word_count': |
| 535 | - $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 ) ) ); | |
| 536 | 669 | preg_match_all( '/<[^>]*>|[\p{L}\p{M}]+/u', $content_without_html_tags, $matches ); |
| 537 | 670 | $total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( array() ); |
| 538 | 671 | $word_count = $total_words; |
| 539 | 672 | echo '<span>' . esc_html( intval( $word_count ) ) . '</span>'; |
| @@ -538,19 +671,22 @@ | ||
| 538 | 671 | $word_count = $total_words; |
| 539 | 672 | echo '<span>' . esc_html( intval( $word_count ) ) . '</span>'; |
| 540 | 673 | break; |
| 541 | 674 | case 'betterdocs_reaction': |
| 542 | - $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. | |
| 543 | 676 | $analytics = $wpdb->get_results( |
| 544 | - "SELECT | |
| 545 | - sum(impressions) as totalViews, | |
| 546 | - sum(unique_visit) as totalUniqueViews, | |
| 547 | - sum(happy + sad + normal) as totalReactions, | |
| 548 | - sum(happy) as totalHappy, | |
| 549 | - sum(normal) as totalNormal, | |
| 550 | - sum(sad) as totalSad | |
| 551 | - FROM {$wpdb->prefix}betterdocs_analytics | |
| 552 | - $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 | + ) | |
| 553 | 689 | ); |
| 554 | 690 | |
| 555 | 691 | echo '<ul class="reactions-count"> |
| 556 | 692 | <li> |
| @@ -632,9 +768,48 @@ | ||
| 632 | 768 | * @return void |
| 633 | 769 | * @since 1.0.0 |
| 634 | 770 | */ |
| 635 | 771 | public function scripts( $hook ) { |
| 636 | - if ( ( 'edit.php' === $hook ) && 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 ) { | |
| 637 | 812 | $this->assets->enqueue( |
| 638 | 813 | 'betterdocs-switcher', |
| 639 | 814 | 'admin/js/switcher.js', |
| 640 | 815 | array( |
| @@ -646,8 +821,13 @@ | ||
| 646 | 821 | 'betterdocs-switcher', |
| 647 | 822 | 'betterdocsSwitcher', |
| 648 | 823 | array( |
| 649 | 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 | + ), | |
| 650 | 830 | 'site_address' => get_bloginfo( 'url' ), |
| 651 | 831 | 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(), |
| 652 | 832 | 'betterdocs_pro_version' => betterdocs()->pro_version(), |
| 653 | 833 | ) |
| @@ -665,10 +845,9 @@ | ||
| 665 | 845 | wp_enqueue_media(); // load early to fix problems with media upload issues on settings for WordPress 6.0.9 |
| 666 | 846 | $this->assets->register( 'betterdocs-admin', 'admin/js/dashboard.js' ); |
| 667 | 847 | |
| 668 | 848 | $saved_settings = get_option( 'betterdocs_settings', false ); |
| 669 | - $dark_mode = $saved_settings['dark_mode'] ?? false; | |
| 670 | - $dark_mode = ! empty( $dark_mode ) && boolval( $dark_mode ); | |
| 849 | + $dark_mode = $this->is_dark_mode(); | |
| 671 | 850 | $this->assets->localize( |
| 672 | 851 | 'betterdocs-admin', |
| 673 | 852 | 'betterdocs_admin', |
| 674 | 853 | array( |
| @@ -674,12 +853,13 @@ | ||
| 674 | 853 | array( |
| 675 | 854 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 676 | 855 | 'doc_cat_order_nonce' => wp_create_nonce( 'doc_cat_order_nonce' ), |
| 677 | 856 | 'knowledge_base_order_nonce' => wp_create_nonce( 'knowledge_base_order_nonce' ), |
| 678 | - 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 857 | + 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- pagination read from URL. | |
| 679 | 858 | 'per_page_id' => 'edit_doc_category_per_page', |
| 680 | 859 | 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ), |
| 681 | 860 | 'dark_mode' => $dark_mode, |
| 861 | + 'kb_is_empty' => $this->kb_is_empty(), | |
| 682 | 862 | 'text' => __( 'Copied!', 'betterdocs' ), |
| 683 | 863 | 'test_report' => __( 'Test Report!', 'betterdocs' ), |
| 684 | 864 | 'sending' => __( 'Sending...', 'betterdocs' ), |
| 685 | 865 | 'dir_url' => BETTERDOCS_ABSURL, |
| @@ -685,12 +865,17 @@ | ||
| 685 | 865 | 'dir_url' => BETTERDOCS_ABSURL, |
| 686 | 866 | 'rest_url' => esc_url_raw( rest_url() ), |
| 687 | 867 | 'free_version' => betterdocs()->version, |
| 688 | 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 | + ), | |
| 689 | 873 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 690 | 874 | 'sync_nonce' => wp_create_nonce( 'ai_chatbot_embed' ), |
| 691 | 875 | 'count_all_docs' => array_sum( (array) wp_count_posts( 'docs' ) ), |
| 692 | 876 | 'count_all_faq' => array_sum( (array) wp_count_posts( 'betterdocs_faq' ) ), |
| 877 | + 'faq_order' => get_option( 'betterdocs_faq_order', 'default' ), | |
| 693 | 878 | 'count_new_docs' => $this->get_not_synced_docs_count(), |
| 694 | 879 | 'admin_url' => admin_url(), |
| 695 | 880 | 'ia_preview' => betterdocs()->settings->get( 'ia_enable_preview', false ), |
| 696 | 881 | 'multiple_kb' => betterdocs()->settings->get( 'multiple_kb' ), |
| @@ -698,13 +883,31 @@ | ||
| 698 | 883 | 'dashboard_mode' => get_option( 'dashboard_mode' ), |
| 699 | 884 | 'betterdocs_pro_plugin' => betterdocs()->is_pro_active(), |
| 700 | 885 | 'betterdocs_pro_version' => betterdocs()->pro_version(), |
| 701 | 886 | 'analytics_older' => version_compare( betterdocs()->pro_version(), '3.3.4', '<=' ), |
| 702 | - 'disabled_embed_model_option' => get_option( 'disabled_embed_model_option' ), | |
| 703 | 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' ), | |
| 704 | 892 | 'total_doc_category_terms' => wp_count_terms( 'doc_category' ), |
| 705 | 893 | 'current_admin_language' => Helper::get_current_admin_language(), |
| 706 | 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 | + ), | |
| 707 | 910 | ) |
| 708 | 911 | ); |
| 709 | 912 | |
| 710 | 913 | // If wp-date (which includes moment.js) is not registered, enqueue your custom moment.js |
| @@ -720,8 +923,16 @@ | ||
| 720 | 923 | // FAQ Builder Related Localization |
| 721 | 924 | betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/css/faq.css' ); |
| 722 | 925 | betterdocs()->assets->enqueue( 'betterdocs-admin-faq', 'admin/js/faq.js' ); |
| 723 | 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 | + | |
| 724 | 935 | // removing emoji support |
| 725 | 936 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 726 | 937 | remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 727 | 938 | |
| @@ -727,10 +938,11 @@ | ||
| 727 | 938 | |
| 728 | 939 | // Get settings and remove unnecessary keys |
| 729 | 940 | $betterdocs_settings = get_option( 'betterdocs_settings', false ); |
| 730 | 941 | if ( is_array( $betterdocs_settings ) && ! current_user_can( 'edit_docs_settings' ) ) { |
| 731 | - unset( $betterdocs_settings['ai_autowrite_api_key'] ); | |
| 732 | - unset( $betterdocs_settings['ai_chatbot_api_key'] ); | |
| 942 | + foreach ( Settings::sensitive_api_key_fields() as $sensitive_key ) { | |
| 943 | + unset( $betterdocs_settings[ $sensitive_key ] ); | |
| 944 | + } | |
| 733 | 945 | } |
| 734 | 946 | |
| 735 | 947 | betterdocs()->assets->localize( |
| 736 | 948 | 'betterdocs-admin-faq', |
| @@ -817,8 +1029,13 @@ | ||
| 817 | 1029 | |
| 818 | 1030 | // Always register both UI endpoints |
| 819 | 1031 | $this->register_modern_ui_fallback(); |
| 820 | 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 | + | |
| 821 | 1038 | foreach ( $this->menu_list() as $key => $value ) { |
| 822 | 1039 | if ( 'betterdocs' === $key ) { |
| 823 | 1040 | $callable = 'add_menu_page'; |
| 824 | 1041 | $value = wp_parse_args( $value, $default_args ); |
| @@ -847,10 +1064,203 @@ | ||
| 847 | 1064 | } |
| 848 | 1065 | ++$_menu_position; |
| 849 | 1066 | } |
| 850 | 1067 | } |
| 1068 | + | |
| 1069 | + $this->paint_mcp_badge(); | |
| 851 | 1070 | } |
| 852 | 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 | + | |
| 853 | 1263 | private function register_modern_ui_fallback() { |
| 854 | 1264 | // Add the submenu with valid parent slug |
| 855 | 1265 | add_submenu_page( |
| 856 | 1266 | 'betterdocs', // Valid parent slug |
| @@ -949,15 +1359,19 @@ | ||
| 949 | 1359 | 'post-new.php?post_type=docs' |
| 950 | 1360 | ), |
| 951 | 1361 | 'categories' => $this->normalize_menu( |
| 952 | 1362 | __( 'Categories', 'betterdocs' ), |
| 953 | - 'edit-tags.php?taxonomy=doc_category&post_type=docs', | |
| 954 | - 'manage_doc_terms' | |
| 1363 | + 'betterdocs-doc-categories', | |
| 1364 | + 'manage_doc_terms', | |
| 1365 | + array( $this, 'output' ), | |
| 1366 | + $parent_slug | |
| 955 | 1367 | ), |
| 956 | 1368 | 'tags' => $this->normalize_menu( |
| 957 | 1369 | __( 'Tags', 'betterdocs' ), |
| 958 | - 'edit-tags.php?taxonomy=doc_tag&post_type=docs', | |
| 959 | - 'manage_doc_terms' | |
| 1370 | + 'betterdocs-doc-tags', | |
| 1371 | + 'manage_doc_terms', | |
| 1372 | + array( $this, 'output' ), | |
| 1373 | + $parent_slug | |
| 960 | 1374 | ), |
| 961 | 1375 | 'settings' => $this->normalize_menu( |
| 962 | 1376 | __( 'Settings', 'betterdocs' ), |
| 963 | 1377 | 'betterdocs-settings', |
| @@ -967,8 +1381,18 @@ | ||
| 967 | 1381 | 'output', |
| 968 | 1382 | ), |
| 969 | 1383 | $parent_slug |
| 970 | 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 | + ), | |
| 971 | 1395 | 'analytics' => $this->normalize_menu( |
| 972 | 1396 | __( 'Analytics', 'betterdocs' ), |
| 973 | 1397 | 'betterdocs-analytics', |
| 974 | 1398 | 'read_docs_analytics', |
| @@ -977,8 +1401,18 @@ | ||
| 977 | 1401 | 'output', |
| 978 | 1402 | ), |
| 979 | 1403 | $parent_slug |
| 980 | 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 | + ), | |
| 981 | 1415 | 'faq' => $this->normalize_menu( |
| 982 | 1416 | __( 'FAQ Builder', 'betterdocs' ), |
| 983 | 1417 | 'betterdocs-faq', |
| 984 | 1418 | 'read_faq_builder', |
| @@ -989,13 +1423,25 @@ | ||
| 989 | 1423 | $parent_slug |
| 990 | 1424 | ), |
| 991 | 1425 | ); |
| 992 | 1426 | |
| 993 | - 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 ) ) { | |
| 994 | 1440 | $betterdocs_admin_pages['glossaries'] = $this->normalize_menu( |
| 995 | 1441 | __( 'Glossaries', 'betterdocs' ), |
| 996 | 1442 | 'betterdocs-glossaries', |
| 997 | - 'read_docs_analytics', | |
| 1443 | + betterdocs()->show_glossary_teaser() ? 'manage_options' : 'read_docs_analytics', | |
| 998 | 1444 | array( |
| 999 | 1445 | $this, |
| 1000 | 1446 | 'output', |
| 1001 | 1447 | ), |
| @@ -1002,8 +1448,24 @@ | ||
| 1002 | 1448 | $parent_slug |
| 1003 | 1449 | ); |
| 1004 | 1450 | } |
| 1005 | 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 | + } | |
| 1467 | + | |
| 1006 | 1468 | if ( ! betterdocs()->is_chatbot_active() ) { |
| 1007 | 1469 | $betterdocs_admin_pages['ai_chatbot'] = $this->normalize_menu( |
| 1008 | 1470 | __( 'AI Chatbot', 'betterdocs' ), |
| 1009 | 1471 | 'betterdocs-ai-chatbot', |
| @@ -1018,8 +1480,70 @@ | ||
| 1018 | 1480 | |
| 1019 | 1481 | return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages, array( $this, 'output' ), $parent_slug ); |
| 1020 | 1482 | } |
| 1021 | 1483 | |
| 1484 | + /** | |
| 1485 | + * Put the BetterDocs submenu in a deliberate order. | |
| 1486 | + * | |
| 1487 | + * Order used to be an accident of *when* each item was added: Free declares | |
| 1488 | + * most of them inline, and reserves in-place slots for `glossaries` / | |
| 1489 | + * `api_docs` so Pro can overwrite the key without moving it. Anything added | |
| 1490 | + * purely through this filter, though, could only land at the end — which is | |
| 1491 | + * why Multiple KB (Pro, priority 100) and AI Chatbot Logs sat after | |
| 1492 | + * everything else regardless of where they belong. | |
| 1493 | + * | |
| 1494 | + * Sorting here, at priority 999, fixes that for every source at once: Free's | |
| 1495 | + * own entries, Pro's, and any add-on's. Knowledge Base now follows Tags (it | |
| 1496 | + * is the third taxonomy-ish thing, so it belongs with Categories and Tags | |
| 1497 | + * rather than past Analytics), and API Docs follows Knowledge Base. | |
| 1498 | + * | |
| 1499 | + * Keys not listed keep their relative order and are appended, so an add-on | |
| 1500 | + * that registers something unknown to this list is never dropped. | |
| 1501 | + * | |
| 1502 | + * @param array $pages Menu pages keyed by slug id. | |
| 1503 | + * @return array | |
| 1504 | + */ | |
| 1505 | + public function order_admin_menu( $pages ) { | |
| 1506 | + if ( ! is_array( $pages ) ) { | |
| 1507 | + return $pages; | |
| 1508 | + } | |
| 1509 | + | |
| 1510 | + $order = array( | |
| 1511 | + 'betterdocs', | |
| 1512 | + 'dashboard', | |
| 1513 | + 'all_docs', | |
| 1514 | + 'add_new', | |
| 1515 | + 'categories', | |
| 1516 | + 'tags', | |
| 1517 | + 'multiple_kb', | |
| 1518 | + 'api_docs', | |
| 1519 | + 'settings', | |
| 1520 | + 'mcp', | |
| 1521 | + 'analytics', | |
| 1522 | + // Content IQ reads as a second Analytics screen, so it has to stay | |
| 1523 | + // pinned directly behind it. Without this entry it would fall into | |
| 1524 | + // the unknown-key bucket below and be appended to the bottom of the | |
| 1525 | + // menu — the exact placement the menu literal avoids by declaring | |
| 1526 | + // the slot inline rather than filtering it in api_docs-style. | |
| 1527 | + 'content_intelligence', | |
| 1528 | + 'faq', | |
| 1529 | + 'glossaries', | |
| 1530 | + 'ai_chatbot', | |
| 1531 | + 'ai_chatbot_logs', | |
| 1532 | + ); | |
| 1533 | + | |
| 1534 | + $ordered = array(); | |
| 1535 | + foreach ( $order as $key ) { | |
| 1536 | + if ( array_key_exists( $key, $pages ) ) { | |
| 1537 | + $ordered[ $key ] = $pages[ $key ]; | |
| 1538 | + unset( $pages[ $key ] ); | |
| 1539 | + } | |
| 1540 | + } | |
| 1541 | + | |
| 1542 | + // `$pages` now holds only unknown keys, still in their original order. | |
| 1543 | + return array_merge( $ordered, $pages ); | |
| 1544 | + } | |
| 1545 | + | |
| 1022 | 1546 | public function add_custom_classes_to_menu_items() { |
| 1023 | 1547 | global $menu, $submenu; |
| 1024 | 1548 | |
| 1025 | 1549 | $menu_items = array( |
| @@ -1025,15 +1549,18 @@ | ||
| 1025 | 1549 | $menu_items = array( |
| 1026 | 1550 | 'betterdocs' => 'betterdocs', |
| 1027 | 1551 | 'betterdocs_page_all_docs' => 'betterdocs-all-docs', |
| 1028 | 1552 | 'betterdocs_page_add_new' => 'betterdocs-add-new', |
| 1029 | - 'edit-tags.php?taxonomy=doc_category&post_type=docs' => 'betterdocs-categories', | |
| 1030 | - 'edit-tags.php?taxonomy=doc_tag&post_type=docs' => 'betterdocs-tags', | |
| 1553 | + 'betterdocs-doc-categories' => 'betterdocs-categories', | |
| 1554 | + 'betterdocs-doc-tags' => 'betterdocs-tags', | |
| 1031 | 1555 | 'betterdocs-settings' => 'betterdocs-settings', |
| 1556 | + 'betterdocs-mcp' => 'betterdocs-mcp', | |
| 1032 | 1557 | 'betterdocs-analytics' => 'betterdocs-analytics', |
| 1558 | + 'betterdocs-content-iq' => 'betterdocs-content-iq', | |
| 1033 | 1559 | 'betterdocs-faq' => 'betterdocs-faq', |
| 1034 | 1560 | 'betterdocs-glossaries' => 'betterdocs-glossaries', |
| 1035 | 1561 | 'betterdocs-ai-chatbot' => 'betterdocs-ai-chatbot', |
| 1562 | + 'betterdocs-api-docs' => 'betterdocs-api-docs', | |
| 1036 | 1563 | 'edit-tags.php?taxonomy=knowledge_base&post_type=docs' => 'betterdocs-multiplekb', |
| 1037 | 1564 | ); |
| 1038 | 1565 | |
| 1039 | 1566 | foreach ( $menu as &$item ) { |
| @@ -1146,11 +1673,18 @@ | ||
| 1146 | 1673 | * |
| 1147 | 1674 | * @since 3.0.1 |
| 1148 | 1675 | */ |
| 1149 | 1676 | public function save_admin_page() { |
| 1150 | - if ( isset( $_GET['post_type'] ) && 'docs' === $_GET['post_type'] && isset( $_GET['bdocs_view'] ) && 'classic' === $_GET['bdocs_view'] ) { // 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 ) { | |
| 1151 | 1685 | update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'classic_ui' ); |
| 1152 | - } elseif ( isset( $_GET['page'] ) && 'betterdocs-admin' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 1686 | + } elseif ( 'betterdocs-admin' === $page ) { | |
| 1153 | 1687 | update_user_meta( get_current_user_id(), 'last_visited_docs_admin_page', 'modern_ui' ); |
| 1154 | 1688 | } |
| 1155 | 1689 | } |
| 1156 | 1690 | |
| @@ -1217,9 +1751,10 @@ | ||
| 1217 | 1751 | * @since 3.7.0 |
| 1218 | 1752 | */ |
| 1219 | 1753 | public function ajax_dismiss_black_friday_pointer() { |
| 1220 | 1754 | // Verify nonce |
| 1221 | - if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'betterdocs_dismiss_pointer' ) ) { | |
| 1755 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; | |
| 1756 | + if ( ! wp_verify_nonce( $nonce, 'betterdocs_dismiss_pointer' ) ) { | |
| 1222 | 1757 | wp_send_json_error( array( 'message' => __( 'Invalid nonce', 'betterdocs' ) ) ); |
| 1223 | 1758 | } |
| 1224 | 1759 | |
| 1225 | 1760 | // Check if user has permission |
| @@ -1227,9 +1762,9 @@ | ||
| 1227 | 1762 | wp_send_json_error( array( 'message' => __( 'Permission denied', 'betterdocs' ) ) ); |
| 1228 | 1763 | } |
| 1229 | 1764 | |
| 1230 | 1765 | // Get the introduction key |
| 1231 | - $introduction_key = isset( $_POST['introduction_key'] ) ? sanitize_text_field( $_POST['introduction_key'] ) : ''; | |
| 1766 | + $introduction_key = isset( $_POST['introduction_key'] ) ? sanitize_text_field( wp_unslash( $_POST['introduction_key'] ) ) : ''; | |
| 1232 | 1767 | |
| 1233 | 1768 | if ( empty( $introduction_key ) ) { |
| 1234 | 1769 | wp_send_json_error( array( 'message' => __( 'Invalid introduction key', 'betterdocs' ) ) ); |
| 1235 | 1770 | } |