Skip to content
Merged
5 changes: 5 additions & 0 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
'DOMXPath',
),
),
'filter' => array(
'functions' => array(
'filter_var',
),
),
// Required by PHP-CSS-Parser.
'iconv' => array(
'functions' => array( 'iconv' ),
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"ext-curl": "*",
"ext-date": "*",
"ext-dom": "*",
"ext-filter": "*",
"ext-iconv": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-spl": "*",
"ampproject/amp-toolbox": "^0.3",
"ampproject/amp-toolbox": "dev-main",
"cweagans/composer-patches": "1.7.0",
"fasterimage/fasterimage": "1.5.0",
"sabberworm/php-css-parser": "dev-master#bfdd976"
Expand Down
84 changes: 51 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 10 additions & 23 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ public static function filter_admin_bar_script_loader_tag( $tag, $handle ) {
*/
public static function ensure_required_markup( Document $dom, $script_handles = [] ) {
// Gather all links.
$links = [
$links = [
Attribute::REL_PRECONNECT => [
// Include preconnect link for AMP CDN for browsers that don't support preload.
AMP_DOM_Utils::create_node(
Expand All @@ -1401,7 +1401,9 @@ public static function ensure_required_markup( Document $dom, $script_handles =
),
],
];

$link_elements = $dom->head->getElementsByTagName( Tag::LINK );

/**
* Link element.
*
Expand Down Expand Up @@ -1500,32 +1502,16 @@ public static function ensure_required_markup( Document $dom, $script_handles =
}
}

/* 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}
/*
* "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."
* @TODO: Move into RewriteAmpUrls transformer, as that will support self-hosting as well.
*/
$prioritized_preloads = [];
if ( ! isset( $links[ Attribute::REL_PRELOAD ] ) ) {
$links[ Attribute::REL_PRELOAD ] = [];
}

$prioritized_preloads[] = AMP_DOM_Utils::create_node(
$dom,
Tag::LINK,
[
Attribute::REL => Attribute::REL_PRELOAD,
Attribute::AS_ => RequestDestination::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_intersect( Amp::RENDER_DELAYING_EXTENSIONS, $amp_script_handles ) as $script_handle ) {
if ( ! in_array( $script_handle, Amp::RENDER_DELAYING_EXTENSIONS, true ) ) {
Expand Down Expand Up @@ -1991,6 +1977,8 @@ public static function prepare_response( $response, $args = [] ) {
}
}

self::ensure_required_markup( $dom, array_keys( $amp_scripts ) );

$enable_optimizer = array_key_exists( ConfigurationArgument::ENABLE_OPTIMIZER, $args )
? $args[ ConfigurationArgument::ENABLE_OPTIMIZER ]
: true;
Expand Down Expand Up @@ -2049,8 +2037,6 @@ static function( Optimizer\Error $error ) {
do_action( 'amp_server_timing_stop', 'amp_optimizer' );
}

self::ensure_required_markup( $dom, array_keys( $amp_scripts ) );

$can_serve = AMP_Validation_Manager::finalize_validation( $dom );

// Redirect to the non-AMP version if not on an AMP-first site.
Expand Down Expand Up @@ -2130,6 +2116,7 @@ private static function get_optimizer_configuration( $args ) {
[
Optimizer\Transformer\AmpRuntimeCss::class,
Optimizer\Transformer\PreloadHeroImage::class,
Optimizer\Transformer\RewriteAmpUrls::class,
Optimizer\Transformer\ServerSideRendering::class,
Optimizer\Transformer\TransformedIdentifier::class,
]
Expand Down
1 change: 1 addition & 0 deletions src/ConfigurationArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
interface ConfigurationArgument {

const ENABLE_ESM = 'enable_esm';
const ENABLE_OPTIMIZER = 'enable_optimizer';
const ENABLE_SSR = 'enable_ssr';
}
Loading