wppaste
WordPress

WP_Upgrader::unpack_package( string $package, bool $delete_package = true ): string|WP_Error

Since
2.8.0
Source
wp-admin/includes/class-wp-upgrader.php:356
Unpacks a compressed package file.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$packagestring
Full path to the package file.
$delete_packagebooloptional
Whether to delete the package file after attempting to unpack it. Default true.Default: true

Return value

string|WP_Error
The path to the unpacked contents, or a WP_Error on failure.

Performance profile

How much work a call to WP_Upgrader::unpack_package() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via unlink().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
18–71

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessunlink()called directly
  • sqldatabase query->delete()called directly
  • hookthird-party callbacksapply_filters()one call below WP_Upgrader::unpack_package()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 13 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Upgrader::unpack_package() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!->wp_content_dir()18->feedback(), ->wp_content_dir()
->wp_content_dir() && !->is_dir() && !is_wp_error()43–46->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), is_wp_error()
->wp_content_dir() && !->is_dir() && !is_wp_error()46–49->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), unlink(), is_wp_error()
->wp_content_dir() && ->is_dir() && !is_wp_error()47–50->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), is_wp_error()
->wp_content_dir() && ->is_dir() && !is_wp_error()50–53->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), unlink(), is_wp_error()
->wp_content_dir() && !->is_dir() && is_wp_error()51–54->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), is_wp_error(), ->delete(), ->get_error_code()
->wp_content_dir() && !->is_dir() && is_wp_error()54–57->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), unlink(), is_wp_error(), ->delete(), ->get_error_code()
->wp_content_dir() && ->is_dir() && is_wp_error()55–58->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), is_wp_error(), ->delete(), ->get_error_code()
->wp_content_dir() && ->is_dir() && is_wp_error()58–61->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), unlink(), is_wp_error(), ->delete(), ->get_error_code()
->wp_content_dir() && !->is_dir() && is_wp_error()61–64->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), is_wp_error(), ->delete(), ->get_error_code(), ->get_error_data()
->wp_content_dir() && !->is_dir() && is_wp_error()64–67->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), unzip_file(), unlink(), is_wp_error(), ->delete(), ->get_error_code(), ->get_error_data()
->wp_content_dir() && ->is_dir() && is_wp_error()65–68->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), is_wp_error(), ->delete(), ->get_error_code(), ->get_error_data()
1 further outcome, up to 71 instructions
->wp_content_dir() && ->is_dir() && is_wp_error()68–71->feedback(), ->wp_content_dir(), ->wp_content_dir(), ->dirlist(), basename(), basename(), ->is_dir(), ->delete(), unzip_file(), unlink(), is_wp_error(), ->delete(), ->get_error_code(), ->get_error_data()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 88 instructions, 18–71 executed per call, 8 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

Used by · 1

Source code

	public function unpack_package( $package, $delete_package = true ) {		global $wp_filesystem; 		$this->skin->feedback( 'unpack_package' ); 		if ( ! $wp_filesystem->wp_content_dir() ) {			return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );		} 		$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/'; 		// Clean up contents of upgrade directory beforehand.		$upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );		if ( ! empty( $upgrade_files ) ) {			foreach ( $upgrade_files as $file ) {				$wp_filesystem->delete( $upgrade_folder . $file['name'], true );			}		} 		// We need a working directory - strip off any .tmp or .zip suffixes.		$working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' ); 		// Clean up working directory.		if ( $wp_filesystem->is_dir( $working_dir ) ) {			$wp_filesystem->delete( $working_dir, true );		} 		// Unzip package to working directory.		$result = unzip_file( $package, $working_dir ); 		// Once extracted, delete the package if required.		if ( $delete_package ) {			unlink( $package );		} 		if ( is_wp_error( $result ) ) {			$wp_filesystem->delete( $working_dir, true );			if ( 'incompatible_archive' === $result->get_error_code() ) {				return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );			}			return $result;		} 		return $working_dir;	}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/class-wp-upgrader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.