PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.13
Post Views Counter v1.3.13
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-cron.php
post-views-counter / includes Last commit date
class-admin.php 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-cron.php
128 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 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'init', [ $this, 'check_cron' ] );
21 add_action( 'pvc_reset_counts', [ $this, 'reset_counts' ] );
22 add_action( 'pvc_flush_cached_counts', [ $this, 'flush_cached_counts' ] );
23
24 // filters
25 add_filter( 'cron_schedules', [ $this, 'cron_time_intervals' ] );
26 }
27
28 /**
29 * Reset daily counts.
30 *
31 * @global object $wpdb
32 *
33 * @return void
34 */
35 public function reset_counts() {
36 global $wpdb;
37
38 $counter = [
39 'days' => 1,
40 'weeks' => 7,
41 'months' => 30,
42 'years' => 365
43 ];
44
45 // get main instance
46 $pvc = Post_Views_Counter();
47
48 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'post_views WHERE type = 0 AND CAST( period AS SIGNED ) < CAST( ' . date( 'Ymd', strtotime( '-' . ( (int) ( $counter[$pvc->options['general']['reset_counts']['type']] * $pvc->options['general']['reset_counts']['number'] ) ) . ' days' ) ) . ' AS SIGNED)' );
49 }
50
51 /**
52 * Call Post_Views_Counter_Counter::flush_cache_to_db().
53 * This is (un)scheduled on plugin activation/deactivation.
54 *
55 * @return void
56 */
57 public function flush_cached_counts() {
58 // get counter class
59 $counter = Post_Views_Counter()->counter;
60
61 // caching?
62 if ( $counter && $counter->using_object_cache() )
63 $counter->flush_cache_to_db();
64 }
65
66 /**
67 * Add new cron interval from settings.
68 *
69 * @param array $schedules
70 * @return array
71 */
72 public function cron_time_intervals( $schedules ) {
73 // get main instance
74 $pvc = Post_Views_Counter();
75
76 $schedules['post_views_counter_interval'] = [
77 'interval' => DAY_IN_SECONDS,
78 'display' => __( 'Post Views Counter reset daily counts interval', 'post-views-counter' )
79 ];
80
81 $schedules['post_views_counter_flush_interval'] = [
82 'interval' => $pvc->counter->get_timestamp( $pvc->options['general']['flush_interval']['type'], $pvc->options['general']['flush_interval']['number'], false ),
83 'display' => __( 'Post Views Counter cache flush interval', 'post-views-counter' )
84 ];
85
86 return $schedules;
87 }
88
89 /**
90 * Check whether WP Cron needs to add new task.
91 *
92 * @return void
93 */
94 public function check_cron() {
95 // only for backend
96 if ( ! is_admin() )
97 return;
98
99 // get main instance
100 $pvc = Post_Views_Counter();
101
102 // set wp cron task
103 if ( $pvc->options['general']['cron_run'] ) {
104 // not set or need to be updated?
105 if ( ! wp_next_scheduled( 'pvc_reset_counts' ) || $pvc->options['general']['cron_update'] ) {
106 // task is added but need to be updated
107 if ( $pvc->options['general']['cron_update'] ) {
108 // remove old schedule
109 wp_clear_scheduled_hook( 'pvc_reset_counts' );
110
111 // set update to false
112 $general = $pvc->options['general'];
113 $general['cron_update'] = false;
114
115 // update settings
116 update_option( 'post_views_counter_settings_general', $general );
117 }
118
119 // set schedule
120 wp_schedule_event( current_time( 'timestamp', true ) + DAY_IN_SECONDS, 'post_views_counter_interval', 'pvc_reset_counts' );
121 }
122 } else {
123 // remove schedule
124 wp_clear_scheduled_hook( 'pvc_reset_counts' );
125 remove_action( 'pvc_reset_counts', [ $this, 'reset_counts' ] );
126 }
127 }
128 }