PluginProbe
Translation with DeepL API / 2.4.5
Translation with DeepL API v2.4.5
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
← All changes | client/deeplapi.class.php +163 -102 1.7.52.4.5 View file →
@@ -1,13 +1,22 @@
1 1 <?php
2 2
3 3 abstract class DeepLApi extends DeepLData {
4 4 protected $endPoint = '';
5 + protected $http_mode = '';
5 6 private $endPointURL = false;
6 7 protected $authKey = '';
7 8 protected $log = false;
8 9 protected $headers = array();
9 10
11 + public $languages;
12 + public $doing_cron;
13 + public $instance;
14 + public $result;
15 + public $why_no_cache;
16 + public $final_request;
17 + public $format;
18 +
10 19 protected $cache_dir = false;
11 20
12 21 protected $request = array();
13 22 protected $uniqid = false;
@@ -33,9 +42,9 @@
33 42 $this->log = $log;
34 43
35 44 $this->languages = DeepLConfiguration::DefaultsAllLanguages();
36 45
37 - if( defined( 'DOING_CRON' ) && DOING_CRON ) {
46 + if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
38 47 $this->doing_cron = true;
39 48 }
40 49 else {
41 50 $this->doing_cron = false;
@@ -45,8 +54,12 @@
45 54
46 55 $this->cache_dir = $this->getCacheDirectory();
47 56 }
48 57
58 + public function allowCache( $allow_cache ) {
59 + $this->allow_cache = filter_var( $allow_cache, FILTER_VALIDATE_BOOLEAN );
60 + }
61 +
49 62 public function wasItCached() {
50 63 return $this->response_type === 'cache';
51 64 }
52 65
@@ -53,59 +66,15 @@
53 66 public function getTimeElapsed() {
54 67 return $this->request_microtime;
55 68 }
56 69
57 - protected function buildQuery( $mode = 'POST' ) {
58 - $args = array();
70 + abstract public function buildBody( $mode, $endPoint );
59 71
60 - if( count( $this->headers ) )
61 - $args['headers'] = $this->headers;
62 -
63 - //$request = array( 'auth_key' => $this->authKey );
64 -
65 - $request = array();
66 - if( $this->request ) {
67 - foreach( $this->request as $key => $value ) {
68 - $request[$key] = $value;
69 - }
70 - }
71 -
72 - if( $mode == 'POST' ) {
73 - // fixed in 0.3 by schalipp https://wordpress.org/support/topic/translate-button-not-doing-anything/#post-10825822
74 - // modified in 1.0 to send all strings in one request
75 -
76 - $bits = array();
77 - foreach( $request as $key => $value ) {
78 - if( is_array( $value ) ) {
79 - foreach( $value as $sub_key => $sub_value ) {
80 - $bits[] = "$key=$sub_value";
81 - }
82 - }
83 - else {
84 - $bits[] = "$key=$value";
85 - }
86 - }
87 -
88 - $args['body'] = implode( '&', $bits );
89 - //$args['body'] = http_build_query( $request );
90 - }
91 - else {
92 - $request['auth_key'] = $this->authKey;
93 - $args['query'] = http_build_query( $request );
94 - }
95 -
96 - $args['timeout'] = self::TIMEOUT;
97 -
98 - $this->final_request = $args;
99 - $this->saveRequest();
100 - return $args;
101 - }
102 -
103 72 public function getRequestUniqueID() {
104 - if( $this->cache_id ) {
73 + if ( $this->cache_id ) {
105 74 $this->uniqid = $this->cache_id;
106 75 }
107 - elseif( !$this->uniqid ) {
76 + elseif ( !$this->uniqid ) {
108 77 $this->uniqid = microtime();
109 78 }
110 79 return $this->uniqid;
111 80 }
@@ -118,16 +87,31 @@
118 87 protected function maybeLimitRequestSize() {
119 88 }
120 89 */
121 90 protected function saveRequest() {
122 - if( isset( $this->request_cache_file ) ) {
91 + if ( isset( $this->request_cache_file ) ) {
123 92 file_put_contents( $this->request_cache_file, json_encode( $this->final_request ) );
124 93 }
125 94 }
126 - protected function doRequest( $mode = 'POST' ) {
127 - if( $mode == 'GET' ) {
95 +
96 + protected function addHeaders( $headers ) {
97 + return $headers;
98 + }
99 +
100 + protected function doRequest( $mode = false ) {
101 + if( !$mode )
102 + $mode = $this->http_mode;
103 +
104 +
105 + if ( $mode == 'GET' ) {
128 106 $response = $this->doGETRequest();
129 107 }
108 + elseif( $mode == 'DELETE' ) {
109 + $response = $this->doGETRequest( 'DELETE' );
110 + }
111 + elseif( $mode == 'PUT' ) {
112 + $response = $this->doGETRequest( 'PUT' );
113 + }
130 114 else {
131 115 $response = $this->doPOSTRequest();
132 116 }
133 117
@@ -134,9 +118,9 @@
134 118 return $response;
135 119 }
136 120
137 121 static function getInstance() {
138 - return $this->instance;
122 + return self::instance;
139 123 }
140 124
141 125 protected function setCacheID( $idstring ) {
142 126 $this->cache_id = $idstring;
@@ -142,9 +126,9 @@
142 126 $this->cache_id = $idstring;
143 127 }
144 128
145 129 public function setCachePrefix( $prefix = '' ) {
146 - if( !empty( $prefix ) ) {
130 + if ( !empty( $prefix ) ) {
147 131 $this->cache_prefix = $prefix;
148 132 }
149 133 else {
150 134 $this->cache_prefix = '';
@@ -151,12 +135,12 @@
151 135 }
152 136 }
153 137
154 138 public function getCacheFile( $type ) {
155 - if( $type == 'response' ) {
139 + if ( $type == 'response' ) {
156 140 return $this->response_cache_file;
157 141 }
158 - elseif( $type == 'request' ) {
142 + elseif ( $type == 'request' ) {
159 143 return $this->request_cache_file;
160 144 }
161 145 }
162 146
@@ -161,9 +145,9 @@
161 145 }
162 146
163 147 public function setCacheNames() {
164 148 $cache_name = '';
165 - if( !empty( $this->cache_prefix ) ) {
149 + if ( !empty( $this->cache_prefix ) ) {
166 150 $cache_name .= $this->cache_prefix .'-';
167 151 }
168 152 $cache_name .= $this->cache_id;
169 153 $this->request_cache_file = trailingslashit( $this->cache_dir ) . $cache_name . '-request';
@@ -173,9 +157,9 @@
173 157 public function request() {
174 158
175 159 $log_bits = array('class' => get_class($this));
176 160
177 - if( $this->allow_cache )
161 + if ( $this->allow_cache )
178 162 $this->uniqid = $this->getRequestUniqueID();
179 163
180 164 $log_bits['uniqid'] = $this->uniqid;
181 165
@@ -180,26 +164,25 @@
180 164 $log_bits['uniqid'] = $this->uniqid;
181 165
182 166 $this->setCacheNames();
183 167 $this->result = false;
184 - //plouf( $this ); die( 'opzirjizerj' );
185 168
186 - if( $this->isCacheValid( $this->response_cache_file ) ) {
169 + if ( $this->isCacheValid( $this->response_cache_file ) ) {
187 170 $this->response_type = 'cache';
188 171 $this->why_no_cache = false;
189 172 $this->result = file_get_contents( $this->response_cache_file );
190 173 $log_bits['type'] = 'cached';
191 -
192 - //plouf( $this->result ); die('cache');
174 +
193 175 }
194 176 else {
195 - //die("no cache");
196 177 $log_bits['type'] = 'fresh';
197 178 $log_bits['mode'] = $this->http_mode;
198 179 $this->start_microtime = microtime( true );
199 180 //echo "\nIS FRESH";
200 181 $this->response_type = 'fresh';
201 - if( $this->http_mode == 'GET' ) {
182 +
183 +
184 + if ( $this->http_mode == 'GET' ) {
202 185 $response = $this->doRequest( 'GET' );
203 186 }
204 187 else {
205 188 $response = $this->doRequest( 'POST' );
@@ -208,36 +191,36 @@
208 191
209 192 $this->end_microtime = microtime( true );
210 193 $this->request_microtime = $this->end_microtime - $this->start_microtime;
211 194
212 - if( is_wp_error( $response ) ) {
195 + if ( is_wp_error( $response ) ) {
213 196 return $response->get_error_message();
214 197 }
215 198
216 - if( $response['response']['code'] > 299 ) {
199 + if ( $response['response']['code'] > 299 ) {
217 200 $log_bits['response_error'] = $response['response']['code'];
218 201 $log_bits['response'] = $response['response'];
219 - if( $response['response']['code'] == 429 ) {
202 + if ( $response['response']['code'] == 429 ) {
220 203 $log_bits['error_cause'] = 'Excessive request size';
221 204 }
222 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
205 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
223 206
224 207 $body = json_decode( $response['body'], true );
225 - $message = $body['message'];
208 + $message = isset( $body['message'] ) ? $body['message'] : false;
226 209 return new WP_Error( $message );
227 210 }
228 211
229 - if( array_key_exists( 'body', $response ) && strlen( $response['body'] ) )
212 + if ( array_key_exists( 'body', $response ) && strlen( $response['body'] ) )
230 213 $this->result = $response['body'];
231 214 else
232 215 $this->result = false;
233 216
234 - if( $this->result ) {
217 + if ( $this->result ) {
235 218 //echo " putting " . strlen( serialize( $this->result ) ) ." in "
236 219 try {
237 220 file_put_contents( $this->response_cache_file, $this->result );
238 221 } catch (Exception $e) {
239 -
222 +
240 223 }
241 224 }
242 225
243 226 $log_bits['request_microtime'] = $this->request_microtime;
@@ -242,22 +225,28 @@
242 225
243 226 $log_bits['request_microtime'] = $this->request_microtime;
244 227 }
245 228
246 - if( DeepLConfiguration::getLogLevel() ) {
247 - if( DeepLConfiguration::getLogLevel() > 1 ) {
248 - $log_bits['args'] = $this->request;
229 + if ( DeepLConfiguration::getLogLevel() ) {
230 + if ( DeepLConfiguration::getLogLevel() > 1 ) {
231 + foreach( $this->request as $key => $value ) {
232 + if( is_string( $value ) && strlen( $value ) > 500 )
233 + $log_bits['args'][$key] = "LEN=" . strlen( $value) .", string = " . substr( $value, 0, 100 );
234 + else
235 + $log_bits['args'][$key] = $value;
236 +
237 + }
249 238 }
250 239 else {
251 - foreach( array ('source_lang', 'target_lang') as $key ) {
252 - if( isset( $this->request[$key] ) ) {
240 + foreach ( array ('source_lang', 'target_lang') as $key ) {
241 + if ( isset( $this->request[$key] ) ) {
253 242 $log_bits[$key] = $this->request[$key];
254 243 }
255 244 }
256 245 }
257 - if( isset( $this->request['text'] ) ) {
246 + if ( isset( $this->request['text'] ) ) {
258 247 $text_size = 0;
259 - foreach( $this->request['text'] as $i => $string ) {
248 + foreach ( $this->request['text'] as $i => $string ) {
260 249 $text_size += strlen($string);
261 250 }
262 251 $log_bits['text_size'] = $text_size;
263 252 }
@@ -262,17 +251,17 @@
262 251 $log_bits['text_size'] = $text_size;
263 252 }
264 253 }
265 254
266 - if($this->response_type == 'fresh') {
255 + if ($this->response_type == 'fresh') {
267 256 // test if JSON
268 257 json_decode($this->result);
269 - if(json_last_error() == JSON_ERROR_NONE) {
258 + if (json_last_error() == JSON_ERROR_NONE) {
270 259 $log_bits['response'] = json_decode($this->result, 1 );
271 - if( DeepLConfiguration::getLogLevel() ) {
272 - if(isset( $log_bits['response']['translations'] ) ) {
260 + if ( DeepLConfiguration::getLogLevel() ) {
261 + if (isset( $log_bits['response']['translations'] ) ) {
273 262 $translated_text_size = 0;
274 - foreach( $log_bits['response']['translations'] as $i => $translation ) {
263 + foreach ( $log_bits['response']['translations'] as $i => $translation ) {
275 264 $translated_text_size += strlen( $translation['text'] );
276 265 }
277 266 $log_bits['translated_text_size'] = $translated_text_size;
278 267 }
@@ -280,9 +269,9 @@
280 269 }
281 270 else {
282 271 $log_bits['response'] = $this->result;
283 272 }
284 -
273 +
285 274 }
286 275 else {
287 276 $log_bits['response'] = '--hidden response because cached request--';
288 277
@@ -288,32 +277,32 @@
288 277
289 278 }
290 279 $log_bits['response_length'] = strlen($this->result);
291 280
292 - if( $this->result ) {
281 + if ( $this->result ) {
293 282 $this->result = json_decode( $this->result );
294 283
295 284 $this->result = ( array ) $this->result;
296 285 $this->result['response_type'] = $this->response_type;
297 - if( $this->why_no_cache ) {
286 + if ( $this->why_no_cache ) {
298 287 $log_bits['why_no_cache'] = $this->why_no_cache;
299 288 $this->result['why_no_cache'] = $this->why_no_cache;
300 289 }
301 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
290 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
302 291 return $this->result;
303 292 }
304 293 else {
305 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
294 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
306 295 return $this->response['response']['message'];
307 296 }
308 297 }
309 298
310 299 public function getRequestTime( $result_in_milliseconds = false ) {
311 - if( !$this->request_microtime ) {
300 + if ( !$this->request_microtime ) {
312 301 return false;
313 302 }
314 303
315 - if( $result_in_milliseconds ) {
304 + if ( $result_in_milliseconds ) {
316 305 return floatval( $this->request_microtime );
317 306 }
318 307 else {
319 308 return floatval( $this->request_microtime/1000 );
@@ -323,10 +312,10 @@
323 312 public function getEndPointURL( $add_auth_key = false ) {
324 313
325 314
326 315 $APIServer = DeeplConfiguration::getAPIServer();
327 - if( !$this->endPointURL ) {
328 - if( $this->endPoint === '' ) {
316 + if ( !$this->endPointURL ) {
317 + if ( $this->endPoint === '' ) {
329 318 $this->endPointURL = $APIServer;
330 319 }
331 320 else {
332 321 $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint;
@@ -332,9 +321,9 @@
332 321 $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint;
333 322 }
334 323 }
335 324
336 - if( $add_auth_key ) {
325 + if ( $add_auth_key ) {
337 326 $this->endPointURL .= '?auth_key=' . $this->authKey;
338 327 }
339 328
340 329 return $this->endPointURL;
@@ -344,30 +333,79 @@
344 333 *
345 334 * @since 0.1
346 335 */
347 336 public function doPOSTRequest( $args = array() ) {
348 - $add_auth_key = true;
349 337
350 - $this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
338 +
339 + $args = array(
340 + 'method' => 'POST',
341 + 'timeout' => self::TIMEOUT,
342 + );
343 +
344 + $args['headers'] = array(
345 + 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
346 + );
347 +
348 + $args['headers'] = $this->addHeaders( $args['headers'] );
349 + $args['body'] = $this->buildBody( 'POST', $this->endPoint );
350 +
351 + //plouf( $args ,__METHOD__ . "args");
352 +
353 + /*
354 + $args['auth_key'] = $this->authKey;
355 +
356 + $request = array();
357 + if ( $this->request ) {
358 + foreach ( $this->request as $key => $value ) {
359 + $request[$key] = $value;
360 + }
361 + }
362 + $args['query'] = http_build_query( $request );
363 + $args['timeout'] =
364 +*/
365 + //$args['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
351 366 // $this->headers['Content-Length'] = strlen( $this->request['text'] );
352 - $args = $this->buildQuery( 'POST' );
353 367
354 368
355 369
370 + $add_auth_key = false;
356 371 $remoteURL = $this->getEndPointURL( $add_auth_key );
357 372
358 373 $bits = array_merge( array( $remoteURL, 'POST' ), $args );
374 + // unsafe
359 375 //wpdeepl_log( $bits, 'apiRequests');
376 +// plouf( $args, "args" );
377 +/*
378 + $exec = "curl -X POST '$remoteURL' \\\n";
379 + foreach( $args['headers'] as $key => $value ) {
380 + $exec .= "-H '$key: $value' \\\n";
381 + }
382 + foreach( $args['body'] as $key => $value ) {
383 + $exec .= "-d '$value' \\\n";
384 + }
360 385
386 + plouf( $exec );
387 + $response = exec ( $exec );
388 + plouf( $response );
389 + die('okaz4a6e4a68e4a86e');
390 + */
391 +
392 + //plouf( $args, $remoteURL ); die('68z4e6z48a68ee8aok');
393 +
394 + //$args['body'] = str_replace('target_lang=NO', 'target_lang=NB', $args['body'] );
361 395 $response = wp_remote_post( $remoteURL, $args );
396 + //plouf( $args, $remoteURL, " post request"); plouf( $response ); die('okesoezporjkezor');
362 397
363 - //plouf($response); plouf( $args, "ARGS POSTS pour URL " . $remoteURL ); die( 'ok izpepiezrir' );
398 +// plouf( $args, " args pour $remoteURL" ); plouf( $response, "zeirjzpeijrpizerjpirj" ); die('okaze6aze44e');
399 + //plouf( $args, "args");
364 400
401 +// plouf( $args, "POST request to $remoteURL ");die('okaz5ea5zea4e');
402 +
365 403 if ( is_wp_error( $response ) ) {
366 404 $this->response = $response->get_error_message();
367 405 plouf( $args, $this->getEndPointURL() );
368 406 die( __METHOD__ );
369 - }
407 + }
370 408 else {
371 409 $this->response = $response;
372 410 }
373 411 return $response;
@@ -377,17 +415,40 @@
377 415 * send a GET request
378 416 *
379 417 * @since 0.1
380 418 */
381 - public function doGETRequest() {
382 - $args = $this->buildQuery( 'GET' );
419 + public function doGETRequest( $alt_mode = false ) {
383 420
384 - $remoteURL = $this->getEndPointURL() . '?' . $args['query'];
385 - //plouf( $args, "REQUESTING URL = '$url'" ); die( 'ok' );
421 + $method = 'GET';
422 + if( in_array( $alt_mode, array('PUT', 'DELETE' ) ) )
423 + $method = $alt_mode;
424 + $args = array(
425 + 'method' => $method,
426 + 'timeout' => self::TIMEOUT,
427 + );
428 +
429 + $args['headers'] = array(
430 + 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
431 + );
432 + $args['headers'] = $this->addHeaders( $args['headers'] );
433 +
434 + $remoteURL = $this->getEndPointURL();
435 + //if( $args['query'] ) $remoteURL .= '?' . $args['query'];
436 +
437 +
438 + $curl_string = 'curl -X ' . $args['method'] . " '$remoteURL'\n";
439 + foreach( $args['headers'] as $key => $value ) {
440 + $curl_string .= "$key: $value\n";
441 + }
442 + //echo "\n CURL = " . str_replace("\n", '<br />', $curl_string );
386 443 $bits = array_merge( array( $remoteURL, 'GET' ), $args );
444 + // unsafe
387 445 //wpdeepl_log( $bits, 'apiRequests');
388 446
447 +// plouf( $args, $remoteURL ); die('oazea4ze9684e84azek');
448 +
389 449 $response = wp_remote_get( $remoteURL, $args );
450 + //plouf( $args, $remoteURL ); plouf( $response ); die('ok');
390 451 if ( is_wp_error( $response ) ) {
391 452 $this->response = $response->get_error_message();
392 453 } else {
393 454 $this->response = $response;