Skip to content

Commit

Permalink
Merge 55d9034 into 4eb4cab
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Aug 3, 2020
2 parents 4eb4cab + 55d9034 commit abdb092
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions php/class-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function init() {
add_filter( 'wp_get_attachment_caption', [ $this, 'wp_get_attachment_caption' ], 10, 2 );
add_filter( 'render_block', [ $this, 'render_block' ], 10, 2 );
add_filter( 'wp_edited_image_metadata', [ $this, 'add_edited_attachment_metadata' ], 10, 3 );
add_filter( 'wp_image_file_matches_image_meta', [ $this, 'make_unsplash_images_cropable' ], 10, 4 );
}

/**
Expand Down Expand Up @@ -717,4 +718,26 @@ public function add_edited_attachment_metadata( $data, $new_attachment_id, $atta

return $data;
}

/**
* Filter whether an image path or URI matches image meta.
*
* @param bool $match Whether the image relative path from the image meta
* matches the end of the URI or path to the image file.
* @param string $image_location Full path or URI to the tested image file.
* @param array $image_meta (Unused) The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id The image attachment ID or 0 if not supplied.
*
* @return bool Can an image cropable.
*/
public function make_unsplash_images_cropable( $match, $image_location, $image_meta, $attachment_id ) {
$unsplash_url = $this->get_unsplash_url( $attachment_id );
$cropped = $this->is_cropped_image( $attachment_id );
$is_unsplash = strpos( $image_location, 'images.unsplash.com' );
if ( ! $unsplash_url || $cropped || ( false === $is_unsplash ) ) {
return $match;
}

return true;
}
}
32 changes: 32 additions & 0 deletions tests/phpunit/php/class-test-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,36 @@ public function test_add_edited_attachment_metadata() {
$this->assertEquals( 'UNSPLASH_ID', get_post_meta( $second_id, 'original_id', true ) );
$this->assertEquals( 'https://www.unsplash.com/foo', get_post_meta( $second_id, 'original_link', true ) );
}

/**
* Test make_unsplash_images_cropable.
*
* @covers ::make_unsplash_images_cropable()
*/
public function test_make_unsplash_images_cropable() {
$image_location = 'https://images.unsplash.com/photo-1593642703055-4b72c180d9b5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjE0NzAzNH0&fm=jpg&q=85&fit=crop&w=1024&h=683';
$result = $this->hotlink->make_unsplash_images_cropable( false, $image_location, [], self::$attachment_id );
$this->assertTrue( $result );
}

/**
* Test make_unsplash_images_cropable.
*
* @covers ::make_unsplash_images_cropable()
*/
public function test_make_unsplash_images_cropable_invalid() {
$first_id = $this->factory->attachment->create_object(
'/tmp/banana.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption 1',
]
);

$image_location = wp_get_attachment_url( $first_id );

$result = $this->hotlink->make_unsplash_images_cropable( false, $image_location, [], $first_id );
$this->assertFalse( $result );
}
}

0 comments on commit abdb092

Please sign in to comment.