Skip to content

Commit 548dcc8

Browse files
Merge pull request #378 from contactashish13/issue-144
Import from WP: add ability to pick up live data
2 parents cb1c75e + 927b24f commit 548dcc8

File tree

7 files changed

+144
-56
lines changed

7 files changed

+144
-56
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 34 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
/**
@@ -592,7 +593,9 @@ public function uploadData() {
592593

593594
if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) {
594595
// delete the filters in case this chart is being uploaded from other data sources
595-
delete_post_meta( $chart_id, 'visualizer-filter-config' );
596+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG );
597+
delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG );
598+
delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY );
596599

597600
// delete "import from db" specific parameters.
598601
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
@@ -609,7 +612,7 @@ public function uploadData() {
609612
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
610613
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
611614
} elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
612-
$source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
615+
$source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST );
613616
} else {
614617
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
615618
}
@@ -846,4 +849,33 @@ public function saveQuery() {
846849
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
847850
}
848851
}
852+
853+
854+
/**
855+
* Saves the filter query and the schedule.
856+
*
857+
* @access public
858+
*/
859+
public function saveFilter() {
860+
check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' );
861+
862+
$chart_id = filter_input(
863+
INPUT_GET,
864+
'chart',
865+
FILTER_VALIDATE_INT,
866+
array(
867+
'options' => array(
868+
'min_range' => 1,
869+
),
870+
)
871+
);
872+
873+
$hours = $_POST['refresh'];
874+
875+
do_action( 'visualizer_save_filter', $chart_id, $hours );
876+
877+
if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
878+
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
879+
}
880+
}
849881
}

classes/Visualizer/Module/Setup.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ 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

5050
$this->_addAction( 'activated_plugin', 'onActivation' );
5151
$this->_addAction( 'init', 'setupCustomPostTypes' );
@@ -165,12 +165,11 @@ public function deactivate() {
165165
}
166166

167167
/**
168-
* Refresh the specific chart from the db. This is mostly for charts that have 0 refresh time i.e. live data.
168+
* Refresh the specific chart from the db.
169169
*
170170
* @param WP_Post $chart The chart object.
171171
* @param int $chart_id The chart id.
172-
* @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.
173-
* If false, data will be refreshed only if the chart requests live data.
172+
* @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.
174173
*
175174
* @access public
176175
*/
@@ -183,6 +182,16 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
183182
$chart = get_post( $chart_id );
184183
}
185184

185+
if ( ! $chart ) {
186+
return $chart;
187+
}
188+
189+
// check if the source is correct.
190+
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
191+
if ( $source !== 'Visualizer_Source_Query' ) {
192+
return $chart;
193+
}
194+
186195
// check if its a live-data chart or a cached-data chart.
187196
if ( ! $force ) {
188197
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
@@ -192,15 +201,10 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
192201
}
193202
}
194203

195-
// check if the source is correct.
196-
$source = get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true );
197-
if ( $source !== 'Visualizer_Source_Query_Params' ) {
198-
return $chart;
199-
}
200-
201204
$params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true );
202205
$source = new Visualizer_Source_Query( $params );
203206
$source->fetch( false );
207+
204208
$error = $source->get_error();
205209
if ( empty( $error ) ) {
206210
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
@@ -242,7 +246,8 @@ public function refreshDbChart() {
242246
continue;
243247
}
244248

245-
$this->refresh_db_for_chart( null, $chart_id, false );
249+
// if the time is nigh, we force an update.
250+
$this->refresh_db_for_chart( null, $chart_id, true );
246251
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
247252
$new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
248253
}

classes/Visualizer/Plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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';

classes/Visualizer/Render/Page/Data.php

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ protected function _renderSidebarContent() {
7676

7777
// this will allow us to open the correct source tab by default.
7878
$source_of_chart = strtolower( get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_SOURCE, true ) );
79+
// both import from wp and import from db have the same source so we need to differentiate.
80+
$filter_config = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_FILTER_CONFIG, true );
81+
// if filter config is present, then its import from wp.
82+
if ( ! empty( $filter_config ) ) {
83+
$source_of_chart .= '_wp';
84+
}
7985
$type = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
8086
?>
8187
<span id="visualizer-chart-id" data-id="<?php echo $this->chart->ID; ?>" data-chart-source="<?php echo $source_of_chart; ?>" data-chart-type="<?php echo $type; ?>"></span>
@@ -215,16 +221,49 @@ class="dashicons dashicons-lock"></span></h2>
215221
</div>
216222
</li>
217223

