Skip to content

Commit ec7f84a

Browse files
committed
fix: multiple charts in same page not showing for different lib
1 parent 2f237cf commit ec7f84a

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

classes/Visualizer/Module/Frontend.php

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -483,19 +483,35 @@ public function renderChart( $atts ) {
483483
$_charts = array();
484484
$_charts_type = '';
485485
$count = 0;
486+
static $visualizer_localized = false;
487+
static $visualizer_charts = array();
488+
static $d3_localized = false;
489+
$facade_handle = 'visualizer-render-facade';
486490
foreach ( $this->_charts as $id => $array ) {
487491
$_charts = $this->_charts;
488492
$library = $array['library'];
489493
$_charts_type = $library;
490-
if ( ! wp_script_is( "visualizer-render-$library", 'registered' ) ) {
494+
$facade_deps = apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true );
495+
if ( ! wp_script_is( $facade_handle, 'registered' ) ) {
491496
wp_register_script(
492-
"visualizer-render-$library",
497+
$facade_handle,
493498
VISUALIZER_ABSURL . 'js/render-facade.js',
494-
apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
499+
$facade_deps,
495500
Visualizer_Plugin::VERSION,
496501
true
497502
);
498-
wp_enqueue_script( "visualizer-render-$library" );
503+
wp_enqueue_script( $facade_handle );
504+
} else {
505+
// Ensure newly discovered dependencies are attached to the facade.
506+
global $wp_scripts;
507+
if ( isset( $wp_scripts->registered[ $facade_handle ] ) ) {
508+
$wp_scripts->registered[ $facade_handle ]->deps = array_unique(
509+
array_merge( $wp_scripts->registered[ $facade_handle ]->deps, $facade_deps )
510+
);
511+
}
512+
foreach ( $facade_deps as $dep_handle ) {
513+
wp_enqueue_script( $dep_handle );
514+
}
499515
}
500516

501517
// For D3 charts, also enqueue the dedicated renderer bundle.
@@ -517,32 +533,50 @@ public function renderChart( $atts ) {
517533
);
518534
wp_enqueue_script( 'visualizer-d3-renderer' );
519535
}
520-
wp_localize_script(
521-
'visualizer-d3-renderer',
522-
'vizD3Renderer',
523-
array(
524-
'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js',
525-
)
526-
);
536+
if ( ! $d3_localized ) {
537+
wp_localize_script(
538+
'visualizer-d3-renderer',
539+
'vizD3Renderer',
540+
array(
541+
'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js',
542+
)
543+
);
544+
$d3_localized = true;
545+
}
527546
}
528547

529-
if ( wp_script_is( "visualizer-render-$_charts_type" ) && 0 === $count ) {
530-
wp_localize_script(
531-
"visualizer-render-$_charts_type",
532-
'visualizer',
533-
array(
534-
'charts' => $this->_charts,
535-
'language' => $this->get_language(),
536-
'map_api_key' => get_option( 'visualizer-map-api-key' ),
537-
'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
538-
'wp_nonce' => wp_create_nonce( 'wp_rest' ),
539-
'i10n' => array(
540-
'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
541-
),
542-
'page_type' => 'frontend',
543-
'is_front' => true,
544-
)
545-
);
548+
if ( wp_script_is( $facade_handle ) && 0 === $count ) {
549+
if ( ! $visualizer_localized ) {
550+
$visualizer_charts = $this->_charts;
551+
wp_localize_script(
552+
$facade_handle,
553+
'visualizer',
554+
array(
555+
'charts' => $this->_charts,
556+
'language' => $this->get_language(),
557+
'map_api_key' => get_option( 'visualizer-map-api-key' ),
558+
'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
559+
'wp_nonce' => wp_create_nonce( 'wp_rest' ),
560+
'i10n' => array(
561+
'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
562+
),
563+
'page_type' => 'frontend',
564+
'is_front' => true,
565+
)
566+
);
567+
$visualizer_localized = true;
568+
} else {
569+
$new_charts = array_diff_key( $this->_charts, $visualizer_charts );
570+
if ( ! empty( $new_charts ) ) {
571+
wp_add_inline_script(
572+
$facade_handle,
573+
'window.visualizer = window.visualizer || {};' .
574+
'window.visualizer.charts = Object.assign(window.visualizer.charts || {}, ' . wp_json_encode( $new_charts ) . ');',
575+
'before'
576+
);
577+
$visualizer_charts = array_merge( $visualizer_charts, $new_charts );
578+
}
579+
}
546580
}
547581
++$count;
548582
}

js/render-google.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ var isResizeRequest = false;
564564
// check what all chart types to load.
565565
$chart_types = [];
566566
$.each(v.charts, function(i, c){
567+
// Only consider charts rendered by Google Charts.
568+
if ( c.library && c.library !== 'google' && c.library !== 'GoogleCharts' ) {
569+
return;
570+
}
567571
var $type = c.type;
568572
switch($type){
569573
case 'bar':
@@ -594,7 +598,7 @@ var isResizeRequest = false;
594598
$type = null;
595599
break;
596600
}
597-
if($type != null){
601+
if($type != null && $type !== ''){
598602
$chart_types.push($type);
599603
}
600604
});

0 commit comments

Comments
 (0)