| @@ -53,8 +53,9 @@ | ||
| 53 | 53 | public function __construct( Visualizer_Plugin $plugin ) { |
| 54 | 54 | parent::__construct( $plugin ); |
| 55 | 55 | |
| 56 | 56 | $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 57 | + $this->_addAction( 'load-index.php', 'enqueueScripts' ); | |
| 57 | 58 | $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' ); |
| 58 | 59 | $this->_addFilter( 'visualizer_get_language', 'getLanguage' ); |
| 59 | 60 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 60 | 61 | |
| @@ -352,8 +353,12 @@ | ||
| 352 | 353 | if ( $chart_image ) { |
| 353 | 354 | return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>'; |
| 354 | 355 | } |
| 355 | 356 | } |
| 357 | + // Unset chart image base64 string. | |
| 358 | + if ( isset( $settings['chart-img'] ) ) { | |
| 359 | + unset( $settings['chart-img'] ); | |
| 360 | + } | |
| 356 | 361 | |
| 357 | 362 | // add chart to the array |
| 358 | 363 | $this->_charts[ $id ] = array( |
| 359 | 364 | 'type' => $type, |
| @@ -394,24 +399,31 @@ | ||
| 394 | 399 | } |
| 395 | 400 | |
| 396 | 401 | $actions_div .= $css; |
| 397 | 402 | |
| 403 | + $_charts = array(); | |
| 404 | + $_charts_type = ''; | |
| 398 | 405 | foreach ( $this->_charts as $id => $array ) { |
| 406 | + $_charts = $this->_charts; | |
| 399 | 407 | $library = $array['library']; |
| 400 | - wp_register_script( | |
| 401 | - "visualizer-render-$library", | |
| 402 | - VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 403 | - apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 404 | - Visualizer_Plugin::VERSION, | |
| 405 | - true | |
| 406 | - ); | |
| 407 | - | |
| 408 | - wp_enqueue_script( "visualizer-render-$library" ); | |
| 408 | + $_charts_type = $library; | |
| 409 | + if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) { | |
| 410 | + wp_register_script( | |
| 411 | + "visualizer-render-$library", | |
| 412 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 413 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 414 | + Visualizer_Plugin::VERSION, | |
| 415 | + true | |
| 416 | + ); | |
| 417 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + if ( wp_script_is( "visualizer-render-$_charts_type" ) ) { | |
| 409 | 421 | wp_localize_script( |
| 410 | - "visualizer-render-$library", | |
| 422 | + "visualizer-render-$_charts_type", | |
| 411 | 423 | 'visualizer', |
| 412 | 424 | array( |
| 413 | - 'charts' => $this->_charts, | |
| 425 | + 'charts' => $_charts, | |
| 414 | 426 | 'language' => $this->get_language(), |
| 415 | 427 | 'map_api_key' => get_option( 'visualizer-map-api-key' ), |
| 416 | 428 | 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', |
| 417 | 429 | 'wp_nonce' => wp_create_nonce( 'wp_rest' ), |
| @@ -421,10 +433,10 @@ | ||
| 421 | 433 | 'page_type' => 'frontend', |
| 422 | 434 | 'is_front' => true, |
| 423 | 435 | ) |
| 424 | 436 | ); |
| 425 | - wp_enqueue_style( 'visualizer-front' ); | |
| 426 | 437 | } |
| 438 | + wp_enqueue_style( 'visualizer-front' ); | |
| 427 | 439 | |
| 428 | 440 | // return placeholder div |
| 429 | 441 | return $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ); |
| 430 | 442 | } |
| @@ -569,15 +581,27 @@ | ||
| 569 | 581 | |
| 570 | 582 | // Get chart by ID. |
| 571 | 583 | $chart = get_post( $chart_id ); |
| 572 | 584 | if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) { |
| 585 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 586 | + $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); | |
| 587 | + | |
| 588 | + if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) { | |
| 589 | + $diff_total_series = abs( count( $settings['series'] ) - count( $series ) ); | |
| 590 | + if ( $diff_total_series ) { | |
| 591 | + foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) { | |
| 592 | + $settings['series'][] = end( $settings['series'] ); | |
| 593 | + } | |
| 594 | + } | |
| 595 | + } | |
| 573 | 596 | $chart_data = array( |
| 574 | 597 | 'chart' => $chart, |
| 575 | 598 | 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), |
| 576 | - 'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ), | |
| 577 | - 'series' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), | |
| 599 | + 'settings' => $settings, | |
| 600 | + 'series' => $series, | |
| 578 | 601 | 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ), |
| 579 | 602 | ); |
| 603 | + | |
| 580 | 604 | // Put the results in a transient. Expire after 12 hours. |
| 581 | 605 | set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) ); |
| 582 | 606 | return $chart_data; |
| 583 | 607 | } |