| @@ -121,9 +121,9 @@ | ||
| 121 | 121 | } |
| 122 | 122 | if ( isset( $this->_args['additional_headers'] ) ) { |
| 123 | 123 | $this->_additional_headers = $this->_args['additional_headers']; |
| 124 | 124 | } |
| 125 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Constructor called for params = %s', print_r( $params, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 125 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON source initialized.', 'debug', __FILE__, __LINE__ ); | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
| 129 | 129 | * Get the root elements for JSON-endpoint. |
| @@ -282,9 +282,9 @@ | ||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $this->_data = $data; |
| 285 | 285 | |
| 286 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Parsed data endpoint %s with root %s is %s', $this->_url, $this->_root, print_r( $data, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 286 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON endpoint data parsed.', 'debug', __FILE__, __LINE__ ); | |
| 287 | 287 | |
| 288 | 288 | return true; |
| 289 | 289 | } |
| 290 | 290 | |
| @@ -320,9 +320,8 @@ | ||
| 320 | 320 | $rows[] = $row; |
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | return $rows; |
| 324 | - | |
| 325 | 324 | } |
| 326 | 325 | |
| 327 | 326 | /** |
| 328 | 327 | * Get the next page URL. |
| @@ -330,9 +329,9 @@ | ||
| 330 | 329 | * @since ? |
| 331 | 330 | * |
| 332 | 331 | * @access private |
| 333 | 332 | */ |
| 334 | - private function getNextPage( $array ) { | |
| 333 | + private function getNextPage( $data ) { | |
| 335 | 334 | if ( empty( $this->_paging ) ) { |
| 336 | 335 | return null; |
| 337 | 336 | } |
| 338 | 337 | |
| @@ -338,12 +337,12 @@ | ||
| 338 | 337 | |
| 339 | 338 | $root = explode( self::TAG_SEPARATOR, $this->_paging ); |
| 340 | 339 | // get rid of the first element as that is the faux root element indicator |
| 341 | 340 | array_shift( $root ); |
| 342 | - $leaf = $array; | |
| 341 | + $leaf = $data; | |
| 343 | 342 | foreach ( $root as $tag ) { |
| 344 | 343 | if ( array_key_exists( $tag, $leaf ) ) { |
| 345 | - $leaf = $array[ $tag ]; | |
| 344 | + $leaf = $leaf[ $tag ]; | |
| 346 | 345 | } else { |
| 347 | 346 | // if the tag does not exist, we assume it is present in the 0th element of the current array. |
| 348 | 347 | // TODO: we may want to change this to a filter later. |
| 349 | 348 | $leaf = $leaf[0][ $tag ]; |
| @@ -361,27 +360,27 @@ | ||
| 361 | 360 | * @since ? |
| 362 | 361 | * |
| 363 | 362 | * @access private |
| 364 | 363 | */ |
| 365 | - private function getRootElements( $parent, $now, $root, $array ) { | |
| 366 | - if ( is_null( $array ) ) { | |
| 364 | + private function getRootElements( $parent_key, $now, $root, $data ) { | |
| 365 | + if ( is_null( $data ) ) { | |
| 367 | 366 | return null; |
| 368 | 367 | } |
| 369 | 368 | |
| 370 | - $root[] = $parent; | |
| 371 | - foreach ( $array as $key => $value ) { | |
| 369 | + $root[] = $parent_key; | |
| 370 | + foreach ( $data as $key => $value ) { | |
| 372 | 371 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 373 | 372 | if ( ! is_numeric( $key ) ) { |
| 374 | - $now = sprintf( '%s%s%s', $parent, self::TAG_SEPARATOR, $key ); | |
| 373 | + $now = sprintf( '%s%s%s', $parent_key, self::TAG_SEPARATOR, $key ); | |
| 375 | 374 | $root[] = $now; |
| 376 | 375 | } else { |
| 377 | - $now = $parent; | |
| 376 | + $now = $parent_key; | |
| 378 | 377 | } |
| 379 | 378 | $root = $this->getRootElements( $now, $key, $root, $value ); |
| 380 | 379 | } |
| 381 | 380 | } |
| 382 | 381 | $roots = array_unique( $root ); |
| 383 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Roots found for %s = %s', $this->_url, print_r( $roots, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 382 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON root elements parsed.', 'debug', __FILE__, __LINE__ ); | |
| 384 | 383 | return $roots; |
| 385 | 384 | } |
| 386 | 385 | |
| 387 | 386 | /** |
| @@ -397,9 +396,10 @@ | ||
| 397 | 396 | } |
| 398 | 397 | |
| 399 | 398 | $response = $this->connect( $url ); |
| 400 | 399 | if ( is_wp_error( $response ) || ! in_array( intval( $response['response']['code'] ), array( 200, 201 ), true ) ) { |
| 401 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint %s = %s', $url, print_r( $response, true ) ), 'error', __FILE__, __LINE__ ); | |
| 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__ ); | |
| 402 | 402 | return null; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | // this filter can be used to rearrange columns e.g. in candlestick charts |
| @@ -408,9 +408,9 @@ | ||
| 408 | 408 | $response_body = wp_remote_retrieve_body( $response ); |
| 409 | 409 | $response_body = preg_replace( "/^$bom/", '', $response_body ); |
| 410 | 410 | $array = apply_filters( 'visualizer_json_massage_data', json_decode( $response_body, true ), $url ); |
| 411 | 411 | |
| 412 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'JSON array for the endpoint is %s = ', print_r( $array, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 412 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON response decoded.', 'debug', __FILE__, __LINE__ ); | |
| 413 | 413 | |
| 414 | 414 | return $array; |
| 415 | 415 | } |
| 416 | 416 | |
| @@ -441,9 +441,9 @@ | ||
| 441 | 441 | $this->_additional_headers = explode( ',', $this->_additional_headers ); |
| 442 | 442 | $this->_additional_headers = array_filter( $this->_additional_headers ); |
| 443 | 443 | if ( ! empty( $this->_additional_headers ) ) { |
| 444 | 444 | $this->_additional_headers = array_map( |
| 445 | - function( $headers ) { | |
| 445 | + function ( $headers ) { | |
| 446 | 446 | $headers = explode( ':', $headers ); |
| 447 | 447 | $headers = array_map( 'trim', $headers ); |
| 448 | 448 | return $headers; |
| 449 | 449 | }, |
| @@ -456,10 +456,21 @@ | ||
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Connecting to %s with args = %s ', $url, print_r( $args, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 461 | - return wp_remote_request( $url, $args ); | |
| 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 ); | |
| 462 | 473 | } |
| 463 | 474 | |
| 464 | 475 | /** |
| 465 | 476 | * Refresh the data for the provided series. |
| @@ -488,8 +499,53 @@ | ||
| 488 | 499 | return true; |
| 489 | 500 | } |
| 490 | 501 | |
| 491 | 502 | /** |
| 503 | + * Check if the URL is a WooCommerce endpoint request. | |
| 504 | + * | |
| 505 | + * @access private | |
| 506 | + * @param string $url The URL to check. | |
| 507 | + * @return bool True if it's a WooCommerce request, false otherwise. | |
| 508 | + */ | |
| 509 | + private function is_woocommerce_request( $url ) { | |
| 510 | + if ( empty( $url ) ) { | |
| 511 | + return false; | |
| 512 | + } | |
| 513 | + | |
| 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; | |
| 545 | + } | |
| 546 | + | |
| 547 | + /** | |
| 492 | 548 | * Returns source name. |
| 493 | 549 | * |
| 494 | 550 | * @since 1.0.0 |
| 495 | 551 | * |
| @@ -498,6 +554,5 @@ | ||
| 498 | 554 | */ |
| 499 | 555 | public function getSourceName() { |
| 500 | 556 | return __CLASS__; |
| 501 | 557 | } |
| 502 | - | |
| 503 | 558 | } |