Skip to content

Compatible with WPML plugin #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions classes/Visualizer/Module/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
97 changes: 73 additions & 24 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 ) ) );
Expand Down
11 changes: 11 additions & 0 deletions classes/Visualizer/Module/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
1 change: 1 addition & 0 deletions classes/Visualizer/Render/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ private function _renderChartBox( $placeholder_id, $chart_id, $with_filter = fal
}
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>';
echo '<span>&nbsp;</span>';
do_action( 'visualizer_chart_languages', $chart_id );
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>';
echo '</div>';
echo '</div></div>';
Expand Down
26 changes: 26 additions & 0 deletions css/library.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 16 additions & 2 deletions js/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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/, '');
Expand Down