LLMS_Block_Templates::maybe_return_blocks_template( WP_Block_Template|null $template, string $id, array $template_type )

This function checks if there’s a blocks template to return to pre_get_posts short-circuiting the query in Gutenberg.


Description Description

Ultimately it resolves either a saved blocks template from the database or a template file in lifterlms/templates/block-templates/. Without this it won’t be possible to save llms templates customizations in the DB.


Top ↑

Parameters Parameters

$template

(WP_Block_Template|null) (Required) Return a block template object to short-circuit the default query, or null to allow WP to run its normal queries.

$id

(string) (Required) Template unique identifier (example: theme_slug//template_slug).

$template_type

(array) (Required) wp_template or wp_template_part.


Top ↑

Return Return

(mixed|WP_Block_Template|WP_Error)


Top ↑

Source Source

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

	public function maybe_return_blocks_template( $template, $id, $template_type ) {

		// Bail if 'get_block_template' (introduced in WP 5.9.) doesn't exist, or the requested template is not a 'wp_template' type.
		if ( ! function_exists( 'get_block_template' ) || 'wp_template' !== $template_type ) {
			return $template;
		}

		$template_name_parts = explode( '//', $id );
		if ( count( $template_name_parts ) < 2 ) {
			return $template;
		}

		list( , $slug ) = $template_name_parts;

		// Remove the filter at this point because if we don't then this function will infinite loop.
		remove_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );

		// Check if the theme has a saved version of this template before falling back to the llms one.
		$maybe_template = get_block_template( $id, $template_type );

		if ( null !== $maybe_template ) {
			add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
			return $maybe_template;
		}

		// Theme-based template didn't exist, try switching the theme to lifterlms and try again. This function has
		// been unhooked so won't run again.
		add_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 );
		$maybe_template = get_block_template( $id, $template_type );

		// Re-hook this function, it was only unhooked to stop recursion.
		add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
		remove_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 );
		if ( null !== $maybe_template ) {
			return $maybe_template;
		}

		// At this point we haven't had any luck finding a template. Give up and let Gutenberg take control again.
		return $template;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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