Skip to content

Commit

Permalink
Add original unsplash meta for edited attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
ravichdev committed Jul 8, 2020
1 parent 042066a commit 70520bf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
26 changes: 25 additions & 1 deletion php/class-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ public function get_image_size_from_url( $url ) {
return [ $width, $height ];
}


/**
* Helper to get original url from post meta.
*
Expand Down Expand Up @@ -709,4 +708,29 @@ public function render_block( $block_content, $block ) {

return $block_content;
}

/**
* Add Unsplash metadata for edited attachment
*
* @filter wp_edited_attachment_metadata, 10, 3
*
* @param array $data Array of updated attachment meta data.
* @param int $new_attachment_id Attachment post ID.
* @param int $attachment_id Original Attachment post ID.
*
* @return array
*/
public function add_edited_attachment_metadata( $data, $new_attachment_id, $attachment_id ) {
// Verify it is an Unsplash ID.
$unsplash_url = $this->get_unsplash_url( $attachment_id );
$cropped = $this->is_cropped_image( $attachment_id );

if ( $unsplash_url && ! $cropped ) {
add_post_meta( $new_attachment_id, 'original_url', $unsplash_url, true );
add_post_meta( $new_attachment_id, 'original_id', get_post_meta( $attachment_id, 'original_id', true ), true );
add_post_meta( $new_attachment_id, 'original_link', get_post_meta( $attachment_id, 'original_link', true ), true );
}

return $data;
}
}
51 changes: 51 additions & 0 deletions tests/phpunit/php/class-test-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static function wpSetUpBeforeClass( $factory ) {

update_post_meta( self::$attachment_id, 'original_url', 'https://images.unsplash.com/test.jpg' );
update_post_meta( self::$attachment_id, 'original_link', 'https://www.unsplash.com/foo' );
update_post_meta( self::$attachment_id, 'original_id', 'UNSPLASH_ID' );
self::$image_tag = get_image_tag( self::$attachment_id, 'alt', 'title', 'left' );
}

Expand Down Expand Up @@ -811,4 +812,54 @@ public function test_is_cropped_image() {
update_post_meta( $second_id, '_wp_attachment_backup_sizes', [ 'foo' => 'bar' ] );
$this->assertTrue( $this->hotlink->is_cropped_image( $second_id ) );
}

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

$this->assertEmpty( get_post_meta( $second_id, 'original_url', true ) );
$this->assertEmpty( get_post_meta( $second_id, 'original_id', true ) );
$this->assertEmpty( get_post_meta( $second_id, 'original_link', true ) );
}

/**
* Test add_edited_attachment_metadata.
*
* @covers ::add_edited_attachment_metadata()
*/
public function test_add_edited_attachment_metadata() {
$second_id = $this->factory->attachment->create_object(
'/tmp/melon.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption 2',
]
);
$this->hotlink->add_edited_attachment_metadata( [], $second_id, self::$attachment_id );

$this->assertEquals( 'https://images.unsplash.com/test.jpg', get_post_meta( $second_id, 'original_url', true ) );
$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 ) );
}
}

0 comments on commit 70520bf

Please sign in to comment.