WordPress.org

Make WordPress Core

Changeset 47638


Ignore:
Timestamp:
04/29/2020 03:38:43 PM (2 months ago)
Author:
whyisjake
Message:

Formatting: Expand sanitize_file_name to have better support for utf8 characters.

Props: xknown, peterwilsoncc.

Location:
trunk
Files:
2 edited

Legend:

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

    r47550 r47638  
    20062006    $filename_raw  = $filename;
    20072007    $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', chr( 0 ) );
     2008
     2009    // Check for support for utf8 in the installed PCRE library once and store the result in a static.
     2010    static $utf8_pcre = null;
     2011    if ( ! isset( $utf8_pcre ) ) {
     2012        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
     2013        $utf8_pcre = @preg_match( '/^./u', 'a' );
     2014    }
     2015
     2016    if ( ! seems_utf8( $filename ) ) {
     2017        $_ext     = pathinfo( $filename, PATHINFO_EXTENSION );
     2018        $_name    = pathinfo( $filename, PATHINFO_FILENAME );
     2019        $filename = sanitize_title_with_dashes( $_name ) . '.' . $_ext;
     2020    }
     2021
     2022    if ( $utf8_pcre ) {
     2023        $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
     2024    }
     2025
    20082026    /**
    20092027     * Filters the list of characters to remove from a filename.
     
    20152033     */
    20162034    $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
    2017     $filename      = preg_replace( "#\x{00a0}#siu", ' ', $filename );
    20182035    $filename      = str_replace( $special_chars, '', $filename );
    20192036    $filename      = str_replace( array( '%20', '+' ), '-', $filename );
  • trunk/tests/phpunit/tests/formatting/SanitizeFileName.php

    r46586 r47638  
    6969        $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
    7070    }
     71
     72    /**
     73     * @dataProvider data_wp_filenames
     74     */
     75    function test_replaces_invalid_utf8_characters( $input, $expected ) {
     76        $this->assertEquals( $expected, sanitize_file_name( $input ) );
     77    }
     78
     79    function data_wp_filenames() {
     80        return array(
     81            [ urldecode( '%B1myfile.png' ), 'myfile.png' ],
     82            [ urldecode( '%B1myfile' ), 'myfile' ],
     83            [ 'demo bar.png', 'demo-bar.png' ],
     84            [ 'demo' . json_decode( '"\u00a0"' ) . 'bar.png', 'demo-bar.png' ],
     85        );
     86    }
    7187}
Note: See TracChangeset for help on using the changeset viewer.