WpOrgRequestsHookManager & WpOrgRequestsHooks
Event dispatcher system for the Requests library. HookManager is the interface; Hooks is the concrete implementation.
Source: wp-includes/Requests/src/HookManager.php, wp-includes/Requests/src/Hooks.php
Namespace: WpOrgRequests
HookManager (Interface)
interface HookManager {
public function register( string $hook, callable $callback, int $priority = 0 );
public function dispatch( string $hook, array $parameters = [] ): bool;
}register()
| Parameter | Type | Description |
|---|---|---|
$hook | string | Hook name |
$callback | callable | Callback to invoke |
$priority | int | Priority number. Negative = earlier, positive = later |
dispatch()
| Parameter | Type | Description |
|---|---|---|
$hook | string | Hook name |
$parameters | array | Parameters passed to callbacks |
Returns: bool — Success status.
Hooks (Implementation)
class Hooks implements HookManagerProperties
| Property | Type | Visibility | Description |
|---|---|---|---|
$hooks | array | protected | Registered callbacks: [ hook_name => [ priority => [ callbacks ] ] ] |
register()
public function register( string $hook, callable $callback, int $priority = 0 ): voidRegister a callback for a hook. Multiple callbacks can be registered at the same priority.
Throws:
InvalidArgument— When$hookis not a stringInvalidArgument— When$callbackis not callableInvalidArgument— When$priorityis not an integer
Storage structure:
$this->hooks['hook.name'][0][] = $callback; // priority 0
$this->hooks['hook.name'][-5][] = $callback; // priority -5 (runs first)dispatch()
public function dispatch( string $hook, array $parameters = [] ): boolDispatch a hook event. Callbacks are executed in priority order (sorted by ksort), then in registration order within the same priority.
Parameters are passed as positional arguments using the spread operator ($callback(...$parameters)). Array keys are stripped via array_values() to prevent them being interpreted as named parameters in PHP 8.0+.
Returns: false if no callbacks are registered for the hook, true after executing callbacks.
Throws:
InvalidArgument— When$hookis not a stringInvalidArgument— When$parametersis not an array (strict check —Array*objects are rejected)
__wakeup()
public function __wakeup(): voidThrows LogicException — Hooks should never be unserialized.
Dispatch Order Example
$hooks = new Hooks();
$hooks->register('my.event', $callbackA, 10); // runs third
$hooks->register('my.event', $callbackB, -5); // runs first
$hooks->register('my.event', $callbackC, 0); // runs second
$hooks->register('my.event', $callbackD, 10); // runs fourth (same priority as A, registered after)
$hooks->dispatch('my.event', [&$data]);
// Execution order: B (-5) → C (0) → A (10) → D (10)Usage in the Library
Hooks is automatically instantiated in Requests::set_defaults() when $options['hooks'] is empty. Auth handlers, proxy handlers, and cookie jars register themselves with the hooks system via their register() methods.
See Hook Points for all dispatched hooks.