| 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 |
* - source : A language ID |
| 24 |
* - target : A languages ID |
| 25 |
* - texts : Array of untranslated texts of website |
| 26 |
* |
| 27 |
* --------------------------------------------------- |
| 28 |
* Data received if successful : |
| 29 |
* --------------------------------------------------- |
| 30 |
* - translations : Array of translated texts |
| 31 |
* |
| 32 |
* --------------------------------------------------- |
| 33 |
* Data received in case of failure |
| 34 |
* --------------------------------------------------- |
| 35 |
* - error : true |
| 36 |
* - code : An integer |
| 37 |
* - message : Error description |
| 38 |
* |
| 39 |
* @param array $texts |
| 40 |
* @param string $language_source_id |
| 41 |
* @param string $language_target_id |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
function wplng_api_call_translate( |
| 45 |
$texts, |
| 46 |
$language_source_id = '', |
| 47 |
$language_target_id = '' |
| 48 |
) { |
| 49 |
|
| 50 |
/** |
| 51 |
* Get and check data |
| 52 |
*/ |
| 53 |
|
| 54 |
// Ckeck and sanitize texts list |
| 55 |
|
| 56 |
if ( empty( $texts ) || ! is_array( $texts ) ) { |
| 57 |
return array(); |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $texts as $key => $text ) { |
| 61 |
if ( ! is_string( $text ) ) { |
| 62 |
$texts[ $key ] = ''; |
| 63 |
} else { |
| 64 |
$texts[ $key ] = esc_html( $text ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Get API key |
| 69 |
|
| 70 |
$api_key = wplng_get_api_key(); |
| 71 |
|
| 72 |
if ( empty( $api_key ) ) { |
| 73 |
return $texts; |
| 74 |
} |
| 75 |
|
| 76 |
// Get and sanitize the API key |
| 77 |
|
| 78 |
if ( empty( $language_target_id ) ) { |
| 79 |
$language_target_id = wplng_get_language_current_id(); |
| 80 |
} elseif ( ! wplng_is_valid_language_id( $language_target_id ) ) { |
| 81 |
return $texts; |
| 82 |
} |
| 83 |
|
| 84 |
if ( empty( $language_source_id ) ) { |
| 85 |
$language_source_id = wplng_get_language_website_id(); |
| 86 |
} elseif ( ! wplng_is_valid_language_id( $language_source_id ) ) { |
| 87 |
return $texts; |
| 88 |
} |
| 89 |
|
| 90 |
// Get texts list as JSON |
| 91 |
|
| 92 |
$json_texts = wp_json_encode( $texts ); |
| 93 |
|
| 94 |
if ( empty( $json_texts ) ) { |
| 95 |
return $texts; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the API call |
| 100 |
*/ |
| 101 |
|
| 102 |
$body = array( |
| 103 |
'request' => 'translate', |
| 104 |
'api_key' => $api_key, |
| 105 |
'version' => WPLNG_API_VERSION, |
| 106 |
'source' => $language_source_id, |
| 107 |
'target' => $language_target_id, |
| 108 |
'texts' => $json_texts, |
| 109 |
); |
| 110 |
|
| 111 |
$args = array( |
| 112 |
'method' => 'POST', |
| 113 |
'timeout' => 99, // 1 min 29 s |
| 114 |
'sslverify' => WPLNG_API_SSLVERIFY, |
| 115 |
'body' => $body, |
| 116 |
); |
| 117 |
|
| 118 |
$request = wp_remote_post( |
| 119 |
WPLNG_API_URL . '/app/', |
| 120 |
$args |
| 121 |
); |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if the API call worked |
| 125 |
*/ |
| 126 |
|
| 127 |
if ( is_wp_error( $request ) |
| 128 |
|| wp_remote_retrieve_response_code( $request ) != 200 |
| 129 |
) { |
| 130 |
return $texts; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Check and sanitize the API response |
| 135 |
*/ |
| 136 |
|
| 137 |
$response = json_decode( wp_remote_retrieve_body( $request ), true ); |
| 138 |
|
| 139 |
if ( isset( $response['error'] ) |
| 140 |
|| empty( $response['translations'] ) |
| 141 |
|| ! is_array( $response['translations'] ) |
| 142 |
) { |
| 143 |
// API returned an error or an unexpected response |
| 144 |
return $texts; |
| 145 |
} |
| 146 |
|
| 147 |
// API returned the list of translations |
| 148 |
// Check and sanitize each translation |
| 149 |
|
| 150 |
$translations = array(); |
| 151 |
|
| 152 |
foreach ( $response['translations'] as $key => $translation ) { |
| 153 |
if ( is_string( $translation ) ) { |
| 154 |
$translations[] = wp_kses( $translation, array() ); |
| 155 |
} elseif ( isset( $texts[ $key ] ) ) { |
| 156 |
$translations = $texts[ $key ]; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $translations; |
| 161 |
} |
| 162 |
|