Skip to content

Commit

Permalink
fixes and optimizations
Browse files Browse the repository at this point in the history
related #7
  • Loading branch information
crstauf committed Apr 11, 2020
1 parent ffa7e17 commit dd08e8b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 42 deletions.
23 changes: 16 additions & 7 deletions Image_Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion image_tags/Image_Tag_JoeSchmoe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions image_tags/Image_Tag_Picsum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion image_tags/Image_Tag_Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
41 changes: 27 additions & 14 deletions image_tags/Image_Tag_WP_Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
}
Expand Down Expand Up @@ -180,49 +181,57 @@ 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;
}

/**
* Get height of largest image version.
*
* @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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 15 additions & 10 deletions tests/test-picsum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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' ) );
}

/**
Expand All @@ -82,15 +87,15 @@ 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,
) );

$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,
Expand All @@ -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' );
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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() );
}

Expand Down Expand Up @@ -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,
) );
Expand Down Expand Up @@ -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' ) );
}

/**
Expand Down
Loading

0 comments on commit dd08e8b

Please sign in to comment.