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