Make WordPress Core


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

    r50842 r52244  
    33 * Session handler for persistent requests and default parameters
    44 *
    5  * @package Requests
    6  * @subpackage Session Handler
     5 * @package Requests\SessionHandler
    76 */
     7
     8namespace WpOrg\Requests;
     9
     10use WpOrg\Requests\Cookie\Jar;
     11use WpOrg\Requests\Exception\InvalidArgument;
     12use WpOrg\Requests\Iri;
     13use WpOrg\Requests\Requests;
     14use WpOrg\Requests\Utility\InputValidator;
    815
    916/**
    … …  
    1522 * a shared cookie jar), then overridden for individual requests.
    1623 *
    17  * @package Requests
    18  * @subpackage Session Handler
     24 * @package Requests\SessionHandler
    1925 */
    20 class Requests_Session {
     26class Session {
    2127        /**
    2228         * Base URL for requests
    … …  
    3339         * @var array
    3440         */
    35         public $headers = array();
     41        public $headers = [];
    3642
    3743        /**
    … …  
    4349         * @var array
    4450         */
    45         public $data = array();
     51        public $data = [];
    4652
    4753        /**
    … …  
    5662         * @var array
    5763         */
    58         public $options = array();
     64        public $options = [];
    5965
    6066        /**
    6167         * Create a new session
    6268         *
    63          * @param string|null $url Base URL for requests
     69         * @param string|Stringable|null $url Base URL for requests
    6470         * @param array $headers Default headers for requests
    6571         * @param array $data Default data for requests
    6672         * @param array $options Default options for requests
    67          */
    68         public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
     73         *
     74         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string, Stringable or null.
     75         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array.
     76         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not an array.
     77         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
     78         */
     79        public function __construct($url = null, $headers = [], $data = [], $options = []) {
     80                if ($url !== null && InputValidator::is_string_or_stringable($url) === false) {
     81                        throw InvalidArgument::create(1, '$url', 'string|Stringable|null', gettype($url));
     82                }
     83
     84                if (is_array($headers) === false) {
     85                        throw InvalidArgument::create(2, '$headers', 'array', gettype($headers));
     86                }
     87
     88                if (is_array($data) === false) {
     89                        throw InvalidArgument::create(3, '$data', 'array', gettype($data));
     90                }
     91
     92                if (is_array($options) === false) {
     93                        throw InvalidArgument::create(4, '$options', 'array', gettype($options));
     94                }
     95
    6996                $this->url     = $url;
    7097                $this->headers = $headers;
    … …  
    73100
    74101                if (empty($this->options['cookies'])) {
    75                         $this->options['cookies'] = new Requests_Cookie_Jar();
     102                        $this->options['cookies'] = new Jar();
    76103                }
    77104        }
    … …  
    80107         * Get a property's value
    81108         *
    82          * @param string $key Property key
     109         * @param string $name Property name.
    83110         * @return mixed|null Property value, null if none found
    84111         */
    85         public function __get($key) {
    86                 if (isset($this->options[$key])) {
    87                         return $this->options[$key];
     112        public function __get($name) {
     113                if (isset($this->options[$name])) {
     114                        return $this->options[$name];
    88115                }
    89116
    … …  
    94121         * Set a property's value
    95122         *
    96          * @param string $key Property key
     123         * @param string $name Property name.
    97124         * @param mixed $value Property value
    98125         */
    99         public function __set($key, $value) {
    100                 $this->options[$key] = $value;
     126        public function __set($name, $value) {
     127                $this->options[$name] = $value;
    101128        }
    102129
    … …  
    104131         * Remove a property's value
    105132         *
    106          * @param string $key Property key
    107          */
    108         public function __isset($key) {
    109                 return isset($this->options[$key]);
     133         * @param string $name Property name.
     134         */
     135        public function __isset($name) {
     136                return isset($this->options[$name]);
    110137        }
    111138
    … …  
    113140         * Remove a property's value
    114141         *
    115          * @param string $key Property key
    116          */
    117         public function __unset($key) {
    118                 if (isset($this->options[$key])) {
    119                         unset($this->options[$key]);
    120                 }
     142         * @param string $name Property name.
     143         */
     144        public function __unset($name) {
     145                unset($this->options[$name]);
    121146        }
    122147
    123148        /**#@+
    124          * @see request()
     149         * @see \WpOrg\Requests\Session::request()
    125150         * @param string $url
    126151         * @param array $headers
    127152         * @param array $options
    128          * @return Requests_Response
     153         * @return \WpOrg\Requests\Response
    129154         */
    130155        /**
    131156         * Send a GET request
    132157         */
    133         public function get($url, $headers = array(), $options = array()) {
     158        public function get($url, $headers = [], $options = []) {
    134159                return $this->request($url, $headers, null, Requests::GET, $options);
    135160        }
    … …  
    138163         * Send a HEAD request
    139164         */
    140         public function head($url, $headers = array(), $options = array()) {
     165        public function head($url, $headers = [], $options = []) {
    141166                return $this->request($url, $headers, null, Requests::HEAD, $options);
    142167        }
    … …  
    145170         * Send a DELETE request
    146171         */
    147         public function delete($url, $headers = array(), $options = array()) {
     172        public function delete($url, $headers = [], $options = []) {
    148173                return $this->request($url, $headers, null, Requests::DELETE, $options);
    149174        }
    … …  
    151176
    152177        /**#@+
    153          * @see request()
     178         * @see \WpOrg\Requests\Session::request()
    154179         * @param string $url
    155180         * @param array $headers
    156181         * @param array $data
    157182         * @param array $options
    158          * @return Requests_Response
     183         * @return \WpOrg\Requests\Response
    159184         */
    160185        /**
    161186         * Send a POST request
    162187         */
    163         public function post($url, $headers = array(), $data = array(), $options = array()) {
     188        public function post($url, $headers = [], $data = [], $options = []) {
    164189                return $this->request($url, $headers, $data, Requests::POST, $options);
    165190        }
    … …  
    168193         * Send a PUT request
    169194         */
    170         public function put($url, $headers = array(), $data = array(), $options = array()) {
     195        public function put($url, $headers = [], $data = [], $options = []) {
    171196                return $this->request($url, $headers, $data, Requests::PUT, $options);
    172197        }
    … …  
    175200         * Send a PATCH request
    176201         *
    177          * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
    178          * specification recommends that should send an ETag
     202         * Note: Unlike {@see \WpOrg\Requests\Session::post()} and {@see \WpOrg\Requests\Session::put()},
     203         * `$headers` is required, as the specification recommends that should send an ETag
    179204         *
    180205         * @link https://tools.ietf.org/html/rfc5789
    181206         */
    182         public function patch($url, $headers, $data = array(), $options = array()) {
     207        public function patch($url, $headers, $data = [], $options = []) {
    183208                return $this->request($url, $headers, $data, Requests::PATCH, $options);
    184209        }
    … …  
    191216         * parsing.
    192217         *
    193          * @see Requests::request()
    194          *
    195          * @throws Requests_Exception On invalid URLs (`nonhttp`)
     218         * @see \WpOrg\Requests\Requests::request()
    196219         *
    197220         * @param string $url URL to request
    198221         * @param array $headers Extra headers to send with the request
    199222         * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
    200          * @param string $type HTTP request type (use Requests constants)
    201          * @param array $options Options for the request (see {@see Requests::request})
    202          * @return Requests_Response
    203          */
    204         public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {
     223         * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants)
     224         * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request()})
     225         * @return \WpOrg\Requests\Response
     226         *
     227         * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`)
     228         */
     229        public function request($url, $headers = [], $data = [], $type = Requests::GET, $options = []) {
    205230                $request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
    206231
    … …  
    211236         * Send multiple HTTP requests simultaneously
    212237         *
    213          * @see Requests::request_multiple()
    214          *
    215          * @param array $requests Requests data (see {@see Requests::request_multiple})
    216          * @param array $options Global and default options (see {@see Requests::request})
    217          * @return array Responses (either Requests_Response or a Requests_Exception object)
    218          */
    219         public function request_multiple($requests, $options = array()) {
     238         * @see \WpOrg\Requests\Requests::request_multiple()
     239         *
     240         * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple()})
     241         * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request()})
     242         * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object)
     243         *
     244         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
     245         * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
     246         */
     247        public function request_multiple($requests, $options = []) {
     248                if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
     249                        throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
     250                }
     251
     252                if (is_array($options) === false) {
     253                        throw InvalidArgument::create(2, '$options', 'array', gettype($options));
     254                }
     255
    220256                foreach ($requests as $key => $request) {
    221257                        $requests[$key] = $this->merge_request($request, false);
    … …  
    233269         * Merge a request's data with the default data
    234270         *
    235          * @param array $request Request data (same form as {@see request_multiple})
     271         * @param array $request Request data (same form as {@see \WpOrg\Requests\Session::request_multiple()})
    236272         * @param boolean $merge_options Should we merge options as well?
    237273         * @return array Request data
    … …  
    239275        protected function merge_request($request, $merge_options = true) {
    240276                if ($this->url !== null) {
    241                         $request['url'] = Requests_IRI::absolutize($this->url, $request['url']);
     277                        $request['url'] = Iri::absolutize($this->url, $request['url']);
    242278                        $request['url'] = $request['url']->uri;
    243279                }
    244280
    245281                if (empty($request['headers'])) {
    246                         $request['headers'] = array();
     282                        $request['headers'] = [];
    247283                }
    248284                $request['headers'] = array_merge($this->headers, $request['headers']);
    … …  
    257293                }
    258294
    259                 if ($merge_options !== false) {
     295                if ($merge_options === true) {
    260296                        $request['options'] = array_merge($this->options, $request['options']);
    261297
Note: See TracChangeset for help on using the changeset viewer.