ajax.php
7 years ago
columns.php
4 years ago
counter.php
4 years ago
crawler-detect.php
6 years ago
cron.php
6 years ago
dashboard.php
4 years ago
frontend.php
6 years ago
functions.php
4 years ago
query.php
6 years ago
settings-api.php
4 years ago
settings.php
4 years ago
update.php
6 years ago
widgets.php
4 years ago
cron.php
109 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 | $counter = array( |
| 32 | 'days' => 1, |
| 33 | 'weeks' => 7, |
| 34 | 'months' => 30, |
| 35 | 'years' => 365 |
| 36 | ); |
| 37 | |
| 38 | $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'post_views WHERE type = 0 AND CAST( period AS SIGNED ) < CAST( ' . date( 'Ymd', strtotime( '-' . ( (int) ( $counter[Post_Views_Counter()->options['general']['reset_counts']['type']] * Post_Views_Counter()->options['general']['reset_counts']['number'] ) ) . ' days' ) ) . ' AS SIGNED)' ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Call Post_Views_Counter_Counter::flush_cache_to_db(). |
| 43 | * This is (un)scheduled on plugin activation/deactivation. |
| 44 | */ |
| 45 | public function flush_cached_counts() { |
| 46 | $counter = Post_Views_Counter()->counter; |
| 47 | |
| 48 | if ( $counter && $counter->using_object_cache() ) |
| 49 | $counter->flush_cache_to_db(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Add new cron interval from settings. |
| 54 | * |
| 55 | * @param array $schedules |
| 56 | * @return array |
| 57 | */ |
| 58 | public function cron_time_intervals( $schedules ) { |
| 59 | $schedules['post_views_counter_interval'] = array( |
| 60 | 'interval' => 86400, |
| 61 | 'display' => __( 'Post Views Counter reset daily counts interval', 'post-views-counter' ) |
| 62 | ); |
| 63 | |
| 64 | $schedules['post_views_counter_flush_interval'] = array( |
| 65 | 'interval' => Post_Views_Counter()->counter->get_timestamp( Post_Views_Counter()->options['general']['flush_interval']['type'], Post_Views_Counter()->options['general']['flush_interval']['number'], false ), |
| 66 | 'display' => __( 'Post Views Counter cache flush interval', 'post-views-counter' ) |
| 67 | ); |
| 68 | |
| 69 | return $schedules; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check whether WP Cron needs to add new task. |
| 74 | */ |
| 75 | public function check_cron() { |
| 76 | if ( ! is_admin() ) |
| 77 | return; |
| 78 | |
| 79 | // set wp cron task |
| 80 | if ( Post_Views_Counter()->options['general']['cron_run'] ) { |
| 81 | |
| 82 | // not set or need to be updated? |
| 83 | if ( ! wp_next_scheduled( 'pvc_reset_counts' ) || Post_Views_Counter()->options['general']['cron_update'] ) { |
| 84 | |
| 85 | // task is added but need to be updated |
| 86 | if ( Post_Views_Counter()->options['general']['cron_update'] ) { |
| 87 | // remove old schedule |
| 88 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 89 | |
| 90 | // set update to false |
| 91 | $general = Post_Views_Counter()->options['general']; |
| 92 | $general['cron_update'] = false; |
| 93 | |
| 94 | // update settings |
| 95 | update_option( 'post_views_counter_settings_general', $general ); |
| 96 | } |
| 97 | |
| 98 | // set schedule |
| 99 | wp_schedule_event( current_time( 'timestamp', true ) + 86400, 'post_views_counter_interval', 'pvc_reset_counts' ); |
| 100 | } |
| 101 | } else { |
| 102 | // remove schedule |
| 103 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 104 | remove_action( 'pvc_reset_counts', array( $this, 'reset_counts' ) ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } |
| 109 |