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