Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::build_template_result_from_post()public staticWC 1.0

Build a unified template object based a post Object. Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.

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

Хуков нет.

Возвращает

\WP_Block_Template|\WP_Error. Template.

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

$result = BlockTemplateUtils::build_template_result_from_post( $post );
$post(\WP_Post) (обязательный)
Template post.

Код BlockTemplateUtils::build_template_result_from_post() WC 8.7.0

public static function build_template_result_from_post( $post ) {
	$terms = get_the_terms( $post, 'wp_theme' );

	if ( is_wp_error( $terms ) ) {
		return $terms;
	}

	if ( ! $terms ) {
		return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'woocommerce' ) );
	}

	$theme          = $terms[0]->name;
	$has_theme_file = true;

	$template                 = new \WP_Block_Template();
	$template->wp_id          = $post->ID;
	$template->id             = $theme . '//' . $post->post_name;
	$template->theme          = $theme;
	$template->content        = $post->post_content;
	$template->slug           = $post->post_name;
	$template->source         = 'custom';
	$template->type           = $post->post_type;
	$template->description    = $post->post_excerpt;
	$template->title          = $post->post_title;
	$template->status         = $post->post_status;
	$template->has_theme_file = $has_theme_file;
	$template->is_custom      = false;
	$template->post_types     = array(); // Don't appear in any Edit Post template selector dropdown.

	if ( 'wp_template_part' === $post->post_type ) {
		$type_terms = get_the_terms( $post, 'wp_template_part_area' );
		if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
			$template->area = $type_terms[0]->name;
		}
	}

	// We are checking 'woocommerce' to maintain classic templates which are saved to the DB,
	// prior to updating to use the correct slug.
	// More information found here: https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5423.
	if ( self::PLUGIN_SLUG === $theme || self::DEPRECATED_PLUGIN_SLUG === strtolower( $theme ) ) {
		$template->origin = 'plugin';
	}

	return $template;
}