PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.1.0
Translate and Go multilingual – Automatic AI translation – wpLingua v1.1.0
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
wplingua / inc / api-call / translate.php

translate.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.1.0, at inc/api-call/translate.php

182 lines 3.9 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 * 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 /**
91 * Add dictionary tag
92 */
93
94 $dictionary_entries = wplng_dictionary_get_entries();
95
96 $texts_tagged = wplng_dictionary_add_tags(
97 $texts,
98 $dictionary_entries
99 );
100
101 /**
102 * Get texts list as JSON
103 */
104
105 $json_texts = wp_json_encode( $texts_tagged );
106
107 if ( empty( $json_texts ) ) {
108 return $texts;
109 }
110
111 /**
112 * Get the API call
113 */
114
115 $body = array(
116 'request' => 'translate',
117 'api_key' => $api_key,
118 'version' => WPLNG_API_VERSION,
119 'source' => $language_source_id,
120 'target' => $language_target_id,
121 'texts' => $json_texts,
122 );
123
124 $args = array(
125 'method' => 'POST',
126 'timeout' => 99, // 1 min 29 s
127 'sslverify' => WPLNG_API_SSLVERIFY,
128 'body' => $body,
129 );
130
131 $request = wp_remote_post(
132 WPLNG_API_URL . '/app/',
133 $args
134 );
135
136 /**
137 * Check if the API call worked
138 */
139
140 if ( is_wp_error( $request )
141 || wp_remote_retrieve_response_code( $request ) != 200
142 ) {
143 return $texts;
144 }
145
146 /**
147 * Check and sanitize the API response
148 */
149
150 $response = json_decode( wp_remote_retrieve_body( $request ), true );
151
152 if ( isset( $response['error'] )
153 || empty( $response['translations'] )
154 || ! is_array( $response['translations'] )
155 ) {
156 // API returned an error or an unexpected response
157 return $texts;
158 }
159
160 /**
161 * Here, API returned the list of translations
162 */
163
164 // Replace dictionary tag
165
166 $texts_untagged = wplng_dictionary_replace_tags( $response['translations'], $dictionary_entries );
167
168 // Check and sanitize each translation
169
170 $translations = array();
171
172 foreach ( $texts_untagged as $key => $translation ) {
173 if ( is_string( $translation ) ) {
174 $translations[] = wp_kses( $translation, array() );
175 } elseif ( isset( $texts[ $key ] ) ) {
176 $translations = $texts[ $key ];
177 }
178 }
179
180 return $translations;
181 }
182