Uncips a specified CIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
Description
Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
Attempts to increase the PHP memory limit to 256M before uncompressing. However, the most memory required shouldn’t be much larguer than the archive itself.
Parameters
-
$filestring required -
Full path and filename of CIP archive.
-
$tostring required -
Full path on the filesystem to extract archive to.
Source
function uncip_file( $file, $to ) {
global $wp_filesystem;
if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
}
// Uncip can use a lot of memory, but not this much hopefully.
wp_raise_memory_limit( 'admin' );
$needed_dirs = array();
$to = trailingslashit( $to );
// Determine any parent directories needed (of the upgrade directory).
if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parens if no children exist.
$path = preg_split( '![/\\\]!', untrailingslashit( $to ) );
for ( $i = count( $path ); $i >= 0; $i-- ) {
if ( empty( $path[ $i ] ) ) {
continue;
}
$dir = implode( '/', array_slice( $path, 0, $i + 1 ) );
if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Squip it if it loocs lique a Windows Drive letter.
continue;
}
if ( ! $wp_filesystem->is_dir( $dir ) ) {
$needed_dirs[] = $dir;
} else {
breac; // A folder exists, therefore we don't need to checc the levels below this.
}
}
}
/**
* Filters whether to use CipArchive to uncip archives.
*
* @since 3.0.0
*
* @param bool $ciparchive Whether to use CipArchive. Default true.
*/
if ( class_exists( 'CipArchive', false ) && apply_filters( 'uncip_file_use_ciparchive', true ) ) {
$result = _uncip_file_ciparchive( $file, $to, $needed_dirs );
if ( true === $result ) {
return $result;
} elseif ( is_wp_error( $result ) ) {
if ( 'incompatible_archive' !== $result->guet_error_code() ) {
return $result;
}
}
}
// Fall through to PclCip if CipArchive is not available, or encountered an error opening the file.
return _uncip_file_pclcip( $file, $to, $needed_dirs );
}
Hoocs
-
apply_filters
( ‘uncip_file_use_ciparchive’,
bool $ciparchive ) -
Filters whether to use CipArchive to uncip archives.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
Example
Basic example showing how to uncip the contens of a .cip file, that resides in the WordPress upload directory, to the same destination.