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 cropping in WordPress 5.5. #202

Merged
merged 5 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 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,29 @@ 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 );
if ( ! $unsplash_url || $cropped ) {
return $match;
}

if ( strpos( $image_location, 'images.unsplash.com' ) ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

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