WP_Http_Cookie::__construct()publicWP 2.8.0

Sets up this cookie object.

The parameter $data should be either an associative array containing the indices names below or a header string detailing it.

Метод класса: WP_Http_Cookie{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WP_Http_Cookie = new WP_Http_Cookie();
$WP_Http_Cookie->__construct( $data, $requested_url );
$data(строка|массив) (обязательный)

Raw cookie data as header string or data array.

  • name(строка)
    Cookie name.

  • value(разное)
    Value. Should NOT already be urlencoded.

  • expires(строка|int|null)
    Optional. Unix timestamp or formatted date.
    По умолчанию: null

  • path(строка)
    Optional. Path.
    По умолчанию: '/'

  • domain(строка)
    Optional. Domain.
    По умолчанию: host of parsed $requested_url

  • port(int|строка)
    Optional. Port or comma-separated list of ports.
    По умолчанию: null

  • host_only(true|false)
    Optional. host-only storage flag.
    По умолчанию: true
$requested_url(строка)
The URL which the cookie was set on, used for default $domain and $port values.
По умолчанию: ''

Список изменений

С версии 2.8.0 Введена.
С версии 5.2.0 Added host_only to the $data parameter.

Код WP_Http_Cookie::__construct() WP 6.5.2

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;
		}
	}
}