PluginProbe
Translate and boost SEO – Automatic AI translation – wpLingua / 2.16.8
Translate and boost SEO – Automatic AI translation – wpLingua v2.16.8
2.16.8 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 All 113 releases
← All changes | inc/api-key.php +417 -417 2.16.12.16.8 View file →
@@ -1,417 +1,417 @@
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-Z0-9]{42}$/s';
18 -
19 - if (
20 - empty( $api_key )
21 - || ! is_string( $api_key )
22 - || false === preg_match( $regex, $api_key )
23 - ) {
24 - return false;
25 - }
26 -
27 - return true;
28 -}
29 -
30 -
31 -/**
32 - * Get the wpLingua registered API key
33 - *
34 - * @return string
35 - */
36 -function wplng_get_api_key() {
37 -
38 - $api_key = trim( get_option( 'wplng_api_key' ) );
39 -
40 - if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
41 - $api_key = '';
42 - }
43 -
44 - return $api_key;
45 -}
46 -
47 -
48 -/**
49 - * Get wpLingua API key data
50 - * - Website language (id or 'all')
51 - * - Target language(s) (array)
52 - * - Enabled features
53 - *
54 - * @return array
55 - */
56 -function wplng_get_api_data( $try_update_from_api = false ) {
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 -
64 - if ( empty( wplng_get_api_key() ) ) {
65 - return array();
66 - }
67 -
68 - /**
69 - * Get data from cache or API
70 - */
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 -
130 - /**
131 - * Check the API key data
132 - */
133 -
134 - if ( ! empty( $data['language_original'] )
135 - && (
136 - wplng_is_valid_language_id( $data['language_original'] )
137 - || 'all' === $data['language_original']
138 - )
139 - && ! empty( $data['languages_target'] )
140 - && wplng_is_valid_language_ids( $data['languages_target'] )
141 - && isset( $data['features'] )
142 - && is_array( $data['features'] )
143 - && ! empty( $data['status'] )
144 - && ! empty( $data['time'] )
145 - && is_int( $data['time'] )
146 - ) {
147 -
148 - /**
149 - * Sanitize languages target
150 - */
151 -
152 - $languages_target = array();
153 -
154 - foreach ( $data['languages_target'] as $id ) {
155 -
156 - if ( ! wplng_is_valid_language_id( $id ) ) {
157 - continue;
158 - }
159 -
160 - $languages_target[] = sanitize_key( $id );
161 - }
162 -
163 - /**
164 - * Sanitize features list
165 - */
166 -
167 - $features = array();
168 -
169 - foreach ( $data['features'] as $key => $allow ) {
170 -
171 - if ( ! is_string( $key ) || ! is_bool( $allow ) ) {
172 - continue;
173 - }
174 -
175 - $key = sanitize_key( $key );
176 - $allow = ( true === $allow );
177 -
178 - $features[ $key ] = $allow;
179 - }
180 -
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 - /**
194 - * Make the checked response
195 - */
196 -
197 - $data_checked = array(
198 - 'language_original' => sanitize_key( $data['language_original'] ),
199 - 'languages_target' => $languages_target,
200 - 'features' => $features,
201 - 'status' => $status,
202 - 'time' => $data['time'],
203 - );
204 -
205 - /**
206 - * Add expiration
207 - */
208 -
209 - if ( ! empty( $data['expiration'] )
210 - && is_string( $data['expiration'] )
211 - ) {
212 - $data_checked['expiration'] = sanitize_text_field(
213 - $data['expiration']
214 - );
215 - }
216 - }
217 -
218 - // Set data in DB cache (option)
219 - if ( ! empty( $data_checked )
220 - && $data_checked !== $data_from_cache
221 - ) {
222 - update_option(
223 - 'wplng_api_key_data',
224 - wp_json_encode( $data_checked ),
225 - true
226 - );
227 -
228 - wp_cache_flush();
229 - }
230 -
231 - // Set data for global variable
232 - $wplng_api_data = $data_checked;
233 -
234 - return $data_checked;
235 -}
236 -
237 -
238 -/**
239 - * Get website language from wpLingua API data
240 - *
241 - * @return string Language ID, 'all' or ''
242 - */
243 -function wplng_get_api_language_website() {
244 -
245 - $data = wplng_get_api_data();
246 -
247 - if (
248 - ! empty( $data['language_original'] )
249 - && (
250 - wplng_is_valid_language_id( $data['language_original'] )
251 - || 'all' === $data['language_original']
252 - )
253 - ) {
254 - return $data['language_original'];
255 - }
256 -
257 - return '';
258 -}
259 -
260 -
261 -/**
262 - * Get target languages from wpLingua API data
263 - *
264 - * @return mixed Language IDs array, 'all' or false
265 - */
266 -function wplng_get_api_languages_target() {
267 -
268 - $data = wplng_get_api_data();
269 -
270 - if ( empty( $data['languages_target'] ) ) {
271 - return false;
272 - } elseif ( 'all' === $data['languages_target'] ) {
273 - return 'all';
274 - } elseif ( is_array( $data['languages_target'] ) ) {
275 -
276 - $all_languages = wplng_get_languages_all();
277 - $languages_id_ordered = array();
278 -
279 - foreach ( $all_languages as $language ) {
280 - foreach ( $data['languages_target'] as $language_id ) {
281 -
282 - if (
283 - ! wplng_is_valid_language_id( $language_id )
284 - || empty( $language['id'] )
285 - || $language['id'] !== $language_id
286 - ) {
287 - continue;
288 - }
289 -
290 - $languages_id_ordered[] = $language_id;
291 - break;
292 -
293 - }
294 - }
295 -
296 - return $languages_id_ordered;
297 - }
298 -
299 - return false;
300 -}
301 -
302 -
303 -/**
304 - * Get enabled features from wpLingua API data
305 - * ('search', 'commercial', 'detection')
306 - *
307 - * @return array
308 - */
309 -function wplng_get_api_feature() {
310 -
311 - $data = wplng_get_api_data();
312 - $all = array( 'search', 'commercial', 'detection' );
313 - $features = array();
314 -
315 - if ( ! empty( $data['features'] ) && is_array( $data['features'] ) ) {
316 - foreach ( $data['features'] as $feature_name => $feature_allow ) {
317 - if ( $feature_allow && in_array( $feature_name, $all, true ) ) {
318 - $features[] = $feature_name;
319 - }
320 - }
321 - }
322 -
323 - return $features;
324 -}
325 -
326 -
327 -/**
328 - * Return true if the feature is enable in wpLingua API key data
329 - *
330 - * @param string $feature_name
331 - * @return bool
332 - */
333 -function wplng_api_feature_is_allow( $feature_name ) {
334 - return in_array( $feature_name, wplng_get_api_feature(), true );
335 -}
336 -
337 -
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 -/**
403 - * Clear wpLingua API key data if wpLingua key is changed
404 - *
405 - * @param string $old_value JSON
406 - * @param string $new_value JSON
407 - * @return void
408 - */
409 -function wplng_on_update_option_wplng_api_key( $old_value, $new_value ) {
410 -
411 - delete_option( 'wplng_api_key_data' );
412 - delete_option( 'wplng_website_language' );
413 - delete_option( 'wplng_website_flag' );
414 -
415 - wplng_clear_translations_cache();
416 - wplng_clear_slugs_cache();
417 -}
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-Z0-9]{42}$/s';
18 +
19 + if (
20 + empty( $api_key )
21 + || ! is_string( $api_key )
22 + || false === preg_match( $regex, $api_key )
23 + ) {
24 + return false;
25 + }
26 +
27 + return true;
28 +}
29 +
30 +
31 +/**
32 + * Get the wpLingua registered API key
33 + *
34 + * @return string
35 + */
36 +function wplng_get_api_key() {
37 +
38 + $api_key = trim( get_option( 'wplng_api_key' ) );
39 +
40 + if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
41 + $api_key = '';
42 + }
43 +
44 + return $api_key;
45 +}
46 +
47 +
48 +/**
49 + * Get wpLingua API key data
50 + * - Website language (id or 'all')
51 + * - Target language(s) (array)
52 + * - Enabled features
53 + *
54 + * @return array
55 + */
56 +function wplng_get_api_data( $try_update_from_api = false ) {
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 +
64 + if ( empty( wplng_get_api_key() ) ) {
65 + return array();
66 + }
67 +
68 + /**
69 + * Get data from cache or API
70 + */
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 +
130 + /**
131 + * Check the API key data
132 + */
133 +
134 + if ( ! empty( $data['language_original'] )
135 + && (
136 + wplng_is_valid_language_id( $data['language_original'] )
137 + || 'all' === $data['language_original']
138 + )
139 + && ! empty( $data['languages_target'] )
140 + && wplng_is_valid_language_ids( $data['languages_target'] )
141 + && isset( $data['features'] )
142 + && is_array( $data['features'] )
143 + && ! empty( $data['status'] )
144 + && ! empty( $data['time'] )
145 + && is_int( $data['time'] )
146 + ) {
147 +
148 + /**
149 + * Sanitize languages target
150 + */
151 +
152 + $languages_target = array();
153 +
154 + foreach ( $data['languages_target'] as $id ) {
155 +
156 + if ( ! wplng_is_valid_language_id( $id ) ) {
157 + continue;
158 + }
159 +
160 + $languages_target[] = sanitize_key( $id );
161 + }
162 +
163 + /**
164 + * Sanitize features list
165 + */
166 +
167 + $features = array();
168 +
169 + foreach ( $data['features'] as $key => $allow ) {
170 +
171 + if ( ! is_string( $key ) || ! is_bool( $allow ) ) {
172 + continue;
173 + }
174 +
175 + $key = sanitize_key( $key );
176 + $allow = ( true === $allow );
177 +
178 + $features[ $key ] = $allow;
179 + }
180 +
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 + /**
194 + * Make the checked response
195 + */
196 +
197 + $data_checked = array(
198 + 'language_original' => sanitize_key( $data['language_original'] ),
199 + 'languages_target' => $languages_target,
200 + 'features' => $features,
201 + 'status' => $status,
202 + 'time' => $data['time'],
203 + );
204 +
205 + /**
206 + * Add expiration
207 + */
208 +
209 + if ( ! empty( $data['expiration'] )
210 + && is_string( $data['expiration'] )
211 + ) {
212 + $data_checked['expiration'] = sanitize_text_field(
213 + $data['expiration']
214 + );
215 + }
216 + }
217 +
218 + // Set data in DB cache (option)
219 + if ( ! empty( $data_checked )
220 + && $data_checked !== $data_from_cache
221 + ) {
222 + update_option(
223 + 'wplng_api_key_data',
224 + wp_json_encode( $data_checked ),
225 + true
226 + );
227 +
228 + wp_cache_flush();
229 + }
230 +
231 + // Set data for global variable
232 + $wplng_api_data = $data_checked;
233 +
234 + return $data_checked;
235 +}
236 +
237 +
238 +/**
239 + * Get website language from wpLingua API data
240 + *
241 + * @return string Language ID, 'all' or ''
242 + */
243 +function wplng_get_api_language_website() {
244 +
245 + $data = wplng_get_api_data();
246 +
247 + if (
248 + ! empty( $data['language_original'] )
249 + && (
250 + wplng_is_valid_language_id( $data['language_original'] )
251 + || 'all' === $data['language_original']
252 + )
253 + ) {
254 + return $data['language_original'];
255 + }
256 +
257 + return '';
258 +}
259 +
260 +
261 +/**
262 + * Get target languages from wpLingua API data
263 + *
264 + * @return mixed Language IDs array, 'all' or false
265 + */
266 +function wplng_get_api_languages_target() {
267 +
268 + $data = wplng_get_api_data();
269 +
270 + if ( empty( $data['languages_target'] ) ) {
271 + return false;
272 + } elseif ( 'all' === $data['languages_target'] ) {
273 + return 'all';
274 + } elseif ( is_array( $data['languages_target'] ) ) {
275 +
276 + $all_languages = wplng_get_languages_all();
277 + $languages_id_ordered = array();
278 +
279 + foreach ( $all_languages as $language ) {
280 + foreach ( $data['languages_target'] as $language_id ) {
281 +
282 + if (
283 + ! wplng_is_valid_language_id( $language_id )
284 + || empty( $language['id'] )
285 + || $language['id'] !== $language_id
286 + ) {
287 + continue;
288 + }
289 +
290 + $languages_id_ordered[] = $language_id;
291 + break;
292 +
293 + }
294 + }
295 +
296 + return $languages_id_ordered;
297 + }
298 +
299 + return false;
300 +}
301 +
302 +
303 +/**
304 + * Get enabled features from wpLingua API data
305 + * ('search', 'commercial', 'detection')
306 + *
307 + * @return array
308 + */
309 +function wplng_get_api_feature() {
310 +
311 + $data = wplng_get_api_data();
312 + $all = array( 'search', 'commercial', 'detection' );
313 + $features = array();
314 +
315 + if ( ! empty( $data['features'] ) && is_array( $data['features'] ) ) {
316 + foreach ( $data['features'] as $feature_name => $feature_allow ) {
317 + if ( $feature_allow && in_array( $feature_name, $all, true ) ) {
318 + $features[] = $feature_name;
319 + }
320 + }
321 + }
322 +
323 + return $features;
324 +}
325 +
326 +
327 +/**
328 + * Return true if the feature is enable in wpLingua API key data
329 + *
330 + * @param string $feature_name
331 + * @return bool
332 + */
333 +function wplng_api_feature_is_allow( $feature_name ) {
334 + return in_array( $feature_name, wplng_get_api_feature(), true );
335 +}
336 +
337 +
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 +/**
403 + * Clear wpLingua API key data if wpLingua key is changed
404 + *
405 + * @param string $old_value JSON
406 + * @param string $new_value JSON
407 + * @return void
408 + */
409 +function wplng_on_update_option_wplng_api_key( $old_value, $new_value ) {
410 +
411 + delete_option( 'wplng_api_key_data' );
412 + delete_option( 'wplng_website_language' );
413 + delete_option( 'wplng_website_flag' );
414 +
415 + wplng_clear_translations_cache();
416 + wplng_clear_slugs_cache();
417 +}