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

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

262 lines 8.0 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 ( ! 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(
24 'index',
25 '404',
26 'archive',
27 'author',
28 'category',
29 'tag',
30 'taxonomy',
31 'date',
32 // Skip 'embed' for now because it is not a regular template type.
33 'home',
34 'frontpage',
35 'privacypolicy',
36 'page',
37 'search',
38 'single',
39 'singular',
40 'attachment',
41 );
42 foreach ( $template_types as $template_type ) {
43 add_filter( $template_type . '_template', 'gutenberg_override_query_template', 20, 3 );
44 }
45
46 add_filter( 'template_include', 'gutenberg_find_template', 20 );
47 }
48 add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
49
50 /**
51 * Filters into the "{$type}_template" hooks to record the current template hierarchy.
52 *
53 * The method returns an empty result for every template so that a 'wp_template' post
54 * is used instead.
55 *
56 * @see gutenberg_find_template
57 *
58 * @param string $template Path to the template. See locate_template().
59 * @param string $type Sanitized filename without extension.
60 * @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.
62 */
63 function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
64 global $_wp_current_template_hierarchy;
65
66 if ( ! is_array( $_wp_current_template_hierarchy ) ) {
67 $_wp_current_template_hierarchy = $templates;
68 } else {
69 $_wp_current_template_hierarchy = array_merge( $_wp_current_template_hierarchy, $templates );
70 }
71
72 return '';
73 }
74
75 /**
76 * Recursively traverses a block tree, creating auto drafts
77 * for any encountered template parts without a fixed post.
78 *
79 * @access private
80 *
81 * @param array $block The root block to start traversing from.
82 */
83 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;
89 }
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 );
102 }
103
104 foreach ( $block['innerBlocks'] as $inner_block ) {
105 create_auto_draft_for_template_part_block( $inner_block );
106 }
107 }
108
109 /**
110 * Find the correct 'wp_template' post for the current hierarchy and return the path
111 * to the canvas file that will render it.
112 *
113 * @param string $template_file Original template file. Will be overridden.
114 * @return string Path to the canvas file to include.
115 */
116 function gutenberg_find_template( $template_file ) {
117 global $_wp_current_template_content, $_wp_current_template_hierarchy;
118
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;
123 }
124
125 $slugs = array_map(
126 'gutenberg_strip_php_suffix',
127 $_wp_current_template_hierarchy
128 );
129
130 // Find most specific 'wp_template' post matching the hierarchy.
131 $template_query = new WP_Query(
132 array(
133 'post_type' => 'wp_template',
134 'post_status' => 'publish',
135 'post_name__in' => $slugs,
136 'orderby' => 'post_name__in',
137 'posts_per_page' => 1,
138 )
139 );
140
141 if ( $template_query->have_posts() ) {
142 $template_posts = $template_query->get_posts();
143 $current_template_post = array_shift( $template_posts );
144
145 // Build map of template slugs to their priority in the current hierarchy.
146 $slug_priorities = array_flip( $slugs );
147
148 // See if there is a theme block template with higher priority than the resolved template post.
149 $higher_priority_block_template_path = null;
150 $higher_priority_block_template_priority = PHP_INT_MAX;
151 $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html', 1 );
152 if ( is_child_theme() ) {
153 $block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html', 1 ) );
154 }
155 foreach ( $block_template_files as $path ) {
156 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
157 if (
158 isset( $theme_block_template_priority ) &&
159 $theme_block_template_priority < $higher_priority_block_template_priority &&
160 $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ]
161 ) {
162 $higher_priority_block_template_path = $path;
163 $higher_priority_block_template_priority = $theme_block_template_priority;
164 }
165 }
166
167 // If there is, use it instead.
168 if ( isset( $higher_priority_block_template_path ) ) {
169 $post_name = basename( $path, '.html' );
170 $current_template_post = array(
171 'post_content' => file_get_contents( $higher_priority_block_template_path ),
172 'post_title' => ucfirst( $post_name ),
173 'post_status' => 'auto-draft',
174 'post_type' => 'wp_template',
175 'post_name' => $post_name,
176 );
177 if ( is_admin() ) {
178 // Only create auto-draft of block template for editing
179 // in admin screens, similarly to how we do it for new
180 // posts in the editor.
181 $current_template_post = get_post(
182 wp_insert_post( $current_template_post )
183 );
184
185 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
186 create_auto_draft_for_template_part_block( $block );
187 }
188 } else {
189 $current_template_post = new WP_Post(
190 (object) $current_template_post
191 );
192 }
193 }
194
195 $_wp_current_template_content = $current_template_post->post_content;
196 }
197
198 // Add extra hooks for template canvas.
199 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
200 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
201 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
202
203 // This file will be included instead of the theme's template file.
204 return gutenberg_dir_path() . 'lib/template-canvas.php';
205 }
206
207 /**
208 * Displays title tag with content, regardless of whether theme has title-tag support.
209 *
210 * @see _wp_render_title_tag()
211 */
212 function gutenberg_render_title_tag() {
213 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
214 }
215
216 /**
217 * Renders the markup for the current template.
218 */
219 function gutenberg_render_the_template() {
220 global $_wp_current_template_content;
221 global $wp_embed;
222
223 if ( ! $_wp_current_template_content ) {
224 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
225 return;
226 }
227
228 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
229 $content = $wp_embed->autoembed( $content );
230 $content = do_blocks( $content );
231 $content = wptexturize( $content );
232 $content = wp_make_content_images_responsive( $content );
233 $content = str_replace( ']]>', ']]&gt;', $content );
234
235 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
236 // (e.g. `.wp-site-blocks > *`).
237 echo '<div class="wp-site-blocks">';
238 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
239 echo '</div>';
240 }
241
242 /**
243 * Renders a 'viewport' meta tag.
244 *
245 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
246 */
247 function gutenberg_viewport_meta_tag() {
248 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
249 }
250
251 /**
252 * Strips .php suffix from template file names.
253 *
254 * @access private
255 *
256 * @param string $template_file Template file name.
257 * @return string Template file name without extension.
258 */
259 function gutenberg_strip_php_suffix( $template_file ) {
260 return preg_replace( '/\.php$/', '', $template_file );
261 }
262