Make WordPress Core

Changeset 56689


Ignore:
Timestamp:
09/25/2023 10:14:11 PM (5 months ago)
Author:
costdev
Message:

Filesystem API: Introduce filters for before/after unzipping archives.

This introduces the following new filters which wrap the process of unzipping an archive:

  • pre_unzip_file - Filters archive unzipping to allow an override with a custom process.
  • unzip_file - Filters the result of unzipping an archive.

Both filters pass the following:

  • string $file - Full path and filename of ZIP archive.
  • string $to - Full path on the filesystem to extract archive to.
  • string[] $needed_dirs - A full list of required folders that need to be created.
  • float|false $required_space - The space required to unzip the file and copy its contents, with a 10% buffer.

Props dfavor, azaozz, oglekler, afragen, costdev.
Fixes #37719.

Location:
trunk
Files:
3 added
1 edited

Legend:

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

    r56599 r56689  
    16981698    }
    16991699
     1700    // Enough space to unzip the file and copy its contents, with a 10% buffer.
     1701    $required_space = $uncompressed_size * 2.1;
     1702
    17001703    /*
    17011704     * disk_free_space() could return false. Assume that any falsey value is an error.
     
    17061709        $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false;
    17071710
    1708         if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
     1711        if ( $available_space && ( $required_space > $available_space ) ) {
    17091712            return new WP_Error(
    17101713                'disk_full_unzip_file',
     
    17471750        }
    17481751    }
    1749     unset( $needed_dirs );
     1752
     1753    /**
     1754     * Filters archive unzipping to override with a custom process.
     1755     *
     1756     * @since 6.4.0
     1757     *
     1758     * @param null|true|WP_Error $result         The result of the override. True on success, otherwise WP Error. Default null.
     1759     * @param string             $file           Full path and filename of ZIP archive.
     1760     * @param string             $to             Full path on the filesystem to extract archive to.
     1761     * @param string[]           $needed_dirs    A full list of required folders that need to be created.
     1762     * @param float              $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
     1763     */
     1764    $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space );
     1765
     1766    if ( null !== $pre ) {
     1767        // Ensure the ZIP file archive has been closed.
     1768        $z->close();
     1769
     1770        return $pre;
     1771    }
    17501772
    17511773    for ( $i = 0; $i < $z->numFiles; $i++ ) {
     
    17821804    $z->close();
    17831805
    1784     return true;
     1806    /**
     1807     * Filters the result of unzipping an archive.
     1808     *
     1809     * @since 6.4.0
     1810     *
     1811     * @param true|WP_Error $result         The result of unzipping the archive. True on success, otherwise WP_Error. Default true.
     1812     * @param string        $file           Full path and filename of ZIP archive.
     1813     * @param string        $to             Full path on the filesystem the archive was extracted to.
     1814     * @param string[]      $needed_dirs    A full list of required folders that were created.
     1815     * @param float         $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
     1816     */
     1817    $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space );
     1818
     1819    unset( $needed_dirs );
     1820
     1821    return $result;
    17851822}
    17861823
     
    18391876    }
    18401877
     1878    // Enough space to unzip the file and copy its contents, with a 10% buffer.
     1879    $required_space = $uncompressed_size * 2.1;
     1880
    18411881    /*
    18421882     * disk_free_space() could return false. Assume that any falsey value is an error.
     
    18471887        $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false;
    18481888
    1849         if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
     1889        if ( $available_space && ( $required_space > $available_space ) ) {
    18501890            return new WP_Error(
    18511891                'disk_full_unzip_file',
     
    18881928        }
    18891929    }
    1890     unset( $needed_dirs );
     1930
     1931    /** This filter is documented in src/wp-admin/includes/file.php */
     1932    $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space );
     1933
     1934    if ( null !== $pre ) {
     1935        return $pre;
     1936    }
    18911937
    18921938    // Extract the files from the zip.
     
    19101956    }
    19111957
    1912     return true;
     1958    /** This action is documented in src/wp-admin/includes/file.php */
     1959    $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space );
     1960
     1961    unset( $needed_dirs );
     1962
     1963    return $result;
    19131964}
    19141965
Note: See TracChangeset for help on using the changeset viewer.