From 6baffaa188a6a9c684d41537cfeec734a477148a Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 6 Aug 2018 16:28:32 -0500 Subject: [PATCH 1/2] Filter the attachment image attributes for the Twenty Seventeen attachment page. Per issue #1237, the Twenty Seventeen theme is adding `$attr['sizes'] = '100vw';` which stretches images on the attachment page when in AMP mode. This commit filters 'wp_get_attachment_image_attributes' to change the layout to responsive and sizes to the image sizes per `wp_get_attachment_image_sizes()`. AMP uses these attributes to render the . --- .../class-amp-core-theme-sanitizer.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/includes/sanitizers/class-amp-core-theme-sanitizer.php b/includes/sanitizers/class-amp-core-theme-sanitizer.php index 55a9c3b63f1..20b1ee563cd 100644 --- a/includes/sanitizers/class-amp-core-theme-sanitizer.php +++ b/includes/sanitizers/class-amp-core-theme-sanitizer.php @@ -78,6 +78,7 @@ class AMP_Core_Theme_Sanitizer extends AMP_Base_Sanitizer { '//header[@id = "masthead"]//a[ contains( @class, "menu-scroll-down" ) ]', ), 'set_twentyseventeen_quotes_icon' => array(), + 'add_twentyseventeen_attachment_image_attributes' => array(), ), // Twenty Sixteen. @@ -344,6 +345,30 @@ public static function set_twentyseventeen_quotes_icon() { } ); } + /** + * Add filter to adjust the attachment image attributes to ensure attachment pages have a consistent rendering. + * + * This is only used in Twenty Seventeen. + * + * @since 1.0 + * @link https://github.com/WordPress/wordpress-develop/blob/ddc8f803c6e99118998191fd2ea24124feb53659/src/wp-content/themes/twentyseventeen/functions.php#L545:L554 + */ + public static function add_twentyseventeen_attachment_image_attributes() { + add_filter( 'wp_get_attachment_image_attributes', function ( $attr, $attachment, $size ) { + if ( ! is_attachment() ) { + return $attr; + } + + $sizes = wp_get_attachment_image_sizes( $attachment->ID, $size ); + if ( false !== $sizes ) { + $attr['layout'] = 'responsive'; + $attr['sizes'] = $sizes; + } + + return $attr; + }, 11, 3 ); + } + /** * Fix up core themes to do things in the AMP way. * From 74dc97a0554eaed405c5fc26e7302248c628ff25 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 7 Aug 2018 10:23:14 -0500 Subject: [PATCH 2/2] Allow default layout attribute for attachment pages. --- includes/sanitizers/class-amp-core-theme-sanitizer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/sanitizers/class-amp-core-theme-sanitizer.php b/includes/sanitizers/class-amp-core-theme-sanitizer.php index 20b1ee563cd..c31566afe0d 100644 --- a/includes/sanitizers/class-amp-core-theme-sanitizer.php +++ b/includes/sanitizers/class-amp-core-theme-sanitizer.php @@ -361,8 +361,7 @@ public static function add_twentyseventeen_attachment_image_attributes() { $sizes = wp_get_attachment_image_sizes( $attachment->ID, $size ); if ( false !== $sizes ) { - $attr['layout'] = 'responsive'; - $attr['sizes'] = $sizes; + $attr['sizes'] = $sizes; } return $attr;