Skip to content

Commit

Permalink
feat: Remove check for attachment post type that can’t be overridden (#…
Browse files Browse the repository at this point in the history
…85)

* don't hardcode attachment post-type

This code-path can't be override (contrary to checks done by the Timmy\Image constructor.
Be it for usage with custom-post-type or subclassing attachment, or any other plugin edge-case, this check would be better left to a place it can be skipped/made optional.

* fix: Fix tests

* fix: Add check for WP_Post

---------

Co-authored-by: Raphaël Droz <[email protected]>
  • Loading branch information
gchtr and drzraf authored Aug 30, 2024
1 parent efcf332 commit 13574be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/Timmy.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ public static function get_image( $attachment, $size ) {
$attachment = (int) $attachment['ID'];
}

if ( 'attachment' !== get_post_type( $attachment ) ) {
// Check if we work with a WordPress post. This doesn’t necessarily have
// to be an attachment, that’s why we don’t check for the 'attachment'
// post type.
$wp_post = \get_post( $attachment );

if ( ! $wp_post ) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/test-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_get_timber_image_src_without_metadata() {
}

public function test_get_timber_image_src_non_image() {
$result = get_timber_image_src( 'gaga', 'large' );
$result = get_timber_image_src( 0, 'large' );

$this->assertEquals( false, $result );
}
Expand Down Expand Up @@ -64,7 +64,7 @@ public function test_get_timber_image_without_metadata() {
}

public function test_get_timber_image_non_image() {
$result = get_timber_image( 'gaga', 'large' );
$result = get_timber_image( 0, 'large' );

$this->assertEquals( false, $result );
}
Expand Down Expand Up @@ -373,7 +373,7 @@ public function test_get_timber_image_srcset() {
}

public function test_get_timber_image_srcset_non_image() {
$result = get_timber_image_srcset( 'gaga', 'large' );
$result = get_timber_image_srcset( 0, 'large' );

$this->assertEquals( false, $result );
}
Expand Down

0 comments on commit 13574be

Please sign in to comment.