Make WordPress Core

Ticket #33227: 33227.patch

File 33227.patch, 4.0 KB (added by gitlost, 9 years ago)

Fix for wp_delete_attachment() (only), includes unit test.

  • src/wp-includes/post.php

     
    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);
     4922                        $thumbfile = str_replace(wp_basename($file), $meta['thumb'], $file);
    49234923                        /** This filter is documented in wp-includes/functions.php */
    49244924                        $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    49254925                        @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     
    49284928
    49294929        // Remove intermediate and backup images if there are any.
    49304930        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4931                $basename_file = wp_basename( $file );
    49314932                foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    4932                         $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
     4933                        $intermediate_file = str_replace( $basename_file, $sizeinfo['file'], $file );
    49334934                        /** This filter is documented in wp-includes/functions.php */
    49344935                        $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    49354936                        @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
  • tests/phpunit/tests/post/attachments.php

     
    204204                // now delete the attachment and make sure all files are gone
    205205                wp_delete_attachment($id);
    206206
    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) );
     207                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     208                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
     209                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
     210                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
    211211        }
    212212
    213213        /**
     214         * Test delete of UTF-8 filename.
     215         * @ticket 39039
     216         */
     217        function test_insert_image_delete_utf8() {
     218                if ( ! function_exists( 'imagejpeg' ) ) {
     219                        $this->markTestSkipped( 'jpeg support unavailable' );
     220                }
     221
     222                $prev_ctype_locale = setlocale( LC_CTYPE, 'C' ); // In case shell env has set LC_CTYPE.
     223                $this->assertTrue( false !== $prev_ctype_locale );
     224
     225                $contents = file_get_contents( DIR_TESTDATA . '/images/a2-small.jpg' ); // Generates thumbnail only.
     226                $basename_noext = 'هم اندیشی';
     227
     228                $upload = wp_upload_bits( $basename_noext . '.jpg', null, $contents );
     229                $this->assertEmpty( $upload['error'] );
     230                $basename_noext = sanitize_file_name( $basename_noext );
     231
     232                $id = $this->_make_attachment( $upload );
     233                $uploads = wp_upload_dir();
     234
     235                // check that the intermediate thumbnail exists.
     236                $thumb = image_get_intermediate_size( $id, 'thumbnail' );
     237                $this->assertEquals( $basename_noext . '-150x150.jpg', $thumb['file'] );
     238                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     239
     240                $meta = wp_get_attachment_metadata( $id );
     241                $original = $meta['file'];
     242                $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     243
     244                // now delete the attachment and make sure all files are gone
     245                wp_delete_attachment( $id );
     246
     247                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     248                $this->assertFalse( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
     249
     250                setlocale( LC_CTYPE, $prev_ctype_locale );
     251        }
     252
     253        /**
    214254         * GUID should never be empty
    215255         * @ticket 18310
    216256         * @ticket 21963