Skip to content
Open
41 changes: 40 additions & 1 deletion src/wp-admin/includes/class-plugin-upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,32 @@ public function bulk_upgrade( $plugins, $args = array() ) {

$results = array();

$packages_to_download = array();
foreach ( $plugins as $plugin ) {
if ( ! isset( $current->response[ $plugin ] ) ) {
continue;
}

$upgrade_data = $current->response[ $plugin ];

if ( isset( $upgrade_data->requires ) && ! is_wp_version_compatible( $upgrade_data->requires ) ) {
continue;
}

if ( isset( $upgrade_data->requires_php ) && ! is_php_version_compatible( $upgrade_data->requires_php ) ) {
continue;
}

if ( ! empty( $upgrade_data->package ) ) {
$packages_to_download[ $plugin ] = $upgrade_data->package;
}
}

$downloaded_packages = array();
if ( ! empty( $packages_to_download ) ) {
$downloaded_packages = download_url_multiple( $packages_to_download );
}

$this->update_count = count( $plugins );
$this->update_current = 0;
foreach ( $plugins as $plugin ) {
Expand Down Expand Up @@ -374,10 +400,16 @@ public function bulk_upgrade( $plugins, $args = array() ) {
$this->skin->error( $result );
$this->skin->after();
} else {
$package = $upgrade_data->package;
if ( ! empty( $downloaded_packages[ $plugin ] ) && ! is_wp_error( $downloaded_packages[ $plugin ] ) ) {
$package = $downloaded_packages[ $plugin ];
unset( $downloaded_packages[ $plugin ] );
}

add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
$result = $this->run(
array(
'package' => $upgrade_data->package,
'package' => $package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'clear_working' => true,
Expand All @@ -403,6 +435,13 @@ public function bulk_upgrade( $plugins, $args = array() ) {
}
} // End foreach $plugins.

// Clean up any pre-downloaded packages that were not consumed.
foreach ( $downloaded_packages as $downloaded_package ) {
if ( is_string( $downloaded_package ) && file_exists( $downloaded_package ) ) {
unlink( $downloaded_package );
}
}

$this->maintenance_mode( false );

// Force refresh of plugin update information.
Expand Down
41 changes: 40 additions & 1 deletion src/wp-admin/includes/class-theme-upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,32 @@ public function bulk_upgrade( $themes, $args = array() ) {

$results = array();

$packages_to_download = array();
foreach ( $themes as $theme ) {
if ( ! isset( $current->response[ $theme ] ) ) {
continue;
}

$upgrade_data = $current->response[ $theme ];

if ( isset( $upgrade_data['requires'] ) && ! is_wp_version_compatible( $upgrade_data['requires'] ) ) {
continue;
}

if ( isset( $upgrade_data['requires_php'] ) && ! is_php_version_compatible( $upgrade_data['requires_php'] ) ) {
continue;
}

if ( ! empty( $upgrade_data['package'] ) ) {
$packages_to_download[ $theme ] = $upgrade_data['package'];
}
}

$downloaded_packages = array();
if ( ! empty( $packages_to_download ) ) {
$downloaded_packages = download_url_multiple( $packages_to_download );
}

$this->update_count = count( $themes );
$this->update_current = 0;
foreach ( $themes as $theme ) {
Expand Down Expand Up @@ -472,10 +498,16 @@ public function bulk_upgrade( $themes, $args = array() ) {
$this->skin->error( $result );
$this->skin->after();
} else {
$package = $upgrade_data['package'];
if ( ! empty( $downloaded_packages[ $theme ] ) && ! is_wp_error( $downloaded_packages[ $theme ] ) ) {
$package = $downloaded_packages[ $theme ];
unset( $downloaded_packages[ $theme ] );
}

add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
$result = $this->run(
array(
'package' => $upgrade_data['package'],
'package' => $package,
'destination' => get_theme_root( $theme ),
'clear_destination' => true,
'clear_working' => true,
Expand All @@ -501,6 +533,13 @@ public function bulk_upgrade( $themes, $args = array() ) {
}
} // End foreach $themes.

// Clean up any pre-downloaded packages that were not consumed.
foreach ( $downloaded_packages as $downloaded_package ) {
if ( is_string( $downloaded_package ) && file_exists( $downloaded_package ) ) {
unlink( $downloaded_package );
}
}

$this->maintenance_mode( false );

// Refresh the Theme Update information.
Expand Down
108 changes: 108 additions & 0 deletions src/wp-admin/includes/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,108 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {
)
);

return _wp_handle_download_response( $response, $tmpfname, $url, $signature_verification );
}

/**
* Downloads multiple URLs to local temporary files concurrently using the WordPress HTTP API.
*
* Please note that the calling function must delete or move the files.
*
* @since 7.2.0
*
* @param string[] $urls An array of URLs to download, optionally keyed by an identifier.
* @param int $timeout Optional. The timeout for the requests to download the files.
* Default 300 seconds.
* @param bool $signature_verification Optional. Whether to perform Signature Verification.
* Default false.
* @return array<array-key, string|WP_Error> An array of results keyed identically to $urls. Each element
* is the path to the downloaded temporary file on success,
* or a WP_Error on failure.
*/
function download_url_multiple( array $urls, $timeout = 300, $signature_verification = false ) {
if ( empty( $urls ) ) {
return array();
}

$results = array();
$requests = array();
$temp_files = array();

foreach ( $urls as $id => $url ) {
if ( ! $url ) {
$results[ $id ] = new WP_Error( 'http_no_url', __( 'No URL Provided.' ) );
continue;
}

$url_path = parse_url( $url, PHP_URL_PATH );
$url_filename = '';
if ( is_string( $url_path ) && '' !== $url_path ) {
$url_filename = basename( $url_path );
}

$tmpfname = wp_tempnam( $url_filename );
if ( ! $tmpfname ) {
$results[ $id ] = new WP_Error( 'http_no_file', __( 'Could not create temporary file.' ) );
continue;
}

$temp_files[ $id ] = $tmpfname;
$requests[ $id ] = array(
'url' => $url,
'args' => array(
'timeout' => $timeout,
'stream' => true,
'filename' => $tmpfname,
),
);
}

if ( ! empty( $requests ) ) {
if ( function_exists( 'wp_safe_remote_request_multiple' ) ) {
$responses = wp_safe_remote_request_multiple( $requests );
} else {
$responses = array();
foreach ( $requests as $id => $request ) {
$responses[ $id ] = wp_safe_remote_get( $request['url'], $request['args'] );
}
}

foreach ( $responses as $id => $response ) {
$results[ $id ] = _wp_handle_download_response(
$response,
$temp_files[ $id ],
$urls[ $id ],
$signature_verification
);
}
}

// Preserve original key ordering.
$ordered_results = array();
foreach ( array_keys( $urls ) as $id ) {
$ordered_results[ $id ] = $results[ $id ];
}

return $ordered_results;
}

/**
* Processes a downloaded file response, handling HTTP status verification,
* content disposition renaming, MIME validation, and signature verification.
*
* For internal use by download_url() and download_url_multiple().
*
* @access private
* @since 7.2.0
*
* @param array|WP_Error $response The HTTP response or WP_Error.
* @param string $tmpfname The temporary file path where the stream was written.
* @param string $url The URL that was requested.
* @param bool $signature_verification Whether signature verification should be performed.
* @return string|WP_Error The path to the verified temporary file, or WP_Error on failure.
*/
function _wp_handle_download_response( $response, $tmpfname, $url, $signature_verification = false ) {
if ( is_wp_error( $response ) ) {
unlink( $tmpfname );
return $response;
Expand Down Expand Up @@ -1288,6 +1390,12 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {

// Perform signature validation if supported.
if ( $signature_verification ) {
$url_path = parse_url( $url, PHP_URL_PATH );
$url_filename = '';
if ( is_string( $url_path ) && '' !== $url_path ) {
$url_filename = basename( $url_path );
}

$signature = wp_remote_retrieve_header( $response, 'X-Content-Signature' );

if ( ! $signature ) {
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/class-wp-http-curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function request( $url, $args = array() ) {
* plugins to handle cookies themselves.
*
* @since 2.8.0
* @since 7.2.0 Also fires for each request sent by {@see WP_Http::request_multiple()}.
*
* @param resource $handle The cURL handle returned by curl_init() (passed by reference).
* @param array $parsed_args The HTTP request arguments.
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/class-wp-http-requests-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public function dispatch( $hook, $parameters = array() ) {

// Handle back-compat actions.
switch ( $hook ) {
/*
* Requests fires 'curl.before_multi_add' instead of 'curl.before_send' for the
* requests of a batch, see WP_Http::request_multiple().
*/
case 'curl.before_send':
case 'curl.before_multi_add':
/** This action is documented in wp-includes/class-wp-http-curl.php */
do_action_ref_array( 'http_api_curl', array( &$parameters[0], $this->request, $this->url ) );
break;
Expand Down
Loading
Loading