Skip to content

Commit 2c42a03

Browse files
Merge pull request #947 from Codeinwp/enhancement/731
Compatible with WPML plugin
2 parents e0b2fe4 + bc68b1b commit 2c42a03

File tree

6 files changed

+143
-26
lines changed

6 files changed

+143
-26
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,22 @@ function setScreenOptions( $status, $option, $value ) {
767767
private function getDisplayFilters( &$query_args ) {
768768
$query = array();
769769

770+
if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) {
771+
$current_lang = icl_get_current_language();
772+
if ( in_array( $current_lang, array( 'all', icl_get_default_language() ), true ) ) {
773+
$query[] = array(
774+
'key' => 'chart_lang',
775+
'compare' => 'NOT EXISTS',
776+
);
777+
} else {
778+
$query[] = array(
779+
'key' => 'chart_lang',
780+
'value' => $current_lang,
781+
'compare' => '=',
782+
);
783+
}
784+
}
785+
770786
// add chart type filter to the query arguments
771787
$type = filter_input( INPUT_GET, 'type' );
772788
if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {

classes/Visualizer/Module/Chart.php

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,20 @@ public function deleteChart() {
439439
}
440440
}
441441
if ( $success ) {
442-
wp_delete_post( $chart_id, true );
442+
if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) {
443+
global $sitepress;
444+
$trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
445+
$translations = $sitepress->get_element_translations( $trid );
446+
if ( ! empty( $translations ) ) {
447+
foreach ( $translations as $translated_post ) {
448+
wp_delete_post( $translated_post->element_id, true );
449+
}
450+
} else {
451+
wp_delete_post( $chart_id, true );
452+
}
453+
} else {
454+
wp_delete_post( $chart_id, true );
455+
}
443456
}
444457
if ( $is_post ) {
445458
self::_sendResponse(
@@ -498,32 +511,68 @@ public function renderChartPages() {
498511
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
499512
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
500513
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
501-
$this->deleteOldCharts();
502-
$default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
503-
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
504-
$source->fetch();
505-
$chart_id = wp_insert_post(
506-
array(
507-
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
508-
'post_title' => 'Visualization',
509-
'post_author' => get_current_user_id(),
510-
'post_status' => 'auto-draft',
511-
'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
512-
)
513-
);
514-
if ( $chart_id && ! is_wp_error( $chart_id ) ) {
515-
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
516-
add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
517-
add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
518-
add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
519-
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
520-
add_post_meta(
521-
$chart_id,
522-
Visualizer_Plugin::CF_SETTINGS,
514+
if ( empty( $_GET['lang'] ) || empty( $_GET['parent_chart_id'] ) ) {
515+
$this->deleteOldCharts();
516+
$default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
517+
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
518+
$source->fetch();
519+
$chart_id = wp_insert_post(
523520
array(
524-
'focusTarget' => 'datum',
521+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
522+
'post_title' => 'Visualization',
523+
'post_author' => get_current_user_id(),
524+
'post_status' => 'auto-draft',
525+
'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
525526
)
526527
);
528+
if ( $chart_id && ! is_wp_error( $chart_id ) ) {
529+
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
530+
add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
531+
add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
532+
add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
533+
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
534+
add_post_meta(
535+
$chart_id,
536+
Visualizer_Plugin::CF_SETTINGS,
537+
array(
538+
'focusTarget' => 'datum',
539+
)
540+
);
541+
542+
do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
543+
}
544+
} else {
545+
if ( current_user_can( 'edit_posts' ) ) {
546+
$parent_chart_id = isset( $_GET['parent_chart_id'] ) ? filter_var( $_GET['parent_chart_id'], FILTER_VALIDATE_INT ) : '';
547+
$success = false;
548+
if ( $parent_chart_id ) {
549+
$parent_chart = get_post( $parent_chart_id );
550+
$success = $parent_chart && $parent_chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
551+
}
552+
if ( $success ) {
553+
$new_chart_id = wp_insert_post(
554+
array(
555+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
556+
'post_title' => 'Visualization',
557+
'post_author' => get_current_user_id(),
558+
'post_status' => $parent_chart->post_status,
559+
'post_content' => $parent_chart->post_content,
560+
)
561+
);
562+
563+
if ( is_wp_error( $new_chart_id ) ) {
564+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while cloning chart %d = %s', $parent_chart_id, print_r( $new_chart_id, true ) ), 'error', __FILE__, __LINE__ );
565+
} else {
566+
$post_meta = get_post_meta( $parent_chart_id );
567+
$chart_id = $new_chart_id;
568+
foreach ( $post_meta as $key => $value ) {
569+
if ( strpos( $key, 'visualizer-' ) !== false ) {
570+
add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) );
571+
}
572+
}
573+
}
574+
}
575+
}
527576
do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
528577
}
529578
wp_redirect( esc_url_raw( add_query_arg( 'chart', (int) $chart_id ) ) );

classes/Visualizer/Module/Frontend.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,17 @@ public function renderChart( $atts ) {
305305
$atts
306306
);
307307

308+
if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) {
309+
global $sitepress;
310+
$locale = icl_get_current_language();
311+
$locale = strtolower( str_replace( '_', '-', $locale ) );
312+
$trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
313+
$translations = $sitepress->get_element_translations( $trid );
314+
if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) {
315+
$atts['id'] = $translations[ $locale ]->element_id;
316+
}
317+
}
318+
308319
$chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
309320
// if empty chart does not exists, then return empty string.
310321
if ( ! $chart_data ) {

classes/Visualizer/Render/Library.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ private function _renderChartBox( $placeholder_id, $chart_id, $with_filter = fal
365365
}
366366
echo '<a class="visualizer-chart-action visualizer-chart-shortcode" href="javascript:;" title="', esc_attr__( 'Click to copy shortcode', 'visualizer' ), '" data-clipboard-text="', esc_attr( $shortcode ), '"><span class="dashicons dashicons-admin-page"></span></a>';
367367
echo '<span>&nbsp;</span>';
368+
do_action( 'visualizer_chart_languages', $chart_id );
368369
echo '<hr><div class="visualizer-chart-status"><span title="' . __( 'Chart ID', 'visualizer' ) . '">(' . $chart_id . '):</span> <span class="visualizer-date" title="' . __( 'Last Updated', 'visualizer' ) . '">' . $chart_status['date'] . '</span><span class="visualizer-error"><i class="dashicons ' . $chart_status['icon'] . '" data-viz-error="' . esc_attr( str_replace( '"', "'", $chart_status['error'] ) ) . '" title="' . esc_attr( $chart_status['title'] ) . '"></i></span></div>';
369370
echo '</div>';
370371
echo '</div></div>';

css/library.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,32 @@ div#visualizer-types ul, div#visualizer-types form p {
445445
.google-visualization-controls-rangefilter {
446446
white-space: inherit !important;
447447
}
448+
.visualizer-languages-list {
449+
text-align: right;
450+
}
451+
.visualizer-languages-list a {
452+
text-decoration: none;
453+
display: inline-block;
454+
margin-left: 3px;
455+
padding: 0;
456+
border: none;
457+
outline: none;
458+
box-shadow: none;
459+
}
460+
.visualizer-languages-list a:focus {
461+
border: none;
462+
outline: none;
463+
box-shadow: none;
464+
}
465+
.visualizer-languages-list i {
466+
display: none;
467+
}
468+
.visualizer-languages-list a:hover img {
469+
display: none;
470+
}
471+
.visualizer-languages-list a:hover img + i {
472+
display: block;
473+
}
448474
@media (-webkit-min-device-pixel-ratio: 2) and (min-width: 1000px) and (max-width: 1600px) {
449475
#visualizer-sidebar.one-columns .visualizer-sidebar-box ul li:nth-child(+n+7) {
450476
display: none;

js/library.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,20 @@
9292
}
9393
});
9494

