From e573329444b726f056b650c5152ebae67ff186a6 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 11 Oct 2022 12:46:04 +0530 Subject: [PATCH 1/4] Add wpml plugin support #731 --- classes/Visualizer/Module/Admin.php | 49 +++++++++++++++++++++ classes/Visualizer/Module/Chart.php | 60 +++++++++++++++++++++++++- classes/Visualizer/Module/Frontend.php | 11 +++++ classes/Visualizer/Render/Library.php | 1 + css/library.css | 26 +++++++++++ js/library.js | 18 +++++++- 6 files changed, 162 insertions(+), 3 deletions(-) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index 46cbd141a..f05135393 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -74,6 +74,9 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addAction( 'admin_init', 'init' ); + // Multilingual support. + $this->_addAction( 'visualizer_chart_languages', 'addMultilingualSupport' ); + if ( defined( 'TI_CYPRESS_TESTING' ) ) { $this->load_cypress_hooks(); } @@ -767,6 +770,13 @@ function setScreenOptions( $status, $option, $value ) { private function getDisplayFilters( &$query_args ) { $query = array(); + if ( function_exists( 'icl_get_languages' ) ) { + $query[] = array( + 'key' => 'chart_lang', + 'compare' => 'NOT EXISTS', + ); + } + // add chart type filter to the query arguments $type = filter_input( INPUT_GET, 'type' ); if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) { @@ -1105,4 +1115,43 @@ public static function proFeaturesLocked() { } return 'yes' === get_option( 'visualizer-new-user', 'yes' ) ? false : true; } + + /** + * Multilingual Support. + * + * @return bool Default false + */ + public static function addMultilingualSupport( $chart_id ) { + if ( function_exists( 'icl_get_languages' ) ) { + $language = icl_get_languages(); + $current_lang = icl_get_current_language(); + global $sitepress; + $trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); + $translations = $sitepress->get_element_translations( $trid ); + ?> +
+ + + <?php echo esc_attr( $lang['translated_name'] ); ?> + + + + + + + +
+ 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( @@ -524,6 +537,12 @@ public function renderChartPages() { 'focusTarget' => 'datum', ) ); + + if ( ! empty( $_GET['lang'] ) && ! empty( $_GET['parent_chart_id'] ) ) { + $parent_chart_id = (int) $_GET['parent_chart_id']; + add_post_meta( $chart_id, 'chart_lang', $_GET['lang'] ); + $this->set_wpml_element_language_details( $parent_chart_id, $chart_id, $_GET['lang'] ); + } do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); } 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 $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename ); return $attach_id; } + + /** + * WPML set element language details. + * + * @param int $post_id Post ID. + * @param int $translated_post_id Translated post ID. + * @param string $language_code Selected language code. + */ + public function set_wpml_element_language_details( $post_id = 0, $translated_post_id = 0, $language_code = '' ) { + global $sitepress; + if ( $post_id && ! empty( $language_code ) ) { + $post_type = Visualizer_Plugin::CPT_VISUALIZER; + $wpml_element_type = apply_filters( 'wpml_element_type', $post_type ); + $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post_type ); + $recursive = false; + $original_post_id = $translated_post_id; + $original_lang_code = $language_code; + if ( empty( $trid ) ) { + $translated_post_id = $post_id; + $trid = $post_id; + $recursive = true; + $language_code = icl_get_default_language(); + } + + $language_args = array( + 'element_id' => $translated_post_id, + 'element_type' => $wpml_element_type, + 'trid' => $trid, + 'language_code' => $language_code, + 'source_language_code' => ! $recursive ? icl_get_default_language() : null, + ); + // Set language details. + do_action( 'wpml_set_element_language_details', $language_args ); + + if ( $recursive ) { + $this->set_wpml_element_language_details( $post_id, $original_post_id, $original_lang_code ); + } + } + } } diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 27a21050c..26b785922 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -305,6 +305,17 @@ public function renderChart( $atts ) { $atts ); + if ( function_exists( 'icl_get_languages' ) ) { + global $sitepress; + $locale = get_locale(); + $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 1acfb257f..5b1fa8d5d 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 9f6fdd882..e4907ca84 100644 --- a/css/library.css +++ b/css/library.css @@ -435,6 +435,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/, ''); From ae91c41aeaa99eee6f1745e29ea9d679a3a6e968 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 11 Oct 2022 15:18:18 +0530 Subject: [PATCH 2/4] Move pro feature #731 --- classes/Visualizer/Module/Admin.php | 61 ++++++-------------------- classes/Visualizer/Module/Chart.php | 46 +------------------ classes/Visualizer/Module/Frontend.php | 2 +- 3 files changed, 16 insertions(+), 93 deletions(-) diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index f05135393..7b884815b 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -74,9 +74,6 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addAction( 'admin_init', 'init' ); - // Multilingual support. - $this->_addAction( 'visualizer_chart_languages', 'addMultilingualSupport' ); - if ( defined( 'TI_CYPRESS_TESTING' ) ) { $this->load_cypress_hooks(); } @@ -770,11 +767,20 @@ function setScreenOptions( $status, $option, $value ) { private function getDisplayFilters( &$query_args ) { $query = array(); - if ( function_exists( 'icl_get_languages' ) ) { - $query[] = array( - 'key' => 'chart_lang', - 'compare' => 'NOT EXISTS', - ); + 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 @@ -1115,43 +1121,4 @@ public static function proFeaturesLocked() { } return 'yes' === get_option( 'visualizer-new-user', 'yes' ) ? false : true; } - - /** - * Multilingual Support. - * - * @return bool Default false - */ - public static function addMultilingualSupport( $chart_id ) { - if ( function_exists( 'icl_get_languages' ) ) { - $language = icl_get_languages(); - $current_lang = icl_get_current_language(); - global $sitepress; - $trid = $sitepress->get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); - $translations = $sitepress->get_element_translations( $trid ); - ?> -
- get_element_trid( $chart_id, 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); $translations = $sitepress->get_element_translations( $trid ); @@ -538,11 +538,6 @@ public function renderChartPages() { ) ); - if ( ! empty( $_GET['lang'] ) && ! empty( $_GET['parent_chart_id'] ) ) { - $parent_chart_id = (int) $_GET['parent_chart_id']; - add_post_meta( $chart_id, 'chart_lang', $_GET['lang'] ); - $this->set_wpml_element_language_details( $parent_chart_id, $chart_id, $_GET['lang'] ); - } do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); } wp_redirect( esc_url_raw( add_query_arg( 'chart', (int) $chart_id ) ) ); @@ -1541,43 +1536,4 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename ); return $attach_id; } - - /** - * WPML set element language details. - * - * @param int $post_id Post ID. - * @param int $translated_post_id Translated post ID. - * @param string $language_code Selected language code. - */ - public function set_wpml_element_language_details( $post_id = 0, $translated_post_id = 0, $language_code = '' ) { - global $sitepress; - if ( $post_id && ! empty( $language_code ) ) { - $post_type = Visualizer_Plugin::CPT_VISUALIZER; - $wpml_element_type = apply_filters( 'wpml_element_type', $post_type ); - $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post_type ); - $recursive = false; - $original_post_id = $translated_post_id; - $original_lang_code = $language_code; - if ( empty( $trid ) ) { - $translated_post_id = $post_id; - $trid = $post_id; - $recursive = true; - $language_code = icl_get_default_language(); - } - - $language_args = array( - 'element_id' => $translated_post_id, - 'element_type' => $wpml_element_type, - 'trid' => $trid, - 'language_code' => $language_code, - 'source_language_code' => ! $recursive ? icl_get_default_language() : null, - ); - // Set language details. - do_action( 'wpml_set_element_language_details', $language_args ); - - if ( $recursive ) { - $this->set_wpml_element_language_details( $post_id, $original_post_id, $original_lang_code ); - } - } - } } diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 26b785922..12e701612 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -305,7 +305,7 @@ public function renderChart( $atts ) { $atts ); - if ( function_exists( 'icl_get_languages' ) ) { + if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) { global $sitepress; $locale = get_locale(); $locale = strtolower( str_replace( '_', '-', $locale ) ); From 63d2d6be8011d2a6b1e4a4472b3e33973e7529dc Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 4 Nov 2022 18:06:23 +0530 Subject: [PATCH 3/4] Fix auto render another language chart #731 --- classes/Visualizer/Module/Frontend.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 12e701612..a1908e668 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -307,7 +307,7 @@ public function renderChart( $atts ) { if ( Visualizer_Module::is_pro() && function_exists( 'icl_get_languages' ) ) { global $sitepress; - $locale = get_locale(); + $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 ); From bc68b1b087593ec6fce14328e4ecf065f8fdc968 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 4 Nov 2022 19:04:58 +0530 Subject: [PATCH 4/4] Skip new chart popup #731 --- classes/Visualizer/Module/Chart.php | 81 +++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 3c48b6865..1beee34a6 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -511,33 +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 ) ) );