-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathclass-amp-post-template.php
More file actions
520 lines (450 loc) · 12.2 KB
/
class-amp-post-template.php
File metadata and controls
520 lines (450 loc) · 12.2 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* AMP_Post_Template class.
*
* @package AMP
*/
/**
* Class AMP_Post_Template
*
* @since 0.2
* @internal
*/
class AMP_Post_Template {
/**
* Site icon size.
*
* @since 0.2
* @var int
*/
const SITE_ICON_SIZE = 32;
/**
* Content max width.
*
* @since 0.4
* @var int
*/
const CONTENT_MAX_WIDTH = 600;
/**
* Default navbar background.
*
* Needed for 0.3 back-compat
*
* @since 0.4
* @var string
*/
const DEFAULT_NAVBAR_BACKGROUND = '#0a89c0';
/**
* Default navbar color.
*
* Needed for 0.3 back-compat
*
* @since 0.4
* @var string
*/
const DEFAULT_NAVBAR_COLOR = '#fff';
/**
* Post template data.
*
* @since 0.2
* @var array
*/
private $data;
/**
* Post ID.
*
* @since 0.2
* @var int
*/
public $ID;
/**
* Post
*
* @since 0.2
* @var WP_Post
*/
public $post;
/**
* AMP_Post_Template constructor.
*
* @param WP_Post|int $post Post.
*/
public function __construct( $post ) {
if ( is_int( $post ) ) {
$this->ID = $post;
$this->post = get_post( $post );
} elseif ( $post instanceof WP_Post ) {
$this->ID = $post->ID;
$this->post = $post;
}
}
/**
* Set data.
*
* This is called in the get method the first time it is called.
*
* @since 1.5
*
* @see \AMP_Post_Template::get()
*/
private function set_data() {
$content_max_width = self::CONTENT_MAX_WIDTH;
if ( isset( $GLOBALS['content_width'] ) && $GLOBALS['content_width'] > 0 ) {
$content_max_width = $GLOBALS['content_width'];
}
/**
* Filters the content max width for Reader templates.
*
* @since 0.2
*
* @param int $content_max_width Content max width.
*/
$content_max_width = apply_filters( 'amp_content_max_width', $content_max_width );
$this->data = [
'content_max_width' => $content_max_width,
'document_title' => wp_get_document_title(),
'canonical_url' => get_permalink( $this->post ),
'home_url' => home_url( '/' ),
'blog_name' => get_bloginfo( 'name' ),
'html_tag_attributes' => [],
'body_class' => '',
/** This filter is documented in includes/amp-helper-functions.php */
'site_icon_url' => apply_filters( 'amp_site_icon_url', get_site_icon_url( self::SITE_ICON_SIZE ) ),
'placeholder_image_url' => amp_get_asset_url( 'images/placeholder-icon.png' ),
'featured_image' => false,
'comments_link_url' => false,
'comments_link_text' => false,
'amp_runtime_script' => 'https://cdn.ampproject.org/v0.js', // Deprecated.
'amp_component_scripts' => [], // Deprecated.
'customizer_settings' => [],
'font_urls' => [],
'post_amp_stylesheets' => [],
'post_amp_styles' => [], // Deprecated.
'amp_analytics' => amp_add_custom_analytics(),
];
$this->build_post_content();
$this->build_post_data();
$this->build_customizer_settings();
$this->build_html_tag_attributes();
/**
* Filters AMP template data.
*
* @since 0.2
*
* @param array $data Template data.
* @param WP_Post $post Post.
*/
$this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
}
/**
* Get template directory for Reader mode.
*
* @since 0.2
* @return string Template dir.
*/
private function get_template_dir() {
static $template_dir = null;
if ( ! isset( $template_dir ) ) {
/**
* Filters the Reader template directory.
*
* @since 0.3.3
*/
$template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );
}
return $template_dir;
}
/**
* Getter.
*
* @param string $property Property name.
* @param mixed $default Default value.
*
* @return mixed Value.
*/
public function get( $property, $default = null ) {
if ( ! isset( $this->data ) ) {
$this->set_data();
}
if ( isset( $this->data[ $property ] ) ) {
return $this->data[ $property ];
}
/* translators: %s is key name */
_doing_it_wrong( __METHOD__, esc_html( sprintf( __( 'Called for non-existent key ("%s").', 'amp' ), $property ) ), '0.1' );
return $default;
}
/**
* Get customizer setting.
*
* @param string $name Name.
* @param mixed $default Default value.
* @return mixed value.
*/
public function get_customizer_setting( $name, $default = null ) {
$settings = $this->get( 'customizer_settings' );
if ( ! empty( $settings[ $name ] ) ) {
return $settings[ $name ];
}
return $default;
}
/**
* Load and print the template parts for the given post.
*/
public function load() {
global $wp_query;
$template = is_page() || $wp_query->is_posts_page ? 'page' : 'single';
$this->load_parts( [ $template ] );
}
/**
* Load template parts.
*
* @param string[] $templates Templates.
*/
public function load_parts( $templates ) {
foreach ( $templates as $template ) {
$file = $this->get_template_path( $template );
$this->verify_and_include( $file, $template );
}
}
/**
* Get template path.
*
* @param string $template Template name.
* @return string Template path.
*/
private function get_template_path( $template ) {
return sprintf( '%s/%s.php', $this->get_template_dir(), $template );
}
/**
* Add data.
*
* @param array $data Data.
*/
private function add_data( $data ) {
$this->data = array_merge( $this->data, $data );
}
/**
* Add data by key.
*
* @param string $key Key.
* @param mixed $value Value.
*/
private function add_data_by_key( $key, $value ) {
$this->data[ $key ] = $value;
}
/**
* Build post data.
*
* @since 0.2
*/
private function build_post_data() {
$post_title = get_the_title( $this->post );
$post_publish_timestamp = $this->build_post_publish_timestamp();
$post_modified_timestamp = get_post_modified_time( 'U', false, $this->post );
$post_author = get_userdata( $this->post->post_author );
$data = [
'post' => $this->post,
'post_id' => $this->ID,
'post_title' => $post_title,
'post_publish_timestamp' => $post_publish_timestamp,
'post_modified_timestamp' => $post_modified_timestamp,
'post_author' => $post_author,
];
$this->add_data( $data );
$this->build_post_featured_image();
$this->build_post_comments_data();
}
/**
* Build post publish timestamp.
*
* We can't use `get_the_date( 'U' )` because it always returns the non-GMT value.
*
* @return int Post publish UTC timestamp.
*/
private function build_post_publish_timestamp() {
$format = 'U';
if ( empty( $this->post->post_date_gmt ) || '0000-00-00 00:00:00' === $this->post->post_date_gmt ) {
$timestamp = (int) get_the_time('U');
} else {
$timestamp = (int) get_post_time( $format, true, $this->post, true );
}
/** This filter is documented in wp-includes/general-template.php. */
$filtered_timestamp = apply_filters( 'get_the_date', $timestamp, $format, $this->post );
// Guard against a plugin poorly filtering get_the_date to be something other than a Unix timestamp.
if ( is_int( $filtered_timestamp ) ) {
$timestamp = $filtered_timestamp;
}
return $timestamp;
}
/**
* Build post comments data.
*/
private function build_post_comments_data() {
if ( ! post_type_supports( $this->post->post_type, 'comments' ) ) {
return;
}
$comments_open = comments_open( $this->post );
// Don't show link if close and no comments.
if ( ! $comments_open
&& ! $this->post->comment_count ) {
return;
}
$comments_link_url = get_comments_link( $this->post );
$comments_link_text = $comments_open
? __( 'Leave a Comment', 'amp' )
: __( 'View Comments', 'amp' );
$this->add_data(
[
'comments_link_url' => $comments_link_url,
'comments_link_text' => $comments_link_text,
]
);
}
/**
* Build post content.
*/
private function build_post_content() {
if ( post_password_required( $this->post ) ) {
$content = get_the_password_form( $this->post );
} else {
/**
* This filter is documented in wp-includes/post-template.php.
*
* Note: This is intentionally not using get_the_content() because the legacy behavior of posts in
* Reader mode is to display multi-page posts as a single page without any pagination links.
*/
$content = apply_filters( 'the_content', $this->post->post_content );
}
$this->add_data_by_key( 'post_amp_content', $content );
}
/**
* Build post featured image.
*/
private function build_post_featured_image() {
$post_id = $this->ID;
$featured_html = get_the_post_thumbnail( $post_id, 'large' );
// Skip featured image if no featured image is available.
if ( ! $featured_html ) {
return;
}
$featured_id = get_post_thumbnail_id( $post_id );
$featured_image = get_post( $featured_id );
if ( ! $featured_image ) {
return;
}
// If an image with the same ID as the featured image exists in the content, skip the featured image markup.
// Prevents duplicate images, which is especially problematic for photo blogs.
// A bit crude but it's fast and should cover most cases.
$post_content = $this->post->post_content;
if ( false !== strpos( $post_content, 'wp-image-' . $featured_id )
|| false !== strpos( $post_content, 'attachment_' . $featured_id ) ) {
return;
}
$this->add_data_by_key(
'featured_image',
[
'amp_html' => $featured_html,
'caption' => $featured_image->post_excerpt,
]
);
}
/**
* Build customizer settings.
*/
private function build_customizer_settings() {
$settings = AMP_Customizer_Settings::get_settings();
/**
* Filter AMP Customizer settings.
*
* Inject your Customizer settings here to make them accessible via the getter in your custom style.php template.
*
* Example:
*
* echo esc_html( $this->get_customizer_setting( 'your_setting_key', 'your_default_value' ) );
*
* @since 0.4
*
* @param array $settings Array of AMP Customizer settings.
* @param WP_Post $post Current post object.
*/
$this->add_data_by_key( 'customizer_settings', apply_filters( 'amp_post_template_customizer_settings', $settings, $this->post ) );
}
/**
* Build HTML tag attributes.
*/
private function build_html_tag_attributes() {
$attributes = [];
if ( function_exists( 'is_rtl' ) && is_rtl() ) {
$attributes['dir'] = 'rtl';
}
$lang = get_bloginfo( 'language' );
if ( $lang ) {
$attributes['lang'] = $lang;
}
$this->add_data_by_key( 'html_tag_attributes', $attributes );
}
/**
* Verify and include.
*
* @param string $file File.
* @param string $template_type Template type.
*/
private function verify_and_include( $file, $template_type ) {
$located_file = $this->locate_template( $file );
if ( $located_file ) {
$file = $located_file;
}
/**
* Filters the template file being loaded for a given template type.
*
* @since 0.2
*
* @param string $file Template file.
* @param string $template_type Template type.
* @param WP_Post $post Post.
*/
$file = apply_filters( 'amp_post_template_file', $file, $template_type, $this->post );
if ( ! $this->is_valid_template( $file ) ) {
/* translators: 1: the template file, 2: WP_CONTENT_DIR. */
_doing_it_wrong( __METHOD__, sprintf( esc_html__( 'Path validation for template (%1$s) failed. Path cannot traverse and must be located in `%2$s`.', 'amp' ), esc_html( $file ), 'WP_CONTENT_DIR' ), '0.1' );
return;
}
/**
* Fires before including a template.
*
* @since 0.4
*
* @param AMP_Post_Template $this Post template.
*/
do_action( "amp_post_template_include_{$template_type}", $this );
include $file;
}
/**
* Locate template.
*
* @param string $file File.
* @return string The template filename if one is located.
*/
private function locate_template( $file ) {
$search_file = sprintf( 'amp/%s', basename( $file ) );
return locate_template( [ $search_file ], false );
}
/**
* Is valid template.
*
* @param string $template Template name.
* @return bool Whether valid.
*/
private function is_valid_template( $template ) {
if ( false !== strpos( $template, '..' ) ) {
return false;
}
if ( false !== strpos( $template, './' ) ) {
return false;
}
if ( ! file_exists( $template ) ) {
return false;
}
return true;
}
}