PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.7
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.7
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
← All changes | inc/api-key.php +177 -23 1.1.02.16.7 View file →
@@ -13,14 +13,13 @@
13 13 * @return bool
14 14 */
15 15 function wplng_is_valid_api_key_format( $api_key ) {
16 16
17 - $regex = '/^[a-zA-Z1-9]{42}$/s';
17 + $regex = '/^[a-zA-Z0-9]{42}$/s';
18 18
19 19 if (
20 20 empty( $api_key )
21 21 || ! is_string( $api_key )
22 - || $api_key !== preg_quote( $api_key )
23 22 || false === preg_match( $regex, $api_key )
24 23 ) {
25 24 return false;
26 25 }
@@ -49,22 +48,86 @@
49 48 /**
50 49 * Get wpLingua API key data
51 50 * - Website language (id or 'all')
52 51 * - Target language(s) (array)
53 - * - Enaled features
52 + * - Enabled features
54 53 *
55 54 * @return array
56 55 */
57 -function wplng_get_api_data() {
56 +function wplng_get_api_data( $try_update_from_api = false ) {
58 57
58 + global $wplng_api_data;
59 +
60 + if ( $wplng_api_data !== null && $try_update_from_api === false ) {
61 + return $wplng_api_data;
62 + }
63 +
59 64 if ( empty( wplng_get_api_key() ) ) {
60 65 return array();
61 66 }
62 67
63 - $data_checked = array();
64 - $data = get_transient( 'wplng_api_key_data' );
65 - $data = json_decode( $data, true );
68 + /**
69 + * Get data from cache or API
70 + */
66 71
72 + $data_checked = array();
73 + $data = array();
74 + $data_from_cache = get_option( 'wplng_api_key_data' );
75 +
76 + if ( ! empty( $data_from_cache ) ) {
77 + $data_from_cache = json_decode( $data_from_cache, true );
78 + if ( ! is_array( $data_from_cache ) ) {
79 + $data_from_cache = array();
80 + }
81 + }
82 +
83 + if ( empty( $data_from_cache ) ) {
84 +
85 + /**
86 + * No data in cache
87 + */
88 +
89 + // Try to get update from API
90 + $data = wplng_api_call_validate_api_key();
91 +
92 + // If no data in cache and no data from API, return empty array
93 + if ( empty( $data ) ) {
94 + return array();
95 + }
96 +
97 + $data['time'] = time();
98 +
99 + } else {
100 +
101 + /**
102 + * Data is in cache
103 + */
104 +
105 + $data = $data_from_cache;
106 +
107 + // Time of last update is not define, set is as current time
108 + if ( empty( $data['time'] )
109 + || ! is_int( $data['time'] )
110 + ) {
111 + $data['time'] = time();
112 + }
113 +
114 + // Time of last update older than 8 hours
115 + // Or $try_update_from_api is true
116 + // Try to get update from API
117 + if ( ( $data['time'] + ( 8 * HOUR_IN_SECONDS ) ) <= time()
118 + || $try_update_from_api === true
119 + ) {
120 +
121 + $data_from_api = wplng_api_call_validate_api_key();
122 +
123 + if ( ! empty( $data_from_api ) ) {
124 + $data = $data_from_api;
125 + $data['time'] = time();
126 + }
127 + }
128 + }
129 +
67 130 /**
68 131 * Check the API key data
69 132 */
70 133
@@ -76,8 +139,11 @@
76 139 && ! empty( $data['languages_target'] )
77 140 && wplng_is_valid_language_ids( $data['languages_target'] )
78 141 && isset( $data['features'] )
79 142 && is_array( $data['features'] )
143 + && ! empty( $data['status'] )
144 + && ! empty( $data['time'] )
145 + && is_int( $data['time'] )
80 146 ) {
81 147
82 148 /**
83 149 * Sanitize languages target
@@ -112,8 +178,20 @@
112 178 $features[ $key ] = $allow;
113 179 }
114 180
115 181 /**
182 + * Sanitize status
183 + */
184 +
185 + $status = 'FREE';
186 +
187 + if ( 'PREMIUM' === $data['status']
188 + || 'VIP' === $data['status']
189 + ) {
190 + $status = $data['status'];
191 + }
192 +
193 + /**
116 194 * Make the checked response
117 195 */
118 196
119 197 $data_checked = array(
@@ -119,29 +197,41 @@
119 197 $data_checked = array(
120 198 'language_original' => sanitize_key( $data['language_original'] ),
121 199 'languages_target' => $languages_target,
122 200 'features' => $features,
201 + 'status' => $status,
202 + 'time' => $data['time'],
123 203 );
124 204
125 - } else {
126 -
127 205 /**
128 - * Get sanitized data from API call
206 + * Add expiration
129 207 */
130 208
131 - $data_checked = wplng_api_call_validate_api_key();
132 -
133 - if ( empty( $data_checked ) ) {
134 - return array();
209 + if ( ! empty( $data['expiration'] )
210 + && is_string( $data['expiration'] )
211 + ) {
212 + $data_checked['expiration'] = sanitize_text_field(
213 + $data['expiration']
214 + );
135 215 }
216 + }
136 217
137 - set_transient(
218 + // Set data in DB cache (option)
219 + if ( ! empty( $data_checked )
220 + && $data_checked !== $data_from_cache
221 + ) {
222 + update_option(
138 223 'wplng_api_key_data',
139 224 wp_json_encode( $data_checked ),
140 - 60 * 60 * 24
225 + true
141 226 );
227 +
228 + wp_cache_flush();
142 229 }
143 230
231 + // Set data for global variable
232 + $wplng_api_data = $data_checked;
233 +
144 234 return $data_checked;
145 235 }
146 236
147 237
@@ -197,8 +287,9 @@
197 287 continue;
198 288 }
199 289
200 290 $languages_id_ordered[] = $language_id;
291 + break;
201 292
202 293 }
203 294 }
204 295
@@ -210,9 +301,9 @@
210 301
211 302
212 303 /**
213 304 * Get enabled features from wpLingua API data
214 - * ('search', 'woocommerce')
305 + * ('search', 'commercial', 'detection')
215 306 *
216 307 * @return array
217 308 */
218 309 function wplng_get_api_feature() {
@@ -217,14 +308,14 @@
217 308 */
218 309 function wplng_get_api_feature() {
219 310
220 311 $data = wplng_get_api_data();
221 - $all = array( 'search', 'woocommerce' );
312 + $all = array( 'search', 'commercial', 'detection' );
222 313 $features = array();
223 314
224 315 if ( ! empty( $data['features'] ) && is_array( $data['features'] ) ) {
225 316 foreach ( $data['features'] as $feature_name => $feature_allow ) {
226 - if ( $feature_allow && in_array( $feature_name, $all ) ) {
317 + if ( $feature_allow && in_array( $feature_name, $all, true ) ) {
227 318 $features[] = $feature_name;
228 319 }
229 320 }
230 321 }
@@ -239,13 +330,77 @@
239 330 * @param string $feature_name
240 331 * @return bool
241 332 */
242 333 function wplng_api_feature_is_allow( $feature_name ) {
243 - return in_array( $feature_name, wplng_get_api_feature() );
334 + return in_array( $feature_name, wplng_get_api_feature(), true );
244 335 }
245 336
246 337
247 338 /**
339 + * Check if the wpLingua API is overloaded
340 + *
341 + * This function determines whether the wpLingua API is currently overloaded
342 + * based on a stored timestamp. If the API is overloaded, it returns true.
343 + * Otherwise, it clears the overloaded status and returns false.
344 + *
345 + * @global bool|null $wplng_api_is_overloaded Cached overloaded status
346 + * @return bool True if the API is overloaded, false otherwise
347 + */
348 +function wplng_get_api_overloaded() {
349 +
350 + global $wplng_api_is_overloaded;
351 +
352 + // If overload is true, return directly
353 + // Else, recheck the value
354 + if ( ! empty( $wplng_api_is_overloaded ) ) {
355 + return $wplng_api_is_overloaded;
356 + }
357 +
358 + $overloaded = get_option( 'wplng_api_overloaded' );
359 +
360 + if ( ! empty( $overloaded )
361 + && ( $overloaded + ( 2 * MINUTE_IN_SECONDS ) ) > time()
362 + ) {
363 + $wplng_api_is_overloaded = true;
364 + } else {
365 + $wplng_api_is_overloaded = false;
366 + delete_option( 'wplng_api_overloaded' );
367 + }
368 +
369 + return $wplng_api_is_overloaded;
370 +}
371 +
372 +
373 +/**
374 + * Mark the wpLingua API as overloaded
375 + *
376 + * This function sets the overloaded status for the wpLingua API by updating
377 + * a timestamp in the database. If the API is already marked as overloaded,
378 + * the function does nothing.
379 + *
380 + * @global bool|null $wplng_api_is_overloaded Cached overloaded status
381 + * @return void
382 + */
383 +function wplng_set_api_overloaded() {
384 +
385 + if ( wplng_get_api_overloaded() ) {
386 + return;
387 + }
388 +
389 + global $wplng_api_is_overloaded;
390 + $wplng_api_is_overloaded = true;
391 +
392 + update_option(
393 + 'wplng_api_overloaded',
394 + time(),
395 + true
396 + );
397 +
398 + wp_cache_flush();
399 +}
400 +
401 +
402 +/**
248 403 * Clear wpLingua API key data if wpLingua key is changed
249 404 *
250 405 * @param string $old_value JSON
251 406 * @param string $new_value JSON
@@ -252,12 +407,11 @@
252 407 * @return void
253 408 */
254 409 function wplng_on_update_option_wplng_api_key( $old_value, $new_value ) {
255 410
256 - delete_transient( 'wplng_api_key_data' );
257 -
411 + delete_option( 'wplng_api_key_data' );
258 412 delete_option( 'wplng_website_language' );
259 413 delete_option( 'wplng_website_flag' );
260 414
261 415 wplng_clear_translations_cache();
262 -
416 + wplng_clear_slugs_cache();
263 417 }