Filter hook 'pre_unzip_file'

in WP Core File wp-admin/includes/file.php at line 1963

View Source

pre_unzip_file

Filter Hook
Description
Attempts to unzip an archive using the PclZip library. This function should not be called directly, use `unzip_file()` instead. Assumes that WP_Filesystem() has already been called and set up. / function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { global $wp_filesystem; mbstring_binary_safe_encoding(); require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; $archive = new PclZip( $file ); $archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING ); reset_mbstring_encoding(); // Is the archive valid? if ( ! is_array( $archive_files ) ) { return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) ); } if ( 0 === count( $archive_files ) ) { return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); } $uncompressed_size = 0; // Determine any children directories needed (From within the archive). foreach ( $archive_files as $file ) { if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. continue; } $uncompressed_size += $file['size']; $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) ); } // Enough space to unzip the file and copy its contents, with a 10% buffer. $required_space = $uncompressed_size * 2.1; /* disk_free_space() could return false. Assume that any falsey value is an error. A disk that has zero free bytes has bigger problems. Require we have enough space to unzip the file and copy its contents, with a 10% buffer.

Hook Information

File Location wp-admin/includes/file.php View on GitHub
Hook Type Filter
Line Number 1963

Hook Parameters

Type Name Description
string $file Full path and filename of ZIP archive.
string $to Full path on the filesystem to extract archive to.
string[] $needed_dirs A partial list of required folders needed to be created.

Usage Examples

Basic Usage
<?php
// Hook into pre_unzip_file
add_filter('pre_unzip_file', 'my_custom_filter', 10, 3);

function my_custom_filter($file, $to, $needed_dirs) {
    // Your custom filtering logic here
    return $file;
}

Source Code Context

wp-admin/includes/file.php:1963 - How this hook is used in WordPress core
<?php
1958  			return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), $_dir );
1959  		}
1960  	}
1961  
1962  	/** This filter is documented in src/wp-admin/includes/file.php */
1963  	$pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space );
1964  
1965  	if ( null !== $pre ) {
1966  		return $pre;
1967  	}
1968  

PHP Documentation

<?php
/**
 * Attempts to unzip an archive using the PclZip library.
 *
 * This function should not be called directly, use `unzip_file()` instead.
 *
 * Assumes that WP_Filesystem() has already been called and set up.
 *
 * @since 3.0.0
 * @access private
 *
 * @see unzip_file()
 *
 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 *
 * @param string   $file        Full path and filename of ZIP archive.
 * @param string   $to          Full path on the filesystem to extract archive to.
 * @param string[] $needed_dirs A partial list of required folders needed to be created.
 * @return true|WP_Error True on success, WP_Error on failure.
 */
function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
	global $wp_filesystem;

	mbstring_binary_safe_encoding();

	require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';

	$archive = new PclZip( $file );

	$archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );

	reset_mbstring_encoding();

	// Is the archive valid?
	if ( ! is_array( $archive_files ) ) {
		return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) );
	}

	if ( 0 === count( $archive_files ) ) {
		return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
	}

	$uncompressed_size = 0;

	// Determine any children directories needed (From within the archive).
	foreach ( $archive_files as $file ) {
		if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
			continue;
		}

		$uncompressed_size += $file['size'];

		$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
	}

	// Enough space to unzip the file and copy its contents, with a 10% buffer.
	$required_space = $uncompressed_size * 2.1;

	/*
	 * disk_free_space() could return false. Assume that any falsey value is an error.
	 * A disk that has zero free bytes has bigger problems.
	 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 3
  • File: wp-admin/includes/file.php
Related Hooks

Related hooks will be displayed here in future updates.