From f16ca1f065c2b9900f8738744ea8423dfef56188 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 30 May 2024 17:34:27 +0300 Subject: [PATCH 1/4] fix: switch to 10 minutes cron --- classes/Visualizer/Module.php | 2 +- classes/Visualizer/Module/Setup.php | 51 +++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index c77cfc12..5604ea5d 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -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 diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index d392894d..0599c504 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -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 ); @@ -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 ) { @@ -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 */ @@ -396,10 +397,11 @@ 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 @@ -407,22 +409,31 @@ public function refreshDbChart() { } $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 ); } @@ -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; + } } From 6bc1ec8b906da4ec07f43b7e0de15f9e86980b1a Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Fri, 31 May 2024 12:11:34 +0300 Subject: [PATCH 2/4] chore: phpunit test for chart refresh cron --- tests/test-import.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test-import.php b/tests/test-import.php index 384f5724..a1b10e91 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -226,6 +226,37 @@ public function test_db_import() { echo $post->post_content; } + /** + * Testing the cron job for refreshing the charts. + * + * @return void + */ + public function test_chart_refresh_cron() { + // Check custom schedule. + $schedules = wp_get_schedules(); + $this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules ); + + // Check if the cron job is scheduled for charts data refresh. + $cron_jobs = _get_cron_array(); + $has_schedule = false; + $schedule_data = array(); + foreach ( $cron_jobs as $time => $jobs ) { + foreach ( $jobs as $job => $data ) { + if ( 'visualizer_schedule_refresh_db' === $job ) { + $has_schedule = true; + foreach ( $data as $key => $value ) { + $schedule_data = $value; + break; + } + break; + } + } + } + + $this->assertTrue( $has_schedule ); + $this->assertEquals( 'visualizer_ten_minutes', $schedule_data['schedule'] ); + } + /** * Provide the fileURL for uploading the file * From cc30883c39a84a8a7a3ca683ea230b6db0895788 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Fri, 31 May 2024 12:18:40 +0300 Subject: [PATCH 3/4] chore: split test --- tests/test-import.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test-import.php b/tests/test-import.php index a1b10e91..285ed6b7 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -227,19 +227,28 @@ public function test_db_import() { } /** - * Testing the cron job for refreshing the charts. + * Testing cron custom schedule. * * @return void */ - public function test_chart_refresh_cron() { + public function test_custom_cron_schedule() { // Check custom schedule. $schedules = wp_get_schedules(); $this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules ); + } - // Check if the cron job is scheduled for charts data refresh. + /** + * Testing if the cron job is scheduled for charts data refresh. + * + * @return void + */ + public function test_chart_refresh_cron() { $cron_jobs = _get_cron_array(); $has_schedule = false; $schedule_data = array(); + + print_r( $cron_jobs); + foreach ( $cron_jobs as $time => $jobs ) { foreach ( $jobs as $job => $data ) { if ( 'visualizer_schedule_refresh_db' === $job ) { From 4184b7eb205c50fee5758b7763d8deda3a51fb26 Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Fri, 31 May 2024 12:43:53 +0300 Subject: [PATCH 4/4] chore: remove cron checker test --- tests/test-import.php | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/tests/test-import.php b/tests/test-import.php index 285ed6b7..8a189488 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -232,40 +232,10 @@ public function test_db_import() { * @return void */ public function test_custom_cron_schedule() { - // Check custom schedule. $schedules = wp_get_schedules(); $this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules ); } - /** - * Testing if the cron job is scheduled for charts data refresh. - * - * @return void - */ - public function test_chart_refresh_cron() { - $cron_jobs = _get_cron_array(); - $has_schedule = false; - $schedule_data = array(); - - print_r( $cron_jobs); - - foreach ( $cron_jobs as $time => $jobs ) { - foreach ( $jobs as $job => $data ) { - if ( 'visualizer_schedule_refresh_db' === $job ) { - $has_schedule = true; - foreach ( $data as $key => $value ) { - $schedule_data = $value; - break; - } - break; - } - } - } - - $this->assertTrue( $has_schedule ); - $this->assertEquals( 'visualizer_ten_minutes', $schedule_data['schedule'] ); - } - /** * Provide the fileURL for uploading the file *