diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b71c0b4d72..9705622305 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -181,27 +181,55 @@ function image_downsize($id, $size = 'medium') { } /** - * Registers a new image size + * Register a new image size. * * @since 2.9.0 + * + * @param string $name The image size name. + * @param int $width The image's width. + * @param int $height The image's width. + * @param bool $width Whether to crop the image to fit the dimensions. Default false. */ function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { global $_wp_additional_image_sizes; - $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); + $_wp_additional_image_sizes[ $name ] = array( + 'width' => absint( $width ), + 'height' => absint( $height ), + 'crop' => (bool) $crop, + ); } /** - * Check if an image size exists + * Check if an image size exists. * * @since 3.9.0 * - * @param string $name The image size name. + * @param string $name The image size to check. * @return bool True if it exists, false if not. */ function has_image_size( $name = '' ) { global $_wp_additional_image_sizes; - return isset( $_wp_additional_image_sizes[$name] ); + return isset( $_wp_additional_image_sizes[ $name ] ); +} + +/** + * Remove a new image size. + * + * @since 3.9.0 + * + * @param string $name The image size to remove. + * @return bool True on success, false on failure. + */ +function remove_image_size( $name ) { + global $_wp_additional_image_sizes; + + if ( isset( $_wp_additional_image_sizes[ $name ] ) ) { + unset( $_wp_additional_image_sizes[ $name ] ); + return true; + } + + return false; } /** diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index c149b69c0d..32c25b20fd 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -406,4 +406,35 @@ VIDEO; $this->assertEquals( $expected, $content ); } + + /** + * @ticket 26768 + */ + function test_add_image_size() { + global $_wp_additional_image_sizes; + $this->assertArrayNotHasKey( 'test-size', $_wp_additional_image_sizes ); + add_image_size( 'test-size', 200, 600 ); + $this->assertArrayHasKey( 'test-size', $_wp_additional_image_sizes ); + $this->assertEquals( 200, $_wp_additional_image_sizes['test-size']['width'] ); + $this->assertEquals( 600, $_wp_additional_image_sizes['test-size']['height'] ); + } + + /** + * @ticket 26768 + */ + function test_remove_image_size() { + add_image_size( 'test-size', 200, 600 ); + $this->assertTrue( has_image_size( 'test-size' ) ); + remove_image_size( 'test-size' ); + $this->assertFalse( has_image_size( 'test-size' ) ); + } + + /** + * @ticket 26951 + */ + function test_has_image_size() { + add_image_size( 'test-size', 200, 600 ); + $this->assertTrue( has_image_size( 'test-size' ) ); + } + }