PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.12.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.12.1
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 / translate.php

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

230 lines 5.0 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 if ( $language_target_id === $language_source_id ) {
106 return array();
107 }
108
109 /**
110 * Add dictionary tag
111 */
112
113 $dictionary_entries = wplng_dictionary_get_entries();
114
115 $texts_tagged = wplng_dictionary_add_tags(
116 $texts,
117 $language_target_id,
118 $dictionary_entries
119 );
120
121 /**
122 * Get texts list as JSON
123 */
124
125 $json_texts = wp_json_encode( $texts_tagged );
126
127 if ( empty( $json_texts ) ) {
128 return array();
129 }
130
131 /**
132 * Get the API call
133 */
134
135 $request = wp_remote_post(
136 WPLNG_API_URL . '/app/',
137 array(
138 'method' => 'POST',
139 'timeout' => 99, // 1 min 29 s
140 'sslverify' => WPLNG_API_SSLVERIFY,
141 'body' => array(
142 'request' => 'translate',
143 'api_key' => $api_key,
144 'version' => WPLNG_API_VERSION,
145 'context' => wplng_get_context(),
146 'source' => $language_source_id,
147 'target' => $language_target_id,
148 'texts' => $json_texts,
149 ),
150 )
151 );
152
153 /**
154 * Check if the API call worked
155 */
156
157 if ( is_wp_error( $request )
158 || wp_remote_retrieve_response_code( $request ) !== 200
159 ) {
160 wplng_set_api_overloaded();
161 return array();
162 }
163
164 /**
165 * Check and sanitize the API response
166 */
167
168 $response = json_decode( wp_remote_retrieve_body( $request ), true );
169
170 // Check error
171 if ( isset( $response['error'] )
172 && ( true === $response['error'] )
173 ) {
174 if ( isset( $response['disconnect'] )
175 && true === $response['disconnect']
176 ) {
177
178 delete_option( 'wplng_api_key_data' );
179 delete_option( 'wplng_api_key' );
180 wplng_clear_translations_cache();
181 wplng_clear_slugs_cache();
182 }
183
184 if ( isset( $response['reload'] )
185 && true === $response['reload']
186 ) {
187
188 delete_option( 'wplng_api_key_data' );
189 wplng_get_api_data();
190 }
191
192 return array();
193 }
194
195 // Check translations
196 if ( empty( $response['translations'] )
197 || ! is_array( $response['translations'] )
198 ) {
199 // API returned an error or an unexpected response
200 wplng_set_api_overloaded();
201 return array();
202 }
203
204 /**
205 * Here, API returned the list of translations
206 */
207
208 // Replace dictionary tag
209
210 $texts_untagged = wplng_dictionary_replace_tags(
211 $response['translations'],
212 $language_target_id,
213 $dictionary_entries
214 );
215
216 // Check and sanitize each translation
217
218 $translations = array();
219
220 foreach ( $texts_untagged as $key => $translation ) {
221 if ( is_string( $translation ) ) {
222 $translations[] = wp_kses( $translation, array() );
223 } elseif ( isset( $texts[ $key ] ) ) {
224 $translations[] = $texts[ $key ];
225 }
226 }
227
228 return $translations;
229 }
230