@@ -209,8 +209,7 @@ public function activate( $network_wide ) {
209209 * Activates the plugin on a particular blog instance (supports multisite and single site).
210210 */
211211 private function activate_on_site () {
212- wp_clear_scheduled_hook ( '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 ' );
212+ $ this ->schedule_refresh_db_action ();
214213 add_option ( 'visualizer-activated ' , true );
215214 $ is_fresh_install = get_option ( 'visualizer_fresh_install ' , false );
216215 if ( ! defined ( 'TI_E2E_TESTING ' ) && false === $ is_fresh_install ) {
@@ -237,7 +236,7 @@ public function deactivate( $network_wide ) {
237236 * Deactivates the plugin on a particular blog instance (supports multisite and single site).
238237 */
239238 private function deactivate_on_site () {
240- wp_clear_scheduled_hook ( ' visualizer_schedule_refresh_db ' );
239+ $ this -> unschedule_refresh_db_action ( );
241240 delete_option ( 'visualizer-activated ' , true );
242241 }
243242
@@ -469,4 +468,54 @@ public function custom_cron_schedules( $schedules ) {
469468
470469 return $ schedules ;
471470 }
471+
472+ /**
473+ * Schedule the recurring DB refresh action.
474+ */
475+ private function schedule_refresh_db_action (): void {
476+ $ hook = 'visualizer_schedule_refresh_db ' ;
477+ $ group = 'visualizer ' ;
478+ $ interval_key = apply_filters ( 'visualizer_chart_schedule_interval ' , 'visualizer_ten_minutes ' );
479+ $ interval = $ this ->get_schedule_interval_seconds ( $ interval_key );
480+ $ timestamp = strtotime ( 'midnight ' ) - get_option ( 'gmt_offset ' ) * HOUR_IN_SECONDS ;
481+
482+ if ( function_exists ( 'as_next_scheduled_action ' ) && function_exists ( 'as_schedule_recurring_action ' ) ) {
483+ $ next = as_next_scheduled_action ( $ hook , array (), $ group );
484+ if ( false === $ next ) {
485+ as_schedule_recurring_action ( $ timestamp , $ interval , $ hook , array (), $ group );
486+ }
487+ wp_clear_scheduled_hook ( $ hook );
488+ return ;
489+ }
490+
491+ wp_clear_scheduled_hook ( $ hook );
492+ wp_schedule_event ( $ timestamp , $ interval_key , $ hook );
493+ }
494+
495+ /**
496+ * Unschedule the recurring DB refresh action.
497+ */
498+ private function unschedule_refresh_db_action (): void {
499+ $ hook = 'visualizer_schedule_refresh_db ' ;
500+ $ group = 'visualizer ' ;
501+ if ( function_exists ( 'as_unschedule_all_actions ' ) ) {
502+ as_unschedule_all_actions ( $ hook , array (), $ group );
503+ }
504+ wp_clear_scheduled_hook ( $ hook );
505+ }
506+
507+ /**
508+ * Resolve a cron schedule key to seconds.
509+ *
510+ * @param string $interval_key Cron schedule key.
511+ * @return int Interval in seconds.
512+ */
513+ private function get_schedule_interval_seconds ( $ interval_key ) {
514+ $ schedules = wp_get_schedules ();
515+ if ( isset ( $ schedules [ $ interval_key ]['interval ' ] ) ) {
516+ return (int ) $ schedules [ $ interval_key ]['interval ' ];
517+ }
518+
519+ return 600 ;
520+ }
472521}
0 commit comments