Skip to content

Commit

Permalink
Merge branch 'develop' into fix/caption-with-period
Browse files Browse the repository at this point in the history
  • Loading branch information
pierlon committed Apr 9, 2020
2 parents 5b144af + 07350e1 commit da68940
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
26 changes: 21 additions & 5 deletions php/class-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function init() {
*/
public function wp_get_attachment_url( $url, $id ) {
$unsplash_url = $this->get_unsplash_url( $id );
if ( ! $unsplash_url ) {
$cropped = $this->is_cropped_image( $id );
if ( ! $unsplash_url || $cropped ) {
return $url;
}

Expand Down Expand Up @@ -185,12 +186,14 @@ public function change_full_url( array $sizes, $field, $url ) {
*/
public function image_downsize( $should_resize, $id, $size ) {
$unsplash_url = $this->get_unsplash_url( $id );
if ( ! $unsplash_url ) {
$cropped = $this->is_cropped_image( $id );
if ( ! $unsplash_url || $cropped ) {
return $should_resize;
}
$image_meta = wp_get_attachment_metadata( $id );
$image_size = ( isset( $image_meta['sizes'] ) ) ? $image_meta['sizes'] : [];
$sizes = $this->plugin->image_sizes();

if ( is_array( $size ) ) {
// If array is passed, just use height and width.
list( $width, $height ) = $size;
Expand Down Expand Up @@ -355,7 +358,8 @@ public function hotlink_images_in_content( $content ) {
*/
public function wp_calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
$unsplash_url = $this->get_unsplash_url( $attachment_id );
if ( ! $unsplash_url ) {
$cropped = $this->is_cropped_image( $attachment_id );
if ( ! $unsplash_url || $cropped ) {
return $sources;
}

Expand Down Expand Up @@ -477,6 +481,7 @@ public function get_image_size_from_url( $url ) {
return [ $width, $height ];
}


/**
* Helper to get original url from post meta.
*
Expand All @@ -488,6 +493,17 @@ protected function get_unsplash_url( $id ) {
return get_post_meta( $id, 'original_url', true );
}

/**
* Is cropped image.
*
* @param int $id Attachment ID.
*
* @return boolean Is cropped image.
*/
public function is_cropped_image( $id ) {
return (bool) get_post_meta( $id, '_wp_attachment_backup_sizes', true );
}


/**
* Warm the object cache with post and meta information for all found
Expand Down Expand Up @@ -559,11 +575,11 @@ public function wp_get_attachment_caption( $caption, $attachment_id ) {
if ( ! $unsplash_url ) {
return $caption;
}


return wp_strip_all_tags( $caption );
}

/**
* Filters the content of a single block.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/php/class-test-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ public function test_wp_get_attachment_url() {
$this->assertEquals( wp_get_attachment_url( self::$attachment_id ), 'https://images.unsplash.com/test.jpg' );
}

/**
* Test wp_get_attachment_url.
*
* @covers ::wp_get_attachment_url()
* @covers ::is_cropped_image()
*/
public function test_wp_get_attachment_url_croppped() {
$first = wp_get_attachment_url( self::$attachment_id );
$this->assertEquals( $first, 'https://images.unsplash.com/test.jpg' );
update_post_meta( self::$attachment_id, '_wp_attachment_backup_sizes', [ 'foo' => 'bar' ] );
$second = wp_get_attachment_url( self::$attachment_id );
$this->assertEquals( $second, 'http://example.org/wp-content/uploads//tmp/canola.jpg' );
$this->assertNotEquals( $first, $second );
delete_post_meta( self::$attachment_id, '_wp_attachment_backup_sizes' );
$third = wp_get_attachment_url( self::$attachment_id );
$this->assertEquals( $third, 'https://images.unsplash.com/test.jpg' );
}

/**
* Test image_downsize.
*
Expand All @@ -104,6 +122,26 @@ public function test_wp_get_attachment_image_src() {
$this->assertEquals( $image[0], 'https://images.unsplash.com/test.jpg?w=300&h=300' );
}

/**
* Test image_downsize.
*
* @covers ::image_downsize()
*/
public function test_wp_get_attachment_image_src_cropped() {
$second_id = $this->factory->attachment->create_object(
'/tmp/apple.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption 2',
]
);
update_post_meta( self::$attachment_id, '_wp_attachment_backup_sizes', [ 'foo' => 'bar' ] );
$image = image_downsize( $second_id );
$this->assertInternalType( 'array', $image );
$this->assertEquals( $image[0], 'http://example.org/wp-content/uploads//tmp/apple.jpg' );
}

/**
* Data for test_get_attachments_from_content
*
Expand Down Expand Up @@ -566,4 +604,23 @@ public function test_no_wp_get_attachment_caption() {
$result = $this->hotlink->wp_get_attachment_caption( $caption, $second_id );
$this->assertEquals( $caption, $result );
}

/**
* Test is_cropped_image.
*
* @covers ::is_cropped_image()
*/
public function test_is_cropped_image() {
$second_id = $this->factory->attachment->create_object(
'/tmp/plum.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption 2',
]
);
$this->assertFalse( $this->hotlink->is_cropped_image( $second_id ) );
update_post_meta( $second_id, '_wp_attachment_backup_sizes', [ 'foo' => 'bar' ] );
$this->assertTrue( $this->hotlink->is_cropped_image( $second_id ) );
}
}

0 comments on commit da68940

Please sign in to comment.