PluginProbe
Polylang / 3.7.5
Polylang v3.7.5
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.7.5, at include/cookie.php

108 lines 3.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 $args = wp_parse_args( $args, $defaults );
44
45 /**
46 * Filters the Polylang cookie arguments.
47 * /!\ This filter may be fired *before* the theme is loaded.
48 *
49 * @since 3.6
50 *
51 * @param array $args {
52 * Optional. Array of arguments for setting the cookie.
53 *
54 * @type int $expires Cookie duration.
55 * If a cookie duration of 0 is specified, a session cookie will be set.
56 * If a negative cookie duration is specified, the cookie is removed.
57 * @type string $path Cookie path.
58 * @type string $domain Cookie domain. Must be set to false for localhost (default value for `COOKIE_DOMAIN`).
59 * @type bool $secure Should the cookie be sent only over https?
60 * @type bool $httponly Should the cookie be accessed only over http protocol?.
61 * @type string $samesite Either 'Strict', 'Lax' or 'None'.
62 * }
63 */
64 return (array) apply_filters( 'pll_cookie_args', $args );
65 }
66
67 /**
68 * Sets the cookie.
69 *
70 * @since 2.9
71 *
72 * @param string $lang Language cookie value.
73 * @param array $args {
74 * Optional. Array of arguments for setting the cookie.
75 *
76 * @type string $path Cookie path, defaults to COOKIEPATH.
77 * @type string $domain Cookie domain, defaults to COOKIE_DOMAIN
78 * @type bool $secure Should the cookie be sent only over https?
79 * @type bool $httponly Should the cookie accessed only over http protocol? Defaults to false.
80 * @type string $samesite Either 'Strict', 'Lax' or 'None', defaults to 'Lax'.
81 * }
82 * @return void
83 */
84 public static function set( $lang, $args = array() ) {
85 $args = self::parse_args( $args );
86
87 if ( ! headers_sent() && PLL_COOKIE !== false && self::get() !== $lang ) {
88 if ( version_compare( PHP_VERSION, '7.3', '<' ) ) {
89 $args['path'] .= '; SameSite=' . $args['samesite']; // Hack to set SameSite value in PHP < 7.3. Doesn't work with newer versions.
90 setcookie( PLL_COOKIE, $lang, $args['expires'], $args['path'], $args['domain'], $args['secure'], $args['httponly'] );
91 } else {
92 setcookie( PLL_COOKIE, $lang, $args );
93 }
94 }
95 }
96
97 /**
98 * Returns the language cookie value.
99 *
100 * @since 2.9
101 *
102 * @return string
103 */
104 public static function get() {
105 return isset( $_COOKIE[ PLL_COOKIE ] ) ? sanitize_key( $_COOKIE[ PLL_COOKIE ] ) : '';
106 }
107 }
108