C:\xampp\htdocs\landing\wp-content\plugins\amp\includes\class-amp-theme-support.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
<?php
/**
 * Class AMP_Theme_Support
 *
 * @package AMP
 */

use AmpProject\Amp;
use 
AmpProject\AmpWP\DevTools\ErrorPage;
use 
AmpProject\AmpWP\ExtraThemeAndPluginHeaders;
use 
AmpProject\AmpWP\Option;
use 
AmpProject\AmpWP\QueryVar;
use 
AmpProject\AmpWP\RemoteRequest\CachedRemoteGetRequest;
use 
AmpProject\AmpWP\ConfigurationArgument;
use 
AmpProject\AmpWP\Services;
use 
AmpProject\AmpWP\Transformer;
use 
AmpProject\Attribute;
use 
AmpProject\Dom\Document;
use 
AmpProject\Extension;
use 
AmpProject\Optimizer;
use 
AmpProject\RemoteRequest\FallbackRemoteGetRequest;
use 
AmpProject\RemoteRequest\FilesystemRemoteGetRequest;
use 
AmpProject\AmpWP\RemoteRequest\WpHttpRemoteGetRequest;
use 
AmpProject\Tag;

/**
 * Class AMP_Theme_Support
 *
 * Callbacks for adding AMP-related things when theme support is added.
 *
 * @internal
 */
class AMP_Theme_Support {

    
/**
     * Theme support slug.
     *
     * @var string
     */
    
const SLUG 'amp';

    
/**
     * Slug identifying standard website mode.
     *
     * @since 1.2
     * @var string
     */
    
const STANDARD_MODE_SLUG 'standard';

    
/**
     * Slug identifying transitional website mode.
     *
     * @since 1.2
     * @var string
     */
    
const TRANSITIONAL_MODE_SLUG 'transitional';

    
/**
     * Slug identifying reader website mode.
     *
     * @since 1.2
     * @var string
     */
    
const READER_MODE_SLUG 'reader';

    
/**
     * Flag used in args passed to add_theme_support('amp') to indicate transitional mode supported.
     *
     * @since 1.2
     * @var string
     */
    
const PAIRED_FLAG 'paired';

    
/**
     * The directory name in a theme where Reader Mode templates can be.
     *
     * For example, this could be at your-theme-name/amp.
     *
     * @var string
     */
    
const READER_MODE_TEMPLATE_DIRECTORY 'amp';

    
/**
     * Query var for requests to open the paired browsing interface.
     *
     * @var string
     */
    
const PAIRED_BROWSING_QUERY_VAR 'amp-paired-browsing';

    
/**
     * Sanitizers, with keys as class names and values as arguments.
     *
     * @var array[]
     */
    
protected static $sanitizer_classes = [];

    
/**
     * Embed handlers.
     *
     * @var AMP_Base_Embed_Handler[]
     */
    
protected static $embed_handlers = [];

    
/**
     * Template types.
     *
     * @var array
     */
    
protected static $template_types = [
        
'paged'// Deprecated.
        
'index',
        
'404',
        
'archive',
        
'author',
        
'category',
        
'tag',
        
'taxonomy',
        
'date',
        
'home',
        
'front_page',
        
'page',
        
'search',
        
'single',
        
'embed',
        
'singular',
        
'attachment',
    ];

    
/**
     * Whether output buffering has started.
     *
     * @since 0.7
     * @var bool
     */
    
protected static $is_output_buffering false;

    
/**
     * Initialize.
     *
     * @since 0.7
     */
    
public static function init() {
        
/**
         * Starts the server-timing measurement for the entire output buffer capture.
         *
         * @since 2.0
         * @internal
         *
         * @param string      $event_name        Name of the event to record.
         * @param string|null $event_description Optional. Description of the event
         *                                       to record. Defaults to null.
         * @param string[]    $properties        Optional. Additional properties to add
         *                                       to the logged record.
         * @param bool        $verbose_only      Optional. Whether to only show the
         *                                       event in verbose mode. Defaults to
         *                                       false.
         */
        
do_action'amp_server_timing_start''amp_output_buffer' );

        
// Ensure extra theme support for core themes is in place.
        
AMP_Core_Theme_Sanitizer::extend_theme_support();

        
/*
         * Note that wp action is use instead of template_redirect because some themes/plugins output
         * the response at this action and then short-circuit with exit. So this is why the the preceding
         * action to template_redirect--the wp action--is used instead.
         */
        
if ( ! is_admin() ) {
            
add_action'wp', [ __CLASS__'finish_init' ], PHP_INT_MAX );
        }
    }

    
/**
     * Determine whether theme support was added via admin option.
     *
     * @since 1.0
     * @see AMP_Theme_Support::read_theme_support()
     * @see AMP_Theme_Support::get_support_mode()
     * @codeCoverageIgnore
     * @deprecated Support is always determined by the option.
     *
     * @return bool Support added via option.
     */
    
public static function is_support_added_via_option() {
        
_deprecated_function__METHOD__'2.0.0' );
        return 
true;
    }

    
/**
     * Get the theme support mode added via admin option.
     *
     * @return null|string Support added via option, with null meaning Reader, and otherwise being 'standard' or 'transitional'.
     * @see AMP_Theme_Support::read_theme_support()
     * @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
     * @see AMP_Theme_Support::STANDARD_MODE_SLUG
     *
     * @since 1.2
     * @deprecated
     * @codeCoverageIgnore
     */
    
public static function get_support_mode_added_via_option() {
        
_deprecated_function__METHOD__'2.0.0''AMP_Options_Manager::get_option' );
        
$value AMP_Options_Manager::get_optionOption::THEME_SUPPORT );
        if ( 
self::READER_MODE_SLUG === $value ) {
            
$value null;
        }
        return 
$value;
    }

    
/**
     * Get the theme support mode added via theme.
     *
     * @return null|string Support added via theme, with null meaning Reader, and otherwise being 'standard' or 'transitional'.
     * @see AMP_Theme_Support::read_theme_support()
     * @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
     * @see AMP_Theme_Support::STANDARD_MODE_SLUG
     *
     * @since 1.2
     * @deprecated
     * @codeCoverageIgnore
     */
    
public static function get_support_mode_added_via_theme() {
        
_deprecated_function__METHOD__'2.0.0''current_theme_supports' );
        
$theme_support_args self::get_theme_support_args();
        if ( ! 
$theme_support_args ) {
            return 
null;
        }
        return empty( 
$theme_support_argsself::PAIRED_FLAG ] ) ? self::STANDARD_MODE_SLUG self::TRANSITIONAL_MODE_SLUG;
    }

    
/**
     * Get theme support mode.
     *
     * @return string Theme support mode.
     * @see AMP_Theme_Support::read_theme_support()
     * @see AMP_Theme_Support::TRANSITIONAL_MODE_SLUG
     * @see AMP_Theme_Support::STANDARD_MODE_SLUG
     *
     * @since 1.2
     * @deprecated
     * @codeCoverageIgnore
     */
    
public static function get_support_mode() {
        
_deprecated_function__METHOD__'2.0.0''AMP_Options_Manager::get_option' );
        return 
AMP_Options_Manager::get_optionOption::THEME_SUPPORT );
    }

    
/**
     * Check theme support args or add theme support if option is set in the admin.
     *
     * In older versions of the plugin, the DB option was only considered if the theme does not already explicitly support AMP.
     * This is no longer the case. The DB option is the only value that is considered.
     *
     * @see AMP_Post_Type_Support::add_post_type_support() For where post type support is added, since it is irrespective of theme support.
     * @deprecated
     * @codeCoverageIgnore
     */
    
public static function read_theme_support() {
        
_deprecated_function__METHOD__'2.0.0' );
    }

    
/**
     * Get the theme support args.
     *
     * This avoids having to repeatedly call `get_theme_support()`, check the args, shift an item off the array, and so on.
     * Note that if the theme's `style.css` has the `AMP` header with a value that when converted to a boolean evaluates to `true`, then this function will return the same
     * as if the theme had done `add_theme_support('amp')`.
     *
     * @since 1.0
     *
     * @return array|false Theme support args, or false if theme support is not present.
     */
    
public static function get_theme_support_args() {
        if ( ! 
current_theme_supportsself::SLUG ) ) {
            
$theme_header wp_get_theme()->getExtraThemeAndPluginHeaders::AMP_HEADER );
            if ( 
rest_sanitize_boolean$theme_header ) && ExtraThemeAndPluginHeaders::AMP_HEADER_LEGACY !== $theme_header ) {
                return [
                    
self::PAIRED_FLAG => true,
                ];
            }
            return 
false;
        }
        
$support get_theme_supportself::SLUG );
        if ( isset( 
$support[0] ) && is_array$support[0] ) ) {
            
$args $support[0];
        } else {
            
$args = [];
        }
        if ( ! isset( 
$argsself::PAIRED_FLAG ] ) ) {
            
// Formerly when paired was not supplied it defaulted to be false. However, the reality is that
            // the vast majority of themes should be built to work in AMP and non-AMP because AMP can be
            // disabled for any URL just by disabling AMP for the post.
            
$argsself::PAIRED_FLAG ] = true;
        }
        return 
$args;
    }

    
/**
     * Gets whether the parent or child theme supports Reader Mode.
     *
     * True if the theme does not call add_theme_support( 'amp' ) at all,
     * and it has an amp/ directory for templates.
     *
     * @return bool Whether the theme supports Reader Mode.
     */
    
public static function supports_reader_mode() {
        return (
            ! 
current_theme_supportsself::SLUG )
            &&
            (
                
is_dirtrailingslashitget_template_directory() ) . self::READER_MODE_TEMPLATE_DIRECTORY )
                ||
                
is_dirtrailingslashitget_stylesheet_directory() ) . self::READER_MODE_TEMPLATE_DIRECTORY )
            )
        );
    }

    
/**
     * Finish initialization once query vars are set.
     *
     * @since 0.7
     */
    
public static function finish_init() {
        if ( 
self::is_paired_available() ) {
            
self::setup_paired_browsing_client();
            
add_action'template_redirect', [ __CLASS__'sanitize_url_for_paired_browsing' ] );
            
add_filter'template_include', [ __CLASS__'serve_paired_browsing_experience' ], PHP_INT_MAX );
        }

        
$has_query_var = (
            isset( 
$_GETamp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            
||
            
false !== get_query_varamp_get_slug(), false )
        );

        if ( ! 
amp_is_request() ) {
            
/*
             * Redirect to AMP-less URL if AMP is not available for this URL and yet the query var is present.
             * Temporary redirect is used for admin users because implied transitional mode and template support can be
             * enabled by user ay any time, so they will be able to make AMP available for this URL and see the change
             * without wrestling with the redirect cache.
             */
            
if ( $has_query_var ) {
                
self::redirect_non_amp_urlcurrent_user_can'manage_options' ) ? 302 301 );
            }

            
amp_add_frontend_actions();
            return;
        }

        
self::ensure_proper_amp_location();

        if ( 
amp_is_legacy() ) {
            
// Make sure there is no confusion when serving the legacy Reader template that the normal theme hooks should not be used.
            
remove_theme_supportself::SLUG );

            
add_filter(
                
'template_include',
                static function() {
                    return 
AMP__DIR__ '/includes/templates/reader-template-loader.php';
                },
                
PHP_INT_MAX
            
);
        } else {
            
$theme_support self::get_theme_support_args();
            if ( 
false === $theme_support ) {
                
// Make sure that 'amp' theme support is present for plugins can use `current_theme_supports('amp')` as
                // a signal for whether to use standard template hooks instead of legacy Reader AMP post template hooks.
                
add_theme_supportself::SLUG );
            } elseif ( ! empty( 
$theme_support['template_dir'] ) ) {
                
self::add_amp_template_filters();
            }
        }

        
self::add_hooks();
        
