PluginProbe
Gutenberg / 9.7.3
Gutenberg v9.7.3
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.7.3, at lib/template-loader.php

244 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 $current_template = gutenberg_resolve_template( $type, $templates );
67
68 if ( $current_template ) {
69 $_wp_current_template_content = empty( $current_template->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template->post_content;
70
71 if ( isset( $_GET['_wp-find-template'] ) ) {
72 wp_send_json_success( $current_template );
73 }
74 } else {
75 if ( 'index' === $type ) {
76 if ( isset( $_GET['_wp-find-template'] ) ) {
77 wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
78 }
79 } else {
80 return false; // So that the template loader keeps looking for templates.
81 }
82 }
83
84 // Add hooks for template canvas.
85 // Add viewport meta tag.
86 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
87
88 // Render title tag with content, regardless of whether theme has title-tag support.
89 remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
90 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional.
91
92 // This file will be included instead of the theme's template file.
93 return gutenberg_dir_path() . 'lib/template-canvas.php';
94 }
95
96 /**
97 * Return the correct 'wp_template' to render fot the request template type.
98 *
99 * Accepts an optional $template_hierarchy argument as a hint.
100 *
101 * @param string $template_type The current template type.
102 * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
103 * @return null|array {
104 * @type WP_Post|null template_post A template post object, or null if none could be found.
105 * @type int[] A list of template parts IDs for the template.
106 * }
107 */
108 function gutenberg_resolve_template( $template_type, $template_hierarchy = array() ) {
109 if ( ! $template_type ) {
110 return null;
111 }
112
113 if ( empty( $template_hierarchy ) ) {
114 if ( 'index' === $template_type ) {
115 $template_hierarchy = get_template_hierarchy( 'index' );
116 } else {
117 $template_hierarchy = array_merge( get_template_hierarchy( $template_type ), get_template_hierarchy( 'index' ) );
118 }
119 }
120
121 $slugs = array_map(
122 'gutenberg_strip_php_suffix',
123 $template_hierarchy
124 );
125
126 // Find all potential templates 'wp_template' post matching the hierarchy.
127 $template_query = new WP_Query(
128 array(
129 'post_type' => 'wp_template',
130 'post_status' => array( 'publish', 'auto-draft' ),
131 'post_name__in' => $slugs,
132 'orderby' => 'post_name__in',
133 'posts_per_page' => -1,
134 'no_found_rows' => true,
135 'tax_query' => array(
136 array(
137 'taxonomy' => 'wp_theme',
138 'field' => 'slug',
139 'terms' => wp_get_theme()->get_stylesheet(),
140 ),
141 ),
142 )
143 );
144 $templates = $template_query->get_posts();
145
146 // Order these templates per slug priority.
147 // Build map of template slugs to their priority in the current hierarchy.
148 $slug_priorities = array_flip( $slugs );
149
150 usort(
151 $templates,
152 function ( $template_a, $template_b ) use ( $slug_priorities ) {
153 return $slug_priorities[ $template_a->post_name ] - $slug_priorities[ $template_b->post_name ];
154 }
155 );
156
157 return count( $templates ) ? $templates[0] : null;
158 }
159
160 /**
161 * Displays title tag with content, regardless of whether theme has title-tag support.
162 *
163 * @see _wp_render_title_tag()
164 */
165 function gutenberg_render_title_tag() {
166 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
167 }
168
169 /**
170 * Renders the markup for the current template.
171 */
172 function gutenberg_render_the_template() {
173 global $_wp_current_template_content;
174 global $wp_embed;
175
176 if ( ! $_wp_current_template_content ) {
177 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
178 return;
179 }
180
181 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
182 $content = $wp_embed->autoembed( $content );
183 $content = do_blocks( $content );
184 $content = wptexturize( $content );
185 if ( function_exists( 'wp_filter_content_tags' ) ) {
186 $content = wp_filter_content_tags( $content );
187 } else {
188 $content = wp_make_content_images_responsive( $content );
189 }
190 $content = str_replace( ']]>', ']]&gt;', $content );
191
192 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
193 // (e.g. `.wp-site-blocks > *`).
194 echo '<div class="wp-site-blocks">';
195 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
196 echo '</div>';
197 }
198
199 /**
200 * Renders a 'viewport' meta tag.
201 *
202 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
203 */
204 function gutenberg_viewport_meta_tag() {
205 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
206 }
207
208 /**
209 * Strips .php suffix from template file names.
210 *
211 * @access private
212 *
213 * @param string $template_file Template file name.
214 * @return string Template file name without extension.
215 */
216 function gutenberg_strip_php_suffix( $template_file ) {
217 return preg_replace( '/\.(php|html)$/', '', $template_file );
218 }
219
220 /**
221 * Removes post details from block context when rendering a block template.
222 *
223 * @param array $context Default context.
224 *
225 * @return array Filtered context.
226 */
227 function gutenberg_template_render_without_post_block_context( $context ) {
228 /*
229 * When loading a template or template part directly and not through a page
230 * that resolves it, the top-level post ID and type context get set to that
231 * of the template part. Templates are just the structure of a site, and
232 * they should not be available as post context because blocks like Post
233 * Content would recurse infinitely.
234 */
235 if ( isset( $context['postType'] ) &&
236 ( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
237 unset( $context['postId'] );
238 unset( $context['postType'] );
239 }
240
241 return $context;
242 }
243 add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );
244