← Back
Editing: Assets.php
<?php /** * Registers the Relative Sale Dates scripts. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ declare( strict_types=1 ); namespace TEC\Tickets\Relative_Sale_Dates; use DateTimeImmutable; use TEC\Common\Contracts\Provider\Controller as Controller_Contract; use TEC\Common\Editor\Canvas_Styles; use TEC\Common\StellarWP\Assets\Assets as Asset_Registry; use Tribe__Tickets__Main as Tickets_Plugin; use Tribe__Timezones as Timezones; use WP_Screen; /** * Registers the scripts of the sales window options in the classic ticket editor and in the Ticket block, and the * Ticket block's style. * * @since 5.31.0 * * @package TEC\Tickets\Relative_Sale_Dates */ final class Assets extends Controller_Contract { /** * The handle of the classic ticket editor script. * * @since 5.31.0 * * @var string */ public const CLASSIC_SCRIPT = 'tec-tickets-relative-sale-dates-classic'; /** * The handle of the Ticket block script. * * @since 5.31.0 * * @var string */ public const BLOCK_EDITOR_SCRIPT = 'tec-tickets-relative-sale-dates-block-editor'; /** * The handle of the Ticket block style. * * @since 5.31.0 * * @var string */ public const BLOCK_EDITOR_STYLE = 'tec-tickets-relative-sale-dates-block-editor-style'; /** * The manual UTC offsets the WordPress timezone field offers, in hours, as `wp_timezone_choice()` lists them. * * @since 5.31.0 * * @var array<int|float> */ private const MANUAL_OFFSETS = [ -12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14, ]; /** * Unregisters the controller. * * @since 5.31.0 * * @return void */ public function unregister(): void { Asset_Registry::init()->remove( self::CLASSIC_SCRIPT ); Asset_Registry::init()->remove( self::BLOCK_EDITOR_SCRIPT ); Asset_Registry::init()->remove( self::BLOCK_EDITOR_STYLE ); } /** * Registers the controller. * * @since 5.31.0 * @since 5.31.1 Added the Ticket block style to the editor canvas styles group. * * @return void */ protected function do_register(): void { tec_asset( Tickets_Plugin::instance(), self::CLASSIC_SCRIPT, 'RelativeSaleDates/classic.js', // `wp-date` sets up the `moment.tz` the script loads its zone data onto. [ 'jquery', 'moment', 'wp-date', 'event-tickets-admin-js', 'tribe-events-dynamic', 'tec-common-php-date-formatter' ], 'admin_enqueue_scripts', [ 'group_path' => Tickets_Plugin::class . '-packages', 'conditionals' => fn(): bool => $this->is_classic_event_edit_screen(), 'in_footer' => true, 'localize' => [ 'name' => 'tec.tickets.relativeSaleDates.classicData', 'data' => fn(): array => $this->get_classic_script_data(), ], 'translations' => [ 'domain' => 'event-tickets', 'path' => Tickets_Plugin::instance()->plugin_path . 'lang', ], ] ); tec_asset( Tickets_Plugin::instance(), self::BLOCK_EDITOR_SCRIPT, 'RelativeSaleDates/block-editor.js', // `wp-date` sets up the `moment.tz` the script loads current zone data onto. [ 'wp-data', 'wp-date', 'wp-hooks' ], 'enqueue_block_editor_assets', [ 'group_path' => Tickets_Plugin::class . '-packages', 'conditionals' => fn(): bool => $this->is_block_event_edit_screen(), 'localize' => [ 'name' => 'tec.tickets.relativeSaleDates.blockEditorData', 'data' => fn(): array => $this->get_block_editor_script_data(), ], 'translations' => [ 'domain' => 'event-tickets', 'path' => Tickets_Plugin::instance()->plugin_path . 'lang', ], ] ); tec_asset( Tickets_Plugin::instance(), self::BLOCK_EDITOR_STYLE, 'RelativeSaleDates/style-block-editor.css', [], 'enqueue_block_editor_assets', [ 'group_path' => Tickets_Plugin::class . '-packages', 'conditionals' => fn(): bool => $this->is_block_event_edit_screen(), 'groups' => [ Canvas_Styles::GROUP ], ] ); } /** * Returns whether the current admin screen edits an event in the classic editor, where the classic ticket form shows * the sales window options: only for the tickets of a provider they apply to, so only with such a provider on. * * @since 5.31.0 * * @return bool Whether the current screen is the classic event edit screen, with a provider the options apply to on. */ private function is_classic_event_edit_screen(): bool { return $this->is_event_edit_screen() && ! get_current_screen()->is_block_editor() && $this->container->get( Providers::class )->has_active_provider(); } /** * Returns whether the current admin screen edits an event in the block editor, where the Ticket block shows the sales * window options: only for the tickets of a provider they apply to, so only with such a provider on. * * @since 5.31.0 * * @return bool Whether the current screen is the block editor event edit screen, with a provider the options apply * to on. */ private function is_block_event_edit_screen(): bool { return $this->is_event_edit_screen() && get_current_screen()->is_block_editor() && $this->container->get( Providers::class )->has_active_provider(); } /** * Gets the data the classic ticket editor script reads the event dates and writes the helper text with. * * @since 5.31.0 * @since 5.31.2 Added the day the event was published, `postDay`. * * @return array{timeFormat: string, dateWithYear: string, dateNoYear: string, timezones: array<string,string>, allDay: array{start: string, end: string, endDays: int}, text: array{start: string, end: string, windows: array<string,array{endsBeforeStart: string, outsideParent?: string, invalidDate: string, valueOutOfRange: string}>}, errorCodes: array<string,array{endsBeforeStart: string, outsideParent?: string, invalidRule: string, invalidDate: string}>, postDay: string|null} The script data. */ private function get_classic_script_data(): array { return [ 'postDay' => $this->get_post_day(), 'timeFormat' => $this->get_time_format(), 'dateWithYear' => $this->get_date_format( true ), 'dateNoYear' => $this->get_date_format( false ), 'timezones' => $this->get_manual_offset_zones(), 'allDay' => $this->get_all_day_times(), 'text' => [ // Translators: %1$s is the date sales start on, %2$s the time. 'start' => __( 'Sales start %1$s at %2$s', 'event-tickets' ), // Translators: %1$s is the date sales end on, %2$s the time. 'end' => __( 'Sales end %1$s at %2$s', 'event-tickets' ), 'windows' => $this->get_window_error_texts(), ], 'errorCodes' => $this->get_window_error_codes(), ]; } /** * Gets the day the event being edited was published, as the start the server stores for a Now sales start. * * The classic form sends no date for a Now start, and the server stores midnight of that day instead * (`Sales_Window_Effects::fill_saved_ends()`). The form judges the window from the same day. * * @since 5.31.2 * * @return string|null Midnight of the day, `Y-m-d H:i:s` in the event timezone, or `null` when it cannot be read. */ private function get_post_day(): ?string { $post = get_post(); // A post can be saved without a date, which PHP would read as the year -1. $date = $post && '0000-00-00 00:00:00' !== $post->post_date ? date_create_immutable( $post->post_date ) : false; return $date ? $date->format( 'Y-m-d 00:00:00' ) : null; } /** * Gets the text of each error a window of each kind can have, keyed by the kind's ID. * * The errors the server rejects a save with carry the server's own text. * * @since 5.31.0 * * @return array<string,array{endsBeforeStart: string, outsideParent?: string, invalidDate: string, valueOutOfRange: string}> The texts of * each kind, keyed by the error as the editors key it; `outsideParent` only for a kind with a parent window. */ private function get_window_error_texts(): array { $texts = []; foreach ( Window_Kind::all() as $kind ) { $kind_texts = []; foreach ( [ 'endsBeforeStart', 'outsideParent', 'invalidDate' ] as $key ) { $error = $kind->get_error( $key ); if ( $error ) { $kind_texts[ $key ] = $error->get_error_message(); } } $kind_texts['valueOutOfRange'] = sprintf( // Translators: %1$d is the smallest number a relative sale date takes, %2$d the largest. __( 'Enter a number from %1$d to %2$d.', 'event-tickets' ), Boundary::MIN_VALUE, $kind->get_max_value() ); $texts[ $kind->get_id() ] = $kind_texts; } return $texts; } /** * Gets the code of each error the server rejects a save of a window of each kind with, keyed by the kind's ID. * * The classic script tells the window a rejected save was about by its code: the text can be translated or escaped * differently, or be another validator's. * * @since 5.31.0 * * @return array<string,array{endsBeforeStart: string, outsideParent?: string, invalidRule: string, invalidDate: string}> The * codes of each kind, keyed by the error as the editors key it; `outsideParent` only for a kind with a parent * window. */ private function get_window_error_codes(): array { $codes = []; foreach ( Window_Kind::all() as $kind ) { $codes[ $kind->get_id() ] = array_filter( [ 'endsBeforeStart' => $kind->get_error_code( 'endsBeforeStart' ), 'outsideParent' => $kind->get_error_code( 'outsideParent' ), 'invalidRule' => $kind->get_error_code( 'invalidRule' ), 'invalidDate' => $kind->get_error_code( 'invalidDate' ), ] ); } return $codes; } /** * Gets the data the Ticket block script reads the defaults and the error texts of each window kind, the event date * settings and the helper text formats from. * * @since 5.31.0 * * @return array{windowDefaults: array<string,array{start: array{mode: string, value: int, unit: int, anchor?: string}, end: array{mode: string, value: int, unit: int, anchor?: string}}>, windowErrorTexts: array<string,array{endsBeforeStart: string, outsideParent?: string, invalidDate: string, valueOutOfRange: string}>, timezones: array<string,string>, allDay: array{start: string, end: string, endDays: int}, formats: array{dateWithYear: string, dateNoYear: string, time: string}, ticketProviders: array{classes: string[], slugs: string[]}} The script data, with the defaults and the error texts keyed by the kind's ID. */ private function get_block_editor_script_data(): array { $editor = $this->container->get( Editor::class ); $window_defaults = []; // The classic form's, so both editors open a new window on the same boundaries. foreach ( Window_Kind::all() as $kind ) { $window_defaults[ $kind->get_id() ] = $editor->get_form_defaults( $kind ); } return [ 'windowDefaults' => $window_defaults, // The classic script's, so both editors word an error the same way. 'windowErrorTexts' => $this->get_window_error_texts(), 'timezones' => $this->get_manual_offset_zones(), 'allDay' => $this->get_all_day_times(), 'formats' => [ 'dateWithYear' => $this->get_date_format( true ), 'dateNoYear' => $this->get_date_format( false ), 'time' => $this->get_time_format(), ], 'ticketProviders' => $this->get_ticket_providers(), ]; } /** * Gets the active providers the sales window options apply to, as the Ticket block names them. * * The block holds the provider of the tickets block as a class, and that of a ticket fetched from the server as the * slug the block editor tickets REST API gives it. * * @since 5.31.0 * * @return array{classes: string[], slugs: string[]} The provider classes and their REST API slugs. */ private function get_ticket_providers(): array { $classes = array_keys( $this->container->get( Providers::class )->get_active() ); $repository = tribe( 'tickets.rest-v1.repository' ); return [ 'classes' => $classes, 'slugs' => array_map( [ $repository, 'get_provider_slug' ], $classes ), ]; } /** * Gets the zone the server resolves each manual UTC offset of the timezone field to. * * The server does not read a manual offset as a fixed one: it guesses a named zone, which may keep daylight saving * time, so the browser needs the same zone to calculate the same dates. * * @since 5.31.0 * * @return array<string,string> The zone names, keyed by the offset option value. */ private function get_manual_offset_zones(): array { $zones = []; foreach ( self::MANUAL_OFFSETS as $offset ) { // The option value `wp_timezone_choice()` prints, such as `UTC+5.5`, `UTC-0.5` or `UTC+0`. $value = 'UTC' . ( 0 <= $offset ? '+' : '' ) . $offset; $zones[ $value ] = Timezones::build_timezone_object( $value )->getName(); } return $zones; } /** * Gets the times TEC saves for an all-day event, which follow the multi-day cutoff setting. * * @since 5.31.0 * * @return array{start: string, end: string, endDays: int} The start and end times, `H:i:s`, and the days the end * falls after the event's last day. */ private function get_all_day_times(): array { $day = '2000-01-01'; $start = new DateTimeImmutable( tribe_beginning_of_day( $day ) ); $end = new DateTimeImmutable( tribe_end_of_day( $day ) ); return [ 'start' => $start->format( 'H:i:s' ), 'end' => $end->format( 'H:i:s' ), 'endDays' => ( new DateTimeImmutable( $day ) )->diff( $end->setTime( 0, 0 ) )->days, ]; } /** * Gets the TEC date format of a date in another year, or in the current one. * * Both editors read it from here; the classic editor's `tribe_dynamic_help_text` leaves a blank setting blank. * * @since 5.31.0 * * @param bool $with_year Whether the format shows the year. * * @return string The date format, in PHP date format. */ private function get_date_format( bool $with_year ): string { $format = tribe_get_date_format( $with_year ); if ( is_string( $format ) && '' !== $format ) { return $format; } return $with_year ? 'F j, Y' : 'F j'; } /** * Gets the TEC time format, which follows the site one unless the `tribe_time_format` filter changes it. * * @since 5.31.0 * * @return string The time format, in PHP date format. */ private function get_time_format(): string { $format = tribe_get_time_format(); return is_string( $format ) && '' !== $format ? $format : 'g:i a'; } /** * Returns whether the current admin screen edits an event, where the classic ticket form or the Ticket block can show. * * @since 5.31.0 * * @return bool Whether the current screen is the event edit screen. */ private function is_event_edit_screen(): bool { $screen = get_current_screen(); return $screen instanceof WP_Screen && 'post' === $screen->base && 'tribe_events' === $screen->post_type; } }
Save File
Cancel