self::$sanitizer_classes amp_get_content_sanitizers();
        
self::$sanitizer_classes AMP_Validation_Manager::filter_sanitizer_argsself::$sanitizer_classes );
        
self::$embed_handlers    self::register_content_embed_handlers();
        
self::$sanitizer_classes['AMP_Embed_Sanitizer']['embed_handlers'] = self::$embed_handlers;

        foreach ( 
self::$sanitizer_classes as $sanitizer_class => $args ) {
            if ( 
method_exists$sanitizer_class'add_buffering_hooks' ) ) {
                
call_user_func( [ $sanitizer_class'add_buffering_hooks' ], $args );
            }
        }
    }

    
/**
     * Ensure that the current AMP location is correct.
     *
     * @since 1.0
     * @since 2.0 Removed $exit param.
     *
     * @return bool Whether redirection should have been done.
     */
    
public static function ensure_proper_amp_location() {
        
$has_query_var false !== get_query_varamp_get_slug(), false ); // May come from URL param or endpoint slug.
        
$has_url_param = isset( $_GETamp_get_slug() ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

        
if ( amp_is_canonical() ) {
            
/*
             * When AMP-first/canonical, then when there is an /amp/ endpoint or ?amp URL param,
             * then a redirect needs to be done to the URL without any AMP indicator in the URL.
             * Permanent redirect is used for unauthenticated users since switching between modes
             * should happen infrequently. For admin users, this is kept temporary to allow them
             * to not be hampered by browser remembering permanent redirects and preventing test.
             */
            
if ( $has_query_var || $has_url_param ) {
                return 
self::redirect_non_amp_urlcurrent_user_can'manage_options' ) ? 302 301 );
            }
        } elseif ( 
amp_is_legacy() && is_singular() ) {
            
// Prevent infinite URL space under /amp/ endpoint.
            
global $wp;
            
$path_args = [];
            
wp_parse_str$wp->matched_query$path_args );
            if ( isset( 
$path_argsamp_get_slug() ] ) && '' !== $path_argsamp_get_slug() ] ) {
                if ( 
wp_safe_redirectamp_get_permalinkget_queried_object_id() ), 301 ) ) {
                    
// @codeCoverageIgnoreStart
                    
exit;
                    
// @codeCoverageIgnoreEnd
                
}
                return 
true;
            }
        } elseif ( 
$has_query_var && ! $has_url_param ) {
            
/*
             * When in AMP transitional mode *with* theme support, then the proper AMP URL has the 'amp' URL param
             * and not the /amp/ endpoint. The URL param is now the exclusive way to mark AMP in transitional mode
             * when amp theme support present. This is important for plugins to be able to reliably call
             * amp_is_request() before the parse_query action.
             */
            
$old_url amp_get_current_url();
            
$new_url add_query_argamp_get_slug(), ''amp_remove_endpoint$old_url ) );
            if ( 
$old_url !== $new_url ) {
                
// A temporary redirect is used for admin users to allow them to see changes between reader mode and transitional modes.
                
if ( wp_safe_redirect$new_urlcurrent_user_can'manage_options' ) ? 302 301 ) ) {
                    
// @codeCoverageIgnoreStart
                    
exit;
                    
// @codeCoverageIgnoreEnd
                
}
                return 
true;
            }
        }
        return 
false;
    }

    
/**
     * Redirect to non-AMP version of the current URL, such as because AMP is canonical or there are unaccepted validation errors.
     *
     * If the current URL is already AMP-less then do nothing.
     *
     * @since 0.7
     * @since 1.0 Added $exit param.
     * @since 1.0 Renamed from redirect_canonical_amp().
     * @since 2.0 Removed $exit param.
     *
     * @param int $status Status code (301 or 302).
     * @return bool Whether redirection should have be done.
     */
    
public static function redirect_non_amp_url$status 302 ) {
        
$current_url amp_get_current_url();
        
$non_amp_url amp_remove_endpoint$current_url );
        if ( 
$non_amp_url === $current_url ) {
            return 
false;
        }

        if ( 
wp_safe_redirect$non_amp_url$status ) ) {
            
// @codeCoverageIgnoreStart
            
exit;
            
// @codeCoverageIgnoreEnd
        
}
        return 
true;
    }

    
/**
     * Determines whether transitional mode is available.
     *
     * When 'amp' theme support has not been added or canonical mode is enabled, then this returns false.
     *
     * @since 0.7
     *
     * @see amp_is_canonical()
     * @return bool Whether available.
     */
    
public static function is_paired_available() {
        if ( 
amp_is_legacy() ) {
            return 
false;
        }

        if ( 
amp_is_canonical() ) {
            return 
false;
        }

        
$availability self::get_template_availability();
        return 
$availability['supported'];
    }

    
/**
     * Determine whether the user is in the Customizer preview iframe.
     *
     * @since 0.7
     *
     * @return bool Whether in Customizer preview iframe.
     */
    
public static function is_customize_preview_iframe() {
        global 
$wp_customize;
        return 
is_customize_preview() && $wp_customize->get_messenger_channel();
    }

    
/**
     * Register filters for loading AMP-specific templates.
     */
    
public static function add_amp_template_filters() {
        foreach ( 
self::$template_types as $template_type ) {
            
// See get_query_template().
            
$template_type preg_replace'|[^a-z0-9-]+|'''$template_type );

            
add_filter"{$template_type}_template_hierarchy", [ __CLASS__'filter_amp_template_hierarchy' ] );
        }
    }

    
/**
     * Determine template availability of AMP for the given query.
     *
     * This is not intended to return whether AMP is available for a _specific_ post. For that, use `amp_is_post_supported()`.
     *
     * @since 1.0
     * @global WP_Query $wp_query
     * @see amp_is_post_supported()
     *
     * @param WP_Query|WP_Post|null $query Query or queried post. If null then the global query will be used.
     * @return array {
     *     Template availability.
     *
     *     @type bool        $supported Whether the template is supported in AMP.
     *     @type string|null $template  The ID of the matched template (conditional), such as 'is_singular', or null if nothing was matched.
     *     @type string[]    $errors    List of the errors or reasons for why the template is not available.
     * }
     */
    
public static function get_template_availability$query null ) {
        global 
$wp_query;
        if ( ! 
$query ) {
            
$query $wp_query;
        } elseif ( 
$query instanceof WP_Post ) {
            
$post  $query;
            
$query = new WP_Query();
            if ( 
'page' === $post->post_type ) {
                
$query->set'page_id'$post->ID );
            } else {
                
$query->set'p'$post->ID );
            }
            
$query->queried_object    $post;
            
$query->queried_object_id $post->ID;
            
$query->parse_query_vars();
        }

        
$default_response = [
            
'errors'    => [],
            
'supported' => false,
            
'immutable' => false// Obsolete.
            
'template'  => null,
        ];

        if ( 
amp_is_legacy() ) {
            return 
array_merge(
                
$default_response,
                [ 
'errors' => [ 'legacy_reader_mode' ] ]
            );
        }

        if ( ! ( 
$query instanceof WP_Query ) ) {
            
_doing_it_wrong__METHOD__esc_html__'No WP_Query available.''amp' ), '1.0' );
            return 
array_merge(
                
$default_response,
                [ 
'errors' => [ 'no_query_available' ] ]
            );
        }

        
$all_templates_supported AMP_Options_Manager::get_optionOption::ALL_TEMPLATES_SUPPORTED );

        
// Make sure global $wp_query is set in case of conditionals that unfortunately look at global scope.
        
$prev_query $wp_query;
        
$wp_query   $query// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

        
$matching_templates    = [];
        
$supportable_templates self::get_supportable_templates();
        foreach ( 
$supportable_templates as $id => $supportable_template ) {
            if ( empty( 
$supportable_template['callback'] ) ) {
                
$callback $id;
            } else {
                
$callback $supportable_template['callback'];
            }

            
// If the callback is a method on the query, then call the method on the query itself.
            
if ( is_string$callback ) && 'is_' === substr$callback0) && method_exists$query$callback ) ) {
                
$is_match call_user_func( [ $query$callback ] );
            } elseif ( 
is_callable$callback ) ) {
                
$is_match $callback$query );
            } else {
                
/* translators: %s: the supportable template ID. */
                
_doing_it_wrong__FUNCTION__esc_htmlsprintf__'Supportable template "%s" does not have a callable callback.''amp' ), $id ) ), '1.0' );
                
$is_match false;
            }

            if ( 
$is_match ) {
                
$matching_templates$id ] = [
                    
'template'  => $id,
                    
'supported' => ! empty( $supportable_template['supported'] ),
                ];
            }
        }

        
// Restore previous $wp_query (if any).
        
$wp_query $prev_query// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

        // Make sure children override their parents.
        
$matching_template_ids array_keys$matching_templates );
        foreach ( 
array_diffarray_keys$supportable_templates ), $matching_template_ids ) as $template_id ) {
            unset( 
$supportable_templates$template_id ] );
        }
        foreach ( 
$matching_template_ids as $id ) {
            
$has_children false;
            foreach ( 
$supportable_templates as $other_id => $supportable_template ) {
                if ( 
$other_id === $id ) {
                    continue;
                }
                if ( isset( 
$supportable_template['parent'] ) && $id === $supportable_template['parent'] ) {
                    
$has_children true;
                    break;
                }
            }

            
// Delete all matching parent templates since the child will override them.
            
if ( ! $has_children ) {
                
$supportable_template $supportable_templates$id ];
                while ( ! empty( 
$supportable_template['parent'] ) ) {
                    
$parent $supportable_template['parent'];

                    
/*
                     * If the parent is not amongst the supportable templates, then something is off in terms of hierarchy.
                     * Either the matching is off-track, or the template is badly configured.
                     */
                    
if ( ! array_key_exists$parent$supportable_templates ) ) {
                        
_doing_it_wrong(
                            
__METHOD__,
                            
esc_html(
                                
sprintf(
                                    
/* translators: %s: amp_supportable_templates */
                                    
__'An expected parent was not found. Did you filter %s to not honor the template hierarchy?''amp' ),
                                    
'amp_supportable_templates'
                                
)
                            ),
                            
'1.4'
                        
);
                        break;
                    }

                    
$supportable_template $supportable_templates$parent ];

                    
// Let the child supported status override the parent's supported status.
                    
unset( $matching_templates$parent ] );
                }
            }
        }

        
// If there is more than 1 matching template, the is_home() condition is the default so discard it if there are other matching templates.
        
if ( count$matching_templates ) > && isset( $matching_templates['is_home'] ) ) {
            unset( 
$matching_templates['is_home'] );
        }

        
/*
         * When there is still more than one matching template, account for ambiguous cases, informed by the order in template-loader.php.
         * See <https://github.com/WordPress/wordpress-develop/blob/5.1.0/src/wp-includes/template-loader.php#L49-L68>.
         */
        
if ( count$matching_templates ) > ) {
            
$template_conditional_priority_order = [
                
'is_embed',
                
'is_404',
                
'is_search',
                
'is_front_page',
                
'is_home',
                
'is_post_type_archive',
                
'is_tax',
                
'is_attachment',
                
'is_single',
                
'is_page',
                
'is_singular',
                
'is_category',
                
'is_tag',
                
'is_author',
                
'is_date',
                
'is_archive',
            ];

            
// Obtain the template conditionals for each matching template ID (e.g. 'is_post_type_archive[product]' => 'is_post_type_archive').
            
$template_conditional_id_mapping = [];
            foreach ( 
array_keys$matching_templates ) as $template_id ) {
                
$template_conditional_id_mappingstrtok$template_id'[' ) ] = $template_id;
            }

            
// If there are any custom supportable templates, only consider them since they would override the conditional logic in core.
            
$custom_template_conditions array_diff(
                
array_keys$template_conditional_id_mapping ),
                
