-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathtemplate-loader.php
273 lines (241 loc) · 8.76 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
<?php
/**
* Block template loader functions.
*
* @package gutenberg
*/
/**
* Return a list of all overrideable default template types.
*
* @see get_query_template
*
* @return string[] List of all overrideable default template types.
*/
function get_template_types() {
return array(
'index',
'404',
'archive',
'author',
'category',
'tag',
'taxonomy',
'date',
'embed',
'home',
'front-page',
'privacy-policy',
'page',
'search',
'single',
'singular',
'attachment',
);
}
/**
* Adds necessary filters to use 'wp_template' posts instead of theme template files.
*/
function gutenberg_add_template_loader_filters() {
if ( ! gutenberg_is_fse_theme() ) {
return;
}
foreach ( get_template_types() as $template_type ) {
if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular template type.
continue;
}
add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
}
}
add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
/**
* Get the template hierarchy for a given template type.
*
* Internally, this filters into the "{$type}_template_hierarchy" hook to record the type-specific template hierarchy.
*
* @param string $template_type A template type.
* @return string[] A list of template candidates, in descending order of priority.
*/
function get_template_hierarchy( $template_type ) {
if ( ! in_array( $template_type, get_template_types(), true ) ) {
return array();
}
$get_template_function = 'get_' . str_replace( '-', '_', $template_type ) . '_template'; // front-page -> get_front_page_template.
$template_hierarchy_filter = str_replace( '-', '', $template_type ) . '_template_hierarchy'; // front-page -> frontpage_template_hierarchy.
$result = array();
$template_hierarchy_filter_function = function( $templates ) use ( &$result ) {
$result = $templates;
return $templates;
};
add_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20, 1 );
call_user_func( $get_template_function ); // This invokes template_hierarchy_filter.
remove_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20 );
return $result;
}
/**
* Filters into the "{$type}_template" hooks to redirect them to the Full Site Editing template canvas.
*
* Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
*
* @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 The path to the Full Site Editing template canvas file.
*/
function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
global $_wp_current_template_content;
// Create auto-drafts for theme templates.
_gutenberg_synchronize_theme_templates( 'template' );
$current_template = gutenberg_resolve_template( $type, $templates );
if ( $current_template ) {
$_wp_current_template_content = empty( $current_template->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template->post_content;
if ( isset( $_GET['_wp-find-template'] ) ) {
wp_send_json_success( $current_template );
}
} else {
if ( 'index' === $type ) {
if ( isset( $_GET['_wp-find-template'] ) ) {
wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) );
}
} else {
return false; // So that the template loader keeps looking for templates.
}
}
// Add hooks for template canvas.
// Add viewport meta tag.
add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
// Render title tag with content, regardless of whether theme has title-tag support.
remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional.
// This file will be included instead of the theme's template file.
return gutenberg_dir_path() . 'lib/template-canvas.php';
}
/**
* Return the correct 'wp_template' to render fot the request template type.
*
* Accepts an optional $template_hierarchy argument as a hint.
*
* @param string $template_type The current template type.
* @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
* @return null|array {
* @type WP_Post|null template_post A template post object, or null if none could be found.
* @type int[] A list of template parts IDs for the template.
* }
*/
function gutenberg_resolve_template( $template_type, $template_hierarchy = array() ) {
if ( ! $template_type ) {
return null;
}
if ( empty( $template_hierarchy ) ) {
if ( 'index' === $template_type ) {
$template_hierarchy = get_template_hierarchy( 'index' );
} else {
$template_hierarchy = array_merge( get_template_hierarchy( $template_type ), get_template_hierarchy( 'index' ) );
}
}
$slugs = array_map(
'gutenberg_strip_php_suffix',
$template_hierarchy
);
// Find all potential templates 'wp_template' post matching the hierarchy.
$template_query = new WP_Query(
array(
'post_type' => 'wp_template',
'post_status' => array( 'publish', 'auto-draft' ),
'post_name__in' => $slugs,
'orderby' => 'post_name__in',
'posts_per_page' => -1,
'no_found_rows' => true,
'meta_key' => 'theme',
'meta_value' => wp_get_theme()->get_stylesheet(),
)
);
$templates = $template_query->get_posts();
// Order these templates per slug priority.
// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities = array_flip( $slugs );
usort(
$templates,
function ( $template_a, $template_b ) use ( $slug_priorities ) {
$priority_a = $slug_priorities[ $template_a->post_name ] * 2 + ( 'publish' === $template_a->post_status ? 1 : 0 );
$priority_b = $slug_priorities[ $template_b->post_name ] * 2 + ( 'publish' === $template_b->post_status ? 1 : 0 );
return $priority_b - $priority_a;
}
);
return count( $templates ) ? $templates[0] : null;
}
/**
* 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 );
}
/**
* Removes post details from block context when rendering a block template.
*
* @param array $context Default context.
*
* @return array Filtered context.
*/
function gutenberg_template_render_without_post_block_context( $context ) {
/*
* When loading a template or template part directly and not through a page
* that resolves it, the top-level post ID and type context get set to that
* of the template part. Templates are just the structure of a site, and
* they should not be available as post context because blocks like Post
* Content would recurse infinitely.
*/
if ( isset( $context['postType'] ) &&
( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
unset( $context['postId'] );
unset( $context['postType'] );
}
return $context;
}
add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );