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

291 lines 9.1 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_id, $_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 if ( ! isset( $slug_priorities[ basename( $path, '.html' ) ] ) ) {
159 continue;
160 }
161 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
162 if (
163 $theme_block_template_priority < $higher_priority_block_template_priority &&
164 ( empty( $current_template_post ) || $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ] )
165 ) {
166 $higher_priority_block_template_path = $path;
167 $higher_priority_block_template_priority = $theme_block_template_priority;
168 }
169 }
170
171 // If there is, use it instead.
172 if ( isset( $higher_priority_block_template_path ) ) {
173 $post_name = basename( $higher_priority_block_template_path, '.html' );
174 $current_template_post = array(
175 'post_content' => file_get_contents( $higher_priority_block_template_path ),
176 'post_title' => ucfirst( $post_name ),
177 'post_status' => 'auto-draft',
178 'post_type' => 'wp_template',
179 'post_name' => $post_name,
180 );
181 if ( is_admin() ) {
182 // Only create auto-draft of block template for editing
183 // in admin screens, similarly to how we do it for new
184 // posts in the editor.
185 $current_template_post = get_post(
186 wp_insert_post( $current_template_post )
187 );
188 } else {
189 $current_template_post = new WP_Post(
190 (object) $current_template_post
191 );
192 }
193 }
194
195 if ( $current_template_post ) {
196 if ( is_admin() ) {
197 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
198 create_auto_draft_for_template_part_block( $block );
199 }
200 }
201 $_wp_current_template_id = $current_template_post->ID;
202 $_wp_current_template_content = $current_template_post->post_content;
203 }
204
205 // Add extra hooks for template canvas.
206 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
207 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
208 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
209
210 // This file will be included instead of the theme's template file.
211 return gutenberg_dir_path() . 'lib/template-canvas.php';
212 }
213
214 /**
215 * Displays title tag with content, regardless of whether theme has title-tag support.
216 *
217 * @see _wp_render_title_tag()
218 */
219 function gutenberg_render_title_tag() {
220 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
221 }
222
223 /**
224 * Renders the markup for the current template.
225 */
226 function gutenberg_render_the_template() {
227 global $_wp_current_template_content;
228 global $wp_embed;
229
230 if ( ! $_wp_current_template_content ) {
231 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
232 return;
233 }
234
235 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
236 $content = $wp_embed->autoembed( $content );
237 $content = do_blocks( $content );
238 $content = wptexturize( $content );
239 $content = wp_make_content_images_responsive( $content );
240 $content = str_replace( ']]>', ']]&gt;', $content );
241
242 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
243 // (e.g. `.wp-site-blocks > *`).
244 echo '<div class="wp-site-blocks">';
245 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
246 echo '</div>';
247 }
248
249 /**
250 * Renders a 'viewport' meta tag.
251 *
252 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
253 */
254 function gutenberg_viewport_meta_tag() {
255 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
256 }
257
258 /**
259 * Strips .php suffix from template file names.
260 *
261 * @access private
262 *
263 * @param string $template_file Template file name.
264 * @return string Template file name without extension.
265 */
266 function gutenberg_strip_php_suffix( $template_file ) {
267 return preg_replace( '/\.php$/', '', $template_file );
268 }
269
270 /**
271 * Extends default editor settings to enable template and template part editing.
272 *
273 * @param array $settings Default editor settings.
274 *
275 * @return array Filtered editor settings.
276 */
277 function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
278 if ( ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
279 return $settings;
280 }
281
282 // Create template part auto-drafts for the edited post.
283 foreach ( parse_blocks( get_post()->post_content ) as $block ) {
284 create_auto_draft_for_template_part_block( $block );
285 }
286
287 // TODO: Set editing mode and current template ID for editing modes support.
288 return $settings;
289 }
290 add_filter( 'block_editor_settings', 'gutenberg_template_loader_filter_block_editor_settings' );
291