← Back
Editing: index.js
/** * Wordpress dependencies */ import { registerBlockType } from '@wordpress/blocks'; const { addFilter, applyFilters, doAction } = wp.hooks; /** * Internal dependencies */ import { initStore } from '../data'; import rsvp from './rsvp'; import rsvpV2 from './rsvp-v2'; import rsvpDisabled from './rsvp-disabled'; import attendees from './attendees'; import { isV2Enabled } from '../data/blocks/rsvp-v2/config'; import { initTicketsBlockFilters } from '../data/blocks/rsvp-v2/tickets-block-filters'; import withBlockWrapper from './with-block-wrapper'; /** * Check if RSVP is disabled via editor config. * * @since 5.30.0 * @return {boolean} Whether RSVP is disabled. */ const isRsvpDisabled = () => Boolean( window.tribe_editor_config?.tickets?.rsvpDisabled ); /** * Filter callback to handle RSVP block based on version state. * * When RSVP is disabled, replaces the block with a disabled placeholder * that shows a migration-in-progress message. * When V2 is enabled, swaps V1 RSVP block with V2. * * @since 5.30.0 * @param {Object[]} blocks The blocks to be registered. * @return {Object[]} The filtered blocks. */ const maybeSwapRsvpBlock = ( blocks ) => { if ( isRsvpDisabled() ) { return blocks.map( ( block ) => ( block.id === 'rsvp' ? rsvpDisabled : block ) ); } if ( ! isV2Enabled() ) { return blocks; } // Replace V1 RSVP block with V2. return blocks.map( ( block ) => ( block.id === 'rsvp' ? rsvpV2 : block ) ); }; // Register the filter to swap RSVP blocks. addFilter( 'tec.tickets.blocks.beforeRegistration', 'tec.tickets.rsvp-v2', maybeSwapRsvpBlock ); // Initialize filters to exclude RSVP V2 tickets from the Tickets block. initTicketsBlockFilters(); /* * Only definitions that are still exactly ours are upgraded: they are dynamic, so their saved markup * is the same at every Block API version. A block a filter callback adds or changes may save static * markup that would lose the classes WordPress adds at version 1, so its version is left to its author. */ const ownBlocks = new Set( [ rsvp, rsvpV2, rsvpDisabled, attendees ] ); /** * Allows filtering the list of blocks registered by Event Tickets. * * Callbacks receive the block definitions as written, before they are adapted to the current * Block API version. Only Event Tickets' own definitions, returned unchanged, are adapted. * * @since 5.8.0 * @since 5.30.2 Event Tickets' own block definitions are adapted to Block API version 3 after this filter. * @param {Object[]} blocks The blocks that will be registered. */ let blocks = applyFilters( 'tec.tickets.blocks.beforeRegistration', [ rsvp, attendees ] ); blocks = blocks.map( ( block ) => ( ownBlocks.has( block ) ? withBlockWrapper( block ) : block ) ); blocks.forEach( ( block ) => registerBlockType( `tribe/${ block.id }`, block ) ); /** * Fires an action after Event Tickets blocks are registered. * * @since 5.8.0 * @param {Object[]} blocks The blocks that were registered. */ doAction( 'tec.tickets.blocks.afterRegistration', blocks ); // Initialize AFTER blocks are registered // to avoid plugin shown as available in reducer // but not having block available for use initStore(); export default blocks;
Save File
Cancel