Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::get_block_templates_from_db()public staticWC 1.0

Gets the templates saved in the database.

Method of the class: BlockTemplateUtils{}

No Hooks.

Return

Int[]|\WP_Post[]. An array of found templates.

Usage

$result = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type );
$slugs(array)
An array of slugs to retrieve templates for.
Default: array()
$template_type(string)
wp_template or wp_template_part.
Default: 'wp_template'

BlockTemplateUtils::get_block_templates_from_db() code WC 9.4.2

public static function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
	$check_query_args = array(
		'post_type'      => $template_type,
		'posts_per_page' => -1,
		'no_found_rows'  => true,
		'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => array( self::DEPRECATED_PLUGIN_SLUG, self::PLUGIN_SLUG, get_stylesheet() ),
			),
		),
	);

	if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
		$check_query_args['post_name__in'] = $slugs;
	}

	$check_query         = new \WP_Query( $check_query_args );
	$saved_woo_templates = $check_query->posts;

	return array_map(
		function ( $saved_woo_template ) {
			return self::build_template_result_from_post( $saved_woo_template );
		},
		$saved_woo_templates
	);
}