Skip to content

fix: switch to 10 minutes cron #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $pri
*
* @access protected
* @param string $tag The name of the filter to hook the $method to.
* @param type $method The name of the method to be called when the filter is applied.
* @param string $method The name of the method to be called when the filter is applied.
* @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.
* @param int $accepted_args optional. The number of arguments the function accept (default 1).
* @return Visualizer_Module
Expand Down
51 changes: 38 additions & 13 deletions classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct( Visualizer_Plugin $plugin ) {

$this->_addAction( 'admin_init', 'adminInit' );
$this->_addAction( 'init', 'setupCustomPostTypes' );
$this->_addFilter( 'cron_schedules', 'custom_cron_schedules' );
$this->_addAction( 'plugins_loaded', 'loadTextDomain' );
$this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
$this->_addFilter( 'visualizer_get_chart_counts', 'getUsage', 10, 2 );
Expand Down Expand Up @@ -209,7 +210,7 @@ public function activate( $network_wide ) {
*/
private function activate_on_site() {
wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'hourly' ), 'visualizer_schedule_refresh_db' );
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' );
add_option( 'visualizer-activated', true );
$is_fresh_install = get_option( 'visualizer_fresh_install', false );
if ( ! defined( 'TI_CYPRESS_TESTING' ) && false === $is_fresh_install ) {
Expand Down Expand Up @@ -281,9 +282,9 @@ public function adminInit() {
/**
* Refresh the specific chart from the db.
*
* @param WP_Post $chart The chart object.
* @param int $chart_id The chart id.
* @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.
* @param WP_Post|null $chart The chart object.
* @param int $chart_id The chart id.
* @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.
*
* @access public
*/
Expand Down Expand Up @@ -396,33 +397,43 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
* @access public
*/
public function refreshDbChart() {
$schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
if ( ! $schedules ) {
$chart_schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
if ( ! $chart_schedules ) {
return;
}

if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) {
// define this so that the ajax call does not die
// this means that if the new version of pro and the old version of free are installed, only the first chart will be updated
define( 'VISUALIZER_DO_NOT_DIE', true );
}

$new_schedules = array();
$now = time();
foreach ( $schedules as $chart_id => $time ) {
$new_schedules[ $chart_id ] = $time;
if ( $time > $now ) {
$current_time = time();
foreach ( $chart_schedules as $chart_id => $scheduled_time ) {

// Skip deleted charts.
if ( false === get_post_status( $chart_id ) ) {
continue;
}

$new_schedules[ $chart_id ] = $scheduled_time;

// Should we do an update?
if ( $scheduled_time > $current_time ) {
continue;
}

// if the time is nigh, we force an update.
$this->refresh_db_for_chart( null, $chart_id, true );

// Clear existing chart cache.
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
if ( get_transient( $cache_key ) ) {
delete_transient( $cache_key );
}
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
$new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;

$scheduled_hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
$new_schedules[ $chart_id ] = $current_time + $scheduled_hours * HOUR_IN_SECONDS;
}
update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules );
}
Expand All @@ -443,4 +454,18 @@ public function checkIsExistingUser() {
}
}

/**
* Add custom cron schedules.
*
* @param array $schedules The current schedules options.
* @return array The modified schedules options.
*/
public function custom_cron_schedules( $schedules ) {
$schedules['visualizer_ten_minutes'] = array(
'interval' => 600,
'display' => __( 'Every 10 minutes', 'visualizer' ),
);

return $schedules;
}
}
10 changes: 10 additions & 0 deletions tests/test-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ public function test_db_import() {
echo $post->post_content;
}

/**
* Testing cron custom schedule.
*
* @return void
*/
public function test_custom_cron_schedule() {
$schedules = wp_get_schedules();
$this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules );
}

/**
* Provide the fileURL for uploading the file
*
Expand Down
Loading