Media: Restore the ability of WP_Image_Editor_Imagick->save() to create a missing directory when needed.

Props eemitch, mikeschroder, hellofromTonya, p00ya, johnbillion

Fixes #51665


git-svn-id: https://develop.svn.wordpress.org/trunk@49542 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2020-11-09 13:16:54 +00:00
parent 6b76c5222d
commit e4f47125ab
2 changed files with 37 additions and 0 deletions

View File

@ -741,6 +741,20 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
return true;
}
} else {
$dir_name = dirname( $filename );
$dir_exists = wp_mkdir_p( $dir_name );
if ( ! $dir_exists ) {
return new WP_Error(
'image_save_error',
sprintf(
/* translators: %s: Directory path. */
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
esc_html( $dir_name )
)
);
}
try {
return $image->writeImage( $filename );
} catch ( Exception $e ) {

View File

@ -597,4 +597,27 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
unlink( $temp_file );
}
/**
* @ticket 51665
*/
public function test_directory_creation() {
$file = realpath( DIR_TESTDATA ) . '/images/a2-small.jpg';
$directory = realpath( DIR_TESTDATA ) . '/images/nonexistent-directory';
$editor = new WP_Image_Editor_Imagick( $file );
$this->assertFileNotExists( $directory );
$loaded = $editor->load();
$this->assertNotWPError( $loaded );
$resized = $editor->resize( 100, 100, true );
$this->assertNotWPError( $resized );
$saved = $editor->save( $directory . '/a2-small-cropped.jpg' );
$this->assertNotWPError( $saved );
unlink( $directory . '/a2-small-cropped.jpg' );
rmdir( $directory );
}
}