$template_conditional_priority_order
            
);
            if ( ! empty( 
$custom_template_conditions ) ) {
                
$matching_templates wp_array_slice_assoc(
                    
$matching_templates,
                    
array_valueswp_array_slice_assoc$template_conditional_id_mapping$custom_template_conditions ) )
                );
            } else {
                
/*
                 * Otherwise, iterate over the template conditionals in the order they occur in the if/elseif/else conditional chain.
                 * to then populate $matching_templates with just this one entry.
                 */
                
foreach ( $template_conditional_priority_order as $template_conditional ) {
                    if ( isset( 
$template_conditional_id_mapping$template_conditional ] ) ) {
                        
$template_id        $template_conditional_id_mapping$template_conditional ];
                        
$matching_templates = [
                            
$template_id => $matching_templates$template_id ],
                        ];
                        break;
                    }
                }
            }
        }

        
/*
         * If there are more than one matching templates, then something is probably not right.
         * Template conditions need to be set up properly to prevent this from happening.
         */
        
if ( count$matching_templates ) > ) {
            
_doing_it_wrong(
                
__METHOD__,
                
esc_html(
                    
sprintf(
                        
/* translators: %s: amp_supportable_templates */
                        
__'Did not expect there to be more than one matching template. Did you filter %s to not honor the template hierarchy?''amp' ),
                        
'amp_supportable_templates'
                    
)
                ),
                
'1.0'
            
);
        }

        
$matching_template array_shift$matching_templates );

        
// If there aren't any matching templates left that are supported, then we consider it to not be available.
        
if ( ! $matching_template ) {
            if ( 
$all_templates_supported ) {
                return 
array_merge(
                    
$default_response,
                    [
                        
'supported' => true,
                    ]
                );
            }

            return 
array_merge(
                
$default_response,
                [ 
'errors' => [ 'no_matching_template' ] ]
            );
        }
        
$matching_template array_merge$default_response$matching_template );

        
// If there aren't any matching templates left that are supported, then we consider it to not be available.
        
if ( empty( $matching_template['supported'] ) ) {
            
$matching_template['errors'][] = 'template_unsupported';
        }

        
// For singular queries, amp_is_post_supported() is given the final say.
        
if ( $query->is_singular() || $query->is_posts_page ) {
            
/**
             * Queried object.
             *
             * @var WP_Post $queried_object
             */
            
$queried_object $query->get_queried_object();
            if ( 
$queried_object instanceof WP_Post ) {
                
$support_errors AMP_Post_Type_Support::get_support_errors$queried_object );
                if ( ! empty( 
$support_errors ) ) {
                    
$matching_template['errors']    = array_merge$matching_template['errors'], $support_errors );
                    
$matching_template['supported'] = false;
                }
            }
        }

        return 
$matching_template;
    }

    
/**
     * Get the templates which can be supported.
     *
     * @return array Supportable templates.
     */
    
public static function get_supportable_templates() {
        
$templates = [
            
'is_singular' => [
                
'label' => __'Singular''amp' ),
            ],
        ];
        if ( 
'page' === get_option'show_on_front' ) ) {
            
$templates['is_front_page'] = [
                
'label'  => __'Homepage''amp' ),
                
'parent' => 'is_singular',
            ];
            if ( 
AMP_Post_Meta_Box::DISABLED_STATUS === get_post_metaget_option'page_on_front' ), AMP_Post_Meta_Box::STATUS_POST_META_KEYtrue ) ) {
                
/* translators: %s: the URL to the edit post screen. */
                
$templates['is_front_page']['description'] = sprintf__'Currently disabled at the <a href="%s">page level</a>.''amp' ), esc_urlget_edit_post_linkget_option'page_on_front' ) ) ) );
            }

            
// In other words, same as is_posts_page, *but* it not is_singular.
            
$templates['is_home'] = [
                
'label' => __'Blog''amp' ),
            ];
            if ( 
AMP_Post_Meta_Box::DISABLED_STATUS === get_post_metaget_option'page_for_posts' ), AMP_Post_Meta_Box::STATUS_POST_META_KEYtrue ) ) {
                
/* translators: %s: the URL to the edit post screen. */
                
$templates['is_home']['description'] = sprintf__'Currently disabled at the <a href="%s">page level</a>.''amp' ), esc_urlget_edit_post_linkget_option'page_for_posts' ) ) ) );
            }
        } else {
            
$templates['is_home'] = [
                
'label' => __'Homepage''amp' ),
            ];
        }

        
$templates array_merge(
            
$templates,
            [
                
'is_archive' => [
                    
'label' => __'Archives''amp' ),
                ],
                
'is_author'  => [
                    
'label'  => __'Author''amp' ),
                    
'parent' => 'is_archive',
                ],
                
'is_date'    => [
                    
'label'  => __'Date''amp' ),
                    
'parent' => 'is_archive',
                ],
                
'is_search'  => [
                    
'label' => __'Search''amp' ),
                ],
                
'is_404'     => [
                    
'label' => __'Not Found (404)''amp' ),
                ],
            ]
        );

        if ( 
taxonomy_exists'category' ) ) {
            
$templates['is_category'] = [
                
'label'  => get_taxonomy'category' )->labels->name,
                
'parent' => 'is_archive',
            ];
        }
        if ( 
taxonomy_exists'post_tag' ) ) {
            
$templates['is_tag'] = [
                
'label'  => get_taxonomy'post_tag' )->labels->name,
                
'parent' => 'is_archive',
            ];
        }

        
$taxonomy_args = [
            
'_builtin' => false,
            
'public'   => true,
        ];
        foreach ( 
get_taxonomies$taxonomy_args'objects' ) as $taxonomy ) {
            
$templatessprintf'is_tax[%s]'$taxonomy->name ) ] = [
                
'label'    => $taxonomy->labels->name,
                
'parent'   => 'is_archive',
                
'callback' => static function ( WP_Query $query ) use ( $taxonomy ) {
                    return 
$query->is_tax$taxonomy->name );
                },
            ];
        }

        
$post_type_args = [
            
'has_archive' => true,
            
'public'      => true,
        ];
        foreach ( 
get_post_types$post_type_args'objects' ) as $post_type ) {
            
$templatessprintf'is_post_type_archive[%s]'$post_type->name ) ] = [
                
'label'    => $post_type->labels->archives,
                
'parent'   => 'is_archive',
                
'callback' => static function ( WP_Query $query ) use ( $post_type ) {
                    return 
$query->is_post_type_archive$post_type->name );
                },
            ];
        }

        
/**
         * Filters list of supportable templates.
         *
         * Each array item should have a key that corresponds to a template conditional function.
         * If the key is such a function, then the key is used to evaluate whether the given template
         * entry is a match. Otherwise, a supportable template item can include a callback value which
         * is used instead. Each item needs a 'label' value. Additionally, if the supportable template
         * is a subset of another condition (e.g. is_singular > is_single) then this relationship needs
         * to be indicated via the 'parent' value.
         *
         * @since 1.0
         *
         * @param array $templates Supportable templates.
         */
        
$templates apply_filters'amp_supportable_templates'$templates );

        
$supported_templates AMP_Options_Manager::get_optionOption::SUPPORTED_TEMPLATES );
        
$are_all_supported   AMP_Options_Manager::get_optionOption::ALL_TEMPLATES_SUPPORTED );

        
$did_filter_supply_supported false;
        
$did_filter_supply_immutable false;
        foreach ( 
$templates as $id => &$template ) {
            if ( isset( 
$template['supported'] ) ) {
                
$did_filter_supply_supported true;
            }
            if ( isset( 
$template['immutable'] ) ) {
                
$did_filter_supply_immutable true;
            }

            
$template['supported']      = $are_all_supported || in_array$id$supported_templatestrue );
            
$template['user_supported'] = $template['supported']; // Obsolete.
            
$template['immutable']      = false// Obsolete.
        
}

        if ( 
$did_filter_supply_supported ) {
            
_doing_it_wrong(
                
'add_filter',
                
esc_html__'The AMP plugin no longer allows `amp_supportable_templates` filters to specify a template as being `supported`. This is now managed only in AMP Settings.''amp' ),
                
'2.0.0'
            
);
        }
        if ( 
$did_filter_supply_immutable ) {
            
_doing_it_wrong(
                
'add_filter',
                
esc_html__'The AMP plugin no longer allows `amp_supportable_templates` filters to specify a template\'s support as being `immutable`. This is now managed only in AMP Settings.''amp' ),
                
'2.0.0'
            
);
        }

        return 
$templates;
    }

    
/**
     * Register hooks.
     */
    
public static function add_hooks() {

        
// Remove core actions which are invalid AMP.
        
remove_action'wp_head''wp_post_preview_js');
        
remove_action'wp_head''wp_oembed_add_host_js' );

        
// Replace JS-based emoji with PHP-based, if the JS-based emoji replacement was not already removed.
        
if ( has_action'wp_head''print_emoji_detection_script' ) ) {
            
remove_action'wp_head''print_emoji_detection_script');
            
remove_action'wp_print_styles''print_emoji_styles' );
            
add_action'wp_print_styles', [ __CLASS__'print_emoji_styles' ] );
            
add_filter'the_title''wp_staticize_emoji' );
            
add_filter'the_excerpt''wp_staticize_emoji' );
            
add_filter'the_content''wp_staticize_emoji' );
            
add_filter'comment_text''wp_staticize_emoji' );
            
add_filter'widget_text''wp_staticize_emoji' );
        }

        
// @todo The wp_mediaelement_fallback() should still run to be injected inside of the audio/video generated by wp_audio_shortcode()/wp_video_shortcode() respectively.
        // Prevent MediaElement.js scripts/styles from being enqueued.
        
add_filter(
            
'wp_video_shortcode_library',
            static function() {
                return 
'amp';
            }
        );
        
add_filter(
            
'wp_audio_shortcode_library',
            static function() {
                return 
'amp';
            }
        );

        
// Don't show loading indicator on custom logo since it makes most sense for larger images.
        
add_filter(
            
'get_custom_logo',
            static function( 
$html ) {
                return 
preg_replace'/(?<=<img\s)/'' data-amp-noloading="" '$html );
            },
            
1
        
);

        
add_action'admin_bar_init', [ __CLASS__'init_admin_bar' ] );
        
add_action'wp_head''amp_add_generator_metadata'20 );
        
add_action'wp_enqueue_scripts', [ __CLASS__'enqueue_assets' ], ); // Enqueue before theme's styles.
        
add_action'wp_enqueue_scripts', [ __CLASS__'dequeue_customize_preview_scripts' ], 1000 );
        
add_filter'customize_partial_render', [ __CLASS__'filter_customize_partial_render' ] );
        if ( 
is_customize_preview() ) {
            
add_filter'style_loader_tag', [ __CLASS__'filter_customize_preview_style_loader_tag' ], 10);
        }

        
add_action'wp_footer''amp_print_analytics' );

        
/*
         * Start output buffering at very low priority for sake of plugins and themes that use template_redirect
         * instead of template_include.
         */
        
$priority defined'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX// phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
        
add_action'template_redirect', [ __CLASS__'start_output_buffering' ], $priority );

        
// Commenting hooks.
        
add_filter'comment_form_defaults', [ __CLASS__'filter_comment_form_defaults' ], PHP_INT_MAX );
        
add_filter'comment_reply_link', [ __CLASS__'filter_comment_reply_link' ], 10);
        
add_filter'cancel_comment_reply_link', [ __CLASS__'filter_cancel_comment_reply_link' ], 10);
        
add_action'comment_form', [ __CLASS__'amend_comment_form' ], 100 );
        
remove_action'comment_form''wp_comment_form_unfiltered_html_nonce' );
        
add_filter'get_comments_link', [ __CLASS__'amend_comments_link' ] );
        
