From e40d370cd03e06b0f86835a92822c21b979930d1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 9 Mar 2022 09:25:53 -0800 Subject: [PATCH] Copy referrerpolicy attribute from img to amp-pixel --- includes/sanitizers/class-amp-img-sanitizer.php | 14 ++++++++++---- tests/php/test-amp-img-sanitizer.php | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/includes/sanitizers/class-amp-img-sanitizer.php b/includes/sanitizers/class-amp-img-sanitizer.php index bbf0ab12fb3..9188ac2f05e 100644 --- a/includes/sanitizers/class-amp-img-sanitizer.php +++ b/includes/sanitizers/class-amp-img-sanitizer.php @@ -185,13 +185,19 @@ public function sanitize() { // Replace img with amp-pixel when dealing with tracking pixels. if ( self::is_tracking_pixel_url( $node->getAttribute( Attribute::SRC ) ) ) { + $attributes = [ + Attribute::SRC => $node->getAttribute( Attribute::SRC ), + Attribute::LAYOUT => Layout::NODISPLAY, + ]; + foreach ( [ Attribute::REFERRERPOLICY ] as $allowed_attribute ) { + if ( $node->hasAttribute( $allowed_attribute ) ) { + $attributes[ $allowed_attribute ] = $node->getAttribute( $allowed_attribute ); + } + } $amp_pixel_node = AMP_DOM_Utils::create_node( $this->dom, Extension::PIXEL, - [ - Attribute::SRC => $node->getAttribute( Attribute::SRC ), - Attribute::LAYOUT => Layout::NODISPLAY, - ] + $attributes ); $node->parentNode->replaceChild( $amp_pixel_node, $node ); continue; diff --git a/tests/php/test-amp-img-sanitizer.php b/tests/php/test-amp-img-sanitizer.php index f10d5a4f3e9..9742e32fbfb 100644 --- a/tests/php/test-amp-img-sanitizer.php +++ b/tests/php/test-amp-img-sanitizer.php @@ -580,6 +580,11 @@ public function get_data() { 'fbpx', '', ], + + 'facebook_pixel_img_to_amp_pixel_with_referrer' => [ + 'fbpx', + '', + ], ]; }