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 preview image. #163

Merged
merged 2 commits into from
Jun 15, 2020
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
33 changes: 33 additions & 0 deletions php/class-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,39 @@ public function image_downsize( $should_resize, $id, $size ) {
return [ $unsplash_url, $width, $height, false ];
}

/**
* Work around for image preview in Attachment screen.
*
* @filter wp_get_attachment_image_src, 10, 5
*
* @param array|false $image rray of image data, or boolean false if no image is available.
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Requested size of image. Image size name, or array of width
* and height values (in that order).
* @param bool $icon Whether the image should be treated as an icon.
*/
public function wp_get_attachment_image_src( $image, $attachment_id, $size, $icon ) {
if ( is_array( $size ) && array( 900, 450 ) === $size && $icon ) {
$unsplash_url = $this->get_unsplash_url( $attachment_id );
$cropped = $this->is_cropped_image( $attachment_id );
if ( ! $unsplash_url || $cropped ) {
return $image;
}
$request_width = 900;
$image_meta = wp_get_attachment_metadata( $attachment_id );
$height = absint( $image_meta['height'] );
$width = absint( $image_meta['width'] );
$request_height = $this->plugin->get_image_height( $width, $height, $request_width );
$image = [
$this->plugin->get_original_url_with_size( $unsplash_url, $request_width, $request_height, [ 'fit' => false ] ),
$request_width,
$request_height,
];
}

return $image;
}

/**
* Retrieve all image tags from content.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/php/class-test-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ public function test_wp_get_attachment_image_src_cropped() {
$this->assertEquals( $image[0], 'http://example.org/wp-content/uploads//tmp/apple.jpg' );
}

/**
* Test wp_get_attachment_image_src.
*
* @covers ::wp_get_attachment_image_src()
*/
public function test_wp_get_attachment_image_src_900() {
$attachment_id = $this->factory->attachment->create_object(
DIR_TESTDATA . '/images/waffles.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
]
);

wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
update_post_meta( $attachment_id, 'original_url', 'https://images.unsplash.com/waffles.jpg' );
$image = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
$this->assertNotContains( 'fit', $image[0] );
$this->assertEquals( 'https://images.unsplash.com/waffles.jpg?fm=jpg&q=85&w=900&h=600', $image[0] );
$this->assertEquals( 900, $image[1] );
$this->assertEquals( 600, $image[2] );
}

/**
* Data for test_get_attachments_from_content
*
Expand Down