PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
← All changes | classes/Visualizer/Source/Json.php +78 -175 4.0.63.3.2 View file →
@@ -59,28 +59,8 @@
59 59 */
60 60 protected $_paging;
61 61
62 62 /**
63 - * The headers.
64 - *
65 - * @since ?
66 - *
67 - * @access protected
68 - * @var array
69 - */
70 - protected $_headers;
71 -
72 - /**
73 - * The other headers.
74 - *
75 - * @since 3.4.11
76 - *
77 - * @access protected
78 - * @var array
79 - */
80 - protected $_additional_headers;
81 -
82 - /**
83 63 * The array that contains the definition of the data.
84 64 *
85 65 * @since 1.0.0
86 66 *
@@ -110,20 +90,10 @@
110 90 }
111 91 if ( isset( $this->_args['paging'] ) ) {
112 92 $this->_paging = trim( $this->_args['paging'] );
113 93 }
114 - if ( isset( $this->_args['auth'] ) && ! empty( $this->_args['auth'] ) ) {
115 - $this->_headers = array( 'auth' => $this->_args['auth'] );
116 - } elseif ( isset( $this->_args['username'] ) && isset( $this->_args['password'] ) ) {
117 - $this->_headers = array( 'username' => $this->_args['username'], 'password' => $this->_args['password'] );
118 - }
119 - if ( isset( $this->_args['method'] ) ) {
120 - $this->_headers['method'] = $this->_args['method'];
121 - }
122 - if ( isset( $this->_args['additional_headers'] ) ) {
123 - $this->_additional_headers = $this->_args['additional_headers'];
124 - }
125 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON source initialized.', 'debug', __FILE__, __LINE__ );
94 +
95 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Constructor called for params = %s', print_r( $params, true ) ), 'debug', __FILE__, __LINE__ );
126 96 }
127 97
128 98 /**
129 99 * Get the root elements for JSON-endpoint.
@@ -137,9 +107,9 @@
137 107 if ( false !== $roots ) {
138 108 return $roots;
139 109 }
140 110 $roots = $this->getRootElements( 'root', '', array(), $this->getJSON() );
141 - if ( is_null( $roots ) ) {
111 + if ( empty( $roots ) ) {
142 112 $this->_error = esc_html__( 'This does not appear to be a valid JSON feed. Please try again.', 'visualizer' );
143 113 return false;
144 114 }
145 115 return $roots;
@@ -183,15 +153,9 @@
183 153
184 154 $paging = array();
185 155 foreach ( $leaf as $key => $value ) {
186 156 // the paging element's value will most probably contain the url of the feed.
187 - if ( ! is_string( $value ) ) {
188 - continue;
189 - }
190 -
191 - // strip the url off the request parameters e.g. format=json as sometimes the pagination urls may not contain them.
192 - $url = wp_parse_url( $value );
193 - if ( 0 === stripos( $value, $this->_url ) || false !== stripos( $this->_url, $url['path'] ) ) {
157 + if ( is_string( $value ) && 0 === stripos( $value, $this->_url ) ) {
194 158 $paging[] = $key;
195 159 }
196 160 }
197 161
@@ -207,9 +171,9 @@
207 171 * @since ?
208 172 *
209 173 * @access public
210 174 */
211 - public function fetch() {
175 + public function parse() {
212 176 $url = $this->_url;
213 177 $data = array();
214 178 $page = 1;
215 179
@@ -215,16 +179,14 @@
215 179
216 180 while ( ! is_null( $url ) && $page++ < apply_filters( 'visualizer_json_fetch_pages', 5, $this->_url ) ) {
217 181 $array = $this->getJSON( $url );
218 182 if ( is_null( $array ) ) {
219 - break;
183 + return $data;
220 184 }
221 185
222 186 $root = explode( self::TAG_SEPARATOR, $this->_root );
223 - if ( count( $root ) > 1 ) {
224 - // get rid of the first element as that is the faux root element indicator
225 - array_shift( $root );
226 - }
187 + // get rid of the first element as that is the faux root element indicator
188 + array_shift( $root );
227 189 $leaf = $array;
228 190 foreach ( $root as $tag ) {
229 191 if ( array_key_exists( $tag, $leaf ) ) {
230 192 $leaf = $leaf[ $tag ];
@@ -241,13 +203,8 @@
241 203 $leaf = $temp;
242 204 }
243 205 }
244 206
245 - // JSONs that do not have a root element may fall into this condition e.g. /wp-json/wp/v2/posts
246 - if ( empty( $leaf ) ) {
247 - $leaf = $array;
248 - }
249 -
250 207 // now that we have got the final array we need to operate on, we will use this as the collection of series.
251 208 // lets check if the series is a flat-series e.g. https://api.exchangeratesapi.io/latest
252 209 // in this, all values of `$leaf` would be a string, not an array.
253 210 $values = array_values( $leaf );
@@ -280,13 +237,11 @@
280 237
281 238 $url = $this->getNextPage( $array );
282 239 }
283 240
284 - $this->_data = $data;
241 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Parsed data endpoint %s with rooot %s is %s = ', $this->_url, $this->_root, print_r( $data, true ) ), 'debug', __FILE__, __LINE__ );
285 242
286 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON endpoint data parsed.', 'debug', __FILE__, __LINE__ );
287 -
288 - return true;
243 + return $data;
289 244 }
290 245
291 246 /**
292 247 * Determines which keys are common to all the data and returns an array with only those elements.
@@ -310,9 +265,8 @@
310 265 do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Extracting data only for the common keys = %s', print_r( $keys, true ) ), 'debug', __FILE__, __LINE__ );
311 266
312 267 $rows = array();
313 268 foreach ( $data as $datum ) {
314 - $row = array();
315 269 foreach ( $datum as $key => $value ) {
316 270 if ( in_array( $key, $keys, true ) ) {
317 271 $row[ $key ] = $value;
318 272 }
@@ -320,8 +274,9 @@
320 274 $rows[] = $row;
321 275 }
322 276
323 277 return $rows;
278 +
324 279 }
325 280
326 281 /**
327 282 * Get the next page URL.
@@ -329,9 +284,9 @@
329 284 * @since ?
330 285 *
331 286 * @access private
332 287 */
333 - private function getNextPage( $data ) {
288 + private function getNextPage( $array ) {
334 289 if ( empty( $this->_paging ) ) {
335 290 return null;
336 291 }
337 292
@@ -337,12 +292,12 @@
337 292
338 293 $root = explode( self::TAG_SEPARATOR, $this->_paging );
339 294 // get rid of the first element as that is the faux root element indicator
340 295 array_shift( $root );
341 - $leaf = $data;
296 + $leaf = $array;
342 297 foreach ( $root as $tag ) {
343 298 if ( array_key_exists( $tag, $leaf ) ) {
344 - $leaf = $leaf[ $tag ];
299 + $leaf = $array[ $tag ];
345 300 } else {
346 301 // if the tag does not exist, we assume it is present in the 0th element of the current array.
347 302 // TODO: we may want to change this to a filter later.
348 303 $leaf = $leaf[0][ $tag ];
@@ -360,27 +315,26 @@
360 315 * @since ?
361 316 *
362 317 * @access private
363 318 */
364 - private function getRootElements( $parent_key, $now, $root, $data ) {
365 - if ( is_null( $data ) ) {
366 - return null;
319 + private function getRootElements( $parent, $now, $root, $array ) {
320 + if ( is_null( $array ) ) {
321 + return array();
367 322 }
368 323
369 - $root[] = $parent_key;
370 - foreach ( $data as $key => $value ) {
324 + foreach ( $array as $key => $value ) {
371 325 if ( is_array( $value ) && ! empty( $value ) ) {
372 326 if ( ! is_numeric( $key ) ) {
373 - $now = sprintf( '%s%s%s', $parent_key, self::TAG_SEPARATOR, $key );
327 + $now = sprintf( '%s%s%s', $parent, self::TAG_SEPARATOR, $key );
374 328 $root[] = $now;
375 329 } else {
376 - $now = $parent_key;
330 + $now = $parent;
377 331 }
378 332 $root = $this->getRootElements( $now, $key, $root, $value );
379 333 }
380 334 }
381 - $roots = array_unique( $root );
382 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON root elements parsed.', 'debug', __FILE__, __LINE__ );
335 + $roots = array_filter( array_unique( $root ) );
336 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Roots found for %s = ', $this->_url, print_r( $roots, true ) ), 'debug', __FILE__, __LINE__ );
383 337 return $roots;
384 338 }
385 339
386 340 /**
@@ -393,100 +347,64 @@
393 347 private function getJSON( $url = null ) {
394 348 if ( is_null( $url ) ) {
395 349 $url = $this->_url;
396 350 }
397 -
398 - $response = $this->connect( $url );
399 - if ( is_wp_error( $response ) || ! in_array( intval( $response['response']['code'] ), array( 200, 201 ), true ) ) {
400 - $error_code = is_wp_error( $response ) ? $response->get_error_code() : (string) wp_remote_retrieve_response_code( $response );
401 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint: %s', $error_code ), 'error', __FILE__, __LINE__ );
351 + // allow hooks to use any other args such as method=POST.
352 + $response = wp_remote_request( $url, apply_filters( 'visualizer_json_args', array( 'method' => 'GET' ), $url ) );
353 + if ( is_wp_error( $response ) ) {
354 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint %s = ', $url, print_r( $response, true ) ), 'error', __FILE__, __LINE__ );
402 355 return null;
403 356 }
404 357
405 - // this filter can be used to rearrange columns e.g. in candlestick charts
406 - // or to add additional data to the root.
407 - $bom = pack( 'H*', 'EFBBBF' );
408 - $response_body = wp_remote_retrieve_body( $response );
409 - $response_body = preg_replace( "/^$bom/", '', $response_body );
410 - $array = apply_filters( 'visualizer_json_massage_data', json_decode( $response_body, true ), $url );
358 + $array = json_decode( wp_remote_retrieve_body( $response ), true );
411 359
412 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON response decoded.', 'debug', __FILE__, __LINE__ );
360 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'JSON array for the endpoint is %s = ', print_r( $array, true ) ), 'debug', __FILE__, __LINE__ );
413 361
414 362 return $array;
415 363 }
416 364
365 +
417 366 /**
418 - * Sets the authentication/authorization headers and connects to the endpoint.
367 + * Fetches series information. This is fetched only through the UI and not while refreshing the chart data.
419 368 *
420 - * @since ?
369 + * @since 1.0.0
421 370 *
422 371 * @access private
423 372 */
424 - private function connect( $url ) {
425 - $args = array( 'method' => 'GET' );
426 - if ( ! empty( $this->_headers ) ) {
427 - $args = array( 'method' => strtoupper( $this->_headers['method'] ) );
428 - if ( array_key_exists( 'auth', $this->_headers ) ) {
429 - if ( ! empty( $this->_headers['auth'] ) ) {
430 - $header_auth = explode( ':', $this->_headers['auth'] );
431 - $header_key = explode( ' ', reset( $header_auth ) );
432 - $args['headers'] = array( 'Authorization' => reset( $header_key ) . ' ' . base64_encode( end( $header_key ) . ':' . end( $header_auth ) ) );
373 + private function _fetchSeries() {
374 + $params = $this->_args;
375 + $headers = array_filter( $params['header'] );
376 + $types = array_filter( $params['type'] );
377 + $header_row = $type_row = array();
378 + if ( $headers ) {
379 + foreach ( $headers as $header ) {
380 + if ( ! empty( $types[ $header ] ) ) {
381 + $this->_series[] = array(
382 + 'label' => $header,
383 + 'type' => $types[ $header ],
384 + );
433 385 }
434 - } elseif ( array_key_exists( 'username', $this->_headers ) && array_key_exists( 'password', $this->_headers ) && ! empty( $this->_headers['username'] ) && ! empty( $this->_headers['password'] ) ) {
435 - $args['headers'] = array( 'Authorization' => 'Basic ' . base64_encode( $this->_headers['username'] . ':' . $this->_headers['password'] ) );
436 386 }
437 387 }
438 388
439 - $args = apply_filters( 'visualizer_json_args', $args, $url );
440 - if ( ! empty( $this->_additional_headers ) ) {
441 - $this->_additional_headers = explode( ',', $this->_additional_headers );
442 - $this->_additional_headers = array_filter( $this->_additional_headers );
443 - if ( ! empty( $this->_additional_headers ) ) {
444 - $this->_additional_headers = array_map(
445 - function ( $headers ) {
446 - $headers = explode( ':', $headers );
447 - $headers = array_map( 'trim', $headers );
448 - return $headers;
449 - },
450 - $this->_additional_headers
451 - );
452 - foreach ( $this->_additional_headers as $additional_headers ) {
453 - $header_key = reset( $additional_headers );
454 - $header_value = end( $additional_headers );
455 - $args['headers'][ $header_key ] = $header_value;
456 - }
457 - }
458 - }
389 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Series found for %s = ', $this->_url, print_r( $this->_series, true ) ), 'debug', __FILE__, __LINE__ );
459 390
460 - // Check if this is a WooCommerce endpoint request and add verification token.
461 - if ( $this->is_woocommerce_request( $url ) ) {
462 - // Generate a unique token for this specific request.
463 - $token = wp_generate_password( 32, false );
464 - set_transient( 'visualizer_wc_token_' . $token, time(), 60 );
465 - if ( ! isset( $args['headers'] ) ) {
466 - $args['headers'] = array();
467 - }
468 - $args['headers']['X-Visualizer-Token'] = $token;
469 - }
470 -
471 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Connecting to JSON endpoint with method %s', $args['method'] ), 'debug', __FILE__, __LINE__ );
472 - return Visualizer_Remote_Fetch::request( $url, $args );
391 + return true;
473 392 }
474 393
475 394 /**
476 - * Refresh the data for the provided series.
395 + * Fetches data information.
477 396 *
478 - * @since ?
397 + * @since 1.0.0
479 398 *
480 - * @access public
399 + * @access private
481 400 */
482 - public function refresh( $series ) {
483 - $this->_series = $series;
484 - $this->fetch();
401 + private function _fetchData() {
402 + $params = $this->_args;
485 403
486 404 $headers = wp_list_pluck( $this->_series, 'label' );
487 - $data = array();
488 - foreach ( $this->_data as $line ) {
405 + $data = $this->parse();
406 + foreach ( $data as $line ) {
489 407 $data_row = array();
490 408 foreach ( $line as $header => $value ) {
491 409 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
492 410 if ( in_array( $header, $headers ) ) {
@@ -492,57 +410,41 @@
492 410 if ( in_array( $header, $headers ) ) {
493 411 $data_row[] = $value;
494 412 }
495 413 }
496 - $data[] = $this->_normalizeData( $data_row );
414 + $this->_data[] = $this->_normalizeData( $data_row );
497 415 }
498 - $this->_data = $data;
416 +
417 + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Data found for %s = ', $this->_url, print_r( $this->_data, true ) ), 'debug', __FILE__, __LINE__ );
418 +
499 419 return true;
500 420 }
501 421
502 422 /**
503 - * Check if the URL is a WooCommerce endpoint request.
423 + * Fetches information from source, parses it and builds series and data arrays.
504 424 *
505 - * @access private
506 - * @param string $url The URL to check.
507 - * @return bool True if it's a WooCommerce request, false otherwise.
425 + * @since 1.0.0
426 + *
427 + * @access public
428 + * @return boolean TRUE on success, otherwise FALSE.
508 429 */
509 - private function is_woocommerce_request( $url ) {
510 - if ( empty( $url ) ) {
511 - return false;
512 - }
430 + public function fetch() {
431 + $this->_fetchSeries();
432 + $this->_fetchData();
433 + return true;
434 + }
513 435
514 - $parsed_url = function_exists( 'wp_parse_url' ) ? wp_parse_url( $url ) : parse_url( $url );
515 - if ( empty( $parsed_url ) || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
516 - return false;
517 - }
518 -
519 - $site_url = function_exists( 'home_url' ) ? home_url() : ( function_exists( 'site_url' ) ? site_url() : '' );
520 - $site_parts = $site_url ? ( function_exists( 'wp_parse_url' ) ? wp_parse_url( $site_url ) : parse_url( $site_url ) ) : array();
521 - if ( empty( $site_parts['host'] ) ) {
522 - return false;
523 - }
524 -
525 - $target_host = strtolower( $parsed_url['host'] );
526 - $site_host = strtolower( $site_parts['host'] );
527 - if ( $target_host !== $site_host ) {
528 - return false;
529 - }
530 -
531 - $path = '/' . ltrim( $parsed_url['path'], '/' );
532 - $wc_patterns = array(
533 - '/wp-json/wc/',
534 - '/wp-json/wc-analytics/',
535 - '/wc-analytics/',
536 - );
537 -
538 - foreach ( $wc_patterns as $pattern ) {
539 - if ( strpos( $path, $pattern ) !== false ) {
540 - return true;
541 - }
542 - }
543 -
544 - return false;
436 + /**
437 + * Refresh the data for the provided series.
438 + *
439 + * @since ?
440 + *
441 + * @access public
442 + */
443 + public function refresh( $series ) {
444 + $this->_series = $series;
445 + $this->_fetchData();
446 + return true;
545 447 }
546 448
547 449 /**
548 450 * Returns source name.
@@ -554,5 +456,6 @@
554 456 */
555 457 public function getSourceName() {
556 458 return __CLASS__;
557 459 }
460 +
558 461 }