PluginProbe
Gutenberg / 7.6.0
Gutenberg v7.6.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.6.0, at lib/template-loader.php

320 lines 10.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 ( ! 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 global $_wp_current_template_part_ids;
85
86 if ( 'core/template-part' === $block['blockName'] ) {
87 if ( ! isset( $block['attrs']['postId'] ) ) {
88 $template_part_file_path =
89 get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html';
90 if ( ! file_exists( $template_part_file_path ) ) {
91 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
92 $template_part_file_path =
93 dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html';
94 if ( ! file_exists( $template_part_file_path ) ) {
95 return;
96 }
97 } else {
98 return;
99 }
100 }
101 $template_part_id = wp_insert_post(
102 array(
103 'post_content' => file_get_contents( $template_part_file_path ),
104 'post_title' => $block['attrs']['slug'],
105 'post_status' => 'auto-draft',
106 'post_type' => 'wp_template_part',
107 'post_name' => $block['attrs']['slug'],
108 'meta_input' => array(
109 'theme' => $block['attrs']['theme'],
110 ),
111 )
112 );
113 } else {
114 $template_part_id = $block['attrs']['postId'];
115 }
116
117 if ( isset( $_wp_current_template_part_ids ) ) {
118 $_wp_current_template_part_ids[ $block['attrs']['slug'] ] = $template_part_id;
119 } else {
120 $_wp_current_template_part_ids = array( $block['attrs']['slug'] => $template_part_id );
121 }
122 }
123
124 foreach ( $block['innerBlocks'] as $inner_block ) {
125 create_auto_draft_for_template_part_block( $inner_block );
126 }
127 }
128
129 /**
130 * Find the correct 'wp_template' post for the current hierarchy and return the path
131 * to the canvas file that will render it.
132 *
133 * @param string $template_file Original template file. Will be overridden.
134 * @return string Path to the canvas file to include.
135 */
136 function gutenberg_find_template( $template_file ) {
137 global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy;
138
139 // Bail if no relevant template hierarchy was determined, or if the template file
140 // was overridden another way.
141 if ( ! $_wp_current_template_hierarchy || $template_file ) {
142 return $template_file;
143 }
144
145 $slugs = array_map(
146 'gutenberg_strip_php_suffix',
147 $_wp_current_template_hierarchy
148 );
149
150 // Find most specific 'wp_template' post matching the hierarchy.
151 $template_query = new WP_Query(
152 array(
153 'post_type' => 'wp_template',
154 'post_status' => 'publish',
155 'post_name__in' => $slugs,
156 'orderby' => 'post_name__in',
157 'posts_per_page' => 1,
158 )
159 );
160
161 $template_posts = $template_query->get_posts();
162 $current_template_post = array_shift( $template_posts );
163
164 // Build map of template slugs to their priority in the current hierarchy.
165 $slug_priorities = array_flip( $slugs );
166
167 // See if there is a theme block template with higher priority than the resolved template post.
168 $higher_priority_block_template_path = null;
169 $higher_priority_block_template_priority = PHP_INT_MAX;
170 $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' ) ?: array();
171 if ( is_child_theme() ) {
172 $block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html' ) ?: array() );
173 }
174 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
175 $block_template_files = array_merge( $block_template_files, glob( dirname( __FILE__ ) . '/demo-block-templates/*.html' ) ?: array() );
176 }
177 foreach ( $block_template_files as $path ) {
178 if ( ! isset( $slug_priorities[ basename( $path, '.html' ) ] ) ) {
179 continue;
180 }
181 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
182 if (
183 $theme_block_template_priority < $higher_priority_block_template_priority &&
184 ( empty( $current_template_post ) || $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ] )
185 ) {
186 $higher_priority_block_template_path = $path;
187 $higher_priority_block_template_priority = $theme_block_template_priority;
188 }
189 }
190
191 // If there is, use it instead.
192 if ( isset( $higher_priority_block_template_path ) ) {
193 $post_name = basename( $higher_priority_block_template_path, '.html' );
194 $current_template_post = array(
195 'post_content' => file_get_contents( $higher_priority_block_template_path ),
196 'post_title' => $post_name,
197 'post_status' => 'auto-draft',
198 'post_type' => 'wp_template',
199 'post_name' => $post_name,
200 );
201 if ( is_admin() ) {
202 // Only create auto-draft of block template for editing
203 // in admin screens, similarly to how we do it for new
204 // posts in the editor.
205 $current_template_post = get_post(
206 wp_insert_post( $current_template_post )
207 );
208 } else {
209 $current_template_post = new WP_Post(
210 (object) $current_template_post
211 );
212 }
213 }
214
215 if ( isset( $_GET['_wp-find-template'] ) ) {
216 if ( $current_template_post ) {
217 wp_send_json_success( $current_template_post );
218 } else {
219 wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
220 }
221 }
222
223 if ( $current_template_post ) {
224 if ( is_admin() ) {
225 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
226 create_auto_draft_for_template_part_block( $block );
227 }
228 }
229 $_wp_current_template_id = $current_template_post->ID;
230 $_wp_current_template_name = $current_template_post->post_name;
231 $_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content;
232 }
233
234 // Add extra hooks for template canvas.
235 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
236 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
237 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
238
239 // This file will be included instead of the theme's template file.
240 return gutenberg_dir_path() . 'lib/template-canvas.php';
241 }
242
243 /**
244 * Displays title tag with content, regardless of whether theme has title-tag support.
245 *
246 * @see _wp_render_title_tag()
247 */
248 function gutenberg_render_title_tag() {
249 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
250 }
251
252 /**
253 * Renders the markup for the current template.
254 */
255 function gutenberg_render_the_template() {
256 global $_wp_current_template_content;
257 global $wp_embed;
258
259 if ( ! $_wp_current_template_content ) {
260 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
261 return;
262 }
263
264 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
265 $content = $wp_embed->autoembed( $content );
266 $content = do_blocks( $content );
267 $content = wptexturize( $content );
268 $content = wp_make_content_images_responsive( $content );
269 $content = str_replace( ']]>', ']]&gt;', $content );
270
271 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
272 // (e.g. `.wp-site-blocks > *`).
273 echo '<div class="wp-site-blocks">';
274 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
275 echo '</div>';
276 }
277
278 /**
279 * Renders a 'viewport' meta tag.
280 *
281 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
282 */
283 function gutenberg_viewport_meta_tag() {
284 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
285 }
286
287 /**
288 * Strips .php suffix from template file names.
289 *
290 * @access private
291 *
292 * @param string $template_file Template file name.
293 * @return string Template file name without extension.
294 */
295 function gutenberg_strip_php_suffix( $template_file ) {
296 return preg_replace( '/\.php$/', '', $template_file );
297 }
298
299 /**
300 * Extends default editor settings to enable template and template part editing.
301 *
302 * @param array $settings Default editor settings.
303 *
304 * @return array Filtered editor settings.
305 */
306 function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
307 if ( ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
308 return $settings;
309 }
310
311 // Create template part auto-drafts for the edited post.
312 foreach ( parse_blocks( get_post()->post_content ) as $block ) {
313 create_auto_draft_for_template_part_block( $block );
314 }
315
316 // TODO: Set editing mode and current template ID for editing modes support.
317 return $settings;
318 }
319 add_filter( 'block_editor_settings', 'gutenberg_template_loader_filter_block_editor_settings' );
320