PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
4.9.3 4.9.2 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 All 201 releases
← All changes | includes/Utils/Helper.php +912 -179 4.4.0 → 4.9.3 View file →
@@ -1,13 +1,167 @@
1 1 <?php
2 2
3 3 namespace WPDeveloper\BetterDocs\Utils;
4 4
5 +// Helper utilities mix per-language URL detection (read-only $_GET reads),
6 +// dynamic alphabet-letter / glossary queries composed via $wpdb->prepare,
7 +// and meta-key term lookups that are core to BetterDocs functionality.
8 +// phpcs:disable WordPress.Security.NonceVerification.Recommended
9 +// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
10 +// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
11 +// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
12 +// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
13 +// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter
14 +// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query
15 +// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key
16 +// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
17 +
5 18 use function BetterLinksPro\Dependencies\GuzzleHttp\json_decode;
6 19 use function WPML\PHP\Logger\error;
7 20
8 21 class Helper extends Base {
9 22
23 + /**
24 + * Mask an API key for safe display.
25 + *
26 + * Prefix-aware: when the key carries a recognizable provider prefix
27 + * (OpenAI sk-/sk-proj-, Anthropic sk-ant-/sk-ant-api03-, Gemini AIza) that
28 + * prefix is kept visible so an admin can tell which provider/key is set,
29 + * then a fixed 8-asterisk block, then the last 4 chars. Keys without a known
30 + * prefix fall back to first 3 + 8 asterisks + last 4. The asterisk count is
31 + * always fixed so the real key length is never leaked.
32 + */
33 + public static function mask_api_key( $key ) {
34 + if ( ! is_string( $key ) || $key === '' ) {
35 + return '';
36 + }
37 + $key = trim( $key );
38 + if ( $key === '' ) {
39 + return '';
40 + }
41 +
42 + // Longest prefixes first so sk-proj-/sk-ant- win over the bare sk-.
43 + $prefixes = array( 'sk-ant-api03-', 'sk-ant-', 'sk-proj-', 'sk-', 'AIza' );
44 + foreach ( $prefixes as $prefix ) {
45 + if ( strncmp( $key, $prefix, strlen( $prefix ) ) === 0
46 + && strlen( $key ) >= strlen( $prefix ) + 4 ) {
47 + return $prefix . str_repeat( '*', 8 ) . substr( $key, -4 );
48 + }
49 + }
50 +
51 + if ( strlen( $key ) < 8 ) {
52 + return str_repeat( '*', strlen( $key ) );
53 + }
54 + return substr( $key, 0, 3 ) . str_repeat( '*', 8 ) . substr( $key, -4 );
55 + }
56 +
57 + /**
58 + * Drop the scheme + host from an absolute URL so it stays same-origin.
59 + *
60 + * Returns the ROOT-RELATIVE form of $url ("/wp-admin/admin-ajax.php",
61 + * "/wp-json/wp/v2/docs?search=foo", "/subdir/..." for a WP install in a
62 + * sub-directory) so the browser resolves it against the CURRENT page's origin
63 + * instead of the host baked into the URL. Path, query and fragment are kept
64 + * verbatim — only scheme+host are dropped — so sub-directory, multisite and
65 + * plain-permalink (?rest_route=) URLs all survive intact. A URL with no usable
66 + * path is returned unchanged.
67 + *
68 + * This is the origin-agnostic primitive behind frontend_ajax_url(); callers
69 + * whose output is embedded on a THIRD-PARTY site (e.g. the Instant Answer
70 + * cross-domain snippet) must keep the absolute URL and simply not call this.
71 + *
72 + * @param string $url Absolute URL.
73 + * @return string Root-relative URL, or $url unchanged when it has no path.
74 + */
75 + public static function relative_url( $url ) {
76 + if ( ! is_string( $url ) || $url === '' ) {
77 + return $url;
78 + }
79 +
80 + $parts = wp_parse_url( $url );
81 +
82 + if ( ! is_array( $parts ) || empty( $parts['path'] ) ) {
83 + return $url;
84 + }
85 +
86 + $relative = $parts['path'];
87 +
88 + if ( isset( $parts['query'] ) && $parts['query'] !== '' ) {
89 + $relative .= '?' . $parts['query'];
90 + }
91 +
92 + if ( isset( $parts['fragment'] ) && $parts['fragment'] !== '' ) {
93 + $relative .= '#' . $parts['fragment'];
94 + }
95 +
96 + return $relative;
97 + }
98 +
99 + /**
100 + * Same-origin admin-ajax URL for front-end requests (live search, etc.).
101 + *
102 + * admin_url() always resolves to the configured Site Address host, so when a
103 + * knowledge base is served on a HOST different from WP's Site Address — a
104 + * subdomain (e.g. faq.example.com), a domain alias, or a reverse proxy — the
105 + * AJAX request becomes cross-origin and is silently blocked or redirected by
106 + * the browser (the live search then returns no results).
107 + *
108 + * Emitting a ROOT-RELATIVE path ("/wp-admin/admin-ajax.php", or
109 + * "/subdir/wp-admin/admin-ajax.php" for a WP install in a sub-directory) lets
110 + * the browser resolve it against the CURRENT page's origin, so the request
111 + * always stays same-origin regardless of the Site Address. The path component
112 + * is taken verbatim from admin_url(), so sub-directory and multisite install
113 + * paths are preserved; only the scheme+host is dropped. When the parsed path
114 + * is empty, or in the admin area, the absolute URL is returned unchanged so
115 + * nothing else is affected.
116 + *
117 + * Override with the `betterdocs_frontend_ajax_url` filter if a site genuinely
118 + * needs an absolute or a different endpoint.
119 + *
120 + * @return string Root-relative admin-ajax path on the front end, else the absolute URL.
121 + */
122 + public static function frontend_ajax_url() {
123 + $ajax_url = admin_url( 'admin-ajax.php' );
124 +
125 + if ( ! is_admin() ) {
126 + $ajax_url = self::relative_url( $ajax_url );
127 + }
128 +
129 + /**
130 + * Filter the front-end admin-ajax URL used by BetterDocs live search.
131 + *
132 + * @param string $ajax_url Root-relative path (front end) or absolute URL.
133 + */
134 + return apply_filters( 'betterdocs_frontend_ajax_url', $ajax_url );
135 + }
136 +
137 + /**
138 + * Resolve the WPML-translated base slug of a taxonomy for the CURRENT language.
139 + *
140 + * WPML registers each translatable taxonomy's rewrite slug as a string named
141 + * "URL <taxonomy> tax slug" in the "WordPress" domain (e.g. "URL doc_tag tax slug").
142 + * BetterDocs stores only the default-language slug in its settings, so routing and
143 + * term links must read the translated value back here. Returns the trimmed default
144 + * slug unchanged when WPML is inactive or the string has no translation.
145 + *
146 + * @param string $taxonomy Taxonomy key, e.g. 'doc_tag'.
147 + * @param string $default_slug Default-language base slug from settings.
148 + * @return string Translated base slug for the active language (falls back to default).
149 + */
150 + public static function wpml_translated_tax_slug( $taxonomy, $default_slug ) {
151 + $default_slug = trim( (string) $default_slug, '/' );
152 +
153 + if ( $default_slug === '' || ! has_filter( 'wpml_translate_single_string' ) ) {
154 + return $default_slug;
155 + }
156 +
157 + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML-owned filter; name must be used verbatim.
158 + $translated = apply_filters( 'wpml_translate_single_string', $default_slug, 'WordPress', 'URL ' . $taxonomy . ' tax slug' );
159 + $translated = trim( (string) $translated, '/' );
160 +
161 + return $translated !== '' ? $translated : $default_slug;
162 + }
163 +
10 164 public static function get_plugins( $plugin_basename = null ) {
11 165 if ( ! function_exists( 'get_plugins' ) ) {
12 166 include_once ABSPATH . 'wp-admin/includes/plugin.php';
13 167 }
@@ -23,8 +177,41 @@
23 177
24 178 return is_plugin_active( $plugin_basename );
25 179 }
26 180
181 + /**
182 + * Whether an SEO plugin already emits FAQPage schema on the current page.
183 + *
184 + * True only when Yoast or Rank Math is active AND its FAQ block is present
185 + * in the post's content, so BetterDocs can skip its own FAQPage JSON-LD and
186 + * avoid duplicate structured data. Defaults to the queried object when no
187 + * post is given.
188 + *
189 + * @param int|\WP_Post|null $post
190 + * @return bool
191 + */
192 + public static function seo_plugin_outputs_faq_schema( $post = null ) {
193 + if ( null === $post ) {
194 + $post = get_queried_object();
195 + }
196 +
197 + $post = get_post( $post );
198 + if ( ! $post instanceof \WP_Post ) {
199 + return false;
200 + }
201 +
202 + if ( self::is_plugin_active( 'wordpress-seo/wp-seo.php' ) && has_block( 'yoast/faq-block', $post ) ) {
203 + return true;
204 + }
205 +
206 + if ( self::is_plugin_active( 'seo-by-rank-math/rank-math.php' ) && has_block( 'rank-math/faq-block', $post ) ) {
207 + return true;
208 + }
209 +
210 + // Extension seam for Pro / other SEO integrations.
211 + return (bool) apply_filters( 'betterdocs_seo_plugin_outputs_faq_schema', false, $post );
212 + }
213 +
27 214 public static function get_tax( $tax = '' ) {
28 215 global $wp_query;
29 216
30 217 if ( is_tax( 'knowledge_base' ) ) {
@@ -83,10 +270,14 @@
83 270 * @return string
84 271 */
85 272 public static function admin_tab() {
86 273 $admin_ui = 'grid';
87 - if ( isset( $_GET['mode'], $_GET['page'] ) && $_GET['page'] === 'betterdocs-admin' && ! empty( $_GET['mode'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
88 - $admin_ui = $_GET['mode'] === 'grid' ? 'grid' : 'list'; // phpcs:ignore WordPress.Security.NonceVerification.Missing
274 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin UI selection, no state change.
275 + $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
276 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin UI selection, no state change.
277 + $mode = isset( $_GET['mode'] ) ? sanitize_text_field( wp_unslash( $_GET['mode'] ) ) : '';
278 + if ( $page === 'betterdocs-admin' && ! empty( $mode ) ) {
279 + $admin_ui = $mode === 'grid' ? 'grid' : 'list';
89 280 }
90 281
91 282 return $admin_ui;
92 283 }
@@ -236,8 +427,58 @@
236 427 ( class_exists( 'TRP_Translate_Press' ) && function_exists( 'trp_get_current_language' ) );
237 428 }
238 429
239 430 /**
431 + * Configured/active languages from whichever multilingual plugin is present.
432 + *
433 + * Returns a list of { value, label } pairs (language code + display name).
434 + * Used to populate the optional language selector in the Write-with-AI modal;
435 + * returns an empty array when no multilingual plugin is active so the
436 + * selector stays hidden. Mirrors the Pro cross-domain language options.
437 + *
438 + * @return array<int,array{value:string,label:string}>
439 + */
440 + public static function get_active_languages() {
441 + $options = array();
442 +
443 + // WPML
444 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
445 + global $sitepress;
446 + if ( $sitepress && method_exists( $sitepress, 'get_active_languages' ) ) {
447 + $active_languages = $sitepress->get_active_languages();
448 + if ( is_array( $active_languages ) ) {
449 + foreach ( $active_languages as $code => $lang ) {
450 + $options[] = array(
451 + 'value' => (string) $code,
452 + 'label' => isset( $lang['native_name'] ) ? $lang['native_name'] : (string) $code,
453 + );
454 + }
455 + }
456 + }
457 + } elseif ( function_exists( 'pll_languages_list' ) ) {
458 + // Polylang
459 + $languages = pll_languages_list( array( 'fields' => array() ) );
460 + if ( is_array( $languages ) ) {
461 + foreach ( $languages as $lang ) {
462 + if ( is_object( $lang ) && isset( $lang->slug ) ) {
463 + $options[] = array(
464 + 'value' => (string) $lang->slug,
465 + 'label' => isset( $lang->name ) ? $lang->name : (string) $lang->slug,
466 + );
467 + }
468 + }
469 + }
470 + }
471 +
472 + /**
473 + * Filter the language options exposed to the Write-with-AI modal.
474 + *
475 + * @param array $options List of { value, label } language pairs.
476 + */
477 + return apply_filters( 'betterdocs_active_languages', $options );
478 + }
479 +
480 + /**
240 481 * Check if we should apply language filtering
241 482 * Only apply on frontend or when specifically requested
242 483 *
243 484 * @return bool
@@ -247,9 +488,9 @@
247 488 if ( is_admin() ) {
248 489 // Allow language filtering for REST API requests that are frontend-facing
249 490 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
250 491 // Check if this is a frontend REST request (not admin)
251 - $request_uri = $_SERVER['REQUEST_URI'] ?? '';
492 + $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
252 493 // Don't filter admin REST requests for glossaries management
253 494 if ( strpos( $request_uri, '/wp/v2/glossaries' ) !== false ) {
254 495 return false; // Don't filter admin glossaries management
255 496 }
@@ -270,15 +511,33 @@
270 511 */
271 512 public static function get_current_admin_language() {
272 513 $current_language = null;
273 514
515 + // Explicit language passed by the admin client takes priority.
516 + // Covers AJAX (POST) and REST/admin requests (GET) where WPML may
517 + // otherwise resolve to the site's default language instead of the
518 + // admin UI language.
519 + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only UI language hint, sanitized; not a state-changing form submission.
520 + if ( isset( $_POST['lang'] ) && ! empty( $_POST['lang'] ) ) {
521 + return self::sanitize_language_code( wp_unslash( $_POST['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- see note above.
522 + }
523 +
524 + // Limit GET handling to admin/REST contexts so a frontend ?lang= switch
525 + // doesn't hijack admin meta-key resolution.
526 + if ( isset( $_GET['lang'] ) && ! empty( $_GET['lang'] )
527 + && ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) ) {
528 + return self::sanitize_language_code( wp_unslash( $_GET['lang'] ) );
529 + }
530 +
274 531 // WPML Support - Admin language detection
275 532 if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
276 533 global $sitepress;
277 534 if ( $sitepress && $sitepress->is_setup_complete() ) {
535 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only language detection from URL.
536 + $tag_id = isset( $_GET['tag_ID'] ) ? (int) $_GET['tag_ID'] : 0;
278 537 // For term editing, check if we have a specific term language
279 - if ( isset( $_GET['tag_ID'] ) && function_exists( 'wpml_get_language_information' ) ) {
280 - $term_info = wpml_get_language_information( null, (int) $_GET['tag_ID'] );
538 + if ( $tag_id && function_exists( 'wpml_get_language_information' ) ) {
539 + $term_info = wpml_get_language_information( null, $tag_id );
281 540 if ( ! is_wp_error( $term_info ) && $term_info && isset( $term_info['language_code'] ) ) {
282 541 $current_language = $term_info['language_code'];
283 542 }
284 543
@@ -284,12 +543,18 @@
284 543
285 544 }
286 545
287 546 // Check for language parameter in URL
547 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only language detection from URL.
288 548 if ( ! $current_language && isset( $_GET['lang'] ) ) {
289 - $current_language = sanitize_text_field( $_GET['lang'] );
549 + $current_language = sanitize_text_field( wp_unslash( $_GET['lang'] ) );
290 550 }
291 551
552 + // Check WPML admin language cookie (persists during AJAX)
553 + if ( ! $current_language && isset( $_COOKIE['_icl_current_admin_language'] ) ) {
554 + $current_language = sanitize_text_field( wp_unslash( $_COOKIE['_icl_current_admin_language'] ) );
555 + }
556 +
292 557 // Fallback to admin language or current language
293 558 if ( ! $current_language ) {
294 559 $current_language = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : $sitepress->get_current_language();
295 560 }
@@ -296,11 +561,13 @@
296 561 }
297 562 }
298 563 // Polylang Support - Admin language detection
299 564 elseif ( function_exists( 'pll_current_language' ) ) {
565 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only language detection from URL.
566 + $tag_id = isset( $_GET['tag_ID'] ) ? (int) $_GET['tag_ID'] : 0;
300 567 // For term editing, get language from term ID
301 - if ( isset( $_GET['tag_ID'] ) && function_exists( 'pll_get_term_language' ) ) {
302 - $term_lang = pll_get_term_language( (int) $_GET['tag_ID'] );
568 + if ( $tag_id && function_exists( 'pll_get_term_language' ) ) {
569 + $term_lang = pll_get_term_language( $tag_id );
303 570 if ( $term_lang ) {
304 571 $current_language = $term_lang;
305 572 }
306 573 }
@@ -305,10 +572,11 @@
305 572 }
306 573 }
307 574
308 575 // Check for language parameter in URL
576 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only language detection from URL.
309 577 if ( ! $current_language && isset( $_GET['lang'] ) ) {
310 - $current_language = sanitize_text_field( $_GET['lang'] );
578 + $current_language = sanitize_text_field( wp_unslash( $_GET['lang'] ) );
311 579 }
312 580
313 581 // Fallback to current admin language
314 582 if ( ! $current_language ) {
@@ -325,12 +593,32 @@
325 593 elseif ( class_exists( 'TRP_Translate_Press' ) && function_exists( 'trp_get_current_language' ) ) {
326 594 $current_language = trp_get_current_language();
327 595 }
328 596
329 - return $current_language;
597 + return self::sanitize_language_code( $current_language );
330 598 }
331 599
332 600 /**
601 + * Normalize a language code to the character set real language codes use
602 + * (`en`, `en_US`, `zh-Hans`). Values reach this from `?lang=`, `$_POST['lang']`
603 + * and the WPML admin cookie, and `sanitize_text_field()` leaves quotes intact —
604 + * so anything used to build a meta key or SQL fragment must be narrowed here.
605 + * Defense in depth: callers that reach SQL must still bind their values.
606 + *
607 + * @param string|null $language Raw language code.
608 + * @return string|null Normalized code, or null when nothing usable remains.
609 + */
610 + private static function sanitize_language_code( $language ) {
611 + if ( ! is_string( $language ) || '' === $language ) {
612 + return null;
613 + }
614 +
615 + $language = preg_replace( '/[^A-Za-z0-9_-]/', '', $language );
616 +
617 + return '' !== $language ? $language : null;
618 + }
619 +
620 + /**
333 621 * Generate language-specific meta key for category ordering
334 622 * Always falls back to base key if language-specific key doesn't exist
335 623 *
336 624 * @param string $base_key The base meta key (e.g., 'doc_category_order')
@@ -358,8 +646,37 @@
358 646 return $base_key;
359 647 }
360 648
361 649 /**
650 + * Get the meta key to write to.
651 + *
652 + * Unlike `get_meta_key_with_fallback`, this never falls back to the base
653 + * key when the language-specific key is empty — that fallback is what
654 + * caused secondary-language drag-and-drop saves to clobber the base meta
655 + * (and on WPML setups that copy term meta from the original language,
656 + * the next read would re-overwrite it from the primary language).
657 + *
658 + * @param string $base_key The base meta key.
659 + * @param string|null $language Language code, auto-detected when null.
660 + * @return string Language-specific key when multilingual + language known, else base.
661 + */
662 + public static function get_meta_key_for_save( $base_key, $language = null ) {
663 + if ( ! self::is_multilingual_active() ) {
664 + return $base_key;
665 + }
666 +
667 + if ( $language === null ) {
668 + $language = self::get_current_admin_language();
669 + }
670 +
671 + if ( ! $language ) {
672 + return $base_key;
673 + }
674 +
675 + return $base_key . '_' . $language;
676 + }
677 +
678 + /**
362 679 * Get the appropriate meta key with fallback logic
363 680 * This function checks if language-specific meta exists, if not falls back to base key
364 681 *
365 682 * @param string $base_key The base meta key
@@ -396,8 +713,9 @@
396 713 }
397 714
398 715 // For queries without specific term ID, we need to check if ANY terms have language-specific meta
399 716 global $wpdb;
717 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- live multilingual meta-key resolution; result varies per active language.
400 718 $has_lang_meta = $wpdb->get_var( $wpdb->prepare(
401 719 "SELECT COUNT(*) FROM {$wpdb->termmeta} tm
402 720 INNER JOIN {$wpdb->term_taxonomy} tt ON tm.term_id = tt.term_id
403 721 WHERE tm.meta_key = %s AND tt.taxonomy = 'doc_category' AND tm.meta_value != ''",
@@ -425,8 +743,9 @@
425 743
426 744 global $wpdb;
427 745
428 746 // Get all terms with the base meta key
747 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- one-shot multilingual migration; cache would be stale immediately after writes.
429 748 $terms_with_order = $wpdb->get_results( $wpdb->prepare(
430 749 "SELECT tm.term_id, tm.meta_value, t.slug
431 750 FROM {$wpdb->termmeta} tm
432 751 INNER JOIN {$wpdb->terms} t ON tm.term_id = t.term_id
@@ -481,8 +800,9 @@
481 800
482 801 global $wpdb;
483 802
484 803 // Get all terms with the base meta key for document ordering
804 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- one-shot multilingual migration; cache would be stale immediately after writes.
485 805 $terms_with_docs_order = $wpdb->get_results( $wpdb->prepare(
486 806 "SELECT tm.term_id, tm.meta_value, t.slug
487 807 FROM {$wpdb->termmeta} tm
488 808 INNER JOIN {$wpdb->terms} t ON tm.term_id = t.term_id
@@ -559,162 +879,294 @@
559 879
560 880 return $languages;
561 881 }
562 882
563 - public static function get_current_letter_docs( $current_letter, $limit = 0 ) {
564 - global $wpdb;
883 + /**
884 + * Rich list of active site languages for the React admin language bar.
885 + *
886 + * @return array<int,array{code:string,label:string,native:string,flag:string}>
887 + * Empty when no supported multilingual plugin is active.
888 + */
889 + public static function get_admin_languages() {
890 + $languages = [];
565 891
566 - $limit = absint( $limit );
567 - $limit_sql = $limit > 0 ? $wpdb->prepare( 'LIMIT %d', $limit ) : '';
892 + // WPML
893 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
894 + global $sitepress;
895 + if ( $sitepress && $sitepress->is_setup_complete() ) {
896 + $active = $sitepress->get_active_languages();
897 + if ( is_array( $active ) ) {
898 + foreach ( $active as $code => $lang ) {
899 + $languages[] = [
900 + 'code' => $code,
901 + 'label' => isset( $lang['english_name'] ) ? $lang['english_name'] : $code,
902 + 'native' => isset( $lang['native_name'] ) ? $lang['native_name'] : ( isset( $lang['display_name'] ) ? $lang['display_name'] : $code ),
903 + 'flag' => isset( $lang['country_flag_url'] ) ? $lang['country_flag_url'] : '',
904 + ];
905 + }
906 + }
907 + }
908 + }
909 + // Polylang
910 + elseif ( function_exists( 'pll_languages_list' ) ) {
911 + $list = pll_languages_list( [ 'fields' => '' ] ); // full PLL_Language objects
912 + if ( is_array( $list ) ) {
913 + foreach ( $list as $lang ) {
914 + if ( ! is_object( $lang ) ) {
915 + continue;
916 + }
917 + $languages[] = [
918 + 'code' => isset( $lang->slug ) ? $lang->slug : '',
919 + 'label' => isset( $lang->name ) ? $lang->name : ( isset( $lang->slug ) ? $lang->slug : '' ),
920 + 'native' => isset( $lang->name ) ? $lang->name : '',
921 + 'flag' => isset( $lang->flag_url ) ? $lang->flag_url : '',
922 + ];
923 + }
924 + }
925 + }
568 926
569 - // Check if the encyclopedia_prefix parameter is set
927 + return $languages;
928 + }
570 929
571 - $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
572 - $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
573 - $encyclopedia_root_slug = betterdocs()->settings->get( 'encyclopedia_root_slug', 'encyclopdia' );
930 + /**
931 + * Read a term's language code via the active multilingual plugin.
932 + *
933 + * @param \WP_Term $term
934 + * @return string Language code, or '' when unavailable.
935 + */
936 + public static function get_term_language( $term ) {
937 + if ( ! is_object( $term ) || empty( $term->term_id ) ) {
938 + return '';
939 + }
574 940
575 - // if($enable_glossaries && $encyclopeia_suorce === 'glossaries'){
576 - if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
577 - $lang_join = '';
578 - $lang_where = '';
941 + // Polylang — takes the term_id.
942 + if ( function_exists( 'pll_get_term_language' ) ) {
943 + $lang = pll_get_term_language( $term->term_id, 'slug' );
944 + return $lang ? $lang : '';
945 + }
579 946
580 - // Add language filtering if multilingual plugin is active and we should apply filtering
581 - $current_language = self::get_current_language();
582 - if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
583 - // For WPML, use icl_translations table
584 - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
585 - $lang_join = " LEFT JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = t.term_id AND icl_t.element_type = 'tax_glossaries'";
586 - $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
947 + // WPML — element_id is the term_taxonomy_id (NOT the term_id); WPML
948 + // normalizes the element_type to `tax_<taxonomy>` internally.
949 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! empty( $term->term_taxonomy_id ) ) {
950 + $lang = apply_filters( 'wpml_element_language_code', null, [
951 + 'element_id' => $term->term_taxonomy_id,
952 + 'element_type' => isset( $term->taxonomy ) ? $term->taxonomy : 'doc_category',
953 + ] );
954 + return $lang ? $lang : '';
955 + }
956 +
957 + return '';
958 + }
959 +
960 + /**
961 + * Stamp a term's language via the active multilingual plugin. Standalone
962 + * assignment only — it sets/re-stamps the term's own language and does not
963 + * link it into an existing translation group.
964 + *
965 + * @param \WP_Term $term
966 + * @param string $lang_code
967 + */
968 + public static function set_term_language( $term, $lang_code ) {
969 + $lang_code = sanitize_text_field( (string) $lang_code );
970 + if ( $lang_code === '' || ! is_object( $term ) || empty( $term->term_id ) ) {
971 + return;
972 + }
973 +
974 + // Polylang
975 + if ( function_exists( 'pll_set_term_language' ) ) {
976 + pll_set_term_language( $term->term_id, $lang_code );
977 + return;
978 + }
979 +
980 + // WPML — element_id is the term_taxonomy_id; element_type is tax_<taxonomy>;
981 + // trid=null sets it as a standalone original in the chosen language.
982 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! empty( $term->term_taxonomy_id ) ) {
983 + $taxonomy = isset( $term->taxonomy ) ? $term->taxonomy : 'doc_category';
984 + do_action( 'wpml_set_element_language_details', [
985 + 'element_id' => $term->term_taxonomy_id,
986 + 'element_type' => 'tax_' . $taxonomy,
987 + 'trid' => null,
988 + 'language_code' => $lang_code,
989 + 'source_language_code' => null,
990 + ] );
991 + }
992 + }
993 +
994 + /**
995 + * The site's default language code, or '' when no multilingual plugin is active.
996 + */
997 + public static function get_default_language() {
998 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
999 + global $sitepress;
1000 + if ( $sitepress ) {
1001 + return (string) $sitepress->get_default_language();
1002 + }
1003 + }
1004 + if ( function_exists( 'pll_default_language' ) ) {
1005 + return (string) pll_default_language( 'slug' );
1006 + }
1007 + return '';
1008 + }
1009 +
1010 + /**
1011 + * All terms in a term's translation group, keyed by language code.
1012 + *
1013 + * @param \WP_Term $term
1014 + * @return array<string,array{term_id:int,name:string}>
1015 + */
1016 + public static function get_term_translations( $term ) {
1017 + if ( ! is_object( $term ) || empty( $term->term_id ) ) {
1018 + return [];
1019 + }
1020 + $taxonomy = isset( $term->taxonomy ) ? $term->taxonomy : 'doc_category';
1021 + $out = [];
1022 +
1023 + // Polylang
1024 + if ( function_exists( 'pll_get_term_translations' ) ) {
1025 + $group = pll_get_term_translations( $term->term_id ); // [lang => term_id]
1026 + if ( is_array( $group ) ) {
1027 + foreach ( $group as $lang => $tid ) {
1028 + $t = get_term( (int) $tid, $taxonomy );
1029 + if ( $t && ! is_wp_error( $t ) ) {
1030 + $out[ $lang ] = [ 'term_id' => (int) $tid, 'name' => $t->name ];
1031 + }
587 1032 }
588 - // For Polylang, use term_relationships with language taxonomy
589 - elseif ( function_exists( 'pll_current_language' ) ) {
590 - $lang_join = " LEFT JOIN {$wpdb->term_relationships} tr ON t.term_id = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt_lang ON tr.term_taxonomy_id = tt_lang.term_taxonomy_id AND tt_lang.taxonomy = 'language' LEFT JOIN {$wpdb->terms} t_lang ON tt_lang.term_id = t_lang.term_id";
591 - $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1033 + }
1034 + return $out;
1035 + }
1036 +
1037 + // WPML
1038 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! empty( $term->term_taxonomy_id ) ) {
1039 + $el_type = 'tax_' . $taxonomy;
1040 + $trid = apply_filters( 'wpml_element_trid', null, $term->term_taxonomy_id, $el_type );
1041 + if ( ! $trid ) {
1042 + return $out;
1043 + }
1044 + $translations = apply_filters( 'wpml_get_element_translations', null, $trid, $el_type );
1045 + if ( is_array( $translations ) ) {
1046 + foreach ( $translations as $lang => $tr ) {
1047 + $tid = isset( $tr->term_id ) ? (int) $tr->term_id : 0;
1048 + if ( ! $tid ) {
1049 + continue;
1050 + }
1051 + $t = get_term( $tid, $taxonomy );
1052 + $out[ $lang ] = [
1053 + 'term_id' => $tid,
1054 + 'name' => ( $t && ! is_wp_error( $t ) ) ? $t->name : ( isset( $tr->name ) ? $tr->name : '' ),
1055 + ];
592 1056 }
593 1057 }
1058 + }
594 1059
595 - $query = "
596 - SELECT
597 - t.term_id,
598 - t.name AS post_title,
599 - t.slug as slug,
600 - '' AS post_excerpt,
601 - CONCAT('" . get_home_url() . "/$encyclopedia_root_slug/', t.slug) AS permalink,
602 - tt.description AS post_content,
603 - JSON_OBJECT(
604 - 'status', COALESCE(MAX(CASE WHEN m.meta_key = 'status' THEN m.meta_value END), ''),
605 - 'glossary_term_description', COALESCE(MAX(CASE WHEN m.meta_key = 'glossary_term_description' THEN m.meta_value END), '')
606 - ) AS meta_data
607 - FROM
608 - {$wpdb->terms} t
609 - INNER JOIN
610 - {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
611 - LEFT JOIN
612 - {$wpdb->termmeta} m ON t.term_id = m.term_id
613 - $lang_join
614 - WHERE
615 - tt.taxonomy = 'glossaries'
616 - AND
617 - SUBSTRING(t.name, 1, 1) = %s
618 - $lang_where
619 - GROUP BY
620 - t.term_id
621 - ORDER BY
622 - t.name ASC
623 - $limit_sql
624 - ";
625 - } else {
626 - $lang_join = '';
627 - $lang_where = '';
1060 + return $out;
1061 + }
628 1062
629 - // Add language filtering for docs if multilingual plugin is active and we should apply filtering
630 - $current_language = self::get_current_language();
631 - if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
632 - // For WPML, use icl_translations table
633 - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
634 - $lang_join = " LEFT JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = {$wpdb->posts}.ID AND icl_t.element_type = 'post_docs'";
635 - $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
1063 + /**
1064 + * Candidate source terms for the "This is a translation of" dropdown — terms in
1065 + * $source_lang (default language) that aren't yet translated into $target_lang.
1066 + *
1067 + * @return array<int,array{term_id:int,name:string}>
1068 + */
1069 + public static function get_translation_candidates( $taxonomy, $target_lang, $source_lang ) {
1070 + $candidates = [];
1071 + $target_lang = sanitize_text_field( (string) $target_lang );
1072 + $source_lang = sanitize_text_field( (string) $source_lang );
1073 + if ( $taxonomy === '' || $source_lang === '' ) {
1074 + return $candidates;
1075 + }
1076 +
1077 + // WPML
1078 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
1079 + global $sitepress;
1080 + if ( $sitepress && method_exists( $sitepress, 'get_elements_without_translations' ) ) {
1081 + $ttids = $sitepress->get_elements_without_translations( 'tax_' . $taxonomy, $target_lang, $source_lang );
1082 + foreach ( (array) $ttids as $ttid ) {
1083 + $t = get_term_by( 'term_taxonomy_id', (int) $ttid, $taxonomy );
1084 + if ( $t && ! is_wp_error( $t ) ) {
1085 + $candidates[] = [ 'term_id' => (int) $t->term_id, 'name' => $t->name ];
1086 + }
636 1087 }
637 - // For Polylang, use term_relationships with language taxonomy
638 - elseif ( function_exists( 'pll_current_language' ) ) {
639 - $lang_join = " LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt_lang ON tr.term_taxonomy_id = tt_lang.term_taxonomy_id AND tt_lang.taxonomy = 'language' LEFT JOIN {$wpdb->terms} t_lang ON tt_lang.term_id = t_lang.term_id";
640 - $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1088 + }
1089 + return $candidates;
1090 + }
1091 +
1092 + // Polylang — source-lang terms whose group lacks the target language.
1093 + if ( function_exists( 'pll_get_term_translations' ) && function_exists( 'pll_get_term_language' ) ) {
1094 + $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'lang' => $source_lang ] );
1095 + foreach ( (array) $terms as $t ) {
1096 + if ( is_wp_error( $t ) ) {
1097 + continue;
641 1098 }
1099 + $group = pll_get_term_translations( $t->term_id );
1100 + if ( ! isset( $group[ $target_lang ] ) ) {
1101 + $candidates[] = [ 'term_id' => (int) $t->term_id, 'name' => $t->name ];
1102 + }
642 1103 }
643 -
644 - $query = "
645 - SELECT ID, post_title, post_excerpt, guid, post_content
646 - FROM {$wpdb->posts}
647 - $lang_join
648 - WHERE post_type = 'docs'
649 - AND post_status = 'publish'
650 - AND SUBSTRING(post_title, 1, 1) = %s
651 - $lang_where
652 - ORDER BY post_date DESC
653 - $limit_sql
654 - ";
655 1104 }
656 1105
657 - $current_letter_docs = $wpdb->get_results( $wpdb->prepare( $query, $current_letter ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
658 -
659 - return $current_letter_docs;
1106 + return $candidates;
660 1107 }
661 1108
662 - public static function docs_sort_by_letter( $limit = 10 ) {
663 - global $wpdb;
664 - $enable_non_latin = betterdocs()->settings->get( 'encyclopedia_enable_non_latin' );
665 - $script = betterdocs()->settings->get( 'encyclopedia_non_latin_option' );
666 - $letters = Helper::get_character_range( $enable_non_latin, $script );
1109 + /**
1110 + * Set a term's language and (optionally) link it into the translation group of
1111 + * $translation_of_term_id. Empty $translation_of_term_id = standalone.
1112 + *
1113 + * @param \WP_Term $term
1114 + * @param string $lang_code
1115 + * @param int $translation_of_term_id
1116 + */
1117 + public static function link_term_translation( $term, $lang_code, $translation_of_term_id = 0 ) {
1118 + $lang_code = sanitize_text_field( (string) $lang_code );
1119 + if ( $lang_code === '' || ! is_object( $term ) || empty( $term->term_id ) ) {
1120 + return;
1121 + }
1122 + $taxonomy = isset( $term->taxonomy ) ? $term->taxonomy : 'doc_category';
1123 + $translation_of_term_id = (int) $translation_of_term_id;
667 1124
668 - $docs_by_letter = [];
669 - $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
670 - $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
1125 + // Polylang
1126 + if ( function_exists( 'pll_set_term_language' ) ) {
1127 + pll_set_term_language( $term->term_id, $lang_code );
1128 + if ( $translation_of_term_id && function_exists( 'pll_save_term_translations' ) ) {
1129 + $group = function_exists( 'pll_get_term_translations' )
1130 + ? (array) pll_get_term_translations( $translation_of_term_id )
1131 + : [];
1132 + $group[ $lang_code ] = $term->term_id;
1133 + pll_save_term_translations( $group );
1134 + }
1135 + return;
1136 + }
671 1137
672 - foreach ( $letters as $letter ) {
673 - $posts = self::get_current_letter_docs( $letter, $limit );
1138 + // WPML
1139 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! empty( $term->term_taxonomy_id ) ) {
1140 + $el_type = 'tax_' . $taxonomy;
1141 + $trid = null;
1142 + $src = null;
674 1143
675 - if ( is_array( $posts ) && ! empty( $posts ) ) {
676 - foreach ( $posts as $post ) {
677 - $description = isset($post['meta_data']) ? \json_decode( $post['meta_data'], true ) : '';
678 - $glossary_term_description = $description['glossary_term_description'] ?? '';
1144 + if ( $translation_of_term_id ) {
1145 + $source = get_term( $translation_of_term_id, $taxonomy );
1146 + if ( $source && ! is_wp_error( $source ) ) {
1147 + $trid = apply_filters( 'wpml_element_trid', null, $source->term_taxonomy_id, $el_type );
1148 + $src = self::get_term_language( $source );
1149 + }
1150 + }
679 1151
680 - // Remove any <p> tags or other unwanted HTML tags
681 - $glossary_term_description = strip_tags( $glossary_term_description );
682 - $post_excerpt = strip_tags( $post['post_excerpt'] ?? '' );
1152 + do_action( 'wpml_set_element_language_details', [
1153 + 'element_id' => $term->term_taxonomy_id,
1154 + 'element_type' => $el_type,
1155 + 'trid' => $trid,
1156 + 'language_code' => $lang_code,
1157 + 'source_language_code' => $src,
1158 + ] );
1159 + }
1160 + }
683 1161
684 - // Prepare post data
685 - if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
686 - // For glossaries
687 - $post_data = [
688 - 'id' => $post['term_id'] ?? '',
689 - 'post_title' => $post['post_title'] ?? '',
690 - 'post_excerpt' => ! empty( $post_excerpt )
691 - ? $post_excerpt
692 - : ( ! empty( $glossary_term_description )
693 - ? self::get_custom_excerpt( $glossary_term_description, 15 )
694 - : self::get_custom_excerpt( strip_tags( $post['post_content'] ?? '' ), 15 ) ),
695 - 'permalink' => isset( $post['slug'] ) ? get_term_link( $post['slug'], 'glossaries' ) : ''
696 - ];
697 - } else {
698 - // For docs
699 - $post_data = [
700 - 'id' => $post['ID'] ?? '',
701 - 'post_title' => $post['post_title'] ?? '',
702 - 'post_excerpt' => ! empty( $post_excerpt )
703 - ? $post_excerpt
704 - : self::get_custom_excerpt( strip_tags( $post['post_content'] ?? '' ), 15 ),
705 - 'permalink' => isset( $post['ID'] ) ? get_the_permalink( $post['ID'] ) : ''
706 - ];
707 - }
1162 + // NOTE: get_current_letter_docs() and docs_sort_by_letter() used to live here.
1163 + // Encyclopedia is a Pro-only feature and these had no callers in Free at all,
1164 + // so they now ship as BetterDocsPro\Utils\EncyclopediaQuery. The generic
1165 + // helpers they lean on (get_current_language, is_multilingual_active,
1166 + // should_apply_language_filtering, get_custom_excerpt) stay here and are
1167 + // called through this class from Pro.
708 1168
709 - $docs_by_letter[$letter][] = $post_data;
710 - }
711 - }
712 - }
713 -
714 - return $docs_by_letter;
715 - }
716 -
717 1169 public static function get_glossaries() {
718 1170 global $wpdb;
719 1171
720 1172 $lang_join = '';
@@ -722,8 +1174,10 @@
722 1174
723 1175 // Add language filtering if multilingual plugin is active and we should apply filtering
724 1176 $current_language = self::get_current_language();
725 1177 if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
1178 + // Restrict language code to a safe character set before SQL interpolation.
1179 + $current_language = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $current_language );
726 1180 // For WPML, use icl_translations table
727 1181 if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
728 1182 $lang_join = " LEFT JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = t.term_id AND icl_t.element_type = 'tax_glossaries'";
729 1183 $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
@@ -789,44 +1243,14 @@
789 1243 }
790 1244
791 1245 return $layout;
792 1246 }
793 - public static function mb_ord_fallback( $char ) {
794 - $code = unpack( 'N', mb_convert_encoding( $char, 'UCS-4BE', 'UTF-8' ) );
795 - return $code[1];
796 - }
797 1247
798 - public static function mb_chr_fallback( $code ) {
799 - return mb_convert_encoding( pack( 'N', $code ), 'UTF-8', 'UCS-4BE' );
800 - }
1248 + // NOTE: the alphabet-range helpers (get_character_range, unicodeRange,
1249 + // mb_ord_fallback, mb_chr_fallback) moved to BetterDocsPro\Utils\EncyclopediaQuery
1250 + // along with the two methods above — same reason: Encyclopedia is Pro-only
1251 + // and nothing in Free ever called them.
801 1252
802 - public static function unicodeRange( $start, $end ) {
803 - $range = [];
804 - for ( $i = self::mb_ord_fallback( $start ); $i <= self::mb_ord_fallback( $end ); $i++ ) {
805 - $range[] = self::mb_chr_fallback( $i );
806 - }
807 - return $range;
808 - }
809 -
810 - public static function get_character_range( $enable_non_latin, $script ) {
811 - if ( $enable_non_latin ) {
812 - switch ( $script ) {
813 - case 'arabic':
814 - return self::unicodeRange( 'ء', 'ي' );
815 - case 'cyrillic':
816 - return self::unicodeRange( 'А', 'Я' );
817 - case 'hebrew':
818 - return self::unicodeRange( 'א', 'ת' );
819 - case 'greek':
820 - return self::unicodeRange( 'Α', 'Ω' );
821 - default:
822 - return range( 'A', 'Z' );
823 - }
824 - }
825 -
826 - return range( 'A', 'Z' );
827 - }
828 -
829 1253 public static function get_the_top_most_parent( $term_id ) {
830 1254 while ( $term_id != 0 ) {
831 1255 $parent_id = wp_get_term_taxonomy_parent_id( $term_id, 'doc_category' );
832 1256
@@ -849,15 +1273,22 @@
849 1273 ] );
850 1274 return isset( $terms[0] ) ? $terms[0] : [];
851 1275 }
852 1276
853 - public static function delete_specific_faq_posts_by_faq_category( $term_id ) {
1277 + public static function delete_specific_faq_posts_by_faq_category( $term_id, $taxonomy = 'betterdocs_faq_category' ) {
1278 + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- targeted bulk delete by FAQ category; tax filter is required.
854 1279 $args = [
855 1280 'post_type' => 'betterdocs_faq',
856 1281 'posts_per_page' => -1,
1282 + // EVERY status, explicitly. WP_Query defaults to 'publish', so "delete this
1283 + // group and its FAQs" was deleting only the published ones — the drafts (and
1284 + // pending/scheduled/private/trashed FAQs) survived, and the wp_delete_term()
1285 + // that follows then stripped their category, leaving them orphaned under
1286 + // "Uncategorized". Note 'any' is NOT enough here: it excludes trash.
1287 + 'post_status' => [ 'publish', 'draft', 'pending', 'future', 'private', 'trash' ],
857 1288 'tax_query' => [
858 1289 [
859 - 'taxonomy' => 'betterdocs_faq_category',
1290 + 'taxonomy' => $taxonomy,
860 1291 'field' => 'id',
861 1292 'terms' => $term_id,
862 1293 'operator' => 'IN'
863 1294 ]
@@ -953,8 +1384,9 @@
953 1384 'json' => '📋',
954 1385 'yaml' => '📋',
955 1386 'xml' => '📄',
956 1387 'markdown' => '📝',
1388 + 'curl' => '💻',
957 1389 'bash' => '💻',
958 1390 'shell' => '💻',
959 1391 'powershell' => '💻',
960 1392 'dockerfile' => '🐳',
@@ -962,8 +1394,89 @@
962 1394
963 1395 return isset( $icons[$language] ) ? $icons[$language] : '📄';
964 1396 }
965 1397
1398 + /**
1399 + * Echo the copy-to-clipboard button used by the Code Snippet and Code
1400 + * Snippet Tab templates.
1401 + *
1402 + * Both icons ship in the markup and CSS cross-fades between them on
1403 + * `.is-copied`, so the frontend script never rewrites the SVG. The tooltip
1404 + * carries its own strings as data attributes so the script can swap
1405 + * "Copy" → "Copied!" without hard-coding English.
1406 + *
1407 + * @return void
1408 + */
1409 + public static function code_snippet_copy_button() {
1410 + ?>
1411 + <div class="betterdocs-code-snippet-copy-container">
1412 + <button class="betterdocs-code-snippet-copy-button"
1413 + type="button"
1414 + aria-label="<?php esc_attr_e( 'Copy code to clipboard', 'betterdocs' ); ?>">
1415 + <span class="betterdocs-code-snippet-copy-icon" aria-hidden="true">
1416 + <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
1417 + <rect x="9" y="9" width="12.5" height="12.5" rx="3" stroke="currentColor" stroke-width="1.7"/>
1418 + <path d="M15.5 5.75V5A2.5 2.5 0 0 0 13 2.5H5A2.5 2.5 0 0 0 2.5 5v8A2.5 2.5 0 0 0 5 15.5h.75" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/>
1419 + </svg>
1420 + </span>
1421 + <span class="betterdocs-code-snippet-copied-icon" aria-hidden="true">
1422 + <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
1423 + <path d="M20 6.5 9.5 17 4 11.5" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/>
1424 + </svg>
1425 + </span>
1426 + </button>
1427 + <span class="betterdocs-code-snippet-tooltip"
1428 + role="status"
1429 + data-copy-label="<?php esc_attr_e( 'Copy', 'betterdocs' ); ?>"
1430 + data-copied-label="<?php esc_attr_e( 'Copied!', 'betterdocs' ); ?>"
1431 + data-error-label="<?php esc_attr_e( 'Copy failed', 'betterdocs' ); ?>"><?php esc_html_e( 'Copy', 'betterdocs' ); ?></span>
1432 + </div>
1433 + <?php
1434 + }
1435 +
1436 + /**
1437 + * Human-readable label for a programming-language identifier, used as the
1438 + * language-dropdown label on multi-language code snippets. Mirrors the
1439 + * block's LANGUAGE_OPTIONS; falls back to an upper-cased identifier.
1440 + *
1441 + * @param string $language Programming language identifier
1442 + * @return string
1443 + */
1444 + public static function get_language_label( $language ) {
1445 + $labels = [
1446 + 'javascript' => 'JavaScript',
1447 + 'typescript' => 'TypeScript',
1448 + 'php' => 'PHP',
1449 + 'python' => 'Python',
1450 + 'java' => 'Java',
1451 + 'ruby' => 'Ruby',
1452 + 'curl' => 'cURL',
1453 + 'bash' => 'Bash',
1454 + 'shell' => 'Shell',
1455 + 'json' => 'JSON',
1456 + 'yaml' => 'YAML',
1457 + 'html' => 'HTML',
1458 + 'css' => 'CSS',
1459 + 'scss' => 'SCSS',
1460 + 'sql' => 'SQL',
1461 + 'xml' => 'XML',
1462 + 'cpp' => 'C++',
1463 + 'csharp' => 'C#',
1464 + 'c' => 'C',
1465 + 'go' => 'Go',
1466 + 'rust' => 'Rust',
1467 + 'swift' => 'Swift',
1468 + 'kotlin' => 'Kotlin',
1469 + 'markdown' => 'Markdown'
1470 + ];
1471 +
1472 + if ( isset( $labels[ $language ] ) ) {
1473 + return $labels[ $language ];
1474 + }
1475 +
1476 + return ucwords( str_replace( [ '-', '_' ], ' ', (string) $language ) );
1477 + }
1478 +
966 1479 /**
967 1480 * Check if AI Chatbot is enabled
968 1481 *
969 1482 * @return bool
@@ -1008,9 +1521,229 @@
1008 1521 * @return int
1009 1522 */
1010 1523 public static function get_max_doc_category_order_from_term_meta() {
1011 1524 global $wpdb;
1012 - $sql = $wpdb->prepare( "SELECT MAX(CAST(meta_value AS UNSIGNED)) AS max FROM {$wpdb->prefix}termmeta WHERE meta_key = %s ", 'doc_category_order');
1013 - $result = $wpdb->get_var($sql);
1525 + $sql = $wpdb->prepare( "SELECT MAX(CAST(meta_value AS UNSIGNED)) AS max FROM {$wpdb->termmeta} WHERE meta_key = %s ", 'doc_category_order' );
1526 + $result = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- query is prepared above.
1014 1527 return $result;
1015 1528 }
1529 +
1530 + /**
1531 + * Encyclopedia / glossary query helpers — backward-compatibility shims.
1532 + *
1533 + * These methods were moved out of Free and into BetterDocs Pro's own
1534 + * `EncyclopediaQuery` class as of Pro 4.3.1 (the "move glossaries to Pro"
1535 + * release). BetterDocs Pro 4.3.0 and earlier, however, still call
1536 + * `Helper::get_character_range()` / `get_current_letter_docs()` /
1537 + * `docs_sort_by_letter()` from their encyclopedia blocks, widgets,
1538 + * shortcodes, templates and AJAX callbacks. When a site runs new Free with
1539 + * an older Pro (< 4.3.1) still active, those calls would fatal with
1540 + * "Call to undefined method". Keeping these shims here lets that older Pro
1541 + * keep rendering until it is updated. Pro 4.3.1+ uses its own copy and never
1542 + * touches these, so there is no double-execution or conflict.
1543 + *
1544 + * @deprecated Retained only for BetterDocs Pro < 4.3.1 compatibility.
1545 + */
1546 + public static function get_current_letter_docs( $current_letter, $limit = 0 ) {
1547 + global $wpdb;
1548 +
1549 + $limit = absint( $limit );
1550 + $limit_sql = $limit > 0 ? $wpdb->prepare( 'LIMIT %d', $limit ) : '';
1551 +
1552 + // Check if the encyclopedia_prefix parameter is set
1553 +
1554 + $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
1555 + $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
1556 + $encyclopedia_root_slug = betterdocs()->settings->get( 'encyclopedia_root_slug', 'encyclopdia' );
1557 + // Sanitize values that may be interpolated into raw SQL fragments below.
1558 + $encyclopedia_root_slug = sanitize_title( $encyclopedia_root_slug );
1559 +
1560 + // if($enable_glossaries && $encyclopeia_suorce === 'glossaries'){
1561 + if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
1562 + $lang_join = '';
1563 + $lang_where = '';
1564 +
1565 + // Add language filtering if multilingual plugin is active and we should apply filtering
1566 + $current_language = self::get_current_language();
1567 + if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
1568 + // Restrict language code to a safe character set before SQL interpolation.
1569 + $current_language = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $current_language );
1570 + // For WPML, use icl_translations table
1571 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
1572 + $lang_join = " LEFT JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = t.term_id AND icl_t.element_type = 'tax_glossaries'";
1573 + $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
1574 + }
1575 + // For Polylang, use term_relationships with language taxonomy
1576 + elseif ( function_exists( 'pll_current_language' ) ) {
1577 + $lang_join = " LEFT JOIN {$wpdb->term_relationships} tr ON t.term_id = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt_lang ON tr.term_taxonomy_id = tt_lang.term_taxonomy_id AND tt_lang.taxonomy = 'language' LEFT JOIN {$wpdb->terms} t_lang ON tt_lang.term_id = t_lang.term_id";
1578 + $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1579 + }
1580 + }
1581 +
1582 + $query = "
1583 + SELECT
1584 + t.term_id,
1585 + t.name AS post_title,
1586 + t.slug as slug,
1587 + '' AS post_excerpt,
1588 + CONCAT('" . get_home_url() . "/$encyclopedia_root_slug/', t.slug) AS permalink,
1589 + tt.description AS post_content,
1590 + JSON_OBJECT(
1591 + 'status', COALESCE(MAX(CASE WHEN m.meta_key = 'status' THEN m.meta_value END), ''),
1592 + 'glossary_term_description', COALESCE(MAX(CASE WHEN m.meta_key = 'glossary_term_description' THEN m.meta_value END), '')
1593 + ) AS meta_data
1594 + FROM
1595 + {$wpdb->terms} t
1596 + INNER JOIN
1597 + {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
1598 + LEFT JOIN
1599 + {$wpdb->termmeta} m ON t.term_id = m.term_id
1600 + $lang_join
1601 + WHERE
1602 + tt.taxonomy = 'glossaries'
1603 + AND
1604 + SUBSTRING(t.name, 1, 1) = %s
1605 + $lang_where
1606 + GROUP BY
1607 + t.term_id
1608 + ORDER BY
1609 + t.name ASC
1610 + $limit_sql
1611 + ";
1612 + } else {
1613 + $lang_join = '';
1614 + $lang_where = '';
1615 +
1616 + // Add language filtering for docs if multilingual plugin is active and we should apply filtering
1617 + $current_language = self::get_current_language();
1618 + if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
1619 + // Restrict language code to a safe character set before SQL interpolation.
1620 + $current_language = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $current_language );
1621 + // For WPML, use icl_translations table
1622 + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
1623 + $lang_join = " LEFT JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = {$wpdb->posts}.ID AND icl_t.element_type = 'post_docs'";
1624 + $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
1625 + }
1626 + // For Polylang, use term_relationships with language taxonomy
1627 + elseif ( function_exists( 'pll_current_language' ) ) {
1628 + $lang_join = " LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id LEFT JOIN {$wpdb->term_taxonomy} tt_lang ON tr.term_taxonomy_id = tt_lang.term_taxonomy_id AND tt_lang.taxonomy = 'language' LEFT JOIN {$wpdb->terms} t_lang ON tt_lang.term_id = t_lang.term_id";
1629 + $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1630 + }
1631 + }
1632 +
1633 + $query = "
1634 + SELECT ID, post_title, post_excerpt, guid, post_content
1635 + FROM {$wpdb->posts}
1636 + $lang_join
1637 + WHERE post_type = 'docs'
1638 + AND post_status = 'publish'
1639 + AND SUBSTRING(post_title, 1, 1) = %s
1640 + $lang_where
1641 + ORDER BY post_date DESC
1642 + $limit_sql
1643 + ";
1644 + }
1645 +
1646 + $current_letter_docs = $wpdb->get_results( $wpdb->prepare( $query, $current_letter ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1647 +
1648 + return $current_letter_docs;
1649 + }
1650 +
1651 + public static function docs_sort_by_letter( $limit = 10 ) {
1652 + global $wpdb;
1653 + $enable_non_latin = betterdocs()->settings->get( 'encyclopedia_enable_non_latin' );
1654 + $script = betterdocs()->settings->get( 'encyclopedia_non_latin_option' );
1655 + $letters = Helper::get_character_range( $enable_non_latin, $script );
1656 +
1657 + $docs_by_letter = [];
1658 + $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
1659 + $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
1660 +
1661 + foreach ( $letters as $letter ) {
1662 + $posts = self::get_current_letter_docs( $letter, $limit );
1663 +
1664 + if ( is_array( $posts ) && ! empty( $posts ) ) {
1665 + foreach ( $posts as $post ) {
1666 + $description = isset($post['meta_data']) ? \json_decode( $post['meta_data'], true ) : '';
1667 + $glossary_term_description = $description['glossary_term_description'] ?? '';
1668 +
1669 + // Remove any <p> tags or other unwanted HTML tags
1670 + $glossary_term_description = wp_strip_all_tags( $glossary_term_description );
1671 + $post_excerpt = wp_strip_all_tags( $post['post_excerpt'] ?? '' );
1672 +
1673 + // Prepare post data
1674 + if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
1675 + // For glossaries
1676 + $permalink = '';
1677 +
1678 + if ( isset( $post['slug'] ) ) {
1679 + $term_link = get_term_link( $post['slug'], 'glossaries' );
1680 +
1681 + if ( ! is_wp_error( $term_link ) ) {
1682 + $permalink = $term_link;
1683 + }
1684 + }
1685 +
1686 + $post_data = [
1687 + 'id' => $post['term_id'] ?? '',
1688 + 'post_title' => $post['post_title'] ?? '',
1689 + 'post_excerpt' => ! empty( $post_excerpt )
1690 + ? $post_excerpt
1691 + : ( ! empty( $glossary_term_description )
1692 + ? self::get_custom_excerpt( $glossary_term_description, 15 )
1693 + : self::get_custom_excerpt( wp_strip_all_tags( $post['post_content'] ?? '' ), 15 ) ),
1694 + 'permalink' => $permalink,
1695 + ];
1696 + } else {
1697 + // For docs
1698 + $post_data = [
1699 + 'id' => $post['ID'] ?? '',
1700 + 'post_title' => $post['post_title'] ?? '',
1701 + 'post_excerpt' => ! empty( $post_excerpt )
1702 + ? $post_excerpt
1703 + : self::get_custom_excerpt( wp_strip_all_tags( $post['post_content'] ?? '' ), 15 ),
1704 + 'permalink' => isset( $post['ID'] ) ? get_the_permalink( $post['ID'] ) : ''
1705 + ];
1706 + }
1707 +
1708 + $docs_by_letter[$letter][] = $post_data;
1709 + }
1710 + }
1711 + }
1712 +
1713 + return $docs_by_letter;
1714 + }
1715 + public static function mb_ord_fallback( $char ) {
1716 + $code = unpack( 'N', mb_convert_encoding( $char, 'UCS-4BE', 'UTF-8' ) );
1717 + return $code[1];
1718 + }
1719 +
1720 + public static function mb_chr_fallback( $code ) {
1721 + return mb_convert_encoding( pack( 'N', $code ), 'UTF-8', 'UCS-4BE' );
1722 + }
1723 + public static function unicodeRange( $start, $end ) {
1724 + $range = [];
1725 + for ( $i = self::mb_ord_fallback( $start ); $i <= self::mb_ord_fallback( $end ); $i++ ) {
1726 + $range[] = self::mb_chr_fallback( $i );
1727 + }
1728 + return $range;
1729 + }
1730 +
1731 + public static function get_character_range( $enable_non_latin, $script ) {
1732 + if ( $enable_non_latin ) {
1733 + switch ( $script ) {
1734 + case 'arabic':
1735 + return self::unicodeRange( 'ء', 'ي' );
1736 + case 'cyrillic':
1737 + return self::unicodeRange( 'А', 'Я' );
1738 + case 'hebrew':
1739 + return self::unicodeRange( 'א', 'ת' );
1740 + case 'greek':
1741 + return self::unicodeRange( 'Α', 'Ω' );
1742 + default:
1743 + return range( 'A', 'Z' );
1744 + }
1745 + }
1746 +
1747 + return range( 'A', 'Z' );
1748 + }
1016 1749 }