| 8412 | |
| 8413 | /** |
| 8414 | * Sets a cookie. |
| 8415 | * |
| 8416 | * @since 6.0.0 |
| 8417 | * |
| 8418 | * @param string $name Cookie name. |
| 8419 | * @param string $value Cookie value, defaults to empty string. |
| 8420 | * @param array $args { |
| 8421 | * Optional. Array of arguments for setting the cookie. |
| 8422 | * |
| 8423 | * @type string $path Cookie path, defaults to COOKIEPATH. |
| 8424 | * @type string $domain Cookie domain, defaults to COOKIE_DOMAIN. |
| 8425 | * @type bool $secure Should the cookie be sent only over https? |
| 8426 | * @type bool $httponly Should the cookie be accessed only over http protocol? Defaults to false. |
| 8427 | * @type string $samesite Either 'Strict', 'Lax' or 'None', defaults to 'Lax'. |
| 8428 | * } |
| 8429 | * @return bool |
| 8430 | */ |
| 8431 | function wp_set_cookie( $name, $value = '', $args ) { |
| 8432 | if ( headers_sent() ) { |
| 8433 | return false; |
| 8434 | } |
| 8435 | |
| 8436 | $defaults = array( |
| 8437 | 'expires' => 0, |
| 8438 | 'path' => COOKIEPATH, |
| 8439 | 'domain' => COOKIE_DOMAIN, |
| 8440 | 'secure' => is_ssl(), |
| 8441 | 'httponly' => false, |
| 8442 | 'samesite' => 'Lax', |
| 8443 | ); |
| 8444 | |
| 8445 | $args = wp_parse_args( $args, $defaults ); |
| 8446 | |
| 8447 | /** |
| 8448 | * Filters wp_set_cookie() arguments. |
| 8449 | * |
| 8450 | * @since 6.0.0 |
| 8451 | * |
| 8452 | * @param array $args { |
| 8453 | * Optional. Array of arguments for setting the cookie. |
| 8454 | * |
| 8455 | * @type string $path Cookie path. |
| 8456 | * @type string $domain Cookie domain. |
| 8457 | * @type bool $secure Should the cookie be sent only over https? |
| 8458 | * @type bool $httponly Should the cookie be accessed only over http protocol?. |
| 8459 | * @type string $samesite Either 'Strict', 'Lax' or 'None'. |
| 8460 | * } |
| 8461 | * @param string $name Cookie name. |
| 8462 | * @param string $value Cookie value, defaults to empty string. |
| 8463 | */ |
| 8464 | $args = apply_filters( 'wp_set_cookie_args', $args, $name, $value ); |
| 8465 | |
| 8466 | if ( version_compare( PHP_VERSION, '7.3', '<' ) ) { |
| 8467 | $args['path'] .= '; SameSite=' . $args['samesite']; // Hack to set SameSite value in PHP < 7.3. Doesn't work with newer versions. |
| 8468 | return setcookie( $name, $value, $args['expires'], $args['path'], $args['domain'], $args['secure'], $args['httponly'] ); |
| 8469 | } else { |
| 8470 | return setcookie( $name, $value, $args ); |
| 8471 | } |
| 8472 | } |