PluginProbe
Polylang / 3.4.4
Polylang v3.4.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / cookie.php

cookie.php in Polylang 3.4.4, at include/cookie.php

87 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * A class to manage manage the language cookie
8 *
9 * @since 2.9
10 */
11 class PLL_Cookie {
12 /**
13 * Parses the cookie parameters.
14 *
15 * @since 2.9
16 *
17 * @param array $args {@see PLL_Cookie::set()}
18 * @return array
19 */
20 protected static function parse_args( $args ) {
21 /**
22 * Filters the Polylang cookie duration.
23 *
24 * If a cookie duration of 0 is specified, a session cookie will be set.
25 * If a negative cookie duration is specified, the cookie is removed.
26 * /!\ This filter may be fired *before* the theme is loaded.
27 *
28 * @since 1.8
29 *
30 * @param int $duration Cookie duration in seconds.
31 */
32 $expiration = (int) apply_filters( 'pll_cookie_expiration', YEAR_IN_SECONDS );
33
34 $defaults = array(
35 'expires' => 0 !== $expiration ? time() + $expiration : 0,
36 'path' => COOKIEPATH,
37 'domain' => COOKIE_DOMAIN, // Cookie domain must be set to false for localhost ( default value for COOKIE_DOMAIN ) thanks to Stephen Harris.
38 'secure' => is_ssl(),
39 'httponly' => false,
40 'samesite' => 'Lax',
41 );
42
43 return wp_parse_args( $args, $defaults );
44 }
45
46 /**
47 * Sets the cookie.
48 *
49 * @since 2.9
50 *
51 * @param string $lang Language cookie value.
52 * @param array $args {
53 * Optional. Array of arguments for setting the cookie.
54 *
55 * @type string $path Cookie path, defaults to COOKIEPATH.
56 * @type string $domain Cookie domain, defaults to COOKIE_DOMAIN
57 * @type bool $secure Should the cookie be sent only over https?
58 * @type bool $httponly Should the cookie accessed only over http protocol? Defaults to false.
59 * @type string $samesite Either 'Strict', 'Lax' or 'None', defaults to 'Lax'.
60 * }
61 * @return void
62 */
63 public static function set( $lang, $args = array() ) {
64 $args = self::parse_args( $args );
65
66 if ( ! headers_sent() && PLL_COOKIE !== false && self::get() !== $lang ) {
67 if ( version_compare( PHP_VERSION, '7.3', '<' ) ) {
68 $args['path'] .= '; SameSite=' . $args['samesite']; // Hack to set SameSite value in PHP < 7.3. Doesn't work with newer versions.
69 setcookie( PLL_COOKIE, $lang, $args['expires'], $args['path'], $args['domain'], $args['secure'], $args['httponly'] );
70 } else {
71 setcookie( PLL_COOKIE, $lang, $args );
72 }
73 }
74 }
75
76 /**
77 * Returns the language cookie value.
78 *
79 * @since 2.9
80 *
81 * @return string
82 */
83 public static function get() {
84 return isset( $_COOKIE[ PLL_COOKIE ] ) ? sanitize_key( $_COOKIE[ PLL_COOKIE ] ) : '';
85 }
86 }
87