WordPress.org

Make WordPress Core

Ticket #39476: 39476.3.diff

File 39476.3.diff, 9.6 KB (added by gitlost, 2 years ago)

Remove unnecessary "cleanup" (save/restore of options).

  • src/wp-includes/post.php

     
    49144914        /** This action is documented in wp-includes/post.php */
    49154915        do_action( 'deleted_post', $post_id );
    49164916
    4917         $uploadpath = wp_get_upload_dir();
     4917        $dirname = trailingslashit( dirname( $file ) );
    49184918
    49194919        if ( ! empty($meta['thumb']) ) {
    49204920                // Don't delete the thumb if another attachment uses it.
    49214921                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'] );
    49264923                }
    49274924        }
    49284925
    49294926        // Remove intermediate and backup images if there are any.
    49304927        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'] );
    49364930                }
    49374931        }
    49384932
    49394933        if ( is_array($backup_sizes) ) {
    49404934                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'] );
    49454936                }
    49464937        }
    49474938
  • tests/phpunit/tests/post/attachments.php

     
    5252                $this->assertEquals( basename( $upload['file'] ), basename($downsize[0]) );
    5353                $this->assertEquals( 50, $downsize[1] );
    5454                $this->assertEquals( 50, $downsize[2] );
    55 
    5655        }
    5756
    5857        function test_insert_image_thumb_only() {
     
    104103                $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
    105104                $this->assertEquals( 400, $downsize[1] );
    106105                $this->assertEquals( 300, $downsize[2] );
    107 
    108106        }
    109107
    110108        function test_insert_image_medium_sizes() {
     
    197195                $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
    198196                $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']) );
    199197
     198                $large = image_get_intermediate_size( $id, 'large' );
     199                $this->assertEquals( '2007-06-17DSC_4173-680x1024.jpg', $large['file'] );
     200                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) );
     201
    200202                $meta = wp_get_attachment_metadata($id);
    201203                $original = $meta['file'];
    202204                $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original) );
     
    204206                // now delete the attachment and make sure all files are gone
    205207                wp_delete_attachment($id);
    206208
    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) );
     209                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     210                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
     211                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
     212                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $large['path'] ) );
     213                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
    211214        }
    212215
    213216        /**
     217         * Test delete with edited image (and backup sizes).
     218         * @ticket 39476
     219         */
     220        function test_insert_image_delete_with_edited_image() {
     221                if ( ! function_exists( 'imagejpeg' ) ) {
     222                        $this->markTestSkipped( 'jpeg support unavailable' );
     223                }
     224
     225                // Generate thumbnail only.
     226                update_option( 'medium_size_w', 0 );
     227                update_option( 'medium_size_h', 0 );
     228
     229                $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' );
     230                $basename_noext = 'blah';
     231
     232                $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
     233                $this->assertEmpty( $upload['error'] );
     234                $basename_noext = sanitize_file_name( $basename_noext );
     235
     236                $id = $this->_make_attachment( $upload );
     237                $uploads = wp_upload_dir();
     238                $dirname = trailingslashit( dirname( get_attached_file( $id ) ) );
     239                $expected_files = array();
     240
     241                // Check that the intermediate thumbnail exists.
     242                $thumb = image_get_intermediate_size( $id, 'thumbnail' );
     243                $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] );
     244                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     245                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'];
     246
     247                // Check that the intermediate medium doesn't exist.
     248                $medium = image_get_intermediate_size( $id, 'medium' );
     249                $this->assertEmpty( $medium );
     250
     251                $meta = wp_get_attachment_metadata( $id );
     252                $original = $meta['file'];
     253                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     254                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $original;
     255
     256                // Create edited version.
     257                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     258                // a2-small.jpg is 400x300, so halve.
     259                $_REQUEST = array( 'fwidth' => 200, 'fheight' => 150, 'target' => 'all', 'do' => 'scale' );
     260                $edited = wp_save_image( $id );
     261                $this->assertTrue( is_object( $edited ) );
     262                $this->assertTrue( isset( $edited->thumbnail ) );
     263                $this->assertTrue( is_file( $dirname . wp_basename( $edited->thumbnail ) ) );
     264                $expected_files[] = $dirname . wp_basename( $edited->thumbnail );
     265
     266                // Backup sizes should exist.
     267                $backup_sizes = get_post_meta( $id, '_wp_attachment_backup_sizes', true );
     268                $this->assertTrue( is_array( $backup_sizes ) );
     269                $this->assertNotEmpty( $backup_sizes['thumbnail-orig'] );
     270
     271                // And be same as prior meta.
     272                $this->assertSame( $meta['sizes']['thumbnail'], $backup_sizes['thumbnail-orig'] );
     273                $this->assertSame( wp_basename( $original ), $backup_sizes['full-orig']['file'] );
     274
     275                // But new meta should point to edited.
     276                $new_meta = wp_get_attachment_metadata( $id );
     277                $this->assertSame( $new_meta['sizes']['thumbnail']['file'], wp_basename( $edited->thumbnail ) );
     278                $this->assertNotSame( $new_meta['file'], $original );
     279                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'] ) );
     280                $expected_files[] = $uploads['basedir'] . DIRECTORY_SEPARATOR . $new_meta['file'];
     281
     282                // Passthru filter.
     283                add_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) );
     284
     285                // Now delete the attachment and make sure all files are gone.
     286                wp_delete_attachment( $id );
     287
     288                foreach ( $expected_files as $expected_file ) {
     289                        $this->assertFalse( is_file( $expected_file ) );
     290                }
     291
     292                $this->assertSame( sort( $expected_files ), sort( self::$wp_delete_files ) );
     293
     294                // Cleanup.
     295                remove_filter( 'wp_delete_file', array( $this, 'wp_delete_file_passthru_filter' ) );
     296        }
     297
     298        static $wp_delete_files = array();
     299
     300        function wp_delete_file_passthru_filter( $file ) {
     301                $this->assertTrue( path_is_absolute( $file ) );
     302                self::$wp_delete_files[] = $file;
     303                return $file;
     304        }
     305
     306        /**
     307         * Test delete of UTF-8 filename.
     308         * @ticket 33227
     309         * @ticket 39476
     310         */
     311        function test_insert_image_delete_utf8() {
     312                if ( ! function_exists( 'imagejpeg' ) ) {
     313                        $this->markTestSkipped( 'jpeg support unavailable' );
     314                }
     315
     316                // Generate thumbnail only.
     317                update_option( 'medium_size_w', 0 );
     318                update_option( 'medium_size_h', 0 );
     319
     320                $prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE.
     321                $this->assertTrue( false !== $prev_ctype_locale );
     322
     323                $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' );
     324                $basename_noext = 'هم اندیشی';
     325
     326                $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
     327                $this->assertEmpty( $upload['error'] );
     328                $basename_noext = sanitize_file_name( $basename_noext );
     329
     330                $id = $this->_make_attachment( $upload );
     331                $uploads = wp_upload_dir();
     332
     333                // Check that the intermediate thumbnail exists.
     334                $thumb = image_get_intermediate_size( $id, 'thumbnail' );
     335                $this->assertSame( $basename_noext . '-150x150.jpg', $thumb['file'] );
     336                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     337
     338                // Check that the intermediate medium doesn't exist.
     339                $medium = image_get_intermediate_size( $id, 'medium' );
     340                $this->assertEmpty( $medium );
     341
     342                $meta = wp_get_attachment_metadata( $id );
     343                $original = $meta['file'];
     344                $this->assertSame( $basename_noext . '.jpg', wp_basename( $original ) );
     345                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     346
     347                // now delete the attachment and make sure all files are gone
     348                wp_delete_attachment( $id );
     349
     350                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     351                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     352
     353                // Cleanup.
     354                setlocale( LC_CTYPE, $prev_ctype_locale );
     355        }
     356
     357        /**
    214358         * GUID should never be empty
    215359         * @ticket 18310
    216360         * @ticket 21963