95+
$( '.visualizer-languages-list' ).on( 'click', '[data-lang_code]', function() {
96+
if ( $(this).find( 'i' ).hasClass( 'otgs-ico-add' ) ) {
97+
vu.create = vu.create + '&lang=' + $(this).data('lang_code') + '&parent_chart_id=' + $(this).data('chart');
98+
$('.add-new-chart').click();
99+
} else {
100+
vu.edit = vu.edit + '&lang=' + $(this).data('lang_code') + '&chart=' + $(this).data('chart');
101+
$('.visualizer-chart-edit').click();
102+
}
103+
} );
104+
95105
$('.add-new-chart').click(function () {
96106
var wnd = window,
97107
view = new vmv.Chart({action: vu.create});
108+
vu.create = vu.create.replace(/[\?&]lang=[^&]+/, '').replace(/[\?&]parent_chart_id=[^&]+/, '');
98109

99110
window.parent.addEventListener('message', function(event){
100111
switch(event.data) {
@@ -116,8 +127,11 @@
116127
});
117128

118129
$('.visualizer-chart-edit').click(function () {
119-
var wnd = window,
120-
view = new vmv.Chart({action: vu.edit + '&chart=' + $(this).attr('data-chart')});
130+
var wnd = window;
131+
var view = new vmv.Chart( {
132+
action: vu.edit.indexOf('&chart') != -1 ? vu.edit : vu.edit + '&chart=' + $(this).attr('data-chart')
133+
} );
134+
vu.edit = vu.edit.replace(/[\?&]lang=[^&]+/, '');
121135

122136
wnd.send_to_editor = function () {
123137
wnd.location.href = wnd.location.href.replace(/vaction/, '');

0 commit comments

Comments
 (0)