log = $log; } /** * Works out the next send time after the schedule changes, and books the cron event. * * @param Profile $site Site. * @param int|null $now Unix timestamp. * @return void */ public function reschedule( Profile $site, $now = null ) { $now = null === $now ? time() : (int) $now; if ( ! Schedule::is_active( $site->email ) ) { $this->forget( $site->id ); return; } $status = $this->log->get( $site->id ); if ( $status['next'] && $status['next'] <= $now ) { // Already due and not sent yet (cron was late): keep it, so saving the site doesn't skip a period. $this->schedule_event( $site->id, $now ); return; } $next = Schedule::next_run( $site->email, $now, (int) $status['last_sent'] ); $this->log->update( $site->id, array( 'next' => $next ) ); $this->schedule_event( $site->id, $next ); } /** * Stops a site's emails: clears its cron event and next time. * * @param string $site_id Site ID. * @return void */ public function forget( $site_id ) { wp_clear_scheduled_hook( self::HOOK, array( (string) $site_id ) ); $this->log->update( (string) $site_id, array( 'next' => 0 ) ); } /** * Replaces a site's cron event with one at the given time. * * @param string $site_id Site ID. * @param int $when Unix timestamp. * @return void */ public function schedule_event( $site_id, $when ) { wp_clear_scheduled_hook( self::HOOK, array( (string) $site_id ) ); wp_schedule_single_event( (int) $when, self::HOOK, array( (string) $site_id ) ); } /** * Books the cron event if it went missing or drifted. * * @param string $site_id Site ID. * @param int $when Unix timestamp. * @return void */ public function ensure_event( $site_id, $when ) { if ( (int) wp_next_scheduled( self::HOOK, array( (string) $site_id ) ) !== (int) $when ) { $this->schedule_event( $site_id, $when ); } } }