Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Block_Templates::build_template_result_from_file( string $template_file, string $template_slug = '' )

Build a wp template from file.


Parameters Parameters

$template_file

(string) (Required) Template file path.

$template_slug

(string) (Optional) Template slug.

Default value: ''


Top ↑

Return Return

(WP_Block_Template)


Top ↑

Source Source

File: includes/class-llms-block-templates.php

	 */
	private function build_template_result_from_file( $template_file, $template_slug = '' ) {

		$template_slug = empty( $template_slug ) ? $this->generate_template_slug_from_path( $template_file ) : $template_slug;
		$namespace     = $this->generate_template_namespace_from_path( $template_file );  // Looks like 'lifterlms/lifterlms' or 'lifterlms-groups/lifterlms-groups', etc.
		$template_file = $this->get_maybe_overridden_block_template_file_path( $template_file );

		// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		$template_content = file_get_contents( $template_file );

		// Is the template from the theme/child-theme.
		$theme = false !== strpos( $template_file, get_template_directory() ) ? get_template() : get_stylesheet();
		$theme = false !== strpos( $template_file, get_stylesheet_directory() ) ? $theme : false;

		$template                 = new WP_Block_Template();
		$template->id             = $theme ? $theme . '//' . $template_slug : $namespace . '//' . $template_slug;
		$template->theme          = $theme ? $theme : $namespace;
		$template->content        = function_exists( 'traverse_and_serialize_blocks' ) ?
			traverse_and_serialize_blocks( parse_blocks( $template_content ), '_inject_theme_attribute_in_template_part_block' ) :
			_inject_theme_attribute_in_block_template_content( $template_content );
		$template->source         = $theme ? 'theme' : 'plugin'; // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
		$template->slug           = $template_slug;
		$template->type           = 'wp_template';
		$template->title          = $this->convert_slug_to_title( $template_slug );
		$template->status         = 'publish';
		$template->has_theme_file = true;
		$template->origin         = $theme ? 'theme' : 'plugin';
		$template->is_custom      = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
		$template->post_types     = array(); // Don't appear in any Edit Post template selector dropdown.

Top ↑

Changelog Changelog

Changelog
Version Description
5.9.0 Allow template directory override when the block template comes from an add-on.
5.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.