Skip to content

Commit de76457

Browse files
Merge pull request #382 from Codeinwp/development
Fix issue with changing column settings of the last column in table chart Add support for query language to get subset of data from Google Spreadsheet Fix conflict with jquery 3.3.x Migrated PHPExcel to PhpSpreadsheet Front end action 'print' should print the chart and fall back to printing the data Fix issue with table chart not showing in IE Fix issue with multiple instances of same chart not showing Fix issue with date type column does not work with Combo charts Tested with WP 5.1
2 parents fbdc746 + 3df57c4 commit de76457

32 files changed

+553
-134
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function enqueue_gutenberg_scripts() {
8686
if ( VISUALIZER_PRO ) {
8787
$type = 'pro';
8888
if ( apply_filters( 'visualizer_is_business', false ) ) {
89-
$type = 'business';
89+
$type = 'developer';
9090
}
9191
}
9292

@@ -121,11 +121,13 @@ public function register_block_type() {
121121
* Gutenberg Block Callback Function
122122
*/
123123
public function gutenberg_block_callback( $attr ) {
124-
$id = $attr['id'];
125-
if ( empty( $id ) || $id === 'none' ) {
126-
return ''; // no id = no fun
124+
if ( isset( $attr['id'] ) ) {
125+
$id = $attr['id'];
126+
if ( empty( $id ) || $id === 'none' ) {
127+
return ''; // no id = no fun
128+
}
129+
return '[visualizer id="' . $id . '"]';
127130
}
128-
return '[visualizer id="' . $id . '"]';
129131
}
130132

131133
/**

classes/Visualizer/Gutenberg/build/block.js

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

classes/Visualizer/Gutenberg/build/handsontable.js

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

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ class Charts extends Component {
9191
title = `#${charts[i].id}`;
9292
}
9393

94-
// Format chart type for Gauge chart.
95-
if ( 0 <= [ 'gauge', 'table', 'timeline' ].indexOf( data['visualizer-chart-type']) ) {
96-
chart = startCase( data['visualizer-chart-type']);
94+
if ( 0 <= [ 'gauge', 'table', 'timeline', 'dataTable' ].indexOf( data['visualizer-chart-type']) ) {
95+
if ( 'dataTable' === data['visualizer-chart-type']) {
96+
chart = data['visualizer-chart-type'];
97+
} else {
98+
chart = startCase( data['visualizer-chart-type']);
99+
}
97100
} else {
98101
chart = `${ startCase( data['visualizer-chart-type']) }Chart`;
99102
}
100103

104+
if ( 'dataTable' === chart ) {
105+
return;
106+
}
107+
101108
return (
102109
<div className="visualizer-settings__charts-single">
103110

classes/Visualizer/Module.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,29 @@ private function _getCSV( $rows, $filename ) {
253253
* @param string $filename The name of the file to use.
254254
*/
255255
private function _getExcel( $rows, $filename ) {
256-
// PHPExcel does not like sheet names longer than 31 characters.
256+
// PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet
257257
$chart = substr( $filename, 0, 30 );
258258
$filename .= '.xlsx';
259259

260+
$vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php';
261+
if ( is_readable( $vendor_file ) ) {
262+
include_once( $vendor_file );
263+
}
264+
260265
$xlsData = '';
261-
if ( class_exists( 'PHPExcel' ) ) {
262-
$doc = new PHPExcel();
266+
if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) {
267+
$doc = new PhpOffice\PhpSpreadsheet\Spreadsheet();
263268
$doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
264269
$doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
265270
$doc = apply_filters( 'visualizer_excel_doc', $doc );
266-
$writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
271+
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' );
267272
ob_start();
268273
$writer->save( 'php://output' );
269274
$xlsData = ob_get_contents();
270275
ob_end_clean();
271276
} else {
272-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ );
273-
error_log( 'Class PHPExcel does not exist!' );
277+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!', 'error', __FILE__, __LINE__ );
278+
error_log( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!' );
274279
}
275280
return array(
276281
'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),

classes/Visualizer/Module/Chart.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
6161

6262
$this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' );
6363
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' );
64+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
6465
}
6566

6667
/**
@@ -163,13 +164,16 @@ private function _getChartArray( WP_Post $chart = null ) {
163164
$settings = $arguments[1];
164165
}
165166

167+
$date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data );
168+
166169
return array(
167170
'type' => $type,
168171
'series' => $series,
169172
'settings' => $settings,
170173
'data' => $data,
171174
'library' => $library,
172175
'css' => $css,
176+
'date_formats' => $date_formats,
173177
);
174178
}
175179

@@ -233,6 +237,34 @@ public function deleteChart() {
233237
exit;
234238
}
235239

240+
/**
241+
* Delete charts that are still in auto-draft mode.
242+
*/
243+
private function deleteOldCharts() {
244+
$query = new WP_Query(
245+
array(
246+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
247+
'post_status' => 'auto-draft',
248+
'fields' => 'ids',
249+
'update_post_meta_cache' => false,
250+
'update_post_term_cache' => false,
251+
'posts_per_page' => 50,
252+
'date_query' => array(
253+
array(
254+
'before' => 'today',
255+
),
256+
),
257+
)
258+
);
259+
260+
if ( $query->have_posts() ) {
261+
$ids = array();
262+
while ( $query->have_posts() ) {
263+
wp_delete_post( $query->next_post(), true );
264+
}
265+
}
266+
}
267+
236268
/**
237269
* Renders appropriate page for chart builder. Creates new auto draft chart
238270
* if no chart has been specified.
@@ -246,6 +278,7 @@ public function renderChartPages() {
246278
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
247279
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
248280
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
281+
$this->deleteOldCharts();
249282
$default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line';
250283
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
251284
$source->fetch();
@@ -589,7 +622,9 @@ public function uploadData() {
589622

590623
if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) {
591624
// delete the filters in case this chart is being uploaded from other data sources
592-
delete_post_meta( $chart_id, 'visualizer-filter-config' );
625+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG );
626+
delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG );
627+
delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY );
593628

594629
// delete "import from db" specific parameters.
595630
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
@@ -606,7 +641,7 @@ public function uploadData() {
606641
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
607642
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
608643
} elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
609-
$source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
644+
$source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST );
610645
} else {
611646
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
612647
}
@@ -843,4 +878,33 @@ public function saveQuery() {
843878
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
844879
}
845880
}
881+
882+
883+
/**
884+
* Saves the filter query and the schedule.
885+
*
886+
* @access public
887+
*/
888+
public function saveFilter() {
889+
check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' );
890+
891+
$chart_id = filter_input(
892+
INPUT_GET,
893+
'chart',
894+
FILTER_VALIDATE_INT,
895+
array(
896+
'options' => array(
897+
'min_range' => 1,
898+
),
899+
)
900+
);
901+
902+
$hours = $_POST['refresh'];
903+
904+
do_action( 'visualizer_save_filter', $chart_id, $hours );
905+
906+
if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
907+
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
908+
}
909+
}
846910
}

classes/Visualizer/Module/Frontend.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ public function renderChart( $atts ) {
245245

246246
$library = $this->load_chart_type( $chart->ID );
247247

248+
$id = $id . '-' . rand();
249+
248250
// add chart to the array
249251
$this->_charts[ $id ] = array(
250252
'type' => $type,
@@ -293,7 +295,7 @@ public function renderChart( $atts ) {
293295
$mime = end( $array );
294296
}
295297
$label = $actions[ $key ];
296-
$actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" ';
298+
$actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-container-id="' . $id . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" ';
297299

298300
if ( 'copy' === $key ) {
299301
$copy = $this->_getDataAs( $atts['id'], 'csv' );

classes/Visualizer/Module/Setup.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function __construct( Visualizer_Plugin $plugin ) {
4545
register_activation_hook( VISUALIZER_BASEFILE, array( $this, 'activate' ) );
4646
register_deactivation_hook( VISUALIZER_BASEFILE, array( $this, 'deactivate' ) );
4747
$this->_addAction( 'visualizer_schedule_refresh_db', 'refreshDbChart' );
48-
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', null, 10, 3 );
48+
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );
4949

50+
$this->_addAction( 'activated_plugin', 'onActivation' );
5051
$this->_addAction( 'init', 'setupCustomPostTypes' );
5152
$this->_addAction( 'plugins_loaded', 'loadTextDomain' );
5253
$this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
@@ -145,6 +146,21 @@ public function activate() {
145146
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 'hourly', 'visualizer_schedule_refresh_db' );
146147
}
147148

149+
/**
150+
* On activation of the plugin
151+
*/
152+
public function onActivation( $plugin ) {
153+
if ( defined( 'TI_UNIT_TESTING' ) ) {
154+
return;
155+
}
156+
157+
if ( $plugin == VISUALIZER_BASENAME ) {
158+
wp_redirect( admin_url( 'upload.php?page=' . Visualizer_Plugin::NAME ) );
159+
exit();
160+
}
161+
}
162+
163+
148164
/**
149165
* Deactivate the plugin
150166
*/
@@ -153,12 +169,11 @@ public function deactivate() {
153169
}
154170

155171
/**
156-
* Refresh the specific chart from the db. This is mostly for charts that have 0 refresh time i.e. live data.
172+
* Refresh the specific chart from the db.
157173
*
158174
* @param WP_Post $chart The chart object.
159175
* @param int $chart_id The chart id.
160-
* @param bool $force If this is true, then the chart data will be refreshed no matter if the chart requests live data or cached data.
161-
* If false, data will be refreshed only if the chart requests live data.
176+
* @param bool $force If this is true, then the chart data will be force refreshed. If false, data will be refreshed only if the chart requests live data.
162177
*
163178
* @access public
164179
*/
@@ -171,6 +186,16 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
171186
$chart = get_post( $chart_id );
172187
}
173188

189+
if ( ! $chart ) {
190+
return $chart;
191+
}
192+
193+
// check if the source is correct.
194+
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
195+
if ( $source !== 'Visualizer_Source_Query' ) {
196+
return $chart;
197+
}
198+
174199
// check if its a live-data chart or a cached-data chart.
175200
if ( ! $force ) {
176201
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
@@ -180,15 +205,10 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
180205
}
181206
}
182207

183-
// check if the source is correct.
184-
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
185-
if ( $source !== 'Visualizer_Source_Query_Params' ) {
186-
return $chart;
187-
}
188-
189208
$params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true );
190209
$source = new Visualizer_Source_Query( $params );
191210
$source->fetch( false );
211+
192212
$error = $source->get_error();
193213
if ( empty( $error ) ) {
194214
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
@@ -230,7 +250,8 @@ public function refreshDbChart() {
230250
continue;
231251
}
232252

233-
$this->refresh_db_for_chart( null, $chart_id, false );
253+
// if the time is nigh, we force an update.
254+
$this->refresh_db_for_chart( null, $chart_id, true );
234255
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
235256
$new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
236257
}

classes/Visualizer/Module/Sources.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function addProUpsell( $old, $feature = null ) {
136136
if ( empty( $feature ) || ( in_array( $feature, $biz_features ) && ! apply_filters( 'visualizer_is_business', false ) ) ) {
137137
$plan = 'PRO';
138138
if ( in_array( $feature, $biz_features ) ) {
139-
$plan = 'BUSINESS';
139+
$plan = 'DEVELOPER';
140140
}
141141
$return = '<div class="only-pro-content">';
142142
$return .= ' <div class="only-pro-container">';

classes/Visualizer/Plugin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class Visualizer_Plugin {
2929

3030
const NAME = 'visualizer';
31-
const VERSION = '3.1.2';
31+
const VERSION = '3.1.3';
3232

3333
// custom post types
3434
const CPT_VISUALIZER = 'visualizer';
@@ -41,6 +41,7 @@ class Visualizer_Plugin {
4141
const CF_SETTINGS = 'visualizer-settings';
4242

4343
const CF_SOURCE_FILTER = 'visualizer-source-filter';
44+
const CF_FILTER_CONFIG = 'visualizer-filter-config';
4445

4546
// custom actions
4647
const ACTION_GET_CHARTS = 'visualizer-get-charts';
@@ -61,6 +62,7 @@ class Visualizer_Plugin {
6162
*/
6263
const ACTION_FETCH_DB_DATA = 'visualizer-fetch-db-data';
6364
const ACTION_SAVE_DB_QUERY = 'visualizer-save-db-query';
65+
const ACTION_SAVE_FILTER_QUERY = 'visualizer-save-filter-query';
6466

6567
// custom filters
6668
const FILTER_CHART_WRAPPER_CLASS = 'visualizer-chart-wrapper-class';

0 commit comments

Comments
 (0)