setup_hooks(); } /** * Set up hooks for various purposes. * * This method adds hooks for different purposes as needed. * * @since 1.0.0 * * @return void */ protected function setup_hooks(): void { $render_block_hook = sprintf( 'render_block_%s', self::BLOCK_NAME ); add_filter( $render_block_hook, array( $this, 'validate_event' ), 10, 2 ); } /** * Validate that the block is connected to a valid event. * * Checks if the block has a valid event ID (either from the current post * or from a postId override). If no valid event is found, returns an empty * string to prevent rendering on the frontend. * * @since 1.0.0 * * @param string $block_content The original block content. * @param array $block The block instance array, used to determine the event. * * @return string The block content if valid event, empty string otherwise. */ public function validate_event( string $block_content, array $block ): string { $block_instance = Block::get_instance(); $post_id = $block_instance->get_post_id( $block ); // Validate that the post ID is an actual event post type. // Only check publish status if not in preview mode. if ( Event::POST_TYPE !== get_post_type( $post_id ) || ( ! is_preview() && 'publish' !== get_post_status( $post_id ) ) ) { return ''; } return $block_content; } }