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

445 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This class will provide all kind of helper methods.
5 */
6 class BetterDocs_Helper
7 {
8 /**
9 * This function is responsible for the data sanitization
10 *
11 * @param array $field
12 * @param string|array $value
13 * @return string|array
14 */
15 public static function sanitize_field($field, $value)
16 {
17 if (isset($field['sanitize']) && !empty($field['sanitize'])) {
18 if (function_exists($field['sanitize'])) {
19 $value = call_user_func($field['sanitize'], $value);
20 }
21 return $value;
22 }
23
24 if (is_array($field) && isset($field['type'])) {
25 switch ($field['type']) {
26 case 'text':
27 $value = sanitize_text_field($value);
28 break;
29 case 'textarea':
30 $value = sanitize_textarea_field($value);
31 break;
32 case 'email':
33 $value = sanitize_email($value);
34 break;
35 default:
36 return $value;
37 break;
38 }
39 } else {
40 $value = sanitize_text_field($value);
41 }
42
43 return $value;
44 }
45
46 /**
47 * This function is responsible for making an array sort by their key
48 * @param array $data
49 * @param string $using
50 * @param string $way
51 * @return array
52 */
53 public static function sorter($data, $using = 'time_date', $way = 'DESC')
54 {
55 if (!is_array($data)) {
56 return $data;
57 }
58
59 $new_array = [];
60
61 if ($using === 'key') {
62 if ($way !== 'ASC') {
63 krsort($data);
64 } else {
65 ksort($data);
66 }
67 } else {
68 foreach ($data as $key => $value) {
69 if (!is_array($value)) continue;
70 foreach ($value as $inner_key => $single) {
71 if ($inner_key == $using) {
72 $value['tempid'] = $key;
73 $single = self::numeric_key_gen($new_array, $single);
74 $new_array[$single] = $value;
75 }
76 }
77 }
78
79 if ($way !== 'ASC') {
80 krsort($new_array);
81 } else {
82 ksort($new_array);
83 }
84
85 if (!empty($new_array)) {
86 foreach ($new_array as $array) {
87 $index = $array['tempid'];
88 unset($array['tempid']);
89 $new_data[$index] = $array;
90 }
91 $data = $new_data;
92 }
93 }
94
95 return $data;
96 }
97
98 /**
99 * This function is responsible for generate unique numeric key for a given array.
100 *
101 * @param array $data
102 * @param integer $index
103 * @return integer
104 */
105 private static function numeric_key_gen($data, $index = 0)
106 {
107 if (isset($data[$index])) {
108 $index += 1;
109 return self::numeric_key_gen($data, $index);
110 }
111 return $index;
112 }
113
114 /**
115 * Sorting Data
116 * by their type
117 *
118 * @param array $value
119 * @param string $key
120 * @return void
121 */
122 public static function sortBy(&$value, $key = 'comments')
123 {
124 switch ($key) {
125 case 'comments':
126 return self::sorter($value, 'key', 'DESC');
127 break;
128 default:
129 return self::sorter($value, 'timestamp', 'DESC');
130 break;
131 }
132 }
133
134 /**
135 * Human Readable Time Diff
136 *
137 * @param boolean $time
138 * @return void
139 */
140 public static function get_timeago_html($time = false)
141 {
142 if (!$time) {
143 return;
144 }
145
146 $offset = get_option('gmt_offset'); // Time offset in hours
147 $local_time = $time + ($offset * 60 * 60); // added offset in seconds
148 $time = human_time_diff($local_time, current_time('timestamp'));
149 ob_start();
150 echo '<small> ' . esc_html__('About', 'betterdocs') . ' ' . esc_html($time) . ' ' . esc_html__('ago', 'betterdocs') . ' </small>';
151 $time_ago = ob_get_clean();
152 return $time_ago;
153 }
154
155 /**
156 * Get all post types
157 *
158 * @param array $exclude
159 * @return void
160 */
161 public static function post_types($exclude = array())
162 {
163 $post_types = get_post_types(array(
164 'public' => true,
165 'show_ui' => true
166 ), 'objects');
167
168 unset($post_types['attachment']);
169
170 if (count($exclude)) {
171 foreach ($exclude as $type) {
172 if (isset($post_types[$type])) {
173 unset($post_types[$type]);
174 }
175 }
176 }
177
178 return apply_filters('betterdocs_post_types', $post_types);
179 }
180
181 /**
182 * Get all taxonomies
183 *
184 * @param string $post_type
185 * @param array $exclude
186 * @return void
187 */
188 public static function taxonomies($post_type = '', $exclude = array())
189 {
190 if (empty($post_type)) {
191 $taxonomies = get_taxonomies(
192 array(
193 'public' => true,
194 '_builtin' => false
195 ),
196 'objects'
197 );
198 } else {
199
200 $taxonomies = get_object_taxonomies($post_type, 'objects');
201 }
202
203 $data = array();
204
205 if (is_array($taxonomies)) {
206 foreach ($taxonomies as $tax_slug => $tax) {
207 if (!$tax->public || !$tax->show_ui) {
208 continue;
209 }
210 if (in_array($tax_slug, $exclude)) {
211 continue;
212 }
213 $data[$tax_slug] = $tax;
214 }
215 }
216
217 return apply_filters('betterdocs_loop_taxonomies', $data, $taxonomies, $post_type);
218 }
219
220 public static function list_svg()
221 {
222 $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>';
223 return $html;
224 }
225
226 public static function arrow_right_svg()
227 {
228 $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>';
229 return $html;
230 }
231
232 public static function arrow_down_svg()
233 {
234 $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>';
235 return $html;
236 }
237
238 public static function get_tax($tax = '')
239 {
240 global $wp_query;
241
242 if (is_tax('knowledge_base')) {
243 $get_tax = $wp_query->tax_query->queried_terms;
244 if (array_key_exists("doc_category", $get_tax)) {
245 $tax = 'doc_category';
246 } else {
247 $tax = 'knowledge_base';
248 }
249 } elseif (is_tax('doc_category')) {
250 $tax = 'doc_category';
251 }
252
253 return $tax;
254 }
255
256 public static function list_query_arg($post_type, $multiple_kb, $tax_slug, $posts_per_grid, $alphabetically_order_post)
257 {
258 $args = array(
259 'post_type' => $post_type
260 );
261
262 $tax_query = array(
263 array(
264 'taxonomy' => 'doc_category',
265 'field' => 'slug',
266 'terms' => $tax_slug,
267 'operator' => 'AND',
268 'include_children' => false
269 ),
270 );
271
272 $args['tax_query'] = apply_filters('betterdocs_list_tax_query_arg', $tax_query, $multiple_kb, $tax_slug);
273
274 if ($posts_per_grid) {
275 $args['posts_per_page'] = $posts_per_grid;
276 }
277
278 if ($alphabetically_order_post == 1) {
279 $args['orderby'] = 'title';
280 $args['order'] = 'ASC';
281 }
282
283 return $args;
284 }
285
286 public static function count_kb()
287 {
288 $result = array();
289 $kbs = get_terms(array(
290 'taxonomy' => 'knowledge_base',
291 'hide_empty' => false,
292 'fields' => 'id=>slug'
293 ));
294
295 $cats = get_terms(array(
296 'taxonomy' => 'doc_category',
297 'hide_empty' => false,
298 'fields' => 'id=>slug'
299 ));
300
301 foreach ($kbs as $kb) {
302 foreach ($cats as $cat) {
303 $args = array(
304 'post_type' => 'docs',
305 'post_status' => 'publish',
306 'tax_query' => array(
307 'relation' => 'AND',
308 array(
309 'taxonomy' => 'knowledge_base',
310 'field' => 'slug',
311 'terms' => array($kb),
312 ),
313 array(
314 'taxonomy' => 'doc_category',
315 'field' => 'slug',
316 'terms' => array($cat),
317 ),
318 ),
319 );
320 $query = new WP_Query($args);
321 $result[$kb][$cat] = $query->post_count;
322 }
323 }
324 return $result;
325 }
326
327 public static function count_category($kb_slug, $cat_slug)
328 {
329 $args = array(
330 'post_type' => 'docs',
331 'post_status' => 'publish',
332 );
333
334 $taxes = array('knowledge_base', 'doc_category');
335 foreach ($taxes as $tax) {
336 $terms = get_terms($tax);
337
338 foreach ($terms as $term)
339 $tax_map[$tax][$term->slug] = $term->term_taxonomy_id;
340 }
341
342 $args['tax_query'] = array(
343 'relation' => 'AND',
344 array(
345 'taxonomy' => 'knowledge_base',
346 'field' => 'term_taxonomy_id',
347 'terms' => array($tax_map['knowledge_base'][$kb_slug]),
348 'operator' => 'IN',
349 'include_children' => false,
350 ),
351 array(
352 'taxonomy' => 'doc_category',
353 'field' => 'term_taxonomy_id',
354 'operator' => 'IN',
355 'terms' => array($tax_map['doc_category'][$cat_slug]),
356 'include_children' => false,
357 ),
358 );
359
360 $query = new WP_Query($args);
361 $count = $query->found_posts;
362 return $count;
363 }
364
365 public static function taxonomy_object($multiple_kb, $terms)
366 {
367 global $wp_query;
368 $terms_object = array(
369 'hide_empty' => true,
370 'taxonomy' => 'doc_category',
371 'orderby' => 'name',
372 'parent' => 0
373 );
374
375 if ($wp_query->query === NULL || (isset($wp_query->query['post_type']) && $wp_query->query['post_type'] != 'docs')) {
376 $terms_object['number'] = 4;
377 }
378
379 $meta_query = '';
380 $terms_object['meta_query'] = apply_filters('betterdocs_taxonomy_object_meta_query', $meta_query, $multiple_kb);
381
382 if ($terms) {
383 unset($terms_object['parent']);
384 }
385
386 if ($terms) {
387 $terms_object['include'] = explode(',', $terms);
388 $terms_object['orderby'] = 'include';
389 }
390
391 $taxonomy_objects = get_terms($terms_object);
392
393 return $taxonomy_objects;
394 }
395
396 public static function child_taxonomy_terms($term_id, $multiple_kb)
397 {
398 global $wp_query;
399 $terms_object = array(
400 'parent' => $term_id,
401 'orderby' => 'name'
402 );
403
404 if ($wp_query->query === NULL || (isset($wp_query->query['post_type']) && $wp_query->query['post_type'] != 'docs')) {
405 $terms_object['number'] = 1;
406 }
407
408 $meta_query = '';
409 $terms_object['meta_query'] = apply_filters('betterdocs_child_taxonomy_meta_query', $meta_query, $multiple_kb);
410 $taxonomy_objects = get_terms('doc_category', $terms_object);
411 return $taxonomy_objects;
412 }
413
414 public static function term_permalink($texanomy, $term_slug) {
415 $get_term_link = get_term_link( $term_slug, $texanomy );
416 $term_permalink = apply_filters( 'betterdocs_cat_term_permalink', $get_term_link, $term_slug, $texanomy );
417
418 return $term_permalink;
419 }
420
421 public static function get_prev($array, $key) {
422 $currentKey = array_search($key, $array);
423 if ($currentKey > 0 || $currentKey != 0) {
424 $nextKey = $currentKey - 1;
425 $prev_post = $array[$nextKey];
426 $nav = '<a rel="prev" class="next-post" href="'.get_post_permalink($prev_post).'"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="42px" viewBox="0 0 50 50" version="1.1"><g id="surface1"><path style=" " d="M 11.957031 13.988281 C 11.699219 14.003906 11.457031 14.117188 11.28125 14.308594 L 1.015625 25 L 11.28125 35.691406 C 11.527344 35.953125 11.894531 36.0625 12.242188 35.976563 C 12.589844 35.890625 12.867188 35.625 12.964844 35.28125 C 13.066406 34.933594 12.972656 34.5625 12.71875 34.308594 L 4.746094 26 L 48 26 C 48.359375 26.003906 48.695313 25.816406 48.878906 25.503906 C 49.058594 25.191406 49.058594 24.808594 48.878906 24.496094 C 48.695313 24.183594 48.359375 23.996094 48 24 L 4.746094 24 L 12.71875 15.691406 C 13.011719 15.398438 13.09375 14.957031 12.921875 14.582031 C 12.753906 14.203125 12.371094 13.96875 11.957031 13.988281 Z "></path></g></svg>'.get_the_title($prev_post).'</a>';
427 } else {
428 $nav = '';
429 }
430 return $nav;
431 }
432
433 public static function get_next($array, $key) {
434 $currentKey = array_search($key, $array);
435 if (end($array) != $array[$currentKey]) {
436 $nextKey = $currentKey + 1;
437 $next_post = $array[$nextKey];
438 $nav = '<a rel="next" class="next-post" href="'.get_post_permalink($next_post).'">'.get_the_title($next_post).'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="42px" viewBox="0 0 50 50" version="1.1"><g id="surface1"><path style=" " d="M 38.035156 13.988281 C 37.628906 13.980469 37.257813 14.222656 37.09375 14.59375 C 36.933594 14.96875 37.015625 15.402344 37.300781 15.691406 L 45.277344 24 L 2.023438 24 C 1.664063 23.996094 1.328125 24.183594 1.148438 24.496094 C 0.964844 24.808594 0.964844 25.191406 1.148438 25.503906 C 1.328125 25.816406 1.664063 26.003906 2.023438 26 L 45.277344 26 L 37.300781 34.308594 C 36.917969 34.707031 36.933594 35.339844 37.332031 35.722656 C 37.730469 36.105469 38.363281 36.09375 38.746094 35.691406 L 49.011719 25 L 38.746094 14.308594 C 38.5625 14.109375 38.304688 13.996094 38.035156 13.988281 Z "></path></g></svg></a>';
439 } else {
440 $nav = '';
441 }
442 return $nav;
443 }
444 }
445