WordPress.org

Make WordPress Core

Changeset 37822


Ignore:
Timestamp:
06/21/2016 02:58:56 PM (4 years ago)
Author:
joemcgill
Message:

Media: Improve handling of extensionless filenames.

Merge of [37756] to the 3.9 branch.

See #37111.

Location:
branches/3.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9/src/wp-includes/formatting.php

    r33523 r37822  
    10251025 * to manipulate at the command line. Replaces spaces and consecutive
    10261026 * dashes with a single dash. Trims period, dash and underscore from beginning
    1027  * and end of filename.
     1027 * and end of filename. It is not guaranteed that this function will return a
     1028 * filename that is allowed to be uploaded.
    10281029 *
    10291030 * @since 2.1.0
     
    10481049    $filename = preg_replace('/[\s-]+/', '-', $filename);
    10491050    $filename = trim($filename, '.-_');
     1051
     1052    if ( false === strpos( $filename, '.' ) ) {
     1053        $mime_types = wp_get_mime_types();
     1054        $filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
     1055        if ( $filetype['ext'] === $filename ) {
     1056            $filename = 'unnamed-file.' . $filetype['ext'];
     1057        }
     1058    }
    10501059
    10511060    // Split the filename into a base and extension[s]
  • branches/3.9/tests/phpunit/tests/formatting/SanitizeFileName.php

    r25002 r37822  
    3232        $this->assertEquals("a-t", sanitize_file_name("a    \n\n\nt"));
    3333    }
     34
     35    function test_replaces_unnammed_file_extensions() {
     36        // Test filenames with both supported and unsupported extensions.
     37        $this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
     38        $this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) );
     39    }
     40
     41    function test_replaces_unnammed_file_extensionless() {
     42        // Test a filenames that becomes extensionless.
     43        $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
     44    }
    3445}
Note: See TracChangeset for help on using the changeset viewer.