PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.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 / Utils / Helper.php

Helper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.8.1, at includes/Utils/Helper.php

412 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Utils;
4
5 use function BetterLinksPro\Dependencies\GuzzleHttp\json_decode;
6
7 class Helper extends Base {
8
9 public static function get_plugins( $plugin_basename = null ) {
10 if ( ! function_exists( 'get_plugins' ) ) {
11 include_once ABSPATH . 'wp-admin/includes/plugin.php';
12 }
13
14 $plugins = get_plugins();
15 return $plugin_basename == null ? $plugins : isset( $plugins[$plugin_basename] );
16 }
17
18 public static function is_plugin_active( $plugin_basename ) {
19 if ( ! function_exists( 'is_plugin_active' ) ) {
20 include_once ABSPATH . 'wp-admin/includes/plugin.php';
21 }
22
23 return is_plugin_active( $plugin_basename );
24 }
25
26 public static function get_tax( $tax = '' ) {
27 global $wp_query;
28
29 if ( is_tax( 'knowledge_base' ) ) {
30 $_taxes = $wp_query->tax_query->queried_terms;
31 if ( array_key_exists( "doc_category", $_taxes ) ) {
32 $tax = 'doc_category';
33 } else {
34 $tax = 'knowledge_base';
35 }
36 } elseif ( is_tax( 'doc_category' ) ) {
37 $tax = 'doc_category';
38 } elseif ( is_tax( 'doc_tag' ) ) {
39 $tax = 'doc_tag';
40 }
41
42 return $tax;
43 }
44
45 public function is_templates() {
46 global $wp_query;
47 $slug = betterdocs()->settings->get( 'encyclopedia_root_slug', 'encyclopedia' );
48
49 $tax = $this->get_tax();
50 if ( is_post_type_archive( 'docs' ) || $tax === 'knowledge_base' || $tax === 'doc_category' || $tax === 'doc_tag' || is_singular( 'docs' ) || is_tax( 'glossaries' ) ) {
51 return true;
52 }
53
54 if ( isset( $wp_query->query['pagename'] ) && $wp_query->query['pagename'] === $slug ) {
55 return true;
56 }
57
58 return false;
59 }
60
61 public function is_el_templates() {
62 $_return_val = betterdocs()->editor->get( 'elementor' )->is_templates();
63
64 if ( $_return_val !== null ) {
65 return $_return_val;
66 }
67
68 $this->is_templates();
69 }
70
71 /**
72 * Which tab to show.
73 *
74 * 1. Drag and Drop UI
75 * 2. Post List UI
76 *
77 * * 1. dnd
78 * * 2. classic
79 *
80 * look into views/admin/docs-ui directory to know more.
81 *
82 * @return string
83 */
84 public static function admin_tab() {
85 $admin_ui = 'grid';
86 if ( isset( $_GET['mode'], $_GET['page'] ) && $_GET['page'] === 'betterdocs-admin' && ! empty( $_GET['mode'] ) ) {
87 $admin_ui = $_GET['mode'] === 'grid' ? 'grid' : 'list';
88 }
89
90 return $admin_ui;
91 }
92
93 public static function is_active( $prev, $current, $class = 'active' ) {
94 if ( $current == $prev ) {
95 return $class;
96 }
97
98 return '';
99 }
100
101 public function get_users( $args ) {
102 $cache_key = 'betterdocs_cache_admin_user_roles';
103 $users = betterdocs()->database->get_cache( $cache_key );
104
105 if ( false === $users ) {
106 $users = get_users( $args );
107 betterdocs()->database->set_cache( $cache_key, $users );
108 }
109
110 return $users;
111 }
112
113 /**
114 * Normalize Menu Array
115 * Menu creator helper
116 *
117 * @since 2.5.0
118 *
119 * @param string $title
120 * @param string $slug
121 * @param string $cap
122 * @param array $callback
123 *
124 * @return array
125 */
126 public static function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null, $optional = [] ) {
127 $args = [
128 'page_title' => $title,
129 'menu_title' => $title,
130 'capability' => $cap,
131 'menu_slug' => $slug
132 ];
133
134 if ( $callback != null ) {
135 $args['callback'] = $callback;
136 }
137
138 return wp_parse_args( $optional, $args );
139 }
140
141 /**
142 * Check if the current theme is a block theme.
143 *
144 * @since x.x.x
145 * @return bool
146 */
147 public function current_theme_is_fse_theme() {
148 if ( function_exists( 'wp_is_block_theme' ) ) {
149 return (bool) wp_is_block_theme();
150 }
151 if ( function_exists( 'gutenberg_is_fse_theme' ) ) {
152 return (bool) gutenberg_is_fse_theme();
153 }
154
155 return false;
156 }
157
158 protected static function is_assoc_array( $array ) {
159 return array_keys( $array ) !== range( 0, count( $array ) - 1 );
160 }
161
162 public static function merge( &$array1, &$array2 ) {
163 $merged = $array1;
164
165 foreach ( $array2 as $key => &$value ) {
166 if ( is_array( $value ) && self::is_assoc_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
167 $merged[$key] = self::merge( $merged[$key], $value );
168 } elseif ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
169 $merged[$key] = array_merge( $merged[$key], $value );
170 } else {
171 $merged[$key] = $value;
172 }
173 }
174
175 return $merged;
176 }
177
178 public static function get_custom_excerpt( $content, $numOfWords ) {
179 $content = strip_shortcodes( $content );
180 $content = wp_strip_all_tags( $content );
181 $words = explode( ' ', $content );
182 $excerptWords = array_slice( $words, 0, $numOfWords );
183 $excerpt = implode( ' ', $excerptWords );
184 if ( count( $words ) > $numOfWords ) {
185 $excerpt .= '...';
186 }
187 return $excerpt;
188 }
189
190 public static function get_current_letter_docs( $current_letter, $limit = '' ) {
191 global $wpdb;
192
193 // Check if the encyclopedia_prefix parameter is set
194
195 $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
196 $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
197 $encyclopedia_root_slug = betterdocs()->settings->get( 'encyclopedia_root_slug', 'encyclopdia' );
198
199 // if($enable_glossaries && $encyclopeia_suorce === 'glossaries'){
200 if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
201 $query = "
202 SELECT
203 t.term_id,
204 t.name AS post_title,
205 t.slug as slug,
206 '' AS post_excerpt,
207 CONCAT('" . get_home_url() . "/$encyclopedia_root_slug/', t.slug) AS permalink,
208 tt.description AS post_content,
209 JSON_OBJECT(
210 'status', COALESCE(MAX(CASE WHEN m.meta_key = 'status' THEN m.meta_value END), ''),
211 'glossary_term_description', COALESCE(MAX(CASE WHEN m.meta_key = 'glossary_term_description' THEN m.meta_value END), '')
212 ) AS meta_data
213 FROM
214 {$wpdb->terms} t
215 INNER JOIN
216 {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
217 LEFT JOIN
218 {$wpdb->termmeta} m ON t.term_id = m.term_id
219 WHERE
220 tt.taxonomy = 'glossaries'
221 AND
222 LEFT(t.name, 1) = %s
223 GROUP BY
224 t.term_id
225 ORDER BY
226 t.name ASC
227 $limit
228 ";
229 } else {
230 $query = "
231 SELECT ID, post_title, post_excerpt, guid, post_content
232 FROM {$wpdb->posts}
233 WHERE post_type = 'docs'
234 AND post_status = 'publish'
235 AND LEFT(post_title, 1) = %s
236 ORDER BY post_date DESC
237 $limit
238 ";
239 }
240
241 $current_letter_docs = $wpdb->get_results( $wpdb->prepare( $query, $current_letter ), ARRAY_A );
242
243 return $current_letter_docs;
244 }
245
246 public static function docs_sort_by_letter($limit = 10) {
247 global $wpdb;
248 $enable_non_latin = betterdocs()->settings->get('encyclopedia_enable_non_latin');
249 $script = betterdocs()->settings->get('encyclopedia_non_latin_option');
250 $letters = Helper::get_character_range($enable_non_latin, $script);
251
252 $docs_by_letter = array();
253 $encyclopeia_suorce = betterdocs()->settings->get('encyclopedia_source', 'docs');
254 $enable_glossaries = betterdocs()->settings->get('enable_glossaries', false);
255
256
257 foreach ($letters as $letter) {
258
259 $posts = self::get_current_letter_docs($letter, "LIMIT $limit");
260
261 if (is_array($posts) && !empty($posts)) {
262 foreach ($posts as $post) {
263
264 $description = \json_decode($post['meta_data'], true);
265 $glossary_term_description = $description['glossary_term_description'] ?? '';
266
267
268 // Remove any <p> tags or other unwanted HTML tags
269 $glossary_term_description = strip_tags($glossary_term_description);
270 $post_excerpt = strip_tags($post['post_excerpt'] ?? '');
271
272 // Prepare post data
273 $post_data = array(
274 'id' => $post['ID'] ?? '',
275 'post_title' => $post['post_title'] ?? '',
276 'post_excerpt' => !empty($post_excerpt)
277 ? $post_excerpt
278 : (!empty($glossary_term_description)
279 ? self::get_custom_excerpt($glossary_term_description, 15)
280 : self::get_custom_excerpt(strip_tags($post['post_content'] ?? ''), 15)),
281 );
282
283
284 if($enable_glossaries && $encyclopeia_suorce === 'glossaries'){
285 $post_data['permalink'] = isset($post['slug']) ? get_term_link($post['slug'], 'glossaries') : '';
286 }
287 else{
288 $post_data['permalink'] = isset($post['ID']) ? get_the_permalink($post['ID']) : '';
289 }
290
291 $docs_by_letter[$letter][] = $post_data;
292 }
293 }
294 }
295
296 return $docs_by_letter;
297 }
298
299 public static function get_glossaries() {
300 global $wpdb;
301
302 $query = "
303 SELECT t.name
304 FROM {$wpdb->terms} t
305 INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
306 WHERE tt.taxonomy = 'glossaries'
307 ORDER BY t.name ASC
308 ";
309
310 $glossaries = $wpdb->get_col( $query );
311
312 return $glossaries;
313 }
314
315 /**
316 * Determine live search template layout, when live search is not selected from customizer(this will work when live search template is not selected from customizer)
317 *
318 * @param string $layout
319 * @return string $layout
320 */
321 public static function determine_search_layout( $layout ) {
322 $search_layout = betterdocs()->customizer->defaults->get( 'betterdocs_search_layout_select' );
323 $docs_layout = betterdocs()->customizer->defaults->get( 'betterdocs_docs_layout_select' );
324 $archive_page_layout = betterdocs()->customizer->defaults->get( 'betterdocs_archive_layout_select' );
325 $single_layout = betterdocs()->customizer->defaults->get( 'betterdocs_single_layout_select' );
326
327 if ( is_post_type_archive( 'docs' ) ) {
328 if ( $docs_layout != "layout-7" && ! $search_layout ) {
329 $layout = 'layout-1';
330 } else if ( $docs_layout == 'layout-7' && ! $search_layout ) {
331 $layout = 'layout-2';
332 }
333 } else if ( is_tax( 'doc_tag' ) && ! $search_layout ) {
334 $layout = 'layout-1';
335 } else if ( is_tax( 'doc_category' ) ) {
336 if ( $archive_page_layout != 'layout-7' && ! $search_layout ) {
337 $layout = 'layout-1';
338 } else if ( $archive_page_layout == 'layout-7' && ! $search_layout ) {
339 $layout = 'layout-2';
340 }
341 } else if ( is_singular( 'docs' ) ) {
342 if ( $single_layout != 'layout-8' && $single_layout != 'layout-9' && !$search_layout ) {
343 $layout = 'layout-1';
344 } else if ( ( $single_layout == 'layout-8' && ! $search_layout ) || ( $single_layout == 'layout-9' && ! $search_layout ) ) {
345 $layout = 'layout-2';
346 }
347 }
348
349 return $layout;
350 }
351 public static function mb_ord_fallback($char) {
352 $code = unpack('N', mb_convert_encoding($char, 'UCS-4BE', 'UTF-8'));
353 return $code[1];
354 }
355
356 public static function mb_chr_fallback($code) {
357 return mb_convert_encoding(pack('N', $code), 'UTF-8', 'UCS-4BE');
358 }
359
360 public static function unicodeRange($start, $end) {
361 $range = [];
362 for ($i = self::mb_ord_fallback($start); $i <= self::mb_ord_fallback($end); $i++) {
363 $range[] = self::mb_chr_fallback($i);
364 }
365 return $range;
366 }
367
368 public static function get_character_range($enable_non_latin, $script) {
369 if($enable_non_latin){
370 switch ($script) {
371 case 'arabic':
372 return self::unicodeRange('ء', 'ي');
373 case 'cyrillic':
374 return self::unicodeRange('А', 'Я');
375 case 'hebrew':
376 return self::unicodeRange('א', 'ת');
377 case 'greek':
378 return self::unicodeRange('Α', 'Ω');
379 default:
380 return range('A', 'Z');
381 }
382 }
383
384 return range('A', 'Z');
385 }
386
387 public static function get_the_top_most_parent( $term_id ) {
388 while( $term_id != 0 ) {
389 $parent_id = wp_get_term_taxonomy_parent_id( $term_id, 'doc_category' );
390
391 if( $parent_id == 0 ) {
392 break;
393 }
394
395 $term_id = $parent_id;
396 }
397 return $term_id;
398 }
399
400 public static function get_highest_docs_term() {
401 $terms = get_terms([
402 'taxonomy' => 'doc_category', // Change to your desired taxonomy
403 'hide_empty' => true, // Only show terms with posts
404 'orderby' => 'count', // Order by post count
405 'order' => 'DESC', // Descending order
406 'number' => 1 // Get only the top term
407 ]);
408 return isset( $terms[0] ) ? $terms[0] : [];
409 }
410
411 }
412