218-
<li class="viz-group <?php echo apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature', 'schedule-chart' ); ?> ">
224+
<?php
225+
$save_filter = add_query_arg(
226+
array(
227+
'action' => Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY,
228+
'security' => wp_create_nonce( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION ),
229+
'chart' => $this->chart->ID,
230+
), admin_url( 'admin-ajax.php' )
231+
);
232+
?>
233+
<li class="viz-group visualizer_source_query_wp <?php echo apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature', 'schedule-chart' ); ?> ">
219234
<h2 class="viz-group-title viz-sub-group"><?php _e( 'Import from WordPress', 'visualizer' ); ?><span
220235
class="dashicons dashicons-lock"></span></h2>
221236
<div class="viz-group-content edit-data-content">
222237
<div>
223238
<p class="viz-group-description"><?php _e( 'You can import data from WordPress here.', 'visualizer' ); ?></p>
224-
<input type="button" id="filter-chart-button" class="button button-primary "
225-
value="<?php _e( 'Create Filters', 'visualizer' ); ?>" data-current="chart"
226-
data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>"
227-
data-t-chart="<?php _e( 'Create Filters', 'visualizer' ); ?>">
239+
<form id="vz-filter-wizard" action="<?php echo $save_filter; ?>" method="post" target="thehole">
240+
<p class="viz-group-description"><?php _e( 'How often do you want to refresh the data from WordPress.', 'visualizer' ); ?></p>
241+
<select name="refresh" id="vz-filter-import-time" class="visualizer-select">
242+
<?php
243+
$bttn_label = 'visualizer_source_query_wp' === $source_of_chart ? __( 'Modify Filter', 'visualizer' ) : __( 'Create Filter', 'visualizer' );
244+
$hours = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_DB_SCHEDULE, true );
245+
$schedules = apply_filters(
246+
'visualizer_schedules', array(
247+
'0' => __( 'Live', 'visualizer' ),
248+
'1' => __( 'Each hour', 'visualizer' ),
249+
'12' => __( 'Each 12 hours', 'visualizer' ),
250+
'24' => __( 'Each day', 'visualizer' ),
251+
'72' => __( 'Each 3 days', 'visualizer' ),
252+
)
253+
);
254+
foreach ( $schedules as $num => $name ) {
255+
$extra = $num == $hours ? 'selected' : '';
256+
?>
257+
<option value="<?php echo $num; ?>" <?php echo $extra; ?>><?php echo $name; ?></option>
258+
<?php
259+
}
260+
?>
261+
</select>
262+
263+
<input type="button" id="filter-chart-button" class="button button-secondary" value="<?php echo $bttn_label; ?>" data-current="chart" data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>" data-t-chart="<?php echo $bttn_label; ?>">
264+
<input type="button" id="db-filter-save-button" class="button button-primary" value="<?php _e( 'Save Schedule', 'visualizer' ); ?>">
265+
<?php echo apply_filters( 'visualizer_pro_upsell', '', 'db-query' ); ?>
266+
</form>
228267
<?php echo apply_filters( 'visualizer_pro_upsell', '', 'schedule-chart' ); ?>
229268
</div>
230269
</div>
@@ -244,42 +283,38 @@ class="dashicons dashicons-lock"></span></h2>
244283
class="dashicons dashicons-lock"></span></h2>
245284
<div class="viz-group-content edit-data-content">
246285
<div>
247-
<p class="viz-group-description"><?php _e( 'You can import data from the database here.', 'visualizer' ); ?></p>
248-
<form id="vz-db-wizard" action="<?php echo $save_query; ?>" method="post" target="thehole">
249-
<p class="viz-group-description"><?php _e( 'How often do you want to refresh the data from the database.', 'visualizer' ); ?></p>
250-
<select name="refresh" id="vz-db-import-time" class="visualizer-select">
251-
<?php
252-
$bttn_label = 'visualizer_source_query' === $source_of_chart ? __( 'Modify Query', 'visualizer' ) : __( 'Create Query', 'visualizer' );
253-
$hours = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_DB_SCHEDULE, true );
254-
$schedules = apply_filters(
255-
'visualizer_schedules', array(
256-
'0' => __( 'Live', 'visualizer' ),
257-
'1' => __( 'Each hour', 'visualizer' ),
258-
'12' => __( 'Each 12 hours', 'visualizer' ),
259-
'24' => __( 'Each day', 'visualizer' ),
260-
'72' => __( 'Each 3 days', 'visualizer' ),
261-
)
262-
);
263-
foreach ( $schedules as $num => $name ) {
264-
$extra = $num == $hours ? 'selected' : '';
286+
<p class="viz-group-description"><?php _e( 'You can import data from the database here.', 'visualizer' ); ?></p>
287+
<form id="vz-db-wizard" action="<?php echo $save_query; ?>" method="post" target="thehole">
288+
<p class="viz-group-description"><?php _e( 'How often do you want to refresh the data from the database.', 'visualizer' ); ?></p>
289+
<select name="refresh" id="vz-db-import-time" class="visualizer-select">
290+
<?php
291+
$bttn_label = 'visualizer_source_query' === $source_of_chart ? __( 'Modify Query', 'visualizer' ) : __( 'Create Query', 'visualizer' );
292+
$hours = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_DB_SCHEDULE, true );
293+
$schedules = apply_filters(
294+
'visualizer_schedules', array(
295+
'0' => __( 'Live', 'visualizer' ),
296+
'1' => __( 'Each hour', 'visualizer' ),
297+
'12' => __( 'Each 12 hours', 'visualizer' ),
298+
'24' => __( 'Each day', 'visualizer' ),
299+
'72' => __( 'Each 3 days', 'visualizer' ),
300+
)
301+
);
302+
foreach ( $schedules as $num => $name ) {
303+
$extra = $num == $hours ? 'selected' : '';
304+
?>
305+
<option value="<?php echo $num; ?>" <?php echo $extra; ?>><?php echo $name; ?></option>
306+
<?php
307+
}
265308
?>
266-
<option value="<?php echo $num; ?>" <?php echo $extra; ?>><?php echo $name; ?></option>
267-
<?php
268-
}
269-
?>
270-
</select>
271-
<input type="hidden" name="params" id="viz-db-wizard-params">
272-
273-
<input type="button" id="db-chart-button" class="button button-secondary "
274-
value="<?php echo $bttn_label; ?>" data-current="chart"
275-
data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>"
276-
data-t-chart="<?php echo $bttn_label; ?>">
277-
<input type="button" id="db-chart-save-button" class="button button-primary "
278-
value="<?php _e( 'Save Schedule', 'visualizer' ); ?>">
309+
</select>
310+
<input type="hidden" name="params" id="viz-db-wizard-params">
311+
312+
<input type="button" id="db-chart-button" class="button button-secondary" value="<?php echo $bttn_label; ?>" data-current="chart" data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>" data-t-chart="<?php echo $bttn_label; ?>">
313+
<input type="button" id="db-chart-save-button" class="button button-primary" value="<?php _e( 'Save Schedule', 'visualizer' ); ?>">
279314
<?php echo apply_filters( 'visualizer_pro_upsell', '', 'db-query' ); ?>
280-
</form>
281-
</div>
282-
</div>
315+
</form>
316+
</div>
317+
</div>
283318
</li>
284319

