Skip to content

Commit fed4c57

Browse files
Copilotswissspidy
andcommitted
Use pre_http_request filter instead of http_api_debug to capture errors
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 447a740 commit fed4c57

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/Core_Command.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,14 +1472,15 @@ private function get_updates( $assoc_args ) {
14721472
// Reset error tracking
14731473
$this->version_check_error = null;
14741474

1475-
// Hook into HTTP API debug to capture errors during version check
1476-
add_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10, 5 );
1475+
// Hook into pre_http_request with max priority to capture errors during version check
1476+
// This is necessary because tests and plugins may use pre_http_request to mock responses
1477+
add_filter( 'pre_http_request', [ $this, 'capture_version_check_error' ], PHP_INT_MAX, 3 );
14771478

14781479
try {
14791480
wp_version_check( [], $force_check );
14801481
} finally {
14811482
// Ensure the hook is always removed, even if wp_version_check() throws an exception
1482-
remove_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10 );
1483+
remove_filter( 'pre_http_request', [ $this, 'capture_version_check_error' ], PHP_INT_MAX );
14831484
}
14841485

14851486
/**
@@ -1538,33 +1539,40 @@ private function get_updates( $assoc_args ) {
15381539
}
15391540

15401541
/**
1541-
* Handles the http_api_debug action to capture HTTP errors during version check.
1542+
* Handles the pre_http_request filter to capture HTTP errors during version check.
15421543
*
1543-
* This method signature matches the http_api_debug action hook signature.
1544-
* Note: $class and $args parameters are not used but are required by the hook.
1544+
* This method signature matches the pre_http_request filter signature.
1545+
* Uses PHP_INT_MAX priority to run after test mocking and plugin modifications.
15451546
*
1546-
* @param array|WP_Error $response HTTP response or WP_Error object.
1547-
* @param string $context Context of the HTTP request.
1548-
* @param string $_class HTTP transport class name (unused).
1549-
* @param array $_args HTTP request arguments (unused).
1550-
* @param string $url URL being requested.
1547+
* @param false|array|WP_Error $response A preemptive return value of an HTTP request.
1548+
* @param array $args HTTP request arguments.
1549+
* @param string $url The request URL.
1550+
* @return false|array|WP_Error The response, unmodified.
15511551
*/
1552-
public function capture_version_check_error( $response, $context, $_class, $_args, $url ) {
1552+
public function capture_version_check_error( $response, $args, $url ) {
15531553
if ( false === strpos( $url, 'api.wordpress.org/core/version-check' ) ) {
1554-
return;
1554+
return $response;
15551555
}
15561556

1557-
// Only capture on response, not pre_response
1558-
if ( 'response' !== $context ) {
1559-
return;
1560-
}
1561-
1562-
// Store the error if the response is a WP_Error; clear it otherwise.
1557+
// Store the error if the response is a WP_Error
15631558
if ( is_wp_error( $response ) ) {
15641559
$this->version_check_error = $response;
1565-
} else {
1560+
} elseif ( is_array( $response ) && isset( $response['response']['code'] ) && $response['response']['code'] >= 400 ) {
1561+
// HTTP error status code (4xx or 5xx) - convert to WP_Error for consistency
1562+
$this->version_check_error = new \WP_Error(
1563+
'http_request_failed',
1564+
sprintf(
1565+
'HTTP request failed with status %d: %s',
1566+
$response['response']['code'],
1567+
isset( $response['response']['message'] ) ? $response['response']['message'] : 'Unknown error'
1568+
)
1569+
);
1570+
} elseif ( false !== $response ) {
1571+
// Clear any previous error if we got a successful mocked response
15661572
$this->version_check_error = null;
15671573
}
1574+
1575+
return $response;
15681576
}
15691577

15701578
/**

0 commit comments

Comments
 (0)