PluginProbe
Gutenberg / 9.5.1
Gutenberg v9.5.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / template-loader.php

template-loader.php in Gutenberg 9.5.1, at lib/template-loader.php

245 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block template loader functions.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Adds necessary filters to use 'wp_template' posts instead of theme template files.
10 */
11 function gutenberg_add_template_loader_filters() {
12 if ( ! gutenberg_is_fse_theme() ) {
13 return;
14 }
15
16 foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
17 if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular template type.
18 continue;
19 }
20 add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
21 }
22 }
23 add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
24
25 /**
26 * Get the template hierarchy for a given template type.
27 *
28 * Internally, this filters into the "{$type}_template_hierarchy" hook to record the type-specific template hierarchy.
29 *
30 * @param string $template_type A template type.
31 * @return string[] A list of template candidates, in descending order of priority.
32 */
33 function get_template_hierarchy( $template_type ) {
34 if ( ! in_array( $template_type, gutenberg_get_template_type_slugs(), true ) ) {
35 return array();
36 }
37
38 $get_template_function = 'get_' . str_replace( '-', '_', $template_type ) . '_template'; // front-page -> get_front_page_template.
39 $template_hierarchy_filter = str_replace( '-', '', $template_type ) . '_template_hierarchy'; // front-page -> frontpage_template_hierarchy.
40
41 $result = array();
42 $template_hierarchy_filter_function = function( $templates ) use ( &$result ) {
43 $result = $templates;
44 return $templates;
45 };
46
47 add_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20, 1 );
48 call_user_func( $get_template_function ); // This invokes template_hierarchy_filter.
49 remove_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20 );
50
51 return $result;
52 }
53
54 /**
55 * Filters into the "{$type}_template" hooks to redirect them to the Full Site Editing template canvas.
56 *
57 * Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
58 *
59 * @param string $template Path to the template. See locate_template().
60 * @param string $type Sanitized filename without extension.
61 * @param array $templates A list of template candidates, in descending order of priority.
62 * @return string The path to the Full Site Editing template canvas file.
63 */
64 function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
65 global $_wp_current_template_content;
66
67 $current_template = gutenberg_resolve_template( $type, $templates );
68
69 if ( $current_template ) {
70 $_wp_current_template_content = empty( $current_template->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template->post_content;
71
72 if ( isset( $_GET['_wp-find-template'] ) ) {
73 wp_send_json_success( $current_template );
74 }
75 } else {
76 if ( 'index' === $type ) {
77 if ( isset( $_GET['_wp-find-template'] ) ) {
78 wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
79 }
80 } else {
81 return false; // So that the template loader keeps looking for templates.
82 }
83 }
84
85 // Add hooks for template canvas.
86 // Add viewport meta tag.
87 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
88
89 // Render title tag with content, regardless of whether theme has title-tag support.
90 remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
91 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional.
92
93 // This file will be included instead of the theme's template file.
94 return gutenberg_dir_path() . 'lib/template-canvas.php';
95 }
96
97 /**
98 * Return the correct 'wp_template' to render fot the request template type.
99 *
100 * Accepts an optional $template_hierarchy argument as a hint.
101 *
102 * @param string $template_type The current template type.
103 * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
104 * @return null|array {
105 * @type WP_Post|null template_post A template post object, or null if none could be found.
106 * @type int[] A list of template parts IDs for the template.
107 * }
108 */
109 function gutenberg_resolve_template( $template_type, $template_hierarchy = array() ) {
110 if ( ! $template_type ) {
111 return null;
112 }
113
114 if ( empty( $template_hierarchy ) ) {
115 if ( 'index' === $template_type ) {
116 $template_hierarchy = get_template_hierarchy( 'index' );
117 } else {
118 $template_hierarchy = array_merge( get_template_hierarchy( $template_type ), get_template_hierarchy( 'index' ) );
119 }
120 }
121
122 $slugs = array_map(
123 'gutenberg_strip_php_suffix',
124 $template_hierarchy
125 );
126
127 // Find all potential templates 'wp_template' post matching the hierarchy.
128 $template_query = new WP_Query(
129 array(
130 'post_type' => 'wp_template',
131 'post_status' => array( 'publish', 'auto-draft' ),
132 'post_name__in' => $slugs,
133 'orderby' => 'post_name__in',
134 'posts_per_page' => -1,
135 'no_found_rows' => true,
136 'tax_query' => array(
137 array(
138 'taxonomy' => 'wp_theme',
139 'field' => 'slug',
140 'terms' => wp_get_theme()->get_stylesheet(),
141 ),
142 ),
143 )
144 );
145 $templates = $template_query->get_posts();
146
147 // Order these templates per slug priority.
148 // Build map of template slugs to their priority in the current hierarchy.
149 $slug_priorities = array_flip( $slugs );
150
151 usort(
152 $templates,
153 function ( $template_a, $template_b ) use ( $slug_priorities ) {
154 return $slug_priorities[ $template_a->post_name ] - $slug_priorities[ $template_b->post_name ];
155 }
156 );
157
158 return count( $templates ) ? $templates[0] : null;
159 }
160
161 /**
162 * Displays title tag with content, regardless of whether theme has title-tag support.
163 *
164 * @see _wp_render_title_tag()
165 */
166 function gutenberg_render_title_tag() {
167 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
168 }
169
170 /**
171 * Renders the markup for the current template.
172 */
173 function gutenberg_render_the_template() {
174 global $_wp_current_template_content;
175 global $wp_embed;
176
177 if ( ! $_wp_current_template_content ) {
178 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
179 return;
180 }
181
182 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
183 $content = $wp_embed->autoembed( $content );
184 $content = do_blocks( $content );
185 $content = wptexturize( $content );
186 if ( function_exists( 'wp_filter_content_tags' ) ) {
187 $content = wp_filter_content_tags( $content );
188 } else {
189 $content = wp_make_content_images_responsive( $content );
190 }
191 $content = str_replace( ']]>', ']]&gt;', $content );
192
193 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
194 // (e.g. `.wp-site-blocks > *`).
195 echo '<div class="wp-site-blocks">';
196 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
197 echo '</div>';
198 }
199
200 /**
201 * Renders a 'viewport' meta tag.
202 *
203 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
204 */
205 function gutenberg_viewport_meta_tag() {
206 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
207 }
208
209 /**
210 * Strips .php suffix from template file names.
211 *
212 * @access private
213 *
214 * @param string $template_file Template file name.
215 * @return string Template file name without extension.
216 */
217 function gutenberg_strip_php_suffix( $template_file ) {
218 return preg_replace( '/\.php$/', '', $template_file );
219 }
220
221 /**
222 * Removes post details from block context when rendering a block template.
223 *
224 * @param array $context Default context.
225 *
226 * @return array Filtered context.
227 */
228 function gutenberg_template_render_without_post_block_context( $context ) {
229 /*
230 * When loading a template or template part directly and not through a page
231 * that resolves it, the top-level post ID and type context get set to that
232 * of the template part. Templates are just the structure of a site, and
233 * they should not be available as post context because blocks like Post
234 * Content would recurse infinitely.
235 */
236 if ( isset( $context['postType'] ) &&
237 ( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
238 unset( $context['postId'] );
239 unset( $context['postType'] );
240 }
241
242 return $context;
243 }
244 add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );
245