PluginProbe
Gutenberg / 7.1.0
Gutenberg v7.1.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.1.0, at lib/template-loader.php

265 lines 8.3 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 $template_posts = $template_query->get_posts();
142 $current_template_post = array_shift( $template_posts );
143
144 // Build map of template slugs to their priority in the current hierarchy.
145 $slug_priorities = array_flip( $slugs );
146
147 // See if there is a theme block template with higher priority than the resolved template post.
148 $higher_priority_block_template_path = null;
149 $higher_priority_block_template_priority = PHP_INT_MAX;
150 $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' ) ?: array();
151 if ( is_child_theme() ) {
152 $block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html' ) ?: array() );
153 }
154 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() );
156 }
157 foreach ( $block_template_files as $path ) {
158 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
159 if (
160 isset( $theme_block_template_priority ) &&
161 $theme_block_template_priority < $higher_priority_block_template_priority &&
162 ( empty( $current_template_post ) || $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ] )
163 ) {
164 $higher_priority_block_template_path = $path;
165 $higher_priority_block_template_priority = $theme_block_template_priority;
166 }
167 }
168
169 // If there is, use it instead.
170 if ( isset( $higher_priority_block_template_path ) ) {
171 $post_name = basename( $higher_priority_block_template_path, '.html' );
172 $current_template_post = array(
173 'post_content' => file_get_contents( $higher_priority_block_template_path ),
174 'post_title' => ucfirst( $post_name ),
175 'post_status' => 'auto-draft',
176 'post_type' => 'wp_template',
177 'post_name' => $post_name,
178 );
179 if ( is_admin() ) {
180 // Only create auto-draft of block template for editing
181 // in admin screens, similarly to how we do it for new
182 // posts in the editor.
183 $current_template_post = get_post(
184 wp_insert_post( $current_template_post )
185 );
186
187 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
188 create_auto_draft_for_template_part_block( $block );
189 }
190 } else {
191 $current_template_post = new WP_Post(
192 (object) $current_template_post
193 );
194 }
195 }
196
197 if ( $current_template_post ) {
198 $_wp_current_template_content = $current_template_post->post_content;
199 }
200
201 // Add extra hooks for template canvas.
202 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
203 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
204 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
205
206 // This file will be included instead of the theme's template file.
207 return gutenberg_dir_path() . 'lib/template-canvas.php';
208 }
209
210 /**
211 * Displays title tag with content, regardless of whether theme has title-tag support.
212 *
213 * @see _wp_render_title_tag()
214 */
215 function gutenberg_render_title_tag() {
216 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
217 }
218
219 /**
220 * Renders the markup for the current template.
221 */
222 function gutenberg_render_the_template() {
223 global $_wp_current_template_content;
224 global $wp_embed;
225
226 if ( ! $_wp_current_template_content ) {
227 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
228 return;
229 }
230
231 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
232 $content = $wp_embed->autoembed( $content );
233 $content = do_blocks( $content );
234 $content = wptexturize( $content );
235 $content = wp_make_content_images_responsive( $content );
236 $content = str_replace( ']]>', ']]&gt;', $content );
237
238 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
239 // (e.g. `.wp-site-blocks > *`).
240 echo '<div class="wp-site-blocks">';
241 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
242 echo '</div>';
243 }
244
245 /**
246 * Renders a 'viewport' meta tag.
247 *
248 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
249 */
250 function gutenberg_viewport_meta_tag() {
251 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
252 }
253
254 /**
255 * Strips .php suffix from template file names.
256 *
257 * @access private
258 *
259 * @param string $template_file Template file name.
260 * @return string Template file name without extension.
261 */
262 function gutenberg_strip_php_suffix( $template_file ) {
263 return preg_replace( '/\.php$/', '', $template_file );
264 }
265