Skip to content

Commit 588bf4a

Browse files
fix: switch to 10 minutes cron (#1161)
1 parent 06b5bfd commit 588bf4a

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

classes/Visualizer/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $pri
138138
*
139139
* @access protected
140140
* @param string $tag The name of the filter to hook the $method to.
141-
* @param type $method The name of the method to be called when the filter is applied.
141+
* @param string $method The name of the method to be called when the filter is applied.
142142
* @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
143143
* @param int $accepted_args optional. The number of arguments the function accept (default 1).
144144
* @return Visualizer_Module

classes/Visualizer/Module/Setup.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
4949

5050
$this->_addAction( 'admin_init', 'adminInit' );
5151
$this->_addAction( 'init', 'setupCustomPostTypes' );
52+
$this->_addFilter( 'cron_schedules', 'custom_cron_schedules' );
5253
$this->_addAction( 'plugins_loaded', 'loadTextDomain' );
5354
$this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
5455
$this->_addFilter( 'visualizer_get_chart_counts', 'getUsage', 10, 2 );
@@ -209,7 +210,7 @@ public function activate( $network_wide ) {
209210
*/
210211
private function activate_on_site() {
211212
wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
212-
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'hourly' ), 'visualizer_schedule_refresh_db' );
213+
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'visualizer_ten_minutes' ), 'visualizer_schedule_refresh_db' );
213214
add_option( 'visualizer-activated', true );
214215
$is_fresh_install = get_option( 'visualizer_fresh_install', false );
215216
if ( ! defined( 'TI_CYPRESS_TESTING' ) && false === $is_fresh_install ) {
@@ -281,9 +282,9 @@ public function adminInit() {
281282
/**
282283
* Refresh the specific chart from the db.
283284
*
284-
* @param WP_Post $chart The chart object.
285-
* @param int $chart_id The chart id.
286-
* @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.
285+
* @param WP_Post|null $chart The chart object.
286+
* @param int $chart_id The chart id.
287+
* @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.
287288
*
288289
* @access public
289290
*/
@@ -396,33 +397,43 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
396397
* @access public
397398
*/
398399
public function refreshDbChart() {
399-
$schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
400-
if ( ! $schedules ) {
400+
$chart_schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
401+
if ( ! $chart_schedules ) {
401402
return;
402403
}
404+
403405
if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) {
404406
// define this so that the ajax call does not die
405407
// this means that if the new version of pro and the old version of free are installed, only the first chart will be updated
406408
define( 'VISUALIZER_DO_NOT_DIE', true );
407409
}
408410

409411
$new_schedules = array();
410-
$now = time();
411-
foreach ( $schedules as $chart_id => $time ) {
412-
$new_schedules[ $chart_id ] = $time;
413-
if ( $time > $now ) {
412+
$current_time = time();
413+
foreach ( $chart_schedules as $chart_id => $scheduled_time ) {
414+
415+
// Skip deleted charts.
416+
if ( false === get_post_status( $chart_id ) ) {
417+
continue;
418+
}
419+
420+
$new_schedules[ $chart_id ] = $scheduled_time;
421+
422+
// Should we do an update?
423+
if ( $scheduled_time > $current_time ) {
414424
continue;
415425
}
416426

417-
// if the time is nigh, we force an update.
418427
$this->refresh_db_for_chart( null, $chart_id, true );
428+
419429
// Clear existing chart cache.
420430
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
421431
if ( get_transient( $cache_key ) ) {
422432
delete_transient( $cache_key );
423433
}
424-
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
425-
$new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
434+
435+
$scheduled_hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
436+
$new_schedules[ $chart_id ] = $current_time + $scheduled_hours * HOUR_IN_SECONDS;
426437
}
427438
update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules );
428439
}
@@ -443,4 +454,18 @@ public function checkIsExistingUser() {
443454
}
444455
}
445456

457+
/**
458+
* Add custom cron schedules.
459+
*
460+
* @param array $schedules The current schedules options.
461+
* @return array The modified schedules options.
462+
*/
463+
public function custom_cron_schedules( $schedules ) {
464+
$schedules['visualizer_ten_minutes'] = array(
465+
'interval' => 600,
466+
'display' => __( 'Every 10 minutes', 'visualizer' ),
467+
);
468+
469+
return $schedules;
470+
}
446471
}

tests/test-import.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ public function test_db_import() {
226226
echo $post->post_content;
227227
}
228228

229+
/**
230+
* Testing cron custom schedule.
231+
*
232+
* @return void
233+
*/
234+
public function test_custom_cron_schedule() {
235+
$schedules = wp_get_schedules();
236+
$this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules );
237+
}
238+
229239
/**
230240
* Provide the fileURL for uploading the file
231241
*

0 commit comments

Comments
 (0)