Ticket #39476: 39476.2.diff
File 39476.2.diff, 11.9 KB (added by , 8 years ago) |
---|
-
src/wp-includes/post.php
4914 4914 /** This action is documented in wp-includes/post.php */ 4915 4915 do_action( 'deleted_post', $post_id ); 4916 4916 4917 $ uploadpath = wp_get_upload_dir();4917 $dirname = trailingslashit( dirname( $file ) ); 4918 4918 4919 4919 if ( ! empty($meta['thumb']) ) { 4920 4920 // Don't delete the thumb if another attachment uses it. 4921 4921 if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) { 4922 $thumbfile = str_replace(basename($file), $meta['thumb'], $file); 4923 /** This filter is documented in wp-includes/functions.php */ 4924 $thumbfile = apply_filters( 'wp_delete_file', $thumbfile ); 4925 @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); 4922 wp_delete_file( $dirname . $meta['thumb'] ); 4926 4923 } 4927 4924 } 4928 4925 4929 4926 // Remove intermediate and backup images if there are any. 4930 4927 if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { 4931 foreach ( $meta['sizes'] as $size => $sizeinfo ) { 4932 $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file ); 4933 /** This filter is documented in wp-includes/functions.php */ 4934 $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file ); 4935 @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) ); 4928 foreach ( $meta['sizes'] as $size ) { 4929 wp_delete_file( $dirname . $size['file'] ); 4936 4930 } 4937 4931 } 4938 4932 4939 4933 if ( is_array($backup_sizes) ) { 4940 4934 foreach ( $backup_sizes as $size ) { 4941 $del_file = path_join( dirname($meta['file']), $size['file'] ); 4942 /** This filter is documented in wp-includes/functions.php */ 4943 $del_file = apply_filters( 'wp_delete_file', $del_file ); 4944 @ unlink( path_join($uploadpath['basedir'], $del_file) ); 4935 wp_delete_file( $dirname . $size['file'] ); 4945 4936 } 4946 4937 } 4947 4938 -
tests/phpunit/tests/post/attachments.php
59 59 if ( !function_exists( 'imagejpeg' ) ) 60 60 $this->markTestSkipped( 'jpeg support unavailable' ); 61 61 62 $prev_opt_medium_size_w = get_option( 'medium_size_w' ); 63 $prev_opt_medium_size_h = get_option( 'medium_size_h' ); 64 62 65 update_option( 'medium_size_w', 0 ); 63 66 update_option( 'medium_size_h', 0 ); 64 67 … … 105 108 $this->assertEquals( 400, $downsize[1] ); 106 109 $this->assertEquals( 300, $downsize[2] ); 107 110 111 // Cleanup. 112 update_option( 'medium_size_w', $prev_opt_medium_size_w ); 113 update_option( 'medium_size_h', $prev_opt_medium_size_h ); 108 114 } 109 115 110 116 function test_insert_image_medium_sizes() { … … 111 117 if ( !function_exists( 'imagejpeg' ) ) 112 118 $this->markTestSkipped( 'jpeg support unavailable' ); 113 119 120 $prev_opt_medium_size_w = get_option( 'medium_size_w' ); 121 $prev_opt_medium_size_h = get_option( 'medium_size_h' ); 122 $prev_opt_medium_large_size_w = get_option( 'medium_large_size_w' ); 123 $prev_opt_medium_large_size_h = get_option( 'medium_large_size_h' ); 124 114 125 update_option('medium_size_w', 400); 115 126 update_option('medium_size_h', 0); 116 127 … … 162 173 $this->assertEquals( '2007-06-17DSC_4173.jpg', basename($downsize[0]) ); 163 174 $this->assertEquals( 680, $downsize[1] ); 164 175 $this->assertEquals( 1024, $downsize[2] ); 176 177 // Cleanup. 178 update_option( 'medium_size_w', $prev_opt_medium_size_w ); 179 update_option( 'medium_size_h', $prev_opt_medium_size_h ); 180 update_option( 'medium_large_size_w', $prev_opt_medium_large_size_w ); 181 update_option( 'medium_large_size_h', $prev_opt_medium_large_size_h ); 165 182 } 166 183 167 184 … … 169 186 if ( !function_exists( 'imagejpeg' ) ) 170 187 $this->markTestSkipped( 'jpeg support unavailable' ); 171 188 189 $prev_opt_medium_size_w = get_option( 'medium_size_w' ); 190 $prev_opt_medium_size_h = get_option( 'medium_size_h' ); 191 $prev_opt_medium_large_size_w = get_option( 'medium_large_size_w' ); 192 $prev_opt_medium_large_size_h = get_option( 'medium_large_size_h' ); 193 172 194 update_option('medium_size_w', 400); 173 195 update_option('medium_size_h', 0); 174 196 … … 197 219 $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] ); 198 220 $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']) ); 199 221 222 $large = image_get_intermediate_size( $id, 'large' ); 223 $this->assertEquals( '2007-06-17DSC_4173-680x1024.jpg', $large['file'] ); 224 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) ); 225 200 226 $meta = wp_get_attachment_metadata($id); 201 227 $original = $meta['file']; 202 228 $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original) ); … … 204 230 // now delete the attachment and make sure all files are gone 205 231 wp_delete_attachment($id); 206 232 207 $this->assertFalse( is_file($thumb['path']) ); 208 $this->assertFalse( is_file($medium['path']) ); 209 $this->assertFalse( is_file($medium_large['path']) ); 210 $this->assertFalse( is_file($original) ); 233 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); 234 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) ); 235 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) ); 236 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) ); 237 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); 238 239 // Cleanup. 240 update_option( 'medium_size_w', $prev_opt_medium_size_w ); 241 update_option( 'medium_size_h', $prev_opt_medium_size_h ); 242 update_option( 'medium_large_size_w', $prev_opt_medium_large_size_w ); 243 update_option( 'medium_large_size_h', $prev_opt_medium_large_size_h ); 211 244 } 212 245 213 246 /** 247 * Test delete with edited image (and backup sizes). 248 * @ticket 39476 249 */ 250 function test_insert_image_delete_with_edited_image() { 251 if ( ! function_exists( 'imagejpeg' ) ) { 252 $this->markTestSkipped( 'jpeg support unavailable' ); 253 } 254 255 $prev_opt_medium_size_w = get_option( 'medium_size_w' ); 256 $prev_opt_medium_size_h = get_option( 'medium_size_h' ); 257 258 // Generate thumbnail only. 259 update_option( 'medium_size_w', 0 ); 260 update_option( 'medium_size_h', 0 ); 261 262 $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); 263 $basename_noext = 'blah'; 264 265 $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents ); 266 $this->assertEmpty( $upload['error'] ); 267 $basename_noext = sanitize_file_name( $basename_noext ); 268 269 $id = $this->_make_attachment( $upload ); 270 $uploads = wp_upload_dir(); 271 $dirname = trailingslashit( dirname( get_attached_file( $id ) ) ); 272 $expected_files = array(); 273 274 // Check that the intermediate thumbnail exists. 275 $thumb = image_get_intermediate_size( $id, 'thumbnail' ); 276 $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] ); 277 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); 278 $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']; 279 280 // Check that the intermediate medium doesn't exist. 281 $medium = image_get_intermediate_size( $id, 'medium' ); 282 $this->assertEmpty( $medium ); 283 284 $meta = wp_get_attachment_metadata( $id ); 285 $original = $meta['file']; 286 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); 287 $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $original; 288 289 // Create edited version. 290 require_once ABSPATH . 'wp-admin/includes/image-edit.php'; 291 // a2-small.jpg is 400x300, so halve. 292 $_REQUEST = array( 'fwidth' => 200, 'fheight' => 150, 'target' => 'all', 'do' => 'scale' ); 293 $edited = wp_save_image( $id ); 294 $this->assertTrue( is_object( $edited ) ); 295 $this->assertTrue( isset( $edited->thumbnail ) ); 296 $this->assertTrue( is_file( $dirname . wp_basename( $edited->thumbnail ) ) ); 297 $expected_files[] = $dirname . wp_basename( $edited->thumbnail ); 298 299 // Backup sizes should exist. 300 $backup_sizes = get_post_meta( $id, '_wp_attachment_backup_sizes', true ); 301 $this->assertTrue( is_array( $backup_sizes ) ); 302 $this->assertNotEmpty( $backup_sizes['thumbnail-orig'] ); 303 304 // And be same as prior meta. 305 $this->assertSame( $meta['sizes']['thumbnail'], $backup_sizes['thumbnail-orig'] ); 306 $this->assertSame( wp_basename( $original ), $backup_sizes['full-orig']['file'] ); 307 308 // But new meta should point to edited. 309 $new_meta = wp_get_attachment_metadata( $id ); 310 $this->assertSame( $new_meta['sizes']['thumbnail']['file'], wp_basename( $edited->thumbnail ) ); 311 $this->assertNotSame( $new_meta['file'], $original ); 312 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'] ) ); 313 $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file']; 314 315 // Passthru filter. 316 add_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) ); 317 318 // Now delete the attachment and make sure all files are gone. 319 wp_delete_attachment( $id ); 320 321 foreach ( $expected_files as $expected_file ) { 322 $this->assertFalse( is_file( $expected_file ) ); 323 } 324 325 $this->assertSame( sort( $expected_files ), sort( self::$wp_delete_files ) ); 326 327 // Cleanup. 328 remove_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) ); 329 update_option( 'medium_size_w', $prev_opt_medium_size_w ); 330 update_option( 'medium_size_h', $prev_opt_medium_size_h ); 331 } 332 333 static $wp_delete_files = array(); 334 335 function wp_delete_file_passthru_filter( $file ) { 336 $this->assertTrue( path_is_absolute( $file ) ); 337 self::$wp_delete_files[] = $file; 338 return $file; 339 } 340 341 /** 342 * Test delete of UTF-8 filename. 343 * @ticket 33227 344 * @ticket 39476 345 */ 346 function test_insert_image_delete_utf8() { 347 if ( ! function_exists( 'imagejpeg' ) ) { 348 $this->markTestSkipped( 'jpeg support unavailable' ); 349 } 350 351 $prev_opt_medium_size_w = get_option( 'medium_size_w' ); 352 $prev_opt_medium_size_h = get_option( 'medium_size_h' ); 353 354 // Generate thumbnail only. 355 update_option( 'medium_size_w', 0 ); 356 update_option( 'medium_size_h', 0 ); 357 358 $prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE. 359 $this->assertTrue( false !== $prev_ctype_locale ); 360 361 $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); 362 $basename_noext = 'هم اندیشی'; 363 364 $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents ); 365 $this->assertEmpty( $upload['error'] ); 366 $basename_noext = sanitize_file_name( $basename_noext ); 367 368 $id = $this->_make_attachment( $upload ); 369 $uploads = wp_upload_dir(); 370 371 // Check that the intermediate thumbnail exists. 372 $thumb = image_get_intermediate_size( $id, 'thumbnail' ); 373 $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] ); 374 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); 375 376 // Check that the intermediate medium doesn't exist. 377 $medium = image_get_intermediate_size( $id, 'medium' ); 378 $this->assertEmpty( $medium ); 379 380 $meta = wp_get_attachment_metadata( $id ); 381 $original = $meta['file']; 382 $this->assertSame( $basename_noext . '.jpg', wp_basename( $original ) ); 383 $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); 384 385 // now delete the attachment and make sure all files are gone 386 wp_delete_attachment( $id ); 387 388 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) ); 389 $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) ); 390 391 // Cleanup. 392 setlocale( LC_CTYPE, $prev_ctype_locale ); 393 update_option( 'medium_size_w', $prev_opt_medium_size_w ); 394 update_option( 'medium_size_h', $prev_opt_medium_size_h ); 395 } 396 397 /** 214 398 * GUID should never be empty 215 399 * @ticket 18310 216 400 * @ticket 21963