← Back
Editing: Sale_Window.php
<?php /** * Resolves a sales or sale price window rule into dates for an event. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ declare( strict_types=1 ); namespace TEC\Tickets\Relative_Sale_Dates; use DateInterval; use DateTimeImmutable; use DateTimeZone; use Exception; use Tribe__Timezones as Timezones; /** * Turns a rule plus an event into the start and end of its window. * * The dates are resolved on the wall clock of the event timezone. A time that happens twice when the clocks go back * takes the occurrence PHP reads it as, the one the stored `Y-m-d H:i:s` strings are read back with and the editors' * JavaScript takes too, so a window is judged at the instants the ticket is sold and its sales actions scheduled at. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ final class Sale_Window { /** * Resolves a rule against the dates of an event, read in the event timezone. * * The resolver works the same for every kind of window; what only the sales window does with its dates, such as * moving a Now start, is `Sales_Window_Effects`'. * * @since 5.31.0 * * @param Rule $rule The rule. * @param int $event_id The event post ID. * * @return Resolved_Window|null The resolved window, or `null` when the event has no valid dates. */ public function resolve_for_event( Rule $rule, int $event_id ): ?Resolved_Window { $event_dates = $this->get_event_dates( $event_id ); return $event_dates ? $this->resolve( $rule, ...$event_dates ) : null; } /** * Gets the event's start and end in the event timezone. * * @since 5.31.0 * * @param int $event_id The event post ID. * * @return array{0: DateTimeImmutable, 1: DateTimeImmutable}|null The event start and end, or `null` when the event has no valid dates. */ public function get_event_dates( int $event_id ): ?array { $start = get_post_meta( $event_id, '_EventStartDate', true ); $end = get_post_meta( $event_id, '_EventEndDate', true ); if ( ! is_string( $start ) || '' === $start || ! is_string( $end ) || '' === $end ) { return null; } $timezone = Timezones::build_timezone_object( get_post_meta( $event_id, '_EventTimezone', true ) ?: null ); try { return [ new DateTimeImmutable( $start, $timezone ), new DateTimeImmutable( $end, $timezone ) ]; } catch ( Exception $e ) { return null; } } /** * Resolves a rule against an event's start and end, both in the event timezone. * * A start that opens the window at once has no date of its own, and a default end closes it when the event starts. * * @since 5.31.0 * * @param Rule $rule The rule. * @param DateTimeImmutable $event_start The event start, in the event's named timezone: only a named timezone, such as `Europe/Athens`, knows its clock changes. * @param DateTimeImmutable $event_end The event end, in the same timezone. * * @return Resolved_Window The resolved window. */ private function resolve( Rule $rule, DateTimeImmutable $event_start, DateTimeImmutable $event_end ): Resolved_Window { $anchors = [ Rule::ANCHOR_START => $event_start, Rule::ANCHOR_END => $event_end, ]; return new Resolved_Window( $this->resolve_boundary( $rule->get_start(), $anchors, null ), $this->resolve_boundary( $rule->get_end(), $anchors, $anchors[ Rule::ANCHOR_START ] ), $event_start->getTimezone() ); } /** * Resolves one boundary of the window. * * @since 5.31.0 * * @param Boundary $boundary The boundary to resolve. * @param array{start: DateTimeImmutable, end: DateTimeImmutable} $anchors The event start and end, in the event timezone. * @param DateTimeImmutable|null $default_date The date a default boundary resolves to. * * @return DateTimeImmutable|null The resolved date in the event timezone, or `null` when the boundary has no date. */ private function resolve_boundary( Boundary $boundary, array $anchors, ?DateTimeImmutable $default_date ): ?DateTimeImmutable { if ( Rule::MODE_DEFAULT === $boundary->get_mode() ) { return $default_date; } $anchor = $boundary->get_anchor(); $interval = $boundary->get_interval(); // Only a relative boundary has both: a specific one keeps the ticket's own date, and a Now start opens the window. if ( null === $anchor || null === $interval ) { return null; } return $this->before( $anchors[ $anchor ], $interval ); } /** * Moves a date back by an interval of wall-clock time, in its own timezone. * * The interval comes off the wall-clock time, not off the instant, so a clock change in between does not shift the * result: 8 hours before 08:00 is 00:00 even on a night an hour longer or shorter than usual. * * A number of minutes or hours that lands on a time skipped by a clock change comes off the instant instead, as the * next valid time can fall on or after the anchor: 30 minutes before 03:15 would otherwise be 03:45. * * A time that happens twice takes the occurrence PHP reads it as, the one the ticket is read back with: an earlier * one would let a window that reads as ending before it starts pass as valid. * * shortcut: two boundaries of one rule can then land on the same instant, as 2 hours and 1 hour before 03:30 EDT * both give 01:30 EST on 14 March 2027 in New York, and the save rejects that window as ending when it starts; move * the boundary off the instant only when it would not keep its order against the other one, if admins hit it. * * @since 5.31.0 * * @param DateTimeImmutable $anchor The date to move back. * @param DateInterval $interval How far to move it back. * * @return DateTimeImmutable The moved date, in the anchor's timezone. */ private function before( DateTimeImmutable $anchor, DateInterval $interval ): DateTimeImmutable { // UTC has no clock change, so the interval moves the wall-clock time and nothing else. $wall_clock = ( new DateTimeImmutable( $anchor->format( 'Y-m-d H:i:s' ), new DateTimeZone( 'UTC' ) ) ) ->sub( $interval ) ->format( 'Y-m-d H:i:s' ); $date = new DateTimeImmutable( $wall_clock, $anchor->getTimezone() ); // Days and weeks keep their wall-clock time: a skipped time a day or more back still lands well before the anchor. if ( $interval->d || $date->format( 'Y-m-d H:i:s' ) === $wall_clock ) { return $date; } $seconds = ( new DateTimeImmutable( '@0' ) )->add( $interval )->getTimestamp(); return ( new DateTimeImmutable( '@' . ( $anchor->getTimestamp() - $seconds ) ) )->setTimezone( $anchor->getTimezone() ); } }
Save File
Cancel