PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 1.3.4
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v1.3.4
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
betterdocs / includes / class-betterdocs-helpers.php

class-betterdocs-helpers.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 1.3.4, at includes/class-betterdocs-helpers.php

410 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This class will provide all kind of helper methods.
4 */
5 class BetterDocs_Helper {
6 /**
7 * This function is responsible for the data sanitization
8 *
9 * @param array $field
10 * @param string|array $value
11 * @return string|array
12 */
13 public static function sanitize_field( $field, $value ) {
14 if ( isset( $field['sanitize'] ) && ! empty( $field['sanitize'] ) ) {
15 if ( function_exists( $field['sanitize'] ) ) {
16 $value = call_user_func( $field['sanitize'], $value );
17 }
18 return $value;
19 }
20
21 if( is_array( $field ) && isset( $field['type'] ) ) {
22 switch ( $field['type'] ) {
23 case 'text':
24 $value = sanitize_text_field( $value );
25 break;
26 case 'textarea':
27 $value = sanitize_textarea_field( $value );
28 break;
29 case 'email':
30 $value = sanitize_email( $value );
31 break;
32 default:
33 return $value;
34 break;
35 }
36 } else {
37 $value = sanitize_text_field( $value );
38 }
39
40 return $value;
41 }
42 /**
43 * This function is responsible for making an array sort by their key
44 * @param array $data
45 * @param string $using
46 * @param string $way
47 * @return array
48 */
49 public static function sorter( $data, $using = 'time_date', $way = 'DESC' ){
50 if( ! is_array( $data ) ) {
51 return $data;
52 }
53 $new_array = [];
54 if( $using === 'key' ) {
55 if( $way !== 'ASC' ) {
56 krsort( $data );
57 } else {
58 ksort( $data );
59 }
60 } else {
61 foreach( $data as $key => $value ) {
62 if( ! is_array( $value ) ) continue;
63 foreach( $value as $inner_key => $single ) {
64 if( $inner_key == $using ) {
65 $value[ 'tempid' ] = $key;
66 $single = self::numeric_key_gen( $new_array, $single );
67 $new_array[ $single ] = $value;
68 }
69 }
70 }
71
72 if( $way !== 'ASC' ) {
73 krsort( $new_array );
74 } else {
75 ksort( $new_array );
76 }
77
78 if( ! empty( $new_array ) ) {
79 foreach( $new_array as $array ) {
80 $index = $array['tempid'];
81 unset( $array['tempid'] );
82 $new_data[ $index ] = $array;
83 }
84 $data = $new_data;
85 }
86 }
87
88 return $data;
89 }
90 /**
91 * This function is responsible for generate unique numeric key for a given array.
92 *
93 * @param array $data
94 * @param integer $index
95 * @return integer
96 */
97 private static function numeric_key_gen( $data, $index = 0 ){
98 if( isset( $data[ $index ] ) ) {
99 $index+=1;
100 return self::numeric_key_gen( $data, $index );
101 }
102 return $index;
103 }
104 /**
105 * Sorting Data
106 * by their type
107 *
108 * @param array $value
109 * @param string $key
110 * @return void
111 */
112 public static function sortBy( &$value, $key = 'comments' ) {
113 switch( $key ){
114 case 'comments' :
115 return self::sorter( $value, 'key', 'DESC' );
116 break;
117 default:
118 return self::sorter( $value, 'timestamp', 'DESC' );
119 break;
120 }
121 }
122 /**
123 * Human Readable Time Diff
124 *
125 * @param boolean $time
126 * @return void
127 */
128 public static function get_timeago_html( $time = false ) {
129 if ( ! $time ) {
130 return;
131 }
132
133 $offset = get_option('gmt_offset'); // Time offset in hours
134 $local_time = $time + ($offset * 60 * 60 ); // added offset in seconds
135 $time = human_time_diff( $local_time, current_time('timestamp') );
136 ob_start();
137 ?>
138 <small><?php echo esc_html__( 'About', 'betterdocs' ) . ' ' . esc_html( $time ) . ' ' . esc_html__( 'ago', 'betterdocs' ) ?></small>
139 <?php
140 $time_ago = ob_get_clean();
141 return $time_ago;
142 }
143 /**
144 * Get all post types
145 *
146 * @param array $exclude
147 * @return void
148 */
149 public static function post_types( $exclude = array() ) {
150 $post_types = get_post_types(array(
151 'public' => true,
152 'show_ui' => true
153 ), 'objects');
154
155 unset( $post_types['attachment'] );
156
157 if ( count( $exclude ) ) {
158 foreach ( $exclude as $type ) {
159 if ( isset( $post_types[$type] ) ) {
160 unset( $post_types[$type] );
161 }
162 }
163 }
164
165 return apply_filters('betterdocs_post_types', $post_types );
166 }
167 /**
168 * Get all taxonomies
169 *
170 * @param string $post_type
171 * @param array $exclude
172 * @return void
173 */
174 public static function taxonomies( $post_type = '', $exclude = array() ) {
175 if ( empty( $post_type ) ) {
176 $taxonomies = get_taxonomies(
177 array(
178 'public' => true,
179 '_builtin' => false
180 ),
181 'objects'
182 );
183 } else {
184 $taxonomies = get_object_taxonomies( $post_type, 'objects' );
185 }
186
187 $data = array();
188 if( is_array( $taxonomies ) ) {
189 foreach ( $taxonomies as $tax_slug => $tax ) {
190 if( ! $tax->public || ! $tax->show_ui ) {
191 continue;
192 }
193 if( in_array( $tax_slug, $exclude ) ) {
194 continue;
195 }
196 $data[$tax_slug] = $tax;
197 }
198 }
199 return apply_filters('betterdocs_loop_taxonomies', $data, $taxonomies, $post_type );
200 }
201
202 public static function list_svg() {
203 $html = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.86em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 1536 1792"><path d="M1468 380q28 28 48 76t20 88v1152q0 40-28 68t-68 28H96q-40 0-68-28t-28-68V96q0-40 28-68T96 0h896q40 0 88 20t76 48zm-444-244v376h376q-10-29-22-41l-313-313q-12-12-41-22zm384 1528V640H992q-40 0-68-28t-28-68V128H128v1536h1280zM384 800q0-14 9-23t23-9h704q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64zm736 224q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h704zm0 256q14 0 23 9t9 23v64q0 14-9 23t-23 9H416q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h704z"/></svg>';
204 return $html;
205 }
206
207 public static function arrow_right_svg() {
208 $html = '<svg class="toggle-arrow arrow-right" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.48em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 608 1280"><g transform="translate(608 0) scale(-1 1)"><path d="M595 288q0 13-10 23L192 704l393 393q10 10 10 23t-10 23l-50 50q-10 10-23 10t-23-10L23 727q-10-10-10-23t10-23l466-466q10-10 23-10t23 10l50 50q10 10 10 23z"/></g></svg>';
209 return $html;
210 }
211
212 public static function arrow_down_svg() {
213 $html = '<svg class="toggle-arrow arrow-down" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" width="0.8em" height="1em" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 1024 1280"><path d="M1011 480q0 13-10 23L535 969q-10 10-23 10t-23-10L23 503q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l393 393l393-393q10-10 23-10t23 10l50 50q10 10 10 23z"/></svg>';
214 return $html;
215 }
216
217 public static function get_tax( $tax = '' ) {
218
219 global $wp_query;
220
221 if ( is_tax( 'knowledge_base' ) ) {
222 $get_tax = $wp_query->tax_query->queried_terms;
223 if (array_key_exists( "doc_category", $get_tax )) {
224 $tax = 'doc_category';
225 } else {
226 $tax = 'knowledge_base';
227 }
228 } elseif ( is_tax( 'doc_category' ) ) {
229 $tax = 'doc_category';
230 }
231
232 return $tax;
233 }
234
235 public static function list_query_arg( $post_type, $multiple_kb, $tax_slug, $posts_per_grid, $alphabetically_order_post ) {
236
237 $args = array(
238 'post_type' => $post_type,
239 'post_status' => 'publish',
240 );
241
242 $tax_query = array(
243 array(
244 'taxonomy' => 'doc_category',
245 'field' => 'slug',
246 'terms' => $tax_slug,
247 'operator' => 'AND',
248 'include_children' => false
249 ),
250 );
251
252 $args['tax_query'] = apply_filters( 'betterdocs_list_tax_query_arg', $tax_query, $multiple_kb, $tax_slug );
253
254
255 if ( $posts_per_grid ) {
256 $args['posts_per_page'] = $posts_per_grid;
257 }
258
259 if($alphabetically_order_post == 1) {
260 $args['orderby'] = 'title';
261 $args['order'] = 'ASC';
262 }
263
264 return $args;
265 }
266
267 public static function count_kb() {
268 $result = array();
269 $kbs = get_terms( array(
270 'taxonomy' => 'knowledge_base',
271 'hide_empty' => false,
272 'fields' => 'id=>slug'
273 ) );
274
275 $cats = get_terms( array(
276 'taxonomy' => 'doc_category',
277 'hide_empty' => false,
278 'fields' => 'id=>slug'
279 ) );
280
281
282 foreach ($kbs as $kb) {
283
284 foreach ($cats as $cat) {
285
286 $args = array(
287 'post_type' => 'docs',
288 'post_status'=>'publish',
289 'tax_query' => array(
290 'relation' => 'AND',
291 array(
292 'taxonomy' => 'knowledge_base',
293 'field' => 'slug',
294 'terms' => array( $kb ),
295 ),
296 array(
297 'taxonomy' => 'doc_category',
298 'field' => 'slug',
299 'terms' => array( $cat ),
300 ),
301 ),
302 );
303 $query = new WP_Query( $args );
304
305 $result[$kb][$cat] = $query->post_count;
306
307 }
308 }
309
310 return $result;
311
312 }
313
314 public static function count_category( $kb_slug, $cat_slug ) {
315
316 $args = array(
317 'post_type' => 'docs',
318 'post_status' => 'publish',
319 );
320
321 $taxes = array( 'knowledge_base', 'doc_category' );
322 foreach ( $taxes as $tax ) {
323 $terms = get_terms( $tax );
324
325 foreach ( $terms as $term )
326 $tax_map[$tax][$term->slug] = $term->term_taxonomy_id;
327 }
328
329 $args['tax_query'] = array(
330 'relation' => 'AND',
331 array(
332 'taxonomy' => 'knowledge_base',
333 'field' => 'term_taxonomy_id',
334 'terms' => array( $tax_map['knowledge_base'][$kb_slug] ),
335 'operator' => 'IN',
336 'include_children' => false,
337 ),
338 array(
339 'taxonomy' => 'doc_category',
340 'field' => 'term_taxonomy_id',
341 'operator' => 'IN',
342 'terms' => array( $tax_map['doc_category'][$cat_slug] ),
343 'include_children' => false,
344 ),
345 );
346
347 $query = new WP_Query( $args );
348
349 $count = $query->found_posts;
350
351 return $count;
352 }
353
354 public static function taxonomy_object( $multiple_kb, $terms ) {
355
356 $terms_object = array(
357 'hide_empty' => true,
358 'taxonomy' => 'doc_category',
359 'orderby' => 'name',
360 'parent' => 0
361 );
362
363 $meta_query = '';
364 $terms_object['meta_query'] = apply_filters( 'betterdocs_taxonomy_object_meta_query', $meta_query, $multiple_kb );
365
366 if ( $terms ) {
367 unset($terms_object['parent']);
368 }
369
370 if ( $terms ) {
371 $terms_object['include'] = explode(',', $terms );
372 $terms_object['orderby'] = 'include';
373 }
374
375 $taxonomy_objects = get_terms( $terms_object );
376
377 return $taxonomy_objects;
378 }
379
380 public static function child_taxonomy_terms( $term_id, $multiple_kb ) {
381
382 $terms_object = array(
383 'child_of' => $term_id,
384 'orderby' => 'name'
385 );
386
387 $meta_query = '';
388 $terms_object['meta_query'] = apply_filters( 'betterdocs_child_taxonomy_meta_query', $meta_query, $multiple_kb );
389
390 $taxonomy_objects = get_terms( 'doc_category', $terms_object);
391
392 return $taxonomy_objects;
393 }
394
395 public static function term_permalink( $texanomy, $term_slug ) {
396
397 $q_object = get_queried_object();
398
399 $term_permalink = get_term_link( $term_slug, $texanomy );
400 $kb_slug = '';
401 if( $q_object instanceof WP_Term ) {
402 $kb_slug = $q_object->slug;
403 }
404 $term_permalink = str_replace( '%knowledge_base%', $kb_slug, $term_permalink );
405
406 return $term_permalink;
407 }
408
409 }
410