PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4
Post Views Counter v1.4
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 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-cron.php
117 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
23 // filters
24 add_filter( 'cron_schedules', [ $this, 'cron_time_intervals' ] );
25 }
26
27 /**
28 * Reset daily counts.
29 *
30 * @global object $wpdb
31 *
32 * @return void
33 */
34 public function reset_counts() {
35 global $wpdb;
36
37 $counter = [
38 'days' => 1,
39 'weeks' => 7,
40 'months' => 30,
41 'years' => 365
42 ];
43
44 // get main instance
45 $pvc = Post_Views_Counter();
46
47 // get number of columns
48 $noc = $pvc->functions->get_number_of_columns();
49
50 if ( $noc === 5 )
51 $where = ' AND content = 0';
52 else
53 $where = '';
54
55 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'post_views WHERE type = 0' . $where . ' 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)' );
56 }
57
58 /**
59 * Add new cron interval from settings.
60 *
61 * @param array $schedules
62 * @return array
63 */
64 public function cron_time_intervals( $schedules ) {
65 // get main instance
66 $pvc = Post_Views_Counter();
67
68 $schedules['post_views_counter_interval'] = [
69 'interval' => DAY_IN_SECONDS,
70 'display' => __( 'Post Views Counter reset daily counts interval', 'post-views-counter' )
71 ];
72
73 return $schedules;
74 }
75
76 /**
77 * Check whether WP Cron needs to add new task.
78 *
79 * @return void
80 */
81 public function check_cron() {
82 // only for backend
83 if ( ! is_admin() )
84 return;
85
86 // get main instance
87 $pvc = Post_Views_Counter();
88
89 // set wp cron task
90 if ( $pvc->options['general']['cron_run'] ) {
91 // not set or need to be updated?
92 if ( ! wp_next_scheduled( 'pvc_reset_counts' ) || $pvc->options['general']['cron_update'] ) {
93 // task is added but need to be updated
94 if ( $pvc->options['general']['cron_update'] ) {
95 // remove old schedule
96 wp_clear_scheduled_hook( 'pvc_reset_counts' );
97
98 // set update to false
99 $general = $pvc->options['general'];
100 $general['cron_update'] = false;
101
102 // update settings
103 update_option( 'post_views_counter_settings_general', $general );
104 }
105
106 // set schedule
107 wp_schedule_event( current_time( 'timestamp', true ) + DAY_IN_SECONDS, 'post_views_counter_interval', 'pvc_reset_counts' );
108 }
109 } else {
110 // remove schedule
111 wp_clear_scheduled_hook( 'pvc_reset_counts' );
112
113 remove_action( 'pvc_reset_counts', [ $this, 'reset_counts' ] );
114 }
115 }
116 }
117