diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index a364c0d23..9e9e64ae2 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -767,6 +767,22 @@ function setScreenOptions( $status, $option, $value ) { private function getDisplayFilters( &$query_args ) { $query = array(); + if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) { + $current_lang = icl_get_current_language(); + if ( in_array( $current_lang, array( 'all', icl_get_default_language() ), true ) ) { + $query[] = array( + 'key' => 'chart_lang', + 'compare' => 'NOT EXISTS', + ); + } else { + $query[] = array( + 'key' => 'chart_lang', + 'value' => $current_lang, + 'compare' => '=', + ); + } + } + // add chart type filter to the query arguments $type = filter_input( INPUT_GET, 'type' ); if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) { diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 7dc438712..1beee34a6 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -439,7 +439,20 @@ public function deleteChart() { } } if ( $success ) { - wp_delete_post( $chart_id, true ); + if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) { + global $sitepress; + $trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); + $translations = $sitepress->get_element_translations( $trid ); + if ( ! empty( $translations ) ) { + foreach ( $translations as $translated_post ) { + wp_delete_post( $translated_post->element_id, true ); + } + } else { + wp_delete_post( $chart_id, true ); + } + } else { + wp_delete_post( $chart_id, true ); + } } if ( $is_post ) { self::_sendResponse( @@ -498,32 +511,68 @@ public function renderChartPages() { // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { - $this->deleteOldCharts(); - $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; - $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); - $source->fetch(); - $chart_id = wp_insert_post( - array( - 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, - 'post_title' => 'Visualization', - 'post_author' => get_current_user_id(), - 'post_status' => 'auto-draft', - 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ), - ) - ); - if ( $chart_id && ! is_wp_error( $chart_id ) ) { - add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type ); - add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 ); - add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); - add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); - add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' ); - add_post_meta( - $chart_id, - Visualizer_Plugin::CF_SETTINGS, + if ( empty( $_GET['lang'] ) || empty( $_GET['parent_chart_id'] ) ) { + $this->deleteOldCharts(); + $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; + $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); + $source->fetch(); + $chart_id = wp_insert_post( array( - 'focusTarget' => 'datum', + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_title' => 'Visualization', + 'post_author' => get_current_user_id(), + 'post_status' => 'auto-draft', + 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ), ) ); + if ( $chart_id && ! is_wp_error( $chart_id ) ) { + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type ); + add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 ); + add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); + add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' ); + add_post_meta( + $chart_id, + Visualizer_Plugin::CF_SETTINGS, + array( + 'focusTarget' => 'datum', + ) + ); + + do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); + } + } else { + if ( current_user_can( 'edit_posts' ) ) { + $parent_chart_id = isset( $_GET['parent_chart_id'] ) ? filter_var( $_GET['parent_chart_id'], FILTER_VALIDATE_INT ) : ''; + $success = false; + if ( $parent_chart_id ) { + $parent_chart = get_post( $parent_chart_id ); + $success = $parent_chart && $parent_chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; + } + if ( $success ) { + $new_chart_id = wp_insert_post( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_title' => 'Visualization', + 'post_author' => get_current_user_id(), + 'post_status' => $parent_chart->post_status, + 'post_content' => $parent_chart->post_content, + ) + ); + + if ( is_wp_error( $new_chart_id ) ) { + 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__ ); + } else { + $post_meta = get_post_meta( $parent_chart_id ); + $chart_id = $new_chart_id; + foreach ( $post_meta as $key => $value ) { + if ( strpos( $key, 'visualizer-' ) !== false ) { + add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) ); + } + } + } + } + } do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); } wp_redirect( esc_url_raw( add_query_arg( 'chart', (int) $chart_id ) ) ); diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 27a21050c..a1908e668 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -305,6 +305,17 @@ public function renderChart( $atts ) { $atts ); + if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) { + global $sitepress; + $locale = icl_get_current_language(); + $locale = strtolower( str_replace( '_', '-', $locale ) ); + $trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); + $translations = $sitepress->get_element_translations( $trid ); + if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) { + $atts['id'] = $translations[ $locale ]->element_id; + } + } + $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] ); // if empty chart does not exists, then return empty string. if ( ! $chart_data ) { diff --git a/classes/Visualizer/Render/Library.php b/classes/Visualizer/Render/Library.php index fdadf26ca..2d4063027 100644 --- a/classes/Visualizer/Render/Library.php +++ b/classes/Visualizer/Render/Library.php @@ -365,6 +365,7 @@ private function _renderChartBox( $placeholder_id, $chart_id, $with_filter = fal } echo ''; echo ' '; + do_action( 'visualizer_chart_languages', $chart_id ); echo '
(' . $chart_id . '): ' . $chart_status['date'] . '
'; echo ''; echo ''; diff --git a/css/library.css b/css/library.css index 70e47d26a..3080d49e6 100644 --- a/css/library.css +++ b/css/library.css @@ -445,6 +445,32 @@ div#visualizer-types ul, div#visualizer-types form p { .google-visualization-controls-rangefilter { white-space: inherit !important; } +.visualizer-languages-list { + text-align: right; +} +.visualizer-languages-list a { + text-decoration: none; + display: inline-block; + margin-left: 3px; + padding: 0; + border: none; + outline: none; + box-shadow: none; +} +.visualizer-languages-list a:focus { + border: none; + outline: none; + box-shadow: none; +} +.visualizer-languages-list i { + display: none; +} +.visualizer-languages-list a:hover img { + display: none; +} +.visualizer-languages-list a:hover img + i { + display: block; +} @media (-webkit-min-device-pixel-ratio: 2) and (min-width: 1000px) and (max-width: 1600px) { #visualizer-sidebar.one-columns .visualizer-sidebar-box ul li:nth-child(+n+7) { display: none; diff --git a/js/library.js b/js/library.js index 503fbc110..5164dbb68 100644 --- a/js/library.js +++ b/js/library.js @@ -92,9 +92,20 @@ } }); + $( '.visualizer-languages-list' ).on( 'click', '[data-lang_code]', function() { + if ( $(this).find( 'i' ).hasClass( 'otgs-ico-add' ) ) { + vu.create = vu.create + '&lang=' + $(this).data('lang_code') + '&parent_chart_id=' + $(this).data('chart'); + $('.add-new-chart').click(); + } else { + vu.edit = vu.edit + '&lang=' + $(this).data('lang_code') + '&chart=' + $(this).data('chart'); + $('.visualizer-chart-edit').click(); + } + } ); + $('.add-new-chart').click(function () { var wnd = window, view = new vmv.Chart({action: vu.create}); + vu.create = vu.create.replace(/[\?&]lang=[^&]+/, '').replace(/[\?&]parent_chart_id=[^&]+/, ''); window.parent.addEventListener('message', function(event){ switch(event.data) { @@ -116,8 +127,11 @@ }); $('.visualizer-chart-edit').click(function () { - var wnd = window, - view = new vmv.Chart({action: vu.edit + '&chart=' + $(this).attr('data-chart')}); + var wnd = window; + var view = new vmv.Chart( { + action: vu.edit.indexOf('&chart') != -1 ? vu.edit : vu.edit + '&chart=' + $(this).attr('data-chart') + } ); + vu.edit = vu.edit.replace(/[\?&]lang=[^&]+/, ''); wnd.send_to_editor = function () { wnd.location.href = wnd.location.href.replace(/vaction/, '');