columns.php
9 years ago
counter.php
9 years ago
cron.php
9 years ago
dashboard.php
9 years ago
frontend.php
9 years ago
functions.php
9 years ago
query.php
9 years ago
settings.php
9 years ago
update.php
9 years ago
widgets.php
9 years ago
cron.php
100 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Cron class. |
| 8 | */ |
| 9 | class Post_Views_Counter_Cron { |
| 10 | |
| 11 | public function __construct() { |
| 12 | // actions |
| 13 | add_action( 'init', array( $this, 'check_cron' ) ); |
| 14 | add_action( 'pvc_reset_counts', array( $this, 'reset_counts' ) ); |
| 15 | add_action( 'pvc_flush_cached_counts', array( $this, 'flush_cached_counts' ) ); |
| 16 | |
| 17 | // filters |
| 18 | add_filter( 'cron_schedules', array( $this, 'cron_time_intervals' ) ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Reset daily counts. |
| 23 | * |
| 24 | * @global object $wpdb |
| 25 | */ |
| 26 | public function reset_counts() { |
| 27 | global $wpdb; |
| 28 | |
| 29 | $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'post_views WHERE type = 0' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Call Post_Views_Counter_Counter::flush_cache_to_db(). |
| 34 | * This is (un)scheduled on plugin activation/deactivation. |
| 35 | */ |
| 36 | public function flush_cached_counts() { |
| 37 | $counter = Post_Views_Counter()->counter; |
| 38 | |
| 39 | if ( $counter && $counter->using_object_cache() ) |
| 40 | $counter->flush_cache_to_db(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add new cron interval from settings. |
| 45 | * |
| 46 | * @param array $schedules |
| 47 | * @return array |
| 48 | */ |
| 49 | public function cron_time_intervals( $schedules ) { |
| 50 | $schedules['post_views_counter_interval'] = array( |
| 51 | 'interval' => Post_Views_Counter()->counter->get_timestamp( Post_Views_Counter()->options['general']['reset_counts']['type'], Post_Views_Counter()->options['general']['reset_counts']['number'], false ), |
| 52 | 'display' => __( 'Post Views Counter reset daily counts interval', 'post-views-counter' ) |
| 53 | ); |
| 54 | |
| 55 | $schedules['post_views_counter_flush_interval'] = array( |
| 56 | 'interval' => Post_Views_Counter()->counter->get_timestamp( Post_Views_Counter()->options['general']['flush_interval']['type'], Post_Views_Counter()->options['general']['flush_interval']['number'], false ), |
| 57 | 'display' => __( 'Post Views Counter cache flush interval', 'post-views-counter' ) |
| 58 | ); |
| 59 | |
| 60 | return $schedules; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check whether WP Cron needs to add new task. |
| 65 | */ |
| 66 | public function check_cron() { |
| 67 | if ( ! is_admin() ) |
| 68 | return; |
| 69 | |
| 70 | // set wp cron task |
| 71 | if ( Post_Views_Counter()->options['general']['cron_run'] ) { |
| 72 | |
| 73 | // not set or need to be updated? |
| 74 | if ( ! wp_next_scheduled( 'pvc_reset_counts' ) || Post_Views_Counter()->options['general']['cron_update'] ) { |
| 75 | |
| 76 | // task is added but need to be updated |
| 77 | if ( Post_Views_Counter()->options['general']['cron_update'] ) { |
| 78 | // remove old schedule |
| 79 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 80 | |
| 81 | // set update to false |
| 82 | $general = Post_Views_Counter()->options['general']; |
| 83 | $general['cron_update'] = false; |
| 84 | |
| 85 | // update settings |
| 86 | update_option( 'post_views_counter_settings_general', $general ); |
| 87 | } |
| 88 | |
| 89 | // set schedule |
| 90 | wp_schedule_event( Post_Views_Counter()->counter->get_timestamp( Post_Views_Counter()->options['general']['reset_counts']['type'], Post_Views_Counter()->options['general']['reset_counts']['number'] ), 'post_views_counter_interval', 'pvc_reset_counts' ); |
| 91 | } |
| 92 | } else { |
| 93 | // remove schedule |
| 94 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 95 | remove_action( 'pvc_reset_counts', array( $this, 'reset_counts' ) ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } |
| 100 |