add_filter'respond_link', [ __CLASS__'amend_comments_link' ] );
        
add_filter'wp_kses_allowed_html', [ __CLASS__'include_layout_in_wp_kses_allowed_html' ], 10 );
        
add_filter'get_header_image_tag', [ __CLASS__'amend_header_image_with_video_header' ], PHP_INT_MAX );
        
add_action(
            
'wp_print_footer_scripts',
            static function() {
                
wp_dequeue_script'wp-custom-header' );
            },
            
0
        
);
        
add_action(
            
'wp_enqueue_scripts',
            static function() {
                
wp_dequeue_script'comment-reply' ); // Handled largely by AMP_Comments_Sanitizer and *reply* methods in this class.
            
}
        );
    }

    
/**
     * Register/override widgets.
     *
     * @deprecated As of 2.0 the AMP_Core_Block_Handler will sanitize the core widgets instead.
     * @global WP_Widget_Factory
     * @return void
     */
    
public static function register_widgets() {
        
_deprecated_function__METHOD__'2.0.0' );
        global 
$wp_widget_factory;
        foreach ( 
$wp_widget_factory->widgets as $registered_widget ) {
            
$registered_widget_class_name get_class$registered_widget );
            if ( ! 
preg_match'/^WP_Widget_(.+)$/'$registered_widget_class_name$matches ) ) {
                continue;
            }
            
$amp_class_name 'AMP_Widget_' $matches[1];
            if ( ! 
class_exists$amp_class_name ) || is_a$amp_class_name$registered_widget_class_name ) ) {
                continue;
            }

            
unregister_widget$registered_widget_class_name );
            
register_widget$amp_class_name );
        }
    }

    
/**
     * Register content embed handlers.
     *
     * This was copied from `AMP_Content::register_embed_handlers()` due to being a private method
     * and due to `AMP_Content` not being well suited for use in AMP canonical.
     *
     * @see AMP_Content::register_embed_handlers()
     * @global int $content_width
     * @return AMP_Base_Embed_Handler[] Handlers.
     */
    
public static function register_content_embed_handlers() {
        global 
$content_width;

        
$embed_handlers = [];
        foreach ( 
amp_get_content_embed_handlers() as $embed_handler_class => $args ) {

            
/**
             * Embed handler.
             *
             * @type AMP_Base_Embed_Handler $embed_handler
             */
            
$embed_handler = new $embed_handler_class(
                
array_merge(
                    [
                        
'content_max_width' => ! empty( $content_width ) ? $content_width AMP_Post_Template::CONTENT_MAX_WIDTH// Back-compat.
                    
],
                    
$args
                
)
            );

            if ( ! 
$embed_handler instanceof AMP_Base_Embed_Handler ) {
                
_doing_it_wrong(
                    
__METHOD__,
                    
esc_html(
                        
sprintf(
                            
/* translators: 1: embed handler. 2: AMP_Embed_Handler */
                            
__'Embed Handler (%1$s) must extend `%2$s`''amp' ),
                            
esc_html$embed_handler_class ),
                            
'AMP_Embed_Handler'
                        
)
                    ),
                    
'0.1'
                
);
                continue;
            }

            
$embed_handler->register_embed();
            
$embed_handlers[] = $embed_handler;
        }

        return 
$embed_handlers;
    }

    
/**
     * Add the comments template placeholder marker
     *
     * @codeCoverageIgnore
     * @deprecated 1.1.0 This functionality was moved to AMP_Comments_Sanitizer
     *
     * @param array $args the args for the comments list.
     * @return array Args to return.
     */
    
public static function set_comments_walker$args ) {
        
_deprecated_function__METHOD__'1.1' );
        
$amp_walker     = new AMP_Comment_Walker();
        
$args['walker'] = $amp_walker;
        return 
$args;
    }

    
/**
     * Amend the comment form with the redirect_to field to persist the AMP page after submission.
     */
    
public static function amend_comment_form() {
        
?>
        <?php if ( is_singular() && ! amp_is_canonical() ) : ?>
            <input type="hidden" name="redirect_to" value="<?php echo esc_urlamp_get_permalinkget_the_ID() ) ); ?>">
        <?php endif; ?>
        <?php
    
}

    
/**
     * Amend the comments/redpond links to go to non-AMP page when in legacy Reader mode.
     *
     * @see get_comments_link()
     * @see comments_popup_link()
     *
     * @param string $comments_link Post comments permalink with '#comments' or '#respond' appended.
     * @return string The link to the comments.
     */
    
public static function amend_comments_link$comments_link ) {
        if ( 
amp_is_legacy() && true === AMP_Options_Manager::get_optionOption::MOBILE_REDIRECT ) ) {
            
$comments_link add_query_argQueryVar::NOAMPQueryVar::NOAMP_MOBILE$comments_link );
        }

        return 
$comments_link;
    }

    
/**
     * Prepends template hierarchy with template_dir for AMP transitional mode templates.
     *
     * @param array $templates Template hierarchy.
     * @return array Templates.
     */
    
public static function filter_amp_template_hierarchy$templates ) {
        
$args self::get_theme_support_args();
        if ( isset( 
$args['template_dir'] ) ) {
            
$amp_templates = [];
            foreach ( 
$templates as $template ) {
                
$amp_templates[] = $args['template_dir'] . '/' $template// Let template_dir have precedence.
                
$amp_templates[] = $template;
            }
            
$templates $amp_templates;
        }
        return 
$templates;
    }

    
/**
     * Get canonical URL for current request.
     *
     * @see rel_canonical()
     * @global WP $wp
     * @global WP_Rewrite $wp_rewrite
     * @link https://www.ampproject.org/docs/reference/spec#canon.
     * @link https://core.trac.wordpress.org/ticket/18660
     *
     * @return string Canonical non-AMP URL.
     */
    
public static function get_current_canonical_url() {
        global 
$wp$wp_rewrite;

        
$url null;
        if ( 
is_singular() ) {
            
$url wp_get_canonical_url();
        }

        
// For non-singular queries, make use of the request URI and public query vars to determine canonical URL.
        
if ( empty( $url ) && $wp instanceof WP && $wp_rewrite instanceof WP_Rewrite ) {
            
$added_query_vars $wp->query_vars;
            if ( ! 
$wp_rewrite->permalink_structure || empty( $wp->request ) ) {
                
$url home_url'/' );
            } else {
                
$url home_urluser_trailingslashit$wp->request ) );
                
parse_str$wp->matched_query$matched_query_vars );
                foreach ( 
$wp->query_vars as $key => $value ) {

                    
// Remove query vars that were matched in the rewrite rules for the request.
                    
if ( isset( $matched_query_vars$key ] ) ) {
                        unset( 
$added_query_vars$key ] );
                    }
                }
            }
        }

        if ( ! empty( 
$added_query_vars ) ) {
            
$url add_query_arg$added_query_vars$url );
        }

        return 
amp_remove_endpoint$url );
    }

    
/**
     * Get the ID for the amp-state.
     *
     * @since 0.7
     *
     * @param int $post_id Post ID.
     * @return string ID for amp-state.
     */
    
public static function get_comment_form_state_id$post_id ) {
        return 
sprintf'commentform_post_%d'$post_id );
    }

    
/**
     * Filter comment form args to an element with [text] AMP binding wrap the title reply.
     *
     * @since 0.7
     * @see comment_form()
     *
     * @param array $default_args Comment form arg defaults.
     * @return array Filtered comment form args.
     */
    
public static function filter_comment_form_defaults$default_args ) {

        
// Obtain the actual args provided to the comment_form() function since it is not available in the filter.
        
$args      = [];
        
$backtrace debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Due to limitation in WordPress core.
        
foreach ( $backtrace as $call ) {
            if ( 
'comment_form' === $call['function'] ) {
                
$args = isset( $call['args'][0] ) ? $call['args'][0] : [];
                break;
            }
        }

        
// Abort if the comment_form() was called with arguments which we cannot override the defaults for.
        // @todo This and the debug_backtrace() call above would be unnecessary if WordPress had a comment_form_args filter.
        
$overridden_keys = [ 'cancel_reply_before''title_reply''title_reply_before''title_reply_to' ];
        foreach ( 
$overridden_keys as $key ) {
            if ( 
array_key_exists$key$args ) && array_key_exists$key$default_args ) && $default_args$key ] !== $args$key ] ) {
                return 
$default_args;
            }
        }

        
$state_id     self::get_comment_form_state_idget_the_ID() );
        
$text_binding sprintf(
            
'%s.replyToName ? %s : %s',
            
$state_id,
            
str_replace(
                
'%s',
                
sprintf'" + %s.replyToName + "'$state_id ),
                
wp_json_encode$default_args['title_reply_to'], JSON_UNESCAPED_UNICODE )
            ),
            
wp_json_encode$default_args['title_reply'], JSON_UNESCAPED_UNICODE )
        );

        
$default_args['title_reply_before'] .= sprintf(
            
'<span [text]="%s">',
            
esc_attr$text_binding )
        );
        
$default_args['cancel_reply_before'] = '</span>' $default_args['cancel_reply_before'];
        return 
$default_args;
    }

    
/**
     * Modify the comment reply link for AMP.
     *
     * @since 0.7
     * @see get_comment_reply_link()
     *
     * @param string     $link    The HTML markup for the comment reply link.
     * @param array      $args    An array of arguments overriding the defaults.
     * @param WP_Comment $comment The object of the comment being replied.
     * @return string Comment reply link.
     */
    
public static function filter_comment_reply_link$link$args$comment ) {

        
// Continue to show default link to wp-login when user is not logged-in.
        
if ( get_option'comment_registration' ) && ! is_user_logged_in() ) {
            return 
$args['before'] . $link $args['after'];
        }

        
$state_id  self::get_comment_form_state_idget_the_ID() );
        
$tap_state = [
            
$state_id => [
                
'replyToName' => $comment->comment_author,
                
'values'      => [
                    
'comment_parent' => (string) $comment->comment_ID,
                ],
            ],
        ];

        
// @todo Figure out how to support add_below. Instead of moving the form, what about letting the form get a fixed position?
        
$link sprintf(
            
'<a rel="nofollow" class="comment-reply-link" href="%s" on="%s" aria-label="%s">%s</a>',
            
esc_attr'#' $args['respond_id'] ),
            
esc_attrsprintf'tap:AMP.setState( %s )'wp_json_encode$tap_stateJSON_UNESCAPED_UNICODE ) ) ),
            
esc_attrsprintf$args['reply_to_text'], $comment->comment_author ) ),
            
$args['reply_text']
        );
        return 
$args['before'] . $link $args['after'];
    }

    
/**
     * Filters the cancel comment reply link HTML.
     *
     * @since 0.7
     * @see get_cancel_comment_reply_link()
     *
     * @param string $formatted_link The HTML-formatted cancel comment reply link.
     * @param string $link           Cancel comment reply link URL.
     * @param string $text           Cancel comment reply link text.
     * @return string Cancel reply link.
     */
    
public static function filter_cancel_comment_reply_link$formatted_link$link$text ) {
        if ( empty( 
$text ) ) {
            
$text __'Click here to cancel reply.''default' );
        }

        
$state_id  self::get_comment_form_state_idget_the_ID() );
        
$tap_state = [
            
$state_id => [
                
'replyToName' => '',
                
'values'      => [
                    
'comment_parent' => '0',
                ],
            ],
        ];

        
$respond_id 'respond'// Hard-coded in comment_form() and default value in get_comment_reply_link().
        
return sprintf(
            
'<a id="cancel-comment-reply-link" href="%s" %s [hidden]="%s" on="%s">%s</a>',
            
esc_urlremove_query_arg'replytocom' ) . '#' $respond_id ),
            isset( 
$_GET['replytocom'] ) ? '' ' hidden'// phpcs:ignore
            
esc_attrsprintf'%s.values.comment_parent == "0"'self::get_comment_form_state_idget_the_ID() ) ) ),
            
esc_attrsprintf'tap:AMP.setState( %s )'wp_json_encode$tap_stateJSON_UNESCAPED_UNICODE ) ) ),
            
esc_html$text )
        );
    }

    
/**
     * Configure the admin bar for AMP.
     *
     * @since 1.0
     */
    
public static function init_admin_bar() {
        
add_filter'style_loader_tag', [ __CLASS__'filter_admin_bar_style_loader_tag' ], 10);
        
add_filter'script_loader_tag', [ __CLASS__'filter_admin_bar_script_loader_tag' ], 10);

        
// Inject the data-ampdevmode attribute into the admin bar bump style. See \WP_Admin_Bar::initialize().
        
if ( current_theme_supports'admin-bar' ) ) {
            
$admin_bar_args  get_theme_support'admin-bar' );
            
$header_callback $admin_bar_args[0]['callback'];
        } else {
            
$header_callback '_admin_bar_bump_cb';
        }
        
remove_action'wp_head'$header_callback );
        if ( 
'__return_false' !== $header_callback ) {
            
ob_start();
            
$header_callback();
            
$style ob_get_clean();
            
$data  trimpreg_replace'#<style[^>]*>(.*)</style>#is''$1'$style ) ); // See wp_add_inline_style().

            // Override AMP's position:relative on the body for the sake of the AMP viewer, which is not relevant an an Admin Bar context.
            
if ( amp_is_dev_mode() ) {
                
$data .= 'html:not(#_) > body { position:unset !important; }';
            }

            
wp_add_inline_style'admin-bar'$data );
        }

        
// Emulate customize support script in PHP, to assume Customizer.
        
add_action(
            
'admin_bar_menu',
            static function() {
                
remove_action'wp_before_admin_bar_render''wp_customize_support_script' );
            },
            
41
        
);
        
add_filter(
            
'body_class',
            static function( 
$body_classes ) {
                return 
array_merge(
                    
array_diff(
                        
$body_classes,
                        [ 
'no-customize-support' ]
                    ),
                    [ 
'customize-support' ]
                );
            }
        );
    }

    
/**
     * Recursively determine if a given dependency depends on another.
     *
     * @since 1.3
     *
     * @param WP_Dependencies $dependencies      Dependencies.
     * @param string          $current_handle    Current handle.
     * @param string          $dependency_handle Dependency handle.
     * @return bool Whether the current handle is a dependency of the dependency handle.
     */
    
protected static function has_dependencyWP_Dependencies $dependencies$current_handle$dependency_handle ) {
        if ( 
$current_handle === $dependency_handle ) {
            return 
true;
        }
        if ( ! isset( 
$dependencies->registered$current_handle ] ) ) {
            return 
false;
        }
        foreach ( 
$dependencies->registered$current_handle ]->deps as $handle ) {
            if ( 
self::has_dependency$dependencies$handle$dependency_handle ) ) {
                return 
true;
            }
        }
        return 
false;
    }

    
/**
     * Check if a handle is exclusively a dependency of another handle.
     *
     * For example, check if dashicons is being added exclusively because it is a dependency of admin-bar, as opposed
     * to being added because it was directly enqueued by a theme or a dependency of some other style.
     *
     * @since 1.4.2
     *
     * @param WP_Dependencies $dependencies      Dependencies.
     * @param string          $dependency_handle Dependency handle.
     * @param string          $dependent_handle  Dependent handle.
     * @return bool Whether the $handle is exclusively a handle of the $exclusive_dependency handle.
     */
    
protected static function is_exclusively_dependentWP_Dependencies $dependencies$dependency_handle$dependent_handle ) {

        
// If a dependency handle is the same as the dependent handle, then this self-referential relationship is exclusive.
        
if ( $dependency_handle === $dependent_handle ) {
            return 
true;
        }

        
// Short-circuit if there is no dependency relationship up front.
        
if ( ! self::has_dependency$dependencies$dependent_handle$dependency_handle ) ) {
            return 
false;
        }

        
// Check whether any enqueued handle depends on the dependency.
        
foreach ( $dependencies->queue as $queued_handle ) {
            
// Skip considering the dependent handle.
            
if ( $dependent_handle === $queued_handle ) {
                continue;
            }

            
// If the dependency handle was directly enqueued, then it is not exclusively dependent.
            
if ( $dependency_handle === $queued_handle ) {
                return 
false;
            }

            
// Otherwise, if the dependency handle is depended on by the queued handle while at the same time the queued
            // handle _does_ have a dependency on the supplied dependent handle, then the dependency handle is not
            // exclusively dependent on the dependent handle.
            
if (
                
self::has_dependency$dependencies$queued_handle$dependency_handle )
                &&
                ! 
self::has_dependency$dependencies$queued_handle$dependent_handle )
            ) {
                return 
false;
            }
        }
        return 
true;
    }

    
/**
     * Add data-ampdevmode attribute to any enqueued style that depends on the admin-bar.
     *
     * @since 1.3
     *
     * @param string $tag    The link tag for the enqueued style.
     * @param string $handle The style's registered handle.
     * @return string Tag.
     */
    
public static function filter_admin_bar_style_loader_tag$tag$handle ) {
        if (
            
is_arraywp_styles()->registered['admin-bar']->deps ) && in_array$handlewp_styles()->registered['admin-bar']->depstrue ) ?
                
self::is_exclusively_dependentwp_styles(), $handle'admin-bar' ) :
                
self::has_dependencywp_styles(), $handle'admin-bar' )
        ) {
            
$tag preg_replace'/(?<=<link)(?=\s|>)/i'' ' AMP_Rule_Spec::DEV_MODE_ATTRIBUTE$tag );
        }
        return 
$tag;
    }

    
/**
     * Add data-ampdevmode attribute to any enqueued style that depends on the `customizer-preview` handle.
     *
     * @since 2.0
     *
     * @param string $tag    The link tag for the enqueued style.
     * @param string $handle The style's registered handle.
     * @return string Tag.
     */
    
public static function filter_customize_preview_style_loader_tag$tag$handle ) {
        
$customize_preview 'customize-preview';
        if (
            
is_arraywp_styles()->registered$customize_preview ]->deps ) && in_array$handlewp_styles()->registered$customize_preview ]->depstrue )
                ? 
self::is_exclusively_dependentwp_styles(), $handle$customize_preview )
                : 
self::has_dependencywp_styles(), $handle$customize_preview )
        ) {
            
$tag preg_replace'/(?<=<link)(?=\s|>)/i'' ' AMP_Rule_Spec::DEV_MODE_ATTRIBUTE$tag );
        }

        return 
$tag;
    }

    
/**
     * Add data-ampdevmode attribute to any enqueued script that depends on the admin-bar.
     *
     * @since 1.3
     *
     * @param string $tag    The `<script>` tag for the enqueued script.
     * @param string $handle The script's registered handle.
     * @return string Tag.
     */
    
public static function filter_admin_bar_script_loader_tag$tag$handle ) {
        if (
            
is_arraywp_scripts()->registered['admin-bar']->deps ) && in_array$handlewp_scripts()->registered['admin-bar']->depstrue ) ?
                
self::is_exclusively_dependentwp_scripts(), $handle'admin-bar' ) :
                
self::has_dependencywp_scripts(), $handle'admin-bar' )
        ) {
            
$tag preg_replace'/(?<=<script)(?=\s|>)/i'' ' AMP_Rule_Spec::DEV_MODE_ATTRIBUTE$tag );
        }
        return 
$tag;
    }

    
/**
     * Ensure the markup exists as required by AMP and elements are in the optimal loading order.
     *
     * Ensure meta[charset], meta[name=viewport], and link[rel=canonical] exist, as the validating sanitizer
     * may have removed an illegal meta[http-equiv] or meta[name=viewport]. For a singular post, core only outputs a
     * canonical URL by default. Adds the preload links.
     *
     * @since 0.7
     * @link https://www.ampproject.org/docs/reference/spec#required-markup
     * @link https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp/
     * @todo All of this might be better placed inside of a sanitizer.
     *
     * @param Document $dom            Document.
     * @param string[] $script_handles AMP script handles for components identified during output buffering.
     */
    
public static function ensure_required_markupDocument $dom$script_handles = [] ) {
        
// Gather all links.
        
$links         = [
            
Attribute::REL_PRECONNECT => [
                
// Include preconnect link for AMP CDN for browsers that don't support preload.
                
AMP_DOM_Utils::create_node(
                    
$dom,
                    
Tag::LINK,
                    [
                        
Attribute::REL  => Attribute::REL_PRECONNECT,
                        
Attribute::HREF => 'https://cdn.ampproject.org',
                    ]
                ),
            ],
        ];
        
$link_elements $dom->head->getElementsByTagNameTag::LINK );
        
/**
         * Link element.
         *
         * @var DOMElement $link
         */
        
foreach ( $link_elements as $link ) {
            if ( 
$link->hasAttributeAttribute::REL ) ) {
                
$links$link->getAttributeAttribute::REL ) ][] = $link;
            }
        }

        
// Ensure rel=canonical link.
        
$rel_canonical null;
        if ( empty( 
$links['canonical'] ) ) {
            
$rel_canonical AMP_DOM_Utils::create_node(
                
$dom,
                
Tag::LINK,
                [
                    
Attribute::REL  => Attribute::REL_CANONICAL,
                    
Attribute::HREF => self::get_current_canonical_url(),
                ]
            );
            
$dom->head->appendChild$rel_canonical );
        }

        
// Store the last meta tag as the previous node to append to.
        
$meta_tags     $dom->head->getElementsByTagNameTag::META );
        
$previous_node $meta_tags->length $meta_tags->item$meta_tags->length ) : $dom->head->firstChild;

        
// Handle the title.
        
$title $dom->head->getElementsByTagNameTag::TITLE )->item);
        if ( 
$title ) {
            
$title->parentNode->removeChild$title ); // So we can move it.
            
$dom->head->insertBefore$title$previous_node->nextSibling );
            
$previous_node $title;
        }

        
// Obtain the existing AMP scripts.
        
$amp_scripts     = [];
        
$ordered_scripts = [];
        
$head_scripts    = [];
        
$runtime_src     wp_scripts()->registeredAmp::RUNTIME ]->src;

        
/**
         * Script element.
         *
         * @var DOMElement $script
         */
        
foreach ( $dom->head->getElementsByTagNameTag::SCRIPT ) as $script ) { // Note that prepare_response() already moved body scripts to head.
            
$head_scripts[] = $script;
        }
        foreach ( 
$head_scripts as $script ) {
            
$src $script->getAttributeAttribute::SRC );
            if ( ! 
$src || 'https://cdn.ampproject.org/' !== substr$src027 ) ) {
                continue;
            }
            if ( 
$runtime_src === $src ) {
                
$amp_scriptsAmp::RUNTIME ] = $script;
            } elseif ( 
$script->hasAttributeAttribute::CUSTOM_ELEMENT ) ) {
                
$amp_scripts$script->getAttributeAttribute::CUSTOM_ELEMENT ) ] = $script;
            } elseif ( 
$script->hasAttributeAttribute::CUSTOM_TEMPLATE ) ) {
                
$amp_scripts$script->getAttributeAttribute::CUSTOM_TEMPLATE ) ] = $script;
            } else {
                continue;
            }
            
$script->parentNode->removeChild$script ); // So we can move it.
        
}

        
// Create scripts for any components discovered from output buffering that are missing.
        
foreach ( array_diff$script_handlesarray_keys$amp_scripts ) ) as $missing_script_handle ) {
            if ( ! 
wp_script_is$missing_script_handle'registered' ) ) {
                continue;
            }
            
$attrs = [
                
Attribute::SRC   => wp_scripts()->registered$missing_script_handle ]->src,
                
