Make WordPress Core

Changeset 37810


Ignore:
Timestamp:
06/21/2016 02:51:38 PM (10 years ago)
Author:
joemcgill
Message:

Media: Improve handling of extensionless filenames.

Merge of [37756] to the 4.4 branch.

See #37111.

Location:
branches/4.4
Files:
2 edited

Legend:

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

    r36428 r37810  
    13651365 * to manipulate at the command line. Replaces spaces and consecutive
    13661366 * dashes with a single dash. Trims period, dash and underscore from beginning
    1367  * and end of filename.
     1367 * and end of filename. It is not guaranteed that this function will return a
     1368 * filename that is allowed to be uploaded.
    13681369 *
    13691370 * @since 2.1.0
     
    13891390        $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
    13901391        $filename = trim( $filename, '.-_' );
     1392
     1393        if ( false === strpos( $filename, '.' ) ) {
     1394                $mime_types = wp_get_mime_types();
     1395                $filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
     1396                if ( $filetype['ext'] === $filename ) {
     1397                        $filename = 'unnamed-file.' . $filetype['ext'];
     1398                }
     1399        }
    13911400
    13921401        // Split the filename into a base and extension[s]
  • branches/4.4/tests/phpunit/tests/formatting/SanitizeFileName.php

    r35122 r37810  
    5757                $this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
    5858        }
     59
     60        function test_replaces_unnammed_file_extensions() {
     61                // Test filenames with both supported and unsupported extensions.
     62                $this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
     63                $this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) );
     64        }
     65
     66        function test_replaces_unnammed_file_extensionless() {
     67                // Test a filenames that becomes extensionless.
     68                $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
     69        }
    5970}
Note: See TracChangeset for help on using the changeset viewer.