Skip to content

Commit

Permalink
Merge pull request ampproject#756 from Automattic/fix/normalized-dime…
Browse files Browse the repository at this point in the history
…nsion-urls

Dimension Extractor should return original URLs
  • Loading branch information
mjangda authored Aug 17, 2017
2 parents ce72b63 + e0afd80 commit 325df70
Showing 2 changed files with 55 additions and 8 deletions.
30 changes: 22 additions & 8 deletions includes/utils/class-amp-image-dimension-extractor.php
Original file line number Diff line number Diff line change
@@ -10,18 +10,32 @@ static public function extract( $urls ) {
self::register_callbacks();
}

$valid_urls = array();
foreach ( $urls as $url ) {
$url = self::normalize_url( $url );
if ( false !== $url ) {
$valid_urls[] = $url;
$return_dimensions = array();

// Normalize URLs and also track a map of normalized-to-original as we'll need it to reformat things when returning the data.
$url_map = array();
$normalized_urls = array();
foreach ( $urls as $original_url ) {
$normalized_url = self::normalize_url( $original_url );
if ( false !== $normalized_url ) {
$url_map[ $normalized_url ] = $original_url;
$normalized_urls[] = $normalized_url;
} else {
// This is not a URL we can extract dimensions from, so default to false.
$return_dimensions[ $original_url ] = false;
}
}

$dimensions = array_fill_keys( $valid_urls, false );
$dimensions = apply_filters( 'amp_extract_image_dimensions_batch', $dimensions );
$extracted_dimensions = array_fill_keys( $normalized_urls, false );
$extracted_dimensions = apply_filters( 'amp_extract_image_dimensions_batch', $extracted_dimensions );

return $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;
}

return $return_dimensions;
}

public static function normalize_url( $url ) {
33 changes: 33 additions & 0 deletions tests/test-amp-image-dimension-extractor.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,39 @@
define( 'IMG_350', 'http://i0.wp.com/amptest.files.wordpress.com/2017/03/350x150.png' );
define( 'IMG_1024', 'http://i0.wp.com/amptest.files.wordpress.com/2017/03/1024x768.png' );

class AMP_Image_Dimension_Extractor__Extract__Test extends
WP_UnitTestCase {
public function setUp() {
parent::setUp();

// We don't want to actually download the images; just testing the extract method
add_action( 'amp_extract_image_dimensions_batch_callbacks_registered', array( $this, 'disable_downloads' ) );
}

public function disable_downloads() {
remove_all_filters( 'amp_extract_image_dimensions_batch' );
}

public function test__should_return_original_urls() {
$source_urls = array(
'https://example.com',
'//example.com/no-protocol',
'/absolute-url/no-host',
'data:image/gif;base64,R0lGODl...', // can't normalize
);
$expected = array(
'https://example.com' => false,
'//example.com/no-protocol' => false,
'/absolute-url/no-host' => false,
'data:image/gif;base64,R0lGODl...' => false,
);

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

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

class AMP_Image_Dimension_Extractor__Normalize_URL__Test extends WP_UnitTestCase {
function get_data() {
$site_url = site_url();

0 comments on commit 325df70

Please sign in to comment.