Make WordPress Core

Ticket #39875: 39875-major.diff

File 39875-major.diff, 4.4 KB (added by desrosj, 7 years ago)
  • src/wp-admin/includes/image-edit.php

     
    776776                        $new_path = $path;
    777777                }
    778778        } else {
    779                 while ( true ) {
    780                         $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
    781                         $filename .= "-e{$suffix}";
    782                         $new_filename = "{$filename}.{$ext}";
    783                         $new_path = "{$dirname}/$new_filename";
    784                         if ( file_exists($new_path) ) {
    785                                 $suffix++;
    786                         } else {
    787                                 break;
    788                         }
    789                 }
     779                $new_path = wp_generate_random_file_name( $path, $dirname, $ext, 'e', $suffix );
    790780        }
    791781
    792782        // Save the full-size file, also needed to create sub-sizes.
  • src/wp-admin/includes/image.php

     
    251251                        $editor = wp_get_image_editor( $file );
    252252
    253253                        if ( ! is_wp_error( $editor ) ) { // No support for this type of file
    254                                 $uploaded = $editor->save( $file, 'image/jpeg' );
     254                                /*
     255                                 * PDFs may have the same file filename as JPGs.
     256                                 * Use a random string for the PDF preview image to prevent the original JPG from being overwritten.
     257                                 */
     258                                $dirname = pathinfo( $file, PATHINFO_DIRNAME );
     259                                $suffix = time() . rand( 100, 999 );
     260
     261                                $preview_filename = wp_generate_random_file_name( $file, $dirname, 'jpg', 'pdf', $suffix );
     262                                $uploaded = $editor->save( $preview_filename, 'image/jpeg' );
     263
    255264                                unset( $editor );
    256265
    257266                                // Resize based on the full size image, rather than the source.
  • src/wp-includes/functions.php

     
    20982098}
    20992099
    21002100/**
     2101 * Generates a random string for a filename.
     2102 *
     2103 * @param string $path    File path.
     2104 * @param string $dirname File directory name.
     2105 * @param string $prefix  Prefix for the file name.
     2106 * @param string $suffix  Suffix for the file name.
     2107 *
     2108 * @return string Generated unique file name.
     2109 */
     2110function wp_generate_random_file_name( $path, $dirname, $ext, $prefix = '', $suffix = '' ) {
     2111        $file_name = '';
     2112        $suffix = time() . rand( 100, 999 );
     2113        $name_parts = array();
     2114
     2115        if ( ! empty( $suffix ) ) {
     2116                $name_parts[] = $suffix;
     2117        }
     2118
     2119        if ( ! empty( $prefix ) ) {
     2120                $name_parts[] = $prefix;
     2121        }
     2122
     2123        while ( true ) {
     2124                $new_filename = implode( '-', $name_parts );
     2125                $file_name = "{$new_filename}.{$ext}";
     2126                $new_path = "{$dirname}/$file_name";
     2127
     2128                if ( file_exists( $new_path ) ) {
     2129                        $suffix++;
     2130                } else {
     2131                        $file_name = $new_path;
     2132                        break;
     2133                }
     2134        }
     2135
     2136        return $file_name;
     2137}
     2138
     2139/**
    21012140 * Create a file in the upload folder with given content.
    21022141 *
    21032142 * If there is an error, then the key 'error' will exist with the error message.
  • tests/phpunit/tests/image/functions.php

     
    400400                );
    401401
    402402                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    403                 $this->assertSame( $expected, $metadata );
     403                $this->assertArrayHasKey( 'sizes', $metadata, 'attachment should have size data' );
     404                $this->assertEquals( count( $expected['sizes'] ), count( $metadata['sizes'] ) );
     405
     406                $this->assertContains( '-pdf', $metadata['sizes']['medium']['file'] );
     407                $this->assertEquals( $expected['sizes']['medium']['height'], $metadata['sizes']['medium']['height'] );
     408
     409                $this->assertContains( '-pdf', $metadata['sizes']['large']['file'] );
     410                $this->assertEquals( $expected['sizes']['large']['width'], $metadata['sizes']['large']['width'] );
    404411
    405412                unlink( $test_file );
    406413        }
     
    435442
    436443                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    437444                $this->assertTrue( isset( $metadata['sizes']['test-size'] ), 'The `test-size` was not added to the metadata.' );
    438                 $this->assertSame( $metadata['sizes']['test-size'], $expected );
     445                $this->assertContains( '-pdf', $metadata['sizes']['test-size']['file'] );
     446                $this->assertEquals( $expected['height'], $metadata['sizes']['test-size']['height'] );
    439447
    440448                remove_image_size( 'test-size' );
    441449                remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );