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