PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.2
4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 All 200 releases
← All changes | includes/REST/CategoryField.php +123 -15 3.4.24.9.2 View file →
@@ -1,24 +1,132 @@
1 1 <?php
2 2
3 3 namespace WPDeveloper\BetterDocs\REST;
4 +
4 5 use WPDeveloper\BetterDocs\Core\BaseAPI;
5 6
6 7 class CategoryField extends BaseAPI {
7 - /**
8 - * @return mixed
9 - */
10 - public function register() {
11 - $this->register_field( 'doc_category', 'thumbnail', [
12 - 'get_callback' => [$this, 'thumbnail_image']
13 - ] );
14 - }
8 + /**
9 + * @return mixed
10 + */
11 + public function register() {
12 + $this->register_field(
13 + 'doc_category',
14 + 'doc_category_order',
15 + [
16 + 'get_callback' => [ $this, 'doc_category_order' ],
17 + 'update_callback' => [ $this, 'update_doc_category_order' ],
18 + // Explicit schema — otherwise BaseAPI's default (a string "thumbnail
19 + // URL" schema) applies and the React slide-over's integer order write
20 + // fails validation with "not of type string".
21 + 'schema' => [
22 + 'description' => 'BetterDocs doc category display order.',
23 + 'type' => [ 'integer', 'null' ],
24 + 'context' => [ 'view', 'edit' ],
25 + // Skip the built-in type check and coerce to int|null ourselves
26 + // (a null/empty value is a no-op upstream).
27 + 'arg_options' => [
28 + 'validate_callback' => '__return_true',
29 + 'sanitize_callback' => function ( $value ) {
30 + return ( null === $value || '' === $value ) ? null : (int) $value;
31 + },
32 + ],
33 + ],
34 + ]
35 + );
36 + $this->register_field(
37 + 'doc_category',
38 + 'thumbnail',
39 + [
40 + 'get_callback' => [ $this, 'thumbnail_image' ]
41 + ]
42 + );
43 + $this->register_field(
44 + 'doc_category',
45 + 'handbookthumbnail',
46 + [
47 + 'get_callback' => [ $this, 'handbook_thumbnail_image' ]
48 + ]
49 + );
50 + $this->register_field(
51 + 'doc_category',
52 + 'subcategories_count',
53 + [
54 + 'get_callback' => [ $this, 'get_subcategory_count' ]
55 + ]
56 + );
57 + $this->register_field(
58 + 'doc_category',
59 + 'total_docs_count',
60 + [
61 + 'get_callback' => [ $this, 'total_docs_count' ]
62 + ]
63 + );
64 + $this->register_field(
65 + 'doc_category',
66 + 'last_updated_time',
67 + [
68 + 'get_callback' => [ $this, 'last_updated_time' ]
69 + ]
70 + );
71 + }
15 72
16 - public function thumbnail_image( $object ) {
17 - $attachment_id = get_term_meta( $object['id'], 'doc_category_image-id', true );
18 - if ( ! $attachment_id ) {
19 - return;
20 - }
73 + public function last_updated_time( $object ) {
74 + $date = betterdocs()->query->latest_updated_date( $object['taxonomy'], $object['slug'] );
75 + return $date;
76 + }
21 77
22 - return wp_get_attachment_url( $attachment_id );
23 - }
78 + public function doc_category_order( $object ) {
79 + // Get language-specific meta key with fallback for multilingual sites
80 + $meta_key = \WPDeveloper\BetterDocs\Utils\Helper::get_meta_key_with_fallback( 'doc_category_order', $object['id'] );
81 + $doc_category_order = get_term_meta( $object['id'], $meta_key, true );
82 +
83 + // If still no order found, try the base key as final fallback
84 + if ( '' === $doc_category_order || null === $doc_category_order ) {
85 + $doc_category_order = get_term_meta( $object['id'], 'doc_category_order', true );
86 + }
87 +
88 + return ( '' === $doc_category_order || null === $doc_category_order ) ? null : (int) $doc_category_order;
89 + }
90 +
91 + /**
92 + * Persist the display order written from the React Doc Categories slide-over.
93 + * Uses the same language-aware meta key as the reader. $term is a WP_Term.
94 + */
95 + public function update_doc_category_order( $value, $term ) {
96 + if ( null === $value || '' === $value ) {
97 + return;
98 + }
99 + $meta_key = \WPDeveloper\BetterDocs\Utils\Helper::get_meta_key_with_fallback( 'doc_category_order', $term->term_id );
100 + update_term_meta( $term->term_id, $meta_key, (int) $value );
101 + }
102 +
103 + public function thumbnail_image( $object ) {
104 + $attachment_id = get_term_meta( $object['id'], 'doc_category_image-id', true );
105 + if ( ! $attachment_id ) {
106 + return;
107 + }
108 +
109 + return wp_get_attachment_url( $attachment_id );
110 + }
111 +
112 + public function handbook_thumbnail_image( $object ) {
113 + $handbook_img_id = get_term_meta( $object['id'], 'doc_category_thumb-id', true );
114 +
115 + if ( ! $handbook_img_id ) {
116 + return;
117 + }
118 +
119 + return wp_get_attachment_url( $handbook_img_id );
120 + }
121 +
122 + public function get_subcategory_count( $object ) {
123 + $sub_terms_count = count( betterdocs()->query->get_all_child_term_ids( 'doc_category', $object['id'] ) );
124 +
125 + return $sub_terms_count;
126 + }
127 +
128 + public function total_docs_count( $object ) {
129 + $docs_count = betterdocs()->query->get_docs_count( (object) $object, false );
130 + return $docs_count;
131 + }
24 132 }