PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.0
WebberZone Top 10 — Popular Posts v4.3.0
4.5.2 4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 All 118 releases
top-10 / includes / admin / class-cron.php

class-cron.php in WebberZone Top 10 — Popular Posts 4.3.0, at includes/admin/class-cron.php

211 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cron class.
4 *
5 * @package WebberZone\Top_Ten\Admin
6 */
7
8 namespace WebberZone\Top_Ten\Admin;
9
10 use WebberZone\Top_Ten\Database;
11 use WebberZone\Top_Ten\Util\Hook_Registry;
12
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Class Cron
19 */
20 class Cron {
21 /**
22 * Whether the aggregation cron was missing and had to be rescheduled.
23 *
24 * @var bool
25 */
26 private bool $aggregation_cron_was_missing = false;
27
28 /**
29 * Initialize the class.
30 */
31 public function __construct() {
32 Hook_Registry::add_action( 'tptn_cron_hook', array( $this, 'run_cron' ) );
33 Hook_Registry::add_action( 'tptn_aggregation_cron_hook', array( $this, 'run_aggregation' ) );
34 Hook_Registry::add_action( 'admin_init', array( $this, 'check_aggregation_cron' ) );
35 Hook_Registry::add_action( 'admin_notices', array( $this, 'aggregation_cron_missing_notice' ) );
36 }
37
38 /**
39 * Function to truncate daily run.
40 *
41 * @since 3.0.0
42 */
43 public function run_cron() {
44 global $wpdb;
45
46 $delete_from = TOP_TEN_STORE_DATA;
47
48 /**
49 * Override maintenance day range.
50 *
51 * @since 2.5.0
52 *
53 * @param int $delete_from Number of days before which post data is deleted from daily tables.
54 */
55 $delete_from = apply_filters( 'tptn_maintenance_days', $delete_from );
56
57 $log_retention = TOP_TEN_LOG_STORE_DATA;
58
59 /**
60 * Override retention period for the visits log table.
61 *
62 * @since 4.3.0
63 *
64 * @param int $log_retention Number of days to retain raw visit log rows.
65 */
66 $log_retention = apply_filters( 'tptn_log_retention_days', $log_retention );
67
68 $current_time = strtotime( current_time( 'mysql' ) );
69 $cutoff = strtotime( "-{$delete_from} DAY", $current_time );
70 $cutoff_log = strtotime( "-{$log_retention} DAY", $current_time );
71 $from_date = gmdate( 'Y-m-d H', $cutoff );
72 $from_date_log = gmdate( 'Y-m-d H:i:s', $cutoff_log );
73
74 // Delete old daily entries.
75 $args = array(
76 'daily' => true,
77 'to_date' => $from_date,
78 'limit' => 1000,
79 );
80
81 $deadline = microtime( true ) + 20;
82 do {
83 $deleted = Database::delete_counts( $args );
84 } while ( $deleted > 0 && microtime( true ) < $deadline );
85
86 // Prune visits log rows older than the retention period.
87 $log_table = Database::get_log_table();
88 $deadline_log = microtime( true ) + 20;
89 do {
90 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
91 $deleted_log = $wpdb->query( $wpdb->prepare( "DELETE FROM {$log_table} WHERE visited_at < %s LIMIT 1000", $from_date_log ) );
92 } while ( $deleted_log > 0 && microtime( true ) < $deadline_log );
93 }
94
95 /**
96 * Function to enable run or actions.
97 *
98 * @since 3.0.0
99 * @param int $hour Hour.
100 * @param int $min Minute.
101 * @param string $recurrence Frequency.
102 */
103 public static function enable_run( $hour, $min, $recurrence ) {
104 // Invoke WordPress internal cron.
105 if ( ! wp_next_scheduled( 'tptn_cron_hook' ) ) {
106 wp_schedule_event( mktime( $hour, $min, 0 ), $recurrence, 'tptn_cron_hook' );
107 } else {
108 wp_clear_scheduled_hook( 'tptn_cron_hook' );
109 wp_schedule_event( mktime( $hour, $min, 0 ), $recurrence, 'tptn_cron_hook' );
110 }
111 }
112
113 /**
114 * Function to disable daily run or actions.
115 *
116 * @since 3.0.0
117 */
118 public static function disable_run() {
119 if ( wp_next_scheduled( 'tptn_cron_hook' ) ) {
120 wp_clear_scheduled_hook( 'tptn_cron_hook' );
121 }
122 }
123
124 /**
125 * Drain the visits log into the count tables and prune orphaned log rows.
126 *
127 * @since 4.3.0
128 */
129 public function run_aggregation() {
130 Database::aggregate_visit_log();
131 }
132
133 /**
134 * Schedule the aggregation cron to run at the configured interval.
135 *
136 * @since 4.3.0
137 */
138 public static function enable_aggregation_run() {
139 /**
140 * Override the aggregation cron schedule. Must be a registered WP-Cron recurrence
141 * (e.g. 'one_minute', 'two_minutes', 'three_minutes', 'five_minutes').
142 *
143 * @since 4.3.0
144 *
145 * @param string $interval Schedule name. Default 'five_minutes'.
146 */
147 $interval = (string) apply_filters( 'tptn_aggregation_cron_interval', 'five_minutes' );
148
149 if ( ! wp_next_scheduled( 'tptn_aggregation_cron_hook' ) ) {
150 wp_schedule_event( time(), $interval, 'tptn_aggregation_cron_hook' );
151 }
152 }
153
154 /**
155 * Remove the aggregation cron.
156 *
157 * @since 4.3.0
158 */
159 public static function disable_aggregation_run() {
160 wp_clear_scheduled_hook( 'tptn_aggregation_cron_hook' );
161 }
162
163 /**
164 * Check that the aggregation cron is scheduled at the correct interval; reschedule if missing or changed.
165 *
166 * @since 4.3.0
167 */
168 public function check_aggregation_cron() {
169 /** This filter is documented in includes/admin/class-cron.php */
170 $interval = (string) apply_filters( 'tptn_aggregation_cron_interval', 'five_minutes' );
171
172 $timestamp = wp_next_scheduled( 'tptn_aggregation_cron_hook' );
173
174 if ( ! $timestamp ) {
175 self::enable_aggregation_run();
176 $this->aggregation_cron_was_missing = true;
177 return;
178 }
179
180 $crons = _get_cron_array();
181 $args_key = md5( serialize( array() ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
182 $current = isset( $crons[ $timestamp ]['tptn_aggregation_cron_hook'][ $args_key ]['schedule'] )
183 ? $crons[ $timestamp ]['tptn_aggregation_cron_hook'][ $args_key ]['schedule']
184 : '';
185
186 if ( $current !== $interval ) {
187 wp_clear_scheduled_hook( 'tptn_aggregation_cron_hook' );
188 self::enable_aggregation_run();
189 $this->aggregation_cron_was_missing = true;
190 }
191 }
192
193 /**
194 * Show an admin notice when the aggregation cron was found missing and rescheduled.
195 *
196 * @since 4.3.0
197 */
198 public function aggregation_cron_missing_notice() {
199 if ( ! $this->aggregation_cron_was_missing ) {
200 return;
201 }
202 ?>
203 <div class="notice notice-warning is-dismissible">
204 <p>
205 <?php esc_html_e( 'Top 10: The visit aggregation cron job (tptn_aggregation_cron_hook) was missing and has been rescheduled automatically.', 'top-10' ); ?>
206 </p>
207 </div>
208 <?php
209 }
210 }
211