← Back
Editing: Window_Kind.php
<?php /** * The kinds of window a ticket's relative sale dates describe: its sales window and its sale price window. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ declare( strict_types=1 ); namespace TEC\Tickets\Relative_Sale_Dates; use Closure; use TEC\Tickets\Commerce\Ticket; use WP_Error; /** * An immutable kind of window: the modes, values, units and anchors its rule accepts, where the rule and the dates it * resolves to are submitted and stored, how the window relates to the ticket's own sales, and the errors that reject it. * * The rules, boundaries, resolver, writer, save and validation work the same for every kind; only these differ. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ final class Window_Kind { /** * The ticket's sales window. * * @since 5.31.0 * * @var string */ public const SALES = 'sales'; /** * The ticket's sale price window. * * @since 5.31.0 * * @var string */ public const SALE_PRICE = 'sale_price'; /** * The highest number of days or weeks a relative boundary of the sale price window accepts. * * The sales window takes up to `Boundary::MAX_VALUE`. * * @since 5.31.0 * * @var int */ public const SALE_PRICE_MAX_VALUE = 30; /** * The kinds built so far, keyed by their ID, so each kind has a single instance. * * @since 5.31.0 * * @var array<string,self> */ private static array $instances = []; /** * The kind's ID, one of the class constants. * * @since 5.31.0 * * @var string */ private string $id; /** * The modes each end of the window accepts, keyed by the end, `start` or `end`. * * @since 5.31.0 * * @var array{start: string[], end: string[]} */ private array $modes; /** * The start mode that opens the window at once. * * @since 5.31.0 * * @var string */ private string $open_start_mode; /** * The highest number of units a relative boundary accepts. * * @since 5.31.0 * * @var int */ private int $max_value; /** * The units a relative boundary accepts, as their length in seconds. * * @since 5.31.0 * * @var int[] */ private array $units; /** * The event dates a relative boundary may be counted from, the implied one first. * * @since 5.31.0 * * @var string[] */ private array $anchors; /** * The key the rule is stored under in the shared rules meta, or `null` for its top level. * * @since 5.31.0 * * @var string|null */ private ?string $store_key; /** * The kind whose window this one is judged against, or `null` for none. * * @since 5.31.0 * * @var self|null */ private ?self $parent; /** * The keys that carry the rule: `data`, the ticket data key a save sends it under; `tec_rest`, the TEC REST API * ticket field; and `block_editor_request` and `block_editor_response`, the paths of keys to the rule in the * `ticket` param of a block editor ticket save and in the ticket data the block editor reads. * * @since 5.31.0 * * @var array{data: string, tec_rest: string, block_editor_request: string[], block_editor_response: string[]} */ private array $rule_keys; /** * The ticket metas each end of the window is written to: its date and, for a kind that stores times, its time. * * @since 5.31.0 * * @var array{start: array{date: string, time: ?string}, end: array{date: string, time: ?string}} */ private array $date_metas; /** * The value an open start is written as, or `null` to leave the ticket's own start. * * @since 5.31.0 * * @var string|null */ private ?string $open_start_value; /** * The keys of the sale the window belongs to, or `null` for a window every ticket has: `enabled_meta`, the ticket * meta that turns the sale on. * * @since 5.31.0 * * @var array{enabled_meta: string}|null */ private ?array $sale_keys; /** * Whether the window's dates are the ticket's own sales dates: the ones the ticket is on sale between, which * `ticket_add()` fills in, the sales actions announce and the event start moves. * * @since 5.31.0 * * @var bool */ private bool $owns_ticket_sales_dates; /** * The ticket data fields each end of the window is submitted in: its date and, for a kind that stores times, its time. * * @since 5.31.0 * * @var array{start: array{date: string, time: ?string}, end: array{date: string, time: ?string}} */ private array $submitted_fields; /** * Whether a specific boundary sent without a date leaves the window without one, which the save rejects. * * @since 5.31.0 * * @var bool */ private bool $specific_needs_date; /** * The errors that reject the window, keyed as the editors key them: `invalidRule` for a rule that cannot be read, * such as a relative number out of range, `invalidDate` for a submitted date that cannot be read, `endsBeforeStart` * for a window that ends before it starts, and `outsideParent` for a window that starts outside its parent window. * * Each message is a closure, translated when the error is built: a kind can be built before the text domain loads, * and it lasts the whole request. * * @since 5.31.0 * * @var array<string,array{code: string, message: Closure(): string}> */ private array $errors; /** * Gets the sales window kind. * * @since 5.31.0 * * @return self The sales window kind. */ public static function sales(): self { if ( isset( self::$instances[ self::SALES ] ) ) { return self::$instances[ self::SALES ]; } $modes = [ Rule::MODE_DEFAULT, Rule::MODE_RELATIVE, Rule::MODE_SPECIFIC ]; $kind = new self(); $kind->id = self::SALES; $kind->modes = [ 'start' => $modes, 'end' => $modes, ]; $kind->open_start_mode = Rule::MODE_DEFAULT; $kind->max_value = Boundary::MAX_VALUE; $kind->units = [ MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS, WEEK_IN_SECONDS ]; $kind->anchors = [ Rule::ANCHOR_START, Rule::ANCHOR_END ]; $kind->store_key = null; $kind->parent = null; $kind->rule_keys = [ 'data' => 'relative_sale_dates', 'tec_rest' => 'relative_sale_dates', 'block_editor_request' => [ 'relative_sale_dates' ], 'block_editor_response' => [ 'relative_sale_dates' ], ]; $kind->date_metas = [ 'start' => [ 'date' => Ticket::START_DATE_META_KEY, 'time' => Ticket::START_TIME_META_KEY, ], 'end' => [ 'date' => Ticket::END_DATE_META_KEY, 'time' => Ticket::END_TIME_META_KEY, ], ]; $kind->open_start_value = null; $kind->sale_keys = null; $kind->owns_ticket_sales_dates = true; $kind->submitted_fields = [ 'start' => [ 'date' => 'ticket_start_date', 'time' => 'ticket_start_time', ], 'end' => [ 'date' => 'ticket_end_date', 'time' => 'ticket_end_time', ], ]; $kind->specific_needs_date = true; $kind->errors = [ 'invalidRule' => [ 'code' => 'tec_tickets_relative_sale_dates_invalid_rule', 'message' => static fn(): string => sprintf( // translators: 1) the lowest number a relative sale date takes, 2) the highest. __( 'The relative sales dates cannot be read. Enter a whole number from %1$d to %2$d for each relative date.', 'event-tickets' ), Boundary::MIN_VALUE, Boundary::MAX_VALUE ), ], 'invalidDate' => [ 'code' => 'tec_tickets_relative_sale_dates_invalid_date', 'message' => static fn(): string => __( 'The sales dates cannot be read. Enter a valid date for each specific date.', 'event-tickets' ), ], 'endsBeforeStart' => [ 'code' => 'tec_tickets_relative_sale_dates_invalid_window', 'message' => static fn(): string => __( 'Ticket sales cannot end before they start. Please adjust the sales window.', 'event-tickets' ), ], ]; self::$instances[ self::SALES ] = $kind; return $kind; } /** * Gets the sale price window kind. * * A relative boundary is 1 to 30 days or weeks before the event start, which it always counts from. The window is * stored as whole days, and an empty start means the sale price has started. * * @since 5.31.0 * * @return self The sale price window kind. */ public static function sale_price(): self { if ( isset( self::$instances[ self::SALE_PRICE ] ) ) { return self::$instances[ self::SALE_PRICE ]; } $kind = new self(); $kind->id = self::SALE_PRICE; $kind->modes = [ 'start' => [ Rule::MODE_NOW, Rule::MODE_RELATIVE, Rule::MODE_SPECIFIC ], 'end' => [ Rule::MODE_RELATIVE, Rule::MODE_SPECIFIC ], ]; $kind->open_start_mode = Rule::MODE_NOW; $kind->max_value = self::SALE_PRICE_MAX_VALUE; $kind->units = [ DAY_IN_SECONDS, WEEK_IN_SECONDS ]; $kind->anchors = [ Rule::ANCHOR_START ]; $kind->store_key = 'sale_price'; $kind->parent = self::sales(); $kind->rule_keys = [ 'data' => 'ticket_sale_price_relative', 'tec_rest' => 'sale_price_relative', 'block_editor_request' => [ 'sale_price', 'relative' ], 'block_editor_response' => [ 'sale_price_data', 'relative' ], ]; $kind->date_metas = [ 'start' => [ 'date' => Ticket::$sale_price_start_date_key, 'time' => null, ], 'end' => [ 'date' => Ticket::$sale_price_end_date_key, 'time' => null, ], ]; $kind->open_start_value = ''; $kind->sale_keys = [ 'enabled_meta' => Ticket::$sale_price_checked_key ]; $kind->owns_ticket_sales_dates = false; $kind->submitted_fields = [ 'start' => [ 'date' => 'ticket_sale_start_date', 'time' => null, ], 'end' => [ 'date' => 'ticket_sale_end_date', 'time' => null, ], ]; $kind->specific_needs_date = false; $kind->errors = [ 'invalidRule' => [ 'code' => 'tec_tickets_relative_sale_dates_sale_price_invalid_rule', 'message' => static fn(): string => sprintf( // translators: 1) the lowest number a relative sale price date takes, 2) the highest. __( 'The relative sale price dates cannot be read. Enter a whole number from %1$d to %2$d for each relative date.', 'event-tickets' ), Boundary::MIN_VALUE, self::SALE_PRICE_MAX_VALUE ), ], 'invalidDate' => [ 'code' => 'tec_tickets_relative_sale_dates_sale_price_invalid_date', 'message' => static fn(): string => __( 'The sale price dates cannot be read. Enter a valid date for each specific date.', 'event-tickets' ), ], 'endsBeforeStart' => [ 'code' => 'tec_tickets_relative_sale_dates_sale_price_ends_before_start', 'message' => static fn(): string => __( 'The sale price cannot end before it starts. Please adjust the sale price window.', 'event-tickets' ), ], 'outsideParent' => [ 'code' => 'tec_tickets_relative_sale_dates_sale_price_outside_sales_window', 'message' => static fn(): string => __( 'The sale price window falls outside the ticket sales window. Please adjust the dates.', 'event-tickets' ), ], ]; self::$instances[ self::SALE_PRICE ] = $kind; return $kind; } /** * Gets every kind. * * @since 5.31.0 * * @return self[] The kinds, the sales window first: the sale price is judged against it. */ public static function all(): array { return [ self::sales(), self::sale_price() ]; } /** * Gets the kind's ID. * * @since 5.31.0 * * @return string The ID, one of the class constants. */ public function get_id(): string { return $this->id; } /** * Gets the modes one edge of the window accepts. * * @since 5.31.0 * * @param string $edge The edge of the window, `Boundary::EDGE_START` or `Boundary::EDGE_END`. * * @return string[] The modes, `Rule::MODE_*` constants, or none for an unknown edge. */ public function get_modes( string $edge ): array { return $this->modes[ $edge ] ?? []; } /** * Gets the start mode that opens the window at once. * * @since 5.31.0 * * @return string The mode, `Rule::MODE_DEFAULT` or `Rule::MODE_NOW`. */ public function get_open_start_mode(): string { return $this->open_start_mode; } /** * Gets the highest number of units a relative boundary accepts. * * @since 5.31.0 * * @return int The highest value. */ public function get_max_value(): int { return $this->max_value; } /** * Gets the units a relative boundary accepts. * * @since 5.31.0 * * @return int[] The units, as their length in seconds: `*_IN_SECONDS` constants. */ public function get_units(): array { return $this->units; } /** * Gets the event dates a relative boundary may be counted from. * * @since 5.31.0 * * @return string[] The anchors, `Rule::ANCHOR_*` constants; the first is the one a kind that takes no anchor implies. */ public function get_anchors(): array { return $this->anchors; } /** * Returns whether a relative boundary names its anchor, which it does when it may be counted from more than one. * * @since 5.31.0 * * @return bool Whether the anchor is sent and stored; when it is not, it is implied and must not be sent. */ public function takes_anchor(): bool { return count( $this->anchors ) > 1; } /** * Gets the key the rule is stored under in the shared rules meta. * * @since 5.31.0 * * @return string|null The key, or `null` when the rule is the meta's top level. */ public function get_store_key(): ?string { return $this->store_key; } /** * Gets the kind whose window this one is judged against. * * @since 5.31.0 * * @return self|null The parent kind, or `null` for none. */ public function get_parent(): ?self { return $this->parent; } /** * Returns whether the window's dates are the ticket's own sales dates, the ones the ticket is on sale between. * * Only such a window: * - moves a ticket start later than now to now when its start opens the window at once; * - lets its end follow the event start when the event moves and the rule leaves the end to the ticket; * - has its dates announced by the "sales started" and "sales ended" actions, so a change to them, or to the event * timezone they are written in, reschedules those actions; * - flags an end the rule now leaves to the ticket as set by hand, and clears that flag when the rule is removed by a * save without an end of its own; * - has an empty date filled in the way `ticket_add()` fills it in: the day the event was published for the start, * the event start for the end. * * @since 5.31.0 * * @return bool Whether the window's dates are the ticket's sales dates. */ public function owns_ticket_sales_dates(): bool { return $this->owns_ticket_sales_dates; } /** * Gets the keys that carry the rule. * * @since 5.31.0 * * @return array{data: string, tec_rest: string, block_editor_request: string[], block_editor_response: string[]} The keys: * - `data`, the ticket data key a save sends the rule under, as a JSON string, an array, or `null` or `''` * to remove it; * - `tec_rest`, the TEC REST API ticket field, `relative_sale_dates` or `sale_price_relative`, whose value * is the rule as an object, or `null` to remove it; * - `block_editor_request`, the keys to the rule in the `ticket` param of a block editor ticket save, * outermost first, such as `ticket[sale_price][relative]`; the rule is sent as JSON, or `''` to remove it; * - `block_editor_response`, the keys to the stored rule in the ticket data the block editor reads, such as * `sale_price_data.relative`; the rule is returned as an array, or `null`. */ public function get_rule_keys(): array { return $this->rule_keys; } /** * Gets the ticket metas each end of the window is written to, its date and its time. * * A `null` time means the kind stores whole days. * * @since 5.31.0 * * @return array{start: array{date: string, time: ?string}, end: array{date: string, time: ?string}} The meta keys. */ public function get_date_metas(): array { return $this->date_metas; } /** * Gets the value an open start is written as. * * @since 5.31.0 * * @return string|null The value, or `null` to leave the ticket's own start, which a kind that * `owns_ticket_sales_dates()` may move to now. */ public function get_open_start_value(): ?string { return $this->open_start_value; } /** * Returns whether the window is on for a ticket. * * @since 5.31.0 * * @param int $ticket_id The ticket post ID. * * @return bool Whether the ticket has the window: always for its sales window, and only with a sale price for its * sale price window. */ public function is_enabled_for_ticket( int $ticket_id ): bool { return null === $this->sale_keys || tribe_is_truthy( get_post_meta( $ticket_id, $this->sale_keys['enabled_meta'], true ) ); } /** * Gets the ticket data fields each end of the window is submitted in, its date and its time. * * A `null` time means the kind is submitted as whole days. * * @since 5.31.0 * * @return array{start: array{date: string, time: ?string}, end: array{date: string, time: ?string}} The field keys. */ public function get_submitted_fields(): array { return $this->submitted_fields; } /** * Returns whether a specific boundary sent without a date is rejected. * * Where it is not, as for the sale price, whose dates are stored empty when none is set, an empty start counts as * the day the parent window opens, and an empty end as no end. * * @since 5.31.0 * * @return bool Whether the save rejects a specific boundary without a date. */ public function specific_needs_date(): bool { return $this->specific_needs_date; } /** * Returns whether the window's dates are compared as days, the way they are stored and read, rather than instants. * * @since 5.31.0 * * @return bool Whether the window is judged by the day. */ public function compares_days(): bool { return null === $this->date_metas['start']['time']; } /** * Returns whether a save of the ticket data keeps the window: Tickets Commerce drops a sale price that is unchecked, * negative, or not lower than the price. * * @since 5.31.0 * * @param array<string,mixed> $data The ticket data about to be saved. * * @return bool Whether the ticket will have the window once saved. */ public function is_saved_with( array $data ): bool { return null === $this->sale_keys || Ticket::keeps_sale_price( $data ); } /** * Gets the code of an error that rejects the window, which the editors tell the errors apart by. * * @since 5.31.0 * * @param string $key The error, as the editors key it: `invalidRule`, `invalidDate`, `endsBeforeStart` or * `outsideParent`. * * @return string|null The code, or `null` for an error the kind does not have. */ public function get_error_code( string $key ): ?string { return $this->errors[ $key ]['code'] ?? null; } /** * Gets an error that rejects the window. * * @since 5.31.0 * * @param string $key The error, as the editors key it: `invalidRule`, `invalidDate`, `endsBeforeStart` or * `outsideParent`. * * @return WP_Error|null The error, with a 400 status and the TEC REST API argument for REST responses, or `null` for * one the kind does not have. */ public function get_error( string $key ): ?WP_Error { if ( ! isset( $this->errors[ $key ] ) ) { return null; } // The TEC REST API names the argument the error is about. return new WP_Error( $this->errors[ $key ]['code'], $this->errors[ $key ]['message'](), [ 'status' => 400, 'argument' => $this->rule_keys['tec_rest'], ] ); } /** * Window_Kind constructor. * * Private: each factory builds its kind and sets every property by name. * * @since 5.31.0 */ private function __construct() { } }
Save File
Cancel