Automattic\WooCommerce\Blocks

BlockTemplatesController::add_db_templates_with_woo_slugpublicWC 1.0

Add the block template objects currently saved in the database with the WooCommerce slug. That is, templates that have been customised before WooCommerce started to use the Template Registration API.

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

Хуков нет.

Возвращает

Массив.

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

$BlockTemplatesController = new BlockTemplatesController();
$BlockTemplatesController->add_db_templates_with_woo_slug( $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_db_templates_with_woo_slug() WC 10.3.6

public function add_db_templates_with_woo_slug( $query_result, $query, $template_type ) {
	$slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array();

	if ( ! BlockTemplateUtils::supports_block_templates( $template_type ) && ! in_array( ComingSoonTemplate::SLUG, $slugs, true ) ) {
		return $query_result;
	}

	// For templates, we only need to load templates from the database. For
	// template parts, we also need to load them from the filesystem, as
	// there is no Template registration API for template parts.
	$template_files = 'wp_template' === $template_type ? BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ) : $this->get_block_templates( $slugs, $template_type );
	$new_templates  = array();

	foreach ( $template_files as $template_file ) {
		// 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 ) {
			if (
				BlockTemplateUtils::PLUGIN_SLUG === $template_file->theme ||
				BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === $template_file->theme
			) {
				array_unshift( $new_templates, $template_file );
			}
			continue;
		}

		// We only need to build templates from the filesystem for template parts.
		// Regular templates are handled by the Template registration API.
		if ( 'wp_template_part' === $template_type ) {
			$theme_slug            = get_stylesheet();
			$possible_template_ids = [
				$theme_slug . '//' . $template_file->slug,
				$theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/' . $template_file->slug,
				$theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'] . '/' . $template_file->slug,
			];

			$is_custom                 = false;
			$query_result_template_ids = array_column( $query_result, 'id' );

			foreach ( $possible_template_ids as $template_id ) {
				if ( in_array( $template_id, $query_result_template_ids, true ) ) {
					$is_custom = true;
					break;
				}
			}
			$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'] );
			$is_from_filesystem = isset( $template_file->path );
			$should_include     = ! $is_custom && $fits_slug_query && $fits_area_query && $is_from_filesystem;

			if ( $should_include ) {
				$template       = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
				$query_result[] = $template;
			}
		}
	}

	$query_result = array_merge( $new_templates, $query_result );

	if ( count( $new_templates ) > 0 ) {
		// If there are certain templates that have been customised with the `woocommerce/woocommerce` slug,
		// We prioritize them over the theme and WC templates. That is, we remove the theme and WC templates
		// from the results and only keep the customised ones.
		$query_result = BlockTemplateUtils::remove_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 );
	}

	/**
	 * 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 ) use ( $template_type ) {
			return BlockTemplateUtils::update_template_data( $template, $template_type );
		},
		$query_result
	);

	return $query_result;
}