-
Notifications
You must be signed in to change notification settings - Fork 385
/
amp-helper-functions.php
850 lines (765 loc) · 25.7 KB
/
amp-helper-functions.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
<?php
/**
* AMP Helper Functions
*
* @package AMP
*/
/**
* Get the slug used in AMP for the query var, endpoint, and post type support.
*
* This function always returns 'amp' when 'amp' theme support is present. Otherwise,
* the return value can be overridden by previously defining a AMP_QUERY_VAR
* constant or by adding a 'amp_query_var' filter, but *warning* this ability
* may be deprecated in the future. Normally the slug should be just 'amp'.
*
* @since 0.7
* @since 1.0 The return value is always 'amp' when 'amp' theme support is present, and the 'amp_query_var' filter no longer applies.
*
* @return string Slug used for query var, endpoint, and post type support.
*/
function amp_get_slug() {
if ( current_theme_supports( 'amp' ) ) {
if ( ! defined( 'AMP_QUERY_VAR' ) ) {
define( 'AMP_QUERY_VAR', 'amp' );
} elseif ( 'amp' !== AMP_QUERY_VAR ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'The AMP_QUERY_VAR constant should only be defined as "amp" when "amp" theme support is present.', 'amp' ), '1.0' );
}
return 'amp';
}
if ( defined( 'AMP_QUERY_VAR' ) ) {
return AMP_QUERY_VAR;
}
/**
* Filter the AMP query variable.
*
* Warning: This filter may become deprecated.
*
* @since 0.3.2
* @since 1.0 This filter does not apply when 'amp' theme support is present.
*
* @param string $query_var The AMP query variable.
*/
$query_var = apply_filters( 'amp_query_var', 'amp' );
define( 'AMP_QUERY_VAR', $query_var );
return $query_var;
}
/**
* Get the URL for the current request.
*
* This is essentially the REQUEST_URI prefixed by the scheme and host for the home URL.
* This is needed in particular due to subdirectory installs.
*
* @since 1.0
*
* @return string Current URL.
*/
function amp_get_current_url() {
$url = preg_replace( '#(^https?://[^/]+)/.*#', '$1', home_url( '/' ) );
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$url = esc_url_raw( $url . wp_unslash( $_SERVER['REQUEST_URI'] ) );
} else {
$url .= '/';
}
return $url;
}
/**
* Retrieves the full AMP-specific permalink for the given post ID.
*
* @since 0.1
* @since 1.0 The query var 'amp' is always used exclusively when 'amp' theme support is present; the 'amp_pre_get_permalink' and 'amp_get_permalink' filters do not apply.
*
* @param int $post_id Post ID.
*
* @return string AMP permalink.
*/
function amp_get_permalink( $post_id ) {
// When theme support is present, the plain query var should always be used.
if ( current_theme_supports( 'amp' ) ) {
$permalink = get_permalink( $post_id );
if ( ! amp_is_canonical() ) {
$permalink = add_query_arg( 'amp', '', $permalink );
}
return $permalink;
}
/**
* Filters the AMP permalink to short-circuit normal generation.
*
* Returning a non-false value in this filter will cause the `get_permalink()` to get called and the `amp_get_permalink` filter to not apply.
*
* @since 0.4
* @since 1.0 This filter does not apply when 'amp' theme support is present.
*
* @param false $url Short-circuited URL.
* @param int $post_id Post ID.
*/
$pre_url = apply_filters( 'amp_pre_get_permalink', false, $post_id );
if ( false !== $pre_url ) {
return $pre_url;
}
$permalink = get_permalink( $post_id );
if ( amp_is_canonical() ) {
$amp_url = $permalink;
} else {
$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
$use_query_var = (
// If pretty permalinks aren't available, then query var must be used.
empty( $structure )
||
// If there are existing query vars, then always use the amp query var as well.
! empty( $parsed_url['query'] )
||
// If the post type is hierarchical then the /amp/ endpoint isn't available.
is_post_type_hierarchical( get_post_type( $post_id ) )
);
if ( $use_query_var ) {
$amp_url = add_query_arg( amp_get_slug(), '', $permalink );
} else {
$amp_url = preg_replace( '/#.*/', '', $permalink );
$amp_url = trailingslashit( $amp_url ) . user_trailingslashit( amp_get_slug(), 'single_amp' );
if ( ! empty( $parsed_url['fragment'] ) ) {
$amp_url .= '#' . $parsed_url['fragment'];
}
}
}
/**
* Filters AMP permalink.
*
* @since 0.2
* @since 1.0 This filter does not apply when 'amp' theme support is present.
*
* @param false $amp_url AMP URL.
* @param int $post_id Post ID.
*/
return apply_filters( 'amp_get_permalink', $amp_url, $post_id );
}
/**
* Remove the AMP endpoint (and query var) from a given URL.
*
* @since 0.7
*
* @param string $url URL.
* @return string URL with AMP stripped.
*/
function amp_remove_endpoint( $url ) {
// Strip endpoint.
$url = preg_replace( ':/' . preg_quote( amp_get_slug(), ':' ) . '(?=/?(\?|#|$)):', '', $url );
// Strip query var.
$url = remove_query_arg( amp_get_slug(), $url );
return $url;
}
/**
* Add amphtml link.
*
* If there are known validation errors for the current URL then do not output anything.
*
* @since 1.0
*/
function amp_add_amphtml_link() {
/**
* Filters whether to show the amphtml link on the frontend.
*
* @todo This filter's name is incorrect. It's not about adding a canonical link but adding the amphtml link.
* @since 0.2
*/
if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) {
return;
}
$current_url = amp_get_current_url();
$amp_url = null;
if ( current_theme_supports( 'amp' ) ) {
if ( AMP_Theme_Support::is_paired_available() ) {
$amp_url = add_query_arg( amp_get_slug(), '', $current_url );
}
} else {
if ( is_singular() ) {
$amp_url = amp_get_permalink( get_queried_object_id() );
} else {
$amp_url = add_query_arg( amp_get_slug(), '', $current_url );
}
}
if ( ! $amp_url ) {
printf( '<!-- %s -->', esc_html__( 'There is no amphtml version available for this URL.', 'amp' ) );
return;
}
// Check to see if there are known unaccepted validation errors for this URL.
if ( current_theme_supports( 'amp' ) ) {
$validation_errors = AMP_Invalid_URL_Post_Type::get_invalid_url_validation_errors( $current_url, array( 'ignore_accepted' => true ) );
$error_count = count( $validation_errors );
if ( $error_count > 0 ) {
echo "<!--\n";
echo esc_html( sprintf(
/* translators: %s is error count */
_n(
'There is %s validation error that is blocking the amphtml version from being available.',
'There are %s validation errors that are blocking the amphtml version from being available.',
$error_count,
'amp'
),
number_format_i18n( $error_count )
) );
echo "\n-->";
return;
}
}
if ( $amp_url ) {
printf( '<link rel="amphtml" href="%s">', esc_url( $amp_url ) );
}
}
/**
* Determine whether a given post supports AMP.
*
* @since 0.1
* @since 0.6 Returns false when post has meta to disable AMP.
* @see AMP_Post_Type_Support::get_support_errors()
*
* @param WP_Post $post Post.
*
* @return bool Whether the post supports AMP.
*/
function post_supports_amp( $post ) {
if ( amp_is_canonical() ) {
return true;
}
$errors = AMP_Post_Type_Support::get_support_errors( $post );
// Return false if an error is found.
if ( ! empty( $errors ) ) {
return false;
}
switch ( get_post_meta( $post->ID, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) ) {
case AMP_Post_Meta_Box::ENABLED_STATUS:
return true;
case AMP_Post_Meta_Box::DISABLED_STATUS:
return false;
// Disabled by default for custom page templates, page on front and page for posts.
default:
$enabled = (
! (bool) get_page_template_slug( $post )
&&
! (
'page' === $post->post_type
&&
'page' === get_option( 'show_on_front' )
&&
in_array( (int) $post->ID, array(
(int) get_option( 'page_on_front' ),
(int) get_option( 'page_for_posts' ),
), true )
)
);
/**
* Filters whether default AMP status should be enabled or not.
*
* @since 0.6
*
* @param string $status Status.
* @param WP_Post $post Post.
*/
return apply_filters( 'amp_post_status_default_enabled', $enabled, $post );
}
}
/**
* Are we currently on an AMP URL?
*
* @since 1.0 This function can be called before the `parse_query` action because the 'amp' query var is specifically and exclusively used when 'amp' theme support is added.
*
* @return bool Whether it is the AMP endpoint.
*/
function is_amp_endpoint() {
if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return false;
}
// When 'amp' theme support is (or will be added) then these are the conditions that are key to be checked.
if ( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ) { // WPCS: CSRF OK.
return true;
}
// Condition for non-theme support when /amp/ endpoint is used.
if ( false !== get_query_var( amp_get_slug(), false ) ) {
return true;
}
return false;
}
/**
* Get AMP asset URL.
*
* @param string $file Relative path to file in assets directory.
* @return string URL.
*/
function amp_get_asset_url( $file ) {
return plugins_url( sprintf( 'assets/%s', $file ), AMP__FILE__ );
}
/**
* Get AMP boilerplate code.
*
* @since 0.7
* @link https://www.ampproject.org/docs/reference/spec#boilerplate
*
* @return string Boilerplate code.
*/
function amp_get_boilerplate_code() {
return '<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>'
. '<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>';
}
/**
* Register default scripts for AMP components.
*
* @param WP_Scripts $wp_scripts Scripts.
*/
function amp_register_default_scripts( $wp_scripts ) {
// AMP Runtime.
$handle = 'amp-runtime';
$wp_scripts->add(
$handle,
'https://cdn.ampproject.org/v0.js',
array(),
null
);
$wp_scripts->add_data( $handle, 'amp_script_attributes', array(
'async' => true,
) );
// Shadow AMP API.
$handle = 'amp-shadow';
$wp_scripts->add(
$handle,
'https://cdn.ampproject.org/shadow-v0.js',
array(),
null
);
$wp_scripts->add_data( $handle, 'amp_script_attributes', array(
'async' => true,
) );
// Get all AMP components as defined in the spec.
$extensions = array();
foreach ( AMP_Allowed_Tags_Generated::get_allowed_tags() as $allowed_tag ) {
foreach ( $allowed_tag as $rule_spec ) {
if ( ! empty( $rule_spec[ AMP_Rule_Spec::TAG_SPEC ]['requires_extension'] ) ) {
$extensions = array_merge(
$extensions,
$rule_spec[ AMP_Rule_Spec::TAG_SPEC ]['requires_extension']
);
}
}
}
$extensions = array_unique( $extensions );
foreach ( $extensions as $extension ) {
$src = sprintf(
'https://cdn.ampproject.org/v0/%s-%s.js',
$extension,
'latest'
);
$wp_scripts->add(
$extension,
$src,
array( 'amp-runtime' ),
null
);
}
}
/**
* Add AMP script attributes to enqueued scripts.
*
* @link https://core.trac.wordpress.org/ticket/12009
* @since 0.7
*
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string Script loader tag.
*/
function amp_filter_script_loader_tag( $tag, $handle ) {
$prefix = 'https://cdn.ampproject.org/';
$src = wp_scripts()->registered[ $handle ]->src;
if ( 0 !== strpos( $src, $prefix ) ) {
return $tag;
}
/*
* All scripts from AMP CDN should be loaded async.
* See <https://www.ampproject.org/docs/integration/pwa-amp/amp-in-pwa#include-"shadow-amp"-in-your-progressive-web-app>.
*/
$attributes = array(
'async' => true,
);
// Add custom-template and custom-element attributes. All component scripts look like https://cdn.ampproject.org/v0/:name-:version.js.
if ( 'v0' === strtok( substr( $src, strlen( $prefix ) ), '/' ) ) {
/*
* Per the spec, "Most extensions are custom-elements." In fact, there is only one custom template. So we hard-code it here.
*
* @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/validator/validator.proto#L326-L328
* @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/extensions/amp-mustache/validator-amp-mustache.protoascii#L27
*/
if ( 'amp-mustache' === $handle ) {
$attributes['custom-template'] = $handle;
} else {
$attributes['custom-element'] = $handle;
}
}
// Add each attribute (if it hasn't already been added).
foreach ( $attributes as $key => $value ) {
if ( ! preg_match( ":\s$key(=|>|\s):", $tag ) ) {
if ( true === $value ) {
$attribute_string = sprintf( ' %s', esc_attr( $key ) );
} else {
$attribute_string = sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
}
$tag = preg_replace(
':(?=></script>):',
$attribute_string,
$tag,
1
);
}
}
return $tag;
}
/**
* Retrieve analytics data added in backend.
*
* @since 0.7
*
* @param array $analytics Analytics entries.
* @return array Analytics.
*/
function amp_get_analytics( $analytics = array() ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );
/**
* Add amp-analytics tags.
*
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
* This filter should be used to alter entries for paired mode.
*
* @since 0.7
*
* @param array $analytics_entries An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details.
*/
$analytics_entries = apply_filters( 'amp_analytics_entries', $analytics_entries );
if ( ! $analytics_entries ) {
return $analytics;
}
foreach ( $analytics_entries as $entry_id => $entry ) {
$analytics[ $entry_id ] = array(
'type' => $entry['type'],
'attributes' => array(),
'config_data' => json_decode( $entry['config'] ),
);
}
return $analytics;
}
/**
* Print analytics data.
*
* @since 0.7
*
* @param array|string $analytics Analytics entries, or empty string when called via wp_footer action.
*/
function amp_print_analytics( $analytics ) {
if ( '' === $analytics ) {
$analytics = array();
}
$analytics_entries = amp_get_analytics( $analytics );
if ( empty( $analytics_entries ) ) {
return;
}
// Can enter multiple configs within backend.
foreach ( $analytics_entries as $id => $analytics_entry ) {
if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) {
/* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' );
continue;
}
$script_element = AMP_HTML_Utils::build_tag( 'script', array(
'type' => 'application/json',
), wp_json_encode( $analytics_entry['config_data'] ) );
$amp_analytics_attr = array_merge( array(
'id' => $id,
'type' => $analytics_entry['type'],
), $analytics_entry['attributes'] );
echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK.
}
}
/**
* Get content embed handlers.
*
* @since 0.7
*
* @param WP_Post $post Post that the content belongs to. Deprecated when theme supports AMP, as embeds may apply
* to non-post data (e.g. Text widget).
* @return array Embed handlers.
*/
function amp_get_content_embed_handlers( $post = null ) {
if ( current_theme_supports( 'amp' ) && $post ) {
_deprecated_argument( __FUNCTION__, '0.7', esc_html__( 'The $post argument is deprecated when theme supports AMP.', 'amp' ) );
$post = null;
}
/**
* Filters the content embed handlers.
*
* @since 0.2
* @since 0.7 Deprecated $post parameter.
*
* @param array $handlers Handlers.
* @param WP_Post $post Post. Deprecated. It will be null when `amp_is_canonical()`.
*/
return apply_filters( 'amp_content_embed_handlers',
array(
'AMP_Core_Block_Handler' => array(),
'AMP_Twitter_Embed_Handler' => array(),
'AMP_YouTube_Embed_Handler' => array(),
'AMP_DailyMotion_Embed_Handler' => array(),
'AMP_Vimeo_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Issuu_Embed_Handler' => array(),
'AMP_Meetup_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Playlist_Embed_Handler' => array(),
'AMP_Reddit_Embed_Handler' => array(),
'AMP_Tumblr_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
'AMP_Gfycat_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
),
$post
);
}
/**
* Get content sanitizers.
*
* @since 0.7
*
* @param WP_Post $post Post that the content belongs to. Deprecated when theme supports AMP, as sanitizers apply
* to non-post data (e.g. Text widget).
* @return array Embed handlers.
*/
function amp_get_content_sanitizers( $post = null ) {
if ( current_theme_supports( 'amp' ) && $post ) {
_deprecated_argument( __FUNCTION__, '0.7', esc_html__( 'The $post argument is deprecated when theme supports AMP.', 'amp' ) );
$post = null;
}
/**
* Filters the content sanitizers.
*
* @since 0.2
* @since 0.7 Deprecated $post parameter. It will be null when `amp_is_canonical()`.
*
* @param array $handlers Handlers.
* @param WP_Post $post Post. Deprecated.
*/
$sanitizers = apply_filters( 'amp_content_sanitizers',
array(
'AMP_Core_Theme_Sanitizer' => array(
'template' => get_template(),
'stylesheet' => get_stylesheet(),
),
'AMP_Img_Sanitizer' => array(),
'AMP_Form_Sanitizer' => array(),
'AMP_Comments_Sanitizer' => array(),
'AMP_Video_Sanitizer' => array(),
'AMP_Audio_Sanitizer' => array(),
'AMP_Playbuzz_Sanitizer' => array(),
'AMP_Iframe_Sanitizer' => array(
'add_placeholder' => true,
),
'AMP_Gallery_Block_Sanitizer' => array( // Note: Gallery block sanitizer must come after image sanitizers since itś logic is using the already sanitized images.
'carousel_required' => ! current_theme_supports( 'amp' ), // For back-compat.
),
'AMP_Block_Sanitizer' => array(), // Note: Block sanitizer must come after embed / media sanitizers since it's logic is using the already sanitized content.
'AMP_Style_Sanitizer' => array(),
'AMP_Tag_And_Attribute_Sanitizer' => array(), // Note: This whitelist sanitizer must come at the end to clean up any remaining issues the other sanitizers didn't catch.
),
$post
);
// Force style sanitizer and whitelist sanitizer to be at end.
foreach ( array( 'AMP_Style_Sanitizer', 'AMP_Tag_And_Attribute_Sanitizer' ) as $class_name ) {
if ( isset( $sanitizers[ $class_name ] ) ) {
$sanitizer = $sanitizers[ $class_name ];
unset( $sanitizers[ $class_name ] );
$sanitizers[ $class_name ] = $sanitizer;
}
}
return $sanitizers;
}
/**
* Grabs featured image or the first attached image for the post.
*
* @since 0.7 This originally was located in the private method AMP_Post_Template::get_post_image_metadata().
*
* @param WP_Post|int $post Post or post ID.
* @return array|false $post_image_meta Post image metadata, or false if not found.
*/
function amp_get_post_image_metadata( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$post_image_meta = null;
$post_image_id = false;
if ( has_post_thumbnail( $post->ID ) ) {
$post_image_id = get_post_thumbnail_id( $post->ID );
} else {
$attached_image_ids = get_posts(
array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'fields' => 'ids',
'suppress_filters' => false,
)
);
if ( ! empty( $attached_image_ids ) ) {
$post_image_id = array_shift( $attached_image_ids );
}
}
if ( ! $post_image_id ) {
return false;
}
$post_image_src = wp_get_attachment_image_src( $post_image_id, 'full' );
if ( is_array( $post_image_src ) ) {
$post_image_meta = array(
'@type' => 'ImageObject',
'url' => $post_image_src[0],
'width' => $post_image_src[1],
'height' => $post_image_src[2],
);
}
return $post_image_meta;
}
/**
* Get schema.org metadata for the current query.
*
* @since 0.7
* @see AMP_Post_Template::build_post_data() Where the logic in this function originally existed.
*
* @return array $metadata All schema.org metadata for the post.
*/
function amp_get_schemaorg_metadata() {
$metadata = array(
'@context' => 'http://schema.org',
'publisher' => array(
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
),
);
/*
* "The logo should be a rectangle, not a square. The logo should fit in a 60x600px rectangle.,
* and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px
* would not be acceptable, even though it fits in the 600x60px rectangle."
* See <https://developers.google.com/search/docs/data-types/article#logo-guidelines>.
*/
$max_logo_width = 600;
$max_logo_height = 60;
$custom_logo_id = get_theme_mod( 'custom_logo' );
$schema_img = array();
if ( has_custom_logo() && $custom_logo_id ) {
$custom_logo_img = wp_get_attachment_image_src( $custom_logo_id, array( $max_logo_width, $max_logo_height ), false );
if ( $custom_logo_img ) {
// @todo Warning: The width/height returned may not actually be physically the $max_logo_width and $max_logo_height for the image returned.
$schema_img = array(
'url' => $custom_logo_img[0],
'width' => $custom_logo_img[1],
'height' => $custom_logo_img[2],
);
}
}
// Try Site Icon, though it is not ideal because "The logo should be a rectangle, not a square." per <https://developers.google.com/search/docs/data-types/article#logo-guidelines>.
if ( empty( $schema_img['url'] ) ) {
/*
* Note that AMP_Post_Template::SITE_ICON_SIZE is used and not $max_logo_height because 32px is the largest
* size that is defined in \WP_Site_Icon::$site_icon_sizes which is less than 60px. It may be a good idea
* to add a site_icon_image_sizes filter which appends 60 to the list of sizes, but this will only help
* when adding a new site icon and it would be irrelevant when a custom logo is present, per above.
*/
$schema_img = array(
'url' => get_site_icon_url( AMP_Post_Template::SITE_ICON_SIZE ),
'width' => AMP_Post_Template::SITE_ICON_SIZE,
'height' => AMP_Post_Template::SITE_ICON_SIZE,
);
}
/**
* Filters the publisher logo URL in the schema.org data.
*
* Previously, this only filtered the Site Icon, as that was the only possible schema.org publisher logo.
* But the Custom Logo is now the preferred publisher logo, if it exists and its dimensions aren't too big.
*
* @since 0.3
*
* @param string $schema_img_url URL of the publisher logo, either the Custom Logo or the Site Icon.
*/
$filtered_schema_img_url = apply_filters( 'amp_site_icon_url', $schema_img['url'] );
if ( $filtered_schema_img_url !== $schema_img['url'] ) {
$schema_img['url'] = $filtered_schema_img_url;
unset( $schema_img['width'], $schema_img['height'] ); // Clear width/height since now unknown, and not required.
}
if ( ! empty( $schema_img['url'] ) ) {
$metadata['publisher']['logo'] = array_merge(
array(
'@type' => 'ImageObject',
),
$schema_img
);
}
$post = get_queried_object();
if ( $post instanceof WP_Post ) {
$metadata = array_merge(
$metadata,
array(
'@type' => is_page() ? 'WebPage' : 'BlogPosting',
'mainEntityOfPage' => get_permalink(),
'headline' => get_the_title(),
'datePublished' => date( 'c', get_the_date( 'U', $post->ID ) ),
'dateModified' => date( 'c', get_the_date( 'U', $post->ID ) ),
)
);
$post_author = get_userdata( $post->post_author );
if ( $post_author ) {
$metadata['author'] = array(
'@type' => 'Person',
'name' => html_entity_decode( $post_author->display_name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
);
}
$image_metadata = amp_get_post_image_metadata( $post );
if ( $image_metadata ) {
$metadata['image'] = $image_metadata;
}
/**
* Filters Schema.org metadata for a post.
*
* The 'post_template' in the filter name here is due to this filter originally being introduced in `AMP_Post_Template`.
* In general the `amp_schemaorg_metadata` filter should be used instead.
*
* @since 0.3
*
* @param array $metadata Metadata.
* @param WP_Post $post Post.
*/
$metadata = apply_filters( 'amp_post_template_metadata', $metadata, $post );
}
/**
* Filters Schema.org metadata for a query.
*
* Check the the main query for the context for which metadata should be added.
*
* @since 0.7
*
* @param array $metadata Metadata.
*/
$metadata = apply_filters( 'amp_schemaorg_metadata', $metadata );
return $metadata;
}
/**
* Output schema.org metadata.
*
* @since 0.7
*/
function amp_print_schemaorg_metadata() {
$metadata = amp_get_schemaorg_metadata();
if ( empty( $metadata ) ) {
return;
}
?>
<script type="application/ld+json"><?php echo wp_json_encode( $metadata ); ?></script>
<?php
}