Make WordPress Core


Ignore:
Timestamp:
11/25/2021 01:10:30 AM (3 years ago)
Author:
SergeyBiryukov
Message:

External Libraries: Update the Requests library to version 2.0.0.

This is a major release and contains breaking changes.

Most important changes to be aware of for this release:

  • All code is now namespaced. Though there is a full backward compatibility layer available and the old class names are still supported, using them will generate a deprecation notice (which can be silenced by plugins if they'd need to support multiple WP versions). See the upgrade guide for more details.
  • A lot of classes have been marked final. This should generally not affect userland code as care has been taken to not apply the final keyword to classes which are known to be extended in userland code.
  • Extensive input validation has been added to Requests. When Requests is used as documented though, this will be unnoticable.
  • A new WpOrg\Requests\Requests::has_capabilities() method has been introduced which can be used to address #37708.
  • A new WpOrg\Requests\Response::decode_body() method has been introduced which may be usable to simplify some of the WP native wrapper code.
  • Remaining PHP 8.0 compatibility fixed (support for named parameters).
  • PHP 8.1 compatibility.

Release notes: https://github.com/WordPress/Requests/releases/tag/v2.0.0

For a full list of changes in this update, see the Requests GitHub:
https://github.com/WordPress/Requests/compare/v1.8.1...v2.0.0

Follow-up to [50842], [51078].

Props jrf, schlessera, datagutten, wojsmol, dd32, dustinrue, soulseekah, costdev, szepeviktor.
Fixes #54504.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/Requests/Response.php

    r50842 r52244  
    33 * HTTP response class
    44 *
    5  * Contains a response from Requests::request()
     5 * Contains a response from \WpOrg\Requests\Requests::request()
     6 *
    67 * @package Requests
    78 */
     9
     10namespace WpOrg\Requests;
     11
     12use WpOrg\Requests\Cookie\Jar;
     13use WpOrg\Requests\Exception;
     14use WpOrg\Requests\Exception\Http;
     15use WpOrg\Requests\Response\Headers;
    816
    917/**
    1018 * HTTP response class
    1119 *
    12  * Contains a response from Requests::request()
     20 * Contains a response from \WpOrg\Requests\Requests::request()
     21 *
    1322 * @package Requests
    1423 */
    15 class Requests_Response {
    16     /**
    17      * Constructor
    18      */
    19     public function __construct() {
    20         $this->headers = new Requests_Response_Headers();
    21         $this->cookies = new Requests_Cookie_Jar();
    22     }
     24class Response {
    2325
    2426    /**
     
    3941     * Headers, as an associative array
    4042     *
    41      * @var Requests_Response_Headers Array-like object representing headers
     43     * @var \WpOrg\Requests\Response\Headers Array-like object representing headers
    4244     */
    43     public $headers = array();
     45    public $headers = [];
    4446
    4547    /**
     
    8183     * Previous requests (from redirects)
    8284     *
    83      * @var array Array of Requests_Response objects
     85     * @var array Array of \WpOrg\Requests\Response objects
    8486     */
    85     public $history = array();
     87    public $history = [];
    8688
    8789    /**
    8890     * Cookies from the request
    8991     *
    90      * @var Requests_Cookie_Jar Array-like object representing a cookie jar
     92     * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar
    9193     */
    92     public $cookies = array();
     94    public $cookies = [];
     95
     96    /**
     97     * Constructor
     98     */
     99    public function __construct() {
     100        $this->headers = new Headers();
     101        $this->cookies = new Jar();
     102    }
    93103
    94104    /**
     
    99109    public function is_redirect() {
    100110        $code = $this->status_code;
    101         return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;
     111        return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400;
    102112    }
    103113
     
    105115     * Throws an exception if the request was not successful
    106116     *
    107      * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
    108      * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
    109117     * @param boolean $allow_redirects Set to false to throw on a 3xx as well
     118     *
     119     * @throws \WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
     120     * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404})
    110121     */
    111122    public function throw_for_status($allow_redirects = true) {
    112123        if ($this->is_redirect()) {
    113             if (!$allow_redirects) {
    114                 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
     124            if ($allow_redirects !== true) {
     125                throw new Exception('Redirection not allowed', 'response.no_redirects', $this);
    115126            }
    116127        }
    117128        elseif (!$this->success) {
    118             $exception = Requests_Exception_HTTP::get_class($this->status_code);
     129            $exception = Http::get_class($this->status_code);
    119130            throw new $exception(null, $this);
    120131        }
    121132    }
     133
     134    /**
     135     * JSON decode the response body.
     136     *
     137     * The method parameters are the same as those for the PHP native `json_decode()` function.
     138     *
     139     * @link https://php.net/json-decode
     140     *
     141     * @param ?bool $associative Optional. When `true`, JSON objects will be returned as associative arrays;
     142     *                           When `false`, JSON objects will be returned as objects.
     143     *                           When `null`, JSON objects will be returned as associative arrays
     144     *                           or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags.
     145     *                           Defaults to `true` (in contrast to the PHP native default of `null`).
     146     * @param int   $depth       Optional. Maximum nesting depth of the structure being decoded.
     147     *                           Defaults to `512`.
     148     * @param int   $options     Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE,
     149     *                           JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
     150     *                           Defaults to `0` (no options set).
     151     *
     152     * @return array
     153     *
     154     * @throws \WpOrg\Requests\Exception If `$this->body` is not valid json.
     155     */
     156    public function decode_body($associative = true, $depth = 512, $options = 0) {
     157        $data = json_decode($this->body, $associative, $depth, $options);
     158
     159        if (json_last_error() !== JSON_ERROR_NONE) {
     160            $last_error = json_last_error_msg();
     161            throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this);
     162        }
     163
     164        return $data;
     165    }
    122166}
Note: See TracChangeset for help on using the changeset viewer.