Skip to content

Commit

Permalink
Use Requests 2.0+ namespaced interfaces when available. (#925)
Browse files Browse the repository at this point in the history
## What is this PR doing?

Uses the WP 6.2+ / Requests 2.0+ namespaced classes, to avoid
deprecation warnings, while supporting older clients.

## What problem is it solving?

Fixes #922 PHP deprecation notices, which can be seen in [Theme
Directory live
previews](https://themes.trac.wordpress.org/ticket/159444#:~:text=zip%3Fnostats%3D1-,Live%20preview,-%E2%80%93%20https%3A//playground)

## How is the problem addressed?

Conditionally selecting the class to use

## Testing Instructions

Go to
http://localhost:5400/website-server/#{%22steps%22:[{%22step%22:%22defineWpConfigConsts%22,%22consts%22:{%22WP_DEBUG%22:true}}]}
and confirm the following deprecation notices are not shown:

```
Deprecated: The PSR-0 'Requests_... class names in the Requests library are
deprecated. Switch to the PSR-4 'WpOrg|Requests|.. class names at your
```

(Other deprecation notices are expected as they're present in WordPress
core)
  • Loading branch information
adamziel authored Jan 9, 2024
1 parent 3d0f04e commit 43beecb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* to prevent the Requests class from complaining about not having any
* transports.
*/
class Requests_Transport_Dummy implements Requests_Transport
class Requests_Transport_Dummy_Base
{
public $headers = '';

Expand Down Expand Up @@ -41,3 +41,15 @@ public static function test($capabilities = array())
return true;
}
}

if (class_exists('\WpOrg\Requests\Requests')) {
class Requests_Transport_Dummy extends Requests_Transport_Dummy_Base implements \WpOrg\Requests\Transport
{

}
} else {
class Requests_Transport_Dummy extends Requests_Transport_Dummy_Base implements Requests_Transport
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* a class named "Wp_Http_" . $transport_name – which means we must adhere to this
* hardcoded pattern.
*/
class Wp_Http_Fetch implements Requests_Transport
class Wp_Http_Fetch_Base
{
public $headers = '';

Expand Down Expand Up @@ -47,31 +47,33 @@ public function request($url, $headers = array(), $data = array(), $options = ar
if (!empty($data)) {
$data_format = $options['data_format'];
if ($data_format === 'query') {
$url = self::format_get($url, $data);
$url = self::format_get($url, $data);
$data = '';
} elseif (!is_string($data)) {
$data = http_build_query($data, null, '&');
}
}

$request = json_encode(array(
'type' => 'request',
'data' => [
'headers' => $headers,
'data' => $data,
'url' => $url,
'method' => $options['type'],
]
));
$request = json_encode(
array(
'type' => 'request',
'data' => [
'headers' => $headers,
'data' => $data,
'url' => $url,
'method' => $options['type'],
]
)
);

$this->headers = post_message_to_js($request);

// Store a file if the request specifies it.
// Are we sure that `$this->headers` includes the body of the response?
$before_response_body = strpos( $this->headers, "\r\n\r\n" );
if ( isset( $options['filename'] ) && $options['filename'] && false !== $before_response_body ) {
$response_body = substr( $this->headers, $before_response_body + 4 );
$this->headers = substr( $this->headers, 0, $before_response_body );
$before_response_body = strpos($this->headers, "\r\n\r\n");
if (isset($options['filename']) && $options['filename'] && false !== $before_response_body) {
$response_body = substr($this->headers, $before_response_body + 4);
$this->headers = substr($this->headers, 0, $before_response_body);
file_put_contents($options['filename'], $response_body);
}

Expand All @@ -81,10 +83,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar
public function request_multiple($requests, $options)
{
$responses = array();
$class = get_class($this);
$class = get_class($this);
foreach ($requests as $id => $request) {
try {
$handler = new $class();
$handler = new $class();
$responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
$request['options']['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$id], $request));
} catch (Requests_Exception $e) {
Expand All @@ -101,7 +103,7 @@ public function request_multiple($requests, $options)
protected static function format_get($url, $data)
{
if (!empty($data)) {
$query = '';
$query = '';
$url_parts = parse_url($url);
if (empty($url_parts['query'])) {
$url_parts['query'] = '';
Expand Down Expand Up @@ -129,3 +131,15 @@ public static function test($capabilities = array())
return true;
}
}

if (class_exists('\WpOrg\Requests\Requests')) {
class Wp_Http_Fetch extends Wp_Http_Fetch_Base implements \WpOrg\Requests\Transport
{

}
} else {
class Wp_Http_Fetch extends Wp_Http_Fetch_Base implements Requests_Transport
{

}
}

0 comments on commit 43beecb

Please sign in to comment.