Make WordPress Core


Ignore:
Timestamp:
11/25/2021 01:10:30 AM (4 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/Cookie/Jar.php

    r50842 r52244  
    33 * Cookie holder object
    44 *
    5  * @package Requests
    6  * @subpackage Cookies
     5 * @package Requests\Cookies
    76 */
     7
     8namespace WpOrg\Requests\Cookie;
     9
     10use ArrayAccess;
     11use ArrayIterator;
     12use IteratorAggregate;
     13use ReturnTypeWillChange;
     14use WpOrg\Requests\Cookie;
     15use WpOrg\Requests\Exception;
     16use WpOrg\Requests\Exception\InvalidArgument;
     17use WpOrg\Requests\HookManager;
     18use WpOrg\Requests\Iri;
     19use WpOrg\Requests\Response;
    820
    921/**
    1022 * Cookie holder object
    1123 *
    12  * @package Requests
    13  * @subpackage Cookies
     24 * @package Requests\Cookies
    1425 */
    15 class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate {
     26class Jar implements ArrayAccess, IteratorAggregate {
    1627    /**
    1728     * Actual item data
     
    1930     * @var array
    2031     */
    21     protected $cookies = array();
     32    protected $cookies = [];
    2233
    2334    /**
     
    2536     *
    2637     * @param array $cookies Existing cookie values
     38     *
     39     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array.
    2740     */
    28     public function __construct($cookies = array()) {
     41    public function __construct($cookies = []) {
     42        if (is_array($cookies) === false) {
     43            throw InvalidArgument::create(1, '$cookies', 'array', gettype($cookies));
     44        }
     45
    2946        $this->cookies = $cookies;
    3047    }
    3148
    3249    /**
    33      * Normalise cookie data into a Requests_Cookie
     50     * Normalise cookie data into a \WpOrg\Requests\Cookie
    3451     *
    35      * @param string|Requests_Cookie $cookie
    36      * @return Requests_Cookie
     52     * @param string|\WpOrg\Requests\Cookie $cookie
     53     * @return \WpOrg\Requests\Cookie
    3754     */
    38     public function normalize_cookie($cookie, $key = null) {
    39         if ($cookie instanceof Requests_Cookie) {
     55    public function normalize_cookie($cookie, $key = '') {
     56        if ($cookie instanceof Cookie) {
    4057            return $cookie;
    4158        }
    4259
    43         return Requests_Cookie::parse($cookie, $key);
    44     }
    45 
    46     /**
    47      * Normalise cookie data into a Requests_Cookie
    48      *
    49      * @codeCoverageIgnore
    50      * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie}
    51      * @return Requests_Cookie
    52      */
    53     public function normalizeCookie($cookie, $key = null) {
    54         return $this->normalize_cookie($cookie, $key);
     60        return Cookie::parse($cookie, $key);
    5561    }
    5662
     
    5864     * Check if the given item exists
    5965     *
    60      * @param string $key Item key
     66     * @param string $offset Item key
    6167     * @return boolean Does the item exist?
    6268     */
    63     public function offsetExists($key) {
    64         return isset($this->cookies[$key]);
     69    #[ReturnTypeWillChange]
     70    public function offsetExists($offset) {
     71        return isset($this->cookies[$offset]);
    6572    }
    6673
     
    6875     * Get the value for the item
    6976     *
    70      * @param string $key Item key
     77     * @param string $offset Item key
    7178     * @return string|null Item value (null if offsetExists is false)
    7279     */
    73     public function offsetGet($key) {
    74         if (!isset($this->cookies[$key])) {
     80    #[ReturnTypeWillChange]
     81    public function offsetGet($offset) {
     82        if (!isset($this->cookies[$offset])) {
    7583            return null;
    7684        }
    7785
    78         return $this->cookies[$key];
     86        return $this->cookies[$offset];
    7987    }
    8088
     
    8290     * Set the given item
    8391     *
    84      * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     92     * @param string $offset Item name
     93     * @param string $value Item value
    8594     *
    86      * @param string $key Item name
    87      * @param string $value Item value
     95     * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
    8896     */
    89     public function offsetSet($key, $value) {
    90         if ($key === null) {
    91             throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
     97    #[ReturnTypeWillChange]
     98    public function offsetSet($offset, $value) {
     99        if ($offset === null) {
     100            throw new Exception('Object is a dictionary, not a list', 'invalidset');
    92101        }
    93102
    94         $this->cookies[$key] = $value;
     103        $this->cookies[$offset] = $value;
    95104    }
    96105
     
    98107     * Unset the given header
    99108     *
    100      * @param string $key
     109     * @param string $offset
    101110     */
    102     public function offsetUnset($key) {
    103         unset($this->cookies[$key]);
     111    #[ReturnTypeWillChange]
     112    public function offsetUnset($offset) {
     113        unset($this->cookies[$offset]);
    104114    }
    105115
     
    107117     * Get an iterator for the data
    108118     *
    109      * @return ArrayIterator
     119     * @return \ArrayIterator
    110120     */
     121    #[ReturnTypeWillChange]
    111122    public function getIterator() {
    112123        return new ArrayIterator($this->cookies);
     
    116127     * Register the cookie handler with the request's hooking system
    117128     *
    118      * @param Requests_Hooker $hooks Hooking system
     129     * @param \WpOrg\Requests\HookManager $hooks Hooking system
    119130     */
    120     public function register(Requests_Hooker $hooks) {
    121         $hooks->register('requests.before_request', array($this, 'before_request'));
    122         $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));
     131    public function register(HookManager $hooks) {
     132        $hooks->register('requests.before_request', [$this, 'before_request']);
     133        $hooks->register('requests.before_redirect_check', [$this, 'before_redirect_check']);
    123134    }
    124135
     
    135146     */
    136147    public function before_request($url, &$headers, &$data, &$type, &$options) {
    137         if (!$url instanceof Requests_IRI) {
    138             $url = new Requests_IRI($url);
     148        if (!$url instanceof Iri) {
     149            $url = new Iri($url);
    139150        }
    140151
    141152        if (!empty($this->cookies)) {
    142             $cookies = array();
     153            $cookies = [];
    143154            foreach ($this->cookies as $key => $cookie) {
    144155                $cookie = $this->normalize_cookie($cookie, $key);
     
    161172     * Parse all cookies from a response and attach them to the response
    162173     *
    163      * @var Requests_Response $response
     174     * @param \WpOrg\Requests\Response $response
    164175     */
    165     public function before_redirect_check(Requests_Response $return) {
    166         $url = $return->url;
    167         if (!$url instanceof Requests_IRI) {
    168             $url = new Requests_IRI($url);
     176    public function before_redirect_check(Response $response) {
     177        $url = $response->url;
     178        if (!$url instanceof Iri) {
     179            $url = new Iri($url);
    169180        }
    170181
    171         $cookies         = Requests_Cookie::parse_from_headers($return->headers, $url);
    172         $this->cookies   = array_merge($this->cookies, $cookies);
    173         $return->cookies = $this;
     182        $cookies           = Cookie::parse_from_headers($response->headers, $url);
     183        $this->cookies     = array_merge($this->cookies, $cookies);
     184        $response->cookies = $this;
    174185    }
    175186}
Note: See TracChangeset for help on using the changeset viewer.