PluginProbe
Gutenberg / 6.9.0
Gutenberg v6.9.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 6.9.0, at lib/template-loader.php

224 lines 6.8 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 * Find the correct 'wp_template' post for the current hierarchy and return the path
77 * to the canvas file that will render it.
78 *
79 * @param string $template_file Original template file. Will be overridden.
80 * @return string Path to the canvas file to include.
81 */
82 function gutenberg_find_template( $template_file ) {
83 global $_wp_current_template_content, $_wp_current_template_hierarchy;
84
85 // Bail if no relevant template hierarchy was determined, or if the template file
86 // was overridden another way.
87 if ( ! $_wp_current_template_hierarchy || $template_file ) {
88 return $template_file;
89 }
90
91 $slugs = array_map(
92 'gutenberg_strip_php_suffix',
93 $_wp_current_template_hierarchy
94 );
95
96 // Find most specific 'wp_template' post matching the hierarchy.
97 $template_query = new WP_Query(
98 array(
99 'post_type' => 'wp_template',
100 'post_status' => 'publish',
101 'post_name__in' => $slugs,
102 'orderby' => 'post_name__in',
103 'posts_per_page' => 1,
104 )
105 );
106
107 if ( $template_query->have_posts() ) {
108 $template_posts = $template_query->get_posts();
109 $current_template_post = array_shift( $template_posts );
110
111 // Build map of template slugs to their priority in the current hierarchy.
112 $slug_priorities = array_flip( $slugs );
113
114 // See if there is a theme block template with higher priority than the resolved template post.
115 $higher_priority_block_template_path = null;
116 $higher_priority_block_template_priority = PHP_INT_MAX;
117 $block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html', 1 );
118 if ( is_child_theme() ) {
119 $block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html', 1 ) );
120 }
121 foreach ( $block_template_files as $path ) {
122 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
123 if (
124 isset( $theme_block_template_priority ) &&
125 $theme_block_template_priority < $higher_priority_block_template_priority &&
126 $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ]
127 ) {
128 $higher_priority_block_template_path = $path;
129 $higher_priority_block_template_priority = $theme_block_template_priority;
130 }
131 }
132
133 // If there is, use it instead.
134 if ( isset( $higher_priority_block_template_path ) ) {
135 $post_name = basename( $path, '.html' );
136 $current_template_post = array(
137 'post_content' => file_get_contents( $higher_priority_block_template_path ),
138 'post_title' => ucfirst( $post_name ),
139 'post_status' => 'auto-draft',
140 'post_type' => 'wp_template',
141 'post_name' => $post_name,
142 );
143 if ( is_admin() ) {
144 // Only create auto-draft of block template for editing
145 // in admin screens, similarly to how we do it for new
146 // posts in the editor.
147 $current_template_post = get_post(
148 wp_insert_post( $current_template_post )
149 );
150 } else {
151 $current_template_post = new WP_Post(
152 (object) $current_template_post
153 );
154 }
155 }
156
157 $_wp_current_template_content = $current_template_post->post_content;
158 }
159
160 // Add extra hooks for template canvas.
161 add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
162 remove_action( 'wp_head', '_wp_render_title_tag', 1 );
163 add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
164
165 // This file will be included instead of the theme's template file.
166 return gutenberg_dir_path() . 'lib/template-canvas.php';
167 }
168
169 /**
170 * Displays title tag with content, regardless of whether theme has title-tag support.
171 *
172 * @see _wp_render_title_tag()
173 */
174 function gutenberg_render_title_tag() {
175 echo '<title>' . wp_get_document_title() . '</title>' . "\n";
176 }
177
178 /**
179 * Renders the markup for the current template.
180 */
181 function gutenberg_render_the_template() {
182 global $_wp_current_template_content;
183 global $wp_embed;
184
185 if ( ! $_wp_current_template_content ) {
186 echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
187 return;
188 }
189
190 $content = $wp_embed->run_shortcode( $_wp_current_template_content );
191 $content = $wp_embed->autoembed( $content );
192 $content = do_blocks( $content );
193 $content = wptexturize( $content );
194 $content = wp_make_content_images_responsive( $content );
195 $content = str_replace( ']]>', ']]&gt;', $content );
196
197 // Wrap block template in .wp-site-blocks to allow for specific descendant styles
198 // (e.g. `.wp-site-blocks > *`).
199 echo '<div class="wp-site-blocks">';
200 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
201 echo '</div>';
202 }
203
204 /**
205 * Renders a 'viewport' meta tag.
206 *
207 * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
208 */
209 function gutenberg_viewport_meta_tag() {
210 echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
211 }
212
213 /**
214 * Strips .php suffix from template file names.
215 *
216 * @access private
217 *
218 * @param string $template_file Template file name.
219 * @return string Template file name without extension.
220 */
221 function gutenberg_strip_php_suffix( $template_file ) {
222 return preg_replace( '/\.php$/', '', $template_file );
223 }
224