| @@ -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,19 +90,9 @@ | ||
| 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 | - } | |
| 94 | + | |
| 125 | 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 | /** |
| @@ -136,14 +106,9 @@ | ||
| 136 | 106 | $roots = apply_filters( 'visualizer_json_get_root_elements', false, $this->_url ); |
| 137 | 107 | if ( false !== $roots ) { |
| 138 | 108 | return $roots; |
| 139 | 109 | } |
| 140 | - $roots = $this->getRootElements( 'root', '', array(), $this->getJSON() ); | |
| 141 | - if ( is_null( $roots ) ) { | |
| 142 | - $this->_error = esc_html__( 'This does not appear to be a valid JSON feed. Please try again.', 'visualizer' ); | |
| 143 | - return false; | |
| 144 | - } | |
| 145 | - return $roots; | |
| 110 | + return $this->getRootElements( 'root', '', array(), $this->getJSON() ); | |
| 146 | 111 | } |
| 147 | 112 | |
| 148 | 113 | /** |
| 149 | 114 | * Get the name of the elements that are likely to contain the paginated URLs. |
| @@ -183,15 +148,9 @@ | ||
| 183 | 148 | |
| 184 | 149 | $paging = array(); |
| 185 | 150 | foreach ( $leaf as $key => $value ) { |
| 186 | 151 | // 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'] ) ) { | |
| 152 | + if ( is_string( $value ) && 0 === stripos( $value, $this->_url ) ) { | |
| 194 | 153 | $paging[] = $key; |
| 195 | 154 | } |
| 196 | 155 | } |
| 197 | 156 | |
| @@ -207,9 +166,9 @@ | ||
| 207 | 166 | * @since ? |
| 208 | 167 | * |
| 209 | 168 | * @access public |
| 210 | 169 | */ |
| 211 | - public function fetch() { | |
| 170 | + public function parse() { | |
| 212 | 171 | $url = $this->_url; |
| 213 | 172 | $data = array(); |
| 214 | 173 | $page = 1; |
| 215 | 174 | |
| @@ -215,39 +174,25 @@ | ||
| 215 | 174 | |
| 216 | 175 | while ( ! is_null( $url ) && $page++ < apply_filters( 'visualizer_json_fetch_pages', 5, $this->_url ) ) { |
| 217 | 176 | $array = $this->getJSON( $url ); |
| 218 | 177 | if ( is_null( $array ) ) { |
| 219 | - break; | |
| 178 | + return $data; | |
| 220 | 179 | } |
| 221 | 180 | |
| 222 | 181 | $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 | - } | |
| 182 | + // get rid of the first element as that is the faux root element indicator | |
| 183 | + array_shift( $root ); | |
| 227 | 184 | $leaf = $array; |
| 228 | 185 | foreach ( $root as $tag ) { |
| 229 | 186 | if ( array_key_exists( $tag, $leaf ) ) { |
| 230 | 187 | $leaf = $leaf[ $tag ]; |
| 231 | 188 | } else { |
| 232 | - // if the tag does not exist, we assume it is present in every element of the current array. | |
| 233 | - $temp = array(); | |
| 234 | - for ( $depth = 0; $depth < count( $leaf ); $depth++ ) { | |
| 235 | - if ( ! isset( $leaf[ $depth ][ $tag ] ) ) { | |
| 236 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Element %s not present at depth %d in %d elements. Ignoring.', $tag, $depth, count( $leaf ) ), 'debug', __FILE__, __LINE__ ); | |
| 237 | - continue; | |
| 238 | - } | |
| 239 | - $temp[] = $leaf[ $depth ][ $tag ]; | |
| 240 | - } | |
| 241 | - $leaf = $temp; | |
| 189 | + // if the tag does not exist, we assume it is present in the 0th element of the current array. | |
| 190 | + // TODO: we may want to change this to a filter later. | |
| 191 | + $leaf = $leaf[0][ $tag ]; | |
| 242 | 192 | } |
| 243 | 193 | } |
| 244 | 194 | |
| 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 | 195 | // now that we have got the final array we need to operate on, we will use this as the collection of series. |
| 251 | 196 | // lets check if the series is a flat-series e.g. https://api.exchangeratesapi.io/latest |
| 252 | 197 | // in this, all values of `$leaf` would be a string, not an array. |
| 253 | 198 | $values = array_values( $leaf ); |
| @@ -258,9 +203,8 @@ | ||
| 258 | 203 | $inner_data[ $datum ] = $value; |
| 259 | 204 | } |
| 260 | 205 | $data[] = $inner_data; |
| 261 | 206 | } else { |
| 262 | - $row_num = 0; | |
| 263 | 207 | // we will filter out all elements of this array that have array as a value. |
| 264 | 208 | foreach ( $leaf as $datum ) { |
| 265 | 209 | $inner_data = array(); |
| 266 | 210 | foreach ( $datum as $key => $value ) { |
| @@ -268,64 +212,24 @@ | ||
| 268 | 212 | continue; |
| 269 | 213 | } |
| 270 | 214 | $inner_data[ $key ] = $value; |
| 271 | 215 | } |
| 272 | - // if we want to exclude entire rows on the basis of some data/key or row index. | |
| 273 | - if ( apply_filters( 'visualizer_json_include_row', true, $inner_data, $this->_root, $this->_url, ++$row_num ) ) { | |
| 216 | + // if we want to exclude entire rows on the basis of some data/key. | |
| 217 | + if ( apply_filters( 'visualizer_json_include_row', true, $inner_data, $this->_root, $this->_url ) ) { | |
| 274 | 218 | $data[] = $inner_data; |
| 275 | 219 | } |
| 276 | 220 | } |
| 277 | 221 | } |
| 278 | 222 | |
| 279 | - $data = $this->collectCommonElements( $data ); | |
| 280 | - | |
| 281 | 223 | $url = $this->getNextPage( $array ); |
| 282 | 224 | } |
| 283 | 225 | |
| 284 | - $this->_data = $data; | |
| 226 | + 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 | 227 | |
| 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__ ); | |
| 287 | - | |
| 288 | - return true; | |
| 228 | + return $data; | |
| 289 | 229 | } |
| 290 | 230 | |
| 291 | 231 | /** |
| 292 | - * Determines which keys are common to all the data and returns an array with only those elements. | |
| 293 | - * This is to ensure that the data set displayed on the table is consistent. | |
| 294 | - * Inconsistent data causes the table to not display. | |
| 295 | - * | |
| 296 | - * @since ? | |
| 297 | - * | |
| 298 | - * @access private | |
| 299 | - */ | |
| 300 | - private function collectCommonElements( $data ) { | |
| 301 | - $keys = array(); | |
| 302 | - foreach ( $data as $datum ) { | |
| 303 | - if ( empty( $keys ) ) { | |
| 304 | - $keys = array_keys( $datum ); | |
| 305 | - continue; | |
| 306 | - } | |
| 307 | - $keys = array_intersect( $keys, array_keys( $datum ) ); | |
| 308 | - } | |
| 309 | - | |
| 310 | - 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 | - | |
| 312 | - $rows = array(); | |
| 313 | - foreach ( $data as $datum ) { | |
| 314 | - $row = array(); | |
| 315 | - foreach ( $datum as $key => $value ) { | |
| 316 | - if ( in_array( $key, $keys, true ) ) { | |
| 317 | - $row[ $key ] = $value; | |
| 318 | - } | |
| 319 | - } | |
| 320 | - $rows[] = $row; | |
| 321 | - } | |
| 322 | - | |
| 323 | - return $rows; | |
| 324 | - | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | 232 | * Get the next page URL. |
| 329 | 233 | * |
| 330 | 234 | * @since ? |
| 331 | 235 | * |
| @@ -363,12 +267,11 @@ | ||
| 363 | 267 | * @access private |
| 364 | 268 | */ |
| 365 | 269 | private function getRootElements( $parent, $now, $root, $array ) { |
| 366 | 270 | if ( is_null( $array ) ) { |
| 367 | - return null; | |
| 271 | + return array(); | |
| 368 | 272 | } |
| 369 | 273 | |
| 370 | - $root[] = $parent; | |
| 371 | 274 | foreach ( $array as $key => $value ) { |
| 372 | 275 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 373 | 276 | if ( ! is_numeric( $key ) ) { |
| 374 | 277 | $now = sprintf( '%s%s%s', $parent, self::TAG_SEPARATOR, $key ); |
| @@ -378,10 +281,10 @@ | ||
| 378 | 281 | } |
| 379 | 282 | $root = $this->getRootElements( $now, $key, $root, $value ); |
| 380 | 283 | } |
| 381 | 284 | } |
| 382 | - $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__ ); | |
| 285 | + $roots = array_filter( array_unique( $root ) ); | |
| 286 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Roots found for %s = ', $this->_url, print_r( $roots, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 384 | 287 | return $roots; |
| 385 | 288 | } |
| 386 | 289 | |
| 387 | 290 | /** |
| @@ -394,21 +297,16 @@ | ||
| 394 | 297 | private function getJSON( $url = null ) { |
| 395 | 298 | if ( is_null( $url ) ) { |
| 396 | 299 | $url = $this->_url; |
| 397 | 300 | } |
| 398 | - | |
| 399 | - $response = $this->connect( $url ); | |
| 400 | - 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__ ); | |
| 301 | + // allow hooks to use any other args such as method=POST. | |
| 302 | + $response = wp_remote_request( $url, apply_filters( 'visualizer_json_args', array( 'method' => 'GET' ), $url ) ); | |
| 303 | + if ( is_wp_error( $response ) ) { | |
| 304 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint %s = ', $url, print_r( $response, true ) ), 'error', __FILE__, __LINE__ ); | |
| 402 | 305 | return null; |
| 403 | 306 | } |
| 404 | 307 | |
| 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 ); | |
| 308 | + $array = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 411 | 309 | |
| 412 | 310 | do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'JSON array for the endpoint is %s = ', print_r( $array, true ) ), 'debug', __FILE__, __LINE__ ); |
| 413 | 311 | |
| 414 | 312 | return $array; |
| @@ -413,56 +311,79 @@ | ||
| 413 | 311 | |
| 414 | 312 | return $array; |
| 415 | 313 | } |
| 416 | 314 | |
| 315 | + | |
| 417 | 316 | /** |
| 418 | - * Sets the authentication/authorization headers and connects to the endpoint. | |
| 317 | + * Fetches series information. This is fetched only through the UI and not while refreshing the chart data. | |
| 419 | 318 | * |
| 420 | - * @since ? | |
| 319 | + * @since 1.0.0 | |
| 421 | 320 | * |
| 422 | 321 | * @access private |
| 423 | 322 | */ |
| 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 ) ) ); | |
| 323 | + private function _fetchSeries() { | |
| 324 | + $params = $this->_args; | |
| 325 | + $headers = array_filter( $params['header'] ); | |
| 326 | + $types = array_filter( $params['type'] ); | |
| 327 | + $header_row = $type_row = array(); | |
| 328 | + if ( $headers ) { | |
| 329 | + foreach ( $headers as $header ) { | |
| 330 | + if ( ! empty( $types[ $header ] ) ) { | |
| 331 | + $this->_series[] = array( | |
| 332 | + 'label' => $header, | |
| 333 | + 'type' => $types[ $header ], | |
| 334 | + ); | |
| 433 | 335 | } |
| 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 | 336 | } |
| 437 | 337 | } |
| 438 | 338 | |
| 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; | |
| 339 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Series found for %s = ', $this->_url, print_r( $this->_series, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 340 | + | |
| 341 | + return true; | |
| 342 | + } | |
| 343 | + | |
| 344 | + /** | |
| 345 | + * Fetches data information. | |
| 346 | + * | |
| 347 | + * @since 1.0.0 | |
| 348 | + * | |
| 349 | + * @access private | |
| 350 | + */ | |
| 351 | + private function _fetchData() { | |
| 352 | + $params = $this->_args; | |
| 353 | + | |
| 354 | + $headers = wp_list_pluck( $this->_series, 'label' ); | |
| 355 | + $data = $this->parse(); | |
| 356 | + foreach ( $data as $line ) { | |
| 357 | + $data_row = array(); | |
| 358 | + foreach ( $line as $header => $value ) { | |
| 359 | + if ( in_array( $header, $headers, true ) ) { | |
| 360 | + $data_row[] = $value; | |
| 456 | 361 | } |
| 457 | 362 | } |
| 363 | + $this->_data[] = $this->_normalizeData( $data_row ); | |
| 458 | 364 | } |
| 459 | 365 | |
| 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 ); | |
| 366 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Data found for %s = ', $this->_url, print_r( $this->_data, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 367 | + | |
| 368 | + return true; | |
| 462 | 369 | } |
| 463 | 370 | |
| 464 | 371 | /** |
| 372 | + * Fetches information from source, parses it and builds series and data arrays. | |
| 373 | + * | |
| 374 | + * @since 1.0.0 | |
| 375 | + * | |
| 376 | + * @access public | |
| 377 | + * @return boolean TRUE on success, otherwise FALSE. | |
| 378 | + */ | |
| 379 | + public function fetch() { | |
| 380 | + $this->_fetchSeries(); | |
| 381 | + $this->_fetchData(); | |
| 382 | + return true; | |
| 383 | + } | |
| 384 | + | |
| 385 | + /** | |
| 465 | 386 | * Refresh the data for the provided series. |
| 466 | 387 | * |
| 467 | 388 | * @since ? |
| 468 | 389 | * |
| @@ -469,23 +390,9 @@ | ||
| 469 | 390 | * @access public |
| 470 | 391 | */ |
| 471 | 392 | public function refresh( $series ) { |
| 472 | 393 | $this->_series = $series; |
| 473 | - $this->fetch(); | |
| 474 | - | |
| 475 | - $headers = wp_list_pluck( $this->_series, 'label' ); | |
| 476 | - $data = array(); | |
| 477 | - foreach ( $this->_data as $line ) { | |
| 478 | - $data_row = array(); | |
| 479 | - foreach ( $line as $header => $value ) { | |
| 480 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 481 | - if ( in_array( $header, $headers ) ) { | |
| 482 | - $data_row[] = $value; | |
| 483 | - } | |
| 484 | - } | |
| 485 | - $data[] = $this->_normalizeData( $data_row ); | |
| 486 | - } | |
| 487 | - $this->_data = $data; | |
| 394 | + $this->_fetchData(); | |
| 488 | 395 | return true; |
| 489 | 396 | } |
| 490 | 397 | |
| 491 | 398 | /** |