wpseek.com
A WordPress-centric search engine for devs and theme authors



_build_block_template_result_from_file › WordPress Function

Since5.9.0
Deprecatedn/a
_build_block_template_result_from_file ( $template_file, $template_type )
Access:
  • private
Parameters: (2)
  • (array) $template_file Theme file.
    Required: Yes
  • (string) $template_type Template type. Either 'wp_template' or 'wp_template_part'.
    Required: Yes
Returns:
  • (WP_Block_Template) Template.
Defined at:
Codex:
Change Log:
  • 6.3.0

Builds a unified template object based on a theme file.



Source

function _build_block_template_result_from_file( $template_file, $template_type ) {
	$default_template_types = get_default_block_template_types();
	$theme                  = get_stylesheet();

	$template                 = new WP_Block_Template();
	$template->id             = $theme . '//' . $template_file['slug'];
	$template->theme          = $theme;
	$template->content        = file_get_contents( $template_file['path'] );
	$template->slug           = $template_file['slug'];
	$template->source         = 'theme';
	$template->type           = $template_type;
	$template->title          = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
	$template->status         = 'publish';
	$template->has_theme_file = true;
	$template->is_custom      = true;
	$template->modified       = null;

	if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
		$template->description = $default_template_types[ $template_file['slug'] ]['description'];
		$template->title       = $default_template_types[ $template_file['slug'] ]['title'];
		$template->is_custom   = false;
	}

	if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {
		$template->post_types = $template_file['postTypes'];
	}

	if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) {
		$template->area = $template_file['area'];
	}

	$hooked_blocks        = get_hooked_blocks();
	$has_hooked_blocks    = ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' );
	$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
	$after_block_visitor  = null;

	if ( $has_hooked_blocks ) {
		$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
		$after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
	}

	if ( 'wp_template_part' === $template->type && $has_hooked_blocks ) {
		/*
		 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
		 * we need to wrap its content a mock template part block and traverse it.
		 */
		$content           = get_comment_delimited_block_content(
			'core/template-part',
			array(),
			$template->content
		);
		$content           = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor );
		$template->content = remove_serialized_parent_block( $content );
	} else {
		$template->content = traverse_and_serialize_blocks(
			parse_blocks( $template->content ),
			$before_block_visitor,
			$after_block_visitor
		);
	}

	return $template;
}