}, ] ); } } foreach ( (array) tribe_get_option( 'ticket-enabled-post-types', [] ) as $ticketable_type ) { foreach ( [ Meta::META_KEY_ENABLED, Meta::META_KEY_LAYOUT_ID ] as $meta_key ) { register_post_meta( $ticketable_type, $meta_key, [ 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'auth_callback' => function () { return current_user_can( 'edit_posts' ); }, ] ); } } } /** * Registers the controller by subscribing to WordPress hooks and binding implementations. * * @since 5.16.0 * * @return void */ protected function do_register(): void { $this->register_block_editor_assets(); add_action( 'init', [ $this, 'register_meta' ], 1000 ); add_action( 'tribe_tickets_ticket_added', [ $this, 'save_ticket_seat_type' ], 10, 3 ); // Priority is important to be above 10! add_filter( 'tribe_rest_single_ticket_data', [ $this, 'filter_seating_totals' ], 20, 2 ); } /** * Filters the seating totals during a rest request. * * @since 5.16.0 * * @param array $data The data to be shared with the block editor. * @param WP_REST_Request $request The block editor's request. * * @return array The filtered totals. */ public function filter_seating_totals( array $data, WP_REST_Request $request ): array { $ticket_id = $request['id'] ?? false; if ( ! $ticket_id ) { return $data; } $ticket_object = Tribe_Tickets::load_ticket_object( $ticket_id ); $event_id = get_post_meta( $ticket_id, Ticket::$event_relation_meta_key, true ); if ( ! ( $ticket_object && $event_id && tec_tickets_seating_enabled( $event_id ) ) ) { return $data; } $data['totals'] = array_merge( $data['totals'], [ 'stock' => $ticket_object->stock(), ] ); return $data; } /** * Registers the Block Editor JavaScript and CSS assets. * * @since 5.16.0 * * @return void */ private function register_block_editor_assets(): void { Asset::add( 'tec-tickets-seating-block-editor', 'blockEditor.js', Tickets::VERSION ) ->add_to_group_path( 'tec-seating' ) ->set_condition( [ $this, 'should_enqueue_assets' ] ) ->set_dependencies( 'wp-hooks', 'react', 'react-dom', 'tec-tickets-seating-utils', 'tec-tickets-seating-ajax', 'tribe-common-gutenberg-vendor' ) ->enqueue_on( 'enqueue_block_editor_assets' ) ->add_localize_script( 'tec.tickets.seating.blockEditorData', [ $this, 'get_store_data' ] ) ->add_localize_script( 'tec.tickets.seating.meta', fn() => [ 'META_KEY_ENABLED' => Meta::META_KEY_ENABLED, 'META_KEY_LAYOUT_ID' => Meta::META_KEY_LAYOUT_ID, 'META_KEY_SEAT_TYPE' => Meta::META_KEY_SEAT_TYPE, ] ) ->add_to_group( 'tec-tickets-seating-editor' ) ->add_to_group( 'tec-tickets-seating' ) ->register(); Asset::add( 'tec-tickets-seating-block-editor-style', 'style-blockEditor.css', Tickets::VERSION ) ->add_to_group_path( 'tec-seating' ) ->set_condition( [ $this, 'should_enqueue_assets' ] ) ->enqueue_on( 'enqueue_block_editor_assets' ) ->add_to_group( 'tec-tickets-seating-editor' ) ->add_to_group( 'tec-tickets-seating' ) ->register(); } /** * Checks if the current context is the Block Editor and the post type is ticket-enabled. * * @since 5.17.0 * * @return bool Whether the assets should be enqueued or not. */ public function should_enqueue_assets() { $ticketable_post_types = (array) tribe_get_option( 'ticket-enabled-post-types', [] ); if ( empty( $ticketable_post_types ) ) { return false; } $post = get_post(); if ( ! $post instanceof WP_Post ) { return false; } return is_admin() && in_array( $post->post_type, $ticketable_post_types, true ); } /** * Saves the seating details of a ticket from the POST or PUT request data sent to the REST API. * * @since 5.16.0 * * @param int $post_id The ID of the post the ticket is attached to. * @param int $ticket_id The ID of the ticket. * @param array $body The body of the request. * * @return void The seating details are saved. */ public function save_ticket_seat_type( $post_id, $ticket_id, $body ) { if ( ! isset( $body['tribe-ticket'], $body['tribe-ticket']['seating'], $body['tribe-ticket']['seating']['enabled'], $body['tribe-ticket']['seating']['seatType'], $body['tribe-ticket']['seating']['layoutId'] ) ) { return; } $enabled = (bool) $body['tribe-ticket']['seating']['enabled']; $seat_type = (string) $body['tribe-ticket']['seating']['seatType']; $layout_id = (string) $body['tribe-ticket']['seating']['layoutId']; update_post_meta( $ticket_id, Meta::META_KEY_ENABLED, $enabled ); update_post_meta( $post_id, Meta::META_KEY_ENABLED, $enabled ); if ( $layout_id ) { update_post_meta( $post_id, Meta::META_KEY_LAYOUT_ID, $layout_id ); } else { delete_post_meta( $post_id, Meta::META_KEY_LAYOUT_ID ); } if ( $seat_type ) { update_post_meta( $ticket_id, Meta::META_KEY_SEAT_TYPE, $seat_type ); } else { delete_post_meta( $ticket_id, Meta::META_KEY_SEAT_TYPE ); } } } }, ] ); } } foreach ( (array) tribe_get_option( 'ticket-enabled-post-types', [] ) as $ticketable_type ) { foreach ( [ Meta::META_KEY_ENABLED, Meta::META_KEY_LAYOUT_ID ] as $meta_key ) { register_post_meta( $ticketable_type, $meta_key, [ 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'auth_callback' => function () { return current_user_can( 'edit_posts' ); }, ] ); } } } /** * Registers the controller by subscribing to WordPress hooks and binding implementations. * * @since 5.16.0 * * @return void */ protected function do_register(): void { $this->register_block_editor_assets(); add_action( 'init', [ $this, 'register_meta' ], 1000 ); add_action( 'tribe_tickets_ticket_added', [ $this, 'save_ticket_seat_type' ], 10, 3 ); // Priority is important to be above 10! add_filter( 'tribe_rest_single_ticket_data', [ $this, 'filter_seating_totals' ], 20, 2 ); } /** * Filters the seating totals during a rest request. * * @since 5.16.0 * * @param array $data The data to be shared with the block editor. * @param WP_REST_Request $request The block editor's request. * * @return array The filtered totals. */ public function filter_seating_totals( array $data, WP_REST_Request $request ): array { $ticket_id = $request['id'] ?? false; if ( ! $ticket_id ) { return $data; } $ticket_object = Tribe_Tickets::load_ticket_object( $ticket_id ); $event_id = get_post_meta( $ticket_id, Ticket::$event_relation_meta_key, true ); if ( ! ( $ticket_object && $event_id && tec_tickets_seating_enabled( $event_id ) ) ) { return $data; } $data['totals'] = array_merge( $data['totals'], [ 'stock' => $ticket_object->stock(), ] ); return $data; } /** * Registers the Block Editor JavaScript and CSS assets. * * @since 5.16.0 * * @return void */ private function register_block_editor_assets(): void { Asset::add( 'tec-tickets-seating-block-editor', 'blockEditor.js', Tickets::VERSION ) ->add_to_group_path( 'tec-seating' ) ->set_condition( [ $this, 'should_enqueue_assets' ] ) ->set_dependencies( 'wp-hooks', 'react', 'react-dom', 'tec-tickets-seating-utils', 'tec-tickets-seating-ajax', 'tribe-common-gutenberg-vendor' ) ->enqueue_on( 'enqueue_block_editor_assets' ) ->add_localize_script( 'tec.tickets.seating.blockEditorData', [ $this, 'get_store_data' ] ) ->add_localize_script( 'tec.tickets.seating.meta', fn() => [ 'META_KEY_ENABLED' => Meta::META_KEY_ENABLED, 'META_KEY_LAYOUT_ID' => Meta::META_KEY_LAYOUT_ID, 'META_KEY_SEAT_TYPE' => Meta::META_KEY_SEAT_TYPE, ] ) ->add_to_group( 'tec-tickets-seating-editor' ) ->add_to_group( 'tec-tickets-seating' ) ->register(); Asset::add( 'tec-tickets-seating-block-editor-style', 'style-blockEditor.css', Tickets::VERSION ) ->add_to_group_path( 'tec-seating' ) ->set_condition( [ $this, 'should_enqueue_assets' ] ) ->enqueue_on( 'enqueue_block_editor_assets' ) ->add_to_group( 'tec-tickets-seating-editor' ) ->add_to_group( 'tec-tickets-seating' ) ->register(); } /** * Checks if the current context is the Block Editor and the post type is ticket-enabled. * * @since 5.17.0 * * @return bool Whether the assets should be enqueued or not. */ public function should_enqueue_assets() { $ticketable_post_types = (array) tribe_get_option( 'ticket-enabled-post-types', [] ); if ( empty( $ticketable_post_types ) ) { return false; } $post = get_post(); if ( ! $post instanceof WP_Post ) { return false; } return is_admin() && in_array( $post->post_type, $ticketable_post_types, true ); } /** * Saves the seating details of a ticket from the POST or PUT request data sent to the REST API. * * @since 5.16.0 * * @param int $post_id The ID of the post the ticket is attached to. * @param int $ticket_id The ID of the ticket. * @param array $body The body of the request. * * @return void The seating details are saved. */ public function save_ticket_seat_type( $post_id, $ticket_id, $body ) { if ( ! isset( $body['tribe-ticket'], $body['tribe-ticket']['seating'], $body['tribe-ticket']['seating']['enabled'], $body['tribe-ticket']['seating']['seatType'], $body['tribe-ticket']['seating']['layoutId'] ) ) { return; } $enabled = (bool) $body['tribe-ticket']['seating']['enabled']; $seat_type = (string) $body['tribe-ticket']['seating']['seatType']; $layout_id = (string) $body['tribe-ticket']['seating']['layoutId']; update_post_meta( $ticket_id, Meta::META_KEY_ENABLED, $enabled ); update_post_meta( $post_id, Meta::META_KEY_ENABLED, $enabled ); if ( $layout_id ) { update_post_meta( $post_id, Meta::META_KEY_LAYOUT_ID, $layout_id ); } else { delete_post_meta( $post_id, Meta::META_KEY_LAYOUT_ID ); } if ( $seat_type ) { update_post_meta( $ticket_id, Meta::META_KEY_SEAT_TYPE, $seat_type ); } else { delete_post_meta( $ticket_id, Meta::META_KEY_SEAT_TYPE ); } } }