PluginProbe
Translation with DeepL API / trunk
Translation with DeepL API vtrunk
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 +170 -105 1.7.5trunk View file →
@@ -1,13 +1,22 @@
1 1 <?php
2 -
2 +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
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,27 @@
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 +
170 +
171 + if ( $this->isCacheValid( $this->response_cache_file ) ) {
187 172 $this->response_type = 'cache';
188 173 $this->why_no_cache = false;
189 174 $this->result = file_get_contents( $this->response_cache_file );
190 175 $log_bits['type'] = 'cached';
191 -
192 - //plouf( $this->result ); die('cache');
176 +
193 177 }
194 178 else {
195 - //die("no cache");
196 179 $log_bits['type'] = 'fresh';
197 180 $log_bits['mode'] = $this->http_mode;
198 181 $this->start_microtime = microtime( true );
199 182 //echo "\nIS FRESH";
200 183 $this->response_type = 'fresh';
201 - if( $this->http_mode == 'GET' ) {
184 +
185 +
186 + if ( $this->http_mode == 'GET' ) {
202 187 $response = $this->doRequest( 'GET' );
203 188 }
204 189 else {
205 190 $response = $this->doRequest( 'POST' );
@@ -208,36 +193,36 @@
208 193
209 194 $this->end_microtime = microtime( true );
210 195 $this->request_microtime = $this->end_microtime - $this->start_microtime;
211 196
212 - if( is_wp_error( $response ) ) {
197 + if ( is_wp_error( $response ) ) {
213 198 return $response->get_error_message();
214 199 }
215 200
216 - if( $response['response']['code'] > 299 ) {
201 + if ( $response['response']['code'] > 299 ) {
217 202 $log_bits['response_error'] = $response['response']['code'];
218 203 $log_bits['response'] = $response['response'];
219 - if( $response['response']['code'] == 429 ) {
204 + if ( $response['response']['code'] == 429 ) {
220 205 $log_bits['error_cause'] = 'Excessive request size';
221 206 }
222 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
207 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
223 208
224 209 $body = json_decode( $response['body'], true );
225 - $message = $body['message'];
210 + $message = isset( $body['message'] ) ? $body['message'] : false;
226 211 return new WP_Error( $message );
227 212 }
228 213
229 - if( array_key_exists( 'body', $response ) && strlen( $response['body'] ) )
214 + if ( array_key_exists( 'body', $response ) && strlen( $response['body'] ) )
230 215 $this->result = $response['body'];
231 216 else
232 217 $this->result = false;
233 218
234 - if( $this->result ) {
219 + if ( $this->result ) {
235 220 //echo " putting " . strlen( serialize( $this->result ) ) ." in "
236 221 try {
237 222 file_put_contents( $this->response_cache_file, $this->result );
238 223 } catch (Exception $e) {
239 -
224 +
240 225 }
241 226 }
242 227
243 228 $log_bits['request_microtime'] = $this->request_microtime;
@@ -242,22 +227,28 @@
242 227
243 228 $log_bits['request_microtime'] = $this->request_microtime;
244 229 }
245 230
246 - if( DeepLConfiguration::getLogLevel() ) {
247 - if( DeepLConfiguration::getLogLevel() > 1 ) {
248 - $log_bits['args'] = $this->request;
231 + if ( DeepLConfiguration::getLogLevel() ) {
232 + if ( DeepLConfiguration::getLogLevel() > 1 ) {
233 + foreach( $this->request as $key => $value ) {
234 + if( is_string( $value ) && strlen( $value ) > 500 )
235 + $log_bits['args'][$key] = "LEN=" . strlen( $value) .", string = " . substr( $value, 0, 100 );
236 + else
237 + $log_bits['args'][$key] = $value;
238 +
239 + }
249 240 }
250 241 else {
251 - foreach( array ('source_lang', 'target_lang') as $key ) {
252 - if( isset( $this->request[$key] ) ) {
242 + foreach ( array ('source_lang', 'target_lang') as $key ) {
243 + if ( isset( $this->request[$key] ) ) {
253 244 $log_bits[$key] = $this->request[$key];
254 245 }
255 246 }
256 247 }
257 - if( isset( $this->request['text'] ) ) {
248 + if ( isset( $this->request['text'] ) ) {
258 249 $text_size = 0;
259 - foreach( $this->request['text'] as $i => $string ) {
250 + foreach ( $this->request['text'] as $i => $string ) {
260 251 $text_size += strlen($string);
261 252 }
262 253 $log_bits['text_size'] = $text_size;
263 254 }
@@ -262,17 +253,17 @@
262 253 $log_bits['text_size'] = $text_size;
263 254 }
264 255 }
265 256
266 - if($this->response_type == 'fresh') {
257 + if ($this->response_type == 'fresh') {
267 258 // test if JSON
268 259 json_decode($this->result);
269 - if(json_last_error() == JSON_ERROR_NONE) {
260 + if (json_last_error() == JSON_ERROR_NONE) {
270 261 $log_bits['response'] = json_decode($this->result, 1 );
271 - if( DeepLConfiguration::getLogLevel() ) {
272 - if(isset( $log_bits['response']['translations'] ) ) {
262 + if ( DeepLConfiguration::getLogLevel() ) {
263 + if (isset( $log_bits['response']['translations'] ) ) {
273 264 $translated_text_size = 0;
274 - foreach( $log_bits['response']['translations'] as $i => $translation ) {
265 + foreach ( $log_bits['response']['translations'] as $i => $translation ) {
275 266 $translated_text_size += strlen( $translation['text'] );
276 267 }
277 268 $log_bits['translated_text_size'] = $translated_text_size;
278 269 }
@@ -280,9 +271,9 @@
280 271 }
281 272 else {
282 273 $log_bits['response'] = $this->result;
283 274 }
284 -
275 +
285 276 }
286 277 else {
287 278 $log_bits['response'] = '--hidden response because cached request--';
288 279
@@ -288,32 +279,34 @@
288 279
289 280 }
290 281 $log_bits['response_length'] = strlen($this->result);
291 282
292 - if( $this->result ) {
283 + //plouf( $this ); die('erorkez');
284 +
285 + if ( $this->result ) {
293 286 $this->result = json_decode( $this->result );
294 287
295 288 $this->result = ( array ) $this->result;
296 289 $this->result['response_type'] = $this->response_type;
297 - if( $this->why_no_cache ) {
290 + if ( $this->why_no_cache ) {
298 291 $log_bits['why_no_cache'] = $this->why_no_cache;
299 292 $this->result['why_no_cache'] = $this->why_no_cache;
300 293 }
301 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
294 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
302 295 return $this->result;
303 296 }
304 297 else {
305 - if( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
298 + if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
306 299 return $this->response['response']['message'];
307 300 }
308 301 }
309 302
310 303 public function getRequestTime( $result_in_milliseconds = false ) {
311 - if( !$this->request_microtime ) {
304 + if ( !$this->request_microtime ) {
312 305 return false;
313 306 }
314 307
315 - if( $result_in_milliseconds ) {
308 + if ( $result_in_milliseconds ) {
316 309 return floatval( $this->request_microtime );
317 310 }
318 311 else {
319 312 return floatval( $this->request_microtime/1000 );
@@ -322,11 +315,11 @@
322 315
323 316 public function getEndPointURL( $add_auth_key = false ) {
324 317
325 318
326 - $APIServer = DeeplConfiguration::getAPIServer();
327 - if( !$this->endPointURL ) {
328 - if( $this->endPoint === '' ) {
319 + $APIServer = DeepLConfiguration::getAPIServer();
320 + if ( !$this->endPointURL ) {
321 + if ( $this->endPoint === '' ) {
329 322 $this->endPointURL = $APIServer;
330 323 }
331 324 else {
332 325 $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint;
@@ -332,9 +325,9 @@
332 325 $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint;
333 326 }
334 327 }
335 328
336 - if( $add_auth_key ) {
329 + if ( $add_auth_key ) {
337 330 $this->endPointURL .= '?auth_key=' . $this->authKey;
338 331 }
339 332
340 333 return $this->endPointURL;
@@ -344,30 +337,79 @@
344 337 *
345 338 * @since 0.1
346 339 */
347 340 public function doPOSTRequest( $args = array() ) {
348 - $add_auth_key = true;
349 341
350 - $this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
342 +
343 + $args = array(
344 + 'method' => 'POST',
345 + 'timeout' => self::TIMEOUT,
346 + );
347 +
348 + $args['headers'] = array(
349 + 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
350 + );
351 +
352 + $args['headers'] = $this->addHeaders( $args['headers'] );
353 + $args['body'] = $this->buildBody( 'POST', $this->endPoint );
354 +
355 + //wpdeepl_debug_display( $args ,__METHOD__ . "args");
356 +
357 + /*
358 + $args['auth_key'] = $this->authKey;
359 +
360 + $request = array();
361 + if ( $this->request ) {
362 + foreach ( $this->request as $key => $value ) {
363 + $request[$key] = $value;
364 + }
365 + }
366 + $args['query'] = http_build_query( $request );
367 + $args['timeout'] =
368 +*/
369 + //$args['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
351 370 // $this->headers['Content-Length'] = strlen( $this->request['text'] );
352 - $args = $this->buildQuery( 'POST' );
353 371
354 372
355 373
374 + $add_auth_key = false;
356 375 $remoteURL = $this->getEndPointURL( $add_auth_key );
357 376
358 377 $bits = array_merge( array( $remoteURL, 'POST' ), $args );
378 + // unsafe
359 379 //wpdeepl_log( $bits, 'apiRequests');
380 +// wpdeepl_debug_display( $args, "args" );
381 +/*
382 + $exec = "curl -X POST '$remoteURL' \\\n";
383 + foreach( $args['headers'] as $key => $value ) {
384 + $exec .= "-H '$key: $value' \\\n";
385 + }
386 + foreach( $args['body'] as $key => $value ) {
387 + $exec .= "-d '$value' \\\n";
388 + }
360 389
390 + wpdeepl_debug_display( $exec );
391 + $response = exec ( $exec );
392 + wpdeepl_debug_display( $response );
393 + die('okaz4a6e4a68e4a86e');
394 + */
395 +
396 + //wpdeepl_debug_display( $args, $remoteURL ); die('68z4e6z48a68ee8aok');
397 +
398 + //$args['body'] = str_replace('target_lang=NO', 'target_lang=NB', $args['body'] );
361 399 $response = wp_remote_post( $remoteURL, $args );
400 + //wpdeepl_debug_display( $args, $remoteURL, " post request"); wpdeepl_debug_display( $response ); die('okesoezporjkezor');
362 401
363 - //plouf($response); plouf( $args, "ARGS POSTS pour URL " . $remoteURL ); die( 'ok izpepiezrir' );
402 +// wpdeepl_debug_display( $args, " args pour $remoteURL" ); wpdeepl_debug_display( $response, "zeirjzpeijrpizerjpirj" ); die('okaze6aze44e');
403 + //wpdeepl_debug_display( $args, "args");
364 404
405 +// wpdeepl_debug_display( $args, "POST request to $remoteURL ");die('okaz5ea5zea4e');
406 +
365 407 if ( is_wp_error( $response ) ) {
366 408 $this->response = $response->get_error_message();
367 - plouf( $args, $this->getEndPointURL() );
409 + wpdeepl_debug_display( $args, $this->getEndPointURL() );
368 410 die( __METHOD__ );
369 - }
411 + }
370 412 else {
371 413 $this->response = $response;
372 414 }
373 415 return $response;
@@ -377,17 +419,40 @@
377 419 * send a GET request
378 420 *
379 421 * @since 0.1
380 422 */
381 - public function doGETRequest() {
382 - $args = $this->buildQuery( 'GET' );
423 + public function doGETRequest( $alt_mode = false ) {
383 424
384 - $remoteURL = $this->getEndPointURL() . '?' . $args['query'];
385 - //plouf( $args, "REQUESTING URL = '$url'" ); die( 'ok' );
425 + $method = 'GET';
426 + if( in_array( $alt_mode, array('PUT', 'DELETE' ) ) )
427 + $method = $alt_mode;
428 + $args = array(
429 + 'method' => $method,
430 + 'timeout' => self::TIMEOUT,
431 + );
432 +
433 + $args['headers'] = array(
434 + 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
435 + );
436 + $args['headers'] = $this->addHeaders( $args['headers'] );
437 +
438 + $remoteURL = $this->getEndPointURL();
439 + //if( $args['query'] ) $remoteURL .= '?' . $args['query'];
440 +
441 +
442 + $curl_string = 'curl -X ' . $args['method'] . " '$remoteURL'\n";
443 + foreach( $args['headers'] as $key => $value ) {
444 + $curl_string .= "$key: $value\n";
445 + }
446 + //echo "\n CURL = " . str_replace("\n", '<br />', $curl_string );
386 447 $bits = array_merge( array( $remoteURL, 'GET' ), $args );
448 + // unsafe
387 449 //wpdeepl_log( $bits, 'apiRequests');
388 450
451 +// wpdeepl_debug_display( $args, $remoteURL ); die('oazea4ze9684e84azek');
452 +
389 453 $response = wp_remote_get( $remoteURL, $args );
454 + //wpdeepl_debug_display( $args, $remoteURL ); wpdeepl_debug_display( $response ); die('ok');
390 455 if ( is_wp_error( $response ) ) {
391 456 $this->response = $response->get_error_message();
392 457 } else {
393 458 $this->response = $response;