le name that is parsed to match our block template. */ public function filter_single_template_hierarchy( $templates ) { if ( empty( $templates ) ) { return $templates; } if ( ! is_array( $templates ) ) { return $templates; } // Is it our post type? $index = array_search( 'single-tribe_events.php', $templates, true ); if ( is_int( $index ) ) { // Switch to our faux template which maps to our slug. $templates[ $index ] = 'single-event.php'; } return $templates; } /** * Adds the archive template to the array of block templates. * * @since 5.14.2 * @since 6.2.7 Added support for single event templates. * @since 6.14.0 Passing $query_result to get_filtered_block_templates(). * * @param WP_Block_Template[] $query_result Array of found block templates. * @param array $query { * Optional. Arguments to retrieve templates. * @param string $template_type The type of template being requested. * * @type array $slug__in List of slugs to include. * @type int $wp_id Post ID of customized template. * } * * @return array The modified $query. */ public function filter_include_templates( $query_result, $query, $template_type ) { if ( ! is_array( $query_result ) ) { return $query_result; } // Get our block template services for this query. $template_services = $this->get_filtered_block_templates( $template_type, $query_result ); foreach ( $template_services as $template ) { if ( empty( $query['slug__in'] ) || in_array( $template->slug(), $query['slug__in'], true ) ) { /** * @var WP_Block_Template $wp_template */ $wp_template = $template->get_block_template(); if ( $wp_template ) { $query_result[] = $wp_template; } } } return $query_result; } /** * Fetch our Block Template by ID. * * @since 6.2.7 * * @param null|WP_Block_Template $block_template The filtered template. * @param string $id The block template ID. * @param string $template_type The template type. * * @return null|WP_Block_Template */ public function filter_include_template_by_id( $block_template, $id, $template_type ) { if ( ! is_null( $block_template ) ) { return $block_template; } $template_services = $this->get_filtered_block_templates( $template_type ); foreach ( $template_services as $template ) { if ( $id === $template->id() ) { return $template->get_block_template(); } } return $block_template; } /** * Filters and returns the available Event Block Template Services, used to locate * WP_Block_Template instances. * * @since 6.2.7 * @since 6.14.0 Added $query_result parameter. * * @param string $template_type The type of templates we are fetching. * @param WP_Block_Template[] $query_result The query result. * * @return Block_Template_Contract[] List of filtered Event Calendar templates. */ public function get_filtered_block_templates( $template_type = 'wp_template', $query_result = [] ): array { $templates = []; if ( $template_type === 'wp_template' ) { $theme_has_single_event_template = false; $theme_has_archive_event_template = false; foreach ( $query_result as $template ) { if ( 'theme' !== $template->origin && 'theme' !== $template->source ) { continue; } if ( 'single-event' === $template->slug ) { $theme_has_single_event_template = true; } if ( 'archive-events' === $template->slug ) { $theme_has_archive_event_template = true; } } /** * Filter whether the event archive block template should be used. * * @since 6.4.0 * * @param bool $allow_archive Whether the event archive block template should be used. */ $allow_archive = apply_filters( 'tec_events_allow_archive_block_template', ! $theme_has_archive_event_template ); if ( $allow_archive ) { $templates[] = tribe( Archive_Block_Template::class ); } /** * Filter whether the event single block template should be used. * * @since 6.4.0 * * @param bool $allow_single Whether the single block template should be used. */ $allow_single = apply_filters( 'tec_events_allow_single_block_template', ! $theme_has_single_event_template ); if ( $allow_single ) { $templates[] = tribe( Single_Block_Template::class ); } return $templates; } /** * Filter our available Full Site Block Template objects available. These are used in to define and store WP_Block_Template instances. * * @since 6.2.7 * * @param Block_Template_Contract[] $templates The list of our Block_Template_Contracts to be used to register and generate WP_Block_Template. * @param string $template_type The type of template being requested. */ return apply_filters( 'tec_events_get_full_site_block_template_services', $templates, $template_type ); } /** * If we're using a FSE theme, we always use the full styling. * * @since 5.14.2 * * @param string $value The value of the option. * * @return string $value The original value, or an empty string if FSE is active. */ public function filter_events_template_setting_option( $value ) { return tec_is_full_site_editor() ? '' : $value; } /** * Override the get_single_option to return the default event template when FSE is active. * * @since 5.14.2 * * @param mixed $option Results of option query. * @param string $default The default value. * @param string $option_name Name of the option. * * @return mixed results of option query. */ public function filter_tribe_get_single_option( $option, $default, $option_name ) { if ( 'tribeEventsTemplate' !== $option_name ) { return $option; } if ( tec_is_full_site_editor() ) { return ''; } return $option; } /** * Overwrite the template option on save if FSE is active. * We only support the default events template for now. * * @since 5.14.2 * * @param array $options The array of values to save. In the format option key => value. * @param string $option_id The main option ID. * * @return array $options The array of values to save. In the format option key => value. */ public function filter_tribe_save_template_option( $options, $option_id ) { if ( tec_is_full_site_editor() ) { $options['tribeEventsTemplate'] = ''; } return $options; } } le name that is parsed to match our block template. */ public function filter_single_template_hierarchy( $templates ) { if ( empty( $templates ) ) { return $templates; } if ( ! is_array( $templates ) ) { return $templates; } // Is it our post type? $index = array_search( 'single-tribe_events.php', $templates, true ); if ( is_int( $index ) ) { // Switch to our faux template which maps to our slug. $templates[ $index ] = 'single-event.php'; } return $templates; } /** * Adds the archive template to the array of block templates. * * @since 5.14.2 * @since 6.2.7 Added support for single event templates. * @since 6.14.0 Passing $query_result to get_filtered_block_templates(). * * @param WP_Block_Template[] $query_result Array of found block templates. * @param array $query { * Optional. Arguments to retrieve templates. * @param string $template_type The type of template being requested. * * @type array $slug__in List of slugs to include. * @type int $wp_id Post ID of customized template. * } * * @return array The modified $query. */ public function filter_include_templates( $query_result, $query, $template_type ) { if ( ! is_array( $query_result ) ) { return $query_result; } // Get our block template services for this query. $template_services = $this->get_filtered_block_templates( $template_type, $query_result ); foreach ( $template_services as $template ) { if ( empty( $query['slug__in'] ) || in_array( $template->slug(), $query['slug__in'], true ) ) { /** * @var WP_Block_Template $wp_template */ $wp_template = $template->get_block_template(); if ( $wp_template ) { $query_result[] = $wp_template; } } } return $query_result; } /** * Fetch our Block Template by ID. * * @since 6.2.7 * * @param null|WP_Block_Template $block_template The filtered template. * @param string $id The block template ID. * @param string $template_type The template type. * * @return null|WP_Block_Template */ public function filter_include_template_by_id( $block_template, $id, $template_type ) { if ( ! is_null( $block_template ) ) { return $block_template; } $template_services = $this->get_filtered_block_templates( $template_type ); foreach ( $template_services as $template ) { if ( $id === $template->id() ) { return $template->get_block_template(); } } return $block_template; } /** * Filters and returns the available Event Block Template Services, used to locate * WP_Block_Template instances. * * @since 6.2.7 * @since 6.14.0 Added $query_result parameter. * * @param string $template_type The type of templates we are fetching. * @param WP_Block_Template[] $query_result The query result. * * @return Block_Template_Contract[] List of filtered Event Calendar templates. */ public function get_filtered_block_templates( $template_type = 'wp_template', $query_result = [] ): array { $templates = []; if ( $template_type === 'wp_template' ) { $theme_has_single_event_template = false; $theme_has_archive_event_template = false; foreach ( $query_result as $template ) { if ( 'theme' !== $template->origin && 'theme' !== $template->source ) { continue; } if ( 'single-event' === $template->slug ) { $theme_has_single_event_template = true; } if ( 'archive-events' === $template->slug ) { $theme_has_archive_event_template = true; } } /** * Filter whether the event archive block template should be used. * * @since 6.4.0 * * @param bool $allow_archive Whether the event archive block template should be used. */ $allow_archive = apply_filters( 'tec_events_allow_archive_block_template', ! $theme_has_archive_event_template ); if ( $allow_archive ) { $templates[] = tribe( Archive_Block_Template::class ); } /** * Filter whether the event single block template should be used. * * @since 6.4.0 * * @param bool $allow_single Whether the single block template should be used. */ $allow_single = apply_filters( 'tec_events_allow_single_block_template', ! $theme_has_single_event_template ); if ( $allow_single ) { $templates[] = tribe( Single_Block_Template::class ); } return $templates; } /** * Filter our available Full Site Block Template objects available. These are used in to define and store WP_Block_Template instances. * * @since 6.2.7 * * @param Block_Template_Contract[] $templates The list of our Block_Template_Contracts to be used to register and generate WP_Block_Template. * @param string $template_type The type of template being requested. */ return apply_filters( 'tec_events_get_full_site_block_template_services', $templates, $template_type ); } /** * If we're using a FSE theme, we always use the full styling. * * @since 5.14.2 * * @param string $value The value of the option. * * @return string $value The original value, or an empty string if FSE is active. */ public function filter_events_template_setting_option( $value ) { return tec_is_full_site_editor() ? '' : $value; } /** * Override the get_single_option to return the default event template when FSE is active. * * @since 5.14.2 * * @param mixed $option Results of option query. * @param string $default The default value. * @param string $option_name Name of the option. * * @return mixed results of option query. */ public function filter_tribe_get_single_option( $option, $default, $option_name ) { if ( 'tribeEventsTemplate' !== $option_name ) { return $option; } if ( tec_is_full_site_editor() ) { return ''; } return $option; } /** * Overwrite the template option on save if FSE is active. * We only support the default events template for now. * * @since 5.14.2 * * @param array $options The array of values to save. In the format option key => value. * @param string $option_id The main option ID. * * @return array $options The array of values to save. In the format option key => value. */ public function filter_tribe_save_template_option( $options, $option_id ) { if ( tec_is_full_site_editor() ) { $options['tribeEventsTemplate'] = ''; } return $options; } }