| @@ -208,10 +208,9 @@ | ||
| 208 | 208 | /** |
| 209 | 209 | * Activates the plugin on a particular blog instance (supports multisite and single site). |
| 210 | 210 | */ |
| 211 | 211 | 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(); | |
| 214 | 213 | add_option( 'visualizer-activated', true ); |
| 215 | 214 | $is_fresh_install = get_option( 'visualizer_fresh_install', false ); |
| 216 | 215 | if ( ! defined( 'TI_E2E_TESTING' ) && false === $is_fresh_install ) { |
| 217 | 216 | update_option( 'visualizer_fresh_install', '1' ); |
| @@ -236,9 +235,9 @@ | ||
| 236 | 235 | /** |
| 237 | 236 | * Deactivates the plugin on a particular blog instance (supports multisite and single site). |
| 238 | 237 | */ |
| 239 | 238 | private function deactivate_on_site() { |
| 240 | - wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' ); | |
| 239 | + $this->unschedule_refresh_db_action(); | |
| 241 | 240 | delete_option( 'visualizer-activated', true ); |
| 242 | 241 | } |
| 243 | 242 | |
| 244 | 243 | /** |
| @@ -252,12 +251,23 @@ | ||
| 252 | 251 | define( 'VISUALIZER_SURVEY', Visualizer_Module::is_pro() ? 'https://forms.gle/7Zo7FuZbvQ8DTvRi6' : 'https://forms.gle/muMtbcyvHn1aTvmJ7' ); |
| 253 | 252 | // fire any upgrades necessary. |
| 254 | 253 | Visualizer_Module_Upgrade::upgrade(); |
| 255 | 254 | |
| 256 | - if ( get_option( 'visualizer-activated' ) ) { | |
| 255 | + $activated_flag = get_option( 'visualizer-activated' ); | |
| 256 | + $fresh_install = get_option( 'visualizer_fresh_install', false ); | |
| 257 | + $is_pro = Visualizer_Module::is_pro(); | |
| 258 | + if ( $activated_flag ) { | |
| 259 | + if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { | |
| 260 | + // Defer redirect until a normal admin request. | |
| 261 | + return; | |
| 262 | + } | |
| 263 | + if ( wp_doing_cron() ) { | |
| 264 | + // Defer redirect during cron requests. | |
| 265 | + return; | |
| 266 | + } | |
| 257 | 267 | delete_option( 'visualizer-activated' ); |
| 258 | 268 | if ( ! headers_sent() ) { |
| 259 | - if ( ! Visualizer_Module::is_pro() && ! empty( get_option( 'visualizer_fresh_install', false ) ) ) { | |
| 269 | + if ( ! $is_pro && ! empty( $fresh_install ) ) { | |
| 260 | 270 | $redirect_url = array( |
| 261 | 271 | 'page' => 'visualizer-setup-wizard', |
| 262 | 272 | 'tab' => '#step-1', |
| 263 | 273 | ); |
| @@ -467,6 +477,56 @@ | ||
| 467 | 477 | 'display' => __( 'Every 10 minutes', 'visualizer' ), |
| 468 | 478 | ); |
| 469 | 479 | |
| 470 | 480 | return $schedules; |
| 481 | + } | |
| 482 | + | |
| 483 | + /** | |
| 484 | + * Schedule the recurring DB refresh action. | |
| 485 | + */ | |
| 486 | + private function schedule_refresh_db_action(): void { | |
| 487 | + $hook = 'visualizer_schedule_refresh_db'; | |
| 488 | + $group = 'visualizer'; | |
| 489 | + $interval_key = apply_filters( 'visualizer_chart_schedule_interval', 'visualizer_ten_minutes' ); | |
| 490 | + $interval = $this->get_schedule_interval_seconds( $interval_key ); | |
| 491 | + $timestamp = strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS; | |
| 492 | + | |
| 493 | + if ( function_exists( 'as_next_scheduled_action' ) && function_exists( 'as_schedule_recurring_action' ) ) { | |
| 494 | + $next = as_next_scheduled_action( $hook, array(), $group ); | |
| 495 | + if ( false === $next ) { | |
| 496 | + as_schedule_recurring_action( $timestamp, $interval, $hook, array(), $group ); | |
| 497 | + } | |
| 498 | + wp_clear_scheduled_hook( $hook ); | |
| 499 | + return; | |
| 500 | + } | |
| 501 | + | |
| 502 | + wp_clear_scheduled_hook( $hook ); | |
| 503 | + wp_schedule_event( $timestamp, $interval_key, $hook ); | |
| 504 | + } | |
| 505 | + | |
| 506 | + /** | |
| 507 | + * Unschedule the recurring DB refresh action. | |
| 508 | + */ | |
| 509 | + private function unschedule_refresh_db_action(): void { | |
| 510 | + $hook = 'visualizer_schedule_refresh_db'; | |
| 511 | + $group = 'visualizer'; | |
| 512 | + if ( function_exists( 'as_unschedule_all_actions' ) ) { | |
| 513 | + as_unschedule_all_actions( $hook, array(), $group ); | |
| 514 | + } | |
| 515 | + wp_clear_scheduled_hook( $hook ); | |
| 516 | + } | |
| 517 | + | |
| 518 | + /** | |
| 519 | + * Resolve a cron schedule key to seconds. | |
| 520 | + * | |
| 521 | + * @param string $interval_key Cron schedule key. | |
| 522 | + * @return int Interval in seconds. | |
| 523 | + */ | |
| 524 | + private function get_schedule_interval_seconds( $interval_key ) { | |
| 525 | + $schedules = wp_get_schedules(); | |
| 526 | + if ( isset( $schedules[ $interval_key ]['interval'] ) ) { | |
| 527 | + return (int) $schedules[ $interval_key ]['interval']; | |
| 528 | + } | |
| 529 | + | |
| 530 | + return 600; | |
| 471 | 531 | } |
| 472 | 532 | } |