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