Attribute::ASYNC => '',
            ];
            if ( 
Extension::MUSTACHE === $missing_script_handle ) {
                
$attrsAttribute::CUSTOM_TEMPLATE ] = $missing_script_handle;
            } else {
                
$attrsAttribute::CUSTOM_ELEMENT ] = $missing_script_handle;
            }

            
$amp_scripts$missing_script_handle ] = AMP_DOM_Utils::create_node$domTag::SCRIPT$attrs );
        }

        
// Remove scripts that had already been added but couldn't be detected from output buffering.
        
$extension_specs            AMP_Allowed_Tags_Generated::get_extension_specs();
        
$superfluous_script_handles array_diff(
            
array_keys$amp_scripts ),
            
array_merge$script_handles, [ Amp::RUNTIME ] )
        );
        foreach ( 
$superfluous_script_handles as $superfluous_script_handle ) {
            if ( ! empty( 
$extension_specs$superfluous_script_handle ]['requires_usage'] ) ) {
                unset( 
$amp_scripts$superfluous_script_handle ] );
            }
        }

        
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
         *
         * "2. Next, preload the AMP runtime v0.js <script> tag with <link as=script href=https://cdn.ampproject.org/v0.js rel=preload>.
         * The AMP runtime should start downloading as soon as possible because the AMP boilerplate hides the document via body { visibility:hidden }
         * until the AMP runtime has loaded. Preloading the AMP runtime tells the browser to download the script with a higher priority."
         * {@link https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp/ Optimize the AMP Runtime loading}
         */
        
$prioritized_preloads = [];
        if ( ! isset( 
$linksAttribute::REL_PRELOAD ] ) ) {
            
$linksAttribute::REL_PRELOAD ] = [];
        }

        
$prioritized_preloads[] = AMP_DOM_Utils::create_node(
            
$dom,
            
Tag::LINK,
            [
                
Attribute::REL  => Attribute::REL_PRELOAD,
                
'as'            => Tag::SCRIPT,
                
Attribute::HREF => $runtime_src,
            ]
        );

        
/*
         * "3. If your page includes render-delaying extensions (e.g., amp-experiment, amp-dynamic-css-classes, amp-story),
         * preload those extensions as they're required by the AMP runtime for rendering the page."
         */
        
$amp_script_handles array_keys$amp_scripts );
        foreach ( 
array_intersectAmp::RENDER_DELAYING_EXTENSIONS$amp_script_handles ) as $script_handle ) {
            if ( ! 
in_array$script_handleAmp::RENDER_DELAYING_EXTENSIONStrue ) ) {
                continue;
            }
            
$prioritized_preloads[] = AMP_DOM_Utils::create_node(
                
$dom,
                
Tag::LINK,
                [
                    
Attribute::REL  => Attribute::REL_PRELOAD,
                    
'as'            => Tag::SCRIPT,
                    
Attribute::HREF => $amp_scripts$script_handle ]->getAttributeAttribute::SRC ),
                ]
            );
        }
        
$linksAttribute::REL_PRELOAD ] = array_merge$prioritized_preloads$linksAttribute::REL_PRELOAD ] );

        
/*
         * "4. Use preconnect to speedup the connection to other origin where the full resource URL is not known ahead of time,
         * for example, when using Google Fonts."
         *
         * Note that \AMP_Style_Sanitizer::process_link_element() will ensure preconnect links for Google Fonts are present.
         */
        
$link_relations = [ Attribute::REL_PRECONNECTAttribute::REL_DNS_PREFETCHAttribute::REL_PRELOADAttribute::REL_PRERENDERAttribute::REL_PREFETCH ];
        foreach ( 
$link_relations as $rel ) {
            if ( ! isset( 
$links$rel ] ) ) {
                continue;
            }
            foreach ( 
$links$rel ] as $link ) {
                if ( 
$link->parentNode ) {
                    
$link->parentNode->removeChild$link ); // So we can move it.
                
}
                
$dom->head->insertBefore$link$previous_node->nextSibling );
                
$previous_node $link;
            }
        }

        
// "5. Load the AMP runtime."
        
if ( isset( $amp_scriptsAmp::RUNTIME ] ) ) {
            
$ordered_scriptsAmp::RUNTIME ] = $amp_scriptsAmp::RUNTIME ];
            unset( 
$amp_scriptsAmp::RUNTIME ] );
        } else {
            
$script $dom->createElementTag::SCRIPT );
            
$script->setAttributeAttribute::ASYNC'' );
            
$script->setAttributeAttribute::SRC$runtime_src );
            
$ordered_scriptsAmp::RUNTIME ] = $script;
        }

        
/*
         * "6. Specify the <script> tags for render-delaying extensions (e.g., amp-experiment amp-dynamic-css-classes and amp-story"
         *
         * {@link https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp/ AMP Hosting Guide}
         */
        
foreach ( Amp::RENDER_DELAYING_EXTENSIONS as $extension ) {
            if ( isset( 
$amp_scripts$extension ] ) ) {
                
$ordered_scripts$extension ] = $amp_scripts$extension ];
                unset( 
$amp_scripts$extension ] );
            }
        }

        
/*
         * "7. Specify the <script> tags for remaining extensions (e.g., amp-bind ...). These extensions are not render-delaying
         * and therefore should not be preloaded as they might take away important bandwidth for the initial render."
         */
        
ksort$amp_scripts );
        
$ordered_scripts array_merge$ordered_scripts$amp_scripts );
        foreach ( 
$ordered_scripts as $ordered_script ) {
            
$dom->head->insertBefore$ordered_script$previous_node->nextSibling );
            
$previous_node $ordered_script;
        }

        unset( 
$previous_node );
    }

    
/**
     * Dequeue Customizer assets which are not necessary outside the preview iframe.
     *
     * Prevent enqueueing customize-preview styles if not in customizer preview iframe.
     * These are only needed for when there is live editing of content, such as selective refresh.
     *
     * @since 0.7
     */
    
public static function dequeue_customize_preview_scripts() {

        
// Dequeue styles unnecessary unless in customizer preview iframe when editing (such as for edit shortcuts).
        
if ( ! self::is_customize_preview_iframe() ) {
            
wp_dequeue_style'customize-preview' );
            foreach ( 
wp_styles()->registered as $handle => $dependency ) {
                if ( 
in_array'customize-preview'$dependency->depstrue ) ) {
                    
wp_dequeue_style$handle );
                }
            }
        }
    }

    
/**
     * Start output buffering.
     *
     * @since 0.7
     * @see AMP_Theme_Support::finish_output_buffering()
     */
    
public static function start_output_buffering() {
        
/*
         * Disable the New Relic Browser agent on AMP responses.
         * This prevents the New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset:
         * https://docs.newrelic.com/docs/browser/new-relic-browser/troubleshooting/google-amp-validator-fails-due-3rd-party-script
         * Sites with New Relic will need to specially configure New Relic for AMP:
         * https://docs.newrelic.com/docs/browser/new-relic-browser/installation/monitor-amp-pages-new-relic-browser
         */
        
if ( function_exists'newrelic_disable_autorum' ) ) {
            
newrelic_disable_autorum();
        }

        
ob_start( [ __CLASS__'finish_output_buffering' ] );
        
self::$is_output_buffering true;
    }

    
/**
     * Determine whether output buffering has started.
     *
     * @since 0.7
     * @see AMP_Theme_Support::start_output_buffering()
     * @see AMP_Theme_Support::finish_output_buffering()
     *
     * @return bool Whether output buffering has started.
     */
    
public static function is_output_buffering() {
        return 
self::$is_output_buffering;
    }

    
/**
     * Finish output buffering.
     *
     * @since 0.7
     * @see AMP_Theme_Support::start_output_buffering()
     *
     * @param string $response Buffered Response.
     * @return string Processed Response.
     */
    
public static function finish_output_buffering$response ) {
        
self::$is_output_buffering false;

        try {
            
$response self::prepare_response$response );
        } catch ( 
Exception $exception ) {
            
$title   __'Failed to prepare AMP page''amp' );
            
$message __'A PHP error occurred while trying to prepare the AMP response. This may not be caused by the AMP plugin but by some other active plugin or the current theme. You will need to review the error details to determine the source of the error.''amp' );

            
$error_page Services::get'dev_tools.error_page' );

            
$error_page
                
->with_title$title )
                ->
with_message$message )
                ->
with_exception$exception )
                ->
with_response_code500 );

            
// Add link to non-AMP version if not canonical.
            
if ( ! amp_is_canonical() ) {
                
$non_amp_url amp_remove_endpointamp_get_current_url() );

                
// Prevent user from being redirected back to AMP version.
                
if ( true === AMP_Options_Manager::get_optionOption::MOBILE_REDIRECT ) ) {
                    
$non_amp_url add_query_argQueryVar::NOAMPQueryVar::NOAMP_MOBILE$non_amp_url );
                }

                
$error_page->with_back_link(
                    
$non_amp_url,
                    
__'Go to non-AMP version''amp' )
                );
            }

            
$response $error_page->render();
        }

        
/**
         * Fires when server timings should be sent.
         *
         * This is immediately before the processed output buffer is sent to the client.
         *
         * @since 2.0
         * @internal
         */
        
do_action'amp_server_timing_send' );
        return 
$response;
    }

    
/**
     * Filter rendered partial to convert to AMP.
     *
     * @see WP_Customize_Partial::render()
     *
     * @param string|mixed $partial Rendered partial.
     * @return string|mixed Filtered partial.
     * @global int $content_width
     */
    
public static function filter_customize_partial_render$partial ) {
        global 
$content_width;
        if ( 
is_string$partial ) && preg_match'/<\w/'$partial ) ) {
            
$dom  AMP_DOM_Utils::get_dom_from_content$partial );
            
$args = [
                
'content_max_width'    => ! empty( $content_width ) ? $content_width AMP_Post_Template::CONTENT_MAX_WIDTH// Back-compat.
                
'use_document_element' => true,
            ];
            
AMP_Content_Sanitizer::sanitize_document$domself::$sanitizer_classes$args ); // @todo Include script assets in response?

            // Move any amp-custom to include in the partial response.
            
$amp_custom_style $dom->xpath->query'//style[ @amp-custom ]' )->item);
            if ( 
$amp_custom_style instanceof DOMElement ) {
                
$amp_custom_style->removeAttributeAttribute::AMP_CUSTOM );
                
$amp_custom_style->setAttribute'amp-custom-partial''' );
                
$amp_custom_style->textContent str_replace(
                    
'/*# sourceURL=amp-custom.css */',
                    
'/*# sourceURL=amp-custom-partial.css */',
                    
$amp_custom_style->textContent
                
);
                
$dom->body->appendChild$amp_custom_style ); // @todo This could cause layout problems. It may be preferable to move to the head.
            
}

            
$partial AMP_DOM_Utils::get_content_from_dom$dom );
        }
        return 
$partial;
    }

    
/**
     * Process response to ensure AMP validity.
     *
     * @since 0.7
     *
     * @param string $response HTML document response. By default it expects a complete document.
     * @param array  $args     Args to send to the preprocessor/sanitizer/optimizer.
     * @return string AMP document response.
     * @global int $content_width
     */
    
public static function prepare_response$response$args = [] ) {
        global 
$content_width;
        
$last_error error_get_last();

        if ( isset( 
$args['validation_error_callback'] ) ) {
            
_doing_it_wrong__METHOD__'Do not supply validation_error_callback arg.''1.0' );
            unset( 
$args['validation_error_callback'] );
        }

        
