| @@ -69,18 +69,8 @@ | ||
| 69 | 69 | */ |
| 70 | 70 | protected $_headers; |
| 71 | 71 | |
| 72 | 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 | 73 | * The array that contains the definition of the data. |
| 84 | 74 | * |
| 85 | 75 | * @since 1.0.0 |
| 86 | 76 | * |
| @@ -118,12 +108,9 @@ | ||
| 118 | 108 | } |
| 119 | 109 | if ( isset( $this->_args['method'] ) ) { |
| 120 | 110 | $this->_headers['method'] = $this->_args['method']; |
| 121 | 111 | } |
| 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__ ); | |
| 112 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Constructor called for params = %s', print_r( $params, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 126 | 113 | } |
| 127 | 114 | |
| 128 | 115 | /** |
| 129 | 116 | * Get the root elements for JSON-endpoint. |
| @@ -282,9 +269,9 @@ | ||
| 282 | 269 | } |
| 283 | 270 | |
| 284 | 271 | $this->_data = $data; |
| 285 | 272 | |
| 286 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON endpoint data parsed.', 'debug', __FILE__, __LINE__ ); | |
| 273 | + 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__ ); | |
| 287 | 274 | |
| 288 | 275 | return true; |
| 289 | 276 | } |
| 290 | 277 | |
| @@ -320,8 +307,9 @@ | ||
| 320 | 307 | $rows[] = $row; |
| 321 | 308 | } |
| 322 | 309 | |
| 323 | 310 | return $rows; |
| 311 | + | |
| 324 | 312 | } |
| 325 | 313 | |
| 326 | 314 | /** |
| 327 | 315 | * Get the next page URL. |
| @@ -329,9 +317,9 @@ | ||
| 329 | 317 | * @since ? |
| 330 | 318 | * |
| 331 | 319 | * @access private |
| 332 | 320 | */ |
| 333 | - private function getNextPage( $data ) { | |
| 321 | + private function getNextPage( $array ) { | |
| 334 | 322 | if ( empty( $this->_paging ) ) { |
| 335 | 323 | return null; |
| 336 | 324 | } |
| 337 | 325 | |
| @@ -337,12 +325,12 @@ | ||
| 337 | 325 | |
| 338 | 326 | $root = explode( self::TAG_SEPARATOR, $this->_paging ); |
| 339 | 327 | // get rid of the first element as that is the faux root element indicator |
| 340 | 328 | array_shift( $root ); |
| 341 | - $leaf = $data; | |
| 329 | + $leaf = $array; | |
| 342 | 330 | foreach ( $root as $tag ) { |
| 343 | 331 | if ( array_key_exists( $tag, $leaf ) ) { |
| 344 | - $leaf = $leaf[ $tag ]; | |
| 332 | + $leaf = $array[ $tag ]; | |
| 345 | 333 | } else { |
| 346 | 334 | // if the tag does not exist, we assume it is present in the 0th element of the current array. |
| 347 | 335 | // TODO: we may want to change this to a filter later. |
| 348 | 336 | $leaf = $leaf[0][ $tag ]; |
| @@ -360,27 +348,27 @@ | ||
| 360 | 348 | * @since ? |
| 361 | 349 | * |
| 362 | 350 | * @access private |
| 363 | 351 | */ |
| 364 | - private function getRootElements( $parent_key, $now, $root, $data ) { | |
| 365 | - if ( is_null( $data ) ) { | |
| 352 | + private function getRootElements( $parent, $now, $root, $array ) { | |
| 353 | + if ( is_null( $array ) ) { | |
| 366 | 354 | return null; |
| 367 | 355 | } |
| 368 | 356 | |
| 369 | - $root[] = $parent_key; | |
| 370 | - foreach ( $data as $key => $value ) { | |
| 357 | + $root[] = $parent; | |
| 358 | + foreach ( $array as $key => $value ) { | |
| 371 | 359 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 372 | 360 | if ( ! is_numeric( $key ) ) { |
| 373 | - $now = sprintf( '%s%s%s', $parent_key, self::TAG_SEPARATOR, $key ); | |
| 361 | + $now = sprintf( '%s%s%s', $parent, self::TAG_SEPARATOR, $key ); | |
| 374 | 362 | $root[] = $now; |
| 375 | 363 | } else { |
| 376 | - $now = $parent_key; | |
| 364 | + $now = $parent; | |
| 377 | 365 | } |
| 378 | 366 | $root = $this->getRootElements( $now, $key, $root, $value ); |
| 379 | 367 | } |
| 380 | 368 | } |
| 381 | 369 | $roots = array_unique( $root ); |
| 382 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON root elements parsed.', 'debug', __FILE__, __LINE__ ); | |
| 370 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Roots found for %s = %s', $this->_url, print_r( $roots, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 383 | 371 | return $roots; |
| 384 | 372 | } |
| 385 | 373 | |
| 386 | 374 | /** |
| @@ -396,21 +384,17 @@ | ||
| 396 | 384 | } |
| 397 | 385 | |
| 398 | 386 | $response = $this->connect( $url ); |
| 399 | 387 | 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__ ); | |
| 388 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint %s = %s', $url, print_r( $response, true ) ), 'error', __FILE__, __LINE__ ); | |
| 402 | 389 | return null; |
| 403 | 390 | } |
| 404 | 391 | |
| 405 | 392 | // this filter can be used to rearrange columns e.g. in candlestick charts |
| 406 | 393 | // 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 ); | |
| 394 | + $array = apply_filters( 'visualizer_json_massage_data', json_decode( wp_remote_retrieve_body( $response ), true ), $url ); | |
| 411 | 395 | |
| 412 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON response decoded.', 'debug', __FILE__, __LINE__ ); | |
| 396 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'JSON array for the endpoint is %s = ', print_r( $array, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 413 | 397 | |
| 414 | 398 | return $array; |
| 415 | 399 | } |
| 416 | 400 | |
| @@ -425,13 +409,9 @@ | ||
| 425 | 409 | $args = array( 'method' => 'GET' ); |
| 426 | 410 | if ( ! empty( $this->_headers ) ) { |
| 427 | 411 | $args = array( 'method' => strtoupper( $this->_headers['method'] ) ); |
| 428 | 412 | 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 ) ) ); | |
| 433 | - } | |
| 413 | + $args['headers'] = array( 'Authorization' => $this->_headers['auth'] ); | |
| 434 | 414 | } elseif ( array_key_exists( 'username', $this->_headers ) && array_key_exists( 'password', $this->_headers ) && ! empty( $this->_headers['username'] ) && ! empty( $this->_headers['password'] ) ) { |
| 435 | 415 | $args['headers'] = array( 'Authorization' => 'Basic ' . base64_encode( $this->_headers['username'] . ':' . $this->_headers['password'] ) ); |
| 436 | 416 | } |
| 437 | 417 | } |
| @@ -436,41 +416,11 @@ | ||
| 436 | 416 | } |
| 437 | 417 | } |
| 438 | 418 | |
| 439 | 419 | $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 | - } | |
| 420 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Connecting to %s with args = %s ', $url, print_r( $args, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 459 | 421 | |
| 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 ); | |
| 422 | + return wp_remote_request( $url, $args ); | |
| 473 | 423 | } |
| 474 | 424 | |
| 475 | 425 | /** |
| 476 | 426 | * Refresh the data for the provided series. |
| @@ -499,53 +449,8 @@ | ||
| 499 | 449 | return true; |
| 500 | 450 | } |
| 501 | 451 | |
| 502 | 452 | /** |
| 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 | - /** | |
| 548 | 453 | * Returns source name. |
| 549 | 454 | * |
| 550 | 455 | * @since 1.0.0 |
| 551 | 456 | * |
| @@ -554,5 +459,6 @@ | ||
| 554 | 459 | */ |
| 555 | 460 | public function getSourceName() { |
| 556 | 461 | return __CLASS__; |
| 557 | 462 | } |
| 463 | + | |
| 558 | 464 | } |