Automattic\WooCommerce\Blocks

BlockTemplatesController::add_block_templates()publicWC 1.0

Add the block template objects to be used.

Метод класса: BlockTemplatesController{}

Хуков нет.

Возвращает

Массив.

Использование

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->add_block_templates( $query_result, $query, $template_type );
$query_result(массив) (обязательный)
Array of template objects.
$query(массив) (обязательный)
Arguments to retrieve templates.
$template_type(строка) (обязательный)
wp_template or wp_template_part.

Код BlockTemplatesController::add_block_templates() WC 8.7.0

public function add_block_templates( $query_result, $query, $template_type ) {
	if ( ! BlockTemplateUtils::supports_block_templates( $template_type ) ) {
		return $query_result;
	}

	$post_type      = isset( $query['post_type'] ) ? $query['post_type'] : '';
	$slugs          = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
	$template_files = $this->get_block_templates( $slugs, $template_type );
	$theme_slug     = wp_get_theme()->get_stylesheet();

	// @todo: Add apply_filters to _gutenberg_get_template_files() in Gutenberg to prevent duplication of logic.
	foreach ( $template_files as $template_file ) {

		// If we have a template which is eligible for a fallback, we need to explicitly tell Gutenberg that
		// it has a theme file (because it is using the fallback template file). And then `continue` to avoid
		// adding duplicates.
		if ( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $query_result, $template_file ) ) {
			continue;
		}

		// If the current $post_type is set (e.g. on an Edit Post screen), and isn't included in the available post_types
		// on the template file, then lets skip it so that it doesn't get added. This is typically used to hide templates
		// in the template dropdown on the Edit Post page.
		if ( $post_type &&
			isset( $template_file->post_types ) &&
			! in_array( $post_type, $template_file->post_types, true )
		) {
			continue;
		}

		// It would be custom if the template was modified in the editor, so if it's not custom we can load it from
		// the filesystem.
		if ( 'custom' !== $template_file->source ) {
			$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
		} else {
			$query_result[] = $template_file;
			continue;
		}

		$is_not_custom   = false === array_search(
			$theme_slug . '//' . $template_file->slug,
			array_column( $query_result, 'id' ),
			true
		);
		$fits_slug_query =
			! isset( $query['slug__in'] ) || in_array( $template_file->slug, $query['slug__in'], true );
		$fits_area_query =
			! isset( $query['area'] ) || ( property_exists( $template_file, 'area' ) && $template_file->area === $query['area'] );
		$should_include  = $is_not_custom && $fits_slug_query && $fits_area_query;
		if ( $should_include ) {
			$query_result[] = $template;
		}
	}

	// We need to remove theme (i.e. filesystem) templates that have the same slug as a customised one.
	// This only affects saved templates that were saved BEFORE a theme template with the same slug was added.
	$query_result = BlockTemplateUtils::remove_theme_templates_with_custom_alternative( $query_result );

	// There is the chance that the user customized the default template, installed a theme with a custom template
	// and customized that one as well. When that happens, duplicates might appear in the list.
	// See: https://github.com/woocommerce/woocommerce/issues/42220.
	$query_result = BlockTemplateUtils::remove_duplicate_customized_templates( $query_result, $theme_slug );

	/**
	 * WC templates from theme aren't included in `$this->get_block_templates()` but are handled by Gutenberg.
	 * We need to do additional search through all templates file to update title and description for WC
	 * templates that aren't listed in theme.json.
	 */
	$query_result = array_map(
		function( $template ) {
			if ( str_contains( $template->slug, 'single-product' ) ) {
				// We don't want to add the compatibility layer on the Editor Side.
				// The second condition is necessary to not apply the compatibility layer on the REST API. Gutenberg uses the REST API to clone the template.
				// More details: https://github.com/woocommerce/woocommerce-blocks/issues/9662.
				if ( ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {
					// Add the product class to the body. We should move this to a more appropriate place.
					add_filter(
						'body_class',
						function( $classes ) {
							return array_merge( $classes, wc_get_product_class() );
						}
					);

					global $product;

					if ( ! $product instanceof \WC_Product ) {
						$product_id = get_the_ID();
						if ( $product_id ) {
							wc_setup_product_data( $product_id );
						}
					}

					if ( post_password_required() ) {
						$template->content = SingleProductTemplate::add_password_form( $template->content );
					} else {
						$template->content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
					}
				}
			}

			if ( ! BlockTemplateUtils::template_has_title( $template ) ) {
				$template->title = BlockTemplateUtils::get_block_template_title( $template->slug );
			}
			if ( ! $template->description ) {
				$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
			}

			return $template;
		},
		$query_result
	);

	return $query_result;
}