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 +310 -203 4.8.2 → 4.9.3 View file →
@@ -54,8 +54,88 @@
54 54 return substr( $key, 0, 3 ) . str_repeat( '*', 8 ) . substr( $key, -4 );
55 55 }
56 56
57 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 + /**
58 138 * Resolve the WPML-translated base slug of a taxonomy for the CURRENT language.
59 139 *
60 140 * WPML registers each translatable taxonomy's rewrite slug as a string named
61 141 * "URL <taxonomy> tax slug" in the "WordPress" domain (e.g. "URL doc_tag tax slug").
@@ -1078,178 +1158,15 @@
1078 1158 ] );
1079 1159 }
1080 1160 }
1081 1161
1082 - public static function get_current_letter_docs( $current_letter, $limit = 0 ) {
1083 - global $wpdb;
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.
1084 1168
1085 - $limit = absint( $limit );
1086 - $limit_sql = $limit > 0 ? $wpdb->prepare( 'LIMIT %d', $limit ) : '';
1087 -
1088 - // Check if the encyclopedia_prefix parameter is set
1089 -
1090 - $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
1091 - $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
1092 - $encyclopedia_root_slug = betterdocs()->settings->get( 'encyclopedia_root_slug', 'encyclopdia' );
1093 - // Sanitize values that may be interpolated into raw SQL fragments below.
1094 - $encyclopedia_root_slug = sanitize_title( $encyclopedia_root_slug );
1095 -
1096 - // if($enable_glossaries && $encyclopeia_suorce === 'glossaries'){
1097 - if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
1098 - $lang_join = '';
1099 - $lang_where = '';
1100 -
1101 - // Add language filtering if multilingual plugin is active and we should apply filtering
1102 - $current_language = self::get_current_language();
1103 - if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
1104 - // Restrict language code to a safe character set before SQL interpolation.
1105 - $current_language = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $current_language );
1106 - // For WPML, use icl_translations table
1107 - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
1108 - $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'";
1109 - $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
1110 - }
1111 - // For Polylang, use term_relationships with language taxonomy
1112 - elseif ( function_exists( 'pll_current_language' ) ) {
1113 - $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";
1114 - $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1115 - }
1116 - }
1117 -
1118 - $query = "
1119 - SELECT
1120 - t.term_id,
1121 - t.name AS post_title,
1122 - t.slug as slug,
1123 - '' AS post_excerpt,
1124 - CONCAT('" . get_home_url() . "/$encyclopedia_root_slug/', t.slug) AS permalink,
1125 - tt.description AS post_content,
1126 - JSON_OBJECT(
1127 - 'status', COALESCE(MAX(CASE WHEN m.meta_key = 'status' THEN m.meta_value END), ''),
1128 - 'glossary_term_description', COALESCE(MAX(CASE WHEN m.meta_key = 'glossary_term_description' THEN m.meta_value END), '')
1129 - ) AS meta_data
1130 - FROM
1131 - {$wpdb->terms} t
1132 - INNER JOIN
1133 - {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
1134 - LEFT JOIN
1135 - {$wpdb->termmeta} m ON t.term_id = m.term_id
1136 - $lang_join
1137 - WHERE
1138 - tt.taxonomy = 'glossaries'
1139 - AND
1140 - SUBSTRING(t.name, 1, 1) = %s
1141 - $lang_where
1142 - GROUP BY
1143 - t.term_id
1144 - ORDER BY
1145 - t.name ASC
1146 - $limit_sql
1147 - ";
1148 - } else {
1149 - $lang_join = '';
1150 - $lang_where = '';
1151 -
1152 - // Add language filtering for docs if multilingual plugin is active and we should apply filtering
1153 - $current_language = self::get_current_language();
1154 - if ( $current_language && self::is_multilingual_active() && self::should_apply_language_filtering() ) {
1155 - // Restrict language code to a safe character set before SQL interpolation.
1156 - $current_language = preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $current_language );
1157 - // For WPML, use icl_translations table
1158 - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
1159 - $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'";
1160 - $lang_where = " AND (icl_t.language_code = '$current_language' OR icl_t.language_code IS NULL)";
1161 - }
1162 - // For Polylang, use term_relationships with language taxonomy
1163 - elseif ( function_exists( 'pll_current_language' ) ) {
1164 - $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";
1165 - $lang_where = " AND (t_lang.slug = '$current_language' OR t_lang.slug IS NULL)";
1166 - }
1167 - }
1168 -
1169 - $query = "
1170 - SELECT ID, post_title, post_excerpt, guid, post_content
1171 - FROM {$wpdb->posts}
1172 - $lang_join
1173 - WHERE post_type = 'docs'
1174 - AND post_status = 'publish'
1175 - AND SUBSTRING(post_title, 1, 1) = %s
1176 - $lang_where
1177 - ORDER BY post_date DESC
1178 - $limit_sql
1179 - ";
1180 - }
1181 -
1182 - $current_letter_docs = $wpdb->get_results( $wpdb->prepare( $query, $current_letter ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1183 -
1184 - return $current_letter_docs;
1185 - }
1186 -
1187 - public static function docs_sort_by_letter( $limit = 10 ) {
1188 - global $wpdb;
1189 - $enable_non_latin = betterdocs()->settings->get( 'encyclopedia_enable_non_latin' );
1190 - $script = betterdocs()->settings->get( 'encyclopedia_non_latin_option' );
1191 - $letters = Helper::get_character_range( $enable_non_latin, $script );
1192 -
1193 - $docs_by_letter = [];
1194 - $encyclopeia_suorce = betterdocs()->settings->get( 'encyclopedia_source', 'docs' );
1195 - $enable_glossaries = betterdocs()->settings->get( 'enable_glossaries', false );
1196 -
1197 - foreach ( $letters as $letter ) {
1198 - $posts = self::get_current_letter_docs( $letter, $limit );
1199 -
1200 - if ( is_array( $posts ) && ! empty( $posts ) ) {
1201 - foreach ( $posts as $post ) {
1202 - $description = isset($post['meta_data']) ? \json_decode( $post['meta_data'], true ) : '';
1203 - $glossary_term_description = $description['glossary_term_description'] ?? '';
1204 -
1205 - // Remove any <p> tags or other unwanted HTML tags
1206 - $glossary_term_description = wp_strip_all_tags( $glossary_term_description );
1207 - $post_excerpt = wp_strip_all_tags( $post['post_excerpt'] ?? '' );
1208 -
1209 - // Prepare post data
1210 - if ( $enable_glossaries && $encyclopeia_suorce === 'glossaries' ) {
1211 - // For glossaries
1212 - $permalink = '';
1213 -
1214 - if ( isset( $post['slug'] ) ) {
1215 - $term_link = get_term_link( $post['slug'], 'glossaries' );
1216 -
1217 - if ( ! is_wp_error( $term_link ) ) {
1218 - $permalink = $term_link;
1219 - }
1220 - }
1221 -
1222 - $post_data = [
1223 - 'id' => $post['term_id'] ?? '',
1224 - 'post_title' => $post['post_title'] ?? '',
1225 - 'post_excerpt' => ! empty( $post_excerpt )
1226 - ? $post_excerpt
1227 - : ( ! empty( $glossary_term_description )
1228 - ? self::get_custom_excerpt( $glossary_term_description, 15 )
1229 - : self::get_custom_excerpt( wp_strip_all_tags( $post['post_content'] ?? '' ), 15 ) ),
1230 - 'permalink' => $permalink,
1231 - ];
1232 - } else {
1233 - // For docs
1234 - $post_data = [
1235 - 'id' => $post['ID'] ?? '',
1236 - 'post_title' => $post['post_title'] ?? '',
1237 - 'post_excerpt' => ! empty( $post_excerpt )
1238 - ? $post_excerpt
1239 - : self::get_custom_excerpt( wp_strip_all_tags( $post['post_content'] ?? '' ), 15 ),
1240 - 'permalink' => isset( $post['ID'] ) ? get_the_permalink( $post['ID'] ) : ''
1241 - ];
1242 - }
1243 -
1244 - $docs_by_letter[$letter][] = $post_data;
1245 - }
1246 - }
1247 - }
1248 -
1249 - return $docs_by_letter;
1250 - }
1251 -
1252 1169 public static function get_glossaries() {
1253 1170 global $wpdb;
1254 1171
1255 1172 $lang_join = '';
@@ -1326,44 +1243,14 @@
1326 1243 }
1327 1244
1328 1245 return $layout;
1329 1246 }
1330 - public static function mb_ord_fallback( $char ) {
1331 - $code = unpack( 'N', mb_convert_encoding( $char, 'UCS-4BE', 'UTF-8' ) );
1332 - return $code[1];
1333 - }
1334 1247
1335 - public static function mb_chr_fallback( $code ) {
1336 - return mb_convert_encoding( pack( 'N', $code ), 'UTF-8', 'UCS-4BE' );
1337 - }
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.
1338 1252
1339 - public static function unicodeRange( $start, $end ) {
1340 - $range = [];
1341 - for ( $i = self::mb_ord_fallback( $start ); $i <= self::mb_ord_fallback( $end ); $i++ ) {
1342 - $range[] = self::mb_chr_fallback( $i );
1343 - }
1344 - return $range;
1345 - }
1346 -
1347 - public static function get_character_range( $enable_non_latin, $script ) {
1348 - if ( $enable_non_latin ) {
1349 - switch ( $script ) {
1350 - case 'arabic':
1351 - return self::unicodeRange( 'ء', 'ي' );
1352 - case 'cyrillic':
1353 - return self::unicodeRange( 'А', 'Я' );
1354 - case 'hebrew':
1355 - return self::unicodeRange( 'א', 'ת' );
1356 - case 'greek':
1357 - return self::unicodeRange( 'Α', 'Ω' );
1358 - default:
1359 - return range( 'A', 'Z' );
1360 - }
1361 - }
1362 -
1363 - return range( 'A', 'Z' );
1364 - }
1365 -
1366 1253 public static function get_the_top_most_parent( $term_id ) {
1367 1254 while ( $term_id != 0 ) {
1368 1255 $parent_id = wp_get_term_taxonomy_parent_id( $term_id, 'doc_category' );
1369 1256
@@ -1638,5 +1525,225 @@
1638 1525 $sql = $wpdb->prepare( "SELECT MAX(CAST(meta_value AS UNSIGNED)) AS max FROM {$wpdb->termmeta} WHERE meta_key = %s ", 'doc_category_order' );
1639 1526 $result = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- query is prepared above.
1640 1527 return $result;
1641 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 + }
1642 1749 }