Changeset 52328 for trunk/src/wp-includes/Requests/Hooks.php
- Timestamp:
- 12/06/2021 09:29:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Hooks.php
r52244 r52328 3 3 * Handles adding and dispatching events 4 4 * 5 * @package Requests\EventDispatcher 5 * @package Requests 6 * @subpackage Utilities 6 7 */ 7 8 namespace WpOrg\Requests;9 10 use WpOrg\Requests\Exception\InvalidArgument;11 use WpOrg\Requests\HookManager;12 use WpOrg\Requests\Utility\InputValidator;13 8 14 9 /** 15 10 * Handles adding and dispatching events 16 11 * 17 * @package Requests\EventDispatcher 12 * @package Requests 13 * @subpackage Utilities 18 14 */ 19 class Hooks implements HookManager {15 class Requests_Hooks implements Requests_Hooker { 20 16 /** 21 17 * Registered callbacks for each hook … … 23 19 * @var array 24 20 */ 25 protected $hooks = []; 21 protected $hooks = array(); 22 23 /** 24 * Constructor 25 */ 26 public function __construct() { 27 // pass 28 } 26 29 27 30 /** … … 29 32 * 30 33 * @param string $hook Hook name 31 * @param call able$callback Function/method to call on event34 * @param callback $callback Function/method to call on event 32 35 * @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.36 36 */ 37 37 public function register($hook, $callback, $priority = 0) { 38 if ( is_string($hook) === false) {39 throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));38 if (!isset($this->hooks[$hook])) { 39 $this->hooks[$hook] = array(); 40 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 50 if (!isset($this->hooks[$hook])) { 51 $this->hooks[$hook] = [ 52 $priority => [], 53 ]; 54 } elseif (!isset($this->hooks[$hook][$priority])) { 55 $this->hooks[$hook][$priority] = []; 41 if (!isset($this->hooks[$hook][$priority])) { 42 $this->hooks[$hook][$priority] = array(); 56 43 } 57 44 … … 65 52 * @param array $parameters Parameters to pass to callbacks 66 53 * @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.69 54 */ 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 55 public function dispatch($hook, $parameters = array()) { 80 56 if (empty($this->hooks[$hook])) { 81 57 return false; 82 58 } 83 59 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 89 60 foreach ($this->hooks[$hook] as $priority => $hooked) { 90 61 foreach ($hooked as $callback) { 91 $callback(...$parameters);62 call_user_func_array($callback, $parameters); 92 63 } 93 64 }
Note: See TracChangeset
for help on using the changeset viewer.