From eeebd830e8b9392b092efb8e49358e1519f23176 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 16 Aug 2019 17:26:56 -0700 Subject: [PATCH 01/11] Introduce amp_story_head action --- includes/class-amp-story-post-type.php | 31 +++++++++++++++++++++++++ includes/templates/single-amp_story.php | 17 +++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/includes/class-amp-story-post-type.php b/includes/class-amp-story-post-type.php index d0c26702536..e872ae51f7e 100644 --- a/includes/class-amp-story-post-type.php +++ b/includes/class-amp-story-post-type.php @@ -197,6 +197,37 @@ public static function register() { add_action( 'wp_enqueue_scripts', [ __CLASS__, 'add_custom_stories_styles' ] ); + add_action( 'amp_story_head', 'rel_canonical' ); + add_action( 'amp_story_head', 'amp_add_generator_metadata' ); + add_action( 'amp_story_head', 'wp_enqueue_scripts' ); + add_action( 'amp_story_head', 'rel_canonical' ); + + // @todo Create methods/functions for some of these. + add_action( + 'amp_story_head', + static function() { + // @todo Eliminate in favor of adding via post-processor. + echo amp_get_boilerplate_code(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + }, + PHP_INT_MAX + ); + add_action( + 'amp_story_head', + function() { + // See _wp_render_title_tag(). + echo '' . esc_html( wp_get_document_title() ) . '' . "\n"; + } + ); + add_action( + 'amp_story_head', + function() { + // @todo Temporary until sanitizer automatically supplies runtime. + wp_scripts()->do_items( [ 'amp-runtime' ] ); // @todo Duplicate with AMP_Theme_Support::enqueue_assets(). + + wp_styles()->do_items(); + } + ); + // Remove unnecessary settings. add_filter( 'block_editor_settings', [ __CLASS__, 'filter_block_editor_settings' ], 10, 2 ); diff --git a/includes/templates/single-amp_story.php b/includes/templates/single-amp_story.php index 312c487913b..6b3353daf0f 100644 --- a/includes/templates/single-amp_story.php +++ b/includes/templates/single-amp_story.php @@ -7,26 +7,25 @@ the_post(); -$metadata = amp_get_schemaorg_metadata(); ?> > - <?php echo esc_html( wp_get_document_title() ); ?> + do_items( [ 'amp-runtime' ] ); // @todo Duplicate with AMP_Theme_Support::enqueue_assets(). - wp_styles()->do_items(); + /** + * Prints scripts or data in the head tag on the front end. + * + * @since 1.3 + */ + do_action( 'amp_story_head' ); ?> - - - - Date: Wed, 21 Aug 2019 17:11:34 -0700 Subject: [PATCH 02/11] Introduce amp_get_boilerplate_stylesheets() function for returning individual boilerplate stylesheets --- includes/amp-helper-functions.php | 19 ++++++++++++-- tests/php/test-amp-helper-functions.php | 33 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 5512f4c4904..8f227f7d7db 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -361,8 +361,23 @@ function amp_get_asset_url( $file ) { * @return string Boilerplate code. */ function amp_get_boilerplate_code() { - return '' - . ''; + $stylesheets = amp_get_boilerplate_stylesheets(); + return sprintf( '', $stylesheets[0], $stylesheets[1] ); +} + +/** + * Get AMP boilerplate stylesheets. + * + * @since 1.3 + * @link https://www.ampproject.org/docs/reference/spec#boilerplate + * + * @return string[] Stylesheets, where first is contained in style[amp-boilerplate] and the second in noscript>style[amp-boilerplate]. + */ +function amp_get_boilerplate_stylesheets() { + return [ + 'body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}', + 'body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}', + ]; } /** diff --git a/tests/php/test-amp-helper-functions.php b/tests/php/test-amp-helper-functions.php index 52cdaf6960f..89e4e236625 100644 --- a/tests/php/test-amp-helper-functions.php +++ b/tests/php/test-amp-helper-functions.php @@ -480,6 +480,39 @@ public function capture_filter_call( $value ) { return $value; } + /** + * Test amp_get_asset_url. + * + * @covers ::amp_get_asset_url() + */ + public function test_amp_get_asset_url() { + $this->assertStringEndsWith( '/assets/foo.jpg', amp_get_asset_url( 'foo.jpg' ) ); + } + + /** + * Test amp_get_boilerplate_code. + * + * @covers ::amp_get_boilerplate_code() + */ + public function test_amp_get_boilerplate_code() { + $boilerplate_code = amp_get_boilerplate_code(); + $this->assertStringStartsWith( ''; - }, - 0 - ); - add_action( - 'wp_head', - static function() { - echo amp_get_boilerplate_code(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - }, - PHP_INT_MAX - ); - 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' ], 0 ); // Enqueue before theme's styles. @@ -1434,7 +1406,7 @@ static function( $body_classes ) { * * @since 0.7 * @link https://www.ampproject.org/docs/reference/spec#required-markup - * @link https://docs.google.com/document/d/169XUxtSSEJb16NfkrCr9y5lqhUR7vxXEAsNxBzg07fM/edit#heading=h.2ha259c3ffos + * @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 DOMDocument $dom Document. @@ -1447,6 +1419,8 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles * @var DOMElement $meta * @var DOMElement $script * @var DOMElement $link + * @var DOMElement $style + * @var DOMElement $noscript */ $xpath = new DOMXPath( $dom ); @@ -1496,9 +1470,9 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles * there are a few basic optimizations that you can apply. The key is to structure the section * in a way so that all render-blocking scripts and custom fonts load as fast as possible." * - * "The first tag should be the meta charset tag, followed by any remaining meta tags." + * "1. The first tag should be the meta charset tag, followed by any remaining meta tags." * - * {@link https://docs.google.com/document/d/169XUxtSSEJb16NfkrCr9y5lqhUR7vxXEAsNxBzg07fM/edit#heading=h.2ha259c3ffos Optimize the AMP Runtime loading} + * {@link https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp/ Optimize the AMP Runtime loading} */ $meta_charset = null; $meta_viewport = null; @@ -1606,10 +1580,10 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles /* phpcs:ignore Squiz.PHP.CommentedOutCode.Found * - * "Next, preload the AMP runtime v0.js ', '', - '', - '', + '##s', - '##s', '', - '