PluginProbe
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / trunk
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress vtrunk
4.12.0 4.10.0 4.9.0 4.8.1 trunk 1.0 1.1 1.12.1 1.2.3 1.2.4 1.2.5 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2 All 171 releases
custom-facebook-feed / inc / UsageTracking / Core / Scheduler.php
Scheduler.php
93 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Schedules the weekly usage tracking cron. Mirrors the legacy weekly schedule pattern.
4 *
5 * @package CustomFacebookFeed\UsageTracking\Core
6 * @since 4.0
7 */
8
9 namespace CustomFacebookFeed\UsageTracking\Core;
10
11 use CustomFacebookFeed\UsageTracking\Config;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 class Scheduler {
18
19 /**
20 * Ensure weekly cron is scheduled when tracking is enabled.
21 * Call on init or when enabling the option.
22 */
23 public function schedule() {
24 $this->cleanup_legacy();
25
26 if ( ! Config::is_enabled() ) {
27 return;
28 }
29
30 if ( wp_next_scheduled( Config::CRON_HOOK ) ) {
31 return;
32 }
33
34 $tracking = array(
35 'day' => wp_rand( 0, 6 ),
36 'hour' => wp_rand( 0, 23 ),
37 'minute' => wp_rand( 0, 59 ),
38 'second' => wp_rand( 0, 59 ),
39 );
40 $tracking['offset'] = ($tracking['day'] * DAY_IN_SECONDS)
41 + ($tracking['hour'] * HOUR_IN_SECONDS)
42 + ($tracking['minute'] * MINUTE_IN_SECONDS)
43 + $tracking['second'];
44
45 $last_sunday = strtotime( 'next sunday' ) - (7 * DAY_IN_SECONDS);
46 if ( ($last_sunday + $tracking['offset']) > time() + 6 * HOUR_IN_SECONDS ) {
47 $tracking['initsend'] = $last_sunday + $tracking['offset'];
48 } else {
49 $tracking['initsend'] = strtotime( 'next sunday' ) + $tracking['offset'];
50 }
51
52 wp_schedule_event( $tracking['initsend'], 'weekly', Config::CRON_HOOK );
53 update_option( Config::OPTION_SCHEDULE, $tracking, false );
54 }
55
56 /**
57 * Add weekly schedule if not already present.
58 *
59 * @param array $schedules Existing cron schedules.
60 * @return array
61 */
62 public function add_schedules( $schedules ) {
63 if ( ! isset( $schedules['weekly'] ) ) {
64 $schedules['weekly'] = array(
65 'interval' => 604800,
66 'display' => __( 'Once Weekly', 'custom-facebook-feed' ),
67 );
68 }
69 return $schedules;
70 }
71
72 /**
73 * Unschedule the cron when tracking is disabled.
74 */
75 public function unschedule() {
76 wp_clear_scheduled_hook( Config::CRON_HOOK );
77 }
78
79 /**
80 * Clear the removed legacy tracking cron and delete its superseded options.
81 * Guarded so steady-state cost is a no-op.
82 */
83 private function cleanup_legacy() {
84 if ( wp_next_scheduled( 'cff_usage_tracking_cron' ) ) {
85 wp_clear_scheduled_hook( 'cff_usage_tracking_cron' );
86 }
87 if ( get_option( 'cff_smash_usage_tracking', null ) !== null ) {
88 delete_option( 'cff_smash_usage_tracking' );
89 delete_option( 'cff_usage_tracking_config' );
90 }
91 }
92 }
93