Skip to content

Commit 46db6f8

Browse files
release
- [Feat] Show chart ID in the chart library - [Fix] Compatibility with WP 5.5 - [Fix] Google charts: Series number format not applying in the Gutenberg editor - [Fix] Google Table chart does not display chart if boolean values are specified - [Fix] Duplicated enque for jsapi loader
2 parents 9280c52 + 58cb6ee commit 46db6f8

26 files changed

+248
-107
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ public function get_visualizer_data( $post ) {
352352
// handle data filter hooks
353353
$data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( get_the_content( $post_id ) ) ), $post_id, $data['visualizer-chart-type'] );
354354

355+
// we are going to format only for tabular charts, because we are not sure of the effect on others.
356+
// this is to solve the case where boolean data shows up as all-ticks on gutenberg.
357+
if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) {
358+
$data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
359+
}
360+
355361
$data['visualizer-data-exploded'] = '';
356362
// handle annotations for google charts
357363
if ( 'GoogleCharts' === $library ) {
@@ -440,9 +446,16 @@ public function get_visualizer_data( $post ) {
440446
if ( Visualizer_Module::is_pro() ) {
441447
$permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true );
442448

443-
if ( ! empty( $permissions ) ) {
444-
$data['visualizer-permissions'] = $permissions;
449+
if ( empty( $permissions ) ) {
450+
$permissions = array( 'permissions' => array(
451+
'read' => 'all',
452+
'edit' => 'roles',
453+
'edit-specific' => array( 'administrator' ),
454+
),
455+
);
445456
}
457+
458+
$data['visualizer-permissions'] = $permissions;
446459
}
447460

448461
return $data;
@@ -646,6 +659,8 @@ public function update_chart_data( $data ) {
646659

647660
/**
648661
* Format chart data.
662+
*
663+
* Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
649664
*/
650665
public function format_chart_data( $data, $series ) {
651666
foreach ( $series as $i => $row ) {
@@ -658,36 +673,35 @@ public function format_chart_data( $data, $series ) {
658673
continue;
659674
}
660675

661-
if ( $row['type'] === 'number' ) {
662-
foreach ( $data as $o => $col ) {
663-
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
664-
}
665-
}
666-
667-
if ( $row['type'] === 'boolean' ) {
668-
foreach ( $data as $o => $col ) {
669-
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
670-
}
671-
}
672-
673-
if ( $row['type'] === 'timeofday' ) {
674-
foreach ( $data as $o => $col ) {
675-
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
676-
if ( $date ) {
677-
$data[ $o ][ $i ] = array(
678-
intval( $date->format( 'H' ) ),
679-
intval( $date->format( 'i' ) ),
680-
intval( $date->format( 's' ) ),
681-
0,
682-
);
676+
switch ( $row['type'] ) {
677+
case 'number':
678+
foreach ( $data as $o => $col ) {
679+
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
683680
}
684-
}
685-
}
686-
687-
if ( $row['type'] === 'string' ) {
688-
foreach ( $data as $o => $col ) {
689-
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
690-
}
681+
break;
682+
case 'boolean':
683+
foreach ( $data as $o => $col ) {
684+
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false;
685+
}
686+
break;
687+
case 'timeofday':
688+
foreach ( $data as $o => $col ) {
689+
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
690+
if ( $date ) {
691+
$data[ $o ][ $i ] = array(
692+
intval( $date->format( 'H' ) ),
693+
intval( $date->format( 'i' ) ),
694+
intval( $date->format( 's' ) ),
695+
0,
696+
);
697+
}
698+
}
699+
break;
700+
case 'string':
701+
foreach ( $data as $o => $col ) {
702+
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
703+
}
704+
break;
691705
}
692706
}
693707

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Visualizer/Gutenberg/src/Components/ChartPermissions.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ class ChartPermissions extends Component {
6464

6565
render() {
6666

67-
let permissions;
68-
69-
if ( 'business' === visualizerLocalize.isPro ) {
70-
permissions = this.props.chart['visualizer-permissions'];
71-
}
67+
let permissions = this.props.chart['visualizer-permissions'];
7268

7369
return (
7470
<Fragment>

classes/Visualizer/Gutenberg/src/Components/ChartRender.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import DataTable from './DataTable.js';
77

88
import merge from 'merge';
99

10-
import { compact, formatDate, isValidJSON } from '../utils.js';
10+
import { compact, formatDate, isValidJSON, formatData } from '../utils.js';
1111

1212
/**
1313
* WordPress dependencies
@@ -97,6 +97,7 @@ class ChartRender extends Component {
9797
compact( this.props.chart['visualizer-settings'])
9898
}
9999
height="500px"
100+
formatters={ formatData( data ) }
100101
/>
101102
) : (
102103
<Chart
@@ -109,6 +110,7 @@ class ChartRender extends Component {
109110
compact( this.props.chart['visualizer-settings'])
110111
}
111112
height="500px"
113+
formatters={ formatData( data ) }
112114
/>
113115
) ) }
114116

classes/Visualizer/Gutenberg/src/Components/ChartSelect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import PanelButton from './PanelButton.js';
2323

2424
import merge from 'merge';
2525

26-
import { compact, formatDate, isValidJSON } from '../utils.js';
26+
import { compact, formatDate, isValidJSON, formatData } from '../utils.js';
2727

2828
/**
2929
* WordPress dependencies
@@ -169,6 +169,7 @@ class ChartSelect extends Component {
169169
compact( this.props.chart['visualizer-settings'])
170170
}
171171
height="500px"
172+
formatters={ formatData( data ) }
172173
/>
173174
) : (
174175
<Chart
@@ -181,6 +182,7 @@ class ChartSelect extends Component {
181182
compact( this.props.chart['visualizer-settings'])
182183
}
183184
height="500px"
185+
formatters={ formatData( data ) }
184186
/>
185187
)
186188
) }

classes/Visualizer/Gutenberg/src/Components/Charts.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Chart } from 'react-google-charts';
55

66
import DataTable from './DataTable.js';
77

8-
import { formatDate, filterCharts } from '../utils.js';
8+
import { formatDate, filterCharts, formatData } from '../utils.js';
99

1010
/**
1111
* WordPress dependencies
@@ -152,13 +152,15 @@ class Charts extends Component {
152152
rows={ data['visualizer-data'] }
153153
columns={ data['visualizer-series'] }
154154
options={ filterCharts( data['visualizer-settings']) }
155+
formatters={ formatData( data ) }
155156
/>
156157
) : (
157158
<Chart
158159
chartType={ chart }
159160
rows={ data['visualizer-data'] }
160161
columns={ data['visualizer-series'] }
161162
options={ filterCharts( data['visualizer-settings']) }
163+
formatters={ formatData( data ) }
162164
/>
163165
) ) }
164166

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class GeneralSettings extends Component {
4646
positions.push({ label: __( 'Inside the chart' ), value: 'in' });
4747
}
4848

49+
let titleHelp = __( 'Text to display above the chart.' );
50+
if ( 0 <= [ 'tabular', 'dataTable', 'gauge', 'geo', 'timeline' ].indexOf( type ) ) {
51+
titleHelp = __( 'Text to display in the back-end admin area' );
52+
}
53+
4954
return (
5055
<PanelBody
5156
title={ __( 'General Settings' ) }
@@ -61,7 +66,7 @@ class GeneralSettings extends Component {
6166

6267
<TextControl
6368
label={ __( 'Chart Title' ) }
64-
help={ __( 'Text to display above the chart.' ) }
69+
help={ titleHelp }
6570
value={ settings.title }
6671
onChange={ e => {
6772
settings.title = e;

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ class SeriesSettings extends Component {
6060

6161
{ Object.keys( settings.series )
6262
.map( ( i, index ) => {
63+
let indexToFormat = parseInt( i );
64+
65+
if ( 'tabular' !== type ) {
66+
indexToFormat = index;
67+
}
68+
6369
i++;
70+
6471
return (
6572
<PanelBody
6673
title={ series[i].label }
@@ -121,9 +128,9 @@ class SeriesSettings extends Component {
121128
<TextControl
122129
label={ __( 'Format' ) }
123130
help={ __( 'Enter custom format pattern to apply to this series value.' ) }
124-
value={ settings.series[index].format }
131+
value={ settings.series[indexToFormat].format }
125132
onChange={ e => {
126-
settings.series[index].format = e;
133+
settings.series[indexToFormat].format = e;
127134
this.props.edit( settings );
128135
} }
129136
/>
@@ -148,9 +155,9 @@ class SeriesSettings extends Component {
148155
label={ __( 'Date Format' ) }
149156
help={ __( 'Enter custom format pattern to apply to this series value.' ) }
150157
placeholder="dd LLLL yyyy"
151-
value={ settings.series[index].format }
158+
value={ settings.series[indexToFormat].format }
152159
onChange={ e => {
153-
settings.series[index].format = e;
160+
settings.series[indexToFormat].format = e;
154161
this.props.edit( settings );
155162
} }
156163
/>

classes/Visualizer/Gutenberg/src/Editor.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,39 @@ class Editor extends Component {
339339
let map = series;
340340
let fieldName = 'series';
341341

342-
if ( 'pie' === type ) {
343-
map = chartData;
344-
fieldName = 'slices';
345-
346-
// pie charts are finicky about a number being a number
347-
// and editing a number makes it a string
348-
// so let's convert it back into a number.
349-
chartData.map( ( i, index ) => {
350-
switch ( series[1].type ) {
351-
case 'number':
352-
i[1] = parseFloat( i[1]);
353-
break;
354-
}
355-
});
342+
switch ( type ) {
343+
case 'pie':
344+
map = chartData;
345+
fieldName = 'slices';
346+
347+
// pie charts are finicky about a number being a number
348+
// and editing a number makes it a string
349+
// so let's convert it back into a number.
350+
chartData.map( ( i, index ) => {
351+
switch ( series[1].type ) {
352+
case 'number':
353+
i[1] = parseFloat( i[1]);
354+
break;
355+
}
356+
});
357+
break;
358+
case 'tabular':
359+
360+
// table charts are finicky about a boolean being a boolean
361+
// and editing a boolean makes it a string
362+
// so let's convert it back into a boolean.
363+
chartData.map( ( i, index ) => {
364+
series.map( ( seriesObject, seriesIndex ) => {
365+
switch ( seriesObject.type ) {
366+
case 'boolean':
367+
if ( 'string' === typeof i[seriesIndex]) {
368+
i[seriesIndex] = 'true' === i[seriesIndex];
369+
}
370+
break;
371+
}
372+
});
373+
});
374+
break;
356375
}
357376

358377
map.map( ( i, index ) => {
@@ -362,16 +381,18 @@ class Editor extends Component {
362381

363382
const seriesIndex = 'pie' !== type ? index - 1 : index;
364383

365-
if ( settings[fieldName][seriesIndex] === undefined ) {
384+
if ( Array.isArray( settings[fieldName]) && settings[fieldName][seriesIndex] === undefined ) {
366385
settings[fieldName][seriesIndex] = {};
367386
settings[fieldName][seriesIndex].temp = 1;
368387
}
369388
});
370389

371-
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
372-
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
373-
return index < length;
374-
});
390+
if ( Array.isArray( settings[fieldName]) ) {
391+
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
392+
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
393+
return index < length;
394+
});
395+
}
375396

376397
chart['visualizer-source'] = source;
377398
chart['visualizer-default-data'] = 0;

0 commit comments

Comments
 (0)