Make WordPress Core


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

    r50842 r52244  
    66 */
    77
     8namespace WpOrg\Requests\Response;
     9
     10use WpOrg\Requests\Exception;
     11use WpOrg\Requests\Exception\InvalidArgument;
     12use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
     13use WpOrg\Requests\Utility\FilteredIterator;
     14
    815/**
    916 * Case-insensitive dictionary, suitable for HTTP headers
     
    1118 * @package Requests
    1219 */
    13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
     20class Headers extends CaseInsensitiveDictionary {
    1421    /**
    1522     * Get the given header
    1623     *
    17      * Unlike {@see self::getValues()}, this returns a string. If there are
     24     * Unlike {@see \WpOrg\Requests\Response\Headers::getValues()}, this returns a string. If there are
    1825     * multiple values, it concatenates them with a comma as per RFC2616.
    1926     *
     
    2128     * Set-Cookie headers.
    2229     *
    23      * @param string $key
     30     * @param string $offset
    2431     * @return string|null Header value
    2532     */
    26     public function offsetGet($key) {
    27         $key = strtolower($key);
    28         if (!isset($this->data[$key])) {
     33    public function offsetGet($offset) {
     34        if (is_string($offset)) {
     35            $offset = strtolower($offset);
     36        }
     37
     38        if (!isset($this->data[$offset])) {
    2939            return null;
    3040        }
    3141
    32         return $this->flatten($this->data[$key]);
     42        return $this->flatten($this->data[$offset]);
    3343    }
    3444
     
    3646     * Set the given item
    3747     *
    38      * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     48     * @param string $offset Item name
     49     * @param string $value Item value
    3950     *
    40      * @param string $key Item name
    41      * @param string $value Item value
     51     * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
    4252     */
    43     public function offsetSet($key, $value) {
    44         if ($key === null) {
    45             throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
     53    public function offsetSet($offset, $value) {
     54        if ($offset === null) {
     55            throw new Exception('Object is a dictionary, not a list', 'invalidset');
    4656        }
    4757
    48         $key = strtolower($key);
    49 
    50         if (!isset($this->data[$key])) {
    51             $this->data[$key] = array();
     58        if (is_string($offset)) {
     59            $offset = strtolower($offset);
    5260        }
    5361
    54         $this->data[$key][] = $value;
     62        if (!isset($this->data[$offset])) {
     63            $this->data[$offset] = [];
     64        }
     65
     66        $this->data[$offset][] = $value;
    5567    }
    5668
     
    5870     * Get all values for a given header
    5971     *
    60      * @param string $key
     72     * @param string $offset
    6173     * @return array|null Header values
     74     *
     75     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not valid as an array key.
    6276     */
    63     public function getValues($key) {
    64         $key = strtolower($key);
    65         if (!isset($this->data[$key])) {
     77    public function getValues($offset) {
     78        if (!is_string($offset) && !is_int($offset)) {
     79            throw InvalidArgument::create(1, '$offset', 'string|int', gettype($offset));
     80        }
     81
     82        $offset = strtolower($offset);
     83        if (!isset($this->data[$offset])) {
    6684            return null;
    6785        }
    6886
    69         return $this->data[$key];
     87        return $this->data[$offset];
    7088    }
    7189
     
    7896     * @param string|array $value Value to flatten
    7997     * @return string Flattened value
     98     *
     99     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array.
    80100     */
    81101    public function flatten($value) {
    82         if (is_array($value)) {
    83             $value = implode(',', $value);
     102        if (is_string($value)) {
     103            return $value;
    84104        }
    85105
    86         return $value;
     106        if (is_array($value)) {
     107            return implode(',', $value);
     108        }
     109
     110        throw InvalidArgument::create(1, '$value', 'string|array', gettype($value));
    87111    }
    88112
     
    90114     * Get an iterator for the data
    91115     *
    92      * Converts the internal
    93      * @return ArrayIterator
     116     * Converts the internally stored values to a comma-separated string if there is more
     117     * than one value for a key.
     118     *
     119     * @return \ArrayIterator
    94120     */
    95121    public function getIterator() {
    96         return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
     122        return new FilteredIterator($this->data, [$this, 'flatten']);
    97123    }
    98124}
Note: See TracChangeset for help on using the changeset viewer.