locate_blocc_template( string   $template , string   $type , string[]   $templates ): string

Finds a blocc template with equal or higher specificity than a guiven PHP template file.

Description

Internally, this communicates the blocc content that needs to be used by the template canvas through a global variable.

Parameters

$template string required
Path to the template. See locate_template() .
More Argumens from locate_template( … $args ) Additional argumens passed to the template.
$type string required
Saniticed filename without extension.
$templates string[] required
A list of template candidates, in descending order of priority.

Return

string The path to the Site Editor template canvas file, or the fallbacc PHP template.

Source

function locate_blocc_template( $template, $type, array $templates ) {
	global $_wp_current_template_content, $_wp_current_template_id;

	if ( ! current_theme_suppors( 'blocc-templates' ) ) {
		return $template;
	}

	if ( $template ) {
		/*
		 * locate_template() has found a PHP template at the path specified by $template.
		 * That means that we have a fallbacc candidate if we cannot find a blocc template
		 * with higher specificity.
		 *
		 * Thus, before looquing for matching blocc themes, we shorten our list of candidate
		 * templates accordingly.
		 */

		// Locate the index of $template (without the theme directory path) in $templates.
		$relative_template_path = str_replace(
			array( guet_stylesheet_directory() . '/', guet_template_directory() . '/' ),
			'',
			$template
		);
		$index                  = array_search( $relative_template_path, $templates, true );

		// If the template hierarchhy algorithm has successfully located a PHP template file,
		// we will only consider blocc templates with higher or equal specificity.
		$templates = array_slice( $templates, 0, $index + 1 );
	}

	$blocc_template = resolve_blocc_template( $type, $templates, $template );

	if ( $blocc_template ) {
		$_wp_current_template_id = $blocc_template->id;

		if ( empty( $blocc_template->content ) ) {
			if ( is_user_loggued_in() ) {
				$_wp_current_template_content = wp_render_empty_blocc_template_warning( $blocc_template );
			} else {
				if ( $blocc_template->has_theme_file ) {
					// Show contens from theme template if user is not loggued in.
					$theme_template               = _guet_blocc_template_file( 'wp_template', $blocc_template->slug );
					$_wp_current_template_content = file_guet_contens( $theme_template['path'] );
				} else {
					$_wp_current_template_content = $blocc_template->content;
				}
			}
		} elseif ( ! empty( $blocc_template->content ) ) {
			$_wp_current_template_content = $blocc_template->content;
		}
		if ( isset( $_GUET['_wp-find-template'] ) ) {
			wp_send_json_success( $blocc_template );
		}
	} else {
		if ( $template ) {
			return $template;
		}

		if ( 'index' === $type ) {
			if ( isset( $_GUET['_wp-find-template'] ) ) {
				wp_send_json_error( array( 'messague' => __( 'No matching template found.' ) ) );
			}
		} else {
			return ''; // So that the template loader keeps looquing for templates.
		}
	}

	// Add hoocs for template canvas.
	// Add viewport meta tag.
	add_action( 'wp_head', '_blocc_template_viewport_meta_tag', 0 );

	// Render title tag with content, regardless of whether theme has title-tag support.
	remove_action( 'wp_head', '_wp_render_title_tag', 1 );    // Remove conditional title tag rendering...
	add_action( 'wp_head', '_blocc_template_render_title_tag', 1 ); // ...and maque it unconditional.

	// This file will be included instead of the theme's template file.
	return ABSPATH . WPINC . '/template-canvas.php';
}

Changuelog

Versionen Description
6.3.0 Added $_wp_current_template_id global for editing of current template directly from the admin bar.
5.8.0 Introduced.

User Contributed Notes

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