-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
template-loader.php
359 lines (326 loc) · 12.2 KB
/
template-loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Block template loader functions.
*
* @package gutenberg
*/
/**
* Adds necessary filters to use 'wp_template' posts instead of theme template files.
*/
function gutenberg_add_template_loader_filters() {
if ( ! post_type_exists( 'wp_template' ) ) {
return;
}
/**
* Array of all overrideable default template types.
*
* @see get_query_template
*
* @var array
*/
$template_types = array(
'index',
'404',
'archive',
'author',
'category',
'tag',
'taxonomy',
'date',
// Skip 'embed' for now because it is not a regular template type.
'home',
'frontpage',
'privacypolicy',
'page',
'search',
'single',
'singular',
'attachment',
);
foreach ( $template_types as $template_type ) {
add_filter( $template_type . '_template', 'gutenberg_override_query_template', 20, 3 );
}
add_filter( 'template_include', 'gutenberg_find_template', 20 );
}
add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
/**
* Filters into the "{$type}_template" hooks to record the current template hierarchy.
*
* The method returns an empty result for every template so that a 'wp_template' post
* is used instead.
*
* @see gutenberg_find_template
*
* @param string $template Path to the template. See locate_template().
* @param string $type Sanitized filename without extension.
* @param array $templates A list of template candidates, in descending order of priority.
* @return string Empty string to ensure template file is considered not found.
*/
function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
global $_wp_current_template_hierarchy;
if ( ! is_array( $_wp_current_template_hierarchy ) ) {
$_wp_current_template_hierarchy = $templates;
} else {
$_wp_current_template_hierarchy = array_merge( $_wp_current_template_hierarchy, $templates );
}
return '';
}
/**
* Recursively traverses a block tree, creating auto drafts
* for any encountered template parts without a fixed post.
*
* @access private
*
* @param array $block The root block to start traversing from.
*/
function create_auto_draft_for_template_part_block( $block ) {
global $_wp_current_template_part_ids;
if ( 'core/template-part' === $block['blockName'] ) {
if ( isset( $block['attrs']['postId'] ) ) {
// Template part is customized.
$template_part_id = $block['attrs']['postId'];
} else {
// A published post might already exist if this template part
// was customized elsewhere or if it's part of a customized
// template. We also check if an auto-draft was already created
// because preloading can make this run twice, so, different code
// paths can end up with different posts for the same template part.
// E.g. The server could send back post ID 1 to the client, preload,
// and create another auto-draft. So, if the client tries to resolve the
// post ID from the slug and theme, it won't match with what the server sent.
$template_part_query = new WP_Query(
array(
'post_type' => 'wp_template_part',
'post_status' => array( 'publish', 'auto-draft' ),
'name' => $block['attrs']['slug'],
'meta_key' => 'theme',
'meta_value' => $block['attrs']['theme'],
'posts_per_page' => 1,
'no_found_rows' => true,
)
);
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
if ( $template_part_post ) {
$template_part_id = $template_part_post->ID;
} else {
// Template part is not customized, get it from a file and make an auto-draft for it.
$template_part_file_path =
get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html';
if ( ! file_exists( $template_part_file_path ) ) {
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
$template_part_file_path =
dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html';
if ( ! file_exists( $template_part_file_path ) ) {
return;
}
} else {
return;
}
}
$template_part_id = wp_insert_post(
array(
'post_content' => file_get_contents( $template_part_file_path ),
'post_title' => $block['attrs']['slug'],
'post_status' => 'auto-draft',
'post_type' => 'wp_template_part',
'post_name' => $block['attrs']['slug'],
'meta_input' => array(
'theme' => $block['attrs']['theme'],
),
)
);
}
}
if ( isset( $_wp_current_template_part_ids ) ) {
$_wp_current_template_part_ids[ $block['attrs']['slug'] ] = $template_part_id;
} else {
$_wp_current_template_part_ids = array( $block['attrs']['slug'] => $template_part_id );
}
}
foreach ( $block['innerBlocks'] as $inner_block ) {
create_auto_draft_for_template_part_block( $inner_block );
}
}
/**
* Find the correct 'wp_template' post for the current hierarchy and return the path
* to the canvas file that will render it.
*
* @param string $template_file Original template file. Will be overridden.
* @return string Path to the canvas file to include.
*/
function gutenberg_find_template( $template_file ) {
global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy;
// Bail if no relevant template hierarchy was determined, or if the template file
// was overridden another way.
if ( ! $_wp_current_template_hierarchy || $template_file ) {
return $template_file;
}
$slugs = array_map(
'gutenberg_strip_php_suffix',
$_wp_current_template_hierarchy
);
// Find most specific 'wp_template' post matching the hierarchy.
$template_query = new WP_Query(
array(
'post_type' => 'wp_template',
'post_status' => 'publish',
'post_name__in' => $slugs,
'orderby' => 'post_name__in',
'posts_per_page' => 1,
'no_found_rows' => true,
)
);
$current_template_post = $template_query->have_posts() ? $template_query->next_post() : null;
// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities = array_flip( $slugs );
// See if there is a theme block template with higher priority than the resolved template post.
$higher_priority_block_template_path = null;
$higher_priority_block_template_priority = PHP_INT_MAX;
$block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html' );
$block_template_files = is_array( $block_template_files ) ? $block_template_files : array();
if ( is_child_theme() ) {
$child_block_template_files = glob( get_template_directory() . '/block-templates/*.html' );
$child_block_template_files = is_array( $child_block_template_files ) ? $child_block_template_files : array();
$block_template_files = array_merge( $block_template_files, $child_block_template_files );
}
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
$demo_block_template_files = glob( dirname( __FILE__ ) . '/demo-block-templates/*.html' );
$demo_block_template_files = is_array( $demo_block_template_files ) ? $demo_block_template_files : array();
$block_template_files = array_merge( $block_template_files, $demo_block_template_files );
}
foreach ( $block_template_files as $path ) {
if ( ! isset( $slug_priorities[ basename( $path, '.html' ) ] ) ) {
continue;
}
$theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
if (
$theme_block_template_priority < $higher_priority_block_template_priority &&
( empty( $current_template_post ) || $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ] )
) {
$higher_priority_block_template_path = $path;
$higher_priority_block_template_priority = $theme_block_template_priority;
}
}
// If there is, use it instead.
if ( isset( $higher_priority_block_template_path ) ) {
$post_name = basename( $higher_priority_block_template_path, '.html' );
$current_template_post = array(
'post_content' => file_get_contents( $higher_priority_block_template_path ),
'post_title' => $post_name,
'post_status' => 'auto-draft',
'post_type' => 'wp_template',
'post_name' => $post_name,
);
if ( is_admin() ) {
// Only create auto-draft of block template for editing
// in admin screens, similarly to how we do it for new
// posts in the editor.
$current_template_post = get_post(
wp_insert_post( $current_template_post )
);
} else {
$current_template_post = new WP_Post(
(object) $current_template_post
);
}
}
if ( isset( $_GET['_wp-find-template'] ) ) {
if ( $current_template_post ) {
wp_send_json_success( $current_template_post );
} else {
wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
}
}
if ( $current_template_post ) {
if ( is_admin() ) {
foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
create_auto_draft_for_template_part_block( $block );
}
}
$_wp_current_template_id = $current_template_post->ID;
$_wp_current_template_name = $current_template_post->post_name;
$_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content;
}
// Add extra hooks for template canvas.
add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
// This file will be included instead of the theme's template file.
return gutenberg_dir_path() . 'lib/template-canvas.php';
}
/**
* Displays title tag with content, regardless of whether theme has title-tag support.
*
* @see _wp_render_title_tag()
*/
function gutenberg_render_title_tag() {
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
/**
* Renders the markup for the current template.
*/
function gutenberg_render_the_template() {
global $_wp_current_template_content;
global $wp_embed;
if ( ! $_wp_current_template_content ) {
echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
return;
}
$content = $wp_embed->run_shortcode( $_wp_current_template_content );
$content = $wp_embed->autoembed( $content );
$content = do_blocks( $content );
$content = wptexturize( $content );
if ( function_exists( 'wp_filter_content_tags' ) ) {
$content = wp_filter_content_tags( $content );
} else {
$content = wp_make_content_images_responsive( $content );
}
$content = str_replace( ']]>', ']]>', $content );
// Wrap block template in .wp-site-blocks to allow for specific descendant styles
// (e.g. `.wp-site-blocks > *`).
echo '<div class="wp-site-blocks">';
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
echo '</div>';
}
/**
* Renders a 'viewport' meta tag.
*
* This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
*/
function gutenberg_viewport_meta_tag() {
echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
}
/**
* Strips .php suffix from template file names.
*
* @access private
*
* @param string $template_file Template file name.
* @return string Template file name without extension.
*/
function gutenberg_strip_php_suffix( $template_file ) {
return preg_replace( '/\.php$/', '', $template_file );
}
/**
* Extends default editor settings to enable template and template part editing.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
global $post, $_wp_current_template_id;
if ( ! $post || ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
return $settings;
}
// Create template part auto-drafts for the edited post.
$post = isset( $_wp_current_template_id )
? get_post( $_wp_current_template_id ) // It's a template.
: get_post(); // It's a post.
foreach ( parse_blocks( $post->post_content ) as $block ) {
create_auto_draft_for_template_part_block( $block );
}
// TODO: Set editing mode and current template ID for editing modes support.
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_template_loader_filter_block_editor_settings' );