$status_code http_response_code();

        
/*
         * Send a JSON response when the site is failing to handle AMP form submissions with a JSON response as required
         * or an AMP-Redirect-To response header was not sent. This is a common scenario for plugins that handle form
         * submissions and show the success page via the POST request's response body instead of invoking wp_redirect(),
         * in which case AMP_HTTP::intercept_post_request_redirect() will automatically send the AMP-Redirect-To header.
         * If the POST response is an HTML document then the form submission will appear to not have worked since there
         * is no success or failure message shown. By catching the case where HTML is sent in the response, we can
         * automatically send a generic success message when a 200 status is returned or a failure message when a 400+
         * response code is sent.
         */
        
$is_form_submission = (
            isset( 
AMP_HTTP::$purged_amp_query_varsAMP_HTTP::ACTION_XHR_CONVERTED_QUERY_VAR ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            
&&
            isset( 
$_SERVER['REQUEST_METHOD'] )
            &&
            
'POST' === $_SERVER['REQUEST_METHOD']
        );
        if ( 
$is_form_submission && null === json_decode$response ) && json_last_error() && ( is_bool$status_code ) || ( $status_code >= 200 && $status_code 300 ) || $status_code >= 400 ) ) {
            if ( 
is_bool$status_code ) ) {
                
$status_code 200// Not a web server environment.
            
}
            return 
wp_json_encode(
                [
                    
'status_code' => $status_code,
                    
'status_text' => get_status_header_desc$status_code ),
                ]
            );
        }

        
// Abort if response type is not HTML.
        
if ( Attribute::TYPE_HTML !== substrAMP_HTTP::get_response_content_type(), 0) ) {
            return 
$response;
        }

        
// Abort if an expected template action didn't fire or if the HTML tag does not have the AMP attribute.
        
if ( ! (
            
did_action'wp_head' )
            ||
            
did_action'wp_footer' )
            ||
            
did_action'amp_post_template_head' )
            ||
            
did_action'amp_post_template_footer' )
            ||
            
preg_match(
                
sprintf(
                    
'#^(?:<!.*?>|\s+)*+<html(?=\s)[^>]*?\s(%1$s|%2$s|%3$s)(\s|=|>)#is',
                    
preg_quoteAttribute::AMP'#' ),
                    
preg_quoteAttribute::AMP_EMOJI'#' ),
                    
preg_quoteAttribute::AMP_EMOJI_ALT'#' )
                ),
                
$response
            
)
        ) ) {
            return 
$response;
        }

        
// Enforce UTF-8 encoding as it is a requirement for AMP.
        
if ( ! headers_sent() ) {
            
header'Content-Type: text/html; charset=utf-8' );
        }

        
$args array_merge(
            [
                
'content_max_width'    => ! empty( $content_width ) ? $content_width AMP_Post_Template::CONTENT_MAX_WIDTH// Back-compat.
                
'use_document_element' => true,
                
'user_can_validate'    => AMP_Validation_Manager::has_cap(),
            ],
            
$args
        
);

        
/**
         * Stops the server-timing measurement for the entire output buffer capture.
         *
         * @since 2.0
         * @internal
         *
         * @param string $event_name Name of the event to stop.
         */
        
do_action'amp_server_timing_stop''amp_output_buffer' );

        
/**
         * Starts the server-timing measurement for the dom parsing subsystem.
         *
         * @since 2.0
         * @internal
         *
         * @param string      $event_name        Name of the event to record.
         * @param string|null $event_description Optional. Description of the event
         *                                       to record. Defaults to null.
         * @param string[]    $properties        Optional. Additional properties to add
         *                                       to the logged record.
         * @param bool        $verbose_only      Optional. Whether to only show the
         *                                       event in verbose mode. Defaults to
         *                                       false.
         */
        
do_action'amp_server_timing_start''amp_dom_parse''', [], true );

        
$dom Document::fromHtml$response );

        if ( 
AMP_Validation_Manager::$is_validate_request ) {
            
AMP_Validation_Manager::remove_illegal_source_stack_comments$dom );
        }

        
/**
         * Stops the server-timing measurement for the dom parsing subsystem.
         *
         * @since 2.0
         * @internal
         *
         * @param string $event_name Name of the event to stop.
         */
        
do_action'amp_server_timing_stop''amp_dom_parse' );

        
/**
         * Starts the server-timing measurement for the AMP Sanitizer subsystem.
         *
         * @since 2.0
         * @internal
         *
         * @param string      $event_name        Name of the event to record.
         * @param string|null $event_description Optional. Description of the event
         *                                       to record. Defaults to null.
         * @param string[]    $properties        Optional. Additional properties to add
         *                                       to the logged record.
         * @param bool        $verbose_only      Optional. Whether to only show the
         *                                       event in verbose mode. Defaults to
         *                                       false.
         */
        
do_action'amp_server_timing_start''amp_sanitizer' );

        
// Make sure scripts from the body get moved to the head.
        
foreach ( $dom->xpath->query'//body//script[ @custom-element or @custom-template or @src = "https://cdn.ampproject.org/v0.js" ]' ) as $script ) {
            
$dom->head->appendChild$script->parentNode->removeChild$script ) );
        }

        
// Ensure the mandatory amp attribute is present on the html element.
        
if ( ! $dom->documentElement->hasAttributeAttribute::AMP )
            && ! 
$dom->documentElement->hasAttributeAttribute::AMP_EMOJI )
            && ! 
$dom->documentElement->hasAttributeAttribute::AMP_EMOJI_ALT ) ) {
            
$dom->documentElement->setAttributeAttribute::AMP'' );
        }

        
$sanitization_results AMP_Content_Sanitizer::sanitize_document$domself::$sanitizer_classes$args );

        
/**
         * Stops the server-timing measurement for the AMP Sanitizer subsystem.
         *
         * @since 2.0
         * @internal
         *
         * @param string $event_name Name of the event to stop.
         */
        
do_action'amp_server_timing_stop''amp_sanitizer' );

        
// Respond early with results if performing a validate request.
        
if ( AMP_Validation_Manager::$is_validate_request ) {
            
status_header200 );
            
header'Content-Type: application/json; charset=utf-8' );
            
$data = [
                
'http_status_code' => $status_code,
                
'php_fatal_error'  => false,
            ];
            if ( 
$last_error && in_array$last_error['type'], [ E_ERRORE_RECOVERABLE_ERRORE_CORE_ERRORE_COMPILE_ERRORE_USER_ERRORE_PARSE ], true ) ) {
                
$data['php_fatal_error'] = $last_error;
            }
            
$data array_merge$dataAMP_Validation_Manager::get_validate_response_data$sanitization_results ) );
            return 
wp_json_encode$dataJSON_PRETTY_PRINT JSON_UNESCAPED_SLASHES );
        }

        
/**
         * Starts the server-timing measurement for the AMP DOM serialization.
         *
         * @since 2.0
         * @internal
         *
         * @param string      $event_name        Name of the event to record.
         * @param string|null $event_description Optional. Description of the event
         *                                       to record. Defaults to null.
         * @param string[]    $properties        Optional. Additional properties to add
         *                                       to the logged record.
         * @param bool        $verbose_only      Optional. Whether to only show the
         *                                       event in verbose mode. Defaults to
         *                                       false.
         */
        
do_action'amp_server_timing_start''amp_dom_serialize''', [], true );

        
// Gather all component scripts that are used in the document and then render any not already printed.
        
$amp_scripts $sanitization_results['scripts'];
        foreach ( 
self::$embed_handlers as $embed_handler ) {
            
$amp_scripts array_merge(
                
$amp_scripts,
                
$embed_handler->get_scripts()
            );
        }
        foreach ( 
$amp_scripts as $handle => $src ) {
            
/*
             * Make sure the src is up-to-date. This allows for embed handlers to override the
             * default extension version by defining a different URL.
             */
            
if ( is_string$src ) && wp_script_is$handle'registered' ) ) {
                
wp_scripts()->registered$handle ]->src $src;
            }
        }

        
$enable_optimizer array_key_existsConfigurationArgument::ENABLE_OPTIMIZER$args )
            ? 
$argsConfigurationArgument::ENABLE_OPTIMIZER ]
            : 
true;

        
/**
         * Filter whether the generated HTML output should be run through the AMP Optimizer or not.
         *
         * @since 1.5.0
         *
         * @param bool $enable_optimizer Whether the generated HTML output should be run through the AMP Optimizer or not.
         */
        
$enable_optimizer apply_filters'amp_enable_optimizer'$enable_optimizer );

        if ( 
$enable_optimizer ) {
            
/**
             * Starts the server-timing measurement for the AMP Optimizer subsystem.
             *
             * @since 2.0
             * @internal
             *
             * @param string      $event_name        Name of the event to record.
             * @param string|null $event_description Optional. Description of the event
             *                                       to record. Defaults to null.
             * @param string[]    $properties        Optional. Additional properties to add
             *                                       to the logged record.
             * @param bool        $verbose_only      Optional. Whether to only show the
             *                                       event in verbose mode. Defaults to
             *                                       false.
             */
            
do_action'amp_server_timing_start''amp_optimizer' );

            
$errors = new Optimizer\ErrorCollection();
            
self::get_optimizer$args )->optimizeDom$dom$errors );

            if ( 
count$errors ) > ) {
                
$error_messages array_map(
                    static function( 
Optimizer\Error $error ) {
                        return 
' - ' $error->getCode() . ': ' $error->getMessage();
                    },
                    
iterator_to_array$errors )
                );
                
$dom->head->appendChild(
                    
$dom->createComment"\n" __'AMP optimization could not be completed due to the following:''amp' ) . "\n" implode"\n"$error_messages ) . "\n" )
                );
                
// @todo Include errors elsewhere than HTML comment?
            
}

            
/**
             * Stops the server-timing measurement for the AMP Optimizer subsystem.
             *
             * @since 2.0
             * @internal
             *
             * @param string $event_name Name of the event to stop.
             */
            
do_action'amp_server_timing_stop''amp_optimizer' );
        }

        
self::ensure_required_markup$domarray_keys$amp_scripts ) );

        
$can_serve AMP_Validation_Manager::finalize_validation$dom );

        
// Redirect to the non-AMP version if not on an AMP-first site.
        
if ( ! $can_serve && ! amp_is_canonical() ) {
            
$non_amp_url amp_remove_endpointamp_get_current_url() );

            
// Redirect to include query var to preventing AMP from even being considered available.
            
$non_amp_url add_query_argQueryVar::NOAMPQueryVar::NOAMP_AVAILABLE$non_amp_url );

            
wp_safe_redirect$non_amp_url302 );
            return 
esc_html__'Redirecting since AMP version not available.''amp' );
        }

        
$response $dom->saveHTML();

        
/**
         * Stops the server-timing measurement for the AMP DOM serialization.
         *
         * @since 2.0
         * @internal
         *
         * @param string $event_name Name of the event to stop.
         */
        
do_action'amp_server_timing_stop''amp_dom_serialize' );

        return 
$response;
    }

    
/**
     * Optimizer instance to use.
     *
     * @param array $args Associative array of arguments to pass into the transformation engine.
     * @return Optimizer\TransformationEngine Optimizer transformation engine to use.
     */
    
private static function get_optimizer$args ) {
        
$configuration self::get_optimizer_configuration$args );

        
$fallback_remote_request_pipeline = new FallbackRemoteGetRequest(
            new 
WpHttpRemoteGetRequest(),
            new 
FilesystemRemoteGetRequestOptimizer\LocalFallback::getMappings() )
        );

        
$cached_remote_request = new CachedRemoteGetRequest$fallback_remote_request_pipelineWEEK_IN_SECONDS );

        return new 
Optimizer\TransformationEngine(
            
$configuration,
            
$cached_remote_request
        
);
    }

    
/**
     * Get the AmpProject\Optimizer configuration object to use.
     *
     * @param array $args Associative array of arguments to pass into the transformation engine.
     * @return Optimizer\Configuration Optimizer configuration to use.
     */
    
