Make WordPress Core

Changeset 61374


Ignore:
Timestamp:
12/13/2025 08:10:50 PM (12 days ago)
Author:
SergeyBiryukov
Message:

Filesystem API: Pass correct $file value to pre_unzip_file and unzip_file filters.

This commit ensures that the original $file argument passed to the function is not unintentionally overwritten by the use of the same variable name in two foreach loops.

Follow-up to [56689].

Props sanchothefat, westonruter, mukesh27, SergeyBiryukov.
Fixes #64398.

Location:
trunk
Files:
2 edited

Legend:

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

    r60309 r61374  
    18971897
    18981898    // Determine any children directories needed (From within the archive).
    1899     foreach ( $archive_files as $file ) {
    1900         if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
     1899    foreach ( $archive_files as $archive_file ) {
     1900        if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
    19011901            continue;
    19021902        }
    19031903
    1904         $uncompressed_size += $file['size'];
    1905 
    1906         $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
     1904        $uncompressed_size += $archive_file['size'];
     1905
     1906        $needed_dirs[] = $to . untrailingslashit( $archive_file['folder'] ? $archive_file['filename'] : dirname( $archive_file['filename'] ) );
    19071907    }
    19081908
     
    19681968
    19691969    // Extract the files from the zip.
    1970     foreach ( $archive_files as $file ) {
    1971         if ( $file['folder'] ) {
     1970    foreach ( $archive_files as $archive_file ) {
     1971        if ( $archive_file['folder'] ) {
    19721972            continue;
    19731973        }
    1974 
    1975         if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
     1974   
     1975        if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
    19761976            continue;
    19771977        }
    19781978
    19791979        // Don't extract invalid files:
    1980         if ( 0 !== validate_file( $file['filename'] ) ) {
     1980        if ( 0 !== validate_file( $archive_file['filename'] ) ) {
    19811981            continue;
    19821982        }
    19831983
    1984         if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
    1985             return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
     1984        if ( ! $wp_filesystem->put_contents( $to . $archive_file['filename'], $archive_file['content'], FS_CHMOD_FILE ) ) {
     1985            return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $archive_file['filename'] );
    19861986        }
    19871987    }
  • trunk/tests/phpunit/tests/filesystem/unzipFilePclzip.php

    r61212 r61374  
    3838    public function test_should_apply_pre_unzip_file_filters() {
    3939        $filter = new MockAction();
    40         add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
     40        add_filter( 'pre_unzip_file', array( $filter, 'filter' ), 10, 2 );
    4141
    4242        // Prepare test environment.
     
    5454        $this->delete_folders( $unzip_destination );
    5555
    56         $this->assertSame( 1, $filter->get_call_count() );
     56        $this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
     57        $this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
    5758    }
    5859
     
    6465    public function test_should_apply_unzip_file_filters() {
    6566        $filter = new MockAction();
    66         add_filter( 'unzip_file', array( $filter, 'filter' ) );
     67        add_filter( 'unzip_file', array( $filter, 'filter' ), 10, 2 );
    6768
    6869        // Prepare test environment.
     
    8081        $this->delete_folders( $unzip_destination );
    8182
    82         $this->assertSame( 1, $filter->get_call_count() );
     83        $this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
     84        $this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
    8385    }
    8486}
Note: See TracChangeset for help on using the changeset viewer.