Make WordPress Core


Ignore:
Timestamp:
12/06/2021 09:29:00 PM (3 years ago)
Author:
SergeyBiryukov
Message:

HTTP API: Revert changeset [52244].

Reverting Requests 2.0.0 changes and moving to WordPress 6.0 cycle. Why? The namespace and file case renaming revealed 2 issues in Core's upgrader process.

See https://core.trac.wordpress.org/ticket/54504#comment:22 for more information.

Follow-up to [52327].

See #54562, #54504.

File:
1 edited

Legend:

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

    r52244 r52328  
    33 * Session handler for persistent requests and default parameters
    44 *
    5  * @package Requests\SessionHandler
     5 * @package Requests
     6 * @subpackage Session Handler
    67 */
    7 
    8 namespace WpOrg\Requests;
    9 
    10 use WpOrg\Requests\Cookie\Jar;
    11 use WpOrg\Requests\Exception\InvalidArgument;
    12 use WpOrg\Requests\Iri;
    13 use WpOrg\Requests\Requests;
    14 use WpOrg\Requests\Utility\InputValidator;
    158
    169/**
     
    2215 * a shared cookie jar), then overridden for individual requests.
    2316 *
    24  * @package Requests\SessionHandler
     17 * @package Requests
     18 * @subpackage Session Handler
    2519 */
    26 class Session {
     20class Requests_Session {
    2721    /**
    2822     * Base URL for requests
     
    3933     * @var array
    4034     */
    41     public $headers = [];
     35    public $headers = array();
    4236
    4337    /**
     
    4943     * @var array
    5044     */
    51     public $data = [];
     45    public $data = array();
    5246
    5347    /**
     
    6256     * @var array
    6357     */
    64     public $options = [];
     58    public $options = array();
    6559
    6660    /**
    6761     * Create a new session
    6862     *
    69      * @param string|Stringable|null $url Base URL for requests
     63     * @param string|null $url Base URL for requests
    7064     * @param array $headers Default headers for requests
    7165     * @param array $data Default data for requests
    7266     * @param array $options Default options for requests
    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 
     67     */
     68    public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
    9669        $this->url     = $url;
    9770        $this->headers = $headers;
     
    10073
    10174        if (empty($this->options['cookies'])) {
    102             $this->options['cookies'] = new Jar();
     75            $this->options['cookies'] = new Requests_Cookie_Jar();
    10376        }
    10477    }
     
    10780     * Get a property's value
    10881     *
    109      * @param string $name Property name.
     82     * @param string $key Property key
    11083     * @return mixed|null Property value, null if none found
    11184     */
    112     public function __get($name) {
    113         if (isset($this->options[$name])) {
    114             return $this->options[$name];
     85    public function __get($key) {
     86        if (isset($this->options[$key])) {
     87            return $this->options[$key];
    11588        }
    11689
     
    12194     * Set a property's value
    12295     *
    123      * @param string $name Property name.
     96     * @param string $key Property key
    12497     * @param mixed $value Property value
    12598     */
    126     public function __set($name, $value) {
    127         $this->options[$name] = $value;
     99    public function __set($key, $value) {
     100        $this->options[$key] = $value;
    128101    }
    129102
     
    131104     * Remove a property's value
    132105     *
    133      * @param string $name Property name.
    134      */
    135     public function __isset($name) {
    136         return isset($this->options[$name]);
     106     * @param string $key Property key
     107     */
     108    public function __isset($key) {
     109        return isset($this->options[$key]);
    137110    }
    138111
     
    140113     * Remove a property's value
    141114     *
    142      * @param string $name Property name.
    143      */
    144     public function __unset($name) {
    145         unset($this->options[$name]);
     115     * @param string $key Property key
     116     */
     117    public function __unset($key) {
     118        if (isset($this->options[$key])) {
     119            unset($this->options[$key]);
     120        }
    146121    }
    147122
    148123    /**#@+
    149      * @see \WpOrg\Requests\Session::request()
     124     * @see request()
    150125     * @param string $url
    151126     * @param array $headers
    152127     * @param array $options
    153      * @return \WpOrg\Requests\Response
     128     * @return Requests_Response
    154129     */
    155130    /**
    156131     * Send a GET request
    157132     */
    158     public function get($url, $headers = [], $options = []) {
     133    public function get($url, $headers = array(), $options = array()) {
    159134        return $this->request($url, $headers, null, Requests::GET, $options);
    160135    }
     
    163138     * Send a HEAD request
    164139     */
    165     public function head($url, $headers = [], $options = []) {
     140    public function head($url, $headers = array(), $options = array()) {
    166141        return $this->request($url, $headers, null, Requests::HEAD, $options);
    167142    }
     
    170145     * Send a DELETE request
    171146     */
    172     public function delete($url, $headers = [], $options = []) {
     147    public function delete($url, $headers = array(), $options = array()) {
    173148        return $this->request($url, $headers, null, Requests::DELETE, $options);
    174149    }
     
    176151
    177152    /**#@+
    178      * @see \WpOrg\Requests\Session::request()
     153     * @see request()
    179154     * @param string $url
    180155     * @param array $headers
    181156     * @param array $data
    182157     * @param array $options
    183      * @return \WpOrg\Requests\Response
     158     * @return Requests_Response
    184159     */
    185160    /**
    186161     * Send a POST request
    187162     */
    188     public function post($url, $headers = [], $data = [], $options = []) {
     163    public function post($url, $headers = array(), $data = array(), $options = array()) {
    189164        return $this->request($url, $headers, $data, Requests::POST, $options);
    190165    }
     
    193168     * Send a PUT request
    194169     */
    195     public function put($url, $headers = [], $data = [], $options = []) {
     170    public function put($url, $headers = array(), $data = array(), $options = array()) {
    196171        return $this->request($url, $headers, $data, Requests::PUT, $options);
    197172    }
     
    200175     * Send a PATCH request
    201176     *
    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
     177     * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
     178     * specification recommends that should send an ETag
    204179     *
    205180     * @link https://tools.ietf.org/html/rfc5789
    206181     */
    207     public function patch($url, $headers, $data = [], $options = []) {
     182    public function patch($url, $headers, $data = array(), $options = array()) {
    208183        return $this->request($url, $headers, $data, Requests::PATCH, $options);
    209184    }
     
    216191     * parsing.
    217192     *
    218      * @see \WpOrg\Requests\Requests::request()
     193     * @see Requests::request()
     194     *
     195     * @throws Requests_Exception On invalid URLs (`nonhttp`)
    219196     *
    220197     * @param string $url URL to request
    221198     * @param array $headers Extra headers to send with the request
    222199     * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
    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 = []) {
     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()) {
    230205        $request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
    231206
     
    236211     * Send multiple HTTP requests simultaneously
    237212     *
    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 
     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()) {
    256220        foreach ($requests as $key => $request) {
    257221            $requests[$key] = $this->merge_request($request, false);
     
    269233     * Merge a request's data with the default data
    270234     *
    271      * @param array $request Request data (same form as {@see \WpOrg\Requests\Session::request_multiple()})
     235     * @param array $request Request data (same form as {@see request_multiple})
    272236     * @param boolean $merge_options Should we merge options as well?
    273237     * @return array Request data
     
    275239    protected function merge_request($request, $merge_options = true) {
    276240        if ($this->url !== null) {
    277             $request['url'] = Iri::absolutize($this->url, $request['url']);
     241            $request['url'] = Requests_IRI::absolutize($this->url, $request['url']);
    278242            $request['url'] = $request['url']->uri;
    279243        }
    280244
    281245        if (empty($request['headers'])) {
    282             $request['headers'] = [];
     246            $request['headers'] = array();
    283247        }
    284248        $request['headers'] = array_merge($this->headers, $request['headers']);
     
    293257        }
    294258
    295         if ($merge_options === true) {
     259        if ($merge_options !== false) {
    296260            $request['options'] = array_merge($this->options, $request['options']);
    297261
Note: See TracChangeset for help on using the changeset viewer.