PluginProbe
Gutenberg / 8.3.0
Gutenberg v8.3.0
24.0.0 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 All 403 releases
← All changes | lib/template-loader.php +257 -86 7.4.08.3.0 View file →
@@ -5,23 +5,16 @@
5 5 * @package gutenberg
6 6 */
7 7
8 8 /**
9 - * Adds necessary filters to use 'wp_template' posts instead of theme template files.
9 + * Return a list of all overrideable default template types.
10 + *
11 + * @see get_query_template
12 + *
13 + * @return string[] List of all overrideable default template types.
10 14 */
11 -function gutenberg_add_template_loader_filters() {
12 - if ( ! post_type_exists( 'wp_template' ) ) {
13 - return;
14 - }
15 -
16 - /**
17 - * Array of all overrideable default template types.
18 - *
19 - * @see get_query_template
20 - *
21 - * @var array
22 - */
23 - $template_types = array(
15 +function get_template_types() {
16 + return array(
24 17 'index',
25 18 '404',
26 19 'archive',
27 20 'author',
@@ -28,12 +21,12 @@
28 21 'category',
29 22 'tag',
30 23 'taxonomy',
31 24 'date',
32 - // Skip 'embed' for now because it is not a regular template type.
25 + 'embed',
33 26 'home',
34 - 'frontpage',
35 - 'privacypolicy',
27 + 'front-page',
28 + 'privacy-policy',
36 29 'page',
37 30 'search',
38 31 'single',
39 32 'singular',
@@ -38,39 +31,98 @@
38 31 'single',
39 32 'singular',
40 33 'attachment',
41 34 );
42 - foreach ( $template_types as $template_type ) {
43 - add_filter( $template_type . '_template', 'gutenberg_override_query_template', 20, 3 );
35 +}
36 +
37 +/**
38 + * Adds necessary filters to use 'wp_template' posts instead of theme template files.
39 + */
40 +function gutenberg_add_template_loader_filters() {
41 + if ( ! post_type_exists( 'wp_template' ) ) {
42 + return;
44 43 }
45 44
46 - add_filter( 'template_include', 'gutenberg_find_template', 20 );
45 + foreach ( get_template_types() as $template_type ) {
46 + if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular template type.
47 + continue;
48 + }
49 + add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
50 + }
47 51 }
48 52 add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
49 53
50 54 /**
51 - * Filters into the "{$type}_template" hooks to record the current template hierarchy.
55 + * Get the template hierarchy for a given template type.
52 56 *
53 - * The method returns an empty result for every template so that a 'wp_template' post
54 - * is used instead.
57 + * Internally, this filters into the "{$type}_template_hierarchy" hook to record the type-specific template hierarchy.
55 58 *
56 - * @see gutenberg_find_template
59 + * @param string $template_type A template type.
60 + * @return string[] A list of template candidates, in descending order of priority.
61 + */
62 +function get_template_hierachy( $template_type ) {
63 + if ( ! in_array( $template_type, get_template_types(), true ) ) {
64 + return array();
65 + }
66 +
67 + $get_template_function = 'get_' . str_replace( '-', '_', $template_type ) . '_template'; // front-page -> get_front_page_template.
68 + $template_hierarchy_filter = str_replace( '-', '', $template_type ) . '_template_hierarchy'; // front-page -> frontpage_template_hierarchy.
69 +
70 + $result = array();
71 + $template_hierarchy_filter_function = function( $templates ) use ( &$result ) {
72 + $result = $templates;
73 + return $templates;
74 + };
75 +
76 + add_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20, 1 );
77 + call_user_func( $get_template_function ); // This invokes template_hierarchy_filter.
78 + remove_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20 );
79 +
80 + return $result;
81 +}
82 +
83 +/**
84 + * Filters into the "{$type}_template" hooks to redirect them to the Full Site Editing template canvas.
57 85 *
86 + * Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
87 + *
58 88 * @param string $template Path to the template. See locate_template().
59 89 * @param string $type Sanitized filename without extension.
60 90 * @param array $templates A list of template candidates, in descending order of priority.
61 - * @return string Empty string to ensure template file is considered not found.
91 + * @return string The path to the Full Site Editing template canvas file.
62 92 */
63 93 function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
64 - global $_wp_current_template_hierarchy;
94 + global $_wp_current_template_id, $_wp_current_template_content;
65 95
66 - if ( ! is_array( $_wp_current_template_hierarchy ) ) {
67 - $_wp_current_template_hierarchy = $templates;
96 + $current_template = gutenberg_find_template_post_and_parts( $type, $templates );
97 +
98 + if ( $current_template ) {
99 + $_wp_current_template_id = $current_template['template_post']->ID;
100 + $_wp_current_template_content = empty( $current_template['template_post']->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template['template_post']->post_content;
101 +
102 + if ( isset( $_GET['_wp-find-template'] ) ) {
103 + wp_send_json_success( $current_template['template_post'] );
104 + }
68 105 } else {
69 - $_wp_current_template_hierarchy = array_merge( $_wp_current_template_hierarchy, $templates );
106 + if ( 'index' === $type ) {
107 + if ( isset( $_GET['_wp-find-template'] ) ) {
108 + wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
109 + }
110 + } else {
111 + return false; // So that the template loader keeps looking for templates.
112 + }
70 113 }
71 114
72 - return '';
115 + // Add hooks for template canvas.
116 + // Add viewport meta tag.
117 + add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
118 +
119 + // Render title tag with content, regardless of whether theme has title-tag support.
120 + remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
121 + add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional.
122 +
123 + // This file will be included instead of the theme's template file.
124 + return gutenberg_dir_path() . 'lib/template-canvas.php';
73 125 }
74 126
75 127 /**
76 128 * Recursively traverses a block tree, creating auto drafts
@@ -78,54 +130,106 @@
78 130 *
79 131 * @access private
80 132 *
81 133 * @param array $block The root block to start traversing from.
134 + * @return int[] A list of template parts IDs for the given block.
82 135 */
83 136 function create_auto_draft_for_template_part_block( $block ) {
84 - if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['id'] ) ) {
85 - $template_part_file_path =
86 - get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html';
87 - if ( ! file_exists( $template_part_file_path ) ) {
88 - return;
137 + $template_part_ids = array();
138 +
139 + if ( 'core/template-part' === $block['blockName'] ) {
140 + if ( isset( $block['attrs']['postId'] ) ) {
141 + // Template part is customized.
142 + $template_part_id = $block['attrs']['postId'];
143 + } else {
144 + // A published post might already exist if this template part
145 + // was customized elsewhere or if it's part of a customized
146 + // template. We also check if an auto-draft was already created
147 + // because preloading can make this run twice, so, different code
148 + // paths can end up with different posts for the same template part.
149 + // E.g. The server could send back post ID 1 to the client, preload,
150 + // and create another auto-draft. So, if the client tries to resolve the
151 + // post ID from the slug and theme, it won't match with what the server sent.
152 + $template_part_query = new WP_Query(
153 + array(
154 + 'post_type' => 'wp_template_part',
155 + 'post_status' => array( 'publish', 'auto-draft' ),
156 + 'name' => $block['attrs']['slug'],
157 + 'meta_key' => 'theme',
158 + 'meta_value' => $block['attrs']['theme'],
159 + 'posts_per_page' => 1,
160 + 'no_found_rows' => true,
161 + )
162 + );
163 + $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
164 + if ( $template_part_post ) {
165 + $template_part_id = $template_part_post->ID;
166 + } else {
167 + // Template part is not customized, get it from a file and make an auto-draft for it.
168 + $template_part_file_path =
169 + get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html';
170 + if ( ! file_exists( $template_part_file_path ) ) {
171 + if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
172 + $template_part_file_path =
173 + dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html';
174 + if ( ! file_exists( $template_part_file_path ) ) {
175 + return;
176 + }
177 + } else {
178 + return;
179 + }
180 + }
181 + $template_part_id = wp_insert_post(
182 + array(
183 + 'post_content' => file_get_contents( $template_part_file_path ),
184 + 'post_title' => $block['attrs']['slug'],
185 + 'post_status' => 'auto-draft',
186 + 'post_type' => 'wp_template_part',
187 + 'post_name' => $block['attrs']['slug'],
188 + 'meta_input' => array(
189 + 'theme' => $block['attrs']['theme'],
190 + ),
191 + )
192 + );
193 + }
89 194 }
90 - wp_insert_post(
91 - array(
92 - 'post_content' => file_get_contents( $template_part_file_path ),
93 - 'post_title' => ucfirst( $block['attrs']['slug'] ),
94 - 'post_status' => 'auto-draft',
95 - 'post_type' => 'wp_template_part',
96 - 'post_name' => $block['attrs']['slug'],
97 - 'meta_input' => array(
98 - 'theme' => $block['attrs']['theme'],
99 - ),
100 - )
101 - );
195 + $template_part_ids[ $block['attrs']['slug'] ] = $template_part_id;
102 196 }
103 197
104 198 foreach ( $block['innerBlocks'] as $inner_block ) {
105 - create_auto_draft_for_template_part_block( $inner_block );
199 + $template_part_ids = array_merge( $template_part_ids, create_auto_draft_for_template_part_block( $inner_block ) );
106 200 }
201 + return $template_part_ids;
107 202 }
108 203
109 204 /**
110 - * Find the correct 'wp_template' post for the current hierarchy and return the path
111 - * to the canvas file that will render it.
205 + * Return the correct 'wp_template' post and template part IDs for the current template.
112 206 *
113 - * @param string $template_file Original template file. Will be overridden.
114 - * @return string Path to the canvas file to include.
207 + * Accepts an optional $template_hierarchy argument as a hint.
208 + *
209 + * @param string $template_type The current template type.
210 + * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
211 + * @return null|array {
212 + * @type WP_Post|null template_post A template post object, or null if none could be found.
213 + * @type int[] A list of template parts IDs for the template.
214 + * }
115 215 */
116 -function gutenberg_find_template( $template_file ) {
117 - global $_wp_current_template_id, $_wp_current_template_content, $_wp_current_template_hierarchy;
216 +function gutenberg_find_template_post_and_parts( $template_type, $template_hierarchy = array() ) {
217 + if ( ! $template_type ) {
218 + return null;
219 + }
118 220
119 - // Bail if no relevant template hierarchy was determined, or if the template file
120 - // was overridden another way.
121 - if ( ! $_wp_current_template_hierarchy || $template_file ) {
122 - return $template_file;
221 + if ( empty( $template_hierarchy ) ) {
222 + if ( 'index' === $template_type ) {
223 + $template_hierarchy = get_template_hierachy( 'index' );
224 + } else {
225 + $template_hierarchy = array_merge( get_template_hierachy( $template_type ), get_template_hierachy( 'index' ) );
226 + }
123 227 }
124 228
125 229 $slugs = array_map(
126 230 'gutenberg_strip_php_suffix',
127 - $_wp_current_template_hierarchy
231 + $template_hierarchy
128 232 );
129 233
130 234 // Find most specific 'wp_template' post matching the hierarchy.
131 235 $template_query = new WP_Query(
@@ -134,13 +238,13 @@
134 238 'post_status' => 'publish',
135 239 'post_name__in' => $slugs,
136 240 'orderby' => 'post_name__in',
137 241 'posts_per_page' => 1,
242 + 'no_found_rows' => true,
138 243 )
139 244 );
140 245
141 - $template_posts = $template_query->get_posts();
142 - $current_template_post = array_shift( $template_posts );
246 + $current_template_post = $template_query->have_posts() ? $template_query->next_post() : null;
143 247
144 248 // Build map of template slugs to their priority in the current hierarchy.
145 249 $slug_priorities = array_flip( $slugs );
146 250
@@ -146,14 +250,19 @@
146 250
147 251 // See if there is a theme block template with higher priority than the resolved template post.
148 252 $higher_priority_block_template_path = null;
149 253 $higher_priority_block_template_priority = PHP_INT_MAX;
150 - $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' ) ?: array();
254 + $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' );
255 + $block_template_files = is_array( $block_template_files ) ? $block_template_files : array();
151 256 if ( is_child_theme() ) {
152 - $block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html' ) ?: array() );
257 + $child_block_template_files = glob( get_template_directory() . '/block-templates/*.html' );
258 + $child_block_template_files = is_array( $child_block_template_files ) ? $child_block_template_files : array();
259 + $block_template_files = array_merge( $block_template_files, $child_block_template_files );
153 260 }
154 261 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
155 - $block_template_files = array_merge( $block_template_files, glob( dirname( __FILE__ ) . '/demo-block-templates/*.html' ) ?: array() );
262 + $demo_block_template_files = glob( dirname( __FILE__ ) . '/demo-block-templates/*.html' );
263 + $demo_block_template_files = is_array( $demo_block_template_files ) ? $demo_block_template_files : array();
264 + $block_template_files = array_merge( $block_template_files, $demo_block_template_files );
156 265 }
157 266 foreach ( $block_template_files as $path ) {
158 267 if ( ! isset( $slug_priorities[ basename( $path, '.html' ) ] ) ) {
159 268 continue;
@@ -170,22 +279,39 @@
170 279
171 280 // If there is, use it instead.
172 281 if ( isset( $higher_priority_block_template_path ) ) {
173 282 $post_name = basename( $higher_priority_block_template_path, '.html' );
283 + $file_contents = file_get_contents( $higher_priority_block_template_path );
174 284 $current_template_post = array(
175 - 'post_content' => file_get_contents( $higher_priority_block_template_path ),
176 - 'post_title' => ucfirst( $post_name ),
285 + 'post_content' => $file_contents,
286 + 'post_title' => $post_name,
177 287 'post_status' => 'auto-draft',
178 288 'post_type' => 'wp_template',
179 289 'post_name' => $post_name,
180 290 );
181 - if ( is_admin() ) {
291 + if ( is_admin() || defined( 'REST_REQUEST' ) ) {
292 + $template_query = new WP_Query(
293 + array(
294 + 'post_type' => 'wp_template',
295 + 'post_status' => 'auto-draft',
296 + 'name' => $post_name,
297 + 'posts_per_page' => 1,
298 + 'no_found_rows' => true,
299 + )
300 + );
301 + $current_template_post = $template_query->have_posts() ? $template_query->next_post() : $current_template_post;
302 +
182 303 // Only create auto-draft of block template for editing
183 - // in admin screens, similarly to how we do it for new
184 - // posts in the editor.
185 - $current_template_post = get_post(
186 - wp_insert_post( $current_template_post )
187 - );
304 + // in admin screens, when necessary, because the underlying
305 + // file has changed.
306 + if ( is_array( $current_template_post ) || $current_template_post->post_content !== $file_contents ) {
307 + if ( ! is_array( $current_template_post ) ) {
308 + $current_template_post->post_content = $file_contents;
309 + }
310 + $current_template_post = get_post(
311 + wp_insert_post( $current_template_post )
312 + );
313 + }
188 314 } else {
189 315 $current_template_post = new WP_Post(
190 316 (object) $current_template_post
191 317 );
@@ -191,25 +317,36 @@
191 317 );
192 318 }
193 319 }
194 320
321 + // If we haven't found any template post by here, it means that this theme doesn't even come with a fallback
322 + // `index.html` block template. We create one so that people that are trying to access the editor are greeted
323 + // with a blank page rather than an error.
324 + if ( ! $current_template_post && ( is_admin() || defined( 'REST_REQUEST' ) ) ) {
325 + $current_template_post = array(
326 + 'post_title' => 'index',
327 + 'post_status' => 'auto-draft',
328 + 'post_type' => 'wp_template',
329 + 'post_name' => 'index',
330 + );
331 + $current_template_post = get_post(
332 + wp_insert_post( $current_template_post )
333 + );
334 + }
335 +
195 336 if ( $current_template_post ) {
337 + $template_part_ids = array();
196 338 if ( is_admin() ) {
197 339 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
198 - create_auto_draft_for_template_part_block( $block );
340 + $template_part_ids = array_merge( $template_part_ids, create_auto_draft_for_template_part_block( $block ) );
199 341 }
200 342 }
201 - $_wp_current_template_id = $current_template_post->ID;
202 - $_wp_current_template_content = $current_template_post->post_content;
343 + return array(
344 + 'template_post' => $current_template_post,
345 + 'template_part_ids' => $template_part_ids,
346 + );
203 347 }
204 -
205 - // Add extra hooks for template canvas.
206 - add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
207 - remove_action( 'wp_head', '_wp_render_title_tag', 1 );
208 - add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
209 -
210 - // This file will be included instead of the theme's template file.
211 - return gutenberg_dir_path() . 'lib/template-canvas.php';
348 + return null;
212 349 }
213 350
214 351 /**
215 352 * Displays title tag with content, regardless of whether theme has title-tag support.
@@ -235,9 +372,13 @@
235 372 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
236 373 $content = $wp_embed->autoembed( $content );
237 374 $content = do_blocks( $content );
238 375 $content = wptexturize( $content );
239 - $content = wp_make_content_images_responsive( $content );
376 + if ( function_exists( 'wp_filter_content_tags' ) ) {
377 + $content = wp_filter_content_tags( $content );
378 + } else {
379 + $content = wp_make_content_images_responsive( $content );
380 + }
240 381 $content = str_replace( ']]>', ']]>', $content );
241 382
242 383 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
243 384 // (e.g. `.wp-site-blocks > *`).
@@ -274,14 +415,19 @@
274 415 *
275 416 * @return array Filtered editor settings.
276 417 */
277 418 function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
278 - if ( ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
419 + global $post, $_wp_current_template_id;
420 +
421 + if ( ! $post || ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
279 422 return $settings;
280 423 }
281 424
282 425 // Create template part auto-drafts for the edited post.
283 - foreach ( parse_blocks( get_post()->post_content ) as $block ) {
426 + $post = isset( $_wp_current_template_id )
427 + ? get_post( $_wp_current_template_id ) // It's a template.
428 + : get_post(); // It's a post.
429 + foreach ( parse_blocks( $post->post_content ) as $block ) {
284 430 create_auto_draft_for_template_part_block( $block );
285 431 }
286 432
287 433 // TODO: Set editing mode and current template ID for editing modes support.
@@ -287,4 +433,29 @@
287 433 // TODO: Set editing mode and current template ID for editing modes support.
288 434 return $settings;
289 435 }
290 436 add_filter( 'block_editor_settings', 'gutenberg_template_loader_filter_block_editor_settings' );
437 +
438 +/**
439 + * Removes post details from block context when rendering a block template.
440 + *
441 + * @param array $context Default context.
442 + *
443 + * @return array Filtered context.
444 + */
445 +function gutenberg_template_render_without_post_block_context( $context ) {
446 + /*
447 + * When loading a template or template part directly and not through a page
448 + * that resolves it, the top-level post ID and type context get set to that
449 + * of the template part. Templates are just the structure of a site, and
450 + * they should not be available as post context because blocks like Post
451 + * Content would recurse infinitely.
452 + */
453 + if ( isset( $context['postType'] ) &&
454 + ( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
455 + unset( $context['postId'] );
456 + unset( $context['postType'] );
457 + }
458 +
459 + return $context;
460 +}
461 +add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );