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.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 1.1.1 All 111 releases
wplingua / inc / api-key.php

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

373 lines 7.4 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 * Validate a wpLingua API key
11 *
12 * @param string $api_key
13 * @return bool
14 */
15 function wplng_is_valid_api_key_format( $api_key ) {
16
17 $regex = '/^[a-zA-Z1-9]{42}$/s';
18
19 if (
20 empty( $api_key )
21 || ! is_string( $api_key )
22 || $api_key !== preg_quote( $api_key )
23 || false === preg_match( $regex, $api_key )
24 ) {
25 return false;
26 }
27
28 return true;
29 }
30
31
32 /**
33 * Get the wpLingua registered API key
34 *
35 * @return string
36 */
37 function wplng_get_api_key() {
38
39 $api_key = trim( get_option( 'wplng_api_key' ) );
40
41 if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
42 $api_key = '';
43 }
44
45 return $api_key;
46 }
47
48
49 /**
50 * Get wpLingua API key data
51 * - Website language (id or 'all')
52 * - Target language(s) (array)
53 * - Enabled features
54 *
55 * @return array
56 */
57 function wplng_get_api_data() {
58
59 if ( empty( wplng_get_api_key() ) ) {
60 return array();
61 }
62
63 $data_checked = array();
64 $data = get_option( 'wplng_api_key_data' );
65
66 if ( ! empty( $data ) ) {
67 $data = json_decode( $data, true );
68 }
69
70 /**
71 * Check the API key data
72 */
73
74 if ( ! empty( $data['language_original'] )
75 && (
76 wplng_is_valid_language_id( $data['language_original'] )
77 || 'all' === $data['language_original']
78 )
79 && ! empty( $data['languages_target'] )
80 && wplng_is_valid_language_ids( $data['languages_target'] )
81 && isset( $data['features'] )
82 && is_array( $data['features'] )
83 && ! empty( $data['status'] )
84 && ! empty( $data['time'] )
85 && is_int( $data['time'] )
86 && ( $data['time'] + ( 8 * HOUR_IN_SECONDS ) ) > time()
87 ) {
88
89 /**
90 * Sanitize languages target
91 */
92
93 $languages_target = array();
94
95 foreach ( $data['languages_target'] as $id ) {
96
97 if ( ! wplng_is_valid_language_id( $id ) ) {
98 continue;
99 }
100
101 $languages_target[] = sanitize_key( $id );
102 }
103
104 /**
105 * Sanitize features list
106 */
107
108 $features = array();
109
110 foreach ( $data['features'] as $key => $allow ) {
111
112 if ( ! is_string( $key ) || ! is_bool( $allow ) ) {
113 continue;
114 }
115
116 $key = sanitize_key( $key );
117 $allow = ( true === $allow );
118
119 $features[ $key ] = $allow;
120 }
121
122 /**
123 * Sanitize status
124 */
125
126 $status = 'FREE';
127
128 if ( 'PREMIUM' === $data['status']
129 || 'VIP' === $data['status']
130 ) {
131 $status = $data['status'];
132 }
133
134 /**
135 * Make the checked response
136 */
137
138 $data_checked = array(
139 'language_original' => sanitize_key( $data['language_original'] ),
140 'languages_target' => $languages_target,
141 'features' => $features,
142 'status' => $status,
143 );
144
145 /**
146 * Add expiration
147 */
148
149 if ( ! empty( $data['expiration'] )
150 && is_string( $data['expiration'] )
151 ) {
152 $data_checked['expiration'] = $data['expiration'];
153 }
154
155 /**
156 * Add time
157 */
158
159 $data_checked['time'] = $data['time'];
160
161 } else {
162
163 /**
164 * Get sanitized data from API call
165 */
166
167 $data_checked = wplng_api_call_validate_api_key();
168
169 if ( empty( $data_checked ) ) {
170 return array();
171 }
172
173 /**
174 * Add time
175 */
176
177 $data_checked['time'] = time();
178
179 update_option(
180 'wplng_api_key_data',
181 wp_json_encode( $data_checked ),
182 true
183 );
184
185 wp_cache_flush();
186
187 }
188
189 return $data_checked;
190 }
191
192
193 /**
194 * Get website language from wpLingua API data
195 *
196 * @return string Language ID, 'all' or ''
197 */
198 function wplng_get_api_language_website() {
199
200 $data = wplng_get_api_data();
201
202 if (
203 ! empty( $data['language_original'] )
204 && (
205 wplng_is_valid_language_id( $data['language_original'] )
206 || 'all' === $data['language_original']
207 )
208 ) {
209 return $data['language_original'];
210 }
211
212 return '';
213 }
214
215
216 /**
217 * Get target languages from wpLingua API data
218 *
219 * @return mixed Language IDs array, 'all' or false
220 */
221 function wplng_get_api_languages_target() {
222
223 $data = wplng_get_api_data();
224
225 if ( empty( $data['languages_target'] ) ) {
226 return false;
227 } elseif ( 'all' === $data['languages_target'] ) {
228 return 'all';
229 } elseif ( is_array( $data['languages_target'] ) ) {
230
231 $all_languages = wplng_get_languages_all();
232 $languages_id_ordered = array();
233
234 foreach ( $all_languages as $language ) {
235 foreach ( $data['languages_target'] as $language_id ) {
236
237 if (
238 ! wplng_is_valid_language_id( $language_id )
239 || empty( $language['id'] )
240 || $language['id'] !== $language_id
241 ) {
242 continue;
243 }
244
245 $languages_id_ordered[] = $language_id;
246 break;
247
248 }
249 }
250
251 return $languages_id_ordered;
252 }
253
254 return false;
255 }
256
257
258 /**
259 * Get enabled features from wpLingua API data
260 * ('search', 'commercial', 'detection')
261 *
262 * @return array
263 */
264 function wplng_get_api_feature() {
265
266 $data = wplng_get_api_data();
267 $all = array( 'search', 'commercial', 'detection' );
268 $features = array();
269
270 if ( ! empty( $data['features'] ) && is_array( $data['features'] ) ) {
271 foreach ( $data['features'] as $feature_name => $feature_allow ) {
272 if ( $feature_allow && in_array( $feature_name, $all ) ) {
273 $features[] = $feature_name;
274 }
275 }
276 }
277
278 return $features;
279 }
280
281
282 /**
283 * Return true if the feature is enable in wpLingua API key data
284 *
285 * @param string $feature_name
286 * @return bool
287 */
288 function wplng_api_feature_is_allow( $feature_name ) {
289 return in_array( $feature_name, wplng_get_api_feature() );
290 }
291
292
293 /**
294 * Check if the wpLingua API is overloaded
295 *
296 * This function determines whether the wpLingua API is currently overloaded
297 * based on a stored timestamp. If the API is overloaded, it returns true.
298 * Otherwise, it clears the overloaded status and returns false.
299 *
300 * @global bool|null $wplng_api_is_overloaded Cached overloaded status
301 * @return bool True if the API is overloaded, false otherwise
302 */
303 function wplng_get_api_overloaded() {
304
305 global $wplng_api_is_overloaded;
306
307 // If overload is true, return directly
308 // Else, recheck the value
309 if ( ! empty( $wplng_api_is_overloaded ) ) {
310 return $wplng_api_is_overloaded;
311 }
312
313 $overloaded = get_option( 'wplng_api_overloaded' );
314
315 if ( ! empty( $overloaded )
316 && ( $overloaded + ( 2 * MINUTE_IN_SECONDS ) ) > time()
317 ) {
318 $wplng_api_is_overloaded = true;
319 } else {
320 $wplng_api_is_overloaded = false;
321 delete_option( 'wplng_api_overloaded' );
322 }
323
324 return $wplng_api_is_overloaded;
325 }
326
327
328 /**
329 * Mark the wpLingua API as overloaded
330 *
331 * This function sets the overloaded status for the wpLingua API by updating
332 * a timestamp in the database. If the API is already marked as overloaded,
333 * the function does nothing.
334 *
335 * @global bool|null $wplng_api_is_overloaded Cached overloaded status
336 * @return void
337 */
338 function wplng_set_api_overloaded() {
339
340 if ( wplng_get_api_overloaded() ) {
341 return;
342 }
343
344 global $wplng_api_is_overloaded;
345 $wplng_api_is_overloaded = true;
346
347 update_option(
348 'wplng_api_overloaded',
349 time(),
350 true
351 );
352
353 wp_cache_flush();
354 }
355
356
357 /**
358 * Clear wpLingua API key data if wpLingua key is changed
359 *
360 * @param string $old_value JSON
361 * @param string $new_value JSON
362 * @return void
363 */
364 function wplng_on_update_option_wplng_api_key( $old_value, $new_value ) {
365
366 delete_option( 'wplng_api_key_data' );
367 delete_option( 'wplng_website_language' );
368 delete_option( 'wplng_website_flag' );
369
370 wplng_clear_translations_cache();
371 wplng_clear_slugs_cache();
372 }
373