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 7.1.0

public function add_block_templates( $query_result, $query, $template_type ) {
	if ( ! BlockTemplateUtils::supports_block_templates() ) {
		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 );

	// @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 {
			$template_file->title       = BlockTemplateUtils::get_block_template_title( $template_file->slug );
			$template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug );
			$query_result[]             = $template_file;
			continue;
		}

		$is_not_custom   = false === array_search(
			wp_get_theme()->get_stylesheet() . '//' . $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'] ) || $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 );

	/**
	 * 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 ( 'theme' === $template->origin ) {
				return $template;
			}
			if ( $template->title === $template->slug ) {
				$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;
}