PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
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 3.5.2 All 199 releases
← All changes | includes/Core/Query.php +1152 -635 3.4.14.2.0 View file →
@@ -7,778 +7,1062 @@
7 7 use WPDeveloper\BetterDocs\Utils\Database;
8 8 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
9 9
10 10 class Query extends Base {
11 - private $container;
12 - protected $database;
13 - protected $settings;
11 + private $container;
12 + protected $database;
13 + protected $settings;
14 14
15 - public function __construct( Container $container, Database $database, Settings $settings ) {
16 - $this->container = $container;
17 - $this->database = $database;
18 - $this->settings = $settings;
15 + public function __construct( Container $container, Database $database, Settings $settings ) {
16 + $this->container = $container;
17 + $this->database = $database;
18 + $this->settings = $settings;
19 19
20 - add_action( 'parse_term_query', [$this, 'parse_term_query'] );
21 - // add_action( 'parse_query', [$this, 'parse_query'], 1 );
22 - add_action( 'pre_get_posts', [$this, 'pre_get_posts'], 1 );
20 + add_action( 'parse_term_query', [ $this, 'parse_term_query' ] );
21 + // add_action( 'parse_query', [$this, 'parse_query'], 1 );
22 + add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ], 1 );
23 + add_filter( 'betterdocs_base_terms_args', [ $this, 'modify_terms_args_for_private_docs' ], 10, 1 );
23 24
24 - /**
25 - * These below filters are hooked for navigation only.
26 - *
27 - * For old version of this portion.
28 - * @see `betterdocs_single_post_nav` filter
29 - *
30 - * For details:
31 - * @see https://developer.wordpress.org/reference/functions/get_next_post/
32 - * @see https://developer.wordpress.org/reference/functions/get_previous_post/
33 - *
34 - * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
35 - */
36 - add_filter( 'get_next_post_where', [$this, 'next_post_where'], 99, 5 );
37 - add_filter( 'get_previous_post_where', [$this, 'previous_post_where'], 99, 5 );
25 + /**
26 + * These below filters are hooked for navigation only.
27 + *
28 + * For old version of this portion.
29 + * @see `betterdocs_single_post_nav` filter
30 + *
31 + * For details:
32 + * @see https://developer.wordpress.org/reference/functions/get_next_post/
33 + * @see https://developer.wordpress.org/reference/functions/get_previous_post/
34 + *
35 + * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
36 + */
37 + add_filter( 'get_next_post_where', [ $this, 'next_post_where' ], 99, 5 );
38 + add_filter( 'get_previous_post_where', [ $this, 'previous_post_where' ], 99, 5 );
38 39
39 - $this->init();
40 + $this->init();
40 41
41 - /**
42 - * Modify Popular Docs Query (For Shortcode & Widget)
43 - */
44 - add_filter( 'posts_clauses', [$this, 'mod_query_popular_docs'], 10, 2 );
45 - }
42 + /**
43 + * Modify Popular Docs Query (For Shortcode & Widget)
44 + */
45 + add_filter( 'posts_clauses', [ $this, 'mod_query_popular_docs' ], 10, 2 );
46 + }
46 47
47 - public function mod_query_popular_docs( $clauses, $wp_query ) {
48 - if ( isset( $wp_query->query['meta_key'] ) ) {
49 - if ( $wp_query->query['meta_key'] == '_betterdocs_meta_views' ) {
50 - global $wpdb;
51 - $order = isset( $wp_query->query['order'] ) ? $wp_query->query['order'] : '';
52 - $order_by_query = ( $order == 'ASC' || $order == 'DESC' ) ? "SUM({$wpdb->prefix}betterdocs_analytics.impressions) {$order}" : ( $order == 'MODIFIED' ? "{$wpdb->prefix}posts.post_modified_gmt DESC" : "{$wpdb->prefix}posts.post_date_gmt DESC" );
53 - $clauses['join'] = "JOIN {$wpdb->prefix}betterdocs_analytics ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}betterdocs_analytics.post_id";
54 - $clauses['where'] = "AND ( ( {$wpdb->prefix}posts.post_type = 'docs' ) AND ( {$wpdb->prefix}posts.post_status = 'publish' OR {$wpdb->prefix}posts.post_status = 'future' OR {$wpdb->prefix}posts.post_status = 'draft' OR {$wpdb->prefix}posts.post_status = 'pending' OR {$wpdb->prefix}posts.post_status = 'private' ) )";
55 - $clauses['orderby'] = $order_by_query;
56 - $clauses['groupby'] = "{$wpdb->prefix}betterdocs_analytics.post_id";
57 - }
58 - }
59 - return $clauses;
60 - }
48 + public function mod_query_popular_docs( $clauses, $wp_query ) {
49 + if ( isset( $wp_query->query['meta_key'] ) ) {
50 + if ( $wp_query->query['meta_key'] == '_betterdocs_meta_views' ) {
51 + global $wpdb;
52 + $order = isset( $wp_query->query['order'] ) ? $wp_query->query['order'] : '';
53 + $order_by_query = ( $order == 'ASC' || $order == 'DESC' ) ? "SUM({$wpdb->prefix}betterdocs_analytics.impressions) {$order}" : ( $order == 'MODIFIED' ? "{$wpdb->prefix}posts.post_modified_gmt DESC" : "{$wpdb->prefix}posts.post_date_gmt DESC" );
54 + $clauses['join'] = "JOIN {$wpdb->prefix}betterdocs_analytics ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}betterdocs_analytics.post_id";
55 + $clauses['where'] = ! current_user_can( 'read_private_docs' ) ? "AND ( ( {$wpdb->prefix}posts.post_type = 'docs' ) AND ( {$wpdb->prefix}posts.post_status = 'publish' OR {$wpdb->prefix}posts.post_status = 'future' ) )" : "AND ( ( {$wpdb->prefix}posts.post_type = 'docs' ) AND ( {$wpdb->prefix}posts.post_status = 'publish' OR {$wpdb->prefix}posts.post_status = 'future' OR {$wpdb->prefix}posts.post_status = 'draft' OR {$wpdb->prefix}posts.post_status = 'pending' OR {$wpdb->prefix}posts.post_status = 'private' ) )";
56 + $clauses['orderby'] = $order_by_query;
57 + $clauses['groupby'] = "{$wpdb->prefix}betterdocs_analytics.post_id";
58 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
59 + global $sitepress;
60 + if ( $sitepress->is_setup_complete() ) {
61 + $constant_language_code = ICL_LANGUAGE_CODE;
62 + $clauses['join'] .= " JOIN {$wpdb->prefix}icl_translations ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}icl_translations.element_id";
63 + $clauses['where'] .= " AND ( {$wpdb->prefix}icl_translations.language_code = '{$constant_language_code}' ) AND ( {$wpdb->prefix}icl_translations.element_type = CONCAT('post_', {$wpdb->prefix}posts.post_type ) )";
64 + }
65 + }
66 + }
67 + }
68 + return $clauses;
69 + }
61 70
62 - public function init() {
63 - }
71 + public function init() {
72 + }
64 73
65 - public function parse_term_query( $term_query ) {
66 - if ( empty( $term_query->query_vars['taxonomy'] ) ) {
67 - return;
68 - }
74 + /**
75 + * Modify terms args to include terms with only private docs for users with read_private_docs capability
76 + *
77 + * @param array $args
78 + * @return array
79 + */
80 + public function modify_terms_args_for_private_docs( $args ) {
81 + // Only modify for users with read_private_docs capability and supported taxonomies
82 + if ( ! current_user_can( 'read_private_docs' ) || ! isset( $args['taxonomy'] ) ) {
83 + return $args;
84 + }
69 85
70 - if ( ! in_array( 'doc_category', $term_query->query_vars['taxonomy'], true ) ) {
71 - return;
72 - }
86 + // Only support doc_category taxonomy for private docs filtering
87 + // knowledge_base terms don't have docs directly assigned to them
88 + $supported_taxonomies = ['doc_category'];
89 + if ( ! in_array( $args['taxonomy'], $supported_taxonomies ) ) {
90 + return $args;
91 + }
73 92
74 - global $current_screen;
93 + // If hide_empty is true, we need to modify the logic to include terms with private docs
94 + if ( isset( $args['hide_empty'] ) && $args['hide_empty'] ) {
95 + // Set hide_empty to false and we'll filter manually later
96 + $args['hide_empty'] = false;
97 + // Add a flag to indicate we need to filter manually
98 + $args['_betterdocs_filter_private'] = true;
99 + }
75 100
76 - if ( $current_screen == null ) {
77 - return;
78 - }
101 + return $args;
102 + }
79 103
80 - if ( $current_screen->taxonomy !== 'doc_category' || $current_screen->id != 'edit-doc_category' ) {
81 - return;
82 - }
104 + public function parse_term_query( $term_query ) {
105 + if ( empty( $term_query->query_vars['taxonomy'] ) ) {
106 + return;
107 + }
83 108
84 - $term_query->query_vars['meta_query'] = [[
85 - 'key' => 'doc_category_order',
86 - 'type' => 'NUMERIC'
87 - ]];
109 + if ( ! in_array( 'doc_category', $term_query->query_vars['taxonomy'], true ) ) {
110 + return;
111 + }
88 112
89 - $term_query->query_vars['orderby'] = 'meta_value_num';
90 - }
113 + global $current_screen;
91 114
92 - public function parse_query( &$query ) {
93 - // dump( is_single(), $query->query_vars );
94 - // if ( is_single() && isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] == 'docs' ) {
95 - // $query->is_single = false;
96 - // $query->is_archive = true;
97 - // $query->set( 'knowledge_base', $query->query_vars['docs'] );
98 - // }
99 - }
115 + if ( $current_screen == null ) {
116 + return;
117 + }
100 118
101 - public function pre_get_posts( &$query ) {
102 - if ( is_admin() || ! $query->is_main_query() ) {
103 - return;
104 - }
119 + if ( $current_screen->taxonomy !== 'doc_category' || $current_screen->id != 'edit-doc_category' ) {
120 + return;
121 + }
105 122
106 - if ( is_tax( 'doc_category' ) ) {
107 - $query->set( 'post_type', 'docs' );
108 - $query->set( 'posts_per_archive_page', -1 );
123 + $term_query->query_vars['meta_query'] = [
124 + [
125 + 'key' => 'doc_category_order',
126 + 'type' => 'NUMERIC'
127 + ]
128 + ];
109 129
110 - $term = get_term_by( 'slug', $query->get( 'doc_category', '' ), 'doc_category' );
130 + $term_query->query_vars['orderby'] = 'meta_value_num';
131 + }
111 132
112 - $post__in = $this->get_docs_order_by_terms( $term->term_id );
113 - if ( ! empty( $post__in ) ) {
114 - $query->set( 'orderby', 'post__in' );
115 - $query->set( 'post__in', $post__in );
116 - }
133 + public function parse_query( &$query ) {
134 + // dump( is_single(), $query->query_vars );
135 + // if ( is_single() && isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] == 'docs' ) {
136 + // $query->is_single = false;
137 + // $query->is_archive = true;
138 + // $query->set( 'knowledge_base', $query->query_vars['docs'] );
139 + // }
140 + }
117 141
118 - // if ( ! empty( $query->query_vars['knowledge_base'] ) ) {
119 - // $query->betterdocs_terms = get_terms( [
120 - // 'taxonomy' => 'doc_category',
121 - // 'parent' => 0,
122 - // 'hide_empty' => true,
123 - // 'meta_query' => [
124 - // 'relation' => 'OR',
125 - // [
126 - // 'key' => 'doc_category_knowledge_base',
127 - // 'value' => $query->query_vars['knowledge_base'],
128 - // 'compare' => 'LIKE'
129 - // ]
130 - // ]
131 - // ] );
132 - // }
133 - }
142 + public function pre_get_posts( &$query ) {
143 + if ( is_admin() || ! $query->is_main_query() ) {
144 + return;
145 + }
134 146
135 - // dump( is_archive(), $query->query_vars );
136 - }
147 + if ( is_tax( 'doc_category' ) ) {
148 + $query->set( 'post_type', 'docs' );
149 + $query->set( 'posts_per_archive_page', -1 );
137 150
138 - /**
139 - * Get docs orders by term_id.
140 - *
141 - * @since 2.5.0
142 - * @param int $term_id
143 - *
144 - * @return array
145 - */
146 - public function get_docs_order_by_terms( $term_id ) {
147 - global $wpdb;
148 - $_docs_order = get_term_meta( $term_id, '_docs_order', true );
149 - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
150 - $query_key = "docs_order_by_terms_{$term_id}_" . md5( $query );
151 + $term = get_term_by( 'slug', $query->get( 'doc_category', '' ), 'doc_category' );
151 152
152 - if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
153 - return $results;
154 - }
153 + if ( $term && isset( $term->term_id ) ) {
154 + $post__in = $this->get_docs_order_by_terms( $term->term_id );
155 + } else {
156 + $post__in = [];
157 + }
158 + if ( ! empty( $post__in ) ) {
159 + $query->set( 'orderby', 'post__in' );
160 + $query->set( 'post__in', $post__in );
161 + }
155 162
156 - if ( ! empty( $_docs_order ) ) {
157 - $_docs_order = explode( ',', $_docs_order );
158 - $new_ids = [];
163 + // if ( ! empty( $query->query_vars['knowledge_base'] ) ) {
164 + // $query->betterdocs_terms = get_terms( [
165 + // 'taxonomy' => 'doc_category',
166 + // 'parent' => 0,
167 + // 'hide_empty' => true,
168 + // 'meta_query' => [
169 + // 'relation' => 'OR',
170 + // [
171 + // 'key' => 'doc_category_knowledge_base',
172 + // 'value' => $query->query_vars['knowledge_base'],
173 + // 'compare' => 'LIKE'
174 + // ]
175 + // ]
176 + // ] );
177 + // }
178 + }
159 179
160 - $results = $wpdb->get_results( $query );
180 + // dump( is_archive(), $query->query_vars );
181 + }
161 182
162 - if ( is_array( $results ) && ! empty( $results ) ) {
163 - $object_ids = array_filter( $results, function ( $value ) use ( $_docs_order ) {
164 - return ! in_array( $value->object_id, $_docs_order );
165 - } );
183 + /**
184 + * Get docs orders by term_id.
185 + *
186 + * @since 2.5.0
187 + * @param int $term_id
188 + *
189 + * @return array
190 + */
191 + public function get_docs_order_by_terms( $term_id ) {
192 + global $wpdb;
193 + $_docs_order = get_term_meta( $term_id, '_docs_order', true );
194 + $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
195 + $query_key = "docs_order_by_terms_{$term_id}_" . md5( $query );
166 196
167 - if ( ! empty( $object_ids ) ) {
168 - array_walk( $object_ids, function ( $value ) use ( &$new_ids ) {
169 - $new_ids[] = $value->object_id;
170 - } );
171 - }
172 - }
197 + if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
198 + return $results;
199 + }
173 200
174 - $_docs_order = array_merge( $new_ids, $_docs_order );
175 - $this->database->set_cache( $query_key, $_docs_order, 1 );
201 + if ( ! empty( $_docs_order ) ) {
202 + $_docs_order = explode( ',', $_docs_order );
203 + $new_ids = [];
176 204
177 - return $_docs_order;
178 - }
205 + $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
179 206
180 - return [];
181 - }
207 + if ( is_array( $results ) && ! empty( $results ) ) {
208 + $object_ids = array_filter(
209 + $results,
210 + function ( $value ) use ( $_docs_order ) {
211 + return ! in_array( $value->object_id, $_docs_order );
212 + }
213 + );
182 214
183 - /**
184 - * For determine the next post ID
185 - *
186 - * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
187 - *
188 - * @param mixed $where
189 - * @param mixed $in_same_term
190 - * @param mixed $excluded_terms
191 - * @param mixed $taxonomy
192 - * @param mixed $post
193 - * @return mixed
194 - */
195 - public function next_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
196 - return $this->get_adjacent_post_id( 'next', $where, $post, $taxonomy );
197 - }
215 + if ( ! empty( $object_ids ) ) {
216 + array_walk(
217 + $object_ids,
218 + function ( $value ) use ( &$new_ids ) {
219 + $new_ids[] = $value->object_id;
220 + }
221 + );
222 + }
223 + }
198 224
199 - /**
200 - * For determine the previous post ID
201 - *
202 - * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
203 - *
204 - * @param mixed $where
205 - * @param mixed $in_same_term
206 - * @param mixed $excluded_terms
207 - * @param mixed $taxonomy
208 - * @param mixed $post
209 - * @return mixed
210 - */
211 - public function previous_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
212 - return $this->get_adjacent_post_id( 'previous', $where, $post, $taxonomy );
213 - }
225 + $_docs_order = array_merge( $new_ids, $_docs_order );
226 + $this->database->set_cache( $query_key, $_docs_order, 1 );
214 227
215 - /**
216 - * Get where clause for next/previous post ID mainly used in post navigation.
217 - *
218 - * @see `previous_post_where` and `next_post_where` methods.
219 - *
220 - * @param mixed $adjacent
221 - * @param mixed $where
222 - * @param mixed $post
223 - * @param mixed $taxonomy
224 - * @return mixed
225 - */
226 - private function get_adjacent_post_id( $adjacent, $where, $post, $taxonomy ) {
227 - if ( $taxonomy !== 'doc_category' ) {
228 - return $where;
229 - }
228 + return $_docs_order;
229 + }
230 230
231 - $_id = null;
232 - $_terms = get_the_terms( $post->ID, 'doc_category' );
233 - if ( empty( $_terms ) ) {
234 - return $where;
235 - }
231 + return [];
232 + }
236 233
237 - global $wp_query, $wpdb;
234 + /**
235 + * For determine the next post ID
236 + *
237 + * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
238 + *
239 + * @param mixed $where
240 + * @param mixed $in_same_term
241 + * @param mixed $excluded_terms
242 + * @param mixed $taxonomy
243 + * @param mixed $post
244 + * @return mixed
245 + */
246 + public function next_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
247 + return $this->get_adjacent_post_id( 'next', $where, $post, $taxonomy );
248 + }
238 249
239 - $_docs_order = $this->get_docs_order_by_terms( $_terms[0]->term_id );
250 + /**
251 + * For determine the previous post ID
252 + *
253 + * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
254 + *
255 + * @param mixed $where
256 + * @param mixed $in_same_term
257 + * @param mixed $excluded_terms
258 + * @param mixed $taxonomy
259 + * @param mixed $post
260 + * @return mixed
261 + */
262 + public function previous_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
263 + return $this->get_adjacent_post_id( 'previous', $where, $post, $taxonomy );
264 + }
240 265
241 - $_where = explode( 'AND', $where );
242 - if ( is_array( $_where ) ) {
243 - // $_where[0] = str_replace( 'WHERE ', '', $_where[0] );
244 - unset( $_where[0] );
245 - $_where = implode( 'AND', $_where );
246 - }
266 + /**
267 + * Get where clause for next/previous post ID mainly used in post navigation.
268 + *
269 + * @see `previous_post_where` and `next_post_where` methods.
270 + *
271 + * @param mixed $adjacent
272 + * @param mixed $where
273 + * @param mixed $post
274 + * @param mixed $taxonomy
275 + * @return mixed
276 + */
277 + private function get_adjacent_post_id( $adjacent, $where, $post, $taxonomy ) {
278 + if ( $taxonomy !== 'doc_category' ) {
279 + return $where;
280 + }
247 281
248 - $_orderby = $this->settings->get( 'alphabetically_order_post', 'betterdocs_order' );
249 - $_order = $this->settings->get( 'docs_order', 'ASC' );
250 - $_docs_order = $_orderby === 'betterdocs_order' ? $_docs_order : [];
282 + $_id = null;
283 + $_terms = get_the_terms( $post->ID, 'doc_category' );
284 + if ( empty( $_terms ) ) {
285 + return $where;
286 + }
251 287
252 - if ( empty( $_docs_order ) ) {
253 - $statuses = [ 'publish' ];
288 + global $wp_query, $wpdb;
254 289
255 - if( is_user_logged_in() ) {
256 - $statuses[] = 'private';
257 - }
290 + $_docs_order = $this->get_docs_order_by_terms( $_terms[0]->term_id );
258 291
259 - $_args = [
260 - 'post_status' => $statuses
261 - ];
262 - if ( isset( $wp_query->query_vars['doc_category'] ) ) {
263 - $_args['tax_query'][] = [
264 - 'taxonomy' => 'doc_category',
265 - 'field' => 'slug',
266 - 'terms' => $wp_query->query_vars['doc_category'],
267 - 'operator' => 'AND',
268 - 'include_children' => false
269 - ];
270 - }
292 + $_where = explode( 'AND', $where );
293 + if ( is_array( $_where ) ) {
294 + // $_where[0] = str_replace( 'WHERE ', '', $_where[0] );
295 + unset( $_where[0] );
296 + $_where = implode( 'AND', $_where );
297 + }
271 298
272 - $_args['orderby'] = $_orderby;
273 - if ( $_orderby != 'betterdocs_order' ) {
274 - $_args['order'] = $_order;
275 - }
299 + $_orderby = $this->settings->get( 'alphabetically_order_post', 'betterdocs_order' );
300 + $_order = $this->settings->get( 'docs_order', 'ASC' );
301 + $_docs_order = $_orderby === 'betterdocs_order' ? $_docs_order : [];
276 302
277 - /**
278 - * Before Query
279 - */
280 - do_action_ref_array( 'betterdocs_navigation_docs_query', [ &$_args] );
281 - $docs = $this->get_posts( $this->docs_query_args( $_args ) );
303 + if ( empty( $_docs_order ) ) {
304 + $statuses = [ 'publish' ];
282 305
283 - $_docs = [];
284 - if ( $docs->have_posts() ) {
285 - array_map( function ( $post ) use ( &$_docs ) {
286 - $_docs[] = $post->ID;
287 - }, $docs->posts );
288 - }
306 + if ( current_user_can( 'read_private_docs' ) ) {
307 + $statuses[] = 'private';
308 + }
289 309
290 - $_docs_order = $_docs;
291 - }
310 + $_args = [
311 + 'post_status' => $statuses
312 + ];
313 + if ( isset( $wp_query->query_vars['doc_category'] ) ) {
314 + $_args['tax_query'][] = [
315 + 'taxonomy' => 'doc_category',
316 + 'field' => 'slug',
317 + 'terms' => $wp_query->query_vars['doc_category'],
318 + 'operator' => 'AND',
319 + 'include_children' => false
320 + ];
321 + } else if( isset( $wp_query->query_vars['name'] ) && isset( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'docs' ) {
322 + $post = get_page_by_path($wp_query->query_vars['name'] , OBJECT, 'docs');
323 + $doc_terms = [];
292 324
293 - $_docs_order = apply_filters( 'betterdocs_adjacent_docs_order', $_docs_order, $_terms );
325 + if( isset( $post->ID ) ) {
326 + $terms = get_the_terms($post->ID, 'doc_category');
327 + if( ! empty( $terms ) ) {
328 + $doc_terms = wp_list_pluck( $terms, 'term_id' );
329 + }
330 + }
294 331
295 - $_id_index = array_search( $post->ID, $_docs_order );
296 - $_id_index = $adjacent === 'next' ? $_id_index + 1 : $_id_index - 1;
297 - $_id = isset( $_docs_order[$_id_index] ) ? (int) $_docs_order[$_id_index] : null;
332 + if( ! empty( $doc_terms ) ) {
333 + $_args['tax_query'][] = [
334 + 'taxonomy' => 'doc_category',
335 + 'field' => 'term_id',
336 + 'terms' => $doc_terms,
337 + 'operator' => 'AND',
338 + 'include_children' => false
339 + ];
340 + }
341 + }
298 342
299 - return $wpdb->prepare( "WHERE p.ID = %s AND $_where", (int) $_id );
300 - }
343 + $_args['orderby'] = $_orderby;
344 + if ( $_orderby != 'betterdocs_order' ) {
345 + $_args['order'] = $_order;
346 + }
301 347
302 - public function parse_terms_args( $args = [] ) {
303 - $_default_args = [
304 - 'hide_empty' => true,
305 - 'taxonomy' => 'doc_category'
306 - ];
348 + /**
349 + * Before Query
350 + */
351 + do_action_ref_array( 'betterdocs_navigation_docs_query', [ &$_args ] );
352 + $docs = $this->get_posts( $this->docs_query_args( $_args ) );
307 353
308 - // OrderBy & Order
309 - $_orderby = ! empty( $args['orderby'] ) && $args['orderby'] != 1 ? $args['orderby'] : 'name';
310 - $_order = ! empty( $args['order'] ) ? $args['order'] : '';
354 + $_docs = [];
355 + if ( $docs->have_posts() ) {
356 + array_map(
357 + function ( $post ) use ( &$_docs ) {
358 + $_docs[] = $post->ID;
359 + },
360 + $docs->posts
361 + );
362 + }
311 363
312 - if ( $_orderby == 'betterdocs_order' ) {
313 - $args['meta_key'] = 'doc_category_order';
314 - $args['orderby'] = 'meta_value_num';
315 - $args['order'] = 'ASC';
316 - } else {
317 - $args['orderby'] = $_orderby;
318 - $args['order'] = $_order;
319 - }
364 + $_docs_order = $_docs;
365 + }
320 366
321 - // Nested Sub Category
322 - // $args['parent'] = 0;
323 - // if ( $nested_subcategory == true ) {
324 - // }
367 + $_docs_order = apply_filters( 'betterdocs_adjacent_docs_order', $_docs_order, $_terms );
325 368
326 - if ( ! isset( $args['number'] ) ) {
327 - global $wp_query;
328 - if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
329 - $args['number'] = 4;
330 - }
331 - }
369 + $_id_index = array_search( $post->ID, $_docs_order );
370 + $_id_index = $adjacent === 'next' ? $_id_index + 1 : $_id_index - 1;
371 + $_id = isset( $_docs_order[ $_id_index ] ) ? (int) $_docs_order[ $_id_index ] : null;
332 372
333 - // Includes
334 - if ( isset( $args['include'] ) ) {
335 - $_include = ! is_array( $args['include'] ) ? explode( ',', $args['include'] ) : $args['include'];
336 - $args['include'] = $_include;
337 - $args['orderby'] = 'include';
373 + return $wpdb->prepare( "WHERE p.ID = %s AND $_where", (int) $_id );
374 + }
338 375
339 - unset( $args['parent'] );
340 - }
376 + public function parse_terms_args( $args = [] ) {
377 + $_default_args = [
378 + 'hide_empty' => true,
379 + 'taxonomy' => 'doc_category'
380 + ];
341 381
342 - $_meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
343 - $args['meta_query'] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $_meta_query, $args );
382 + // OrderBy & Order
383 + $_orderby = ! empty( $args['orderby'] ) && $args['orderby'] != 1 ? $args['orderby'] : 'name';
384 + $_order = ! empty( $args['order'] ) ? $args['order'] : '';
344 385
345 - return apply_filters( 'betterdocs_category_terms_object', wp_parse_args( $args, $_default_args ), $args );
346 - }
386 + if ( $_orderby == 'betterdocs_order' ) {
387 + $args['meta_key'] = 'doc_category_order';
388 + $args['orderby'] = 'meta_value_num';
389 + $args['order'] = 'ASC';
390 + } else {
391 + $args['orderby'] = $_orderby;
392 + $args['order'] = $_order;
393 + }
347 394
348 - public function get_terms( $args ) {
349 - return get_terms( $this->parse_terms_args( $args ) );
350 - }
395 + // Nested Sub Category
396 + // $args['parent'] = 0;
397 + // if ( $nested_subcategory == true ) {
398 + // }
351 399
352 - public function get_child_terms( $args ) {
353 - if ( ! isset( $args['number'] ) ) {
354 - global $wp_query;
355 - if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
356 - $args['number'] = 1;
357 - }
358 - }
400 + if ( ! isset( $args['number'] ) ) {
401 + global $wp_query;
402 + if ( $wp_query->query === null || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
403 + $args['number'] = 4;
404 + }
405 + }
359 406
360 - return $this->get_terms( $args );
361 - }
407 + // Includes
408 + if ( isset( $args['include'] ) ) {
409 + $_include = ! is_array( $args['include'] ) ? explode( ',', $args['include'] ) : $args['include'];
410 + $args['include'] = $_include;
411 + $args['orderby'] = 'include';
362 412
363 - /**
364 - * Get POSTs of Docs type.
365 - *
366 - * @param mixed $args
367 - * @return WP_Query
368 - */
369 - public function get_posts( $args, $ignore = false ) {
370 - if ( ! $ignore ) {
371 - $args = $this->docs_query_args( $args );
372 - }
413 + unset( $args['parent'] );
414 + }
373 415
374 - return new WP_Query( $args );
375 - }
416 + $_meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
417 + $args['meta_query'] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $_meta_query, $args );
376 418
377 - public function get_taxonomy( $tax = '' ) {
378 - global $wp_query;
379 - if ( is_tax( 'knowledge_base' ) ) {
380 - $_tax = $wp_query->tax_query->queried_terms;
381 - if ( array_key_exists( "doc_category", $_tax ) ) {
382 - $tax = 'doc_category';
383 - } else {
384 - $tax = 'knowledge_base';
385 - }
386 - } elseif ( is_tax( 'doc_category' ) ) {
387 - $tax = 'doc_category';
388 - }
419 + return apply_filters( 'betterdocs_category_terms_object', wp_parse_args( $args, $_default_args ), $args );
420 + }
389 421
390 - return $tax;
391 - }
422 + public function get_terms( $args ) {
423 + $parsed_args = $this->parse_terms_args( $args );
424 + $terms = get_terms( $parsed_args );
392 425
393 - public function terms_query( $args = [] ) {
394 - global $wp_query;
395 - $_origin_args = $args;
426 + // Filter terms manually if we need to consider private docs for users with read_private_docs capability
427 + if ( isset( $parsed_args['_betterdocs_filter_private'] ) && $parsed_args['_betterdocs_filter_private'] && current_user_can( 'read_private_docs' ) ) {
428 + $terms = array_filter( $terms, function( $term ) {
429 + // Get the actual count including private docs for users with read_private_docs capability
430 + $actual_count = $this->get_docs_count( $term, false );
431 + return $actual_count > 0;
432 + });
433 + }
396 434
397 - $default_args = [
398 - 'hide_empty' => true,
399 - 'taxonomy' => 'doc_category',
400 - 'orderby' => 'name'
401 - ];
435 + return $terms;
436 + }
402 437
403 - /**
404 - * Number Set
405 - *
406 - * @FIX: If Built-in Docs page off and docs_page in use, then Terms Query Number 4|1 set.
407 - */
408 - // if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
409 - // $default_args['number'] = 1;
410 - // }
438 + public function get_child_terms( $args ) {
439 + if ( ! isset( $args['number'] ) ) {
440 + global $wp_query;
441 + if ( $wp_query->query === null || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
442 + $args['number'] = 1;
443 + }
444 + }
411 445
412 - /**
413 - * Nested Sub Category
414 - */
415 - if ( isset( $args['nested_subcategory'] ) && $args['nested_subcategory'] == true ) {
416 - $default_args['parent'] = 0;
417 - unset( $args['nested_subcategory'] );
418 - // if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
419 - // $default_args['number'] = 4;
420 - // }
421 - }
446 + return $this->get_terms( $args );
447 + }
422 448
423 - /**
424 - * OrderBy and Order
425 - */
426 - if ( ! isset( $args['orderby'] ) ) {
427 - $_orderby = $this->settings->get( 'terms_orderby', 'name' );
428 - $_order = $this->settings->get( 'terms_order', '' );
429 - } else {
430 - $_orderby = $args['orderby'];
431 - $_order = ! empty( $args['order'] ) ? $args['order'] : '';
432 - }
449 + /**
450 + * Get POSTs of Docs type.
451 + *
452 + * @param mixed $args
453 + * @return WP_Query
454 + */
455 + public function get_posts( $args, $ignore = false ) {
456 + if ( ! $ignore ) {
457 + $args = $this->docs_query_args( $args );
458 + }
433 459
434 - if ( 'betterdocs_order' === $_orderby ) {
435 - $default_args['meta_key'] = 'doc_category_order';
436 - $_orderby = 'meta_value_num';
437 - $_order = 'ASC';
438 - } elseif ( $_orderby === true ) {
439 - $_orderby = 'name';
440 - }
460 + return new WP_Query( $args );
461 + }
441 462
442 - $args['orderby'] = $_orderby;
443 - if( ! empty( $_order ) ) {
444 - $args['order'] = $_order;
445 - }
463 + public function get_taxonomy( $tax = '' ) {
464 + global $wp_query;
465 + if ( is_tax( 'knowledge_base' ) ) {
466 + $_tax = $wp_query->tax_query->queried_terms;
467 + if ( array_key_exists( 'doc_category', $_tax ) ) {
468 + $tax = 'doc_category';
469 + } else {
470 + $tax = 'knowledge_base';
471 + }
472 + } elseif ( is_tax( 'doc_category' ) ) {
473 + $tax = 'doc_category';
474 + }
446 475
447 - /**
448 - * @todo old hook
449 - * hook: betterdocs_child_taxonomy_meta_query
450 - */
451 - $_multiple_kb = apply_filters(
452 - 'betterdocs_query_args_multiple_kb_enabled',
453 - isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
454 - $_origin_args
455 - );
476 + return $tax;
477 + }
456 478
457 - $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
479 + public function terms_query( $args = [] ) {
480 + global $wp_query;
481 + $_origin_args = $args;
458 482
459 - unset( $args['multiple_kb'] );
460 - unset( $args['kb_slug'] );
483 + $default_args = [
484 + 'hide_empty' => true,
485 + 'taxonomy' => 'doc_category',
486 + 'orderby' => 'name'
487 + ];
461 488
462 - $meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
463 - $meta_query = apply_filters( 'betterdocs_terms_meta_query_args', $meta_query, $_multiple_kb, $_kb_slug, $_origin_args );
489 + /**
490 + * Number Set
491 + *
492 + * @FIX: If Built-in Docs page off and docs_page in use, then Terms Query Number 4|1 set.
493 + */
494 + // if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
495 + // $default_args['number'] = 1;
496 + // }
464 497
465 - if ( ! empty( $meta_query ) ) {
466 - $default_args['meta_query'] = $meta_query;
467 - }
498 + /**
499 + * Nested Sub Category
500 + */
501 + if ( isset( $args['nested_subcategory'] ) && $args['nested_subcategory'] == true ) {
502 + $default_args['parent'] = 0;
503 + unset( $args['nested_subcategory'] );
504 + // if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
505 + // $default_args['number'] = 4;
506 + // }
507 + }
468 508
469 - if ( ! empty( $args['terms'] ) ) {
470 - $args['include'] = explode( ',', $args['terms'] );
471 - $args['orderby'] = 'include';
472 - $args['order'] = 'ASC';
509 + /**
510 + * OrderBy and Order
511 + */
512 + if ( ! isset( $args['orderby'] ) ) {
513 + $_orderby = $this->settings->get( 'terms_orderby', 'name' );
514 + $_order = $this->settings->get( 'terms_order', '' );
515 + } else {
516 + $_orderby = $args['orderby'];
517 + $_order = ! empty( $args['order'] ) ? $args['order'] : '';
518 + }
473 519
474 - unset( $default_args['parent'] );
475 - unset( $args['parent'] );
476 - unset( $args['terms'] );
477 - }
520 + if ( 'betterdocs_order' === $_orderby ) {
521 + // Use different meta keys for different taxonomies
522 + if ( isset( $args['taxonomy'] ) && $args['taxonomy'] === 'knowledge_base' ) {
523 + $default_args['meta_key'] = 'kb_order';
524 + } else {
525 + $default_args['meta_key'] = 'doc_category_order';
526 + }
527 + $_orderby = 'meta_value_num';
528 + $_order = 'ASC';
529 + } elseif ( $_orderby === true ) {
530 + $_orderby = 'name';
531 + }
478 532
479 - $_query_args = wp_parse_args( $args, $default_args );
480 - return apply_filters( 'betterdocs_terms_query_args', $_query_args, $_origin_args );
481 - }
533 + $args['orderby'] = $_orderby;
534 + if ( ! empty( $_order ) ) {
535 + $args['order'] = $_order;
536 + }
482 537
483 - public function get_term_parents( $term_id, $taxonomy = 'doc_category', $args = [] ) {
484 - $term = get_term( $term_id, $taxonomy );
485 - if ( is_wp_error( $term ) ) {
486 - return $term;
487 - }
538 + /**
539 + * @todo old hook
540 + * hook: betterdocs_child_taxonomy_meta_query
541 + */
542 + $_multiple_kb = apply_filters(
543 + 'betterdocs_query_args_multiple_kb_enabled',
544 + isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
545 + $_origin_args
546 + );
488 547
489 - if ( ! $term ) {
490 - return [];
491 - }
548 + $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
492 549
493 - $_lists = [];
494 - $origin_term = $term_id;
495 - $term_id = $term->term_id;
550 + unset( $args['multiple_kb'] );
551 + unset( $args['kb_slug'] );
496 552
497 - $defaults = [
498 - 'format' => 'name',
499 - 'inclusive' => true
500 - ];
553 + $meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
554 + $meta_query = apply_filters( 'betterdocs_terms_meta_query_args', $meta_query, $_multiple_kb, $_kb_slug, $_origin_args );
501 555
502 - $args = wp_parse_args( $args, $defaults );
556 + if ( ! empty( $meta_query ) ) {
557 + $default_args['meta_query'] = $meta_query;
558 + }
503 559
504 - $args['inclusive'] = wp_validate_boolean( $args['inclusive'] );
560 + if ( ! empty( $args['terms'] ) ) {
561 + $args['include'] = explode( ',', $args['terms'] );
562 + $args['orderby'] = 'include';
563 + $args['order'] = 'ASC';
505 564
506 - $parents = get_ancestors( $term_id, $taxonomy );
565 + unset( $default_args['parent'] );
566 + unset( $args['parent'] );
567 + unset( $args['terms'] );
568 + }
507 569
508 - if ( $args['inclusive'] ) {
509 - array_unshift( $parents, $term_id );
510 - }
570 + $_query_args = wp_parse_args( $args, $default_args );
571 + $_query_args = apply_filters( 'betterdocs_terms_query_args', $_query_args, $_origin_args );
511 572
512 - foreach ( array_reverse( $parents ) as $term_id ) {
513 - $parent = get_term( $term_id, $taxonomy );
514 - $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;
515 - $term_permalink = get_term_link( $parent->term_id, $taxonomy );
516 - $term_permalink = apply_filters( 'betterdocs_breadcrumb_term_permalink', $term_permalink, $term_id );
573 + // Apply private docs logic for logged-in users
574 + $_query_args = $this->modify_terms_args_for_private_docs( $_query_args );
517 575
518 - $_item = [
519 - 'url' => $term_permalink,
520 - 'text' => $name
521 - ];
576 + return $_query_args;
577 + }
522 578
523 - $_lists[] = $_item;
524 - }
579 + public function get_term_parents( $term_id, $taxonomy = 'doc_category', $args = [] ) {
580 + $term = get_term( $term_id, $taxonomy );
581 + if ( is_wp_error( $term ) ) {
582 + return $term;
583 + }
525 584
526 - return apply_filters( 'betterdocs_breadcrumb_archive_lists', $_lists, $origin_term );
527 - }
585 + if ( ! $term || ! isset( $term->term_id ) ) {
586 + return [];
587 + }
528 588
529 - /**
530 - * Summary of docs_query_args
531 - * @param mixed $args
532 - * @throws \Exception
533 - * @return mixed
534 - */
535 - public function docs_query_args( $args, $filter = [] ) {
536 - $_origin_args = $args;
589 + $_lists = [];
590 + $origin_term = $term_id;
591 + $term_id = $term->term_id;
537 592
538 - $default_args = [
539 - 'post_type' => 'docs'
540 - ];
593 + $defaults = [
594 + 'format' => 'name',
595 + 'inclusive' => true
596 + ];
541 597
542 - if ( ! empty( $args['post_type'] ) && trim( $args['post_type'] ) === 'docs_any' ) {
543 - $default_args['post_type'] = 'docs';
544 - $default_args['post_status'] = 'any';
598 + $args = wp_parse_args( $args, $defaults );
545 599
546 - unset( $args['post_type'] );
547 - }
600 + $args['inclusive'] = wp_validate_boolean( $args['inclusive'] );
548 601
549 - /**
550 - * OrderBy and Order
551 - */
552 - if ( ! isset( $args['orderby'] ) ) {
553 - $_orderby = $this->settings->get( 'alphabetically_order_post' );
554 - $_order = $this->settings->get( 'docs_order', 'ASC' );
555 - } else {
556 - $_orderby = $args['orderby'];
557 - $_order = ! empty( $args['order'] ) ? $args['order'] : 'ASC';
558 - }
602 + $parents = get_ancestors( $term_id, $taxonomy );
559 603
560 - if ( 'betterdocs_order' != $_orderby ) {
561 - if ( $_orderby === true ) {
562 - $args['orderby'] = 'title';
563 - } else {
564 - $args['orderby'] = $_orderby;
565 - }
604 + if ( $args['inclusive'] ) {
605 + array_unshift( $parents, $term_id );
606 + }
566 607
567 - $args['order'] = $_order;
568 - } elseif ( 'betterdocs_order' == $_orderby ) {
569 - unset( $args['orderby'] );
570 - }
608 + foreach ( array_reverse( $parents ) as $term_id ) {
609 + $parent = get_term( $term_id, $taxonomy );
610 + $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;
611 + $term_permalink = get_term_link( $parent->term_id, $taxonomy );
612 + $term_permalink = apply_filters( 'betterdocs_breadcrumb_term_permalink', $term_permalink, $term_id );
571 613
572 - if ( empty( $args['orderby'] ) ) {
573 - unset( $args['order'] );
574 - }
614 + $_item = [
615 + 'url' => $term_permalink,
616 + 'text' => $name
617 + ];
575 618
576 - /**
577 - * Term ID
578 - */
579 - $_term_id = null;
580 - if ( ! empty( $args['term_id'] ) ) {
581 - $_term_id = intval( $args['term_id'] );
582 - unset( $args['term_id'] );
583 - }
619 + $_lists[] = $_item;
620 + }
584 621
585 - // if ( $_term_id == null ) {
586 - // throw new \Exception( __( '$args["term_id"] cannot be null.', 'betterdocs' ) );
587 - // }
622 + return apply_filters( 'betterdocs_breadcrumb_archive_lists', $_lists, $origin_term );
623 + }
588 624
589 - /**
590 - * Term Slug for tax_query
591 - */
592 - $_term_slug = '';
593 - if ( ! empty( $args['term_slug'] ) ) {
594 - $_term_slug = trim( $args['term_slug'] );
595 - unset( $args['term_slug'] );
596 - }
625 + /**
626 + * Get all non-empty child term IDs recursively for a given taxonomy and parent term.
627 + *
628 + * This function retrieves all child terms for a specified taxonomy and parent term,
629 + * recursively fetching child terms of child terms, and returns only those with a non-zero post count.
630 + *
631 + * @param string $taxonomy The taxonomy name (e.g., 'doc_category').
632 + * @param int $parent_id The ID of the parent term to start retrieving children from.
633 + *
634 + * @return array An array of non-empty child term IDs.
635 + */
636 + public function get_all_child_term_ids( $taxonomy, $parent_id ) {
637 + // Set up the arguments for retrieving child terms
638 + $args = apply_filters(
639 + 'betterdocs_get_child_term_ids_args',
640 + [
641 + 'taxonomy' => $taxonomy,
642 + 'parent' => $parent_id,
643 + 'hide_empty' => true,
644 + 'fields' => 'ids'
645 + ]
646 + );
597 647
598 - /**
599 - * @todo old hook
600 - * hook: betterdocs_cat_template_multikb
601 - */
648 + // Get the terms based on the arguments
649 + $terms = get_terms( $args );
602 650
603 - $_multiple_kb = apply_filters(
604 - 'betterdocs_enable_multiple_knowledge_base',
605 - isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
606 - $_origin_args
607 - );
651 + // Initialize an empty array to hold non-empty child term IDs
652 + $non_empty_children = [];
608 653
609 - $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
654 + // Check if terms were retrieved without errors and that the result isn't empty
655 + if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
656 + $term_ids = [];
610 657
611 - unset( $args['multiple_kb'] );
612 - unset( $args['kb_slug'] );
658 + // Loop through each term ID
659 + foreach ( $terms as $term_id ) {
660 + // Add the current term ID to the term_ids array
661 + $term_ids[] = $term_id;
613 662
614 - $tax_query = [
615 - [
616 - 'taxonomy' => 'doc_category',
617 - 'field' => 'slug',
618 - 'terms' => $_term_slug,
619 - 'operator' => 'AND',
620 - 'include_children' => false
621 - ]
622 - ];
663 + // Recursively get child terms for the current term
664 + $child_term_ids = $this->get_all_child_term_ids( $taxonomy, $term_id );
623 665
624 - if ( isset( $args['tax_query'] ) ) {
625 - $tax_query = $args['tax_query'];
626 - }
666 + // If there are child terms, merge them into the term_ids array
667 + if ( ! empty( $child_term_ids ) ) {
668 + $term_ids = array_merge( $term_ids, $child_term_ids );
669 + }
670 + }
627 671
628 - $args['tax_query'] = apply_filters(
629 - 'betterdocs_docs_tax_query_args',
630 - $tax_query, $_multiple_kb, $_term_slug, $_kb_slug, $_origin_args
631 - );
632 - /**
633 - * Final parse args
634 - */
635 - $args = wp_parse_args( $args, $default_args );
672 + // Loop through all retrieved term IDs to filter out empty ones
673 + foreach ( $term_ids as $term_id ) {
674 + // Get the term object for the current term ID
675 + $child_term = get_term( $term_id, $taxonomy );
636 676
637 - if ( ! empty( $filter ) ) {
638 - $filter = array_flip( $filter );
639 - $args = array_filter( $args, function ( $item ) use ( $filter ) {
640 - return ! array_key_exists( $item, $filter );
641 - }, ARRAY_FILTER_USE_KEY );
642 - }
677 + // Only include terms that have a non-zero post count
678 + if ( $child_term && isset( $child_term->term_id ) && $child_term->count > 0 ) {
679 + $non_empty_children[] = $child_term->term_id;
680 + }
681 + }
682 + }
643 683
644 - return apply_filters( 'betterdocs_articles_args', $args, $_term_id, $_origin_args );
645 - }
684 + // Return the final array of non-empty child term IDs
685 + return $non_empty_children;
686 + }
646 687
647 - public function faq_terms_query_args( $includes = '', $excludes = '', $args = [] ) {
648 - $_args = [
649 - 'taxonomy' => 'betterdocs_faq_category',
650 - 'meta_key' => 'order',
651 - 'orderby' => 'meta_value_num',
652 - 'order' => 'ASC',
653 - 'include' => $includes,
654 - 'exclude' => $excludes,
655 - 'meta_query' => [
656 - [
657 - 'key' => 'status',
658 - 'value' => 1,
659 - 'compare' => '=='
660 - ]
661 - ]
662 - ];
688 + /**
689 + * Get all nested child term IDs of a specific parent term in a taxonomy and return as a comma-separated string.
690 + *
691 + * @param string $taxonomy The taxonomy.
692 + * @param int $parent_id The parent term ID.
693 + * @return string The comma-separated term IDs.
694 + */
695 + public function get_child_term_ids_by_parent_id( $taxonomy, $parent_id ) {
696 + $term_ids = $this->get_all_child_term_ids( $taxonomy, $parent_id );
697 + return implode( ',', $term_ids );
698 + }
663 699
664 - if ( $_args['include'] == 'all' ) {
665 - unset( $_args['include'] );
666 - unset( $_args['exclude'] );
667 - }
700 + public function get_terms_children( $taxonomy, $parent_id ) {
701 + $children = get_term_children( $parent_id, $taxonomy );
702 + $non_empty_children = [];
703 + if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
704 + foreach ( $children as $child_id ) {
705 + $child_term = get_term( $child_id, $taxonomy );
668 706
669 - if ( empty( $_args['exclude'] ) ) {
670 - unset( $_args['exclude'] );
671 - }
707 + // Only include non-empty terms
708 + if ( $child_term && $child_term->count > 0 ) {
709 + $non_empty_children[] = $child_term;
710 + }
711 + }
712 + }
672 713
673 - if ( empty( $_args['include'] ) ) {
674 - unset( $_args['include'] );
675 - }
714 + return $non_empty_children;
715 + }
676 716
677 - return wp_parse_args( $args, $_args );
678 - }
717 + public function get_terms_children_ids( $taxonomy, $parent_id ) {
718 + $children = $this->get_terms_children( $taxonomy, $parent_id );
719 + $term_ids = wp_list_pluck( $children, 'term_id' );
679 720
680 - public function get_faq_by_term( $term_id ) {
681 - global $wpdb;
721 + return implode( ',', $term_ids );
722 + }
682 723
683 - $args = [
684 - 'post_type' => 'betterdocs_faq',
685 - 'post_status' => 'publish',
686 - 'tax_query' => [
687 - [
688 - 'taxonomy' => 'betterdocs_faq_category',
689 - 'field' => 'term_id',
690 - 'terms' => $term_id,
691 - 'operator' => 'AND'
692 - ]
693 - ],
694 - 'posts_per_page' => -1
695 - ];
724 + public function count_terms_children( $taxonomy, $parent_id ) {
725 + return count( $this->get_terms_children( $taxonomy, $parent_id ) );
726 + }
696 727
697 - $args['orderby'] = 'post__in';
698 - $args['post__in'] = $this->get_faq_orders( $term_id );
728 + public function is_inner_templates() {
729 + if ( is_tax( 'knowledge_base' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
730 + return true;
731 + }
732 + return false;
733 + }
699 734
700 - return new WP_Query( $args );
701 - }
735 + /**
736 + * Summary of docs_query_args
737 + * @param mixed $args
738 + * @throws \Exception
739 + * @return mixed
740 + */
741 + public function docs_query_args( $args, $filter = [] ) {
742 + $_origin_args = $args;
702 743
703 - public function get_faq_orders( $term_id = null ) {
704 - global $wpdb;
705 - $faq_order = get_term_meta( $term_id, '_betterdocs_faq_order', true );
706 - $faq_order = explode( ',', $faq_order );
744 + $default_args = [
745 + 'post_type' => 'docs'
746 + ];
707 747
708 - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
709 - $query_key = "betterdocs_faq_order_" . md5( $query );
748 + if ( ! empty( $args['post_type'] ) && trim( $args['post_type'] ) === 'docs_any' ) {
749 + $default_args['post_type'] = 'docs';
750 + $default_args['post_status'] = 'any';
710 751
711 - if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
712 - return $results;
713 - }
752 + unset( $args['post_type'] );
753 + }
714 754
715 - if ( ! empty( $faq_order ) ) {
716 - $new_ids = [];
717 - $results = $wpdb->get_results( $query );
755 + /**
756 + * OrderBy and Order
757 + */
758 + if ( ! isset( $args['orderby'] ) ) {
759 + $_orderby = $this->settings->get( 'alphabetically_order_post' );
760 + $_order = $this->settings->get( 'docs_order', 'ASC' );
761 + } else {
762 + $_orderby = $args['orderby'];
763 + $_order = ! empty( $args['order'] ) ? $args['order'] : 'ASC';
764 + }
718 765
719 - if ( ! is_null( $results ) && ! empty( $results ) && is_array( $results ) ) {
720 - $object_ids = array_filter( $results, function ( $value ) use ( $faq_order ) {
721 - return ! in_array( $value->object_id, $faq_order );
722 - } );
766 + if ( 'betterdocs_order' != $_orderby ) {
767 + if ( $_orderby === true ) {
768 + $args['orderby'] = 'title';
769 + } else {
770 + $args['orderby'] = $_orderby;
771 + }
723 772
724 - if ( ! empty( $object_ids ) ) {
725 - array_walk( $object_ids, function ( $value ) use ( &$new_ids ) {
726 - $new_ids[] = $value->object_id;
727 - } );
728 - }
729 - }
773 + $args['order'] = $_order;
774 + } elseif ( 'betterdocs_order' == $_orderby ) {
775 + unset( $args['orderby'] );
776 + }
730 777
731 - $faq_order = array_merge( $new_ids, $faq_order );
732 - }
778 + if ( empty( $args['orderby'] ) ) {
779 + unset( $args['order'] );
780 + }
733 781
734 - $this->database->set_cache( $query_key, $faq_order, 1 );
782 + /**
783 + * Term ID
784 + */
785 + $_term_id = null;
786 + if ( ! empty( $args['term_id'] ) ) {
787 + $_term_id = intval( $args['term_id'] );
788 + unset( $args['term_id'] );
789 + }
735 790
736 - return $faq_order;
737 - }
791 + // if ( $_term_id == null ) {
792 + // throw new \Exception( __( '$args["term_id"] cannot be null.', 'betterdocs' ) );
793 + // }
738 794
739 - public function get_faq_terms( $terms = [] ) {
740 - $_terms = get_terms( [
741 - 'taxonomy' => 'betterdocs_faq_category',
742 - 'hide_empty' => true,
743 - 'orderby' => 'name',
744 - 'order' => 'ASC',
745 - 'meta_query' => [
746 - [
747 - 'key' => 'status',
748 - 'value' => 1,
749 - 'compare' => '=='
750 - ]
751 - ]
752 - ] );
795 + /**
796 + * Term Slug for tax_query
797 + */
798 + $_term_slug = '';
799 + if ( ! empty( $args['term_slug'] ) ) {
800 + $_term_slug = trim( $args['term_slug'] );
801 + unset( $args['term_slug'] );
802 + }
753 803
754 - if ( ! is_wp_error( $_terms ) ) {
755 - foreach ( $_terms as $term ) {
756 - $terms[$term->term_id] = $term->name;
757 - }
758 - }
804 + /**
805 + * @todo old hook
806 + * hook: betterdocs_cat_template_multikb
807 + */
759 808
760 - return $terms;
761 - }
809 + $_multiple_kb = apply_filters(
810 + 'betterdocs_enable_multiple_knowledge_base',
811 + isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
812 + $_origin_args
813 + );
762 814
763 - public function get_docs_count( $term, $nested_subcategory = false, $args = [] ) {
764 - $counts = isset( $term->count ) ? $term->count : 0;
815 + $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
765 816
766 - if ( $nested_subcategory == false ) {
767 - return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
768 - }
817 + unset( $args['multiple_kb'] );
818 + unset( $args['kb_slug'] );
769 819
770 - $_child_terms_docs_ids = $this->get_doc_ids_by_term( $term, null, $nested_subcategory );
771 - if ( is_array( $_child_terms_docs_ids ) ) {
772 - $counts = count( $_child_terms_docs_ids );
773 - }
820 + $tax_query = [
821 + [
822 + 'taxonomy' => 'doc_category',
823 + 'field' => 'slug',
824 + 'terms' => $_term_slug,
825 + 'operator' => 'AND',
826 + 'include_children' => false
827 + ]
828 + ];
774 829
775 - return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
776 - }
830 + if ( ! isset( $args['orderby'] ) || $args['orderby'] == 'betterdocs_order' ) {
831 + $args['orderby'] = 'post__in';
832 + $args['post__in'] = $this->get_docs_order_by_terms( $_term_id );
833 + }
777 834
778 - public function get_doc_ids_by_term( $term, $optional = null, $nested_subcategory = false ) {
779 - $args = [ 'include' => $term->term_id ];
780 - if( $nested_subcategory ) {
835 + if ( isset( $args['tax_query'] ) ) {
836 + $tax_query = $args['tax_query'];
837 + }
838 +
839 + $args['tax_query'] = apply_filters(
840 + 'betterdocs_docs_tax_query_args',
841 + $tax_query,
842 + $_multiple_kb,
843 + $_term_slug,
844 + $_kb_slug,
845 + $_origin_args,
846 + $this->is_inner_templates()
847 + );
848 + /**
849 + * Final parse args
850 + */
851 + $args = wp_parse_args( $args, $default_args );
852 +
853 + if ( ! empty( $filter ) ) {
854 + $filter = array_flip( $filter );
855 + $args = array_filter(
856 + $args,
857 + function ( $item ) use ( $filter ) {
858 + return ! array_key_exists( $item, $filter );
859 + },
860 + ARRAY_FILTER_USE_KEY
861 + );
862 + }
863 +
864 + return apply_filters( 'betterdocs_articles_args', $args, $_term_id, $_origin_args );
865 + }
866 +
867 + public function faq_terms_query_args( $includes = '', $excludes = '', $args = [] ) {
868 + $_args = [
869 + 'taxonomy' => 'betterdocs_faq_category',
870 + 'meta_key' => 'order',
871 + 'orderby' => 'meta_value_num',
872 + 'order' => 'ASC',
873 + 'include' => $includes,
874 + 'exclude' => $excludes,
875 + 'meta_query' => [
876 + [
877 + 'key' => 'status',
878 + 'value' => 1,
879 + 'compare' => '=='
880 + ]
881 + ]
882 + ];
883 +
884 + if ( $_args['include'] == 'all' ) {
885 + unset( $_args['include'] );
886 + unset( $_args['exclude'] );
887 + }
888 +
889 + if ( empty( $_args['exclude'] ) ) {
890 + unset( $_args['exclude'] );
891 + }
892 +
893 + if ( empty( $_args['include'] ) ) {
894 + unset( $_args['include'] );
895 + }
896 +
897 + return wp_parse_args( $args, $_args );
898 + }
899 +
900 + public function get_faq_by_term( $term_id ) {
901 + global $wpdb;
902 +
903 + $args = [
904 + 'post_type' => 'betterdocs_faq',
905 + 'post_status' => 'publish',
906 + 'tax_query' => [
907 + [
908 + 'taxonomy' => 'betterdocs_faq_category',
909 + 'field' => 'term_id',
910 + 'terms' => $term_id,
911 + 'operator' => 'AND'
912 + ]
913 + ],
914 + 'posts_per_page' => -1
915 + ];
916 +
917 + $args['orderby'] = 'post__in';
918 + $args['post__in'] = $this->get_faq_orders( $term_id );
919 +
920 + return new WP_Query( $args );
921 + }
922 +
923 + public function get_faq_orders( $term_id = null ) {
924 + global $wpdb;
925 + $faq_order = get_term_meta( $term_id, '_betterdocs_faq_order', true );
926 + $faq_order = explode( ',', $faq_order );
927 +
928 + $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
929 + $query_key = 'betterdocs_faq_order_' . md5( $query );
930 +
931 + if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
932 + return $results;
933 + }
934 +
935 + if ( ! empty( $faq_order ) ) {
936 + $new_ids = [];
937 + $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
938 +
939 + if ( ! is_null( $results ) && ! empty( $results ) && is_array( $results ) ) {
940 + $object_ids = array_filter(
941 + $results,
942 + function ( $value ) use ( $faq_order ) {
943 + return ! in_array( $value->object_id, $faq_order );
944 + }
945 + );
946 +
947 + if ( ! empty( $object_ids ) ) {
948 + array_walk(
949 + $object_ids,
950 + function ( $value ) use ( &$new_ids ) {
951 + $new_ids[] = $value->object_id;
952 + }
953 + );
954 + }
955 + }
956 +
957 + $faq_order = array_merge( $new_ids, $faq_order );
958 + }
959 +
960 + $this->database->set_cache( $query_key, $faq_order, 1 );
961 +
962 + return $faq_order;
963 + }
964 +
965 + public function get_faq_terms( $terms = [] ) {
966 + $_terms = get_terms(
967 + [
968 + 'taxonomy' => 'betterdocs_faq_category',
969 + 'hide_empty' => true,
970 + 'orderby' => 'name',
971 + 'order' => 'ASC',
972 + 'meta_query' => [
973 + [
974 + 'key' => 'status',
975 + 'value' => 1,
976 + 'compare' => '=='
977 + ]
978 + ]
979 + ]
980 + );
981 +
982 + if ( ! is_wp_error( $_terms ) ) {
983 + foreach ( $_terms as $term ) {
984 + $terms[ $term->term_id ] = $term->name;
985 + }
986 + }
987 +
988 + return $terms;
989 + }
990 +
991 + public function get_doc_terms( $terms = [] ) {
992 + $_terms = get_terms(
993 + [
994 + 'taxonomy' => 'doc_category',
995 + 'hide_empty' => true,
996 + 'orderby' => 'name',
997 + 'order' => 'ASC',
998 + ]
999 + );
1000 +
1001 + if ( ! is_wp_error( $_terms ) ) {
1002 + foreach ( $_terms as $term ) {
1003 + $terms[ $term->term_id ] = $term->name;
1004 + }
1005 + }
1006 +
1007 + return $terms;
1008 + }
1009 +
1010 +
1011 + public function get_docs_count( $term, $nested_subcategory = false, $args = [] ) {
1012 + // Validate term object
1013 + if ( ! is_object( $term ) ) {
1014 + return 0;
1015 + }
1016 +
1017 + $counts = isset( $term->count ) ? $term->count : 0;
1018 +
1019 + if ( $nested_subcategory == false ) {
1020 + // For non-nested categories, we need to recalculate counts based on user capabilities
1021 + // Only proceed if we have a valid term with required properties
1022 + if ( isset( $term->term_id ) && isset( $term->taxonomy ) && is_numeric( $term->term_id ) ) {
1023 + // Get all post IDs for this term
1024 + $post_ids = get_objects_in_term( $term->term_id, $term->taxonomy );
1025 +
1026 + if ( ! empty( $post_ids ) ) {
1027 + if ( current_user_can( 'read_private_docs' ) ) {
1028 + // For users with read_private_docs capability, include both private and public posts
1029 + $filtered_post_ids = array_filter( $post_ids, function( $post_id ) {
1030 + $post_status = get_post_status( $post_id );
1031 + return $post_status === 'private' || is_post_publicly_viewable( $post_id );
1032 + });
1033 + } else {
1034 + // For users without read_private_docs capability, only include public posts
1035 + $filtered_post_ids = array_filter( $post_ids, function( $post_id ) {
1036 + return is_post_publicly_viewable( $post_id );
1037 + });
1038 + }
1039 +
1040 + $counts = count( $filtered_post_ids );
1041 + } else {
1042 + $counts = 0;
1043 + }
1044 + }
1045 +
1046 + return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
1047 + }
1048 +
1049 + $_child_terms_docs_ids = $this->get_doc_ids_by_term( $term, null, $nested_subcategory );
1050 + if ( is_array( $_child_terms_docs_ids ) ) {
1051 + $counts = count( $_child_terms_docs_ids );
1052 + }
1053 +
1054 + return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
1055 + }
1056 +
1057 + public function get_doc_ids_by_term( $term, $optional = null, $nested_subcategory = false ) {
1058 + // Check if term has required properties and is a valid object
1059 + if ( ! is_object( $term ) || ! isset( $term->term_id ) || ! isset( $term->taxonomy ) || ! is_numeric( $term->term_id ) ) {
1060 + return false;
1061 + }
1062 +
1063 + $args = ['include' => $term->term_id];
1064 + if ( $nested_subcategory ) {
781 1065 $args['child_of'] = $term->term_id;
782 1066 unset( $args['include'] );
783 1067 }
784 1068 $_child_terms = get_terms( $term->taxonomy, $args );
@@ -798,9 +1082,9 @@
798 1082 $_child_terms_docs_ids = array_intersect( $_child_terms_docs_ids, $_optional_doc_ids );
799 1083 }
800 1084
801 1085 return array_filter( $_child_terms_docs_ids, function ( $doc_id ) {
802 - if ( ! is_user_logged_in() ) {
1086 + if ( ! current_user_can( 'read_private_docs' ) ) {
803 1087 return is_post_publicly_viewable( $doc_id );
804 1088 }
805 1089
806 1090 $_status = get_post_status( $doc_id );
@@ -805,6 +1089,239 @@
805 1089
806 1090 $_status = get_post_status( $doc_id );
807 1091 return $_status == 'private' || is_post_publicly_viewable( $doc_id );
808 1092 } );
809 - }
1093 + }
1094 +
1095 + /**
1096 + * Get the common query arguments for WP_Query.
1097 + *
1098 + * @param string $terms The taxonomy.
1099 + * @param string $term_slug The taxonomy term slug.
1100 + * @param array $additional_args Additional arguments to merge with the common arguments.
1101 + * @return array The query arguments.
1102 + */
1103 + private function tax_query_args( $terms, $term_slug, $additional_args = array() ) {
1104 + $common_args = array(
1105 + 'post_type' => 'docs',
1106 + 'post_status' => 'publish',
1107 + 'tax_query' => array(
1108 + array(
1109 + 'taxonomy' => $terms,
1110 + 'field' => 'slug',
1111 + 'terms' => $term_slug,
1112 + ),
1113 + ),
1114 + );
1115 + return array_merge( $common_args, $additional_args );
1116 + }
1117 +
1118 + /**
1119 + * Get the latest updated date for a specific taxonomy term.
1120 + *
1121 + * @param string $terms The taxonomy.
1122 + * @param string $term_slug The taxonomy term slug.
1123 + * @return string|null The latest modified date or null if no posts found.
1124 + */
1125 + public function latest_updated_date( $terms, $term_slug ) {
1126 + $args = $this->tax_query_args(
1127 + $terms,
1128 + $term_slug,
1129 + array(
1130 + 'posts_per_page' => 1,
1131 + 'orderby' => 'modified',
1132 + 'order' => 'DESC',
1133 + )
1134 + );
1135 +
1136 + $query = new WP_Query( $args );
1137 +
1138 + if ( $query->have_posts() ) {
1139 + while ( $query->have_posts() ) {
1140 + $query->the_post();
1141 + $latest_post = get_the_modified_date();
1142 + wp_reset_postdata();
1143 + return $latest_post;
1144 + }
1145 + } else {
1146 + return null;
1147 + }
1148 + }
1149 +
1150 + /**
1151 + * Check if there are any new posts within the last 7 days for a specific taxonomy term.
1152 + *
1153 + * @param string $terms The taxonomy.
1154 + * @param string $term_slug The taxonomy term slug.
1155 + * @return bool True if there are new posts, false otherwise.
1156 + */
1157 + public function check_new_posts( $terms, $term_slug ) {
1158 + $date_7_days_ago = date( 'Y-m-d H:i:s', strtotime( '-7 days' ) );
1159 +
1160 + $args = $this->tax_query_args(
1161 + $terms,
1162 + $term_slug,
1163 + array(
1164 + 'posts_per_page' => 1,
1165 + 'orderby' => 'modified',
1166 + 'order' => 'DESC',
1167 + 'date_query' => array(
1168 + array(
1169 + 'after' => $date_7_days_ago,
1170 + 'inclusive' => true,
1171 + ),
1172 + ),
1173 + )
1174 + );
1175 +
1176 + $query = new WP_Query( $args );
1177 +
1178 + $has_new_posts = $query->have_posts();
1179 +
1180 + wp_reset_postdata();
1181 +
1182 + return $has_new_posts;
1183 + }
1184 +
1185 + public function insert_search_keyword( $search_input, $input_not_found ) {
1186 + if ( empty( $search_input ) ) {
1187 + return false;
1188 + }
1189 +
1190 + global $wpdb;
1191 + $search = $wpdb->get_results(
1192 + $wpdb->prepare(
1193 + "SELECT *
1194 + FROM {$wpdb->prefix}betterdocs_search_keyword
1195 + WHERE keyword = %s",
1196 + $search_input
1197 + )
1198 + );
1199 +
1200 + if ( ! empty( $search ) ) {
1201 + $search_log = $wpdb->get_results(
1202 + $wpdb->prepare(
1203 + "SELECT *
1204 + FROM {$wpdb->prefix}betterdocs_search_log
1205 + WHERE created_at = %s AND keyword_id = %d",
1206 + date( 'Y-m-d' ),
1207 + $search[0]->id
1208 + )
1209 + );
1210 +
1211 + if ( ! empty( $search_log ) ) {
1212 + if ( ! empty( $input_not_found ) ) {
1213 + $tbl_field = 'not_found_count';
1214 + $count = $search_log[0]->not_found_count + 1;
1215 + } else {
1216 + $tbl_field = 'count';
1217 + $count = $search_log[0]->count + 1;
1218 + }
1219 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1220 + $insert = $wpdb->query(
1221 + $wpdb->prepare(
1222 + "UPDATE {$wpdb->prefix}betterdocs_search_log
1223 + SET " . $tbl_field . ' = ' . $count . '
1224 + WHERE created_at = %s AND keyword_id = %d',
1225 + $search_log[0]->created_at,
1226 + $search_log[0]->keyword_id
1227 + )
1228 + );
1229 + } else {
1230 + if ( ! empty( $input_not_found ) ) {
1231 + $count = 0;
1232 + $not_found_count = 1;
1233 + } else {
1234 + $count = 1;
1235 + $not_found_count = 0;
1236 + }
1237 + $insert = $wpdb->query(
1238 + $wpdb->prepare(
1239 + "INSERT INTO {$wpdb->prefix}betterdocs_search_log
1240 + ( keyword_id, count, not_found_count, created_at )
1241 + VALUES ( %d, %d, %d, %s )",
1242 + [
1243 + $search[0]->id,
1244 + $count,
1245 + $not_found_count,
1246 + date( 'Y-m-d' )
1247 + ]
1248 + )
1249 + );
1250 + }
1251 + } else {
1252 + $insert = $wpdb->query(
1253 + $wpdb->prepare(
1254 + "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword
1255 + ( keyword )
1256 + VALUES ( %s )",
1257 + [
1258 + $search_input
1259 + ]
1260 + )
1261 + );
1262 +
1263 + if ( $insert ) {
1264 + if ( ! empty( $input_not_found ) ) {
1265 + $count = 0;
1266 + $not_found_count = 1;
1267 + } else {
1268 + $count = 1;
1269 + $not_found_count = 0;
1270 + }
1271 + $insert = $wpdb->query(
1272 + $wpdb->prepare(
1273 + "INSERT INTO {$wpdb->prefix}betterdocs_search_log
1274 + ( keyword_id, count, not_found_count, created_at )
1275 + VALUES ( %d, %d, %d, %s )",
1276 + [
1277 + $wpdb->insert_id,
1278 + $count,
1279 + $not_found_count,
1280 + date( 'Y-m-d' )
1281 + ]
1282 + )
1283 + );
1284 + }
1285 + }
1286 + return $insert;
1287 + }
1288 +
1289 + /**
1290 + * Counts the number of 'doc_category' terms assigned to a specific 'knowledge_base' by slug.
1291 + *
1292 + * This method retrieves all terms in the 'doc_category' taxonomy and checks the term meta
1293 + * 'doc_category_knowledge_base' to determine how many 'doc_category' terms are associated
1294 + * with a given 'knowledge_base' slug.
1295 + *
1296 + * @param string $knowledge_base_slug The slug of the 'knowledge_base' to count the assignments for.
1297 + *
1298 + * @return int The count of 'doc_category' terms assigned to the specified 'knowledge_base'.
1299 + */
1300 + public function count_doc_categories_for_knowledge_base( $knowledge_base_slug ) {
1301 + $count = 0;
1302 +
1303 + $doc_categories = get_terms(
1304 + array(
1305 + 'taxonomy' => 'doc_category',
1306 + 'hide_empty' => true,
1307 + )
1308 + );
1309 +
1310 + if ( ! empty( $doc_categories ) && ! is_wp_error( $doc_categories ) ) {
1311 + foreach ( $doc_categories as $term ) {
1312 + $meta_value = get_term_meta( $term->term_id, 'doc_category_knowledge_base', true );
1313 +
1314 + if ( $meta_value ) {
1315 + $knowledge_bases = maybe_unserialize( $meta_value );
1316 +
1317 + // Check if the specific knowledge base slug is present in the meta value array
1318 + if ( is_array( $knowledge_bases ) && in_array( $knowledge_base_slug, $knowledge_bases ) ) {
1319 + ++$count;
1320 + }
1321 + }
1322 + }
1323 + }
1324 +
1325 + return $count;
1326 + }
810 1327 }