-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathclass-wp-template.php
221 lines (185 loc) · 5.99 KB
/
class-wp-template.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
<?php
/**
* WP Template file.
*
* @package A8C\FSE
*/
namespace A8C\FSE;
/**
* Class WP_Template
*/
class WP_Template {
/**
* Header template type constant.
*
* @var string HEADER
*/
const HEADER = 'header';
/**
* Footer template type constant
*
* @var string FOOTER
*/
const FOOTER = 'footer';
/**
* Name of the currently active theme that is used to reference its template CPTs.
*
* @var string $current_theme_name Name of currently active theme on the site.
*/
private $current_theme_name;
/**
* List of template types that FSE is currently supporting.
*
* @var array $supported_template_types Array of strings containing supported template types.
*/
public $supported_template_types = [ self::HEADER, self::FOOTER ];
/**
* A8C_WP_Template constructor.
*/
public function __construct() {
$this->current_theme_name = $this->normalize_theme_slug( get_stylesheet() );
}
/**
* Returns normalized theme slug for the current theme.
*
* Normalize WP.com theme slugs that differ from those that we'll get on self hosted sites.
* For example, we will get 'modern-business-wpcom' when retrieving theme slug on self hosted sites,
* but due to WP.com setup, on Simple sites we'll get 'pub/modern-business' for the theme.
*
* @param string $theme_slug Theme slug to check support for.
*
* @return string Normalized theme slug.
*/
public function normalize_theme_slug( $theme_slug ) {
if ( 'pub/' === substr( $theme_slug, 0, 4 ) ) {
$theme_slug = substr( $theme_slug, 4 );
}
if ( '-wpcom' === substr( $theme_slug, -6, 6 ) ) {
$theme_slug = substr( $theme_slug, 0, -6 );
}
return $theme_slug;
}
/**
* Checks whether the provided template type is supported in FSE.
*
* @param string $template_type String representing the template type.
*
* @return bool True if provided template type is supported in FSE, false otherwise.
*/
public function is_supported_template_type( $template_type ) {
return in_array( $template_type, $this->supported_template_types, true );
}
/**
* Returns the post ID of the default template CPT for a given template type.
*
* @param string $template_type String representing the template type.
*
* @return null|int Template ID if it exists or null otherwise.
*/
public function get_template_id( $template_type ) {
if ( ! $this->is_supported_template_type( $template_type ) ) {
return null;
}
$term = get_term_by( 'name', "$this->current_theme_name-$template_type", 'wp_template_part_type', ARRAY_A );
// Bail if current site doesn't have this term registered.
if ( ! isset( $term['term_id'] ) ) {
return null;
}
$template_ids = get_objects_in_term( $term['term_id'], $term['taxonomy'], [ 'order' => 'DESC' ] );
// Bail if we haven't found any post instances for this template type.
if ( empty( $template_ids ) ) {
return null;
}
/*
* Assuming that we'll have just one default template for now.
* We'll add support for multiple header and footer variations in future iterations.
*/
return $template_ids[0];
}
/**
* Returns template content for given template type.
*
* @param string $template_type String representing the template type.
*
* @return null|string Template content if it exists or null otherwise.
*/
public function get_template_content( $template_type ) {
if ( ! $this->is_supported_template_type( $template_type ) ) {
return null;
}
$template_id = $this->get_template_id( $template_type );
if ( null === $template_id ) {
return null;
}
$template_post = get_post( $template_id );
if ( null === $template_post ) {
return;
}
return $template_post->post_content;
}
/**
* Returns full page template content.
*
* We only support one page template for now with header at the top and footer at the bottom.
*
* @return null|string
*/
public function get_page_template_content() {
$header_id = $this->get_template_id( self::HEADER );
$footer_id = $this->get_template_id( self::FOOTER );
/*
* Bail if we are missing header or footer. Otherwise this would cause us to
* always return some page template content and show template parts (with empty IDs),
* even for themes that don't support FSE.
*/
if ( ! $header_id || ! $footer_id ) {
return null;
}
return "<!-- wp:a8c/template {\"templateId\":$header_id,\"className\":\"site-header site-branding\"} /-->" .
'<!-- wp:a8c/post-content /-->' .
"<!-- wp:a8c/template {\"templateId\":$footer_id,\"className\":\"site-footer\"} /-->";
}
/**
* Returns array of blocks that represent the template.
*
* @return array
*/
public function get_template_blocks() {
$template_content = $this->get_page_template_content();
$template_blocks = parse_blocks( $template_content );
return is_array( $template_blocks ) ? $template_blocks : [];
}
/**
* Output FSE template markup.
*
* @param string $template_type String representing the template type.
*
* @return null|void Null if unsupported template type is passed, outputs content otherwise.
*/
public function output_template_content( $template_type ) {
if ( ! $this->is_supported_template_type( $template_type ) ) {
return null;
}
// Things that follow are from wp-includes/default-filters.php
// not everything is appropriate for template content as opposed to post content.
global $wp_embed;
$content = $this->get_template_content( $template_type );
// 8 priority
$content = $wp_embed->run_shortcode( $content );
$content = $wp_embed->autoembed( $content );
// 9 priority
$content = do_blocks( $content );
// 10 priority
$content = wptexturize( $content );
// 11 priority
$content = do_shortcode( $content );
$content = prepend_attachment( $content );
if ( has_filter( 'a8c_fse_make_content_images_responsive' ) ) {
$content = apply_filters( 'a8c_fse_make_content_images_responsive', $content );
} else {
$content = wp_make_content_images_responsive( $content );
}
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $content;
}
}