PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.0
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 / REST / Translations.php

Translations.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.0, at includes/REST/Translations.php

110 lines 3.8 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\REST;
4
5 use WP_REST_Request;
6 use WPDeveloper\BetterDocs\Core\BaseAPI;
7 use WPDeveloper\BetterDocs\Utils\Helper;
8
9 /**
10 * Term translation linking for the React admin (WPML / Polylang).
11 *
12 * Powers the slide-over "Language / This is a translation of / Translate" panel
13 * for the doc_category, doc_tag and knowledge_base taxonomies.
14 */
15 class Translations extends BaseAPI {
16
17 /** Taxonomies the React admins manage. */
18 private function allowed_taxonomies() {
19 return array_filter(
20 [ 'doc_category', 'doc_tag', 'knowledge_base' ],
21 'taxonomy_exists'
22 );
23 }
24
25 public function permission_check(): bool {
26 // QA-013: these routes read/write term language meta, so require the
27 // doc-term management cap (Editor+) — `edit_docs` is Author-level and
28 // let any author rewrite another category's language assignment. Mirrors
29 // the `manage_doc_terms` guard used by PostType term operations.
30 return current_user_can( 'manage_doc_terms' );
31 }
32
33 public function register() {
34 $this->get( 'term-translations', [ $this, 'get_term_translations' ] );
35 $this->post( 'term-language', [ $this, 'set_term_language' ] );
36 }
37
38 /**
39 * Translation group + "this is a translation of" candidates for a term (or for
40 * a target language when creating a new translation).
41 */
42 public function get_term_translations( WP_REST_Request $request ) {
43 $taxonomy = sanitize_key( (string) $request->get_param( 'taxonomy' ) );
44 if ( ! in_array( $taxonomy, $this->allowed_taxonomies(), true ) || ! Helper::is_multilingual_active() ) {
45 return rest_ensure_response( [ 'enabled' => false ] );
46 }
47
48 $default_lang = Helper::get_default_language();
49 $target_lang = sanitize_text_field( (string) $request->get_param( 'lang' ) );
50 $source_lang = sanitize_text_field( (string) $request->get_param( 'source_lang' ) ) ?: $default_lang;
51 $term_id = absint( $request->get_param( 'term_id' ) );
52
53 $current_lang = '';
54 $translation_of = 0;
55 $translations = [];
56
57 if ( $term_id ) {
58 $term = get_term( $term_id, $taxonomy );
59 if ( $term && ! is_wp_error( $term ) ) {
60 $current_lang = Helper::get_term_language( $term );
61 $translations = Helper::get_term_translations( $term );
62 if ( ! $target_lang ) {
63 $target_lang = $current_lang;
64 }
65 // The source-language sibling (if this term is already a translation).
66 if ( isset( $translations[ $source_lang ]['term_id'] ) ) {
67 $translation_of = (int) $translations[ $source_lang ]['term_id'];
68 }
69 }
70 }
71
72 // Candidates only matter when assigning a non-default language.
73 $candidates = ( $target_lang && $target_lang !== $default_lang )
74 ? Helper::get_translation_candidates( $taxonomy, $target_lang, $source_lang )
75 : [];
76
77 return rest_ensure_response( [
78 'enabled' => true,
79 'default_lang' => $default_lang,
80 'current_lang' => $current_lang,
81 'translation_of' => $translation_of,
82 'translations' => $translations,
83 'candidates' => $candidates,
84 ] );
85 }
86
87 /**
88 * Set a term's language and link it into the chosen translation group.
89 */
90 public function set_term_language( WP_REST_Request $request ) {
91 $taxonomy = sanitize_key( (string) $request->get_param( 'taxonomy' ) );
92 $term_id = absint( $request->get_param( 'term_id' ) );
93 $lang = sanitize_text_field( (string) $request->get_param( 'lang' ) );
94 $source = absint( $request->get_param( 'translation_of' ) );
95
96 if ( ! in_array( $taxonomy, $this->allowed_taxonomies(), true ) || ! $term_id || ! Helper::is_multilingual_active() ) {
97 return rest_ensure_response( [ 'success' => false ] );
98 }
99
100 $term = get_term( $term_id, $taxonomy );
101 if ( ! $term || is_wp_error( $term ) ) {
102 return rest_ensure_response( [ 'success' => false ] );
103 }
104
105 Helper::link_term_translation( $term, $lang, $source );
106
107 return rest_ensure_response( [ 'success' => true, 'lang' => $lang ] );
108 }
109 }
110