285320
<?php

classes/Visualizer/Render/Sidebar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ protected function _renderAnimationSettings() {
363363
self::_renderCheckboxItem(
364364
esc_html__( 'Animate on startup', 'visualizer' ),
365365
'animation[startup]',
366-
$this->animation['startup'],
366+
isset( $this->animation['startup'] ) ? $this->animation['startup'] : 0,
367367
true,
368368
esc_html__( 'Determines if the chart will animate on the initial draw.', 'visualizer' )
369369
);

classes/Visualizer/Source/Query.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,33 @@ public function __construct( $query = null ) {
5757
* Fetches information from source, parses it and builds series and data arrays.
5858
*
5959
* @param bool $as_html Should the result be fetched as an HTML table or as an object.
60+
* @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A.
61+
* @param bool $raw_results Should the result be returned without processing.
6062
* @access public
6163
* @return boolean TRUE on success, otherwise FALSE.
6264
*/
63-
public function fetch( $as_html = false ) {
65+
public function fetch( $as_html = false, $results_as_numeric_array = false, $raw_results = false ) {
6466
if ( empty( $this->_query ) ) {
6567
return false;
6668
}
6769

6870
// impose a limit if no limit clause is provided.
6971
if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) {
70-
$this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 300 );
72+
$this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 );
7173
}
7274

7375
global $wpdb;
7476
$wpdb->hide_errors();
7577
// @codingStandardsIgnoreStart
76-
$rows = $wpdb->get_results( $this->_query, ARRAY_A );
78+
$rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
79+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ );
7780
// @codingStandardsIgnoreEnd
7881
$wpdb->show_errors();
7982

83+
if ( $raw_results ) {
84+
return $rows;
85+
}
86+
8087
if ( $rows ) {
8188
$results = array();
8289
$headers = array();

js/frame.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
if(typeof visualizer !== 'undefined' && visualizer.is_pro) {
2323
init_db_import();
24+
init_filter_import();
2425
}
2526

2627
// update the manual configuation link to point to the correct chart type.
@@ -221,6 +222,12 @@
221222
});
222223
}
223224

225+
function init_filter_import() {
226+
$( '#db-filter-save-button' ).on( 'click', function(){
227+
$('#vz-filter-wizard').submit();
228+
});
229+
}
230+
224231
function init_db_import(){
225232
$( '#visualizer-db-query' ).css("z-index", "-1").hide();
226233

0 commit comments

Comments
 (0)