PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.5.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.5.3
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 +267 -1082 4.9.13.5.3 View file →
@@ -1,14 +1,8 @@
1 1 <?php
2 -// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query,WordPress.DB.SlowDBQuery.slow_db_query_tax_query,WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- core docs query builder; meta/tax filtering required for docs/category/KB filtering.
3 -// phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- excludes are user-driven (settings UI) and part of the query builder's public contract.
2 +
4 3 namespace WPDeveloper\BetterDocs\Core;
5 4
6 -if ( ! defined( 'ABSPATH' ) ) {
7 - exit;
8 -}
9 -
10 -
11 5 use WP_Query;
12 6 use WPDeveloper\BetterDocs\Utils\Base;
13 7 use WPDeveloper\BetterDocs\Utils\Database;
14 8 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
@@ -22,24 +16,12 @@
22 16 $this->container = $container;
23 17 $this->database = $database;
24 18 $this->settings = $settings;
25 19
26 - add_action( 'parse_term_query', array( $this, 'parse_term_query' ) );
20 + add_action( 'parse_term_query', [$this, 'parse_term_query'] );
27 21 // add_action( 'parse_query', [$this, 'parse_query'], 1 );
28 - add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 1 );
29 - add_filter( 'betterdocs_base_terms_args', array( $this, 'modify_terms_args_for_private_docs' ), 10, 1 );
30 - // Drops terms whose private-inclusive count is 0 after `hide_empty` was
31 - // turned off by modify_terms_args_for_private_docs().
32 - add_filter( 'get_terms', array( $this, 'filter_terms_for_private_docs' ), 10, 3 );
22 + add_action( 'pre_get_posts', [$this, 'pre_get_posts'], 1 );
33 23
34 - // Invalidate cached doc-category counts on any write that could change them.
35 - add_action( 'save_post_docs', array( $this, 'flush_term_counts_cache' ) );
36 - add_action( 'deleted_post', array( $this, 'flush_term_counts_cache_on_post' ), 10, 2 );
37 - add_action( 'edited_doc_category', array( $this, 'flush_term_counts_cache' ) );
38 - add_action( 'created_doc_category', array( $this, 'flush_term_counts_cache' ) );
39 - add_action( 'delete_doc_category', array( $this, 'flush_term_counts_cache' ) );
40 - add_action( 'set_object_terms', array( $this, 'flush_term_counts_cache_on_set' ), 10, 4 );
41 -
42 24 /**
43 25 * These below filters are hooked for navigation only.
44 26 *
45 27 * For old version of this portion.
@@ -50,10 +32,10 @@
50 32 * @see https://developer.wordpress.org/reference/functions/get_previous_post/
51 33 *
52 34 * @link https://developer.wordpress.org/reference/hooks/get_adjacent_post_where/
53 35 */
54 - add_filter( 'get_next_post_where', array( $this, 'next_post_where' ), 99, 5 );
55 - add_filter( 'get_previous_post_where', array( $this, 'previous_post_where' ), 99, 5 );
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 );
56 38
57 39 $this->init();
58 40
59 41 /**
@@ -58,29 +40,21 @@
58 40
59 41 /**
60 42 * Modify Popular Docs Query (For Shortcode & Widget)
61 43 */
62 - add_filter( 'posts_clauses', array( $this, 'mod_query_popular_docs' ), 10, 2 );
44 + add_filter( 'posts_clauses', [$this, 'mod_query_popular_docs'], 10, 2 );
63 45 }
64 46
65 47 public function mod_query_popular_docs( $clauses, $wp_query ) {
66 - if ( isset( $wp_query->query[ 'meta_key' ] ) ) {
67 - if ( '_betterdocs_meta_views' == $wp_query->query[ 'meta_key' ] ) {
48 + if ( isset( $wp_query->query['meta_key'] ) ) {
49 + if ( $wp_query->query['meta_key'] == '_betterdocs_meta_views' ) {
68 50 global $wpdb;
69 - $order = isset( $wp_query->query[ 'order' ] ) ? $wp_query->query[ 'order' ] : '';
70 - $order_by_query = ( 'ASC' == $order || 'DESC' == $order ) ? "SUM({$wpdb->prefix}betterdocs_analytics.impressions) {$order}" : ( 'MODIFIED' == $order ? "{$wpdb->prefix}posts.post_modified_gmt DESC" : "{$wpdb->prefix}posts.post_date_gmt DESC" );
71 - $clauses[ 'join' ] = "JOIN {$wpdb->prefix}betterdocs_analytics ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}betterdocs_analytics.post_id";
72 - $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' ) )";
73 - $clauses[ 'orderby' ] = $order_by_query;
74 - $clauses[ 'groupby' ] = "{$wpdb->prefix}betterdocs_analytics.post_id";
75 - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
76 - global $sitepress;
77 - if ( $sitepress->is_setup_complete() ) {
78 - $constant_language_code = ICL_LANGUAGE_CODE;
79 - $clauses[ 'join' ] .= " JOIN {$wpdb->prefix}icl_translations ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}icl_translations.element_id";
80 - $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 ) )";
81 - }
82 - }
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";
83 57 }
84 58 }
85 59 return $clauses;
86 60 }
@@ -87,95 +61,33 @@
87 61
88 62 public function init() {
89 63 }
90 64
91 - /**
92 - * Modify terms args to include terms whose only docs are private, for users allowed to read them.
93 - *
94 - * Gated on being logged in rather than on `read_private_docs`, because a doc's own
95 - * author may read their private doc without holding that capability. Which docs
96 - * actually count is decided per post by can_read_doc().
97 - *
98 - * @param array $args
99 - * @return array
100 - */
101 - public function modify_terms_args_for_private_docs( $args ) {
102 - // Logged-out visitors always get WordPress' stock `hide_empty` behaviour.
103 - if ( ! is_user_logged_in() || ! isset( $args[ 'taxonomy' ] ) ) {
104 - return $args;
105 - }
106 -
107 - /**
108 - * Let add-ons opt their own taxonomies into the private-docs rescue path.
109 - *
110 - * BetterDocs Pro hooks this for `knowledge_base` (Multiple KB), where docs
111 - * ARE assigned to the KB term directly.
112 - *
113 - * @param array $args Term query args.
114 - */
115 - $_filtered = apply_filters( 'betterdocs_modify_terms_args_for_private_docs', $args );
116 - if ( is_array( $_filtered ) ) {
117 - $args = $_filtered;
118 - }
119 -
120 - // Already handled by an add-on (e.g. Pro's Multiple KB).
121 - if ( ! empty( $args[ '_betterdocs_filter_private' ] ) ) {
122 - return $args;
123 - }
124 -
125 - /**
126 - * Taxonomies whose terms have docs assigned directly, so a private-inclusive
127 - * object count can rescue a term WordPress dropped for `hide_empty`.
128 - *
129 - * `knowledge_base` is included so a KB whose only docs are private still shows
130 - * up for privileged users even on older Pro builds that predate the filter above.
131 - */
132 - $supported_taxonomies = array( 'doc_category', 'knowledge_base' );
133 - if ( ! in_array( $args[ 'taxonomy' ], $supported_taxonomies, true ) ) {
134 - return $args;
135 - }
136 -
137 - // If hide_empty is true, we need to modify the logic to include terms with private docs
138 - if ( isset( $args[ 'hide_empty' ] ) && $args[ 'hide_empty' ] ) {
139 - // Set hide_empty to false and we'll filter manually later
140 - $args[ 'hide_empty' ] = false;
141 - // Add a flag to indicate we need to filter manually
142 - $args[ '_betterdocs_filter_private' ] = true;
143 - }
144 -
145 - return $args;
146 - }
147 -
148 65 public function parse_term_query( $term_query ) {
149 - if ( empty( $term_query->query_vars[ 'taxonomy' ] ) ) {
66 + if ( empty( $term_query->query_vars['taxonomy'] ) ) {
150 67 return;
151 68 }
152 69
153 - if ( ! in_array( 'doc_category', $term_query->query_vars[ 'taxonomy' ], true ) ) {
70 + if ( ! in_array( 'doc_category', $term_query->query_vars['taxonomy'], true ) ) {
154 71 return;
155 72 }
156 73
157 74 global $current_screen;
158 75
159 - if ( null == $current_screen ) {
76 + if ( $current_screen == null ) {
160 77 return;
161 78 }
162 79
163 - if ( 'doc_category' !== $current_screen->taxonomy || 'edit-doc_category' != $current_screen->id ) {
80 + if ( $current_screen->taxonomy !== 'doc_category' || $current_screen->id != 'edit-doc_category' ) {
164 81 return;
165 82 }
166 83
167 - // Use base meta key for admin listing - fallback logic is handled in set_tax_order
168 - $meta_key = 'doc_category_order';
84 + $term_query->query_vars['meta_query'] = [[
85 + 'key' => 'doc_category_order',
86 + 'type' => 'NUMERIC'
87 + ]];
169 88
170 - $term_query->query_vars[ 'meta_query' ] = array(
171 - array(
172 - 'key' => $meta_key,
173 - 'type' => 'NUMERIC'
174 - )
175 - );
176 -
177 - $term_query->query_vars[ 'orderby' ] = 'meta_value_num';
89 + $term_query->query_vars['orderby'] = 'meta_value_num';
178 90 }
179 91
180 92 public function parse_query( &$query ) {
181 93 // dump( is_single(), $query->query_vars );
@@ -196,13 +108,9 @@
196 108 $query->set( 'posts_per_archive_page', -1 );
197 109
198 110 $term = get_term_by( 'slug', $query->get( 'doc_category', '' ), 'doc_category' );
199 111
200 - if ( $term && isset( $term->term_id ) ) {
201 - $post__in = $this->get_docs_order_by_terms( $term->term_id );
202 - } else {
203 - $post__in = array();
204 - }
112 + $post__in = $this->get_docs_order_by_terms( $term->term_id );
205 113 if ( ! empty( $post__in ) ) {
206 114 $query->set( 'orderby', 'post__in' );
207 115 $query->set( 'post__in', $post__in );
208 116 }
@@ -236,42 +144,31 @@
236 144 * @return array
237 145 */
238 146 public function get_docs_order_by_terms( $term_id ) {
239 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 );
240 151
241 - // Get language-specific meta key with fallback
242 - $meta_key = \WPDeveloper\BetterDocs\Utils\Helper::get_meta_key_with_fallback( '_docs_order', $term_id );
243 - $_docs_order = get_term_meta( $term_id, $meta_key, true );
244 -
245 - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
246 - $query_key = "docs_order_by_terms_{$term_id}_{$meta_key}_" . md5( $query );
247 -
248 - if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
152 + if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
249 153 return $results;
250 154 }
251 155
252 156 if ( ! empty( $_docs_order ) ) {
253 157 $_docs_order = explode( ',', $_docs_order );
254 - $new_ids = array();
158 + $new_ids = [];
255 159
256 - // $query is prepared upstream; results are cached via $this->database->get_cache above (line 210).
257 - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
160 + $results = $wpdb->get_results( $query );
258 161
259 162 if ( is_array( $results ) && ! empty( $results ) ) {
260 - $object_ids = array_filter(
261 - $results,
262 - function ( $value ) use ( $_docs_order ) {
263 - return ! in_array( $value->object_id, $_docs_order );
264 - }
265 - );
163 + $object_ids = array_filter( $results, function ( $value ) use ( $_docs_order ) {
164 + return ! in_array( $value->object_id, $_docs_order );
165 + } );
266 166
267 167 if ( ! empty( $object_ids ) ) {
268 - array_walk(
269 - $object_ids,
270 - function ( $value ) use ( &$new_ids ) {
271 - $new_ids[ ] = $value->object_id;
272 - }
273 - );
168 + array_walk( $object_ids, function ( $value ) use ( &$new_ids ) {
169 + $new_ids[] = $value->object_id;
170 + } );
274 171 }
275 172 }
276 173
277 174 $_docs_order = array_merge( $new_ids, $_docs_order );
@@ -279,9 +176,9 @@
279 176
280 177 return $_docs_order;
281 178 }
282 179
283 - return array();
180 + return [];
284 181 }
285 182
286 183 /**
287 184 * For determine the next post ID
@@ -326,9 +223,9 @@
326 223 * @param mixed $taxonomy
327 224 * @return mixed
328 225 */
329 226 private function get_adjacent_post_id( $adjacent, $where, $post, $taxonomy ) {
330 - if ( 'doc_category' !== $taxonomy ) {
227 + if ( $taxonomy !== 'doc_category' ) {
331 228 return $where;
332 229 }
333 230
334 231 $_id = null;
@@ -338,88 +235,57 @@
338 235 }
339 236
340 237 global $wp_query, $wpdb;
341 238
342 - $_docs_order = $this->get_docs_order_by_terms( $_terms[ 0 ]->term_id );
239 + $_docs_order = $this->get_docs_order_by_terms( $_terms[0]->term_id );
343 240
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 + }
247 +
344 248 $_orderby = $this->settings->get( 'alphabetically_order_post', 'betterdocs_order' );
345 249 $_order = $this->settings->get( 'docs_order', 'ASC' );
346 - $_docs_order = 'betterdocs_order' === $_orderby ? $_docs_order : array();
250 + $_docs_order = $_orderby === 'betterdocs_order' ? $_docs_order : [];
347 251
348 252 if ( empty( $_docs_order ) ) {
349 - $statuses = array( 'publish' );
253 + $statuses = [ 'publish' ];
350 254
351 - if ( is_user_logged_in() ) {
352 - $statuses[ ] = 'private';
255 + if( is_user_logged_in() ) {
256 + $statuses[] = 'private';
353 257 }
354 258
355 - $_args = array(
356 - 'post_status' => $statuses,
357 - // Required: with an explicit `post_status` WordPress only narrows private
358 - // docs to the ones the user may read when `perm` is `readable`. Without it,
359 - // listing `private` would expose every private doc to any logged-in user.
360 - 'perm' => 'readable',
361 - 'term_id' => $_terms[ 0 ]->term_id
362 - );
363 - if ( isset( $wp_query->query_vars[ 'doc_category' ] ) ) {
364 - $_args[ 'tax_query' ][ ] = array(
365 - 'taxonomy' => 'doc_category',
366 - 'field' => 'slug',
367 - 'terms' => $wp_query->query_vars[ 'doc_category' ],
368 - 'operator' => 'AND',
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',
369 268 'include_children' => false
370 - );
371 - } elseif ( isset( $wp_query->query_vars[ 'name' ] ) && isset( $wp_query->query_vars[ 'post_type' ] ) && 'docs' == $wp_query->query_vars[ 'post_type' ] ) {
372 - $_post = get_page_by_path( $wp_query->query_vars[ 'name' ], OBJECT, 'docs' );
373 - $doc_terms = array();
374 -
375 - if ( isset( $_post->ID ) ) {
376 - $terms = get_the_terms( $_post->ID, 'doc_category' );
377 - if ( ! empty( $terms ) ) {
378 - $doc_terms = wp_list_pluck( $terms, 'term_id' );
379 - }
380 - }
381 -
382 - if ( ! empty( $doc_terms ) ) {
383 - $_args[ 'tax_query' ][ ] = array(
384 - 'taxonomy' => 'doc_category',
385 - 'field' => 'term_id',
386 - 'terms' => $doc_terms,
387 - 'operator' => 'AND',
388 - 'include_children' => false
389 - );
390 - $_args[ 'term_id' ] = $doc_terms[ 0 ];
391 - }
392 - } else {
393 - // Fallback: use the term from current post
394 - $_args[ 'tax_query' ][ ] = array(
395 - 'taxonomy' => 'doc_category',
396 - 'field' => 'term_id',
397 - 'terms' => $_terms[ 0 ]->term_id,
398 - 'operator' => 'AND',
399 - 'include_children' => false
400 - );
269 + ];
401 270 }
402 271
403 - $_args[ 'orderby' ] = $_orderby;
404 - if ( 'betterdocs_order' != $_orderby ) {
405 - $_args[ 'order' ] = $_order;
272 + $_args['orderby'] = $_orderby;
273 + if ( $_orderby != 'betterdocs_order' ) {
274 + $_args['order'] = $_order;
406 275 }
407 276
408 277 /**
409 278 * Before Query
410 279 */
411 - do_action_ref_array( 'betterdocs_navigation_docs_query', array( &$_args ) );
280 + do_action_ref_array( 'betterdocs_navigation_docs_query', [ &$_args] );
412 281 $docs = $this->get_posts( $this->docs_query_args( $_args ) );
413 282
414 - $_docs = array();
283 + $_docs = [];
415 284 if ( $docs->have_posts() ) {
416 - array_map(
417 - function ( $_post ) use ( &$_docs ) {
418 - $_docs[ ] = $_post->ID;
419 - },
420 - $docs->posts
421 - );
285 + array_map( function ( $post ) use ( &$_docs ) {
286 + $_docs[] = $post->ID;
287 + }, $docs->posts );
422 288 }
423 289
424 290 $_docs_order = $_docs;
425 291 }
@@ -425,49 +291,32 @@
425 291 }
426 292
427 293 $_docs_order = apply_filters( 'betterdocs_adjacent_docs_order', $_docs_order, $_terms );
428 294
429 - // Ensure consistent type comparison by converting all IDs to integers
430 - $_docs_order = array_map( 'intval', $_docs_order );
431 - $_docs_order = array_values( $_docs_order ); // Re-index array after type conversion
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;
432 298
433 - $_id_index = array_search( (int) $post->ID, $_docs_order, true );
434 -
435 - // If current post not found in order list, return original where clause
436 - if ( false === $_id_index ) {
437 - return $where;
438 - }
439 -
440 - $_id_index = 'next' === $adjacent ? $_id_index + 1 : $_id_index - 1;
441 - $_id = isset( $_docs_order[ $_id_index ] ) ? (int) $_docs_order[ $_id_index ] : null;
442 -
443 - // Fix: replace only ID comparison in where clause using regex
444 - if ( $_id ) {
445 - // This replaces any 'p.ID < N', 'p.ID > N', 'p.ID <= N', or 'p.ID >= N' etc, with 'p.ID = $_id'
446 - $where = preg_replace( '/p\.ID\s*[<>!=]+\s*\d+/', 'p.ID = ' . (int) $_id, $where );
447 - }
448 -
449 - return $where;
299 + return $wpdb->prepare( "WHERE p.ID = %s AND $_where", (int) $_id );
450 300 }
451 301
452 - public function parse_terms_args( $args = array() ) {
453 - $_default_args = array(
302 + public function parse_terms_args( $args = [] ) {
303 + $_default_args = [
454 304 'hide_empty' => true,
455 - 'taxonomy' => 'doc_category'
456 - );
305 + 'taxonomy' => 'doc_category'
306 + ];
457 307
458 308 // OrderBy & Order
459 - $_orderby = ! empty( $args[ 'orderby' ] ) && 1 != $args[ 'orderby' ] ? $args[ 'orderby' ] : 'name';
460 - $_order = ! empty( $args[ 'order' ] ) ? $args[ 'order' ] : '';
309 + $_orderby = ! empty( $args['orderby'] ) && $args['orderby'] != 1 ? $args['orderby'] : 'name';
310 + $_order = ! empty( $args['order'] ) ? $args['order'] : '';
461 311
462 - if ( 'betterdocs_order' == $_orderby ) {
463 - // Use base meta key - fallback logic will be handled in the terms_clauses filter
464 - $args[ 'meta_key' ] = 'doc_category_order';
465 - $args[ 'orderby' ] = 'meta_value_num';
466 - $args[ 'order' ] = 'ASC';
312 + if ( $_orderby == 'betterdocs_order' ) {
313 + $args['meta_key'] = 'doc_category_order';
314 + $args['orderby'] = 'meta_value_num';
315 + $args['order'] = 'ASC';
467 316 } else {
468 - $args[ 'orderby' ] = $_orderby;
469 - $args[ 'order' ] = $_order;
317 + $args['orderby'] = $_orderby;
318 + $args['order'] = $_order;
470 319 }
471 320
472 321 // Nested Sub Category
473 322 // $args['parent'] = 0;
@@ -473,166 +322,39 @@
473 322 // $args['parent'] = 0;
474 323 // if ( $nested_subcategory == true ) {
475 324 // }
476 325
477 - if ( ! isset( $args[ 'number' ] ) ) {
326 + if ( ! isset( $args['number'] ) ) {
478 327 global $wp_query;
479 - if ( null === $wp_query->query || ( isset( $wp_query->query[ 'post_type' ] ) && 'docs' != $wp_query->query[ 'post_type' ] ) ) {
480 - $args[ 'number' ] = 4;
328 + if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
329 + $args['number'] = 4;
481 330 }
482 331 }
483 332
484 333 // Includes
485 - if ( isset( $args[ 'include' ] ) ) {
486 - $_include = ! is_array( $args[ 'include' ] ) ? explode( ',', $args[ 'include' ] ) : $args[ 'include' ];
487 - $args[ 'include' ] = $_include;
488 - $args[ 'orderby' ] = 'include';
334 + if ( isset( $args['include'] ) ) {
335 + $_include = ! is_array( $args['include'] ) ? explode( ',', $args['include'] ) : $args['include'];
336 + $args['include'] = $_include;
337 + $args['orderby'] = 'include';
489 338
490 - unset( $args[ 'parent' ] );
339 + unset( $args['parent'] );
491 340 }
492 341
493 - $_meta_query = ! empty( $args[ 'meta_query' ] ) ? $args[ 'meta_query' ] : array();
494 - $args[ 'meta_query' ] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $_meta_query, $args );
342 + $_meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
343 + $args['meta_query'] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $_meta_query, $args );
495 344
496 345 return apply_filters( 'betterdocs_category_terms_object', wp_parse_args( $args, $_default_args ), $args );
497 346 }
498 347
499 348 public function get_terms( $args ) {
500 - $parsed_args = $this->parse_terms_args( $args );
501 - $terms = get_terms( $parsed_args );
502 -
503 - // Filter terms manually if we need to consider private docs the current user may read
504 - if ( isset( $parsed_args[ '_betterdocs_filter_private' ] ) && $parsed_args[ '_betterdocs_filter_private' ] && is_user_logged_in() ) {
505 - $terms = array_filter( $terms, function ( $term ) {
506 - // Get the actual count including private docs for users with read_private_docs capability
507 - return $this->get_private_inclusive_docs_count( $term ) > 0;
508 - } );
509 - }
510 -
511 - return $terms;
349 + return get_terms( $this->parse_terms_args( $args ) );
512 350 }
513 351
514 - /**
515 - * Drop terms with no visible docs after `hide_empty` was disabled for private docs.
516 - *
517 - * modify_terms_args_for_private_docs() turns `hide_empty` off so WordPress stops
518 - * dropping terms whose only docs are private. Without this counterpart, every
519 - * genuinely empty term would leak into the listing for privileged users. Runs only
520 - * for queries carrying the `_betterdocs_filter_private` flag, so anonymous and
521 - * unflagged queries are untouched.
522 - *
523 - * @param array|WP_Error $terms
524 - * @param array|null $taxonomies
525 - * @param array $args
526 - * @return array|WP_Error
527 - */
528 - public function filter_terms_for_private_docs( $terms, $taxonomies, $args ) {
529 - if ( empty( $args[ '_betterdocs_filter_private' ] ) || empty( $terms ) || is_wp_error( $terms ) ) {
530 - return $terms;
531 - }
532 -
533 - if ( ! is_user_logged_in() ) {
534 - return $terms;
535 - }
536 -
537 - // Only term objects can be counted; `ids`, `names`, `count`, ... pass through.
538 - $_fields = isset( $args[ 'fields' ] ) ? $args[ 'fields' ] : 'all';
539 - if ( ! in_array( $_fields, array( 'all', 'all_with_object_id' ), true ) ) {
540 - return $terms;
541 - }
542 -
543 - $_filtered = array_filter( $terms, function ( $term ) {
544 - if ( ! is_object( $term ) ) {
545 - return true;
546 - }
547 -
548 - return $this->get_private_inclusive_docs_count( $term ) > 0;
549 - } );
550 -
551 - return array_values( $_filtered );
552 - }
553 -
554 - /**
555 - * Docs count for a term, including private docs when the user may read them.
556 - *
557 - * KB terms need object-in-term counting rather than the doc_category count path,
558 - * which BetterDocs Pro provides through the filter below; get_docs_count() is the
559 - * fallback and already counts via get_objects_in_term() for the term's own taxonomy.
560 - *
561 - * @param WP_Term $term
562 - * @return int
563 - */
564 - /**
565 - * Whether the current user may see a doc in a listing.
566 - *
567 - * Public docs are visible to everyone. A private doc is visible to whoever may read
568 - * it — `read_post` maps to the base `read` capability for the doc's own author and
569 - * to `read_private_docs` for everyone else, so owners see their own private docs
570 - * without needing the capability. Other non-public statuses (draft, pending) stay
571 - * out of listings for everyone, as before.
572 - *
573 - * @param int $post_id
574 - * @return bool
575 - */
576 - /**
577 - * Cache-key fragment identifying whose visibility a cached count reflects.
578 - *
579 - * Everyone who can read every private doc sees the same numbers, so they share one
580 - * `priv` bucket; authors differ from each other and get their own. Logged-out
581 - * visitors all share bucket `0`, keeping the anonymous cache as hot as before.
582 - *
583 - * @return string
584 - */
585 - protected function count_cache_viewer_key() {
586 - if ( ! is_user_logged_in() ) {
587 - return '0';
588 - }
589 -
590 - if ( current_user_can( 'read_private_docs' ) ) {
591 - return 'priv';
592 - }
593 -
594 - return 'u' . get_current_user_id();
595 - }
596 -
597 - public function can_read_doc( $post_id ) {
598 - if ( is_post_publicly_viewable( $post_id ) ) {
599 - return true;
600 - }
601 -
602 - return 'private' === get_post_status( $post_id ) && current_user_can( 'read_post', $post_id );
603 - }
604 -
605 - protected function get_private_inclusive_docs_count( $term ) {
606 - /**
607 - * Let add-ons supply their own private-inclusive count for a term.
608 - *
609 - * Passing `null` means "not handled" — implementations that don't recognise the
610 - * term's taxonomy return the value untouched, and we fall back to get_docs_count().
611 - *
612 - * @param int|null $count
613 - * @param WP_Term $term
614 - */
615 - $_count = apply_filters( 'betterdocs_get_term_docs_count_for_private_filter', null, $term );
616 -
617 - if ( null !== $_count ) {
618 - return (int) $_count;
619 - }
620 -
621 - // Count nested sub-category docs too, so a parent whose docs live only in its
622 - // children stays visible to logged-in users exactly as it does for anonymous
623 - // visitors (whose grid is descendant-aware). get_docs_count( …, true ) already
624 - // filters each descendant doc through can_read_doc() via get_doc_ids_by_term(),
625 - // so private/unreadable docs never resurrect a term. A non-nested count here
626 - // hid those parents from logged-in users only.
627 - return (int) $this->get_docs_count( $term, true );
628 - }
629 -
630 352 public function get_child_terms( $args ) {
631 - if ( ! isset( $args[ 'number' ] ) ) {
353 + if ( ! isset( $args['number'] ) ) {
632 354 global $wp_query;
633 - if ( null === $wp_query->query || ( isset( $wp_query->query[ 'post_type' ] ) && 'docs' != $wp_query->query[ 'post_type' ] ) ) {
634 - $args[ 'number' ] = 1;
355 + if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
356 + $args['number'] = 1;
635 357 }
636 358 }
637 359
638 360 return $this->get_terms( $args );
@@ -655,9 +377,9 @@
655 377 public function get_taxonomy( $tax = '' ) {
656 378 global $wp_query;
657 379 if ( is_tax( 'knowledge_base' ) ) {
658 380 $_tax = $wp_query->tax_query->queried_terms;
659 - if ( array_key_exists( 'doc_category', $_tax ) ) {
381 + if ( array_key_exists( "doc_category", $_tax ) ) {
660 382 $tax = 'doc_category';
661 383 } else {
662 384 $tax = 'knowledge_base';
663 385 }
@@ -667,17 +389,17 @@
667 389
668 390 return $tax;
669 391 }
670 392
671 - public function terms_query( $args = array() ) {
393 + public function terms_query( $args = [] ) {
672 394 global $wp_query;
673 395 $_origin_args = $args;
674 396
675 - $default_args = array(
397 + $default_args = [
676 398 'hide_empty' => true,
677 - 'taxonomy' => 'doc_category',
678 - 'orderby' => 'name'
679 - );
399 + 'taxonomy' => 'doc_category',
400 + 'orderby' => 'name'
401 + ];
680 402
681 403 /**
682 404 * Number Set
683 405 *
@@ -689,11 +411,11 @@
689 411
690 412 /**
691 413 * Nested Sub Category
692 414 */
693 - if ( isset( $args[ 'nested_subcategory' ] ) && true == $args[ 'nested_subcategory' ] ) {
694 - $default_args[ 'parent' ] = 0;
695 - unset( $args[ 'nested_subcategory' ] );
415 + if ( isset( $args['nested_subcategory'] ) && $args['nested_subcategory'] == true ) {
416 + $default_args['parent'] = 0;
417 + unset( $args['nested_subcategory'] );
696 418 // if ( $wp_query->query === NULL || ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] != 'docs' ) ) {
697 419 // $default_args['number'] = 4;
698 420 // }
699 421 }
@@ -700,32 +422,27 @@
700 422
701 423 /**
702 424 * OrderBy and Order
703 425 */
704 - if ( ! isset( $args[ 'orderby' ] ) ) {
426 + if ( ! isset( $args['orderby'] ) ) {
705 427 $_orderby = $this->settings->get( 'terms_orderby', 'name' );
706 428 $_order = $this->settings->get( 'terms_order', '' );
707 429 } else {
708 - $_orderby = $args[ 'orderby' ];
709 - $_order = ! empty( $args[ 'order' ] ) ? $args[ 'order' ] : '';
430 + $_orderby = $args['orderby'];
431 + $_order = ! empty( $args['order'] ) ? $args['order'] : '';
710 432 }
711 433
712 434 if ( 'betterdocs_order' === $_orderby ) {
713 - // Use different meta keys for different taxonomies
714 - if ( isset( $args[ 'taxonomy' ] ) && 'knowledge_base' === $args[ 'taxonomy' ] ) {
715 - $default_args[ 'meta_key' ] = 'kb_order';
716 - } else {
717 - $default_args[ 'meta_key' ] = 'doc_category_order';
718 - }
719 - $_orderby = 'meta_value_num';
720 - $_order = 'ASC';
721 - } elseif ( true === $_orderby ) {
435 + $default_args['meta_key'] = 'doc_category_order';
436 + $_orderby = 'meta_value_num';
437 + $_order = 'ASC';
438 + } elseif ( $_orderby === true ) {
722 439 $_orderby = 'name';
723 440 }
724 441
725 - $args[ 'orderby' ] = $_orderby;
726 - if ( ! empty( $_order ) ) {
727 - $args[ 'order' ] = $_order;
442 + $args['orderby'] = $_orderby;
443 + if( ! empty( $_order ) ) {
444 + $args['order'] = $_order;
728 445 }
729 446
730 447 /**
731 448 * @todo old hook
@@ -732,170 +449,84 @@
732 449 * hook: betterdocs_child_taxonomy_meta_query
733 450 */
734 451 $_multiple_kb = apply_filters(
735 452 'betterdocs_query_args_multiple_kb_enabled',
736 - isset( $args[ 'multiple_kb' ] ) ? (bool) $args[ 'multiple_kb' ] : false,
453 + isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
737 454 $_origin_args
738 455 );
739 456
740 - $_kb_slug = isset( $args[ 'kb_slug' ] ) ? trim( $args[ 'kb_slug' ] ) : '';
457 + $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
741 458
742 - unset( $args[ 'multiple_kb' ] );
743 - unset( $args[ 'kb_slug' ] );
459 + unset( $args['multiple_kb'] );
460 + unset( $args['kb_slug'] );
744 461
745 - $meta_query = ! empty( $args[ 'meta_query' ] ) ? $args[ 'meta_query' ] : array();
462 + $meta_query = ! empty( $args['meta_query'] ) ? $args['meta_query'] : [];
746 463 $meta_query = apply_filters( 'betterdocs_terms_meta_query_args', $meta_query, $_multiple_kb, $_kb_slug, $_origin_args );
747 464
748 465 if ( ! empty( $meta_query ) ) {
749 - $default_args[ 'meta_query' ] = $meta_query;
466 + $default_args['meta_query'] = $meta_query;
750 467 }
751 468
752 - if ( ! empty( $args[ 'terms' ] ) ) {
753 - $args[ 'include' ] = explode( ',', $args[ 'terms' ] );
754 - $args[ 'orderby' ] = 'include';
755 - $args[ 'order' ] = 'ASC';
469 + if ( ! empty( $args['terms'] ) ) {
470 + $args['include'] = explode( ',', $args['terms'] );
471 + $args['orderby'] = 'include';
472 + $args['order'] = 'ASC';
756 473
757 - unset( $default_args[ 'parent' ] );
758 - unset( $args[ 'parent' ] );
759 - unset( $args[ 'terms' ] );
474 + unset( $default_args['parent'] );
475 + unset( $args['parent'] );
476 + unset( $args['terms'] );
760 477 }
761 478
762 479 $_query_args = wp_parse_args( $args, $default_args );
763 - $_query_args = apply_filters( 'betterdocs_terms_query_args', $_query_args, $_origin_args );
764 -
765 - // Apply private docs logic for logged-in users
766 - $_query_args = $this->modify_terms_args_for_private_docs( $_query_args );
767 -
768 - return $_query_args;
480 + return apply_filters( 'betterdocs_terms_query_args', $_query_args, $_origin_args );
769 481 }
770 482
771 - public function get_term_parents( $term_id, $taxonomy = 'doc_category', $args = array() ) {
483 + public function get_term_parents( $term_id, $taxonomy = 'doc_category', $args = [] ) {
772 484 $term = get_term( $term_id, $taxonomy );
773 485 if ( is_wp_error( $term ) ) {
774 486 return $term;
775 487 }
776 488
777 - if ( ! $term || ! isset( $term->term_id ) ) {
778 - return array();
489 + if ( ! $term ) {
490 + return [];
779 491 }
780 492
781 - $_lists = array();
493 + $_lists = [];
782 494 $origin_term = $term_id;
783 495 $term_id = $term->term_id;
784 496
785 - $defaults = array(
786 - 'format' => 'name',
497 + $defaults = [
498 + 'format' => 'name',
787 499 'inclusive' => true
788 - );
500 + ];
789 501
790 502 $args = wp_parse_args( $args, $defaults );
791 503
792 - $args[ 'inclusive' ] = wp_validate_boolean( $args[ 'inclusive' ] );
504 + $args['inclusive'] = wp_validate_boolean( $args['inclusive'] );
793 505
794 506 $parents = get_ancestors( $term_id, $taxonomy );
795 507
796 - if ( $args[ 'inclusive' ] ) {
508 + if ( $args['inclusive'] ) {
797 509 array_unshift( $parents, $term_id );
798 510 }
799 511
800 512 foreach ( array_reverse( $parents ) as $term_id ) {
801 513 $parent = get_term( $term_id, $taxonomy );
802 - $name = ( 'slug' === $args[ 'format' ] ) ? $parent->slug : $parent->name;
514 + $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;
803 515 $term_permalink = get_term_link( $parent->term_id, $taxonomy );
804 516 $term_permalink = apply_filters( 'betterdocs_breadcrumb_term_permalink', $term_permalink, $term_id );
805 517
806 - $_item = array(
807 - 'url' => $term_permalink,
518 + $_item = [
519 + 'url' => $term_permalink,
808 520 'text' => $name
809 - );
521 + ];
810 522
811 - $_lists[ ] = $_item;
523 + $_lists[] = $_item;
812 524 }
813 525
814 526 return apply_filters( 'betterdocs_breadcrumb_archive_lists', $_lists, $origin_term );
815 527 }
816 528
817 - /**
818 - * Get all non-empty child term IDs recursively for a given taxonomy and parent term.
819 - *
820 - * This function retrieves all child terms for a specified taxonomy and parent term,
821 - * recursively fetching child terms of child terms, and returns only those with a non-zero post count.
822 - *
823 - * @param string $taxonomy The taxonomy name (e.g., 'doc_category').
824 - * @param int $parent_id The ID of the parent term to start retrieving children from.
825 - *
826 - * @return array An array of non-empty child term IDs.
827 - */
828 - public function get_all_child_term_ids( $taxonomy, $parent_id ) {
829 - $version = $this->database->get_cache_version( 'betterdocs_term_counts' );
830 - $cache_key = "bd_term_counts_v{$version}_child_ids_{$taxonomy}_{$parent_id}";
831 -
832 - $cached = wp_cache_get( $cache_key, 'betterdocs' );
833 - if ( false !== $cached ) {
834 - return $cached;
835 - }
836 -
837 - // get_terms( child_of => X ) walks the full descendant tree using WP's
838 - // internally cached term hierarchy, replacing the previous recursive
839 - // get_term()-in-a-loop pattern with one call.
840 - $args = apply_filters(
841 - 'betterdocs_get_child_term_ids_args',
842 - array(
843 - 'taxonomy' => $taxonomy,
844 - 'child_of' => $parent_id,
845 - 'hide_empty' => true,
846 - 'fields' => 'ids',
847 - )
848 - );
849 -
850 - $ids = get_terms( $args );
851 - $ids = ( is_wp_error( $ids ) || ! is_array( $ids ) ) ? array() : array_map( 'intval', $ids );
852 -
853 - wp_cache_set( $cache_key, $ids, 'betterdocs', HOUR_IN_SECONDS * 6 );
854 -
855 - return $ids;
856 - }
857 -
858 - /**
859 - * Get all nested child term IDs of a specific parent term in a taxonomy and return as a comma-separated string.
860 - *
861 - * @param string $taxonomy The taxonomy.
862 - * @param int $parent_id The parent term ID.
863 - * @return string The comma-separated term IDs.
864 - */
865 - public function get_child_term_ids_by_parent_id( $taxonomy, $parent_id ) {
866 - $term_ids = $this->get_all_child_term_ids( $taxonomy, $parent_id );
867 - return implode( ',', $term_ids );
868 - }
869 -
870 - public function get_terms_children( $taxonomy, $parent_id ) {
871 - $children = get_term_children( $parent_id, $taxonomy );
872 - $non_empty_children = array();
873 - if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
874 - foreach ( $children as $child_id ) {
875 - $child_term = get_term( $child_id, $taxonomy );
876 -
877 - // Only include non-empty terms
878 - if ( $child_term && $child_term->count > 0 ) {
879 - $non_empty_children[ ] = $child_term;
880 - }
881 - }
882 - }
883 -
884 - return $non_empty_children;
885 - }
886 -
887 - public function get_terms_children_ids( $taxonomy, $parent_id ) {
888 - $children = $this->get_terms_children( $taxonomy, $parent_id );
889 - $term_ids = wp_list_pluck( $children, 'term_id' );
890 -
891 - return implode( ',', $term_ids );
892 - }
893 -
894 - public function count_terms_children( $taxonomy, $parent_id ) {
895 - return count( $this->get_terms_children( $taxonomy, $parent_id ) );
896 - }
897 -
898 529 public function is_inner_templates() {
899 530 if ( is_tax( 'knowledge_base' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
900 531 return true;
901 532 }
@@ -907,47 +538,47 @@
907 538 * @param mixed $args
908 539 * @throws \Exception
909 540 * @return mixed
910 541 */
911 - public function docs_query_args( $args, $filter = array() ) {
542 + public function docs_query_args( $args, $filter = [] ) {
912 543 $_origin_args = $args;
913 544
914 - $default_args = array(
545 + $default_args = [
915 546 'post_type' => 'docs'
916 - );
547 + ];
917 548
918 - if ( ! empty( $args[ 'post_type' ] ) && trim( $args[ 'post_type' ] ) === 'docs_any' ) {
919 - $default_args[ 'post_type' ] = 'docs';
920 - $default_args[ 'post_status' ] = 'any';
549 + if ( ! empty( $args['post_type'] ) && trim( $args['post_type'] ) === 'docs_any' ) {
550 + $default_args['post_type'] = 'docs';
551 + $default_args['post_status'] = 'any';
921 552
922 - unset( $args[ 'post_type' ] );
553 + unset( $args['post_type'] );
923 554 }
924 555
925 556 /**
926 557 * OrderBy and Order
927 558 */
928 - if ( ! isset( $args[ 'orderby' ] ) ) {
559 + if ( ! isset( $args['orderby'] ) ) {
929 560 $_orderby = $this->settings->get( 'alphabetically_order_post' );
930 561 $_order = $this->settings->get( 'docs_order', 'ASC' );
931 562 } else {
932 - $_orderby = $args[ 'orderby' ];
933 - $_order = ! empty( $args[ 'order' ] ) ? $args[ 'order' ] : 'ASC';
563 + $_orderby = $args['orderby'];
564 + $_order = ! empty( $args['order'] ) ? $args['order'] : 'ASC';
934 565 }
935 566
936 567 if ( 'betterdocs_order' != $_orderby ) {
937 - if ( true === $_orderby ) {
938 - $args[ 'orderby' ] = 'title';
568 + if ( $_orderby === true ) {
569 + $args['orderby'] = 'title';
939 570 } else {
940 - $args[ 'orderby' ] = $_orderby;
571 + $args['orderby'] = $_orderby;
941 572 }
942 573
943 - $args[ 'order' ] = $_order;
574 + $args['order'] = $_order;
944 575 } elseif ( 'betterdocs_order' == $_orderby ) {
945 - unset( $args[ 'orderby' ] );
576 + unset( $args['orderby'] );
946 577 }
947 578
948 - if ( empty( $args[ 'orderby' ] ) ) {
949 - unset( $args[ 'order' ] );
579 + if ( empty( $args['orderby'] ) ) {
580 + unset( $args['order'] );
950 581 }
951 582
952 583 /**
953 584 * Term ID
@@ -952,11 +583,11 @@
952 583 /**
953 584 * Term ID
954 585 */
955 586 $_term_id = null;
956 - if ( ! empty( $args[ 'term_id' ] ) ) {
957 - $_term_id = intval( $args[ 'term_id' ] );
958 - unset( $args[ 'term_id' ] );
587 + if ( ! empty( $args['term_id'] ) ) {
588 + $_term_id = intval( $args['term_id'] );
589 + unset( $args['term_id'] );
959 590 }
960 591
961 592 // if ( $_term_id == null ) {
962 593 // throw new \Exception( __( '$args["term_id"] cannot be null.', 'betterdocs' ) );
@@ -965,11 +596,11 @@
965 596 /**
966 597 * Term Slug for tax_query
967 598 */
968 599 $_term_slug = '';
969 - if ( ! empty( $args[ 'term_slug' ] ) ) {
970 - $_term_slug = trim( $args[ 'term_slug' ] );
971 - unset( $args[ 'term_slug' ] );
600 + if ( ! empty( $args['term_slug'] ) ) {
601 + $_term_slug = trim( $args['term_slug'] );
602 + unset( $args['term_slug'] );
972 603 }
973 604
974 605 /**
975 606 * @todo old hook
@@ -977,44 +608,36 @@
977 608 */
978 609
979 610 $_multiple_kb = apply_filters(
980 611 'betterdocs_enable_multiple_knowledge_base',
981 - isset( $args[ 'multiple_kb' ] ) ? (bool) $args[ 'multiple_kb' ] : false,
612 + isset( $args['multiple_kb'] ) ? (bool) $args['multiple_kb'] : false,
982 613 $_origin_args
983 614 );
984 615
985 - $_kb_slug = isset( $args[ 'kb_slug' ] ) ? trim( $args[ 'kb_slug' ] ) : '';
616 + $_kb_slug = isset( $args['kb_slug'] ) ? trim( $args['kb_slug'] ) : '';
986 617
987 - unset( $args[ 'multiple_kb' ] );
988 - unset( $args[ 'kb_slug' ] );
618 + unset( $args['multiple_kb'] );
619 + unset( $args['kb_slug'] );
989 620
990 - $tax_query = array(
991 - array(
992 - 'taxonomy' => 'doc_category',
993 - 'field' => 'slug',
994 - 'terms' => $_term_slug,
995 - 'operator' => 'AND',
621 + $tax_query = [
622 + [
623 + 'taxonomy' => 'doc_category',
624 + 'field' => 'slug',
625 + 'terms' => $_term_slug,
626 + 'operator' => 'AND',
996 627 'include_children' => false
997 - )
998 - );
628 + ]
629 + ];
999 630
1000 - if ( ! isset( $args[ 'orderby' ] ) || 'betterdocs_order' == $args[ 'orderby' ] ) {
1001 - $args[ 'orderby' ] = 'post__in';
1002 - $args[ 'post__in' ] = $this->get_docs_order_by_terms( $_term_id );
631 + if ( isset( $args['tax_query'] ) ) {
632 + $tax_query = $args['tax_query'];
1003 633 }
1004 634
1005 - if ( isset( $args[ 'tax_query' ] ) ) {
1006 - $tax_query = $args[ 'tax_query' ];
1007 - }
1008 635
1009 - $args[ 'tax_query' ] = apply_filters(
636 +
637 + $args['tax_query'] = apply_filters(
1010 638 'betterdocs_docs_tax_query_args',
1011 - $tax_query,
1012 - $_multiple_kb,
1013 - $_term_slug,
1014 - $_kb_slug,
1015 - $_origin_args,
1016 - $this->is_inner_templates()
639 + $tax_query, $_multiple_kb, $_term_slug, $_kb_slug, $_origin_args, $this->is_inner_templates()
1017 640 );
1018 641 /**
1019 642 * Final parse args
1020 643 */
@@ -1021,157 +644,68 @@
1021 644 $args = wp_parse_args( $args, $default_args );
1022 645
1023 646 if ( ! empty( $filter ) ) {
1024 647 $filter = array_flip( $filter );
1025 - $args = array_filter(
1026 - $args,
1027 - function ( $item ) use ( $filter ) {
1028 - return ! array_key_exists( $item, $filter );
1029 - },
1030 - ARRAY_FILTER_USE_KEY
1031 - );
648 + $args = array_filter( $args, function ( $item ) use ( $filter ) {
649 + return ! array_key_exists( $item, $filter );
650 + }, ARRAY_FILTER_USE_KEY );
1032 651 }
1033 652
1034 - $final_args = apply_filters( 'betterdocs_articles_args', $args, $_term_id, $_origin_args );
1035 -
1036 - // Process betterdocs_order AFTER all filters have run
1037 - if ( isset( $final_args[ 'orderby' ] ) && 'betterdocs_order' == $final_args[ 'orderby' ] ) {
1038 - $docs_order = $this->get_docs_order_by_terms( $_term_id );
1039 -
1040 - if ( ! empty( $docs_order ) ) {
1041 - $final_args[ 'orderby' ] = 'post__in';
1042 - $final_args[ 'post__in' ] = $docs_order;
1043 - } else {
1044 - $final_args[ 'orderby' ] = 'menu_order';
1045 - $final_args[ 'order' ] = 'ASC';
1046 - }
1047 - }
1048 -
1049 - return $final_args;
653 + return apply_filters( 'betterdocs_articles_args', $args, $_term_id, $_origin_args );
1050 654 }
1051 655
1052 - /**
1053 - * The front-end FAQ ordering preference, set via the FAQ Builder header
1054 - * dropdown (the `.betterdocs-dropdown-select` control, saved to the
1055 - * `betterdocs_faq_order` option). The front end mirrors the builder so it
1056 - * shows FAQ groups (and the FAQs inside them) in the same order the admin
1057 - * sees while building.
1058 - *
1059 - * @return string One of: default, most_recent, least_recent, a_to_z, z_to_a, most_questions.
1060 - */
1061 - public function get_faq_order_key() {
1062 - $key = get_option( 'betterdocs_faq_order', 'default' );
1063 - $allowed = array( 'default', 'most_recent', 'least_recent', 'a_to_z', 'z_to_a', 'most_questions' );
1064 -
1065 - if ( ! in_array( $key, $allowed, true ) ) {
1066 - $key = 'default';
1067 - }
1068 -
1069 - return apply_filters( 'betterdocs_faq_order_key', $key );
1070 - }
1071 -
1072 - /**
1073 - * Translate the FAQ order preference into `get_terms()` order clauses for
1074 - * the FAQ group list. Mirrors the admin builder's ORDER_MAP so the front
1075 - * end matches what's configured there.
1076 - *
1077 - * @return array orderby/order (+ meta_key for the manual `default` order).
1078 - */
1079 - public function faq_terms_order_clause() {
1080 - switch ( $this->get_faq_order_key() ) {
1081 - case 'most_recent':
1082 - return array( 'orderby' => 'term_id', 'order' => 'DESC' );
1083 - case 'least_recent':
1084 - return array( 'orderby' => 'term_id', 'order' => 'ASC' );
1085 - case 'a_to_z':
1086 - return array( 'orderby' => 'name', 'order' => 'ASC' );
1087 - case 'z_to_a':
1088 - return array( 'orderby' => 'name', 'order' => 'DESC' );
1089 - case 'most_questions':
1090 - return array( 'orderby' => 'count', 'order' => 'DESC' );
1091 - case 'default':
1092 - default:
1093 - // Manual drag-drop order stored in the `order` term meta.
1094 - return array( 'meta_key' => 'order', 'orderby' => 'meta_value_num', 'order' => 'ASC' );
1095 - }
1096 - }
1097 -
1098 - public function faq_terms_query_args( $includes = '', $excludes = '', $args = array(), $taxonomy = 'betterdocs_faq_category' ) {
1099 - $_args = array(
1100 - 'taxonomy' => $taxonomy,
1101 - 'include' => $includes,
1102 - 'exclude' => $excludes,
1103 - 'meta_query' => array(
1104 - array(
1105 - 'key' => 'status',
1106 - 'value' => 1,
656 + public function faq_terms_query_args( $includes = '', $excludes = '', $args = [] ) {
657 + $_args = [
658 + 'taxonomy' => 'betterdocs_faq_category',
659 + 'meta_key' => 'order',
660 + 'orderby' => 'meta_value_num',
661 + 'order' => 'ASC',
662 + 'include' => $includes,
663 + 'exclude' => $excludes,
664 + 'meta_query' => [
665 + [
666 + 'key' => 'status',
667 + 'value' => 1,
1107 668 'compare' => '=='
1108 - )
1109 - )
1110 - );
669 + ]
670 + ]
671 + ];
1111 672
1112 - $_args = array_merge( $_args, $this->faq_terms_order_clause() );
1113 -
1114 - if ( 'all' == $_args[ 'include' ] ) {
1115 - unset( $_args[ 'include' ] );
1116 - unset( $_args[ 'exclude' ] );
673 + if ( $_args['include'] == 'all' ) {
674 + unset( $_args['include'] );
675 + unset( $_args['exclude'] );
1117 676 }
1118 677
1119 - if ( empty( $_args[ 'exclude' ] ) ) {
1120 - unset( $_args[ 'exclude' ] );
678 + if ( empty( $_args['exclude'] ) ) {
679 + unset( $_args['exclude'] );
1121 680 }
1122 681
1123 - if ( empty( $_args[ 'include' ] ) ) {
1124 - unset( $_args[ 'include' ] );
682 + if ( empty( $_args['include'] ) ) {
683 + unset( $_args['include'] );
1125 684 }
1126 685
1127 686 return wp_parse_args( $args, $_args );
1128 687 }
1129 688
1130 - public function get_faq_by_term( $term_id, $taxonomy = 'betterdocs_faq_category' ) {
689 + public function get_faq_by_term( $term_id ) {
1131 690 global $wpdb;
1132 691
1133 - $args = array(
1134 - 'post_type' => 'betterdocs_faq',
1135 - 'post_status' => 'publish',
1136 - 'tax_query' => array(
1137 - array(
1138 - 'taxonomy' => $taxonomy,
1139 - 'field' => 'term_id',
1140 - 'terms' => $term_id,
692 + $args = [
693 + 'post_type' => 'betterdocs_faq',
694 + 'post_status' => 'publish',
695 + 'tax_query' => [
696 + [
697 + 'taxonomy' => 'betterdocs_faq_category',
698 + 'field' => 'term_id',
699 + 'terms' => $term_id,
1141 700 'operator' => 'AND'
1142 - )
1143 - ),
701 + ]
702 + ],
1144 703 'posts_per_page' => -1
1145 - );
704 + ];
1146 705
1147 - // Order the FAQs inside the group to mirror the FAQ Builder header
1148 - // dropdown. `default`/`most_questions` keep the manual drag-drop order
1149 - // (the others sort live, just like the admin builder does).
1150 - switch ( $this->get_faq_order_key() ) {
1151 - case 'most_recent':
1152 - $args[ 'orderby' ] = 'ID';
1153 - $args[ 'order' ] = 'DESC';
1154 - break;
1155 - case 'least_recent':
1156 - $args[ 'orderby' ] = 'ID';
1157 - $args[ 'order' ] = 'ASC';
1158 - break;
1159 - case 'a_to_z':
1160 - $args[ 'orderby' ] = 'title';
1161 - $args[ 'order' ] = 'ASC';
1162 - break;
1163 - case 'z_to_a':
1164 - $args[ 'orderby' ] = 'title';
1165 - $args[ 'order' ] = 'DESC';
1166 - break;
1167 - case 'default':
1168 - case 'most_questions':
1169 - default:
1170 - $args[ 'orderby' ] = 'post__in';
1171 - $args[ 'post__in' ] = $this->get_faq_orders( $term_id );
1172 - break;
1173 - }
706 + $args['orderby'] = 'post__in';
707 + $args['post__in'] = $this->get_faq_orders( $term_id );
1174 708
1175 709 return new WP_Query( $args );
1176 710 }
1177 711
@@ -1180,34 +714,27 @@
1180 714 $faq_order = get_term_meta( $term_id, '_betterdocs_faq_order', true );
1181 715 $faq_order = explode( ',', $faq_order );
1182 716
1183 717 $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}term_relationships WHERE term_taxonomy_id = %d", $term_id );
1184 - $query_key = 'betterdocs_faq_order_' . md5( $query );
718 + $query_key = "betterdocs_faq_order_" . md5( $query );
1185 719
1186 - if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
720 + if ( ( $results = $this->database->get_cache( $query_key ) ) !== false ) {
1187 721 return $results;
1188 722 }
1189 723
1190 724 if ( ! empty( $faq_order ) ) {
1191 - $new_ids = array();
1192 - // $query is prepared upstream; results are cached via $this->database->get_cache above (line 980).
1193 - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
725 + $new_ids = [];
726 + $results = $wpdb->get_results( $query );
1194 727
1195 728 if ( ! is_null( $results ) && ! empty( $results ) && is_array( $results ) ) {
1196 - $object_ids = array_filter(
1197 - $results,
1198 - function ( $value ) use ( $faq_order ) {
1199 - return ! in_array( $value->object_id, $faq_order );
1200 - }
1201 - );
729 + $object_ids = array_filter( $results, function ( $value ) use ( $faq_order ) {
730 + return ! in_array( $value->object_id, $faq_order );
731 + } );
1202 732
1203 733 if ( ! empty( $object_ids ) ) {
1204 - array_walk(
1205 - $object_ids,
1206 - function ( $value ) use ( &$new_ids ) {
1207 - $new_ids[ ] = $value->object_id;
1208 - }
1209 - );
734 + array_walk( $object_ids, function ( $value ) use ( &$new_ids ) {
735 + $new_ids[] = $value->object_id;
736 + } );
1210 737 }
1211 738 }
1212 739
1213 740 $faq_order = array_merge( $new_ids, $faq_order );
@@ -1217,28 +744,26 @@
1217 744
1218 745 return $faq_order;
1219 746 }
1220 747
1221 - public function get_faq_terms( $terms = array(), $taxonomy = 'betterdocs_faq_category' ) {
1222 - $_terms = get_terms(
1223 - array(
1224 - 'taxonomy' => $taxonomy,
1225 - 'hide_empty' => true,
1226 - 'orderby' => 'name',
1227 - 'order' => 'ASC',
1228 - 'meta_query' => array(
1229 - array(
1230 - 'key' => 'status',
1231 - 'value' => 1,
1232 - 'compare' => '=='
1233 - )
1234 - )
1235 - )
1236 - );
748 + public function get_faq_terms( $terms = [] ) {
749 + $_terms = get_terms( [
750 + 'taxonomy' => 'betterdocs_faq_category',
751 + 'hide_empty' => true,
752 + 'orderby' => 'name',
753 + 'order' => 'ASC',
754 + 'meta_query' => [
755 + [
756 + 'key' => 'status',
757 + 'value' => 1,
758 + 'compare' => '=='
759 + ]
760 + ]
761 + ] );
1237 762
1238 763 if ( ! is_wp_error( $_terms ) ) {
1239 764 foreach ( $_terms as $term ) {
1240 - $terms[ $term->term_id ] = $term->name;
765 + $terms[$term->term_id] = $term->name;
1241 766 }
1242 767 }
1243 768
1244 769 return $terms;
@@ -1243,129 +768,31 @@
1243 768
1244 769 return $terms;
1245 770 }
1246 771
1247 - public function get_doc_terms( $terms = array() ) {
1248 - $_terms = get_terms(
1249 - array(
1250 - 'taxonomy' => 'doc_category',
1251 - 'hide_empty' => true,
1252 - 'orderby' => 'name',
1253 - 'order' => 'ASC'
1254 - )
1255 - );
1256 -
1257 - if ( ! is_wp_error( $_terms ) ) {
1258 - foreach ( $_terms as $term ) {
1259 - $terms[ $term->term_id ] = $term->name;
1260 - }
1261 - }
1262 -
1263 - return $terms;
1264 - }
1265 -
1266 - public function flush_term_counts_cache() {
1267 - $this->database->bump_cache_version( 'betterdocs_term_counts' );
1268 - }
1269 -
1270 - public function flush_term_counts_cache_on_post( $post_id, $post = null ) {
1271 - if ( $post && isset( $post->post_type ) && $post->post_type === 'docs' ) {
1272 - $this->flush_term_counts_cache();
1273 - }
1274 - }
1275 -
1276 - public function flush_term_counts_cache_on_set( $object_id, $terms, $tt_ids, $taxonomy ) {
1277 - if ( $taxonomy === 'doc_category' ) {
1278 - $this->flush_term_counts_cache();
1279 - }
1280 - }
1281 -
1282 - public function get_docs_count( $term, $nested_subcategory = false, $args = array() ) {
1283 - // Validate term object
1284 - if ( ! is_object( $term ) ) {
1285 - return 0;
1286 - }
1287 -
772 + public function get_docs_count( $term, $nested_subcategory = false, $args = [] ) {
1288 773 $counts = isset( $term->count ) ? $term->count : 0;
1289 774
1290 - if ( ! isset( $term->term_id ) || ! is_numeric( $term->term_id ) ) {
775 + if ( $nested_subcategory == false ) {
1291 776 return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
1292 777 }
1293 778
1294 - $version = $this->database->get_cache_version( 'betterdocs_term_counts' );
1295 - // Counts depend on who is asking: an author sees their own private docs, so the
1296 - // key carries the user id. Logged-out visitors all share user 0.
1297 - $viewer = $this->count_cache_viewer_key();
1298 - $kb_slug = isset( $args['kb_slug'] ) ? $args['kb_slug'] : '';
1299 - $multi = ! empty( $args['multiple_knowledge_base'] ) ? 1 : 0;
1300 - $nested = $nested_subcategory ? 1 : 0;
1301 - $cache_key = "bd_term_counts_v{$version}_docs_count_{$term->term_id}_{$nested}_{$viewer}_{$kb_slug}_{$multi}";
1302 -
1303 - $cached = wp_cache_get( $cache_key, 'betterdocs' );
1304 - if ( false !== $cached ) {
1305 - return apply_filters( 'betterdocs_docs_count', $cached, $term, $nested_subcategory, $args );
779 + $_child_terms_docs_ids = $this->get_doc_ids_by_term( $term, null, $nested_subcategory );
780 + if ( is_array( $_child_terms_docs_ids ) ) {
781 + $counts = count( $_child_terms_docs_ids );
1306 782 }
1307 783
1308 - if ( false == $nested_subcategory ) {
1309 - // For non-nested categories, we need to recalculate counts based on user capabilities
1310 - // Only proceed if we have a valid term with required properties
1311 - if ( isset( $term->taxonomy ) ) {
1312 - // Get all post IDs for this term
1313 - $post_ids = get_objects_in_term( $term->term_id, $term->taxonomy );
1314 -
1315 - if ( ! empty( $post_ids ) ) {
1316 - _prime_post_caches( $post_ids, false, false );
1317 -
1318 - // Public docs for everyone, plus any private doc this user may read
1319 - // (its own author, or a `read_private_docs` holder).
1320 - $filtered_post_ids = array_filter( $post_ids, function ( $post_id ) {
1321 - return $this->can_read_doc( $post_id );
1322 - } );
1323 -
1324 - $counts = count( $filtered_post_ids );
1325 - } else {
1326 - $counts = 0;
1327 - }
1328 - }
1329 - } else {
1330 - $_child_terms_docs_ids = $this->get_doc_ids_by_term( $term, null, $nested_subcategory );
1331 - if ( is_array( $_child_terms_docs_ids ) ) {
1332 - $counts = count( $_child_terms_docs_ids );
1333 - }
1334 - }
1335 -
1336 - wp_cache_set( $cache_key, $counts, 'betterdocs', HOUR_IN_SECONDS * 6 );
1337 -
1338 784 return apply_filters( 'betterdocs_docs_count', $counts, $term, $nested_subcategory, $args );
1339 785 }
1340 786
1341 787 public function get_doc_ids_by_term( $term, $optional = null, $nested_subcategory = false ) {
1342 - // Check if term has required properties and is a valid object
1343 - if ( ! is_object( $term ) || ! isset( $term->term_id ) || ! isset( $term->taxonomy ) || ! is_numeric( $term->term_id ) ) {
1344 - return false;
788 + $args = [ 'include' => $term->term_id ];
789 + if( $nested_subcategory ) {
790 + $args['child_of'] = $term->term_id;
791 + unset( $args['include'] );
1345 792 }
793 + $_child_terms = get_terms( $term->taxonomy, $args );
1346 794
1347 - $version = $this->database->get_cache_version( 'betterdocs_term_counts' );
1348 - $viewer = $this->count_cache_viewer_key();
1349 - $nested = $nested_subcategory ? 1 : 0;
1350 - $optional_id = is_object( $optional ) && isset( $optional->term_id ) ? (int) $optional->term_id : 0;
1351 - $cache_key = "bd_term_counts_v{$version}_doc_ids_{$term->term_id}_{$nested}_{$optional_id}_{$viewer}";
1352 -
1353 - $cached = wp_cache_get( $cache_key, 'betterdocs' );
1354 - if ( false !== $cached ) {
1355 - return $cached;
1356 - }
1357 -
1358 - $args = array(
1359 - 'taxonomy' => $term->taxonomy,
1360 - 'include' => $term->term_id,
1361 - );
1362 - if ( $nested_subcategory ) {
1363 - $args[ 'child_of' ] = $term->term_id;
1364 - unset( $args[ 'include' ] );
1365 - }
1366 - $_child_terms = get_terms( $args );
1367 -
1368 795 if ( ! is_array( $_child_terms ) ) {
1369 796 return false;
1370 797 }
1371 798
@@ -1374,261 +801,19 @@
1374 801 $_child_terms_ids = array_column( $_child_terms, 'term_id' );
1375 802 $_child_terms_taxs = array_column( $_child_terms, 'taxonomy' );
1376 803 $_child_terms_docs_ids = get_objects_in_term( $_child_terms_ids, $_child_terms_taxs );
1377 804
1378 - if ( null !== $optional ) {
805 + if ( $optional !== null ) {
1379 806 $_optional_doc_ids = get_objects_in_term( $optional->term_id, $optional->taxonomy );
1380 807 $_child_terms_docs_ids = array_intersect( $_child_terms_docs_ids, $_optional_doc_ids );
1381 808 }
1382 809
1383 - if ( ! empty( $_child_terms_docs_ids ) ) {
1384 - _prime_post_caches( $_child_terms_docs_ids, false, false );
1385 - }
810 + return array_filter( $_child_terms_docs_ids, function ( $doc_id ) {
811 + if ( ! is_user_logged_in() ) {
812 + return is_post_publicly_viewable( $doc_id );
813 + }
1386 814
1387 - $filtered = array_filter( $_child_terms_docs_ids, function ( $doc_id ) {
1388 - return $this->can_read_doc( $doc_id );
815 + $_status = get_post_status( $doc_id );
816 + return $_status == 'private' || is_post_publicly_viewable( $doc_id );
1389 817 } );
1390 -
1391 - wp_cache_set( $cache_key, $filtered, 'betterdocs', HOUR_IN_SECONDS * 6 );
1392 -
1393 - return $filtered;
1394 - }
1395 -
1396 - /**
1397 - * Get the common query arguments for WP_Query.
1398 - *
1399 - * @param string $terms The taxonomy.
1400 - * @param string $term_slug The taxonomy term slug.
1401 - * @param array $additional_args Additional arguments to merge with the common arguments.
1402 - * @return array The query arguments.
1403 - */
1404 - private function tax_query_args( $terms, $term_slug, $additional_args = array() ) {
1405 - $common_args = array(
1406 - 'post_type' => 'docs',
1407 - 'post_status' => 'publish',
1408 - 'tax_query' => array(
1409 - array(
1410 - 'taxonomy' => $terms,
1411 - 'field' => 'slug',
1412 - 'terms' => $term_slug
1413 - )
1414 - )
1415 - );
1416 - return array_merge( $common_args, $additional_args );
1417 - }
1418 -
1419 - /**
1420 - * Get the latest updated date for a specific taxonomy term.
1421 - *
1422 - * @param string $terms The taxonomy.
1423 - * @param string $term_slug The taxonomy term slug.
1424 - * @return string|null The latest modified date or null if no posts found.
1425 - */
1426 - public function latest_updated_date( $terms, $term_slug ) {
1427 - $args = $this->tax_query_args(
1428 - $terms,
1429 - $term_slug,
1430 - array(
1431 - 'posts_per_page' => 1,
1432 - 'orderby' => 'modified',
1433 - 'order' => 'DESC'
1434 - )
1435 - );
1436 -
1437 - $query = new WP_Query( $args );
1438 -
1439 - if ( $query->have_posts() ) {
1440 - while ( $query->have_posts() ) {
1441 - $query->the_post();
1442 - $latest_post = get_the_modified_date();
1443 - wp_reset_postdata();
1444 - return $latest_post;
1445 - }
1446 - } else {
1447 - return null;
1448 - }
1449 - }
1450 -
1451 - /**
1452 - * Check if there are any new posts within the last 7 days for a specific taxonomy term.
1453 - *
1454 - * @param string $terms The taxonomy.
1455 - * @param string $term_slug The taxonomy term slug.
1456 - * @return bool True if there are new posts, false otherwise.
1457 - */
1458 - public function check_new_posts( $terms, $term_slug ) {
1459 - $date_7_days_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-7 days' ) );
1460 -
1461 - $args = $this->tax_query_args(
1462 - $terms,
1463 - $term_slug,
1464 - array(
1465 - 'posts_per_page' => 1,
1466 - 'orderby' => 'modified',
1467 - 'order' => 'DESC',
1468 - 'date_query' => array(
1469 - array(
1470 - 'after' => $date_7_days_ago,
1471 - 'inclusive' => true
1472 - )
1473 - )
1474 - )
1475 - );
1476 -
1477 - $query = new WP_Query( $args );
1478 -
1479 - $has_new_posts = $query->have_posts();
1480 -
1481 - wp_reset_postdata();
1482 -
1483 - return $has_new_posts;
1484 - }
1485 -
1486 - public function insert_search_keyword( $search_input, $input_not_found ) {
1487 - if ( empty( $search_input ) ) {
1488 - return false;
1489 - }
1490 -
1491 - global $wpdb;
1492 -
1493 - // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- live search-keyword analytics; cache would defeat the purpose.
1494 - // Use BINARY comparison to avoid collation mismatch errors
1495 - // This works across all character sets (latin1, utf8, utf8mb4, etc.)
1496 - $search = $wpdb->get_results(
1497 - $wpdb->prepare(
1498 - "SELECT *
1499 - FROM {$wpdb->prefix}betterdocs_search_keyword
1500 - WHERE BINARY keyword = %s",
1501 - $search_input
1502 - )
1503 - );
1504 -
1505 - if ( ! empty( $search ) ) {
1506 - $search_log = $wpdb->get_results(
1507 - $wpdb->prepare(
1508 - "SELECT *
1509 - FROM {$wpdb->prefix}betterdocs_search_log
1510 - WHERE created_at = %s AND keyword_id = %d",
1511 - gmdate( 'Y-m-d' ),
1512 - $search[ 0 ]->id
1513 - )
1514 - );
1515 -
1516 - if ( ! empty( $search_log ) ) {
1517 - if ( ! empty( $input_not_found ) ) {
1518 - $tbl_field = 'not_found_count';
1519 - $count = $search_log[ 0 ]->not_found_count + 1;
1520 - } else {
1521 - $tbl_field = 'count';
1522 - $count = $search_log[ 0 ]->count + 1;
1523 - }
1524 - // $tbl_field is validated immediately above to be either 'count' or
1525 - // 'not_found_count' — safe to interpolate as a column identifier.
1526 - // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- $tbl_field is an allowlisted column name ('count'|'not_found_count'); safe to interpolate.
1527 - $insert = $wpdb->query(
1528 - $wpdb->prepare(
1529 - "UPDATE {$wpdb->prefix}betterdocs_search_log SET {$tbl_field} = %d WHERE created_at = %s AND keyword_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1530 - $count,
1531 - $search_log[ 0 ]->created_at,
1532 - $search_log[ 0 ]->keyword_id
1533 - )
1534 - );
1535 - } else {
1536 - if ( ! empty( $input_not_found ) ) {
1537 - $count = 0;
1538 - $not_found_count = 1;
1539 - } else {
1540 - $count = 1;
1541 - $not_found_count = 0;
1542 - }
1543 - $insert = $wpdb->query(
1544 - $wpdb->prepare(
1545 - "INSERT INTO {$wpdb->prefix}betterdocs_search_log
1546 - ( keyword_id, count, not_found_count, created_at )
1547 - VALUES ( %d, %d, %d, %s )",
1548 - array(
1549 - $search[ 0 ]->id,
1550 - $count,
1551 - $not_found_count,
1552 - gmdate( 'Y-m-d' )
1553 - )
1554 - )
1555 - );
1556 - }
1557 - } else {
1558 - $insert = $wpdb->query(
1559 - $wpdb->prepare(
1560 - "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword
1561 - ( keyword )
1562 - VALUES ( %s )",
1563 - array(
1564 - $search_input
1565 - )
1566 - )
1567 - );
1568 -
1569 - if ( $insert ) {
1570 - if ( ! empty( $input_not_found ) ) {
1571 - $count = 0;
1572 - $not_found_count = 1;
1573 - } else {
1574 - $count = 1;
1575 - $not_found_count = 0;
1576 - }
1577 - $insert = $wpdb->query(
1578 - $wpdb->prepare(
1579 - "INSERT INTO {$wpdb->prefix}betterdocs_search_log
1580 - ( keyword_id, count, not_found_count, created_at )
1581 - VALUES ( %d, %d, %d, %s )",
1582 - array(
1583 - $wpdb->insert_id,
1584 - $count,
1585 - $not_found_count,
1586 - gmdate( 'Y-m-d' )
1587 - )
1588 - )
1589 - );
1590 - }
1591 - }
1592 - // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
1593 - return $insert;
1594 - }
1595 -
1596 - /**
1597 - * Counts the number of 'doc_category' terms assigned to a specific 'knowledge_base' by slug.
1598 - *
1599 - * This method retrieves all terms in the 'doc_category' taxonomy and checks the term meta
1600 - * 'doc_category_knowledge_base' to determine how many 'doc_category' terms are associated
1601 - * with a given 'knowledge_base' slug.
1602 - *
1603 - * @param string $knowledge_base_slug The slug of the 'knowledge_base' to count the assignments for.
1604 - *
1605 - * @return int The count of 'doc_category' terms assigned to the specified 'knowledge_base'.
1606 - */
1607 - public function count_doc_categories_for_knowledge_base( $knowledge_base_slug ) {
1608 - $count = 0;
1609 -
1610 - $doc_categories = get_terms(
1611 - array(
1612 - 'taxonomy' => 'doc_category',
1613 - 'hide_empty' => true
1614 - )
1615 - );
1616 -
1617 - if ( ! empty( $doc_categories ) && ! is_wp_error( $doc_categories ) ) {
1618 - foreach ( $doc_categories as $term ) {
1619 - $meta_value = get_term_meta( $term->term_id, 'doc_category_knowledge_base', true );
1620 -
1621 - if ( $meta_value ) {
1622 - $knowledge_bases = maybe_unserialize( $meta_value );
1623 -
1624 - // Check if the specific knowledge base slug is present in the meta value array
1625 - if ( is_array( $knowledge_bases ) && in_array( $knowledge_base_slug, $knowledge_bases ) ) {
1626 - ++$count;
1627 - }
1628 - }
1629 - }
1630 - }
1631 -
1632 - return $count;
1633 818 }
1634 819 }