class-admin.php
3 months ago
class-columns-modal.php
1 month ago
class-columns.php
2 weeks ago
class-counter.php
2 months ago
class-crawler-detect.php
7 months ago
class-cron.php
1 month ago
class-dashboard.php
1 month ago
class-emails-mailer.php
1 month ago
class-emails-period.php
1 month ago
class-emails-query.php
1 month ago
class-emails-scheduler.php
1 month ago
class-emails-template.php
1 month ago
class-emails.php
1 month ago
class-frontend.php
2 weeks ago
class-functions.php
1 year ago
class-import.php
2 months ago
class-integration-gutenberg.php
6 months ago
class-integrations.php
2 months ago
class-query.php
2 months ago
class-settings-api.php
1 month ago
class-settings-display.php
4 months ago
class-settings-emails.php
1 month ago
class-settings-general.php
1 month ago
class-settings-integrations.php
5 months ago
class-settings-other.php
1 month ago
class-settings-reports.php
1 month ago
class-settings.php
1 month ago
class-toolbar.php
3 months ago
class-traffic-signals.php
2 months ago
class-update.php
1 month ago
class-widgets.php
1 month ago
functions.php
1 month ago
class-cron.php
120 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 | // default where clause |
| 48 | $where = [ 'type = 0', '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 | // update where clause |
| 51 | $where = apply_filters( 'pvc_reset_counts_where_clause', $where ); |
| 52 | |
| 53 | // delete views |
| 54 | $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'post_views WHERE ' . implode( ' AND ', $where ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Add new cron interval from settings. |
| 59 | * |
| 60 | * @param array $schedules |
| 61 | * @return array |
| 62 | */ |
| 63 | public function cron_time_intervals( $schedules ) { |
| 64 | // get main instance |
| 65 | $pvc = Post_Views_Counter(); |
| 66 | $display = 'Post Views Counter reset daily counts interval'; |
| 67 | |
| 68 | if ( did_action( 'init' ) ) |
| 69 | $display = __( 'Post Views Counter reset daily counts interval', 'post-views-counter' ); |
| 70 | |
| 71 | $schedules['post_views_counter_interval'] = [ |
| 72 | 'interval' => DAY_IN_SECONDS, |
| 73 | 'display' => $display |
| 74 | ]; |
| 75 | |
| 76 | return $schedules; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check whether WP Cron needs to add new task. |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | public function check_cron() { |
| 85 | // only for backend |
| 86 | if ( ! is_admin() ) |
| 87 | return; |
| 88 | |
| 89 | // get main instance |
| 90 | $pvc = Post_Views_Counter(); |
| 91 | |
| 92 | // set wp cron task |
| 93 | if ( $pvc->options['general']['cron_run'] ) { |
| 94 | // not set or need to be updated? |
| 95 | if ( ! wp_next_scheduled( 'pvc_reset_counts' ) || $pvc->options['general']['cron_update'] ) { |
| 96 | // task is added but need to be updated |
| 97 | if ( $pvc->options['general']['cron_update'] ) { |
| 98 | // remove old schedule |
| 99 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 100 | |
| 101 | // set update to false |
| 102 | $general = $pvc->options['general']; |
| 103 | $general['cron_update'] = false; |
| 104 | |
| 105 | // update settings |
| 106 | update_option( 'post_views_counter_settings_general', $general ); |
| 107 | } |
| 108 | |
| 109 | // set schedule |
| 110 | wp_schedule_event( current_time( 'timestamp', true ) + DAY_IN_SECONDS, 'post_views_counter_interval', 'pvc_reset_counts' ); |
| 111 | } |
| 112 | } else { |
| 113 | // remove schedule |
| 114 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 115 | |
| 116 | remove_action( 'pvc_reset_counts', [ $this, 'reset_counts' ] ); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |