| @@ -1,462 +1,294 @@ | ||
| 1 | -<?php | |
| 2 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 3 | -abstract class DeepLApi extends DeepLData { | |
| 4 | - protected $endPoint = ''; | |
| 5 | - protected $http_mode = ''; | |
| 6 | - private $endPointURL = false; | |
| 7 | - protected $authKey = ''; | |
| 8 | - protected $log = false; | |
| 9 | - protected $headers = array(); | |
| 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 | - | |
| 19 | - protected $cache_dir = false; | |
| 20 | - | |
| 21 | - protected $request = array(); | |
| 22 | - protected $uniqid = false; | |
| 23 | - protected $cache_file = false; | |
| 24 | - protected $response = false; | |
| 25 | - public $cache_prefix = false; | |
| 26 | - protected $cache_id = false; | |
| 27 | - | |
| 28 | - public $allow_cache = true; | |
| 29 | - public $response_type = 'fresh'; | |
| 30 | - public $request_cache_file = ''; | |
| 31 | - public $response_cache_file = ''; | |
| 32 | - | |
| 33 | - public $start_microtime = 0; | |
| 34 | - public $end_microtime = 0; | |
| 35 | - public $request_microtime = 0; | |
| 36 | - | |
| 37 | - const TIMEOUT = 15; | |
| 38 | - | |
| 39 | - public function __construct( $languages = array(), $log = false ) { | |
| 40 | - $this->authKey = DeepLConfiguration::getAPIKey(); | |
| 41 | - //$this->request['auth_key'] = $auth_key; | |
| 42 | - $this->log = $log; | |
| 43 | - | |
| 44 | - $this->languages = DeepLConfiguration::DefaultsAllLanguages(); | |
| 45 | - | |
| 46 | - if ( defined( 'DOING_CRON' ) && DOING_CRON ) { | |
| 47 | - $this->doing_cron = true; | |
| 48 | - } | |
| 49 | - else { | |
| 50 | - $this->doing_cron = false; | |
| 51 | - } | |
| 52 | - | |
| 53 | - $this->instance = $this; | |
| 54 | - | |
| 55 | - $this->cache_dir = $this->getCacheDirectory(); | |
| 56 | - } | |
| 57 | - | |
| 58 | - public function allowCache( $allow_cache ) { | |
| 59 | - $this->allow_cache = filter_var( $allow_cache, FILTER_VALIDATE_BOOLEAN ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - public function wasItCached() { | |
| 63 | - return $this->response_type === 'cache'; | |
| 64 | - } | |
| 65 | - | |
| 66 | - public function getTimeElapsed() { | |
| 67 | - return $this->request_microtime; | |
| 68 | - } | |
| 69 | - | |
| 70 | - abstract public function buildBody( $mode, $endPoint ); | |
| 71 | - | |
| 72 | - public function getRequestUniqueID() { | |
| 73 | - if ( $this->cache_id ) { | |
| 74 | - $this->uniqid = $this->cache_id; | |
| 75 | - } | |
| 76 | - elseif ( !$this->uniqid ) { | |
| 77 | - $this->uniqid = microtime(); | |
| 78 | - } | |
| 79 | - return $this->uniqid; | |
| 80 | - } | |
| 81 | - | |
| 82 | - public function isValidRequest() { | |
| 83 | - return true; | |
| 84 | - } | |
| 85 | - | |
| 86 | -/* | |
| 87 | - protected function maybeLimitRequestSize() { | |
| 88 | - } | |
| 89 | -*/ | |
| 90 | - protected function saveRequest() { | |
| 91 | - if ( isset( $this->request_cache_file ) ) { | |
| 92 | - file_put_contents( $this->request_cache_file, json_encode( $this->final_request ) ); | |
| 93 | - } | |
| 94 | - } | |
| 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' ) { | |
| 106 | - $response = $this->doGETRequest(); | |
| 107 | - } | |
| 108 | - elseif( $mode == 'DELETE' ) { | |
| 109 | - $response = $this->doGETRequest( 'DELETE' ); | |
| 110 | - } | |
| 111 | - elseif( $mode == 'PUT' ) { | |
| 112 | - $response = $this->doGETRequest( 'PUT' ); | |
| 113 | - } | |
| 114 | - else { | |
| 115 | - $response = $this->doPOSTRequest(); | |
| 116 | - } | |
| 117 | - | |
| 118 | - return $response; | |
| 119 | - } | |
| 120 | - | |
| 121 | - static function getInstance() { | |
| 122 | - return self::instance; | |
| 123 | - } | |
| 124 | - | |
| 125 | - protected function setCacheID( $idstring ) { | |
| 126 | - $this->cache_id = $idstring; | |
| 127 | - } | |
| 128 | - | |
| 129 | - public function setCachePrefix( $prefix = '' ) { | |
| 130 | - if ( !empty( $prefix ) ) { | |
| 131 | - $this->cache_prefix = $prefix; | |
| 132 | - } | |
| 133 | - else { | |
| 134 | - $this->cache_prefix = ''; | |
| 135 | - } | |
| 136 | - } | |
| 137 | - | |
| 138 | - public function getCacheFile( $type ) { | |
| 139 | - if ( $type == 'response' ) { | |
| 140 | - return $this->response_cache_file; | |
| 141 | - } | |
| 142 | - elseif ( $type == 'request' ) { | |
| 143 | - return $this->request_cache_file; | |
| 144 | - } | |
| 145 | - } | |
| 146 | - | |
| 147 | - public function setCacheNames() { | |
| 148 | - $cache_name = ''; | |
| 149 | - if ( !empty( $this->cache_prefix ) ) { | |
| 150 | - $cache_name .= $this->cache_prefix .'-'; | |
| 151 | - } | |
| 152 | - $cache_name .= $this->cache_id; | |
| 153 | - $this->request_cache_file = trailingslashit( $this->cache_dir ) . $cache_name . '-request'; | |
| 154 | - $this->response_cache_file = trailingslashit( $this->cache_dir ) . $cache_name . '-response'; | |
| 155 | - } | |
| 156 | - | |
| 157 | - public function request() { | |
| 158 | - | |
| 159 | - $log_bits = array('class' => get_class($this)); | |
| 160 | - | |
| 161 | - if ( $this->allow_cache ) | |
| 162 | - $this->uniqid = $this->getRequestUniqueID(); | |
| 163 | - | |
| 164 | - $log_bits['uniqid'] = $this->uniqid; | |
| 165 | - | |
| 166 | - $this->setCacheNames(); | |
| 167 | - $this->result = false; | |
| 168 | - | |
| 169 | - | |
| 170 | - | |
| 171 | - if ( $this->isCacheValid( $this->response_cache_file ) ) { | |
| 172 | - $this->response_type = 'cache'; | |
| 173 | - $this->why_no_cache = false; | |
| 174 | - $this->result = file_get_contents( $this->response_cache_file ); | |
| 175 | - $log_bits['type'] = 'cached'; | |
| 176 | - | |
| 177 | - } | |
| 178 | - else { | |
| 179 | - $log_bits['type'] = 'fresh'; | |
| 180 | - $log_bits['mode'] = $this->http_mode; | |
| 181 | - $this->start_microtime = microtime( true ); | |
| 182 | - //echo "\nIS FRESH"; | |
| 183 | - $this->response_type = 'fresh'; | |
| 184 | - | |
| 185 | - | |
| 186 | - if ( $this->http_mode == 'GET' ) { | |
| 187 | - $response = $this->doRequest( 'GET' ); | |
| 188 | - } | |
| 189 | - else { | |
| 190 | - $response = $this->doRequest( 'POST' ); | |
| 191 | - } | |
| 192 | - | |
| 193 | - | |
| 194 | - $this->end_microtime = microtime( true ); | |
| 195 | - $this->request_microtime = $this->end_microtime - $this->start_microtime; | |
| 196 | - | |
| 197 | - if ( is_wp_error( $response ) ) { | |
| 198 | - return $response->get_error_message(); | |
| 199 | - } | |
| 200 | - | |
| 201 | - if ( $response['response']['code'] > 299 ) { | |
| 202 | - $log_bits['response_error'] = $response['response']['code']; | |
| 203 | - $log_bits['response'] = $response['response']; | |
| 204 | - if ( $response['response']['code'] == 429 ) { | |
| 205 | - $log_bits['error_cause'] = 'Excessive request size'; | |
| 206 | - } | |
| 207 | - if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation"); | |
| 208 | - | |
| 209 | - $body = json_decode( $response['body'], true ); | |
| 210 | - $message = isset( $body['message'] ) ? $body['message'] : false; | |
| 211 | - return new WP_Error( $message ); | |
| 212 | - } | |
| 213 | - | |
| 214 | - if ( array_key_exists( 'body', $response ) && strlen( $response['body'] ) ) | |
| 215 | - $this->result = $response['body']; | |
| 216 | - else | |
| 217 | - $this->result = false; | |
| 218 | - | |
| 219 | - if ( $this->result ) { | |
| 220 | - //echo " putting " . strlen( serialize( $this->result ) ) ." in " | |
| 221 | - try { | |
| 222 | - file_put_contents( $this->response_cache_file, $this->result ); | |
| 223 | - } catch (Exception $e) { | |
| 224 | - | |
| 225 | - } | |
| 226 | - } | |
| 227 | - | |
| 228 | - $log_bits['request_microtime'] = $this->request_microtime; | |
| 229 | - } | |
| 230 | - | |
| 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 | - } | |
| 240 | - } | |
| 241 | - else { | |
| 242 | - foreach ( array ('source_lang', 'target_lang') as $key ) { | |
| 243 | - if ( isset( $this->request[$key] ) ) { | |
| 244 | - $log_bits[$key] = $this->request[$key]; | |
| 245 | - } | |
| 246 | - } | |
| 247 | - } | |
| 248 | - if ( isset( $this->request['text'] ) ) { | |
| 249 | - $text_size = 0; | |
| 250 | - foreach ( $this->request['text'] as $i => $string ) { | |
| 251 | - $text_size += strlen($string); | |
| 252 | - } | |
| 253 | - $log_bits['text_size'] = $text_size; | |
| 254 | - } | |
| 255 | - } | |
| 256 | - | |
| 257 | - if ($this->response_type == 'fresh') { | |
| 258 | - // test if JSON | |
| 259 | - json_decode($this->result); | |
| 260 | - if (json_last_error() == JSON_ERROR_NONE) { | |
| 261 | - $log_bits['response'] = json_decode($this->result, 1 ); | |
| 262 | - if ( DeepLConfiguration::getLogLevel() ) { | |
| 263 | - if (isset( $log_bits['response']['translations'] ) ) { | |
| 264 | - $translated_text_size = 0; | |
| 265 | - foreach ( $log_bits['response']['translations'] as $i => $translation ) { | |
| 266 | - $translated_text_size += strlen( $translation['text'] ); | |
| 267 | - } | |
| 268 | - $log_bits['translated_text_size'] = $translated_text_size; | |
| 269 | - } | |
| 270 | - } | |
| 271 | - } | |
| 272 | - else { | |
| 273 | - $log_bits['response'] = $this->result; | |
| 274 | - } | |
| 275 | - | |
| 276 | - } | |
| 277 | - else { | |
| 278 | - $log_bits['response'] = '--hidden response because cached request--'; | |
| 279 | - | |
| 280 | - } | |
| 281 | - $log_bits['response_length'] = strlen($this->result); | |
| 282 | - | |
| 283 | - //plouf( $this ); die('erorkez'); | |
| 284 | - | |
| 285 | - if ( $this->result ) { | |
| 286 | - $this->result = json_decode( $this->result ); | |
| 287 | - | |
| 288 | - $this->result = ( array ) $this->result; | |
| 289 | - $this->result['response_type'] = $this->response_type; | |
| 290 | - if ( $this->why_no_cache ) { | |
| 291 | - $log_bits['why_no_cache'] = $this->why_no_cache; | |
| 292 | - $this->result['why_no_cache'] = $this->why_no_cache; | |
| 293 | - } | |
| 294 | - if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation"); | |
| 295 | - return $this->result; | |
| 296 | - } | |
| 297 | - else { | |
| 298 | - if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation"); | |
| 299 | - return $this->response['response']['message']; | |
| 300 | - } | |
| 301 | - } | |
| 302 | - | |
| 303 | - public function getRequestTime( $result_in_milliseconds = false ) { | |
| 304 | - if ( !$this->request_microtime ) { | |
| 305 | - return false; | |
| 306 | - } | |
| 307 | - | |
| 308 | - if ( $result_in_milliseconds ) { | |
| 309 | - return floatval( $this->request_microtime ); | |
| 310 | - } | |
| 311 | - else { | |
| 312 | - return floatval( $this->request_microtime/1000 ); | |
| 313 | - } | |
| 314 | - } | |
| 315 | - | |
| 316 | - public function getEndPointURL( $add_auth_key = false ) { | |
| 317 | - | |
| 318 | - | |
| 319 | - $APIServer = DeepLConfiguration::getAPIServer(); | |
| 320 | - if ( !$this->endPointURL ) { | |
| 321 | - if ( $this->endPoint === '' ) { | |
| 322 | - $this->endPointURL = $APIServer; | |
| 323 | - } | |
| 324 | - else { | |
| 325 | - $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint; | |
| 326 | - } | |
| 327 | - } | |
| 328 | - | |
| 329 | - if ( $add_auth_key ) { | |
| 330 | - $this->endPointURL .= '?auth_key=' . $this->authKey; | |
| 331 | - } | |
| 332 | - | |
| 333 | - return $this->endPointURL; | |
| 334 | - } | |
| 335 | - /* | |
| 336 | - * send a POST request | |
| 337 | - * | |
| 338 | - * @since 0.1 | |
| 339 | - */ | |
| 340 | - public function doPOSTRequest( $args = array() ) { | |
| 341 | - | |
| 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'; | |
| 370 | -// $this->headers['Content-Length'] = strlen( $this->request['text'] ); | |
| 371 | - | |
| 372 | - | |
| 373 | - | |
| 374 | - $add_auth_key = false; | |
| 375 | - $remoteURL = $this->getEndPointURL( $add_auth_key ); | |
| 376 | - | |
| 377 | - $bits = array_merge( array( $remoteURL, 'POST' ), $args ); | |
| 378 | - // unsafe | |
| 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 | - } | |
| 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'] ); | |
| 399 | - $response = wp_remote_post( $remoteURL, $args ); | |
| 400 | - //wpdeepl_debug_display( $args, $remoteURL, " post request"); wpdeepl_debug_display( $response ); die('okesoezporjkezor'); | |
| 401 | - | |
| 402 | -// wpdeepl_debug_display( $args, " args pour $remoteURL" ); wpdeepl_debug_display( $response, "zeirjzpeijrpizerjpirj" ); die('okaze6aze44e'); | |
| 403 | - //wpdeepl_debug_display( $args, "args"); | |
| 404 | - | |
| 405 | -// wpdeepl_debug_display( $args, "POST request to $remoteURL ");die('okaz5ea5zea4e'); | |
| 406 | - | |
| 407 | - if ( is_wp_error( $response ) ) { | |
| 408 | - $this->response = $response->get_error_message(); | |
| 409 | - wpdeepl_debug_display( $args, $this->getEndPointURL() ); | |
| 410 | - die( __METHOD__ ); | |
| 411 | - } | |
| 412 | - else { | |
| 413 | - $this->response = $response; | |
| 414 | - } | |
| 415 | - return $response; | |
| 416 | - } | |
| 417 | - | |
| 418 | - /* | |
| 419 | - * send a GET request | |
| 420 | - * | |
| 421 | - * @since 0.1 | |
| 422 | - */ | |
| 423 | - public function doGETRequest( $alt_mode = false ) { | |
| 424 | - | |
| 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 ); | |
| 447 | - $bits = array_merge( array( $remoteURL, 'GET' ), $args ); | |
| 448 | - // unsafe | |
| 449 | - //wpdeepl_log( $bits, 'apiRequests'); | |
| 450 | - | |
| 451 | -// wpdeepl_debug_display( $args, $remoteURL ); die('oazea4ze9684e84azek'); | |
| 452 | - | |
| 453 | - $response = wp_remote_get( $remoteURL, $args ); | |
| 454 | - //wpdeepl_debug_display( $args, $remoteURL ); wpdeepl_debug_display( $response ); die('ok'); | |
| 455 | - if ( is_wp_error( $response ) ) { | |
| 456 | - $this->response = $response->get_error_message(); | |
| 457 | - } else { | |
| 458 | - $this->response = $response; | |
| 459 | - } | |
| 460 | - return $response; | |
| 461 | - } | |
| 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 | + } | |
| 462 | 294 | } |