PluginProbe
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin / trunk
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin vtrunk
6.5.1.6 6.5.1.5 6.5.1.4 6.5.1.3 6.5.1.2 6.5.1.1 6.5.0.9 6.5.0.8 6.5.0.7 6.5.0.6 trunk 3.4.2.40 3.4.2.41 3.4.2.42 3.4.2.43 3.4.2.44 3.4.2.45 3.4.2.46 3.4.2.47 3.4.2.48 3.4.2.49 3.4.2.50 6.3.2 6.3.3.1 6.3.3.10 All 46 releases
wpdatatables / source / class.wdtnestedjson.php

class.wdtnestedjson.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin trunk, at source/class.wdtnestedjson.php

391 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined('ABSPATH') or die('Access denied.');
4
5 class WDTNestedJson
6 {
7 private $_url = '';
8 private $_method = 'get';
9 private $_authOption = '';
10 private $_username = '';
11 private $_password = '';
12 private $_customHeaders = [];
13 private $_root = '';
14
15 public function __construct($params = null)
16 {
17 if (isset($params->url))
18 $this->setUrl($params->url);
19 if (isset($params->method))
20 $this->setMethod($params->method);
21 if (isset($params->authOption))
22 $this->setAuthOption($params->authOption);
23 if (isset($params->username))
24 $this->setUsername($params->username);
25 if (isset($params->password))
26 $this->setPassword($params->password);
27 if (isset($params->customHeaders))
28 $this->setCustomHeaders($params->customHeaders);
29 if (isset($params->root))
30 $this->setRoot($params->root);
31 }
32
33 /**
34 * @return mixed
35 */
36 public function getUrl()
37 {
38 return $this->_url;
39 }
40
41 /**
42 * @param mixed $url
43 */
44 public function setUrl($url)
45 {
46 $this->_url = $url;
47 }
48
49 /**
50 * @return string
51 */
52 public function getMethod()
53 {
54 return $this->_method;
55 }
56
57 /**
58 * @param string $method
59 */
60 public function setMethod($method)
61 {
62 $this->_method = $method;
63 }
64
65 /**
66 * @return string
67 */
68 public function getAuthOption()
69 {
70 return $this->_authOption;
71 }
72
73 /**
74 * @param string $authOption
75 */
76 public function setAuthOption($authOption)
77 {
78 $this->_authOption = $authOption;
79 }
80
81 /**
82 * @return string
83 */
84 public function getUsername()
85 {
86 return $this->_username;
87 }
88
89 /**
90 * @param string $username
91 */
92 public function setUsername($username)
93 {
94 $this->_username = $username;
95 }
96
97 /**
98 * @return string
99 */
100 public function getPassword()
101 {
102 return $this->_password;
103 }
104
105 /**
106 * @param string $password
107 */
108 public function setPassword($password)
109 {
110 $this->_password = $password;
111 }
112
113 /**
114 * @return array
115 */
116 public function getCustomHeaders()
117 {
118 return $this->_customHeaders;
119 }
120
121 /**
122 * @param mixed $customHeaders
123 */
124 public function setCustomHeaders($customHeaders)
125 {
126 $this->_customHeaders = $customHeaders;
127 }
128
129 /**
130 * @return string
131 */
132 public function getRoot()
133 {
134 return $this->_root;
135 }
136
137 /**
138 * @param string $root
139 */
140 public function setRoot($root)
141 {
142 $this->_root = $root;
143 }
144
145 /**
146 * Prepare Endpoint arguments
147 * @return array
148 */
149 public function prepareEndPointArgs($tableID)
150 {
151 $endPointArgs = array(
152 'method' => strtoupper($this->getMethod()),
153 'sslverify' => false,
154 'timeout' => 100
155 );
156
157 if ($this->getUsername() !== '' && $this->getPassword() !== '') {
158 $endPointArgs['headers'] = array('Authorization' => 'Basic ' . base64_encode($this->getUsername() . ':' . $this->getPassword()));
159 }
160
161 if ($this->getCustomHeaders() !== []) {
162 $customHeaders = $this->getCustomHeaders();
163 if (!empty($customHeaders)) {
164 foreach ($customHeaders as $customHeader) {
165 $headerKey = $customHeader->setKeyName;
166 $headerValue = $customHeader->setKeyValue;
167 $endPointArgs['headers'][$headerKey] = $headerValue;
168 }
169 }
170 }
171
172 return apply_filters('wpdatatables_filter_nested_json_endpoint_args', $endPointArgs, $this->getAllArgs(), $tableID);
173 }
174
175 /**
176 * Get all JSON arguments
177 */
178 public function getAllArgs()
179 {
180 $args = new stdClass();
181
182 $args->url = $this->getUrl();
183 $args->method = $this->getMethod();
184 $args->authOption = $this->getAuthOption();
185 $args->username = $this->getUsername();
186 $args->password = $this->getPassword();
187 $args->customHeaders = $this->getCustomHeaders();
188 $args->root = $this->getRoot();
189
190 return $args;
191 }
192
193 /**
194 * Get data from JSON
195 *
196 * @throws Exception
197 */
198 public function getData($tableID)
199 {
200 $response = $this->getResponse($tableID);
201 if ($response === '' || $response === null) {
202 throw new Exception('Response is empty. Please check is valid URL.');
203 }
204 if (!is_array($response)) {
205 throw new Exception($response);
206 }
207
208 return $this->getRowsByRootPath($response, $tableID);
209 }
210
211 /**
212 * Get response from JSON API
213 */
214 public function getResponse($tableID)
215 {
216 $endPointArgs = $this->prepareEndPointArgs($tableID);
217 $response = wp_remote_request($this->getUrl(), $endPointArgs);;
218 if (is_wp_error($response) || !in_array(intval($response['response']['code']), array(200, 201), true)) {
219 return wp_remote_retrieve_response_message($response);
220 }
221 $response_body = wp_remote_retrieve_body($response);
222
223 return json_decode($response_body, true);
224 }
225
226 /**
227 * Get data by filtering array with root path
228 */
229 private function getDataByRootPath($response, $root)
230 {
231 $filteredDataByChosenRoot = $response;
232 foreach ($root as $tag) {
233 if (array_key_exists($tag, $filteredDataByChosenRoot)) {
234 $filteredDataByChosenRoot = $filteredDataByChosenRoot[$tag];
235 } else {
236 $temp = array();
237 for ($depth = 0; $depth < count($filteredDataByChosenRoot); $depth++) {
238 if (!isset($filteredDataByChosenRoot[$depth][$tag])) {
239 continue;
240 }
241 $temp[] = $filteredDataByChosenRoot[$depth][$tag];
242 }
243 $filteredDataByChosenRoot = $temp;
244 }
245 }
246
247 if (empty($filteredDataByChosenRoot)) {
248 $filteredDataByChosenRoot = $response;
249 }
250
251 return $filteredDataByChosenRoot;
252 }
253
254 /**
255 * Get rows by filtering array with root path
256 * @throws Exception
257 */
258 private function getRowsByRootPath($response, $tableID)
259 {
260 $root = explode('->', $this->getRoot());
261 if (count($root) > 1) {
262 array_shift($root);
263 }
264 $filteredDataByChosenRoot = $this->getDataByRootPath($response, $root);
265 $data = $this->getFinalArray($filteredDataByChosenRoot, $tableID);
266 $rows = $this->prepareFinalArrayWithSameStructure($data);
267
268 return apply_filters('wpdatatables_filter_nested_json_rows_by_root_path', $rows, $this->getRoot(), $response);
269 }
270
271 /**
272 * Get final array data from response
273 */
274 private function getFinalArray($filteredDataByChosenRoot, $tableID)
275 {
276 $oneLevelDeepArrayValues = apply_filters('wpdatatables_get_one_level_deep_json_data_from_array_as_string', false, $this->getUrl(), $tableID);
277 $arrayValuesSeparator = apply_filters('wpdatatables_set_one_level_deep_json_data_separator', '<br>', $this->getUrl(), $tableID);
278 $isArrayContainsOnlyArrays = array_filter($filteredDataByChosenRoot, 'is_array') === $filteredDataByChosenRoot;
279 $data = [];
280 if (!$isArrayContainsOnlyArrays) {
281 $tempData = array();
282 foreach ($filteredDataByChosenRoot as $filteredDataKey => $value) {
283 if (is_array($value)) {
284 if ($oneLevelDeepArrayValues) {
285 foreach ($value as $keyVal => $val) {
286 if (is_array($val)) {
287 unset($value[$keyVal]);
288 } else {
289 if (!is_numeric($keyVal)) {
290 $value[$keyVal] = $keyVal . ' = ' . $val;
291 }
292 }
293 }
294 $value = implode($arrayValuesSeparator, $value);
295 } else {
296 continue;
297 }
298 }
299 $tempData[$filteredDataKey] = $value;
300 }
301 if(!empty($tempData))
302 $data[] = $tempData;
303 } else {
304 foreach ($filteredDataByChosenRoot as $filteredData) {
305 $tempData = array();
306 foreach ($filteredData as $key => $value) {
307 if (is_array($value)) {
308 if ($oneLevelDeepArrayValues) {
309 foreach ($value as $keyVal => $val) {
310 if (is_array($val)) {
311 unset($value[$keyVal]);
312 } else {
313 if (!is_numeric($keyVal)) {
314 $value[$keyVal] = $keyVal . ' = ' . $val;
315 }
316 }
317 }
318 $value = implode($arrayValuesSeparator, $value);
319 } else {
320 continue;
321 }
322 }
323 $tempData[$key] = $value;
324 }
325 if(!empty($tempData))
326 $data[] = $tempData;
327 }
328 }
329 return apply_filters('wpdatatables_filter_nested_json_final_array', $data, $filteredDataByChosenRoot, $tableID);
330 }
331
332 /**
333 * Prepare final array with same structure
334 * @throws Exception
335 */
336 private function prepareFinalArrayWithSameStructure($data)
337 {
338 $keys = array();
339 $rows = array();
340 foreach ($data as $el) {
341 if (empty($el)) return $rows;
342 if (empty($keys)) {
343 $keys = array_keys($el);
344 continue;
345 }
346 $keys = array_intersect($keys, array_keys($el));
347 }
348
349 foreach ($data as $el) {
350 $row = array();
351 foreach ($el as $key => $value) {
352 if (in_array($key, $keys, true)) {
353 $row[$key] = $value;
354 }
355 }
356 $rows[] = $row;
357 }
358
359 if (empty($rows[0]) || isset($rows[0][0]))
360 throw new Exception('Unable to retrieve data for chosen root path.');
361
362 return $rows;
363 }
364
365 /**
366 * Get root elements from JSON URL
367 */
368 public function prepareRoots($parent, $now, $root, $array)
369 {
370 if (is_null($array)) {
371 return null;
372 }
373
374 $root[] = $parent;
375 foreach ($array as $key => $value) {
376 if (is_array($value) && !empty($value)) {
377 if (!is_numeric($key)) {
378 $now = sprintf('%s%s%s', $parent, '->', $key);
379 $root[] = $now;
380 } else {
381 $now = $parent;
382 }
383 $root = $this->prepareRoots($now, $key, $root, $value);
384 }
385 }
386
387 return array_unique($root);
388 }
389 }
390
391