class-admin.php
3 weeks ago
class-columns-modal.php
3 weeks ago
class-columns.php
3 weeks ago
class-counter.php
3 weeks ago
class-crawler-detect.php
3 weeks ago
class-cron.php
3 weeks ago
class-dashboard.php
3 weeks ago
class-emails-mailer.php
3 weeks ago
class-emails-period.php
3 weeks ago
class-emails-query.php
3 weeks ago
class-emails-scheduler.php
3 weeks ago
class-emails-template.php
3 weeks ago
class-emails.php
3 weeks ago
class-frontend.php
3 weeks ago
class-functions.php
3 weeks ago
class-import.php
3 weeks ago
class-integration-gutenberg.php
3 weeks ago
class-integrations.php
3 weeks ago
class-query.php
3 weeks ago
class-settings-api.php
3 weeks ago
class-settings-display.php
3 weeks ago
class-settings-emails.php
3 weeks ago
class-settings-general.php
3 weeks ago
class-settings-integrations.php
3 weeks ago
class-settings-other.php
3 weeks ago
class-settings-reports.php
3 weeks ago
class-settings.php
3 weeks ago
class-toolbar.php
3 weeks ago
class-traffic-signals.php
3 weeks ago
class-update.php
3 weeks ago
class-widgets.php
3 weeks ago
functions.php
3 weeks ago
class-emails-scheduler.php
252 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Emails_Scheduler class. |
| 8 | * |
| 9 | * @class Post_Views_Counter_Emails_Scheduler |
| 10 | */ |
| 11 | class Post_Views_Counter_Emails_Scheduler { |
| 12 | |
| 13 | /** |
| 14 | * @var Post_Views_Counter |
| 15 | */ |
| 16 | private $pvc; |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | private $hook = 'pvc_weekly_content_summary_send'; |
| 22 | |
| 23 | /** |
| 24 | * Class constructor. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | $this->pvc = Post_Views_Counter(); |
| 30 | |
| 31 | add_action( 'pvc_weekly_content_summary_send', [ $this, 'send_weekly_summary' ] ); |
| 32 | add_filter( 'cron_schedules', [ $this, 'add_cron_schedules' ] ); |
| 33 | add_action( 'pvc_configuration_updated', [ $this, 'handle_configuration_updated' ], 10, 2 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register the weekly email summary schedule. |
| 38 | * |
| 39 | * @param array $schedules |
| 40 | * @return array |
| 41 | */ |
| 42 | public function add_cron_schedules( $schedules ) { |
| 43 | $display = 'Post Views Counter weekly email summary interval'; |
| 44 | |
| 45 | if ( did_action( 'init' ) ) |
| 46 | $display = __( 'Post Views Counter weekly email summary interval', 'post-views-counter' ); |
| 47 | |
| 48 | $schedules['post_views_counter_weekly'] = [ |
| 49 | 'interval' => WEEK_IN_SECONDS, |
| 50 | 'display' => $display |
| 51 | ]; |
| 52 | |
| 53 | return $schedules; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Schedule the weekly summary event when enabled, otherwise clear it. |
| 58 | * |
| 59 | * @param array|null $settings |
| 60 | * @return int|false |
| 61 | */ |
| 62 | public function maybe_schedule( $settings = null ) { |
| 63 | if ( $this->is_enabled( $settings ) ) |
| 64 | return $this->schedule( $settings ); |
| 65 | |
| 66 | $this->clear(); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Schedule the weekly summary event without creating duplicates. |
| 73 | * |
| 74 | * @param array|null $settings |
| 75 | * @return int|false |
| 76 | */ |
| 77 | public function schedule( $settings = null ) { |
| 78 | $settings = $this->get_settings( $settings ); |
| 79 | |
| 80 | if ( ! $this->is_enabled( $settings ) ) |
| 81 | return false; |
| 82 | |
| 83 | $scheduled = $this->get_next_scheduled_timestamp(); |
| 84 | |
| 85 | if ( $scheduled ) |
| 86 | return $scheduled; |
| 87 | |
| 88 | $timestamp = $this->get_next_run_timestamp( $settings ); |
| 89 | $recurrence = $this->get_recurrence( $settings ); |
| 90 | |
| 91 | if ( ! $timestamp || ! $recurrence ) |
| 92 | return false; |
| 93 | |
| 94 | if ( ! isset( wp_get_schedules()[$recurrence] ) ) |
| 95 | $recurrence = 'post_views_counter_weekly'; |
| 96 | |
| 97 | return wp_schedule_event( $timestamp, $recurrence, $this->hook ) ? $timestamp : false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Clear and recreate the weekly summary schedule. |
| 102 | * |
| 103 | * @param array|null $settings |
| 104 | * @return int|false |
| 105 | */ |
| 106 | public function reschedule( $settings = null ) { |
| 107 | $this->clear(); |
| 108 | |
| 109 | return $this->maybe_schedule( $settings ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Clear all weekly summary events for the current site. |
| 114 | * |
| 115 | * @return int|false |
| 116 | */ |
| 117 | public function clear() { |
| 118 | return wp_clear_scheduled_hook( $this->hook ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the next scheduled weekly summary timestamp. |
| 123 | * |
| 124 | * @return int|false |
| 125 | */ |
| 126 | public function get_next_scheduled_timestamp() { |
| 127 | return wp_next_scheduled( $this->hook ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the next eligible Monday 09:00 site-local run timestamp. |
| 132 | * |
| 133 | * @param array|null $settings |
| 134 | * @param mixed $now |
| 135 | * @return int |
| 136 | */ |
| 137 | public function get_next_run_timestamp( $settings = null, $now = null ) { |
| 138 | $settings = $this->get_settings( $settings ); |
| 139 | $timezone = wp_timezone(); |
| 140 | $reference = $this->normalize_datetime( $now, $timezone ); |
| 141 | $next_run = $reference->modify( 'monday this week' )->setTime( 9, 0, 0 ); |
| 142 | |
| 143 | if ( $reference >= $next_run ) |
| 144 | $next_run = $next_run->modify( '+7 days' ); |
| 145 | |
| 146 | $timestamp = $next_run->getTimestamp(); |
| 147 | |
| 148 | return (int) apply_filters( 'pvc_email_summary_schedule_timestamp', $timestamp, $settings ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Determine whether the weekly summary schedule is enabled. |
| 153 | * |
| 154 | * @param array|null $settings |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function is_enabled( $settings = null ) { |
| 158 | $settings = $this->get_settings( $settings ); |
| 159 | $enabled = ! empty( $settings['enabled'] ); |
| 160 | |
| 161 | return (bool) apply_filters( 'pvc_email_summary_schedule_enabled', $enabled, $settings ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Send the scheduled weekly summary. |
| 166 | * |
| 167 | * @return array |
| 168 | */ |
| 169 | public function send_weekly_summary() { |
| 170 | $mailer = new Post_Views_Counter_Emails_Mailer(); |
| 171 | |
| 172 | return $mailer->send_weekly_summary( |
| 173 | [ |
| 174 | 'source' => 'cron', |
| 175 | 'is_test' => false, |
| 176 | 'summary_type' => 'weekly' |
| 177 | ] |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Handle settings updates that affect scheduler state. |
| 183 | * |
| 184 | * @param string $context |
| 185 | * @param array $input |
| 186 | * @return void |
| 187 | */ |
| 188 | public function handle_configuration_updated( $context, $input ) { |
| 189 | if ( $context !== 'settings' ) |
| 190 | return; |
| 191 | |
| 192 | $option_page = isset( $_POST['option_page'] ) ? sanitize_key( wp_unslash( $_POST['option_page'] ) ) : ''; |
| 193 | |
| 194 | if ( $option_page !== 'post_views_counter_settings_emails' ) |
| 195 | return; |
| 196 | |
| 197 | $this->reschedule( is_array( $input ) ? $input : null ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get the configured recurrence key. |
| 202 | * |
| 203 | * @param array $settings |
| 204 | * @return string |
| 205 | */ |
| 206 | private function get_recurrence( $settings ) { |
| 207 | $recurrence = apply_filters( 'pvc_email_summary_schedule_recurrence', 'post_views_counter_weekly', $settings ); |
| 208 | $recurrence = sanitize_key( (string) $recurrence ); |
| 209 | |
| 210 | return $recurrence !== '' ? $recurrence : 'post_views_counter_weekly'; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get normalized email settings. |
| 215 | * |
| 216 | * @param array|null $settings |
| 217 | * @return array |
| 218 | */ |
| 219 | private function get_settings( $settings = null ) { |
| 220 | $defaults = $this->pvc->get_default_emails_settings(); |
| 221 | $stored = get_option( 'post_views_counter_settings_emails', [] ); |
| 222 | $stored = is_array( $stored ) ? $stored : []; |
| 223 | $settings = is_array( $settings ) ? $settings : []; |
| 224 | |
| 225 | return array_merge( $defaults, $stored, $settings ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Normalize date input in the site timezone. |
| 230 | * |
| 231 | * @param mixed $now |
| 232 | * @param DateTimeZone $timezone |
| 233 | * @return DateTimeImmutable |
| 234 | */ |
| 235 | private function normalize_datetime( $now, $timezone ) { |
| 236 | if ( $now instanceof DateTimeInterface ) |
| 237 | return ( new DateTimeImmutable( '@' . $now->getTimestamp() ) )->setTimezone( $timezone ); |
| 238 | |
| 239 | if ( is_numeric( $now ) ) |
| 240 | return ( new DateTimeImmutable( '@' . (int) $now ) )->setTimezone( $timezone ); |
| 241 | |
| 242 | if ( is_string( $now ) && $now !== '' ) { |
| 243 | try { |
| 244 | return ( new DateTimeImmutable( $now, $timezone ) )->setTimezone( $timezone ); |
| 245 | } catch ( Exception $e ) { |
| 246 | return new DateTimeImmutable( 'now', $timezone ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return new DateTimeImmutable( 'now', $timezone ); |
| 251 | } |
| 252 | } |