-
Notifications
You must be signed in to change notification settings - Fork 385
/
class-amp-core-theme-sanitizer.php
1923 lines (1752 loc) · 63 KB
/
class-amp-core-theme-sanitizer.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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class AMP_Core_Theme_Sanitizer.
*
* @package AMP
* @since 1.0
*/
use Amp\AmpWP\Dom\Document;
/**
* Class AMP_Core_Theme_Sanitizer
*
* Fixes up common issues in core themes and others.
*
* @see AMP_Validation_Error_Taxonomy::accept_core_theme_validation_errors()
* @since 1.0
*/
class AMP_Core_Theme_Sanitizer extends AMP_Base_Sanitizer {
/**
* Array of flags used to control sanitization.
*
* @since 1.0
* @var array {
* @type string $stylesheet Stylesheet slug.
* @type string $template Template slug.
* @type array $theme_features List of theme features that need to be applied. Features are method names,
* }
*/
protected $args;
/**
* Array of themes that are supported.
*
* @var array
*/
protected static $supported_themes = [
'twentytwenty',
'twentynineteen',
'twentyseventeen',
'twentysixteen',
'twentyfifteen',
'twentyfourteen',
'twentythirteen',
'twentytwelve',
'twentyeleven',
'twentyten',
];
/**
* Retrieve the config for features needed by a theme.
*
* @since 1.0
* @since 1.5.0 Converted `theme_features` variable into `get_theme_config` function.
*
* @param string $theme_slug Theme slug.
* @return array|null Array comprising of the theme config if its slug is found, null if it is not.
*/
protected static function get_theme_features_config( $theme_slug ) {
switch ( $theme_slug ) {
// Twenty Twenty.
case 'twentytwenty':
$config = [
'dequeue_scripts' => [
'twentytwenty-js',
],
'remove_actions' => [
'wp_head' => [
'twentytwenty_no_js_class', // AMP is essentially no-js, with any interactivity added explicitly via amp-bind.
],
'wp_print_footer_scripts' => [
'twentytwenty_skip_link_focus_fix', // See <https://github.com/WordPress/twentynineteen/pull/47>.
],
],
'add_twentytwenty_modals' => [],
'add_twentytwenty_toggles' => [],
'add_nav_menu_styles' => [],
'add_twentytwenty_masthead_styles' => [],
'add_twentytwenty_current_page_awareness' => [],
];
$theme = wp_get_theme( 'twentytwenty' );
if ( $theme->exists() && version_compare( $theme->get( 'Version' ), '1.0.0', '<=' ) ) {
$config['add_smooth_scrolling'] = [
'//a[ starts-with( @href, "#" ) and not( @href = "#" )and not( @href = "#0" ) and not( contains( @class, "do-not-scroll" ) ) and not( contains( @class, "skip-link" ) ) ]',
];
}
return $config;
// Twenty Nineteen.
case 'twentynineteen':
return [
'dequeue_scripts' => [
'twentynineteen-skip-link-focus-fix', // This is part of AMP. See <https://github.com/ampproject/amphtml/issues/18671>.
'twentynineteen-priority-menu',
'twentynineteen-touch-navigation', // @todo There could be an AMP implementation of this, similar to what is implemented on ampproject.org.
],
'remove_actions' => [
'wp_print_footer_scripts' => [
'twentynineteen_skip_link_focus_fix', // See <https://github.com/WordPress/twentynineteen/pull/47>.
],
],
'add_twentynineteen_masthead_styles' => [],
'adjust_twentynineteen_images' => [],
];
// Twenty Seventeen.
case 'twentyseventeen':
return [
// @todo Try to implement belowEntryMetaClass().
'dequeue_scripts' => [
'twentyseventeen-html5', // Only relevant for IE<9.
'twentyseventeen-global', // There are somethings not yet implemented in AMP. See todos below.
'jquery-scrollto', // Implemented via add_smooth_scrolling().
'twentyseventeen-navigation', // Handled by add_nav_menu_styles, add_nav_menu_toggle, add_nav_sub_menu_buttons.
'twentyseventeen-skip-link-focus-fix', // Unnecessary since part of the AMP runtime.
],
'remove_actions' => [
'wp_head' => [
'twentyseventeen_javascript_detection', // AMP is essentially no-js, with any interactivity added explicitly via amp-bind.
],
],
'force_fixed_background_support' => [],
'add_twentyseventeen_masthead_styles' => [],
'add_twentyseventeen_image_styles' => [],
'add_twentyseventeen_sticky_nav_menu' => [],
'add_has_header_video_body_class' => [],
'add_nav_menu_styles' => [
'sub_menu_button_toggle_class' => 'toggled-on',
'no_js_submenu_visible' => true,
],
'add_smooth_scrolling' => [
'//header[@id = "masthead"]//a[ contains( @class, "menu-scroll-down" ) ]',
],
'set_twentyseventeen_quotes_icon' => [],
'add_twentyseventeen_attachment_image_attributes' => [],
];
// Twenty Sixteen.
case 'twentysixteen':
return [
// @todo Figure out an AMP solution for onResizeARIA().
// @todo Try to implement belowEntryMetaClass().
'dequeue_scripts' => [
'twentysixteen-script',
'twentysixteen-html5', // Only relevant for IE<9.
'twentysixteen-keyboard-image-navigation', // AMP does not yet allow for listening to keydown events.
'twentysixteen-skip-link-focus-fix', // Unnecessary since part of the AMP runtime.
],
'remove_actions' => [
'wp_head' => [
'twentysixteen_javascript_detection', // AMP is essentially no-js, with any interactivity added explicitly via amp-bind.
],
],
'add_nav_menu_styles' => [
'sub_menu_button_toggle_class' => 'toggled-on',
'no_js_submenu_visible' => true,
],
];
// Twenty Fifteen.
case 'twentyfifteen':
return [
// @todo Figure out an AMP solution for onResizeARIA().
'dequeue_scripts' => [
'twentyfifteen-script',
'twentyfifteen-keyboard-image-navigation', // AMP does not yet allow for listening to keydown events.
'twentyfifteen-skip-link-focus-fix', // Unnecessary since part of the AMP runtime.
],
'remove_actions' => [
'wp_head' => [
'twentyfifteen_javascript_detection', // AMP is essentially no-js, with any interactivity added explicitly via amp-bind.
],
],
'add_nav_menu_styles' => [
'sub_menu_button_toggle_class' => 'toggle-on',
'no_js_submenu_visible' => true,
],
];
// Twenty Fourteen.
case 'twentyfourteen':
return [
// @todo Figure out an AMP solution for onResizeARIA().
'dequeue_scripts' => [
'twentyfourteen-script',
'twentyfourteen-keyboard-image-navigation', // AMP does not yet allow for listening to keydown events.
'jquery-masonry', // Masonry style layout is not supported in AMP.
'twentyfourteen-slider',
],
'add_nav_menu_styles' => [],
'add_twentyfourteen_masthead_styles' => [],
'add_twentyfourteen_slider_carousel' => [],
'add_twentyfourteen_search' => [],
];
// Twenty Thirteen.
case 'twentythirteen':
return [
'dequeue_scripts' => [
'jquery-masonry', // Masonry style layout is not supported in AMP.
'twentythirteen-script',
],
'add_nav_menu_toggle' => [],
'add_nav_sub_menu_buttons' => [],
'add_nav_menu_styles' => [],
];
// Twenty Twelve.
case 'twentytwelve':
return [
'dequeue_scripts' => [
'twentytwelve-navigation',
],
'add_nav_menu_styles' => [],
];
// Twenty Eleven.
case 'twentyeleven':
// Twenty Ten.
case 'twentyten':
return [];
default:
return null;
}
}
/**
* Get list of supported core themes.
*
* @since 1.0
*
* @return string[] Slugs for supported themes.
*/
public static function get_supported_themes() {
return self::$supported_themes;
}
/**
* Get the acceptable validation errors.
*
* @since 1.0
*
* @param string $template Template.
* @return array Acceptable errors.
*/
public static function get_acceptable_errors( $template ) {
if ( in_array( $template, self::$supported_themes, true ) ) {
return [
AMP_Style_Sanitizer::CSS_SYNTAX_INVALID_AT_RULE => [
[
'at_rule' => 'viewport',
],
[
'at_rule' => '-ms-viewport',
],
],
];
}
return [];
}
/**
* Adds extra theme support arguments on the fly.
*
* This method is neither a buffering hook nor a sanitization callback and is called manually by
* {@see AMP_Theme_Support}. Typically themes will add theme support directly and don't need such
* a method. In this case, it is a workaround for adding theme support on behalf of external themes.
*
* @since 1.1
*/
public static function extend_theme_support() {
$args = self::get_theme_support_args( get_template() );
if ( empty( $args ) ) {
return;
}
$support = AMP_Theme_Support::get_theme_support_args();
if ( ! is_array( $support ) ) {
$support = [];
}
add_theme_support( AMP_Theme_Support::SLUG, array_merge( $support, $args ) );
}
/**
* Returns extra arguments to pass to `add_theme_support()`.
*
* @since 1.1
*
* @param string $theme Theme slug.
* @return array Arguments to merge with existing theme support arguments.
*/
protected static function get_theme_support_args( $theme ) {
// phpcs:disable WordPress.WP.I18n.TextDomainMismatch
switch ( $theme ) {
case 'twentytwelve':
return [
'nav_menu_toggle' => [
'nav_container_xpath' => '//nav[ @id = "site-navigation" ]//ul',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//nav[ @id = "site-navigation" ]//button[ contains( @class, "menu-toggle" ) ]',
'menu_button_toggle_class' => 'toggled-on',
],
];
case 'twentythirteen':
return [
'nav_menu_toggle' => [
'nav_container_id' => 'site-navigation',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//nav[ @id = "site-navigation" ]//button[ contains( @class, "menu-toggle" ) ]',
],
'nav_menu_dropdown' => [
'sub_menu_button_class' => 'dropdown-toggle',
'sub_menu_button_toggle_class' => 'toggle-on',
'expand_text' => __( 'expand child menu', 'amp' ),
'collapse_text' => __( 'collapse child menu', 'amp' ),
],
'nav_menu_styles' => [],
];
case 'twentyfourteen':
return [
'nav_menu_toggle' => [
'nav_container_id' => 'primary-navigation',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//header[ @id = "masthead" ]//button[ contains( @class, "menu-toggle" ) ]',
'menu_button_toggle_class' => '',
],
];
case 'twentyfifteen':
return [
'nav_menu_toggle' => [
'nav_container_id' => 'secondary',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//header[ @id = "masthead" ]//button[ contains( @class, "secondary-toggle" ) ]',
'menu_button_toggle_class' => 'toggled-on',
],
'nav_menu_dropdown' => [
'sub_menu_button_class' => 'dropdown-toggle',
'sub_menu_button_toggle_class' => 'toggle-on',
'expand_text ' => __( 'expand child menu', 'twentyfifteen' ),
'collapse_text' => __( 'collapse child menu', 'twentyfifteen' ),
],
];
case 'twentysixteen':
return [
'nav_menu_toggle' => [
'nav_container_id' => 'site-header-menu',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//header[@id = "masthead"]//button[ @id = "menu-toggle" ]',
'menu_button_toggle_class' => 'toggled-on',
],
'nav_menu_dropdown' => [
'sub_menu_button_class' => 'dropdown-toggle',
'sub_menu_button_toggle_class' => 'toggled-on',
'expand_text ' => __( 'expand child menu', 'twentysixteen' ),
'collapse_text' => __( 'collapse child menu', 'twentysixteen' ),
],
];
case 'twentyseventeen':
$config = [
'nav_menu_toggle' => [
'nav_container_id' => 'site-navigation',
'nav_container_toggle_class' => 'toggled-on',
'menu_button_xpath' => '//nav[@id = "site-navigation"]//button[ contains( @class, "menu-toggle" ) ]',
'menu_button_toggle_class' => 'toggled-on',
],
'nav_menu_dropdown' => [
'sub_menu_button_class' => 'dropdown-toggle',
'sub_menu_button_toggle_class' => 'toggled-on',
'expand_text ' => __( 'expand child menu', 'twentyseventeen' ),
'collapse_text' => __( 'collapse child menu', 'twentyseventeen' ),
],
];
if ( function_exists( 'twentyseventeen_get_svg' ) ) {
$config['nav_menu_dropdown']['icon'] = twentyseventeen_get_svg(
[
'icon' => 'angle-down',
'fallback' => true,
]
);
}
return $config;
}
// phpcs:enable WordPress.WP.I18n.TextDomainMismatch
return [];
}
/**
* Get theme config.
*
* @since 1.0
* @deprecated 1.1
*
* @param string $theme Theme slug.
* @return array Class names.
*/
protected static function get_theme_config( $theme ) {
_deprecated_function( __METHOD__, '1.1' );
$args = self::get_theme_support_args( $theme );
// This returns arguments in a backward-compatible way.
return array_merge( $args['nav_menu_toggle'], $args['nav_menu_dropdown'] );
}
/**
* Find theme features for core theme.
*
* @since 1.0
*
* @param array $args Args.
* @param bool $static Static. that is, whether should run during output buffering.
* @return array Theme features.
*/
protected static function get_theme_features( $args, $static = false ) {
$theme_features = [];
$theme_candidates = wp_array_slice_assoc( $args, [ 'stylesheet', 'template' ] );
foreach ( $theme_candidates as $theme_candidate ) {
if ( in_array( $theme_candidate, self::$supported_themes, true ) ) {
$theme_features = self::get_theme_features_config( $theme_candidate );
break;
}
}
// Allow specific theme features to be requested even if the theme is not in core.
if ( isset( $args['theme_features'] ) ) {
$theme_features = array_merge( $args['theme_features'], $theme_features );
}
$final_theme_features = [];
foreach ( $theme_features as $theme_feature => $feature_args ) {
if ( ! method_exists( __CLASS__, $theme_feature ) ) {
continue;
}
try {
$reflection = new ReflectionMethod( __CLASS__, $theme_feature );
if ( $reflection->isStatic() === $static ) {
$final_theme_features[ $theme_feature ] = $feature_args;
}
} catch ( Exception $e ) {
unset( $e );
}
}
return $final_theme_features;
}
/**
* Add filters to manipulate output during output buffering before the DOM is constructed.
*
* @since 1.0
*
* @param array $args Args.
*/
public static function add_buffering_hooks( $args = [] ) {
$theme_features = self::get_theme_features( $args, true );
foreach ( $theme_features as $theme_feature => $feature_args ) {
if ( method_exists( __CLASS__, $theme_feature ) ) {
call_user_func( [ __CLASS__, $theme_feature ], $feature_args );
}
}
}
/**
* Add filter to output the quote icons in front of the article content.
*
* This is only used in Twenty Seventeen.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/f4580c122b7d0d2d66d22f806c6fe6e11023c6f0/src/wp-content/themes/twentyseventeen/assets/js/global.js#L105-L108
*/
public static function set_twentyseventeen_quotes_icon() {
add_filter(
'the_content',
static function ( $content ) {
// Why isn't Twenty Seventeen doing this to begin with? Why is it using JS to add the quote icon?
if ( function_exists( 'twentyseventeen_get_svg' ) && 'quote' === get_post_format() ) {
$icon = twentyseventeen_get_svg( [ 'icon' => 'quote-right' ] );
$content = preg_replace( '#(<blockquote.*?>)#s', '$1' . $icon, $content );
}
return $content;
}
);
}
/**
* Add filter to adjust the attachment image attributes to ensure attachment pages have a consistent <amp-img> rendering.
*
* This is only used in Twenty Seventeen.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/ddc8f803c6e99118998191fd2ea24124feb53659/src/wp-content/themes/twentyseventeen/functions.php#L545:L554
*/
public static function add_twentyseventeen_attachment_image_attributes() {
/*
* The max-height of the `.custom-logo-link img` is defined as being 80px, unless
* there is header media in which case it is 200px. Issues related to vertically-squashed
* images can be avoided if we just make sure that the image has this height to begin with.
*/
add_filter(
'get_custom_logo',
static function( $html ) {
$src = wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' );
if ( ! $src ) {
return $html;
}
if ( 'blank' === get_header_textcolor() && has_custom_header() ) {
$height = 200;
} else {
$height = 80;
}
$width = $height * ( $src[1] / $src[2] ); // Note that float values are allowed.
$html = preg_replace( '/(?<=width=")\d+(?=")/', $width, $html );
$html = preg_replace( '/(?<=height=")\d+(?=")/', $height, $html );
return $html;
}
);
}
/**
* Fix up core themes to do things in the AMP way.
*
* @since 1.0
*/
public function sanitize() {
$theme_features = self::get_theme_features( $this->args, false );
foreach ( $theme_features as $theme_feature => $feature_args ) {
if ( method_exists( $this, $theme_feature ) ) {
$this->$theme_feature( $feature_args );
}
}
}
/**
* Dequeue scripts.
*
* @since 1.0
*
* @param string[] $handles Handles, where each item value is the script handle.
*/
public static function dequeue_scripts( $handles = [] ) {
add_action(
'wp_enqueue_scripts',
static function() use ( $handles ) {
foreach ( $handles as $handle ) {
wp_dequeue_script( $handle );
}
},
PHP_INT_MAX
);
}
/**
* Remove actions.
*
* @since 1.0
*
* @param array $actions Actions, with action name as key and value being callback.
*/
public static function remove_actions( $actions = [] ) {
foreach ( $actions as $action => $callbacks ) {
foreach ( $callbacks as $callback ) {
$priority = has_action( $action, $callback );
if ( false !== $priority ) {
remove_action( $action, $callback, $priority );
}
}
}
}
/**
* Add smooth scrolling from link to target element.
*
* @since 1.0
*
* @param string[] $link_xpaths XPath queries to the links that should smooth scroll.
*/
public function add_smooth_scrolling( $link_xpaths ) {
foreach ( $link_xpaths as $link_xpath ) {
foreach ( $this->dom->xpath->query( $link_xpath ) as $link ) {
if ( $link instanceof DOMElement && preg_match( '/#(.+)/', $link->getAttribute( 'href' ), $matches ) ) {
$link->setAttribute( 'on', sprintf( 'tap:%s.scrollTo(duration=600)', $matches[1] ) );
// Prevent browser from jumping immediately to the link target.
$link->removeAttribute( 'href' );
$link->setAttribute( 'tabindex', '0' );
$link->setAttribute( 'role', 'button' );
}
}
}
}
/**
* Force SVG support, replacing no-svg class name with svg class name.
*
* @since 1.0
*
* @link https://github.com/WordPress/wordpress-develop/blob/1af1f65a21a1a697fb5f33027497f9e5ae638453/src/wp-content/themes/twentyseventeen/assets/js/global.js#L211-L213
* @link https://caniuse.com/#feat=svg
*/
public function force_svg_support() {
$class = $this->dom->documentElement->getAttribute( 'class' );
if ( $class ) {
$count = 0;
$class = preg_replace(
'/(^|\s)no-svg(\s|$)/',
' svg ',
$class,
-1,
$count
);
if ( $count > 0 ) {
$this->dom->documentElement->setAttribute( 'class', $class );
}
}
}
/**
* Force support for fixed background-attachment.
*
* @since 1.0
*
* @link https://github.com/WordPress/wordpress-develop/blob/1af1f65a21a1a697fb5f33027497f9e5ae638453/src/wp-content/themes/twentyseventeen/assets/js/global.js#L215-L217
* @link https://caniuse.com/#feat=background-attachment
*/
public function force_fixed_background_support() {
$this->dom->documentElement->setAttribute(
'class',
$this->dom->documentElement->getAttribute( 'class' ) . ' background-fixed'
);
}
/**
* Add body class when there is a header video.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/a26c24226c6b131a0ed22c722a836c100d3ba254/src/wp-content/themes/twentyseventeen/assets/js/global.js#L244-L247
*
* @param array $args Args.
*/
public static function add_has_header_video_body_class( $args = [] ) {
$args = array_merge(
[
'class_name' => 'has-header-video',
],
$args
);
add_filter(
'body_class',
static function( $body_classes ) use ( $args ) {
if ( has_header_video() ) {
$body_classes[] = $args['class_name'];
}
return $body_classes;
}
);
}
/**
* Get the (common) navigation outer height.
*
* @todo If the nav menu has many items and it spans multiple rows, this will be too small.
* @link https://github.com/WordPress/wordpress-develop/blob/fd5ba80c5c3d9cf62348567073945e246285fbca/src/wp-content/themes/twentyseventeen/assets/js/global.js#L50
*
* @return int Navigation outer height.
*/
protected static function get_twentyseventeen_navigation_outer_height() {
return 72;
}
/**
* Add required styles for featured image header and image blocks in Twenty Twenty.
*/
public static function add_twentytwenty_masthead_styles() {
add_action(
'wp_enqueue_scripts',
static function() {
ob_start();
?>
<style>
.featured-media amp-img {
position: static;
}
.wp-block-image img {
display: block;
}
</style>
<?php
$styles = str_replace( [ '<style>', '</style>' ], '', ob_get_clean() );
wp_add_inline_style( get_template() . '-style', $styles );
},
11
);
}
/**
* Add required styles for featured image header in Twenty Nineteen.
*
* The following is necessary because the styles in the theme apply to the featured img,
* and the CSS parser will then convert the selectors to amp-img. Nevertheless, object-fit
* does not apply on amp-img and it needs to apply on an actual img.
*
* @link https://github.com/WordPress/wordpress-develop/blob/5.0/src/wp-content/themes/twentynineteen/style.css#L2276-L2299
* @since 1.0
*/
public static function add_twentynineteen_masthead_styles() {
add_action(
'wp_enqueue_scripts',
static function() {
ob_start();
?>
<style>
.site-header.featured-image .site-featured-image .post-thumbnail amp-img > img {
height: auto;
left: 50%;
max-width: 1000%;
min-height: 100%;
min-width: 100vw;
position: absolute;
top: 50%;
transform: translateX(-50%) translateY(-50%);
width: auto;
z-index: 1;
/* When image filters are active, make it grayscale to colorize it blue. */
}
@supports (object-fit: cover) {
.site-header.featured-image .site-featured-image .post-thumbnail amp-img > img {
height: 100%;
left: 0;
object-fit: cover;
top: 0;
transform: none;
width: 100%;
}
}
</style>
<?php
$styles = str_replace( [ '<style>', '</style>' ], '', ob_get_clean() );
wp_add_inline_style( get_template() . '-style', $styles );
},
11
);
}
/**
* Add required styles for video and image headers.
*
* This is currently used exclusively for Twenty Seventeen.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/1af1f65a21a1a697fb5f33027497f9e5ae638453/src/wp-content/themes/twentyseventeen/style.css#L1687
* @link https://github.com/WordPress/wordpress-develop/blob/1af1f65a21a1a697fb5f33027497f9e5ae638453/src/wp-content/themes/twentyseventeen/style.css#L1743
*/
public static function add_twentyseventeen_masthead_styles() {
/*
* The following is necessary because the styles in the theme apply to img and video,
* and the CSS parser will then convert the selectors to amp-img and amp-video respectively.
* Nevertheless, object-fit does not apply on amp-img and it needs to apply on an actual img.
*/
add_action(
'wp_enqueue_scripts',
static function() {
$is_front_page_layout = ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) || ( is_home() && is_front_page() );
ob_start();
?>
<style>
.has-header-image .custom-header-media amp-img > img,
.has-header-video .custom-header-media amp-video > video{
position: fixed;
height: auto;
left: 50%;
max-width: 1000%;
min-height: 100%;
min-width: 100%;
min-width: 100vw; /* vw prevents 1px gap on left that 100% has */
width: auto;
top: 50%;
padding-bottom: 1px; /* Prevent header from extending beyond the footer */
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
.has-header-image:not(.twentyseventeen-front-page):not(.home) .custom-header-media amp-img > img {
bottom: 0;
position: absolute;
top: auto;
-ms-transform: translateX(-50%) translateY(0);
-moz-transform: translateX(-50%) translateY(0);
-webkit-transform: translateX(-50%) translateY(0);
transform: translateX(-50%) translateY(0);
}
/* For browsers that support object-fit */
@supports ( object-fit: cover ) {
.has-header-image .custom-header-media amp-img > img,
.has-header-video .custom-header-media amp-video > video,
.has-header-image:not(.twentyseventeen-front-page):not(.home) .custom-header-media amp-img > img {
height: 100%;
left: 0;
-o-object-fit: cover;
object-fit: cover;
top: 0;
-ms-transform: none;
-moz-transform: none;
-webkit-transform: none;
transform: none;
width: 100%;
}
}
.navigation-top.site-navigation-fixed {
display: none;
}
/* This is needed by add_smooth_scrolling because it removes the [href] attribute. */
.menu-scroll-down {
cursor: pointer;
}
<?php if ( $is_front_page_layout && ! has_custom_header() ) : ?>
/* https://github.com/WordPress/wordpress-develop/blob/fd5ba80c5c3d9cf62348567073945e246285fbca/src/wp-content/themes/twentyseventeen/assets/js/global.js#L92-L94 */
.site-branding {
margin-bottom: <?php echo (int) AMP_Core_Theme_Sanitizer::get_twentyseventeen_navigation_outer_height(); ?>px;
}
<?php endif; ?>
@media screen and (min-width: 48em) {
/* Note that adjustHeaderHeight() is irrelevant with this change */
<?php if ( ! $is_front_page_layout ) : ?>
.navigation-top {
position: static;
}
<?php endif; ?>
/* Initial styles that amp-animations for navigationTopShow and navigationTopHide will override */
.navigation-top.site-navigation-fixed {
opacity: 0;
transform: translateY( -<?php echo (int) AMP_Core_Theme_Sanitizer::get_twentyseventeen_navigation_outer_height(); ?>px );
display: block;
}
}
</style>
<?php
$styles = str_replace( [ '<style>', '</style>' ], '', ob_get_clean() );
wp_add_inline_style( get_template() . '-style', $styles );
},
11
);
}
/**
* Override the featured image header styling in style.css.
* Used only for Twenty Seventeen.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/1af1f65a21a1a697fb5f33027497f9e5ae638453/src/wp-content/themes/twentyseventeen/style.css#L2100
*/
public static function add_twentyseventeen_image_styles() {
add_action(
'wp_enqueue_scripts',
static function() {
ob_start();
?>
<style>
/* Override the display: block in twentyseventeen/style.css, as <amp-img> is usually inline-block. */
.single-featured-image-header amp-img {
display: inline-block;
}
/* Because the <amp-img> is inline-block, its container needs this rule to center it. */
.single-featured-image-header {
text-align: center;
}
</style>
<?php
$styles = str_replace( [ '<style>', '</style>' ], '', ob_get_clean() );
wp_add_inline_style( get_template() . '-style', $styles );
},
11
);
}
/**
* Add sticky nav menu to Twenty Seventeen.
*
* This is implemented by cloning the navigation-top element, giving it a fixed position outside of the viewport,
* and then showing it at the top of the window as soon as the original nav begins to get scrolled out of view.
* In order to improve accessibility, the cloned nav gets aria-hidden=true and all of the links get tabindex=-1
* to prevent the keyboard from focusing on elements off the screen; it is not necessary to focus on the elements
* in the fixed nav menu because as soon as the original nav menu is focused then the window is scrolled to the
* top anyway.
*
* @since 1.0
*/
public function add_twentyseventeen_sticky_nav_menu() {
/**
* Elements.
*
* @var DOMElement $link
* @var DOMElement $element
* @var DOMElement $navigation_top
* @var DOMElement $navigation_top_fixed
*/
$navigation_top = $this->dom->xpath->query( '//header[ @id = "masthead" ]//div[ contains( @class, "navigation-top" ) ]' )->item( 0 );
if ( ! $navigation_top ) {
return;
}
$navigation_top_fixed = $navigation_top->cloneNode( true );
$navigation_top_fixed->setAttribute( 'class', $navigation_top_fixed->getAttribute( 'class' ) . ' site-navigation-fixed' );
$navigation_top_fixed->setAttribute( 'aria-hidden', 'true' );
foreach ( $navigation_top_fixed->getElementsByTagName( 'a' ) as $link ) {
$link->setAttribute( 'tabindex', '-1' );
}
$navigation_top->parentNode->insertBefore( $navigation_top_fixed, $navigation_top->nextSibling );
foreach ( $this->dom->xpath->query( './/*[ @id ]', $navigation_top_fixed ) as $element ) {
$element->setAttribute( 'id', $element->getAttribute( 'id' ) . '-fixed' );
}
$attributes = [
'layout' => 'nodisplay',
'intersection-ratios' => 1,
'on' => implode(
';',
[
'exit:navigationTopShow.start',
'enter:navigationTopHide.start',
]
),
];
if ( is_admin_bar_showing() ) {
$attributes['viewport-margins'] = '32px 0';
}
$position_observer = AMP_DOM_Utils::create_node( $this->dom, 'amp-position-observer', $attributes );
$navigation_top->appendChild( $position_observer );
$animations = [
'navigationTopShow' => [
'duration' => 0,
'fill' => 'both',
'animations' => [
'selector' => '.navigation-top.site-navigation-fixed',
'media' => '(min-width: 48em)',
'keyframes' => [
'opacity' => 1.0,
'transform' => 'translateY( 0 )',
],
],
],
'navigationTopHide' => [
'duration' => 0,
'fill' => 'both',
'animations' => [
'selector' => '.navigation-top.site-navigation-fixed',
'media' => '(min-width: 48em)',
'keyframes' => [
'opacity' => 0.0,
'transform' => sprintf( 'translateY( -%dpx )', self::get_twentyseventeen_navigation_outer_height() ),
],
],
],
];
foreach ( $animations as $animation_id => $animation ) {
$amp_animation = AMP_DOM_Utils::create_node(
$this->dom,
'amp-animation',
[
'id' => $animation_id,
'layout' => 'nodisplay',
]
);
$position_script = $this->dom->createElement( 'script' );
$position_script->setAttribute( 'type', 'application/json' );
$position_script->appendChild( $this->dom->createTextNode( wp_json_encode( $animation ) ) );
$amp_animation->appendChild( $position_script );
$this->dom->body->appendChild( $amp_animation );
}