| @@ -48,8 +48,9 @@ | ||
| 48 | 48 | $this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 ); |
| 49 | 49 | |
| 50 | 50 | $this->_addAction( 'admin_init', 'adminInit' ); |
| 51 | 51 | $this->_addAction( 'init', 'setupCustomPostTypes' ); |
| 52 | + $this->_addFilter( 'cron_schedules', 'custom_cron_schedules' ); | |
| 52 | 53 | $this->_addAction( 'plugins_loaded', 'loadTextDomain' ); |
| 53 | 54 | $this->_addFilter( 'visualizer_logger_data', 'getLoggerData' ); |
| 54 | 55 | $this->_addFilter( 'visualizer_get_chart_counts', 'getUsage', 10, 2 ); |
| 55 | 56 | |
| @@ -208,9 +209,9 @@ | ||
| 208 | 209 | * Activates the plugin on a particular blog instance (supports multisite and single site). |
| 209 | 210 | */ |
| 210 | 211 | private function activate_on_site() { |
| 211 | 212 | 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' ); | |
| 213 | 214 | add_option( 'visualizer-activated', true ); |
| 214 | 215 | $is_fresh_install = get_option( 'visualizer_fresh_install', false ); |
| 215 | 216 | if ( ! defined( 'TI_CYPRESS_TESTING' ) && false === $is_fresh_install ) { |
| 216 | 217 | update_option( 'visualizer_fresh_install', '1' ); |
| @@ -280,11 +281,11 @@ | ||
| 280 | 281 | |
| 281 | 282 | /** |
| 282 | 283 | * Refresh the specific chart from the db. |
| 283 | 284 | * |
| 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. | |
| 287 | 288 | * |
| 288 | 289 | * @access public |
| 289 | 290 | */ |
| 290 | 291 | public function refresh_db_for_chart( $chart, $chart_id, $force = false ) { |
| @@ -390,17 +391,19 @@ | ||
| 390 | 391 | return $chart; |
| 391 | 392 | } |
| 392 | 393 | |
| 393 | 394 | /** |
| 394 | - * Refresh the db chart. | |
| 395 | + * Refresh the Database chart type. | |
| 395 | 396 | * |
| 396 | 397 | * @access public |
| 397 | 398 | */ |
| 398 | 399 | public function refreshDbChart() { |
| 399 | - $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() ); | |
| 400 | - if ( ! $schedules ) { | |
| 400 | + // NOTE: This use a different key from normal schedule. Updated only by Database chart. Check `visualizer_schedule_import` action. | |
| 401 | + $chart_schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() ); | |
| 402 | + if ( ! $chart_schedules ) { | |
| 401 | 403 | return; |
| 402 | 404 | } |
| 405 | + | |
| 403 | 406 | if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) { |
| 404 | 407 | // define this so that the ajax call does not die |
| 405 | 408 | // this means that if the new version of pro and the old version of free are installed, only the first chart will be updated |
| 406 | 409 | define( 'VISUALIZER_DO_NOT_DIE', true ); |
| @@ -406,24 +409,33 @@ | ||
| 406 | 409 | define( 'VISUALIZER_DO_NOT_DIE', true ); |
| 407 | 410 | } |
| 408 | 411 | |
| 409 | 412 | $new_schedules = array(); |
| 410 | - $now = time(); | |
| 411 | - foreach ( $schedules as $chart_id => $time ) { | |
| 412 | - $new_schedules[ $chart_id ] = $time; | |
| 413 | - if ( $time > $now ) { | |
| 413 | + $current_time = time(); | |
| 414 | + foreach ( $chart_schedules as $chart_id => $scheduled_time ) { | |
| 415 | + | |
| 416 | + // Skip deleted charts. | |
| 417 | + if ( false === get_post_status( $chart_id ) ) { | |
| 414 | 418 | continue; |
| 415 | 419 | } |
| 416 | 420 | |
| 417 | - // if the time is nigh, we force an update. | |
| 421 | + $new_schedules[ $chart_id ] = $scheduled_time; | |
| 422 | + | |
| 423 | + // Should we do an update? | |
| 424 | + if ( $scheduled_time > $current_time ) { | |
| 425 | + continue; | |
| 426 | + } | |
| 427 | + | |
| 418 | 428 | $this->refresh_db_for_chart( null, $chart_id, true ); |
| 429 | + | |
| 419 | 430 | // Clear existing chart cache. |
| 420 | 431 | $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id; |
| 421 | 432 | if ( get_transient( $cache_key ) ) { |
| 422 | 433 | delete_transient( $cache_key ); |
| 423 | 434 | } |
| 424 | - $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 425 | - $new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS; | |
| 435 | + | |
| 436 | + $scheduled_hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 437 | + $new_schedules[ $chart_id ] = $current_time + $scheduled_hours * HOUR_IN_SECONDS; | |
| 426 | 438 | } |
| 427 | 439 | update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules ); |
| 428 | 440 | } |
| 429 | 441 | |
| @@ -442,5 +454,19 @@ | ||
| 442 | 454 | update_option( 'visualizer-new-user', ! empty( $charts ) ? 'no' : 'yes' ); |
| 443 | 455 | } |
| 444 | 456 | } |
| 445 | 457 | |
| 458 | + /** | |
| 459 | + * Add custom cron schedules. | |
| 460 | + * | |
| 461 | + * @param array $schedules The current schedules options. | |
| 462 | + * @return array The modified schedules options. | |
| 463 | + */ | |
| 464 | + public function custom_cron_schedules( $schedules ) { | |
| 465 | + $schedules['visualizer_ten_minutes'] = array( | |
| 466 | + 'interval' => 600, | |
| 467 | + 'display' => __( 'Every 10 minutes', 'visualizer' ), | |
| 468 | + ); | |
| 469 | + | |
| 470 | + return $schedules; | |
| 471 | + } | |
| 446 | 472 | } |