columns.php
12 years ago
counter.php
12 years ago
cron.php
12 years ago
frontend.php
11 years ago
functions.php
12 years ago
query.php
12 years ago
settings.php
12 years ago
update.php
12 years ago
widgets.php
12 years ago
cron.php
87 lines
| 1 | <?php |
| 2 | if(!defined('ABSPATH')) exit; |
| 3 | |
| 4 | new Post_Views_Counter_Cron(); |
| 5 | |
| 6 | class Post_Views_Counter_Cron |
| 7 | { |
| 8 | public function __construct() |
| 9 | { |
| 10 | // sets 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 | |
| 17 | // filters |
| 18 | add_filter('cron_schedules', array(&$this, 'cron_time_intervals')); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Resets daily counts |
| 24 | */ |
| 25 | public function reset_counts() |
| 26 | { |
| 27 | global $wpdb; |
| 28 | |
| 29 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'post_views WHERE type = 0'); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Adds new cron interval from settings |
| 35 | */ |
| 36 | public function cron_time_intervals($schedules) |
| 37 | { |
| 38 | $schedules['post_views_counter_interval'] = array( |
| 39 | '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), |
| 40 | 'display' => __('Post Views Counter reset daily counts interval', 'post-views-counter') |
| 41 | ); |
| 42 | |
| 43 | return $schedules; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Checks whether WP Cron needs to add new task |
| 49 | */ |
| 50 | public function check_cron() |
| 51 | { |
| 52 | if(!is_admin()) |
| 53 | return; |
| 54 | |
| 55 | // sets wp cron task |
| 56 | if(Post_Views_Counter()->get_attribute('options', 'general', 'cron_run')) |
| 57 | { |
| 58 | // not set or need to be updated? |
| 59 | if(!wp_next_scheduled('pvc_reset_counts') || Post_Views_Counter()->get_attribute('options', 'general', 'cron_update')) |
| 60 | { |
| 61 | // task is added but need to be updated |
| 62 | if(Post_Views_Counter()->get_attribute('options', 'general', 'cron_update')) |
| 63 | { |
| 64 | // removes old schedule |
| 65 | wp_clear_scheduled_hook('pvc_reset_counts'); |
| 66 | |
| 67 | // sets update to false |
| 68 | $general = Post_Views_Counter()->get_attribute('options', 'general'); |
| 69 | $general['cron_update'] = false; |
| 70 | |
| 71 | // updates settings |
| 72 | update_option('post_views_counter_settings_general', $general); |
| 73 | } |
| 74 | |
| 75 | // sets schedule |
| 76 | 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'); |
| 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | // removes schedule |
| 82 | wp_clear_scheduled_hook('pvc_reset_counts'); |
| 83 | remove_action('pvc_reset_counts', array(&$this, 'reset_counts')); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | ?> |