WP_Upgrader::download_package( string $package, bool $check_signatures = false, array $hook_extra = array() ): string|WP_Error
- Since
- 2.8.0, 5.2.0, 5.5.0
- Source
wp-admin/includes/class-wp-upgrader.php:307
Compatibility
- WordPress
- since 5.5.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- The URI of the package. May be a remote URL or local file path. If this is the full path to an existing local file, it will be returned untouched.
$check_signaturesbooloptional- Whether to validate file signatures. Default false.Default:
false $hook_extraarrayoptional- Extra arguments to pass to the filter hooks. Default empty array.Default:
array()
Return value
string|WP_Error- The full path to the downloaded package file, or a WP_Error object.
Performance profile
How much work a call to WP_Upgrader::download_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
- Expensive
- Scaling
- Constant
- Instructions
- 15–52
- Plugin surface
- 1 hook
- Called by
- 1
Reaches an outbound HTTP request via wp_safe_remote_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.
Third-party callbacks on 'upgrader_pre_download' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()called directly - httpoutbound HTTP request
wp_safe_remote_get()one call below WP_Upgrader::download_package()
Further down the call graph this can also reach transient, option, query, cache and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Upgrader::download_package() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–26 | apply_filters() |
$reply === false && !$package | 21–30 | apply_filters(), file_exists() |
$reply === false && $package && !empty($package) && !is_wp_error() | 34 | apply_filters(), ->feedback(), download_url(), is_wp_error() |
$reply === false && $package && !empty($package) && is_wp_error() && ->get_error_data() | 38 | apply_filters(), ->feedback(), download_url(), is_wp_error(), ->get_error_data() |
$reply === false && !$package && !file_exists() && !empty($package) && !is_wp_error() | 38 | apply_filters(), file_exists(), ->feedback(), download_url(), is_wp_error() |
$reply === false && !$package && !file_exists() && !empty($package) && is_wp_error() && ->get_error_data() | 42 | apply_filters(), file_exists(), ->feedback(), download_url(), is_wp_error(), ->get_error_data() |
$reply === false && $package && !empty($package) && is_wp_error() && !->get_error_data() | 48 | apply_filters(), ->feedback(), download_url(), is_wp_error(), ->get_error_data(), ->get_error_message() |
$reply === false && !$package && !file_exists() && !empty($package) && is_wp_error() && !->get_error_data() | 52 | apply_filters(), file_exists(), ->feedback(), download_url(), is_wp_error(), ->get_error_data(), ->get_error_message() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 63 | 15–52 | 6 | |
| 8.5 | 63 | 15–52 | 6 | |
| 8.4 | 63 | 15–52 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 66 | 15–55 | 6 | |
| 8.2 | 66 | 15–55 | 6 | |
| 8.1 | 66 | 15–55 | 6 | |
| 7.4 | 66 | 15–55 | 6 |
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.
Hooks and filters fired · 1
One hook fires while WP_Upgrader::download_package() runs, in this order:
- apply_filters( upgrader_pre_download )filterline 322 (+15 into the body)
Filters whether to download a package for a WordPress core, plugin, theme, or translation upgrade.
Uses · 4
- apply_filters()Calls the callback functions that have been added to a filter hook.
- download_url()Downloads a URL to a local temporary file using the WordPress HTTP API.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_Upgrader::run()Runs an upgrade/installation.
Source code
public function download_package( $package, $check_signatures = false, $hook_extra = array() ) { /** * Filters whether to download a package for a WordPress core, plugin, theme, or translation upgrade. * * Return a non-false value to short-circuit the download and return that value instead. * * @since 3.7.0 * @since 5.5.0 Added the `$hook_extra` parameter. * * @param false|string|WP_Error $reply Whether to short-circuit the download, the path to the downloaded package, * or a WP_Error object. Default false. * @param string $package The package URI. May be a remote URL or local file path. * @param WP_Upgrader $upgrader The WP_Upgrader instance. * @param array $hook_extra Extra arguments passed to hooked filters. */ $reply = apply_filters( 'upgrader_pre_download', false, $package, $this, $hook_extra ); if ( false !== $reply ) { return $reply; } if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote? return $package; // Must be a local file. } if ( empty( $package ) ) { return new WP_Error( 'no_package', $this->strings['no_package'] ); } $this->skin->feedback( 'downloading_package', $package ); $download_file = download_url( $package, 300, $check_signatures ); if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) { return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() ); } return $download_file; }Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$hook_extra parameter.from the docblock$check_signatures parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.