PluginProbe
Translation with DeepL API / 0.1.20180323
Translation with DeepL API v0.1.20180323
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
wpdeepl / classes / deeplapi.php

deeplapi.php in Translation with DeepL API 0.1.20180323, at classes/deeplapi.php

160 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class DeepLApi {
4
5 protected $endpointURL = '';
6 protected $authKey = '';
7 protected $log = false;
8 protected $headers = array();
9 protected $request = array();
10 protected $cache_dir = false;
11 protected $allow_cache = false;
12 protected $request_args = array();
13 protected $uniqid = false;
14 protected $cache_file = false;
15 protected $response = false;
16
17
18
19 const TIMEOUT = 15;
20
21 public function __construct($auth_key, $log = false) {
22 $this->authKey = $auth_key;
23 //$this->request['auth_key'] = $auth_key;
24 $this->log = $log;
25
26 $this->cache_dir = WPDeepL::getUploadDirectory();
27 }
28
29 protected function buildQuery($mode = 'POST') {
30 $args = array();
31 if(count($this->headers))
32 $args['headers'] = $this->headers;
33
34 $request = array('auth_key' => $this->authKey);
35 if($this->request) {
36 foreach($this->request as $key => $value) {
37 $request[$key] = $value;
38 }
39 }
40
41 if($mode == 'POST') {
42 $args['body'] = http_build_query($request);
43 }
44 else
45 $args['query'] = http_build_query($request);
46
47 $args['timeout'] = self::TIMEOUT;
48
49 $this->request_args = $args;
50 return $args;
51
52 }
53
54 public function getRequestUniqueID() {
55 if(!$this->uniqid)
56 $this->uniqid = microtime();
57 return $this->uniqid;
58 }
59
60 public function isValidRequest() {
61 return true;
62
63 }
64
65
66 public function request() {
67
68 if($this->allow_cache)
69 $this->uniqid = $this->getRequestUniqueID();
70
71 $this->cache_file = trailingslashit( $this->cache_dir ) . $this->uniqid;
72
73 if($this->allow_cache && file_exists($this->cache_file) && filesize($this->cache_file) > 10 && $this->result = file_get_contents($this->cache_file)) {
74 $this->response_type = 'cache';
75 $this->why_no_cache = false;
76 }
77 else {
78 $this->response_type = 'fresh';
79 $this->why_no_cache = true;
80 if(!$this->allow_cache)
81 $this->why_no_cache = "Cache not allowed";
82 elseif(!file_exists($this->cache_file))
83 $this->why_no_cache = "No cache file for id $this->uniqid";
84 elseif(filesize($this->cache_file) < 10)
85 $this->why_no_cache = "Cache file too small";
86 elseif(!file_get_contents($this->cache_file))
87 $this->why_no_cache = "Cache file not readable";
88
89 if($this->http_mode == 'GET') {
90 $this->get();
91 }
92 else {
93 $this->post();
94 }
95
96 if(array_key_exists('body',$this->response) && strlen($this->response['body']))
97 $this->result = $this->response['body'];
98 else
99 $this->result = false;
100
101 if($this->allow_cache && $this->result) {
102 //echo " putting " . strlen(serialize($this->result)) ." in "
103 file_put_contents($this->cache_file,$this->result);
104 }
105 }
106
107 if($this->result) {
108 $this->result = json_decode($this->result);
109 $this->result = (array) $this->result;
110 $this->result['response_type'] = $this->response_type;
111 if($this->why_no_cache) {
112 $this->result['why_no_cache'] = $this->why_no_cache;
113 }
114 return $this->result;
115 }
116 else {
117 return $this->response['response']['message'];
118 }
119
120 }
121
122 /*
123 * send a POST request
124 *
125 * @since 0.1
126 */
127 public function post($args = array()) {
128 $this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
129 //if(array_key_exists('text',$this->request))
130 // $this->headers['Content-Length'] = strlen($this->request['text']);
131 $args = $this->buildQuery('POST');
132 $response = wp_remote_post($this->endpointURL, $args);
133 if ( is_wp_error( $response ) ) {
134 $this->response = $response->get_error_message();
135 } else {
136 $this->response = $response;
137 }
138 }
139
140 /*
141 * send a GET request
142 *
143 * @since 0.1
144 */
145 public function get() {
146 $args = $this->buildQuery('GET');
147 $url = $this->endpointURL . '?' . $args['query'];
148 unset($args['query']);
149 $response = wp_remote_get($url, $args);
150 if ( is_wp_error( $response ) ) {
151 $this->response = $response->get_error_message();
152 } else {
153 $this->response = $response;
154 }
155
156 }
157
158
159
160 }