Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extractor that forgot duplicate images #1314

Merged
merged 5 commits into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions includes/utils/class-amp-image-dimension-extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ public static function extract( $urls ) {
foreach ( $urls as $original_url ) {
$normalized_url = self::normalize_url( $original_url );
if ( false !== $normalized_url ) {
$url_map[ $normalized_url ] = $original_url;
$url_map[ $original_url ] = $normalized_url;
$normalized_urls[] = $normalized_url;
} else {
// This is not a URL we can extract dimensions from, so default to false.
$url_map[ $original_url ] = $original_url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line being removed? It was recently added in #793.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old version the line is unnecessary. The code after this line never uses url_map to get an url that cannot be normalized. Therefore, this line is unnecessary and can be removed.

In my version, it is also unnecessary and causes an error.

$return_dimensions[ $original_url ] = false;
}
}
Expand All @@ -45,9 +44,8 @@ public static function extract( $urls ) {
$extracted_dimensions = apply_filters( 'amp_extract_image_dimensions_batch', $extracted_dimensions );

// We need to return a map with the original (un-normalized URL) as we that to match nodes that need dimensions.
foreach ( $extracted_dimensions as $normalized_url => $dimension ) {
$original_url = $url_map[ $normalized_url ];
$return_dimensions[ $original_url ] = $dimension;
foreach ( $url_map as $original_url => $normalized_url ) {
$return_dimensions[ $original_url ] = $extracted_dimensions[ $normalized_url ];
}

// Back-compat: just return the dimensions, not the full mapped array.
Expand Down
18 changes: 18 additions & 0 deletions tests/test-amp-image-dimension-extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ function() {
$this->assertEquals( $expected, $actual );
}

/**
* Test should return both urls
*/
public function test__should_return_both_urls() {
$source_urls = array(
site_url( '/wp-content/uploads/2018/06/IMG_0183-300x300.jpg' ),
'/wp-content/uploads/2018/06/IMG_0183-300x300.jpg',
);
$expected = array(
site_url( '/wp-content/uploads/2018/06/IMG_0183-300x300.jpg' ) => false,
'/wp-content/uploads/2018/06/IMG_0183-300x300.jpg' => false,
);

$actual = AMP_Image_Dimension_Extractor::extract( $source_urls );

$this->assertEquals( $expected, $actual );
}

/**
* Test where processed URLs should match originals.
*/
Expand Down