private static function get_optimizer_configuration$args ) {
        
$transformers Optimizer\Configuration::DEFAULT_TRANSFORMERS;

        
$enable_ssr array_key_existsConfigurationArgument::ENABLE_SSR$args )
            ? 
$argsConfigurationArgument::ENABLE_SSR ]
            : 
true;

        
/**
         * Filter whether the AMP Optimizer should use server-side rendering or not.
         *
         * @since 1.5.0
         *
         * @param bool $enable_ssr Whether the AMP Optimizer should use server-side rendering or not.
         */
        
$enable_ssr apply_filters'amp_enable_ssr'$enable_ssr );

        
// In debugging mode, we don't use server-side rendering, as it further obfuscates the HTML markup.
        
if ( ! $enable_ssr ) {
            
$transformers array_diff(
                
$transformers,
                [
                    
Optimizer\Transformer\AmpRuntimeCss::class,
                    
Optimizer\Transformer\ServerSideRendering::class,
                    
Optimizer\Transformer\TransformedIdentifier::class,
                ]
            );
        }

        
array_unshift$transformersTransformer\AmpSchemaOrgMetadata::class );

        
/**
         * Filter the configuration to be used for the AMP Optimizer.
         *
         * @since 1.5.0
         *
         * @param array $configuration Associative array of configuration data.
         */
        
$configuration apply_filters(
            
'amp_optimizer_config',
            
array_merge(
                [ 
Optimizer\Configuration::KEY_TRANSFORMERS => $transformers ],
                
$args
            
)
        );

        
$config = new Optimizer\Configuration$configuration );
        
$config->registerConfigurationClass(
            
Transformer\AmpSchemaOrgMetadata::class,
            
Transformer\AmpSchemaOrgMetadataConfiguration::class
        );

        return 
$config;
    }

    
/**
     * Adds 'data-amp-layout' to the allowed <img> attributes for wp_kses().
     *
     * @since 0.7
     *
     * @param array $context Allowed tags and their allowed attributes.
     * @return array $context Filtered allowed tags and attributes.
     */
    
public static function include_layout_in_wp_kses_allowed_html$context ) {
        if ( ! empty( 
$contextTag::IMG ][ Attribute::WIDTH ] ) && ! empty( $contextTag::IMG ][ Attribute::HEIGHT ] ) ) {
            
$contextTag::IMG ]['data-amp-layout'] = true;
        }

        return 
$context;
    }

    
/**
     * Enqueue AMP assets if this is an AMP endpoint.
     *
     * @since 0.7
     *
     * @return void
     */
    
public static function enqueue_assets() {
        
// Enqueue default styles expected by sanitizer.
        
wp_enqueue_style'amp-default' );
    }

    
/**
     * Setup pages to have the paired browsing client script so that the app can interact with it.
     *
     * @since 1.5.0
     *
     * @return void
     */
    
public static function setup_paired_browsing_client() {
        
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
        
if ( isset( $_GETself::PAIRED_BROWSING_QUERY_VAR ] ) ) {
            return;
        }

        
// Paired browsing requires a custom script which in turn requires dev mode.
        
if ( ! amp_is_dev_mode() ) {
            return;
        }

        
/**
         * Fires before registering plugin assets that may require core asset polyfills.
         *
         * @internal
         */
        
do_action'amp_register_polyfills' );

        
$asset_file   AMP__DIR__ '/assets/js/amp-paired-browsing-client.asset.php';
        
$asset        = require $asset_file;
        
$dependencies $asset['dependencies'];
        
$version      $asset['version'];

        
wp_enqueue_script(
            
'amp-paired-browsing-client',
            
amp_get_asset_url'/js/amp-paired-browsing-client.js' ),
            
$dependencies,
            
$version,
            
true
        
);

        
// Mark enqueued script for AMP dev mode so that it is not removed.
        // @todo Revisit with <https://github.com/google/site-kit-wp/pull/505#discussion_r348683617>.
        
add_filter(
            
'script_loader_tag',
            static function( 
$tag$handle ) {
                if ( 
amp_is_request() && self::has_dependencywp_scripts(), 'amp-paired-browsing-client'$handle ) ) {
                    
$tag preg_replace'/(?<=<script)(?=\s|>)/i'' ' AMP_Rule_Spec::DEV_MODE_ATTRIBUTE$tag );
                }
                return 
$tag;
            },
            
10,
            
2
        
);
    }

    
/**
     * Get paired browsing URL for a given URL.
     *
     * @since 1.5.0
     *
     * @param string $url URL.
     * @return string Paired browsing URL.
     */
    
public static function get_paired_browsing_url$url null ) {
        if ( ! 
$url ) {
            
$url wp_unslash$_SERVER['REQUEST_URI'] );
        }
        
$url remove_query_arg(
            [ 
amp_get_slug(), QueryVar::NOAMPAMP_Validated_URL_Post_Type::VALIDATE_ACTIONAMP_Validation_Manager::VALIDATION_ERROR_TERM_STATUS_QUERY_VAR ],
            
$url
        
);
        
$url add_query_argself::PAIRED_BROWSING_QUERY_VAR'1'$url );
        return 
$url;
    }

    
/**
     * Remove any unnecessary query vars that could hamper the paired browsing experience.
     *
     * @since 1.5.0
     */
    
public static function sanitize_url_for_paired_browsing() {
        if ( isset( 
$_GETself::PAIRED_BROWSING_QUERY_VAR ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            
$original_url wp_unslash$_SERVER['REQUEST_URI'] );
            
$updated_url  self::get_paired_browsing_url$original_url );
            if ( 
$updated_url !== $original_url ) {
                
wp_safe_redirect$updated_url );
                exit;
            }
        }
    }

    
/**
     * Serve paired browsing experience if it is being requested.
     *
     * Includes a custom template that acts as an interface to facilitate a side-by-side comparison of a
     * non-AMP page and its AMP version to review any discrepancies.
     *
     * @since 1.5.0
     *
     * @param string $template Path of the template to include.
     * @return string Custom template if in paired browsing mode, else the supplied template.
     */
    
public static function serve_paired_browsing_experience$template ) {
        
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
        
if ( ! isset( $_GETself::PAIRED_BROWSING_QUERY_VAR ] ) ) {
            return 
$template;
        }

        if ( ! 
amp_is_dev_mode() ) {
            
wp_die(
                
esc_html__'Paired browsing is only available when AMP dev mode is enabled (e.g. when logged-in and admin bar is showing).''amp' ),
                
esc_html__'AMP Paired Browsing Unavailable''amp' ),
                [ 
'response' => 403 ]
            );
        }

        
/** This action is documented in includes/class-amp-theme-support.php */
        
do_action'amp_register_polyfills' );

        
wp_enqueue_style(
            
'amp-paired-browsing-app',
            
amp_get_asset_url'/css/amp-paired-browsing-app.css' ),
            [ 
'dashicons' ],
            
AMP__VERSION
        
);

        
wp_styles()->add_data'amp-paired-browsing-app''rtl''replace' );

        
$asset_file   AMP__DIR__ '/assets/js/amp-paired-browsing-app.asset.php';
        
$asset        = require $asset_file;
        
$dependencies $asset['dependencies'];
        
$version      $asset['version'];

        
wp_enqueue_script(
            
'amp-paired-browsing-app',
            
amp_get_asset_url'/js/amp-paired-browsing-app.js' ),
            
$dependencies,
            
$version,
            
true
        
);

        
wp_localize_script(
            
'amp-paired-browsing-app',
            
'app',
            [
                
'ampSlug'                   => amp_get_slug(),
                
'ampPairedBrowsingQueryVar' => self::PAIRED_BROWSING_QUERY_VAR,
                
'noampQueryVar'             => QueryVar::NOAMP,
                
'noampMobile'               => QueryVar::NOAMP_MOBILE,
                
'documentTitlePrefix'       => __'AMP Paired Browsing:''amp' ),
            ]
        );

        return 
AMP__DIR__ '/includes/templates/amp-paired-browsing.php';
    }

    
/**
     * Print the important emoji-related styles.
     *
     * @see print_emoji_styles()
     * @staticvar bool $printed
     */
    
public static function print_emoji_styles() {
        static 
$printed false;

        if ( 
$printed ) {
            return;
        }

        
$printed true;
        
?>
        <style type="text/css">
            img.wp-smiley,
            img.emoji {
                display: inline-block !important; /* Patched from core, which had display:inline */
                border: none !important;
                box-shadow: none !important;
                height: 1em !important;
                width: 1em !important;
                margin: 0 .07em !important;
                vertical-align: -0.1em !important;
                background: none !important;
                padding: 0 !important;
            }
        </style>
        <?php
    
}

    
/**
     * Conditionally replace the header image markup with a header video or image.
     *
     * This is JS-driven in Core themes like Twenty Sixteen and Twenty Seventeen.
     * So in order for the header video to display, this replaces the markup of the header image.
     *
     * @since 1.0
     * @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L54
     * @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L78
     *
     * @param string $image_markup The image markup to filter.
     * @return string $html Filtered markup.
     */
    
public static function amend_header_image_with_video_header$image_markup ) {

        
// If there is no video, just pass the image through.
        
if ( ! has_header_video() || ! is_header_video_active() ) {
            return 
$image_markup;
        }

        
$video_settings   get_header_video_settings();
        
$parsed_url       wp_parse_url$video_settings['videoUrl'] );
        
$query            = isset( $parsed_url['query'] ) ? wp_parse_args$parsed_url['query'] ) : [];
        
$video_attributes = [
            
Attribute::MEDIA    => '(min-width: ' $video_settings['minWidth'] . 'px)',
            
Attribute::WIDTH    => $video_settingsAttribute::WIDTH ],
            
Attribute::HEIGHT   => $video_settingsAttribute::HEIGHT ],
            
Attribute::LAYOUT   => 'responsive',
            
Attribute::AUTOPLAY => '',
            
Attribute::LOOP     => '',
            
Attribute::ID       => 'wp-custom-header-video',
        ];

        
$youtube_id null;
        if ( isset( 
$parsed_url['host'] ) && preg_match'/(^|\.)(youtube\.com|youtu\.be)$/'$parsed_url['host'] ) ) {
            if ( 
'youtu.be' === $parsed_url['host'] && ! empty( $parsed_url['path'] ) ) {
                
$youtube_id trim$parsed_url['path'], '/' );
            } elseif ( isset( 
$query['v'] ) ) {
                
$youtube_id $query['v'];
            }
        }

        
// If the video URL is for YouTube, return an <amp-youtube> element.
        
if ( ! empty( $youtube_id ) ) {
            
$video_markup AMP_HTML_Utils::build_tag(
                
Extension::YOUTUBE,
                
array_merge(
                    
$video_attributes,
                    [
                        
'data-videoid'              => $youtube_id,

                        
// For documentation on the params, see <https://developers.google.com/youtube/player_parameters>.
                        
'data-param-rel'            => '0'// Don't show related videos.
                        
'data-param-showinfo'       => '0'// Don't show video title at the top.
                        
'data-param-controls'       => '0'// Don't show video controls.
                        
'data-param-iv_load_policy' => '3'// Suppress annotations.
                        
'data-param-modestbranding' => '1'// Show modest branding.
                        
'data-param-playsinline'    => '1'// Prevent fullscreen playback on iOS.
                        
'data-param-disablekb'      => '1'// Disable keyboard conttrols.
                        
'data-param-fs'             => '0'// Suppress full screen button.
                    
]
                )
            );

            
// Hide equalizer video animation.
            
$video_markup .= '<style>#wp-custom-header-video .amp-video-eq { display:none; }</style>';
        } else {
            
$video_markup AMP_HTML_Utils::build_tag(
                
Extension::VIDEO,
                
array_merge(
                    
$video_attributes,
                    [
                        
Attribute::SRC => $video_settings['videoUrl'],
                    ]
                )
            );
        }

        return 
$image_markup $video_markup;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586