WP_Http_Cookie::__construct( string|array $data, string $requested_url = '' )
- Since
- 2.8.0, 5.2.0
- Source
wp-includes/class-wp-http-cookie.php:110
Description
The parameter $data should be either an associative array containing the indices names below or a header string detailing it.
Compatibility
- WordPress
- since 5.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$datastring|array- Raw cookie data as header string or data array.
$namestringCookie name.$valuemixedValue. Should NOT already be urlencoded.$expiresstring|int|nulldefault: nullOptional. Unix timestamp or formatted date.$pathstringdefault: '/'Optional. Path.$domainstringdefault: host of parsed $requested_urlOptional. Domain.$portint|stringdefault: nullOptional. Port or comma-separated list of ports.$host_onlybooldefault: trueOptional. host-only storage flag.
$requested_urlstringoptional- The URL which the cookie was set on, used for default $domain and $port values.Default:
''
Performance profile
How much work a call to WP_Http_Cookie::__construct() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 21–61
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Http_Cookie::__construct() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($data) | 21–42 | str_ends_with() |
!is_string($data) | 25–46 | parse_url(), str_ends_with() |
!is_string($data) && isset($data) | 34–44 | str_ends_with(), strtotime() |
!is_string($data) && isset($data) | 38–48 | parse_url(), str_ends_with(), strtotime() |
is_string($data) | 47–57 | str_ends_with(), explode(), urldecode(), array_shift() |
is_string($data) | 51–61 | parse_url(), str_ends_with(), explode(), urldecode(), array_shift() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 126 | 21–61 | 16 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 127 | 21–61 | 16 | |
| 8.4 | 127 | 21–61 | 16 | 23 fewer instructions than PHP 8.3 |
| 8.3 | 150 | 21–79 | 16 | |
| 8.2 | 150 | 21–79 | 16 | |
| 8.1 | 150 | 21–79 | 16 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 154 | 22–81 | 16 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
Used by · 3
- WP_HTTP_Requests_Response::get_cookies()Retrieves cookies from the response.
- WP_Http::buildCookieHeader()Takes the arguments for a ::request() and checks for the cookie array.
- WP_Http::processHeaders()Transforms header string into an array.
Source code
public function __construct( $data, $requested_url = '' ) { if ( $requested_url ) { $parsed_url = parse_url( $requested_url ); } if ( isset( $parsed_url['host'] ) ) { $this->domain = $parsed_url['host']; } $this->path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '/'; if ( ! str_ends_with( $this->path, '/' ) ) { $this->path = dirname( $this->path ) . '/'; } if ( is_string( $data ) ) { // Assume it's a header string direct from a previous request. $pairs = explode( ';', $data ); // Special handling for first pair; name=value. Also be careful of "=" in value. $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) ); $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 ); $this->name = $name; $this->value = urldecode( $value ); // Removes name=value from items. array_shift( $pairs ); // Set everything else as a property. foreach ( $pairs as $pair ) { $pair = rtrim( $pair ); // Handle the cookie ending in ; which results in an empty final pair. if ( empty( $pair ) ) { continue; } list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' ); $key = strtolower( trim( $key ) ); if ( 'expires' === $key ) { $val = strtotime( $val ); } $this->$key = $val; } } else { if ( ! isset( $data['name'] ) ) { return; } // Set properties based directly on parameters. foreach ( array( 'name', 'value', 'path', 'domain', 'port', 'host_only' ) as $field ) { if ( isset( $data[ $field ] ) ) { $this->$field = $data[ $field ]; } } if ( isset( $data['expires'] ) ) { $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] ); } else { $this->expires = null; } } }Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
host_only to the $data parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-http-cookie.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.