diff --git a/Image_Tag.php b/Image_Tag.php index 705270e..78b833d 100644 --- a/Image_Tag.php +++ b/Image_Tag.php @@ -53,7 +53,8 @@ class Image_Tag implements ArrayAccess { * @param array $settings * @return Image_Tag */ - static function create( $source, array $attributes = array(), array $settings = array() ) { + static function create( $source, $attributes = array(), array $settings = array() ) { + $attributes = ( array ) $attributes; # If integer, create WordPress attachment image. if ( is_int( $source ) ) @@ -580,8 +581,9 @@ function http( bool $force = false ) { * @uses Image_Tag::create() * @return Image_Tag_Picsum */ - function picsum( array $attributes = array(), array $settings = array() ) { - $attributes = wp_parse_args( $attributes, $this->attributes ); + function picsum( $attributes = array(), array $settings = array() ) { + $attributes = wp_parse_args( ( array ) $attributes, $this->attributes ); + $settings = wp_parse_args( $settings, $this->settings ); $settings = wp_parse_args( $settings, array( 'width' => $this->get_width(), 'height' => $this->get_height(), @@ -600,8 +602,9 @@ function picsum( array $attributes = array(), array $settings = array() ) { * @uses Image_Tag::create() * @return Image_Tag_Placeholder */ - function placeholder( array $attributes = array(), array $settings = array() ) { - $attributes = wp_parse_args( $attributes, $this->attributes ); + function placeholder( $attributes = array(), array $settings = array() ) { + $attributes = wp_parse_args( ( array ) $attributes, $this->attributes ); + $settings = wp_parse_args( $settings, $this->settings ); $settings = wp_parse_args( $settings, array( 'width' => $this->get_width(), 'height' => $this->get_height(), @@ -618,14 +621,20 @@ function placeholder( array $attributes = array(), array $settings = array() ) { * @uses Image_Tag::create() * @return Image_Tag_JoeSchmoe */ - function joeschmoe( array $attributes = array(), array $settings = array() ) { + function joeschmoe( $attributes = array(), array $settings = array() ) { + $settings = wp_parse_args( $settings, $this->settings ); + $settings = wp_parse_args( $settings, array( + 'width' => $this->get_width(), + 'height' => $this->get_height(), + ) ); + $_attributes = $this->attributes; unset( $_attributes['srcset'] ); - $attributes = wp_parse_args( $attributes, $_attributes ); + $attributes = wp_parse_args( ( array ) $attributes, $_attributes ); return Image_Tag::create( 'joeschmoe', $attributes, $settings ); } diff --git a/image_tags/Image_Tag_JoeSchmoe.php b/image_tags/Image_Tag_JoeSchmoe.php index 5d3ab99..294851c 100644 --- a/image_tags/Image_Tag_JoeSchmoe.php +++ b/image_tags/Image_Tag_JoeSchmoe.php @@ -88,7 +88,7 @@ function get_ratio() { * @param array $settings * @return $this */ - function joeschmoe( array $attributes = array(), array $settings = array() ) { + function joeschmoe( $attributes = array(), array $settings = array() ) { return $this; } diff --git a/image_tags/Image_Tag_Picsum.php b/image_tags/Image_Tag_Picsum.php index ad0abbb..0209ffa 100644 --- a/image_tags/Image_Tag_Picsum.php +++ b/image_tags/Image_Tag_Picsum.php @@ -93,7 +93,8 @@ function get_src_attribute() { if ( !empty( $this->get_setting( 'grayscale' ) ) ) $src = add_query_arg( 'grayscale', 1, $src ); - return $src; + # If there's a trailing slash, an extra redirect is made. + return untrailingslashit( $src ); } /** @@ -254,7 +255,7 @@ function details() { * @param array $settings * @return $this */ - function picsum( array $attributes = array(), array $settings = array() ) { + function picsum( $attributes = array(), array $settings = array() ) { return $this; } diff --git a/image_tags/Image_Tag_Placeholder.php b/image_tags/Image_Tag_Placeholder.php index b2a44aa..4a92fd3 100644 --- a/image_tags/Image_Tag_Placeholder.php +++ b/image_tags/Image_Tag_Placeholder.php @@ -183,7 +183,7 @@ function get_height_setting() { * @param array $settings * @return $this */ - function placeholder( array $attributes = array(), array $settings = array() ) { + function placeholder( $attributes = array(), array $settings = array() ) { return $this; } diff --git a/image_tags/Image_Tag_WP_Attachment.php b/image_tags/Image_Tag_WP_Attachment.php index 66dcb5e..9122324 100644 --- a/image_tags/Image_Tag_WP_Attachment.php +++ b/image_tags/Image_Tag_WP_Attachment.php @@ -93,7 +93,8 @@ function __toString() { */ function is_valid() { return ( - 'attachment' === get_post_type( $this->attachment_id ) + !empty( $this->attachment_id ) + && 'attachment' === get_post_type( $this->attachment_id ) && wp_attachment_is_image( $this->attachment_id ) ); } @@ -180,24 +181,28 @@ protected function set_image_sizes_setting( $image_sizes ) { * * @uses Image_Tag::get_width() * @uses $this->is_valid() - * @uses $this->get_setting() * @uses $this->get_versions() + * @uses $this->get_setting() * @return int */ function get_width() { if ( !empty( parent::get_width() ) ) return parent::get_width(); - if ( !$this->is_valid() ) { - $image_sizes = $this->get_setting( 'image-sizes' ); + if ( $this->is_valid() ) + return ( int ) $this->get_versions()['__largest']->width; + + $image_sizes = $this->get_setting( 'image-sizes' ); + $sizes = wp_get_registered_image_subsizes(); + + while ( !empty( $image_sizes ) ) { $image_size = array_pop( $image_sizes ); - $sizes = wp_get_registered_image_subsizes(); if ( isset( $sizes[$image_size] ) ) return ( int ) $sizes[$image_size]['width']; } - return ( int ) $this->get_versions()['__largest']->width; + return 0; } /** @@ -205,24 +210,28 @@ function get_width() { * * @uses Image_Tag::get_height() * @uses $this->is_valid() - * @uses $this->get_setting() * @uses $this->get_versions() + * @uses $this->get_setting() * @return int */ function get_height() { if ( !empty( parent::get_height() ) ) return parent::get_height(); - if ( !$this->is_valid() ) { - $image_sizes = $this->get_setting( 'image-sizes' ); + if ( $this->is_valid() ) + return ( int ) $this->get_versions()['__largest']->height; + + $image_sizes = $this->get_setting( 'image-sizes' ); + $sizes = wp_get_registered_image_subsizes(); + + while ( !empty( $image_sizes ) ) { $image_size = array_pop( $image_sizes ); - $sizes = wp_get_registered_image_subsizes(); if ( isset( $sizes[$image_size] ) ) return ( int ) $sizes[$image_size]['height']; } - return ( int ) $this->get_versions()['__largest']->height; + return 0; } /** @@ -253,6 +262,9 @@ function get_versions() { if ( !empty( array_filter( $this->versions ) ) ) return $this->versions; + if ( !$this->is_valid() ) + return array(); + $image_sizes = $this->get_setting( 'image-sizes' ); $largest = null; $smallest = null; @@ -383,7 +395,7 @@ function get_colors( int $count = 3, bool $force = false ) { * @uses Image_Tag_Picsum->add_srcset() * @return Image_Tag_Picsum */ - function picsum( array $attributes = array(), array $settings = array() ) { + function picsum( $attributes = array(), array $settings = array() ) { $picsum = parent::picsum( $attributes, $settings ); if ( @@ -421,7 +433,7 @@ function picsum( array $attributes = array(), array $settings = array() ) { * @uses Image_Tag_Placeholder->add_srcset() * @return Image_Tag_Placeholder */ - function placeholder( array $attributes = array(), array $settings = array() ) { + function placeholder( $attributes = array(), array $settings = array() ) { $placeholder = parent::placeholder( $attributes, $settings ); if ( @@ -466,7 +478,8 @@ static function lqip_transient_key( int $attachment_id ) { * * @todo check if LQIP file already exists */ - function lqip( array $attributes = array(), array $settings = array() ) { + function lqip( $attributes = array(), array $settings = array() ) { + $attributes = ( array ) $attributes; $_attributes = $this->attributes; unset( diff --git a/tests/test-picsum.php b/tests/test-picsum.php index 8b5238f..7237984 100644 --- a/tests/test-picsum.php +++ b/tests/test-picsum.php @@ -11,7 +11,12 @@ function test_base_source() { $img = Image_Tag::create( 'picsum' ); $this->assertEquals( 'https://picsum.photos/', Image_Tag_Picsum::BASE_URL ); - $this->assertEquals( Image_Tag_Picsum::BASE_URL, $img->get_attribute( 'src' ) ); + $this->assertEquals( untrailingslashit( Image_Tag_Picsum::BASE_URL ), $img->get_attribute( 'src' ) ); + } + + function test_src_attribute() { + $img = Image_Tag::create( 'picsum' ); + $this->assertEquals( untrailingslashit( Image_Tag_Picsum::BASE_URL ), $img->get_attribute( 'src' ) ); } function test_blur_setting() { @@ -56,7 +61,7 @@ function test_seed_setting() { $seed = urlencode( sanitize_title_with_dashes( $seed ) ); $this->assertEquals( $seed, $img->get_setting( 'seed' ) ); - $this->assertContains( 'seed/' . $seed . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( 'seed/' . $seed, $img->get_attribute( 'src' ) ); } /** @@ -82,7 +87,7 @@ function test_width() { $this->assertEquals( $width, $img->get_setting( 'width' ) ); $this->assertEquals( $width, $img->get_attribute( 'width' ) ); - $this->assertContains( '/' . $width . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . $width, $img->get_attribute( 'src' ) ); $img = Image_Tag::create( 'picsum', array( 'width' => $width, @@ -90,7 +95,7 @@ function test_width() { $this->assertEquals( $width, $img->get_setting( 'width' ) ); $this->assertEquals( $width, $img->get_attribute( 'width' ) ); - $this->assertContains( '/' . $width . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . $width, $img->get_attribute( 'src' ) ); $img = Image_Tag::create( 'picsum', array( 'width' => $width, @@ -100,7 +105,7 @@ function test_width() { $this->assertEquals( $width, $img->get_attribute( 'width' ) ); $this->assertEquals( $width * 2, $img->get_setting( 'width' ) ); - $this->assertContains( '/' . ( $width * 2 ) . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . ( $width * 2 ), $img->get_attribute( 'src' ) ); $this->assertContains( 'width="' . $width . '"', $img->__toString() ); $img = Image_Tag::create( 'picsum' ); @@ -134,7 +139,7 @@ function test_height() { $this->assertEquals( $width, $img->get_attribute( 'width' ) ); $this->assertEquals( $height, $img->get_setting( 'height' ) ); $this->assertEquals( $height, $img->get_attribute( 'height' ) ); - $this->assertContains( '/' . $width . '/' . $height . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . $width . '/' . $height, $img->get_attribute( 'src' ) ); $img = Image_Tag::create( 'picsum', array( 'width' => $width, @@ -145,7 +150,7 @@ function test_height() { $this->assertEquals( $width, $img->get_attribute( 'width' ) ); $this->assertEquals( $height, $img->get_setting( 'height' ) ); $this->assertEquals( $height, $img->get_attribute( 'height' ) ); - $this->assertContains( '/' . $width . '/' . $height . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . $width . '/' . $height, $img->get_attribute( 'src' ) ); $img = Image_Tag::create( 'picsum', array( 'width' => $width, @@ -159,7 +164,7 @@ function test_height() { $this->assertEquals( 2 * $height, $img->get_setting( 'height' ) ); $this->assertEquals( $width, $img->get_attribute( 'width' ) ); $this->assertEquals( $height, $img->get_attribute( 'height' ) ); - $this->assertContains( '/' . ( 2 * $width ) . '/' . ( 2 * $height ) . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/' . ( 2 * $width ) . '/' . ( 2 * $height ), $img->get_attribute( 'src' ) ); $this->assertContains( 'width="' . $width . '" height="' . $height . '"', $img->__toString() ); } @@ -206,7 +211,7 @@ function test_random_setting() { * @group http */ function test_random_http() { - $img = Image_Tag::create( 'picsum', array(), array( + $img = Image_Tag::create( 'picsum', null, array( 'random' => true, 'width' => 200, ) ); @@ -241,7 +246,7 @@ function test_image_id_setting() { ) ); $this->assertEquals( $image_id, $img->get_setting( 'image_id' ) ); - $this->assertContains( '/id/' . $image_id . '/', $img->get_attribute( 'src' ) ); + $this->assertContains( '/id/' . $image_id, $img->get_attribute( 'src' ) ); } /** diff --git a/tests/test-wp-attachment.php b/tests/test-wp-attachment.php index f05dc06..c196083 100644 --- a/tests/test-wp-attachment.php +++ b/tests/test-wp-attachment.php @@ -172,7 +172,7 @@ function test_ratio() { * @group picsum */ function test_picsum() { - $image_sizes = array( 'medium', 'large' ); + $image_sizes = array( 'medium', 'large', 'full' ); $img = Image_Tag::create( static::$attachment_id, array(), array( 'image-sizes' => $image_sizes ) ); $picsum = $img->picsum(); @@ -183,9 +183,10 @@ function test_picsum() { /** * @group _placeholder * @group picsum + * @group http */ function test_picsum_from_invalid() { - $image_sizes = array( 'medium', 'large' ); + $image_sizes = array( 'medium', 'large', 'full' ); $img = Image_Tag::create( 0, array(), array( 'image-sizes' => $image_sizes ) ); $picsum = $img->picsum(); @@ -201,6 +202,20 @@ function test_picsum_from_invalid() { ) ); $this->assertEquals( 160, $picsum->get_attribute( 'width' ) ); $this->assertEquals( 90, $picsum->get_attribute( 'height' ) ); + + $this->assertEquals( 'image/jpeg', wp_remote_retrieve_header( $picsum->http(), 'content-type' ) ); + + $img = Image_Tag::create( 0, null, array( + 'image-sizes' => array( 'full' ), + 'width' => 1600, + 'height' => 900, + ) ); + $this->assertEquals( 1600, $img->get_setting( 'width' ) ); + $this->assertEquals( 900, $img->get_setting( 'height' ) ); + + $picsum = $img->picsum(); + $this->assertEquals( 1600, $picsum->get_setting( 'width' ) ); + $this->assertEquals( 900, $picsum->get_setting( 'height' ) ); } /** @@ -208,7 +223,7 @@ function test_picsum_from_invalid() { * @group placeholder */ function test_placeholder() { - $image_sizes = array( 'medium', 'large' ); + $image_sizes = array( 'medium', 'large', 'full' ); $img = Image_Tag::create( static::$attachment_id, array(), array( 'image-sizes' => $image_sizes ) ); $placeholder = $img->placeholder(); @@ -219,17 +234,32 @@ function test_placeholder() { /** * @group _placeholder * @group placeholder + * @group http */ function test_placeholder_from_invalid() { - $image_sizes = array( 'medium', 'large' ); + $image_sizes = array( 'medium', 'large', 'full' ); $img = Image_Tag::create( 0, array(), array( 'image-sizes' => $image_sizes ) ); $placeholder = $img->placeholder(); $this->assertInstanceOf( 'Image_Tag_Placeholder', $placeholder ); - $this->assertEquals( 1024, $placeholder->get_attribute( 'width' ) ); + $this->assertEquals( 1024, $placeholder->get_attribute( 'width' ) ); $this->assertEquals( 1024, $placeholder->get_attribute( 'height' ) ); $this->assertNotEmpty( $placeholder->get_attribute( 'src' ) ); $this->assertEmpty( $placeholder['srcset'] ); + + $this->assertEquals( 'image/png', wp_remote_retrieve_header( $placeholder->http(), 'content-type' ) ); + + $img = Image_Tag::create( 0, null, array( + 'image-sizes' => array( 'full' ), + 'width' => 1600, + 'height' => 900, + ) ); + $this->assertEquals( 1600, $img->get_setting( 'width' ) ); + $this->assertEquals( 900, $img->get_setting( 'height' ) ); + + $placeholder = $img->picsum(); + $this->assertEquals( 1600, $placeholder->get_setting( 'width' ) ); + $this->assertEquals( 900, $placeholder->get_setting( 'height' ) ); } /** @@ -237,7 +267,7 @@ function test_placeholder_from_invalid() { * @group joeschmoe */ function test_joeschmoe() { - $img = Image_Tag::create( static::$attachment_id, array(), array( 'image-sizes' => array( 'medium', 'large' ) ) ); + $img = Image_Tag::create( static::$attachment_id, array(), array( 'image-sizes' => array( 'medium', 'large', 'full' ) ) ); $joeschmoe = $img->joeschmoe(); $this->assertInstanceOf( 'Image_Tag_JoeSchmoe', $joeschmoe ); @@ -247,13 +277,15 @@ function test_joeschmoe() { /** * @group _placeholder * @group joeschmoe + * @group http */ function test_joeschmoe_from_invalid() { - $img = Image_Tag::create( 0, array(), array( 'image-sizes' => array( 'medium', 'large' ) ) ); + $img = Image_Tag::create( 0, array(), array( 'image-sizes' => array( 'medium', 'large', 'full' ) ) ); $joeschmoe = $img->joeschmoe(); $this->assertInstanceOf( 'Image_Tag_JoeSchmoe', $joeschmoe ); $this->assertNotEmpty( $joeschmoe->get_attribute( 'src' ) ); + $this->assertEquals( 'image/svg+xml; charset=utf-8', wp_remote_retrieve_header( $joeschmoe->http(), 'content-type' ) ); } function test_colors() { diff --git a/tests/test-wp-theme.php b/tests/test-wp-theme.php index 9243ac6..17cc4e1 100644 --- a/tests/test-wp-theme.php +++ b/tests/test-wp-theme.php @@ -36,6 +36,7 @@ function test_picsum() { /** * @group _placeholder * @group picsum + * @group http */ function test_picsum_from_invalid() { $img = Image_Tag::create( 'image-does-not-exist.jpg' ); @@ -47,6 +48,20 @@ function test_picsum_from_invalid() { ) ); $this->assertEquals( 160, $picsum->get_attribute( 'width' ) ); $this->assertEquals( 90, $picsum->get_attribute( 'height' ) ); + + $this->assertEquals( 'image/jpeg', wp_remote_retrieve_header( $picsum->http(), 'content-type' ) ); + + $img = Image_Tag::create( 0, null, array( + 'image-sizes' => array( 'full' ), + 'width' => 1600, + 'height' => 900, + ) ); + $this->assertEquals( 1600, $img->get_setting( 'width' ) ); + $this->assertEquals( 900, $img->get_setting( 'height' ) ); + + $picsum = $img->picsum(); + $this->assertEquals( 1600, $picsum->get_setting( 'width' ) ); + $this->assertEquals( 900, $picsum->get_setting( 'height' ) ); } /** @@ -61,6 +76,7 @@ function test_placeholder() { /** * @group _placeholder * @group placeholder + * @group http */ function test_placeholder_from_invalid() { $img = Image_Tag::create( 'image-does-not-exist.jpg' ); @@ -72,6 +88,20 @@ function test_placeholder_from_invalid() { ) ); $this->assertEquals( 160, $placeholder->get_attribute( 'width' ) ); $this->assertEquals( 90, $placeholder->get_attribute( 'height' ) ); + + $this->assertEquals( 'image/png', wp_remote_retrieve_header( $placeholder->http(), 'content-type' ) ); + + $img = Image_Tag::create( 0, null, array( + 'image-sizes' => array( 'full' ), + 'width' => 1600, + 'height' => 900, + ) ); + $this->assertEquals( 1600, $img->get_setting( 'width' ) ); + $this->assertEquals( 900, $img->get_setting( 'height' ) ); + + $placeholder = $img->placeholder(); + $this->assertEquals( 1600, $placeholder->get_setting( 'width' ) ); + $this->assertEquals( 900, $placeholder->get_setting( 'height' ) ); } /** @@ -86,10 +116,13 @@ function test_joeschmoe() { /** * @group _placeholder * @group joeschmoe + * @group http */ function test_joeschmoe_from_invalid() { $img = Image_Tag::create( 'image-does-not-exist.jpg' ); + $this->assertInstanceOf( 'Image_Tag_JoeSchmoe', $img->joeschmoe() ); + $this->assertEquals( 'image/svg+xml; charset=utf-8', wp_remote_retrieve_header( $img->joeschmoe()->http(), 'content-type' ) ); } function test_colors() {