Skip to content

Commit bb2e689

Browse files
authored
release(minor): improvements
#### Features - Add option to enable/disable save the chart image - Enhanced caching #### Fixes - Fix undefined variable errors when using Gutenberg blocks - Fix broken chart in iOS and Safari - Fix custom refresh time support for DB chart - Fix table placed inside accordion cannot be scrolled down (refreshes all the time) - Fix pagination doesn't work in the library - Fix HTML entities in JSON break Visualizer Table Chart - Fix string type columns affect the target of the formatting applied to a number column
2 parents de5b582 + eaf3aa4 commit bb2e689

File tree

19 files changed

+168
-36
lines changed

19 files changed

+168
-36
lines changed

.github/workflows/test-e2e.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
fail-fast: false
1313
matrix:
1414
env: ["default","gutenberg", "gutenberg-datatable"]
15-
runs-on: ubuntu-16.04
15+
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v2
1818
- uses: actions/setup-node@v2

classes/Visualizer/Gutenberg/Block.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,12 @@ public function update_chart_data( $data ) {
659659

660660
wp_update_post( $chart );
661661

662+
// Clear existing chart cache.
663+
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id'];
664+
if ( get_transient( $cache_key ) ) {
665+
delete_transient( $cache_key );
666+
}
667+
662668
$revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
663669

664670
if ( count( $revisions ) > 1 ) {

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Visualizer/Gutenberg/src/Components/Sidebar/SeriesSettings.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class SeriesSettings extends Component {
6161
{ Object.keys( settings.series )
6262
.map( ( i, index ) => {
6363
let indexToFormat = parseInt( i );
64+
let label = series[i].label || '';
65+
let type = series[i].type || '';
6466

6567
if ( 'tabular' !== type ) {
6668
indexToFormat = index;
@@ -70,7 +72,7 @@ class SeriesSettings extends Component {
7072

7173
return (
7274
<PanelBody
73-
title={ series[i].label }
75+
title={ label }
7476
className="visualizer-inner-sections"
7577
initialOpen={ false }
7678
>
@@ -121,7 +123,7 @@ class SeriesSettings extends Component {
121123

122124
{ ( -1 >= [ 'candlestick' ].indexOf( type ) ) &&
123125

124-
( 'number' === series[i].type ) ? (
126+
( type && 'number' === type ) ? (
125127

126128
<Fragment>
127129

@@ -147,7 +149,7 @@ class SeriesSettings extends Component {
147149

148150
) :
149151

150-
( 0 <= [ 'date', 'datetime', 'timeofday' ].indexOf( series[i].type ) ) && (
152+
( 0 <= [ 'date', 'datetime', 'timeofday' ].indexOf( type ) ) && (
151153

152154
<Fragment>
153155

classes/Visualizer/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ public static final function get_features_for_license( $plan ) {
740740
*/
741741
public static function get_chart_data( $chart, $type, $run_filter = true ) {
742742
// change HTML entities
743-
$data = unserialize( html_entity_decode( $chart->post_content ) );
743+
$data = unserialize( html_entity_decode( htmlentities( $chart->post_content ) ) );
744744
$altered = array();
745745
foreach ( $data as $index => $array ) {
746746
if ( ! is_array( $index ) && is_array( $array ) ) {

classes/Visualizer/Module/Admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ function addScreenOptions() {
703703
$args = array(
704704
'label' => __( 'Number of charts per page:', 'visualizer' ),
705705
'default' => 6,
706-
'option' => 'visualizer_per_page_library',
706+
'option' => 'visualizer_library_per_page',
707707
);
708708
add_screen_option( 'per_page', $args );
709709
}
@@ -712,7 +712,7 @@ function addScreenOptions() {
712712
* Returns the screen option for pagination.
713713
*/
714714
function setScreenOptions( $status, $option, $value ) {
715-
if ( 'visualizer_per_page_library' === $option ) {
715+
if ( 'visualizer_library_per_page' === $option ) {
716716
return $value;
717717
}
718718
}

classes/Visualizer/Module/Chart.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,10 @@ public function renderChartPages() {
527527
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
528528
}
529529

530+
$_POST['save_chart_image'] = isset( $_POST['save_chart_image'] ) && 'yes' === $_POST['save_chart_image'] ? true : false;
531+
530532
if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) {
531-
$attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id );
533+
$attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id, $_POST['save_chart_image'] );
532534
if ( $attachment_id ) {
533535
update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id );
534536
}
@@ -596,6 +598,14 @@ public function renderChartPages() {
596598
$this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart );
597599
}
598600

601+
// Clear existing chart cache.
602+
if ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) {
603+
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
604+
if ( get_transient( $cache_key ) ) {
605+
delete_transient( $cache_key );
606+
}
607+
}
608+
599609
switch ( $tab ) {
600610
case 'settings':
601611
$this->_handleDataAndSettingsPage();
@@ -1334,7 +1344,7 @@ public function saveQuery() {
13341344
$hours = filter_input(
13351345
INPUT_POST,
13361346
'refresh',
1337-
FILTER_VALIDATE_INT,
1347+
FILTER_VALIDATE_FLOAT,
13381348
array(
13391349
'options' => array(
13401350
'min_range' => -1,
@@ -1343,7 +1353,7 @@ public function saveQuery() {
13431353
)
13441354
);
13451355

1346-
if ( ! is_int( $hours ) ) {
1356+
if ( ! is_numeric( $hours ) ) {
13471357
$hours = -1;
13481358
}
13491359

@@ -1437,9 +1447,20 @@ public function saveFilter() {
14371447
*
14381448
* @param string $base64_img Chart image.
14391449
* @param int $chart_id Chart ID.
1450+
* @param bool $save_attachment Save attachment.
14401451
* @return attachment ID
14411452
*/
1442-
public function save_chart_image( $base64_img, $chart_id ) {
1453+
public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) {
1454+
// Delete old chart image.
1455+
$old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
1456+
if ( $old_attachment_id ) {
1457+
wp_delete_attachment( $old_attachment_id, true );
1458+
}
1459+
1460+
if ( ! $save_attachment ) {
1461+
return 0;
1462+
}
1463+
14431464
// Upload dir.
14441465
$upload_dir = wp_upload_dir();
14451466
$upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
@@ -1451,12 +1472,6 @@ public function save_chart_image( $base64_img, $chart_id ) {
14511472
$file_type = 'image/png';
14521473
$hashed_filename = $filename;
14531474

1454-
// Delete old chart image.
1455-
$old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
1456-
if ( $old_attachment_id ) {
1457-
wp_delete_attachment( $old_attachment_id, true );
1458-
}
1459-
14601475
// Save the image in the uploads directory.
14611476
require_once ABSPATH . '/wp-admin/includes/file.php';
14621477
\WP_Filesystem();

classes/Visualizer/Module/Frontend.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,16 @@ public function renderChart( $atts ) {
277277
),
278278
$atts
279279
);
280-
// if empty id or chart does not exists, then return empty string
281-
if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) {
280+
281+
$chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] );
282+
// if empty chart does not exists, then return empty string.
283+
if ( ! $chart_data ) {
282284
return '';
283285
}
284286

287+
$chart = $chart_data['chart'];
288+
$type = $chart_data['type'];
289+
$series = $chart_data['series'];
285290
// do not show the chart?
286291
if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
287292
return '';
@@ -309,18 +314,16 @@ public function renderChart( $atts ) {
309314
$attributes['data-lazy-limit'] = $atts['lazy'];
310315
}
311316

312-
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
313-
314317
$chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false );
315318

316-
// fetch and update settings
317-
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
319+
// Get and update settings.
320+
$settings = $chart_data['settings'];
318321
if ( empty( $settings['height'] ) ) {
319322
$settings['height'] = '400';
320323
}
321324

322325
// handle series filter hooks
323-
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
326+
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type );
324327

325328
// handle settings filter hooks
326329
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
@@ -345,7 +348,7 @@ public function renderChart( $atts ) {
345348
}
346349

347350
if ( 'yes' === $atts['use_image'] ) {
348-
$chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true );
351+
$chart_image = $chart_data['chart_image'];
349352
if ( $chart_image ) {
350353
return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
351354
}
@@ -544,4 +547,41 @@ private function addSchema( $id ) {
544547

545548
return '<script type="application/ld+json">' . $schema . '</script>';
546549
}
550+
551+
/**
552+
* Get chart by ID.
553+
*
554+
* @param string $cache_key Cache key.
555+
* @param int $chart_id Chart ID.
556+
* @return mixed
557+
*/
558+
private function getChartData( $cache_key = '', $chart_id = 0 ) {
559+
if ( ! $chart_id ) {
560+
return false;
561+
}
562+
// Create unique cache key of each chart.
563+
$cache_key .= '_' . $chart_id;
564+
// Get chart from cache.
565+
$chart = get_transient( $cache_key );
566+
if ( $chart ) {
567+
return $chart;
568+
}
569+
570+
// Get chart by ID.
571+
$chart = get_post( $chart_id );
572+
if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
573+
$chart_data = array(
574+
'chart' => $chart,
575+
'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
576+
'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ),
577+
'series' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ),
578+
'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
579+
);
580+
// Put the results in a transient. Expire after 12 hours.
581+
set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) );
582+
return $chart_data;
583+
}
584+
585+
return false;
586+
}
547587
}

classes/Visualizer/Module/Setup.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function activate( $network_wide ) {
207207
*/
208208
private function activate_on_site() {
209209
wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
210-
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 'hourly', 'visualizer_schedule_refresh_db' );
210+
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'hourly' ), 'visualizer_schedule_refresh_db' );
211211
add_option( 'visualizer-activated', true );
212212
}
213213

@@ -394,6 +394,11 @@ public function refreshDbChart() {
394394

395395
// if the time is nigh, we force an update.
396396
$this->refresh_db_for_chart( null, $chart_id, true );
397+
// Clear existing chart cache.
398+
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
399+
if ( get_transient( $cache_key ) ) {
400+
delete_transient( $cache_key );
401+
}
397402
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
398403
$new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
399404
}

classes/Visualizer/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Visualizer_Plugin {
8989
const FILTER_UNDO_REVISIONS = 'visualizer-undo-revisions';
9090
const FILTER_HANDLE_REVISIONS = 'visualizer-handle-revisions';
9191
const FILTER_GET_CHART_DATA_AS = 'visualizer-get-chart-data-as';
92+
const FILTER_HANDLE_CACHE_EXPIRATION_TIME = 'visualizer-handle-expiration-time';
9293

9394
const CF_DB_SCHEDULE = 'visualizer-db-schedule';
9495
const CF_DB_QUERY = 'visualizer-db-query';
@@ -99,6 +100,8 @@ class Visualizer_Plugin {
99100
const PRO_TEASER_URL = 'https://themeisle.com/plugins/visualizer-charts-and-graphs/upgrade/#pricing';
100101
const PRO_TEASER_TITLE = 'Check PRO version ';
101102

103+
const CF_CHART_CACHE = 'visualizer-chart-cache';
104+
102105
/**
103106
* Name of the option for WordPress DB.
104107
*/

0 commit comments

Comments
 (0)