Skip to content

Commit

Permalink
Remove empty width and height from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
junaidbhura committed Mar 5, 2018
1 parent e40b372 commit 1b181dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
16 changes: 15 additions & 1 deletion inc/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ public function get_domain() {
return $this->_urls[ $this->_url_counter - 1 ];
}

/**
* Check if the value is valid.
*
* @param string $key
* @param string $value
* @return bool
*/
public function valid_value( $key = '', $value = '' ) {
if ( ( 'w' === $key || 'h' === $key ) && empty( $value ) ) {
return false;
}
return true;
}

/**
* Build a Cloudinary transformation slug from arguments.
*
Expand Down Expand Up @@ -196,7 +210,7 @@ public function build_transformation_slug( $args = array() ) {

$slug = array();
foreach ( $args as $key => $value ) {
if ( array_key_exists( $key, $cloudinary_params ) ) {
if ( array_key_exists( $key, $cloudinary_params ) && $this->valid_value( $cloudinary_params[ $key ], $value ) ) {
$slug[] = $cloudinary_params[ $key ] . '_' . $value;
}
}
Expand Down
57 changes: 57 additions & 0 deletions tests/test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,61 @@ function test_filters() {
$this->assertEquals( $src[0], 'https://res-3.cloudinary.com/test-cloud/c_scale,w_1024,h_576,g_face/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
}

/**
* @covers \JB\Cloudinary\Core::valid_value()
*/
function test_invalid_values() {
$this->assertEquals(
cloudinary_url( self::$_image_id, array(
'transform' => array(
'width' => 0,
'height' => 0,
'crop' => 'fill',
'gravity' => 'face',
),
) ),
'https://res-1.cloudinary.com/test-cloud/c_fill,g_face/test-auto-folder' . $this->get_image_path(),
'Incorrect value.'
);

$this->assertEquals(
cloudinary_url( self::$_image_id, array(
'transform' => array(
'width' => 100,
'height' => 0,
'crop' => 'fill',
'gravity' => 'face',
),
) ),
'https://res-2.cloudinary.com/test-cloud/w_100,c_fill,g_face/test-auto-folder' . $this->get_image_path(),
'Incorrect value.'
);

$this->assertEquals(
cloudinary_url( self::$_image_id, array(
'transform' => array(
'width' => 0,
'height' => 100,
'crop' => 'fill',
'gravity' => 'face',
),
) ),
'https://res-3.cloudinary.com/test-cloud/h_100,c_fill,g_face/test-auto-folder' . $this->get_image_path(),
'Incorrect value.'
);

$this->assertEquals(
cloudinary_url( self::$_image_id, array(
'transform' => array(
'width' => '',
'height' => '',
'crop' => 'fill',
'gravity' => 'face',
),
) ),
'https://res-1.cloudinary.com/test-cloud/c_fill,g_face/test-auto-folder' . $this->get_image_path(),
'Incorrect value.'
);
}

}

1 comment on commit 1b181dc

@junaidbhura
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #1

Please sign in to comment.