WpOrgRequestsCookieJar
Cookie collection that automatically handles sending cookies on requests and parsing cookies from responses.
Source: wp-includes/Requests/src/Cookie/Jar.php
Namespace: WpOrgRequestsCookie
Implements: ArrayAccess, IteratorAggregate
Properties
| Property | Type | Visibility | Default | Description |
|---|---|---|---|---|
$cookies | array | protected | [] | Stored cookie data |
Methods
__construct()
public function __construct( array $cookies = [] )Throws: InvalidArgument when $cookies is not an array.
normalize_cookie()
public function normalize_cookie( string|Cookie $cookie, string $key = '' ): CookieNormalize a cookie value into a Cookie object. If already a Cookie instance, returns it directly. Otherwise, parses the string via Cookie::parse().
ArrayAccess Methods
public function offsetExists( string $offset ): bool
public function offsetGet( string $offset ): string|null
public function offsetSet( string $offset, string $value ): void
public function offsetUnset( string $offset ): voidStandard dictionary access. offsetSet() throws Exception('invalidset') if $offset is null.
getIterator()
public function getIterator(): ArrayIteratorReturns an ArrayIterator over the cookies array.
register()
public function register( HookManager $hooks ): voidRegister the cookie handler with the request hook system. Registers two hooks:
| Hook | Callback | Purpose |
|---|---|---|
requests.before_request | $this->before_request | Add Cookie header to outgoing requests |
requests.before_redirect_check | $this->before_redirect_check | Parse Set-Cookie headers from responses |
before_request()
public function before_request( string $url, array &$headers, array &$data, string &$type, array &$options ): voidCalled before each request. Filters cookies by domain match and expiration, formats matching cookies as name=value pairs joined with '; ' (per RFC 6265), and sets the Cookie header.
Converts the URL to an Iri object for domain matching.
before_redirect_check()
public function before_redirect_check( Response $response ): voidCalled after each response. Parses Set-Cookie headers from the response via Cookie::parse_from_headers(), merges them into the jar, and sets $response->cookies to this jar instance.
Integration Flow
Request out:
register() hooks into before_request
→ before_request() adds Cookie header with matching, non-expired cookies
Response in:
register() hooks into before_redirect_check
→ before_redirect_check() parses Set-Cookie headers
→ Merges new cookies into jar
→ Sets $response->cookies = $this
Next request (redirect or new):
→ before_request() sends updated cookies