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