Skip to content

Commit e573329

Browse files
Add wpml plugin support #731
1 parent 622e2cf commit e573329

File tree

6 files changed

+162
-3
lines changed

6 files changed

+162
-3
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public function __construct( Visualizer_Plugin $plugin ) {
7474

7575
$this->_addAction( 'admin_init', 'init' );
7676

77+
// Multilingual support.
78+
$this->_addAction( 'visualizer_chart_languages', 'addMultilingualSupport' );
79+
7780
if ( defined( 'TI_CYPRESS_TESTING' ) ) {
7881
$this->load_cypress_hooks();
7982
}
@@ -767,6 +770,13 @@ function setScreenOptions( $status, $option, $value ) {
767770
private function getDisplayFilters( &$query_args ) {
768771
$query = array();
769772

773+
if ( function_exists( 'icl_get_languages' ) ) {
774+
$query[] = array(
775+
'key' => 'chart_lang',
776+
'compare' => 'NOT EXISTS',
777+
);
778+
}
779+
770780
// add chart type filter to the query arguments
771781
$type = filter_input( INPUT_GET, 'type' );
772782
if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
@@ -1105,4 +1115,43 @@ public static function proFeaturesLocked() {
11051115
}
11061116
return 'yes' === get_option( 'visualizer-new-user', 'yes' ) ? false : true;
11071117
}
1118+
1119+
/**
1120+
* Multilingual Support.
1121+
*
1122+
* @return bool Default false
1123+
*/
1124+
public static function addMultilingualSupport( $chart_id ) {
1125+
if ( function_exists( 'icl_get_languages' ) ) {
1126+
$language = icl_get_languages();
1127+
$current_lang = icl_get_current_language();
1128+
global $sitepress;
1129+
$trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER );
1130+
$translations = $sitepress->get_element_translations( $trid );
1131+
?>
1132+
<hr><div class="visualizer-languages-list">
1133+
<?php
1134+
foreach ( $language as $lang ) {
1135+
$lang_code = $lang['code'];
1136+
if ( $current_lang !== $lang_code ) {
1137+
$lang_chart_exist = get_post_meta( $chart_id, Visualizer_Plugin::NAME . '_' . $lang_code, true );
1138+
$translate_info = isset( $translations[ $lang_code ] ) ? $translations[ $lang_code ] : false;
1139+
?>
1140+
<a href="javascript:;" data-lang_code="<?php echo esc_attr( $lang_code ); ?>" data-chart="<?php echo $translate_info ? $translate_info->element_id : $chart_id; ?>">
1141+
<img src="<?php echo esc_url( $lang['country_flag_url'] ); ?>" alt="<?php echo esc_attr( $lang['translated_name'] ); ?>">
1142+
<?php if ( $translate_info ) : ?>
1143+
<i class="otgs-ico-edit"></i>
1144+
<?php else : ?>
1145+
<i class="otgs-ico-add"></i>
1146+
<?php endif; ?>
1147+
</a>
1148+
<?php
1149+
}
1150+
}
1151+
?>
1152+
</div>
1153+
<?php
1154+
}
1155+
return;
1156+
}
11081157
}

classes/Visualizer/Module/Chart.php

Lines changed: 59 additions & 1 deletion
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 ( 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(
@@ -524,6 +537,12 @@ public function renderChartPages() {
524537
'focusTarget' => 'datum',
525538
)
526539
);
540+
541+
if ( ! empty( $_GET['lang'] ) && ! empty( $_GET['parent_chart_id'] ) ) {
542+
$parent_chart_id = (int) $_GET['parent_chart_id'];
543+
add_post_meta( $chart_id, 'chart_lang', $_GET['lang'] );
544+
$this->set_wpml_element_language_details( $parent_chart_id, $chart_id, $_GET['lang'] );
545+
}
527546
do_action( 'visualizer_pro_new_chart_defaults', $chart_id );
528547
}
529548
wp_redirect( esc_url_raw( add_query_arg( 'chart', (int) $chart_id ) ) );
@@ -1522,4 +1541,43 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru
15221541
$attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename );
15231542
return $attach_id;
15241543
}
1544+
1545+
/**
1546+
* WPML set element language details.
1547+
*
1548+
* @param int $post_id Post ID.
1549+
* @param int $translated_post_id Translated post ID.
1550+
* @param string $language_code Selected language code.
1551+
*/
1552+
public function set_wpml_element_language_details( $post_id = 0, $translated_post_id = 0, $language_code = '' ) {
1553+
global $sitepress;
1554+
if ( $post_id && ! empty( $language_code ) ) {
1555+
$post_type = Visualizer_Plugin::CPT_VISUALIZER;
1556+
$wpml_element_type = apply_filters( 'wpml_element_type', $post_type );
1557+
$trid = $sitepress->get_element_trid( $post_id, 'post_' . $post_type );
1558+
$recursive = false;
1559+
$original_post_id = $translated_post_id;
1560+
$original_lang_code = $language_code;
1561+
if ( empty( $trid ) ) {
1562+
$translated_post_id = $post_id;
1563+
$trid = $post_id;
1564+
$recursive = true;
1565+
$language_code = icl_get_default_language();
1566+
}
1567+
1568+
$language_args = array(
1569+
'element_id' => $translated_post_id,
1570+
'element_type' => $wpml_element_type,
1571+
'trid' => $trid,
1572+
'language_code' => $language_code,
1573+
'source_language_code' => ! $recursive ? icl_get_default_language() : null,
1574+
);
1575+
// Set language details.
1576+
do_action( 'wpml_set_element_language_details', $language_args );
1577+
1578+
if ( $recursive ) {
1579+
$this->set_wpml_element_language_details( $post_id, $original_post_id, $original_lang_code );
1580+
}
1581+
}
1582+
}
15251583
}

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 ( function_exists( 'icl_get_languages' ) ) {
309+
global $sitepress;
310+
$locale = get_locale();
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 ), '"></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
@@ -435,6 +435,32 @@ div#visualizer-types ul, div#visualizer-types form p {
435435
.google-visualization-controls-rangefilter {
436436
white-space: inherit !important;
437437
}
438+
.visualizer-languages-list {
439+
text-align: right;
440+
}
441+
.visualizer-languages-list a {
442+
text-decoration: none;
443+
display: inline-block;
444+
margin-left: 3px;
445+
padding: 0;
446+
border: none;
447+
outline: none;
448+
box-shadow: none;
449+
}
450+
.visualizer-languages-list a:focus {
451+
border: none;
452+
outline: none;
453+
box-shadow: none;
454+
}
455+
.visualizer-languages-list i {
456+
display: none;
457+
}
458+
.visualizer-languages-list a:hover img {
459+
display: none;
460+
}
461+
.visualizer-languages-list a:hover img + i {
462+
display: block;
463+
}
438464
@media (-webkit-min-device-pixel-ratio: 2) and (min-width: 1000px) and (max-width: 1600px) {
439465
#visualizer-sidebar.one-columns .visualizer-sidebar-box ul li:nth-child(+n+7) {
440466
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)