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/Hooks.php

    r50842 r52244  
    33 * Handles adding and dispatching events
    44 *
    5  * @package Requests
    6  * @subpackage Utilities
     5 * @package Requests\EventDispatcher
    76 */
     7
     8namespace WpOrg\Requests;
     9
     10use WpOrg\Requests\Exception\InvalidArgument;
     11use WpOrg\Requests\HookManager;
     12use WpOrg\Requests\Utility\InputValidator;
    813
    914/**
    1015 * Handles adding and dispatching events
    1116 *
    12  * @package Requests
    13  * @subpackage Utilities
     17 * @package Requests\EventDispatcher
    1418 */
    15 class Requests_Hooks implements Requests_Hooker {
     19class Hooks implements HookManager {
    1620    /**
    1721     * Registered callbacks for each hook
     
    1923     * @var array
    2024     */
    21     protected $hooks = array();
    22 
    23     /**
    24      * Constructor
    25      */
    26     public function __construct() {
    27         // pass
    28     }
     25    protected $hooks = [];
    2926
    3027    /**
     
    3229     *
    3330     * @param string $hook Hook name
    34      * @param callback $callback Function/method to call on event
     31     * @param callable $callback Function/method to call on event
    3532     * @param int $priority Priority number. <0 is executed earlier, >0 is executed later
     33     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
     34     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $callback argument is not callable.
     35     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $priority argument is not an integer.
    3636     */
    3737    public function register($hook, $callback, $priority = 0) {
     38        if (is_string($hook) === false) {
     39            throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
     40        }
     41
     42        if (is_callable($callback) === false) {
     43            throw InvalidArgument::create(2, '$callback', 'callable', gettype($callback));
     44        }
     45
     46        if (InputValidator::is_numeric_array_key($priority) === false) {
     47            throw InvalidArgument::create(3, '$priority', 'integer', gettype($priority));
     48        }
     49
    3850        if (!isset($this->hooks[$hook])) {
    39             $this->hooks[$hook] = array();
    40         }
    41         if (!isset($this->hooks[$hook][$priority])) {
    42             $this->hooks[$hook][$priority] = array();
     51            $this->hooks[$hook] = [
     52                $priority => [],
     53            ];
     54        } elseif (!isset($this->hooks[$hook][$priority])) {
     55            $this->hooks[$hook][$priority] = [];
    4356        }
    4457
     
    5265     * @param array $parameters Parameters to pass to callbacks
    5366     * @return boolean Successfulness
     67     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
     68     * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $parameters argument is not an array.
    5469     */
    55     public function dispatch($hook, $parameters = array()) {
     70    public function dispatch($hook, $parameters = []) {
     71        if (is_string($hook) === false) {
     72            throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
     73        }
     74
     75        // Check strictly against array, as Array* objects don't work in combination with `call_user_func_array()`.
     76        if (is_array($parameters) === false) {
     77            throw InvalidArgument::create(2, '$parameters', 'array', gettype($parameters));
     78        }
     79
    5680        if (empty($this->hooks[$hook])) {
    5781            return false;
    5882        }
    5983
     84        if (!empty($parameters)) {
     85            // Strip potential keys from the array to prevent them being interpreted as parameter names in PHP 8.0.
     86            $parameters = array_values($parameters);
     87        }
     88
    6089        foreach ($this->hooks[$hook] as $priority => $hooked) {
    6190            foreach ($hooked as $callback) {
    62                 call_user_func_array($callback, $parameters);
     91                $callback(...$parameters);
    6392            }
    6493        }
Note: See TracChangeset for help on using the changeset viewer.