From 4118d809c33b7b6eb6b01b819e3b8e637e85851a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 22 Sep 2026 12:00:00 +0000 Subject: [PATCH 01/10] HTTP API: Add `wp_remote_request_multiple()` for concurrent requests. Introduce `WP_Http::request_multiple()`, `wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()`, which send a batch of requests side by side and return their responses keyed and ordered like the requests. `WP_Http::request()` is split into `prepare_request()` and `finalize_response()`, so a batched request goes through the same steps as a single one: the `http_request_args`, `pre_http_request` and `http_response` filters, URL validation, blocked hosts, proxies and the `http_api_debug` action all apply to each request individually. Compared to the earlier patch on the ticket: * The single-request functions keep their signatures instead of accepting an array of requests, so `reject_unsafe_urls` can still be passed for a batch. * Batches are sent at most `concurrency` requests at a time (default 6, filterable with `http_request_multiple_concurrency`). * The `http_api_curl` action fires for batched requests too. Requests dispatches `curl.before_multi_add` instead of `curl.before_send` for them, which `WP_HTTP_Requests_Hooks` did not map. * When the Requests library rejects a batch as a whole, its requests are retried one by one so that each gets its own response or error. See #37459. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01RvSBA4zz2vHphxunmJCV2G --- src/wp-includes/class-wp-http-curl.php | 1 + .../class-wp-http-requests-hooks.php | 5 + src/wp-includes/class-wp-http.php | 234 ++++++++- src/wp-includes/http.php | 59 +++ .../tests/http/wpRemoteRequestMultiple.php | 478 ++++++++++++++++++ 5 files changed, 767 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/tests/http/wpRemoteRequestMultiple.php diff --git a/src/wp-includes/class-wp-http-curl.php b/src/wp-includes/class-wp-http-curl.php index 4296bf42f2b00..d793fb7aed6a5 100644 --- a/src/wp-includes/class-wp-http-curl.php +++ b/src/wp-includes/class-wp-http-curl.php @@ -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 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. diff --git a/src/wp-includes/class-wp-http-requests-hooks.php b/src/wp-includes/class-wp-http-requests-hooks.php index f399acc860229..d40abebf956d6 100644 --- a/src/wp-includes/class-wp-http-requests-hooks.php +++ b/src/wp-includes/class-wp-http-requests-hooks.php @@ -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; diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 13b82d95bfbd2..1a0c73f2deb06 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -168,6 +168,198 @@ class WP_Http { * } */ public function request( $url, $args = array() ) { + $prepared = $this->prepare_request( $url, $args ); + + if ( is_wp_error( $prepared ) ) { + return $prepared; + } + + // The request was short-circuited by the 'pre_http_request' filter. + if ( isset( $prepared['response'] ) ) { + return $prepared['response']; + } + + // Avoid issues where mbstring.func_overload is enabled. + mbstring_binary_safe_encoding(); + + try { + $response = WpOrg\Requests\Requests::request( + $prepared['url'], + $prepared['headers'], + $prepared['data'], + $prepared['type'], + $prepared['options'] + ); + } catch ( WpOrg\Requests\Exception $e ) { + $response = new WP_Error( 'http_request_failed', $e->getMessage() ); + } + + reset_mbstring_encoding(); + + return $this->finalize_response( $response, $prepared['args'], $prepared['url'] ); + } + + /** + * Sends multiple HTTP requests concurrently and returns their responses. + * + * Every request is prepared, and its response processed, exactly like a request + * sent through WP_Http::request(): the 'http_request_args', 'pre_http_request' and + * 'http_response' filters and the 'http_api_debug' action apply to each request + * individually. The requests that were not short-circuited are then sent side by + * side, at most $concurrency at a time. + * + * Non-blocking requests are not supported: every request is performed in full. + * + * @since 7.2.0 + * + * @see WP_Http::request() For the accepted request arguments and the response format. + * + * @param array $requests { + * Requests to send, keyed by an identifier of the caller's choosing. Each request is + * either a URL string, for a GET request with the default arguments, or an array: + * + * @type string $url The request URL. + * @type string|array $args Optional. Request arguments. See WP_Http::request(). + * } + * @param array $options { + * Optional. Options that apply to the batch of requests. + * + * @type int $concurrency Maximum number of requests to send at once. Default 6. + * } + * @return array Responses keyed like $requests. Each is a response array, or a WP_Error + * on failure. See WP_Http::request() for the response format. + */ + public function request_multiple( array $requests, array $options = array() ) { + $responses = array(); + $pending = array(); + + foreach ( $requests as $id => $request ) { + if ( ! is_array( $request ) ) { + $request = array( 'url' => $request ); + } + + $url = isset( $request['url'] ) ? $request['url'] : ''; + $args = isset( $request['args'] ) ? wp_parse_args( $request['args'] ) : array(); + + // Batches cannot send non-blocking requests, each request is performed in full. + $args['blocking'] = true; + + $prepared = $this->prepare_request( $url, $args ); + + if ( is_wp_error( $prepared ) ) { + $responses[ $id ] = $prepared; + } elseif ( isset( $prepared['response'] ) ) { + // The request was short-circuited by the 'pre_http_request' filter. + $responses[ $id ] = $prepared['response']; + } else { + $pending[ $id ] = $prepared; + } + } + + if ( ! empty( $pending ) ) { + $concurrency = isset( $options['concurrency'] ) ? (int) $options['concurrency'] : 6; + + /** + * Filters the maximum number of HTTP requests that WP_Http::request_multiple() sends at once. + * + * @since 7.2.0 + * + * @param int $concurrency Maximum number of requests to send at once. Default 6. + * @param array $requests The requests to send. See WP_Http::request_multiple(). + */ + $concurrency = (int) apply_filters( 'http_request_multiple_concurrency', $concurrency, $requests ); + $concurrency = max( 1, $concurrency ); + + foreach ( array_chunk( $pending, $concurrency, true ) as $batch ) { + $batch_requests = array(); + + foreach ( $batch as $id => $prepared ) { + $batch_requests[ $id ] = array( + 'url' => $prepared['url'], + 'headers' => $prepared['headers'], + 'data' => $prepared['data'], + 'type' => $prepared['type'], + 'options' => $prepared['options'], + ); + } + + // Avoid issues where mbstring.func_overload is enabled. + mbstring_binary_safe_encoding(); + + try { + $results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); + } catch ( WpOrg\Requests\Exception $e ) { + /* + * The batch could not be sent at all, for example because one of the URLs + * is not an HTTP(S) URL. Send its requests one by one instead, so that each + * of them gets its own response or error. + */ + $results = array(); + + foreach ( $batch_requests as $id => $request ) { + try { + $results[ $id ] = WpOrg\Requests\Requests::request( + $request['url'], + $request['headers'], + $request['data'], + $request['type'], + $request['options'] + ); + } catch ( WpOrg\Requests\Exception $e ) { + $results[ $id ] = $e; + } + } + } + + reset_mbstring_encoding(); + + foreach ( $batch as $id => $prepared ) { + if ( isset( $results[ $id ] ) ) { + $result = $results[ $id ]; + } else { + $result = new WP_Error( 'http_request_failed', __( 'No response was received.' ) ); + } + + $responses[ $id ] = $this->finalize_response( $result, $prepared['args'], $prepared['url'] ); + } + } + } + + // Return the responses in the order the requests were given. + $ordered = array(); + + foreach ( $requests as $id => $request ) { + $ordered[ $id ] = $responses[ $id ]; + } + + return $ordered; + } + + /** + * Prepares a request for the Requests library. + * + * Parses and filters the request arguments, lets the 'pre_http_request' filter + * short-circuit the request, validates the URL, checks whether the request is + * blocked, and translates the arguments into options for the Requests library. + * + * @since 7.2.0 + * + * @param string $url The request URL. + * @param string|array $args Optional. Request arguments. See WP_Http::request(). + * @return array|WP_Error { + * The prepared request, or a WP_Error if the request cannot be sent. If the request was + * short-circuited by the 'pre_http_request' filter, only the 'response' key is present. + * + * @type string $url The validated request URL. + * @type array $headers Request headers. + * @type mixed $data Request body. + * @type string $type Request method. + * @type array $options Options for the Requests library. + * @type array $args The parsed request arguments, after the 'http_request_args' filter. + * @type array|WP_Error $response The response returned by the 'pre_http_request' filter, if any. + * } + */ + protected function prepare_request( $url, $args = array() ) { $defaults = array( 'method' => 'GET', /** @@ -278,7 +470,9 @@ public function request( $url, $args = array() ) { $pre = apply_filters( 'pre_http_request', false, $parsed_args, $url ); if ( false !== $pre ) { - return $pre; + return array( + 'response' => $pre, + ); } if ( function_exists( 'wp_kses_bad_protocol' ) ) { @@ -410,24 +604,44 @@ public function request( $url, $args = array() ) { } } - // Avoid issues where mbstring.func_overload is enabled. - mbstring_binary_safe_encoding(); + return array( + 'url' => $url, + 'headers' => $headers, + 'data' => $data, + 'type' => $type, + 'options' => $options, + 'args' => $parsed_args, + ); + } - try { - $requests_response = WpOrg\Requests\Requests::request( $url, $headers, $data, $type, $options ); + /** + * Turns the result of a request into a WP_Http response array. + * + * Fires the 'http_api_debug' action and applies the 'http_response' filter. + * + * @since 7.2.0 + * + * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the + * error the request failed with. + * @param array $parsed_args The parsed request arguments, + * as returned by WP_Http::prepare_request(). + * @param string $url The request URL. + * @return array|WP_Error The response array, or a WP_Error on failure. See WP_Http::request(). + */ + protected function finalize_response( $response, $parsed_args, $url ) { + if ( $response instanceof WpOrg\Requests\Exception ) { + $response = new WP_Error( 'http_request_failed', $response->getMessage() ); + } + if ( ! is_wp_error( $response ) ) { // Convert the response into an array. - $http_response = new WP_HTTP_Requests_Response( $requests_response, $parsed_args['filename'] ); + $http_response = new WP_HTTP_Requests_Response( $response, $parsed_args['filename'] ); $response = $http_response->to_array(); // Add the original object to the array. $response['http_response'] = $http_response; - } catch ( WpOrg\Requests\Exception $e ) { - $response = new WP_Error( 'http_request_failed', $e->getMessage() ); } - reset_mbstring_encoding(); - /** * Fires after an HTTP API response is received and before the response is returned. * diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index c2855a8d8d9c1..9668604fd26db 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -142,6 +142,38 @@ function wp_safe_remote_head( $url, $args = array() ) { return $http->head( $url, $args ); } +/** + * Performs multiple HTTP requests concurrently using the safe variant of the HTTP API. + * + * Like wp_remote_request_multiple(), but every URL is validated to avoid redirection + * and request forgery attacks. See wp_safe_remote_request(). + * + * @since 7.2.0 + * + * @see wp_remote_request_multiple() For information on the request and response format. + * + * @param array $requests Requests to send. See wp_remote_request_multiple(). + * @param array $options Optional. Options that apply to the batch of requests. Default empty array. + * See WP_Http::request_multiple(). + * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. + */ +function wp_safe_remote_request_multiple( $requests, $options = array() ) { + foreach ( $requests as $id => $request ) { + if ( ! is_array( $request ) ) { + $request = array( 'url' => $request ); + } + + $request['args'] = isset( $request['args'] ) ? wp_parse_args( $request['args'] ) : array(); + + $request['args']['reject_unsafe_urls'] = true; + + $requests[ $id ] = $request; + } + + $http = _wp_http_get_object(); + return $http->request_multiple( $requests, $options ); +} + /** * Performs an HTTP request and returns its response. * @@ -231,6 +263,33 @@ function wp_remote_head( $url, $args = array() ) { return $http->head( $url, $args ); } +/** + * Performs multiple HTTP requests concurrently and returns their responses. + * + * The requests are sent side by side rather than one after the other, so a batch of + * requests takes about as long as its slowest request instead of the sum of all of them. + * + * Each request is filtered, and its response processed, individually and exactly as + * if it had been sent with wp_remote_request(). + * + * @since 7.2.0 + * + * @see WP_Http::request_multiple() For information on the request format and the batch options. + * @see WP_Http::request() For information on accepted request arguments and the response format. + * + * @param array $requests Requests to send, keyed by an identifier of the caller's choosing. + * Each request is either a URL string, for a GET request with the + * default arguments, or an array with a 'url' key and an optional + * 'args' key holding the request arguments. + * @param array $options Optional. Options that apply to the batch of requests, such as + * 'concurrency'. Default empty array. See WP_Http::request_multiple(). + * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. + */ +function wp_remote_request_multiple( $requests, $options = array() ) { + $http = _wp_http_get_object(); + return $http->request_multiple( $requests, $options ); +} + /** * Retrieves only the headers from the raw response. * diff --git a/tests/phpunit/tests/http/wpRemoteRequestMultiple.php b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php new file mode 100644 index 0000000000000..383b8d8414a33 --- /dev/null +++ b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php @@ -0,0 +1,478 @@ +assertSame( array(), wp_remote_request_multiple( array() ) ); + } + + /** + * @ticket 37459 + */ + public function test_should_key_and_order_the_responses_like_the_requests() { + $this->allow_requests_rejected_before_sending(); + add_filter( 'pre_http_request', array( $this, 'short_circuit_example_org' ), 10, 3 ); + + $responses = wp_remote_request_multiple( + array( + 'first' => 'https://example.org/first', + // This request is not short-circuited and is answered after the others, but must keep its place. + 'second' => 'ssl://example.org/second', + 7 => array( 'url' => 'https://example.org/third' ), + 'fourth' => array( + 'url' => 'https://example.org/fourth', + 'args' => array( 'method' => 'POST' ), + ), + ) + ); + + $this->assertSame( array( 'first', 'second', 7, 'fourth' ), array_keys( $responses ), 'The responses should be keyed and ordered like the requests.' ); + $this->assertSame( 'https://example.org/first', wp_remote_retrieve_response_message( $responses['first'] ) ); + $this->assertWPError( $responses['second'] ); + $this->assertSame( 'https://example.org/third', wp_remote_retrieve_response_message( $responses[7] ) ); + $this->assertSame( 'https://example.org/fourth', wp_remote_retrieve_response_message( $responses['fourth'] ) ); + } + + /** + * @ticket 37459 + */ + public function test_should_filter_each_request_individually() { + $methods = array(); + + add_filter( + 'http_request_args', + static function ( $parsed_args, $url ) use ( &$methods ) { + $methods[ $url ] = $parsed_args['method']; + return $parsed_args; + }, + 10, + 2 + ); + add_filter( 'pre_http_request', array( $this, 'short_circuit_example_org' ), 10, 3 ); + + wp_remote_request_multiple( + array( + 'https://example.org/get', + array( + 'url' => 'https://example.org/post', + 'args' => array( 'method' => 'POST' ), + ), + array( + 'url' => 'https://example.org/head', + 'args' => 'method=HEAD', + ), + ) + ); + + $this->assertSame( + array( + 'https://example.org/get' => 'GET', + 'https://example.org/post' => 'POST', + 'https://example.org/head' => 'HEAD', + ), + $methods + ); + } + + /** + * @ticket 37459 + */ + public function test_should_perform_each_request_in_full() { + $blocking = null; + + add_filter( + 'pre_http_request', + static function ( $response, $parsed_args ) use ( &$blocking ) { + $blocking = $parsed_args['blocking']; + + return array( + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + ); + }, + 10, + 2 + ); + + wp_remote_request_multiple( + array( + array( + 'url' => 'https://example.org/', + 'args' => array( 'blocking' => false ), + ), + ) + ); + + $this->assertTrue( $blocking, 'Batched requests should always be blocking.' ); + } + + /** + * @ticket 37459 + */ + public function test_should_return_a_wp_error_for_a_request_that_cannot_be_prepared() { + $this->allow_requests_rejected_before_sending(); + add_filter( 'pre_http_request', array( $this, 'short_circuit_example_org' ), 10, 3 ); + + $responses = wp_remote_request_multiple( + array( + 'valid' => 'https://example.org/', + 'invalid' => 'not a url', + 'missing' => array( 'args' => array( 'method' => 'POST' ) ), + ) + ); + + $this->assertSame( 200, wp_remote_retrieve_response_code( $responses['valid'] ) ); + $this->assertWPError( $responses['invalid'] ); + $this->assertSame( 'http_request_failed', $responses['invalid']->get_error_code() ); + $this->assertWPError( $responses['missing'] ); + $this->assertSame( 'http_request_failed', $responses['missing']->get_error_code() ); + } + + /** + * @ticket 37459 + */ + public function test_should_return_a_wp_error_for_a_request_the_requests_library_rejects() { + $this->allow_requests_rejected_before_sending(); + + $responses = wp_remote_request_multiple( array( 'ssl' => 'ssl://example.org/' ) ); + + $this->assertWPError( $responses['ssl'] ); + $this->assertSame( 'http_request_failed', $responses['ssl']->get_error_code() ); + $this->assertSame( 'Only HTTP(S) requests are handled.', $responses['ssl']->get_error_message() ); + } + + /** + * @ticket 37459 + */ + public function test_safe_variant_should_reject_unsafe_urls_for_every_request() { + $reject_unsafe_urls = array(); + + add_filter( + 'http_request_args', + static function ( $parsed_args, $url ) use ( &$reject_unsafe_urls ) { + $reject_unsafe_urls[ $url ] = $parsed_args['reject_unsafe_urls']; + return $parsed_args; + }, + 10, + 2 + ); + add_filter( 'pre_http_request', array( $this, 'short_circuit_example_org' ), 10, 3 ); + + $responses = wp_safe_remote_request_multiple( + array( + 'https://example.org/string', + array( 'url' => 'https://example.org/no-args' ), + array( + 'url' => 'https://example.org/string-args', + 'args' => 'method=HEAD', + ), + array( + 'url' => 'https://example.org/array-args', + 'args' => array( 'reject_unsafe_urls' => false ), + ), + ) + ); + + $this->assertSame( + array( + 'https://example.org/string' => true, + 'https://example.org/no-args' => true, + 'https://example.org/string-args' => true, + 'https://example.org/array-args' => true, + ), + $reject_unsafe_urls + ); + + foreach ( $responses as $response ) { + $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); + } + } + + /** + * @ticket 37459 + * @group external-http + */ + public function test_should_send_the_requests_and_return_their_responses() { + $file = $this->temp_filename(); + + $responses = wp_remote_request_multiple( + array( + 'redirected' => array( + 'url' => self::REDIRECTION_SCRIPT . '?code=301&rt=2', + 'args' => array( 'redirection' => 5 ), + ), + 'not_followed' => array( + 'url' => self::REDIRECTION_SCRIPT . '?code=302&rt=1', + 'args' => array( 'redirection' => 0 ), + ), + 'head' => array( + 'url' => self::FILE_URL, + 'args' => array( 'method' => 'HEAD' ), + ), + 'stream' => array( + 'url' => self::FILE_URL, + 'args' => array( + 'stream' => true, + 'filename' => $file, + 'timeout' => 30, + ), + ), + ) + ); + + foreach ( $responses as $response ) { + $this->skipTestOnTimeout( $response ); + } + + $this->assertSame( array( 'redirected', 'not_followed', 'head', 'stream' ), array_keys( $responses ) ); + + $this->assertNotWPError( $responses['redirected'] ); + $this->assertSame( 200, wp_remote_retrieve_response_code( $responses['redirected'] ) ); + + $this->assertNotWPError( $responses['not_followed'] ); + $this->assertSame( 302, wp_remote_retrieve_response_code( $responses['not_followed'] ) ); + + $this->assertNotWPError( $responses['head'] ); + $this->assertSame( 200, wp_remote_retrieve_response_code( $responses['head'] ) ); + $this->assertSame( 'image/png', wp_remote_retrieve_header( $responses['head'], 'Content-Type' ) ); + $this->assertSame( '', wp_remote_retrieve_body( $responses['head'] ) ); + + $this->assertNotWPError( $responses['stream'] ); + $this->assertSame( 200, wp_remote_retrieve_response_code( $responses['stream'] ) ); + $this->assertSame( $file, $responses['stream']['filename'] ); + $this->assertSame( 153204, filesize( $file ) ); + + unlink( $file ); + } + + /** + * @ticket 37459 + * @group external-http + */ + public function test_should_process_each_response_individually() { + $filtered = array(); + $debugged = array(); + + add_filter( + 'http_response', + static function ( $response, $parsed_args, $url ) use ( &$filtered ) { + $filtered[] = $url; + return $response; + }, + 10, + 3 + ); + add_action( + 'http_api_debug', + static function ( $response, $context, $transport, $parsed_args, $url ) use ( &$debugged ) { + $debugged[] = $url; + }, + 10, + 5 + ); + + $requests = $this->get_head_requests( 2 ); + $responses = wp_remote_request_multiple( $requests ); + + foreach ( $responses as $response ) { + $this->skipTestOnTimeout( $response ); + } + + $urls = wp_list_pluck( $requests, 'url' ); + + $this->assertSame( $urls, $filtered, "The 'http_response' filter should be applied to each response." ); + $this->assertSame( $urls, $debugged, "The 'http_api_debug' action should fire for each response." ); + } + + /** + * @ticket 37459 + * @group external-http + */ + public function test_should_send_the_other_requests_when_one_cannot_be_batched() { + $responses = wp_remote_request_multiple( + array( + 'ssl' => 'ssl://example.org/', + 'head' => array( + 'url' => self::FILE_URL, + 'args' => array( 'method' => 'HEAD' ), + ), + ) + ); + + $this->skipTestOnTimeout( $responses['head'] ); + + $this->assertWPError( $responses['ssl'] ); + $this->assertNotWPError( $responses['head'] ); + $this->assertSame( 200, wp_remote_retrieve_response_code( $responses['head'] ) ); + } + + /** + * @ticket 37459 + * @group external-http + * + * @covers WP_HTTP_Requests_Hooks::dispatch + */ + public function test_should_fire_http_api_curl_for_each_batched_request() { + $this->skip_without_curl(); + + $count = 0; + + add_action( + 'http_api_curl', + static function () use ( &$count ) { + ++$count; + } + ); + + $responses = wp_remote_request_multiple( $this->get_head_requests( 2 ) ); + + foreach ( $responses as $response ) { + $this->skipTestOnTimeout( $response ); + } + + $this->assertSame( 2, $count ); + } + + /** + * @ticket 37459 + * @group external-http + */ + public function test_should_send_at_most_concurrency_requests_at_once() { + $this->skip_without_curl(); + + $batches = 0; + + add_action( + 'requests-curl.before_multi_exec', + static function () use ( &$batches ) { + ++$batches; + } + ); + + $responses = wp_remote_request_multiple( $this->get_head_requests( 3 ), array( 'concurrency' => 2 ) ); + + foreach ( $responses as $response ) { + $this->skipTestOnTimeout( $response ); + } + + $this->assertSame( 2, $batches ); + } + + /** + * @ticket 37459 + * @group external-http + */ + public function test_should_apply_the_concurrency_filter() { + $this->skip_without_curl(); + + $batches = 0; + + add_action( + 'requests-curl.before_multi_exec', + static function () use ( &$batches ) { + ++$batches; + } + ); + add_filter( + 'http_request_multiple_concurrency', + static function () { + return 1; + } + ); + + $responses = wp_remote_request_multiple( $this->get_head_requests( 3 ) ); + + foreach ( $responses as $response ) { + $this->skipTestOnTimeout( $response ); + } + + $this->assertSame( 3, $batches ); + } + + /** + * Short-circuits requests to example.org with a 200 response whose message is the request URL. + * + * @param false|array|WP_Error $response A preemptive return value of an HTTP request. + * @param array $parsed_args HTTP request arguments. + * @param string $url The request URL. + * @return false|array|WP_Error The short-circuited response for example.org, $response otherwise. + */ + public function short_circuit_example_org( $response, $parsed_args, $url ) { + if ( 0 !== strpos( $url, 'https://example.org/' ) ) { + return $response; + } + + return array( + 'headers' => array(), + 'body' => '', + 'response' => array( + 'code' => 200, + 'message' => $url, + ), + 'cookies' => array(), + 'filename' => null, + ); + } + + /** + * Returns HEAD requests for distinct URLs that are served with a direct 200 response. + * + * @param int $count Number of requests. + * @return array Requests, see wp_remote_request_multiple(). + */ + private function get_head_requests( $count ) { + $requests = array(); + + for ( $i = 1; $i <= $count; $i++ ) { + $requests[] = array( + 'url' => self::FILE_URL . '?' . $i, + 'args' => array( 'method' => 'HEAD' ), + ); + } + + return $requests; + } + + /** + * Lets requests reach WP_Http, for tests whose URLs are rejected before anything is sent. + * + * The test suite blocks external requests on the 'pre_http_request' filter, which runs + * before WP_Http validates the URL. The tests calling this only use URLs that WP_Http or + * the Requests library reject before a transport is used, so no request leaves the process. + */ + private function allow_requests_rejected_before_sending() { + remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX ); + } + + /** + * Skips the current test when the cURL transport of the Requests library is not available. + */ + private function skip_without_curl() { + if ( ! WpOrg\Requests\Transport\Curl::test() ) { + $this->markTestSkipped( 'This test requires the cURL transport.' ); + } + } +} From 077ed59d4c062bcdc81772adb444f48397fce147 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 22 Sep 2026 12:30:57 +0000 Subject: [PATCH 02/10] HTTP API: Address PHPStan findings in `WP_Http::request_multiple()`. * Catch `Exception` around `Requests::request_multiple()`. The Requests library only documents `InvalidArgument` on that method, although the URL validation and transport lookup it performs throw `WpOrg\Requests\Exception`, so PHPStan considered the narrower catch dead. * Document the two shapes `WP_Http::prepare_request()` returns with a `@phpstan-return`, as hash notation cannot express a union of shapes. * Describe the `$requests` parameter in prose. Its hash notation described a single request rather than the list of them. See #37459. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01RvSBA4zz2vHphxunmJCV2G --- src/wp-includes/class-wp-http.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 1a0c73f2deb06..ed56e5530087a 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -214,13 +214,11 @@ public function request( $url, $args = array() ) { * * @see WP_Http::request() For the accepted request arguments and the response format. * - * @param array $requests { - * Requests to send, keyed by an identifier of the caller's choosing. Each request is - * either a URL string, for a GET request with the default arguments, or an array: - * - * @type string $url The request URL. - * @type string|array $args Optional. Request arguments. See WP_Http::request(). - * } + * @param array $requests Requests to send, keyed by an identifier of the caller's choosing. + * Each request is either a URL string, for a GET request with the + * default arguments, or an array with the request URL under 'url' + * and, optionally, the request arguments under 'args'. + * See WP_Http::request() for the accepted request arguments. * @param array $options { * Optional. Options that apply to the batch of requests. * @@ -288,11 +286,11 @@ public function request_multiple( array $requests, array $options = array() ) { try { $results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); - } catch ( WpOrg\Requests\Exception $e ) { + } catch ( Exception $e ) { /* - * The batch could not be sent at all, for example because one of the URLs - * is not an HTTP(S) URL. Send its requests one by one instead, so that each - * of them gets its own response or error. + * The batch could not be sent at all, for example because the Requests + * library rejected one of the URLs while building it. Send its requests + * one by one instead, so that each of them gets its own response or error. */ $results = array(); @@ -356,8 +354,9 @@ public function request_multiple( array $requests, array $options = array() ) { * @type string $type Request method. * @type array $options Options for the Requests library. * @type array $args The parsed request arguments, after the 'http_request_args' filter. - * @type array|WP_Error $response The response returned by the 'pre_http_request' filter, if any. + * @type array|WP_Error $response Optional. The response returned by the 'pre_http_request' filter. * } + * @phpstan-return array{url: string, headers: array, data: mixed, type: string, options: array, args: array}|array{response: array|WP_Error}|WP_Error */ protected function prepare_request( $url, $args = array() ) { $defaults = array( From 40803fc110e17ea1dd2459b8075fae462bebfe3a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 22 Sep 2026 12:36:06 +0000 Subject: [PATCH 03/10] HTTP API: Return a null `pre_http_request` value as is. `WP_Http::prepare_request()` signals a short-circuited request with the presence of the `response` key, but the callers checked it with `isset()`, which does not see a `null` value. A `pre_http_request` callback returning `null` therefore made `WP_Http::request()` read undefined keys and `WP_Http::request_multiple()` send a request without a URL, whereas `null` used to be returned to the caller unchanged. Check for the key with `array_key_exists()` instead, and add a test for it. See #37459. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED --- src/wp-includes/class-wp-http.php | 4 ++-- tests/phpunit/tests/http/wpRemoteRequestMultiple.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index ed56e5530087a..a3984c71c2413 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -175,7 +175,7 @@ public function request( $url, $args = array() ) { } // The request was short-circuited by the 'pre_http_request' filter. - if ( isset( $prepared['response'] ) ) { + if ( array_key_exists( 'response', $prepared ) ) { return $prepared['response']; } @@ -246,7 +246,7 @@ public function request_multiple( array $requests, array $options = array() ) { if ( is_wp_error( $prepared ) ) { $responses[ $id ] = $prepared; - } elseif ( isset( $prepared['response'] ) ) { + } elseif ( array_key_exists( 'response', $prepared ) ) { // The request was short-circuited by the 'pre_http_request' filter. $responses[ $id ] = $prepared['response']; } else { diff --git a/tests/phpunit/tests/http/wpRemoteRequestMultiple.php b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php index 383b8d8414a33..29b48d26cb126 100644 --- a/tests/phpunit/tests/http/wpRemoteRequestMultiple.php +++ b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php @@ -129,6 +129,18 @@ static function ( $response, $parsed_args ) use ( &$blocking ) { $this->assertTrue( $blocking, 'Batched requests should always be blocking.' ); } + /** + * @ticket 37459 + */ + public function test_should_return_a_null_short_circuit_value_as_is() { + add_filter( 'pre_http_request', '__return_null' ); + + $responses = wp_remote_request_multiple( array( 'first' => 'https://example.org/first' ) ); + + $this->assertSame( array( 'first' => null ), $responses, 'A null short-circuit value should be returned like any other.' ); + $this->assertNull( wp_remote_request( 'https://example.org/first' ), 'A single request should still return a null short-circuit value as is.' ); + } + /** * @ticket 37459 */ From b9fbfe5891efcfcf3723baae9aba8691418d96a9 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 25 Sep 2026 20:39:50 +0000 Subject: [PATCH 04/10] HTTP API: Reference `WP_Http::request_multiple()` with `{@see}` in the `http_api_curl` docblock. See #37459. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED --- src/wp-includes/class-wp-http-curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-http-curl.php b/src/wp-includes/class-wp-http-curl.php index d793fb7aed6a5..d7aef05849535 100644 --- a/src/wp-includes/class-wp-http-curl.php +++ b/src/wp-includes/class-wp-http-curl.php @@ -228,7 +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 WP_Http::request_multiple(). + * @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. From 000e9a1270475c12ae7921e59e7c90189a1f2149 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 25 Sep 2026 20:52:40 +0000 Subject: [PATCH 05/10] HTTP API: Type the batch request API for static analysis. * Declare the response array as a `Response` type on `WP_Http` and use it for the return value of `request()`, `finalize_response()` and the short-circuit value `prepare_request()` returns, rather than describing the shape in each place. * Declare the prepared request as a `Prepared_Request` type and give `request_multiple()`, `wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()` value types for their requests, options and responses. * Type the results the Requests library returns for a batch. * Only parse request arguments given as a string, since arrays are passed on as they are. * Align the parameter descriptions of `finalize_response()` and reference methods with `{@see}`. See #37459. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED --- src/wp-includes/class-wp-http.php | 51 +++++++++++++++++++++++-------- src/wp-includes/http.php | 31 ++++++++++++++----- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index a3984c71c2413..daf7dd606f314 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -29,6 +29,23 @@ * Debugging includes several actions, which pass different variables for debugging the HTTP API. * * @since 2.7.0 + * + * @phpstan-type Response array{ + * headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array, + * body: string, + * response: array{ code: int|false, message: string|false }, + * cookies: WP_Http_Cookie[], + * filename?: string|null, + * http_response: WP_HTTP_Requests_Response|null, + * } + * @phpstan-type Prepared_Request array{ + * url: string, + * headers: array, + * data: mixed, + * type: string, + * options: array, + * args: array, + * } */ #[AllowDynamicProperties] class WP_Http { @@ -166,6 +183,7 @@ class WP_Http { * @type string|null $filename Optional. Filename of the response. * @type WP_HTTP_Requests_Response|null $http_response Response object. * } + * @phpstan-return Response|WP_Error */ public function request( $url, $args = array() ) { $prepared = $this->prepare_request( $url, $args ); @@ -218,14 +236,16 @@ public function request( $url, $args = array() ) { * Each request is either a URL string, for a GET request with the * default arguments, or an array with the request URL under 'url' * and, optionally, the request arguments under 'args'. - * See WP_Http::request() for the accepted request arguments. + * See {@see WP_Http::request()} for the accepted request arguments. * @param array $options { * Optional. Options that apply to the batch of requests. * * @type int $concurrency Maximum number of requests to send at once. Default 6. * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error - * on failure. See WP_Http::request() for the response format. + * on failure. See {@see WP_Http::request()} for the response format. + * @phpstan-param array }> $requests + * @phpstan-return array */ public function request_multiple( array $requests, array $options = array() ) { $responses = array(); @@ -237,7 +257,11 @@ public function request_multiple( array $requests, array $options = array() ) { } $url = isset( $request['url'] ) ? $request['url'] : ''; - $args = isset( $request['args'] ) ? wp_parse_args( $request['args'] ) : array(); + $args = isset( $request['args'] ) ? $request['args'] : array(); + + if ( ! is_array( $args ) ) { + $args = wp_parse_args( $args ); + } // Batches cannot send non-blocking requests, each request is performed in full. $args['blocking'] = true; @@ -285,6 +309,7 @@ public function request_multiple( array $requests, array $options = array() ) { mbstring_binary_safe_encoding(); try { + /** @var array $results */ $results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); } catch ( Exception $e ) { /* @@ -342,8 +367,8 @@ public function request_multiple( array $requests, array $options = array() ) { * * @since 7.2.0 * - * @param string $url The request URL. - * @param string|array $args Optional. Request arguments. See WP_Http::request(). + * @param string $url The request URL. + * @param string|array $args Optional. Request arguments. See {@see WP_Http::request()}. * @return array|WP_Error { * The prepared request, or a WP_Error if the request cannot be sent. If the request was * short-circuited by the 'pre_http_request' filter, only the 'response' key is present. @@ -356,7 +381,8 @@ public function request_multiple( array $requests, array $options = array() ) { * @type array $args The parsed request arguments, after the 'http_request_args' filter. * @type array|WP_Error $response Optional. The response returned by the 'pre_http_request' filter. * } - * @phpstan-return array{url: string, headers: array, data: mixed, type: string, options: array, args: array}|array{response: array|WP_Error}|WP_Error + * @phpstan-param string|array $args + * @phpstan-return Prepared_Request|array{ response: Response|WP_Error }|WP_Error */ protected function prepare_request( $url, $args = array() ) { $defaults = array( @@ -620,12 +646,13 @@ protected function prepare_request( $url, $args = array() ) { * * @since 7.2.0 * - * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the - * error the request failed with. - * @param array $parsed_args The parsed request arguments, - * as returned by WP_Http::prepare_request(). - * @param string $url The request URL. - * @return array|WP_Error The response array, or a WP_Error on failure. See WP_Http::request(). + * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the error the + * request failed with. + * @param array $parsed_args The parsed request arguments, as + * returned by {@see WP_Http::prepare_request()}. + * @param string $url The request URL. + * @return array|WP_Error The response array, or a WP_Error on failure. See {@see WP_Http::request()}. + * @phpstan-return Response|WP_Error */ protected function finalize_response( $response, $parsed_args, $url ) { if ( $response instanceof WpOrg\Requests\Exception ) { diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index 9668604fd26db..faecedafc0abe 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -152,10 +152,16 @@ function wp_safe_remote_head( $url, $args = array() ) { * * @see wp_remote_request_multiple() For information on the request and response format. * - * @param array $requests Requests to send. See wp_remote_request_multiple(). - * @param array $options Optional. Options that apply to the batch of requests. Default empty array. - * See WP_Http::request_multiple(). + * @param array $requests Requests to send. See {@see wp_remote_request_multiple()}. + * @param array $options { + * Optional. Options that apply to the batch of requests. See {@see WP_Http::request_multiple()}. + * Default empty array. + * + * @type int $concurrency Maximum number of requests to send at once. Default 6. + * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. + * @phpstan-param array }> $requests + * @phpstan-return array|WP_Error> */ function wp_safe_remote_request_multiple( $requests, $options = array() ) { foreach ( $requests as $id => $request ) { @@ -163,9 +169,14 @@ function wp_safe_remote_request_multiple( $requests, $options = array() ) { $request = array( 'url' => $request ); } - $request['args'] = isset( $request['args'] ) ? wp_parse_args( $request['args'] ) : array(); + $args = isset( $request['args'] ) ? $request['args'] : array(); + + if ( ! is_array( $args ) ) { + $args = wp_parse_args( $args ); + } - $request['args']['reject_unsafe_urls'] = true; + $args['reject_unsafe_urls'] = true; + $request['args'] = $args; $requests[ $id ] = $request; } @@ -281,9 +292,15 @@ function wp_remote_head( $url, $args = array() ) { * Each request is either a URL string, for a GET request with the * default arguments, or an array with a 'url' key and an optional * 'args' key holding the request arguments. - * @param array $options Optional. Options that apply to the batch of requests, such as - * 'concurrency'. Default empty array. See WP_Http::request_multiple(). + * @param array $options { + * Optional. Options that apply to the batch of requests. See {@see WP_Http::request_multiple()}. + * Default empty array. + * + * @type int $concurrency Maximum number of requests to send at once. Default 6. + * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. + * @phpstan-param array }> $requests + * @phpstan-return array|WP_Error> */ function wp_remote_request_multiple( $requests, $options = array() ) { $http = _wp_http_get_object(); From f15403f62d8e63d073d116b2b096f4e1e022edae Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:57:02 +0000 Subject: [PATCH 06/10] HTTP API: Fix PHPStan return type errors in WP_Http::prepare_request(). Type the value of the 'pre_http_request' filter as a response, as documented, so that a short-circuited request matches the method's return type. Loosen the Prepared_Request shape to what prepare_request() actually guarantees, since its values come from the filtered request arguments. Co-authored-by: Pascal Birchler --- src/wp-includes/class-wp-http.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 62f8185541a28..288cf38501c78 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -40,11 +40,11 @@ * } * @phpstan-type Prepared_Request array{ * url: string, - * headers: array, + * headers: array, * data: mixed, - * type: string, + * type: mixed, * options: array, - * args: array, + * args: array, * } */ #[AllowDynamicProperties] @@ -491,6 +491,7 @@ protected function prepare_request( $url, $args = array() ) { * @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false. * @param array $parsed_args HTTP request arguments. * @param string $url The request URL. + * @phpstan-param false|Response|WP_Error $response */ $pre = apply_filters( 'pre_http_request', false, $parsed_args, $url ); @@ -648,7 +649,7 @@ protected function prepare_request( $url, $args = array() ) { * * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the error the * request failed with. - * @param array $parsed_args The parsed request arguments, as + * @param array $parsed_args The parsed request arguments, as * returned by {@see WP_Http::prepare_request()}. * @param string $url The request URL. * @return array|WP_Error The response array, or a WP_Error on failure. See {@see WP_Http::request()}. From bf090db40fa496d09dd843386d8c008f306dc195 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 22:06:21 +0000 Subject: [PATCH 07/10] HTTP API: Address review feedback on concurrent requests. Update the `@since` tags for the new functions, methods, filter, and changelog entry to 7.3.0. Add native parameter and return types to the new functions and methods. WP_Http::prepare_request() now requires an array of arguments, so WP_Http::request() parses string arguments before passing them on, as WP_Http::request_multiple() already does. Its $url parameter stays untyped, so a URL that is not a string still produces a WP_Error from WP_Http::request() rather than a TypeError. Turn the comment about 'curl.before_multi_add' into a docblock that links to WP_Http::request_multiple(). Co-authored-by: Pascal Birchler --- src/wp-includes/class-wp-http-curl.php | 2 +- .../class-wp-http-requests-hooks.php | 4 +-- src/wp-includes/class-wp-http.php | 27 ++++++++++--------- src/wp-includes/http.php | 8 +++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/class-wp-http-curl.php b/src/wp-includes/class-wp-http-curl.php index d7aef05849535..cb6a9ac6056c4 100644 --- a/src/wp-includes/class-wp-http-curl.php +++ b/src/wp-includes/class-wp-http-curl.php @@ -228,7 +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()}. + * @since 7.3.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. diff --git a/src/wp-includes/class-wp-http-requests-hooks.php b/src/wp-includes/class-wp-http-requests-hooks.php index 16bcd31a44bff..be6341ec27ebb 100644 --- a/src/wp-includes/class-wp-http-requests-hooks.php +++ b/src/wp-includes/class-wp-http-requests-hooks.php @@ -53,9 +53,9 @@ 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(). + * requests of a batch, see {@see WP_Http::request_multiple()}. */ case 'curl.before_send': case 'curl.before_multi_add': diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 288cf38501c78..bee1ed060cd38 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -186,6 +186,10 @@ class WP_Http { * @phpstan-return Response|WP_Error */ public function request( $url, $args = array() ) { + if ( ! is_array( $args ) ) { + $args = wp_parse_args( $args ); + } + $prepared = $this->prepare_request( $url, $args ); if ( is_wp_error( $prepared ) ) { @@ -228,7 +232,7 @@ public function request( $url, $args = array() ) { * * Non-blocking requests are not supported: every request is performed in full. * - * @since 7.2.0 + * @since 7.3.0 * * @see WP_Http::request() For the accepted request arguments and the response format. * @@ -247,7 +251,7 @@ public function request( $url, $args = array() ) { * @phpstan-param array }> $requests * @phpstan-return array */ - public function request_multiple( array $requests, array $options = array() ) { + public function request_multiple( array $requests, array $options = array() ): array { $responses = array(); $pending = array(); @@ -284,7 +288,7 @@ public function request_multiple( array $requests, array $options = array() ) { /** * Filters the maximum number of HTTP requests that WP_Http::request_multiple() sends at once. * - * @since 7.2.0 + * @since 7.3.0 * * @param int $concurrency Maximum number of requests to send at once. Default 6. * @param array $requests The requests to send. See WP_Http::request_multiple(). @@ -365,10 +369,10 @@ public function request_multiple( array $requests, array $options = array() ) { * short-circuit the request, validates the URL, checks whether the request is * blocked, and translates the arguments into options for the Requests library. * - * @since 7.2.0 + * @since 7.3.0 * - * @param string $url The request URL. - * @param string|array $args Optional. Request arguments. See {@see WP_Http::request()}. + * @param string $url The request URL. + * @param array $args Optional. Request arguments. See {@see WP_Http::request()}. * @return array|WP_Error { * The prepared request, or a WP_Error if the request cannot be sent. If the request was * short-circuited by the 'pre_http_request' filter, only the 'response' key is present. @@ -381,10 +385,10 @@ public function request_multiple( array $requests, array $options = array() ) { * @type array $args The parsed request arguments, after the 'http_request_args' filter. * @type array|WP_Error $response Optional. The response returned by the 'pre_http_request' filter. * } - * @phpstan-param string|array $args + * @phpstan-param array $args * @phpstan-return Prepared_Request|array{ response: Response|WP_Error }|WP_Error */ - protected function prepare_request( $url, $args = array() ) { + protected function prepare_request( $url, array $args = array() ) { $defaults = array( 'method' => 'GET', /** @@ -450,9 +454,6 @@ protected function prepare_request( $url, $args = array() ) { 'limit_response_size' => null, ); - // Pre-parse for the HEAD checks. - $args = wp_parse_args( $args ); - // By default, HEAD requests do not cause redirections. if ( isset( $args['method'] ) && 'HEAD' === $args['method'] ) { $defaults['redirection'] = 0; @@ -645,7 +646,7 @@ protected function prepare_request( $url, $args = array() ) { * * Fires the 'http_api_debug' action and applies the 'http_response' filter. * - * @since 7.2.0 + * @since 7.3.0 * * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the error the * request failed with. @@ -655,7 +656,7 @@ protected function prepare_request( $url, $args = array() ) { * @return array|WP_Error The response array, or a WP_Error on failure. See {@see WP_Http::request()}. * @phpstan-return Response|WP_Error */ - protected function finalize_response( $response, $parsed_args, $url ) { + protected function finalize_response( $response, array $parsed_args, string $url ) { if ( $response instanceof WpOrg\Requests\Exception ) { $response = new WP_Error( 'http_request_failed', $response->getMessage() ); } diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index 573b499037ea6..73689209c9138 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -148,7 +148,7 @@ function wp_safe_remote_head( $url, $args = array() ) { * Like wp_remote_request_multiple(), but every URL is validated to avoid redirection * and request forgery attacks. See wp_safe_remote_request(). * - * @since 7.2.0 + * @since 7.3.0 * * @see wp_remote_request_multiple() For information on the request and response format. * @@ -163,7 +163,7 @@ function wp_safe_remote_head( $url, $args = array() ) { * @phpstan-param array }> $requests * @phpstan-return array|WP_Error> */ -function wp_safe_remote_request_multiple( $requests, $options = array() ) { +function wp_safe_remote_request_multiple( array $requests, array $options = array() ): array { foreach ( $requests as $id => $request ) { if ( ! is_array( $request ) ) { $request = array( 'url' => $request ); @@ -283,7 +283,7 @@ function wp_remote_head( $url, $args = array() ) { * Each request is filtered, and its response processed, individually and exactly as * if it had been sent with wp_remote_request(). * - * @since 7.2.0 + * @since 7.3.0 * * @see WP_Http::request_multiple() For information on the request format and the batch options. * @see WP_Http::request() For information on accepted request arguments and the response format. @@ -302,7 +302,7 @@ function wp_remote_head( $url, $args = array() ) { * @phpstan-param array }> $requests * @phpstan-return array|WP_Error> */ -function wp_remote_request_multiple( $requests, $options = array() ) { +function wp_remote_request_multiple( array $requests, array $options = array() ): array { $http = _wp_http_get_object(); return $http->request_multiple( $requests, $options ); } From 7bc0de1e755f6f0eed9156f6eaa78962d2661859 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 22:40:13 +0000 Subject: [PATCH 08/10] HTTP API: Address further review feedback on concurrent requests. Reset the mbstring encoding in `finally` blocks, so that it is restored even when the Requests library throws an exception that is not caught. Use the null coalescing operator for optional request values and options. Simplify the PHPStan types by dropping `array-key`, which adds no information. Co-authored-by: Pascal Birchler --- src/wp-includes/class-wp-http.php | 28 ++++++++++++++-------------- src/wp-includes/http.php | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index bee1ed060cd38..715986992cc0e 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -40,11 +40,11 @@ * } * @phpstan-type Prepared_Request array{ * url: string, - * headers: array, + * headers: mixed[], * data: mixed, * type: mixed, * options: array, - * args: array, + * args: mixed[], * } */ #[AllowDynamicProperties] @@ -214,10 +214,10 @@ public function request( $url, $args = array() ) { ); } catch ( WpOrg\Requests\Exception $e ) { $response = new WP_Error( 'http_request_failed', $e->getMessage() ); + } finally { + reset_mbstring_encoding(); } - reset_mbstring_encoding(); - return $this->finalize_response( $response, $prepared['args'], $prepared['url'] ); } @@ -248,8 +248,8 @@ public function request( $url, $args = array() ) { * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error * on failure. See {@see WP_Http::request()} for the response format. - * @phpstan-param array }> $requests - * @phpstan-return array + * @phpstan-param array $requests + * @phpstan-return array */ public function request_multiple( array $requests, array $options = array() ): array { $responses = array(); @@ -260,8 +260,8 @@ public function request_multiple( array $requests, array $options = array() ): a $request = array( 'url' => $request ); } - $url = isset( $request['url'] ) ? $request['url'] : ''; - $args = isset( $request['args'] ) ? $request['args'] : array(); + $url = $request['url'] ?? ''; + $args = $request['args'] ?? array(); if ( ! is_array( $args ) ) { $args = wp_parse_args( $args ); @@ -283,7 +283,7 @@ public function request_multiple( array $requests, array $options = array() ): a } if ( ! empty( $pending ) ) { - $concurrency = isset( $options['concurrency'] ) ? (int) $options['concurrency'] : 6; + $concurrency = (int) ( $options['concurrency'] ?? 6 ); /** * Filters the maximum number of HTTP requests that WP_Http::request_multiple() sends at once. @@ -313,7 +313,7 @@ public function request_multiple( array $requests, array $options = array() ): a mbstring_binary_safe_encoding(); try { - /** @var array $results */ + /** @var array<\WpOrg\Requests\Response|\WpOrg\Requests\Exception> $results */ $results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); } catch ( Exception $e ) { /* @@ -336,10 +336,10 @@ public function request_multiple( array $requests, array $options = array() ): a $results[ $id ] = $e; } } + } finally { + reset_mbstring_encoding(); } - reset_mbstring_encoding(); - foreach ( $batch as $id => $prepared ) { if ( isset( $results[ $id ] ) ) { $result = $results[ $id ]; @@ -385,7 +385,7 @@ public function request_multiple( array $requests, array $options = array() ): a * @type array $args The parsed request arguments, after the 'http_request_args' filter. * @type array|WP_Error $response Optional. The response returned by the 'pre_http_request' filter. * } - * @phpstan-param array $args + * @phpstan-param mixed[] $args * @phpstan-return Prepared_Request|array{ response: Response|WP_Error }|WP_Error */ protected function prepare_request( $url, array $args = array() ) { @@ -650,7 +650,7 @@ protected function prepare_request( $url, array $args = array() ) { * * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response The response, or the error the * request failed with. - * @param array $parsed_args The parsed request arguments, as + * @param mixed[] $parsed_args The parsed request arguments, as * returned by {@see WP_Http::prepare_request()}. * @param string $url The request URL. * @return array|WP_Error The response array, or a WP_Error on failure. See {@see WP_Http::request()}. diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index 73689209c9138..1e16a26e081a5 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -160,8 +160,8 @@ function wp_safe_remote_head( $url, $args = array() ) { * @type int $concurrency Maximum number of requests to send at once. Default 6. * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. - * @phpstan-param array }> $requests - * @phpstan-return array|WP_Error> + * @phpstan-param array $requests + * @phpstan-return array|WP_Error> */ function wp_safe_remote_request_multiple( array $requests, array $options = array() ): array { foreach ( $requests as $id => $request ) { @@ -169,7 +169,7 @@ function wp_safe_remote_request_multiple( array $requests, array $options = arra $request = array( 'url' => $request ); } - $args = isset( $request['args'] ) ? $request['args'] : array(); + $args = $request['args'] ?? array(); if ( ! is_array( $args ) ) { $args = wp_parse_args( $args ); @@ -299,8 +299,8 @@ function wp_remote_head( $url, $args = array() ) { * @type int $concurrency Maximum number of requests to send at once. Default 6. * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. - * @phpstan-param array }> $requests - * @phpstan-return array|WP_Error> + * @phpstan-param array $requests + * @phpstan-return array|WP_Error> */ function wp_remote_request_multiple( array $requests, array $options = array() ): array { $http = _wp_http_get_object(); From d622142cd09b0e05749e551a9e1b5b22f3fb0c7d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Nov 2026 11:17:44 +0000 Subject: [PATCH 09/10] HTTP API: Do not resend batched requests when a batch fails. `WP_Http::request_multiple()` caught every exception thrown while sending a batch and then sent each request of the batch again, one by one. Such an exception can come from a callback on a hook that fires after the requests were sent, so non-idempotent requests could be sent twice, and exceptions unrelated to the Requests library were swallowed. Reject non-HTTP(S) URLs per request up front instead, with the same error that `Requests::request()` throws, and report a Requests exception thrown for the batch as the error of each of its requests without resending them. Also fire the `requests.before_request` hook for each batched request, as `Requests::request()` does for a single request but `Requests::request_multiple()` does not. See #37459. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT --- src/wp-includes/class-wp-http.php | 75 +++++++++++------- .../tests/http/wpRemoteRequestMultiple.php | 76 +++++++++++++++++++ 2 files changed, 123 insertions(+), 28 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 44b1e54a4f695..38db56efbc4e0 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -298,42 +298,61 @@ public function request_multiple( array $requests, array $options = array() ): a $concurrency = max( 1, $concurrency ); foreach ( array_chunk( $pending, $concurrency, true ) as $batch ) { + $results = array(); $batch_requests = array(); - foreach ( $batch as $id => $prepared ) { - $batch_requests[ $id ] = array( - 'url' => $prepared['url'], - 'headers' => $prepared['headers'], - 'data' => $prepared['data'], - 'type' => $prepared['type'], - 'options' => $prepared['options'], - ); - } - // Avoid issues where mbstring.func_overload is enabled. mbstring_binary_safe_encoding(); try { - /** @var array<\WpOrg\Requests\Response|\WpOrg\Requests\Exception> $results */ - $results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); - } catch ( Exception $e ) { + foreach ( $batch as $id => $prepared ) { + $request_url = $prepared['url']; + $request_headers = $prepared['headers']; + $request_data = $prepared['data']; + $request_type = $prepared['type']; + $request_options = $prepared['options']; + + /* + * Requests::request() fires this hook for a single request, but + * Requests::request_multiple() does not, so fire it here to treat + * the requests of a batch like requests sent on their own. + */ + $request_options['hooks']->dispatch( + 'requests.before_request', + array( &$request_url, &$request_headers, &$request_data, &$request_type, &$request_options ) + ); + + /* + * Requests::request_multiple() throws for the whole batch if one of its URLs + * is not an HTTP(S) URL. Reject such a request on its own instead, with the + * same error that Requests::request() would throw for it. + */ + if ( ! preg_match( '/^http(s)?:\/\//i', $request_url ) ) { + $results[ $id ] = new WpOrg\Requests\Exception( 'Only HTTP(S) requests are handled.', 'nonhttp', $request_url ); + continue; + } + + $batch_requests[ $id ] = array( + 'url' => $request_url, + 'headers' => $request_headers, + 'data' => $request_data, + 'type' => $request_type, + 'options' => $request_options, + ); + } + + if ( ! empty( $batch_requests ) ) { + /** @var array<\WpOrg\Requests\Response|\WpOrg\Requests\Exception> $batch_results */ + $batch_results = WpOrg\Requests\Requests::request_multiple( $batch_requests ); + $results = $batch_results + $results; + } + } catch ( WpOrg\Requests\Exception $e ) { /* - * The batch could not be sent at all, for example because the Requests - * library rejected one of the URLs while building it. Send its requests - * one by one instead, so that each of them gets its own response or error. + * The batch failed as a whole. Its requests may already have been sent, + * so do not send them again: report the error for each of them instead. */ - $results = array(); - - foreach ( $batch_requests as $id => $request ) { - try { - $results[ $id ] = WpOrg\Requests\Requests::request( - $request['url'], - $request['headers'], - $request['data'], - $request['type'], - $request['options'] - ); - } catch ( WpOrg\Requests\Exception $e ) { + foreach ( $batch as $id => $prepared ) { + if ( ! isset( $results[ $id ] ) ) { $results[ $id ] = $e; } } diff --git a/tests/phpunit/tests/http/wpRemoteRequestMultiple.php b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php index 29b48d26cb126..7e68683a0f7dc 100644 --- a/tests/phpunit/tests/http/wpRemoteRequestMultiple.php +++ b/tests/phpunit/tests/http/wpRemoteRequestMultiple.php @@ -319,6 +319,82 @@ static function ( $response, $context, $transport, $parsed_args, $url ) use ( &$ $this->assertSame( $urls, $debugged, "The 'http_api_debug' action should fire for each response." ); } + /** + * @ticket 37459 + * + * @covers WP_HTTP_Requests_Hooks::dispatch + */ + public function test_should_fire_the_requests_before_request_hook_for_each_batched_request() { + $this->allow_requests_rejected_before_sending(); + + $urls = array(); + + add_action( + 'requests-requests.before_request', + static function ( &$url ) use ( &$urls ) { + $urls[] = $url; + // Rewrite the URL so that the Requests library rejects it before anything is sent. + $url = str_replace( 'https://', 'ftp://', $url ); + } + ); + + $responses = wp_remote_request_multiple( + array( + 'first' => 'https://example.org/first', + 'second' => 'https://example.org/second', + ) + ); + + $this->assertSame( array( 'https://example.org/first', 'https://example.org/second' ), $urls, 'The hook should fire once for each request.' ); + + foreach ( $responses as $response ) { + $this->assertWPError( $response ); + $this->assertSame( 'Only HTTP(S) requests are handled.', $response->get_error_message() ); + } + } + + /** + * @ticket 37459 + */ + public function test_should_not_send_the_requests_again_when_the_batch_fails() { + $this->skip_without_curl(); + $this->allow_requests_rejected_before_sending(); + + $count = 0; + + add_action( + 'http_api_curl', + static function () use ( &$count ) { + ++$count; + } + ); + // Make the batch fail before the transfers start, so that nothing leaves the process. + add_action( + 'requests-curl.before_multi_exec', + static function () { + throw new WpOrg\Requests\Exception( 'Batch failed.', 'test' ); + } + ); + + $responses = wp_remote_request_multiple( + array( + 'first' => 'https://example.org/first', + 'second' => array( + 'url' => 'https://example.org/second', + 'args' => array( 'method' => 'POST' ), + ), + ) + ); + + $this->assertSame( 2, $count, 'Each request should only be prepared for sending once.' ); + + foreach ( $responses as $response ) { + $this->assertWPError( $response ); + $this->assertSame( 'http_request_failed', $response->get_error_code() ); + $this->assertSame( 'Batch failed.', $response->get_error_message() ); + } + } + /** * @ticket 37459 * @group external-http From 0b9eda57e05a5fb668936d6ad1bdb0bb98a9f5ed Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Nov 2026 20:47:49 +0000 Subject: [PATCH 10/10] HTTP API: Narrow PHPStan types for concurrent requests. Add `HTTP_Request_Args` and `Parsed_HTTP_Request_Args` PHPStan types and use them for the request arguments of `WP_Http::request()`, `WP_Http::request_multiple()`, `WP_Http::prepare_request()` and `WP_Http::finalize_response()`. Match the return types of `wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()` to `WP_Http::request_multiple()`, and document the concurrency passed to the `http_request_multiple_concurrency` filter as a positive integer, clamping it before the filter as well as after. See #37459. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT --- src/wp-includes/class-wp-http.php | 57 +++++++++++++++++++++++++++---- src/wp-includes/http.php | 18 ++++++++-- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 38db56efbc4e0..681b501a37aff 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -38,13 +38,54 @@ * filename?: string|null, * http_response?: WP_HTTP_Requests_Response|null, * } + * @phpstan-type HTTP_Request_Args array{ + * method?: string, + * timeout?: float|int, + * redirection?: int, + * httpversion?: string, + * 'user-agent'?: string, + * reject_unsafe_urls?: bool, + * blocking?: bool, + * headers?: string|mixed[]|null, + * cookies?: mixed[], + * body?: string|mixed[]|null, + * compress?: bool, + * decompress?: bool, + * sslverify?: bool, + * sslcertificates?: string, + * stream?: bool, + * filename?: string|null, + * limit_response_size?: int|null, + * ... + * } + * @phpstan-type Parsed_HTTP_Request_Args array{ + * method: string, + * timeout: float|int, + * redirection: int, + * httpversion: string, + * 'user-agent': string, + * reject_unsafe_urls: bool, + * blocking: bool, + * headers: string|mixed[]|null, + * cookies: mixed[], + * body: string|mixed[]|null, + * compress: bool, + * decompress: bool, + * sslverify: bool, + * sslcertificates: string, + * stream: bool, + * filename: string|null, + * limit_response_size: int|null, + * _redirection: int, + * ... + * } * @phpstan-type Prepared_Request array{ * url: string, * headers: mixed[], * data: mixed, * type: mixed, * options: array, - * args: mixed[], + * args: Parsed_HTTP_Request_Args, * } */ #[AllowDynamicProperties] @@ -184,6 +225,7 @@ class WP_Http { * @type WP_HTTP_Requests_Response|null $http_response Response object. * } * + * @phpstan-param string|HTTP_Request_Args $args * @phpstan-return HTTP_Response|WP_Error */ public function request( $url, $args = array() ) { @@ -245,11 +287,11 @@ public function request( $url, $args = array() ) { * @param array $options { * Optional. Options that apply to the batch of requests. * - * @type int $concurrency Maximum number of requests to send at once. Default 6. + * @type int $concurrency Maximum number of requests to send at once. Default 6. Minimum 1. * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error * on failure. See {@see WP_Http::request()} for the response format. - * @phpstan-param array $requests + * @phpstan-param array $requests * @phpstan-return array */ public function request_multiple( array $requests, array $options = array() ): array { @@ -284,15 +326,17 @@ public function request_multiple( array $requests, array $options = array() ): a } if ( ! empty( $pending ) ) { - $concurrency = (int) ( $options['concurrency'] ?? 6 ); + $concurrency = max( 1, (int) ( $options['concurrency'] ?? 6 ) ); /** * Filters the maximum number of HTTP requests that WP_Http::request_multiple() sends at once. * * @since 7.3.0 * - * @param int $concurrency Maximum number of requests to send at once. Default 6. + * @param int $concurrency Maximum number of requests to send at once. Default 6. Minimum 1. * @param array $requests The requests to send. See WP_Http::request_multiple(). + * @phpstan-param positive-int $concurrency + * @phpstan-param array $requests */ $concurrency = (int) apply_filters( 'http_request_multiple_concurrency', $concurrency, $requests ); $concurrency = max( 1, $concurrency ); @@ -405,7 +449,7 @@ public function request_multiple( array $requests, array $options = array() ): a * @type array $args The parsed request arguments, after the 'http_request_args' filter. * @type array|WP_Error $response Optional. The response returned by the 'pre_http_request' filter. * } - * @phpstan-param mixed[] $args + * @phpstan-param HTTP_Request_Args $args * @phpstan-return Prepared_Request|array{ response: HTTP_Response|WP_Error }|WP_Error */ protected function prepare_request( $url, array $args = array() ) { @@ -674,6 +718,7 @@ protected function prepare_request( $url, array $args = array() ) { * returned by {@see WP_Http::prepare_request()}. * @param string $url The request URL. * @return array|WP_Error The response array, or a WP_Error on failure. See {@see WP_Http::request()}. + * @phpstan-param Parsed_HTTP_Request_Args $parsed_args * @phpstan-return HTTP_Response|WP_Error */ protected function finalize_response( $response, array $parsed_args, string $url ) { diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index a17b8e0612038..5796edb36176d 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -197,7 +197,14 @@ function wp_safe_remote_head( $url, $args = array() ) { * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. * @phpstan-param array $requests - * @phpstan-return array|WP_Error> + * @phpstan-return array, + * body: string, + * response: array{code: int|false, message: string|false}, + * cookies: WP_Http_Cookie[], + * filename?: string|null, + * http_response?: WP_HTTP_Requests_Response|null, + * }|WP_Error> */ function wp_safe_remote_request_multiple( array $requests, array $options = array() ): array { foreach ( $requests as $id => $request ) { @@ -372,7 +379,14 @@ function wp_remote_head( $url, $args = array() ) { * } * @return array Responses keyed like $requests. Each is a response array, or a WP_Error on failure. * @phpstan-param array $requests - * @phpstan-return array|WP_Error> + * @phpstan-return array, + * body: string, + * response: array{code: int|false, message: string|false}, + * cookies: WP_Http_Cookie[], + * filename?: string|null, + * http_response?: WP_HTTP_Requests_Response|null, + * }|WP_Error> */ function wp_remote_request_multiple( array $requests, array $options = array() ): array { $http = _wp_http_get_object();