PluginProbe
Gutenberg / 9.4.0
Gutenberg v9.4.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 7.4.0 All 402 releases
gutenberg / lib / template-loader.php

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

271 lines 8.7 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 * 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.
14 */
15 function get_template_types() {
16 return array(
17 'index',
18 '404',
19 'archive',
20 'author',
21 'category',
22 'tag',
23 'taxonomy',
24 'date',
25 'embed',
26 'home',
27 'front-page',
28 'privacy-policy',
29 'page',
30 'search',
31 'single',
32 'singular',
33 'attachment',
34 );
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 ( ! gutenberg_is_fse_theme() ) {
42 return;
43 }
44
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 }
51 }
52 add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
53
54 /**
55 * Get the template hierarchy for a given template type.
56 *
57 * Internally, this filters into the "{$type}_template_hierarchy" hook to record the type-specific template hierarchy.
58 *
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_hierarchy( $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.
85 *
86 * Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
87 *
88 * @param string $template Path to the template. See locate_template().
89 * @param string $type Sanitized filename without extension.
90 * @param array $templates A list of template candidates, in descending order of priority.
91 * @return string The path to the Full Site Editing template canvas file.
92 */
93 function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
94 global $_wp_current_template_content;
95
96 $current_template = gutenberg_resolve_template( $type, $templates );
97
98 if ( $current_template ) {
99 $_wp_current_template_content = empty( $current_template->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template->post_content;
100
101 if ( isset( $_GET['_wp-find-template'] ) ) {
102 wp_send_json_success( $current_template );
103 }
104 } else {
105 if ( 'index' === $type ) {
106 if ( isset( $_GET['_wp-find-template'] ) ) {
107 wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
108 }
109 } else {
110 return false; // So that the template loader keeps looking for templates.
111 }
112 }
113
114 // Add hooks for template canvas.
115 // Add viewport meta tag.
116 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
117
118 // Render title tag with content, regardless of whether theme has title-tag support.
119 remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
120 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional.
121
122 // This file will be included instead of the theme's template file.
123 return gutenberg_dir_path() . 'lib/template-canvas.php';
124 }
125
126 /**
127 * Return the correct 'wp_template' to render fot the request template type.
128 *
129 * Accepts an optional $template_hierarchy argument as a hint.
130 *
131 * @param string $template_type The current template type.
132 * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
133 * @return null|array {
134 * @type WP_Post|null template_post A template post object, or null if none could be found.
135 * @type int[] A list of template parts IDs for the template.
136 * }
137 */
138 function gutenberg_resolve_template( $template_type, $template_hierarchy = array() ) {
139 if ( ! $template_type ) {
140 return null;
141 }
142
143 if ( empty( $template_hierarchy ) ) {
144 if ( 'index' === $template_type ) {
145 $template_hierarchy = get_template_hierarchy( 'index' );
146 } else {
147 $template_hierarchy = array_merge( get_template_hierarchy( $template_type ), get_template_hierarchy( 'index' ) );
148 }
149 }
150
151 $slugs = array_map(
152 'gutenberg_strip_php_suffix',
153 $template_hierarchy
154 );
155
156 // Find all potential templates 'wp_template' post matching the hierarchy.
157 $template_query = new WP_Query(
158 array(
159 'post_type' => 'wp_template',
160 'post_status' => array( 'publish', 'auto-draft' ),
161 'post_name__in' => $slugs,
162 'orderby' => 'post_name__in',
163 'posts_per_page' => -1,
164 'no_found_rows' => true,
165 'meta_key' => 'theme',
166 'meta_value' => wp_get_theme()->get_stylesheet(),
167 )
168 );
169 $templates = $template_query->get_posts();
170
171 // Order these templates per slug priority.
172 // Build map of template slugs to their priority in the current hierarchy.
173 $slug_priorities = array_flip( $slugs );
174
175 usort(
176 $templates,
177 function ( $template_a, $template_b ) use ( $slug_priorities ) {
178 $priority_a = $slug_priorities[ $template_a->post_name ] * 2 + ( 'publish' === $template_a->post_status ? 1 : 0 );
179 $priority_b = $slug_priorities[ $template_b->post_name ] * 2 + ( 'publish' === $template_b->post_status ? 1 : 0 );
180 return $priority_b - $priority_a;
181 }
182 );
183
184 return count( $templates ) ? $templates[0] : null;
185 }
186
187 /**
188 * Displays title tag with content, regardless of whether theme has title-tag support.
189 *
190 * @see _wp_render_title_tag()
191 */
192 function gutenberg_render_title_tag() {
193 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
194 }
195
196 /**
197 * Renders the markup for the current template.
198 */
199 function gutenberg_render_the_template() {
200 global $_wp_current_template_content;
201 global $wp_embed;
202
203 if ( ! $_wp_current_template_content ) {
204 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
205 return;
206 }
207
208 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
209 $content = $wp_embed->autoembed( $content );
210 $content = do_blocks( $content );
211 $content = wptexturize( $content );
212 if ( function_exists( 'wp_filter_content_tags' ) ) {
213 $content = wp_filter_content_tags( $content );
214 } else {
215 $content = wp_make_content_images_responsive( $content );
216 }
217 $content = str_replace( ']]>', ']]&gt;', $content );
218
219 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
220 // (e.g. `.wp-site-blocks > *`).
221 echo '<div class="wp-site-blocks">';
222 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
223 echo '</div>';
224 }
225
226 /**
227 * Renders a 'viewport' meta tag.
228 *
229 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
230 */
231 function gutenberg_viewport_meta_tag() {
232 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
233 }
234
235 /**
236 * Strips .php suffix from template file names.
237 *
238 * @access private
239 *
240 * @param string $template_file Template file name.
241 * @return string Template file name without extension.
242 */
243 function gutenberg_strip_php_suffix( $template_file ) {
244 return preg_replace( '/\.php$/', '', $template_file );
245 }
246
247 /**
248 * Removes post details from block context when rendering a block template.
249 *
250 * @param array $context Default context.
251 *
252 * @return array Filtered context.
253 */
254 function gutenberg_template_render_without_post_block_context( $context ) {
255 /*
256 * When loading a template or template part directly and not through a page
257 * that resolves it, the top-level post ID and type context get set to that
258 * of the template part. Templates are just the structure of a site, and
259 * they should not be available as post context because blocks like Post
260 * Content would recurse infinitely.
261 */
262 if ( isset( $context['postType'] ) &&
263 ( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
264 unset( $context['postId'] );
265 unset( $context['postType'] );
266 }
267
268 return $context;
269 }
270 add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );
271