PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.10.0
Translate and Go multilingual – Automatic AI translation – wpLingua v2.10.0
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / api-call / translate.php

translate.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.10.0, at inc/api-call/translate.php

229 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Get data from wpLingua API : Translated texts
11 *
12 * Return an error message with error code
13 * or an array of translated texts
14 *
15 * API terms : https://wplingua.com/terms/
16 *
17 * ---------------------------------------------------
18 * Data sent :
19 * ---------------------------------------------------
20 * - request : 'translate'
21 * - api_key : the API key of website
22 * - version : API compatile version
23 * - context : Sends page calling URL
24 * - source : A language ID
25 * - target : A languages ID
26 * - texts : Array of untranslated texts of website
27 *
28 * ---------------------------------------------------
29 * Data received if successful :
30 * ---------------------------------------------------
31 * - translations : Array of translated texts
32 *
33 * ---------------------------------------------------
34 * Data received in case of failure
35 * ---------------------------------------------------
36 * - error : true
37 * - code : An integer
38 * - message : Error description
39 *
40 * @param array $texts
41 * @param string $language_source_id
42 * @param string $language_target_id
43 * @return array Translated texts or empty array
44 */
45 function wplng_api_call_translate(
46 $texts,
47 $language_source_id = '',
48 $language_target_id = ''
49 ) {
50
51 // Check texts array
52
53 if ( empty( $texts )
54 || ! is_array( $texts )
55 || wplng_get_api_overloaded()
56 || ! wplng_api_feature_is_allow( 'detection' )
57 ) {
58 return array();
59 }
60
61 // Check cookie
62
63 if ( empty( $_COOKIE['wplingua'] )
64 && apply_filters( 'wplng_cookie_check', true )
65 ) {
66
67 global $wplng_class_reload;
68 $wplng_class_reload = true;
69
70 return array();
71 }
72
73 // Ckeck and sanitize texts list
74
75 foreach ( $texts as $key => $text ) {
76 if ( ! is_string( $text ) ) {
77 $texts[ $key ] = '';
78 } else {
79 $texts[ $key ] = esc_html( $text );
80 }
81 }
82
83 // Get API key
84
85 $api_key = wplng_get_api_key();
86
87 if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
88 return array();
89 }
90
91 // Get and sanitize the API key
92
93 if ( empty( $language_target_id ) ) {
94 $language_target_id = wplng_get_language_current_id();
95 } elseif ( ! wplng_is_valid_language_id( $language_target_id ) ) {
96 return array();
97 }
98
99 if ( empty( $language_source_id ) ) {
100 $language_source_id = wplng_get_language_website_id();
101 } elseif ( ! wplng_is_valid_language_id( $language_source_id ) ) {
102 return array();
103 }
104
105 /**
106 * Add dictionary tag
107 */
108
109 $dictionary_entries = wplng_dictionary_get_entries();
110
111 $texts_tagged = wplng_dictionary_add_tags(
112 $texts,
113 $dictionary_entries
114 );
115
116 /**
117 * Get texts list as JSON
118 */
119
120 $json_texts = wp_json_encode( $texts_tagged );
121
122 if ( empty( $json_texts ) ) {
123 return array();
124 }
125
126 /**
127 * Get the API call
128 */
129
130 $body = array(
131 'request' => 'translate',
132 'api_key' => $api_key,
133 'version' => WPLNG_API_VERSION,
134 'context' => wplng_get_context(),
135 'source' => $language_source_id,
136 'target' => $language_target_id,
137 'texts' => $json_texts,
138 );
139
140 $args = array(
141 'method' => 'POST',
142 'timeout' => 99, // 1 min 29 s
143 'sslverify' => WPLNG_API_SSLVERIFY,
144 'body' => $body,
145 );
146
147 $request = wp_remote_post(
148 WPLNG_API_URL . '/app/',
149 $args
150 );
151
152 /**
153 * Check if the API call worked
154 */
155
156 if ( is_wp_error( $request )
157 || wp_remote_retrieve_response_code( $request ) != 200
158 ) {
159 wplng_set_api_overloaded();
160 return array();
161 }
162
163 /**
164 * Check and sanitize the API response
165 */
166
167 $response = json_decode( wp_remote_retrieve_body( $request ), true );
168
169 // Check error
170 if ( isset( $response['error'] )
171 && ( true === $response['error'] )
172 ) {
173 if ( isset( $response['disconnect'] )
174 && true === $response['disconnect']
175 ) {
176
177 delete_option( 'wplng_api_key_data' );
178 delete_option( 'wplng_api_key' );
179 wplng_clear_translations_cache();
180 wplng_clear_slugs_cache();
181 }
182
183 if ( isset( $response['reload'] )
184 && true === $response['reload']
185 ) {
186
187 delete_option( 'wplng_api_key_data' );
188 wplng_get_api_data();
189 }
190
191 return array();
192 }
193
194 // Check translations
195 if ( empty( $response['translations'] )
196 || ! is_array( $response['translations'] )
197 ) {
198 // API returned an error or an unexpected response
199 wplng_set_api_overloaded();
200 return array();
201 }
202
203 /**
204 * Here, API returned the list of translations
205 */
206
207 // Replace dictionary tag
208
209 $texts_untagged = wplng_dictionary_replace_tags(
210 $response['translations'],
211 $dictionary_entries,
212 $language_target_id
213 );
214
215 // Check and sanitize each translation
216
217 $translations = array();
218
219 foreach ( $texts_untagged as $key => $translation ) {
220 if ( is_string( $translation ) ) {
221 $translations[] = wp_kses( $translation, array() );
222 } elseif ( isset( $texts[ $key ] ) ) {
223 $translations = $texts[ $key ];
224 }
225 }
226
227 return $translations;
228 }
229