forked from humanmade/wp-consent-level-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie-info.php
121 lines (109 loc) · 4.38 KB
/
cookie-info.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php // phpcs:ignore WordPress.Files.Filename.InvalidClassFileName
/**
* This file is part of WP Consent API.
*
* Copyright 2020 Rogier Lankhorst and the WordPress Core Privacy team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* @package wordpress/consent-api
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/
// Check that the file is not accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
die( 'We\'re sorry, but you can not directly access this file.' );
}
if ( ! class_exists( 'WP_CONSENT_API_COOKIE_INFO' ) ) {
/**
* A class implementing the WP Consent API cookie handling.
*
* @since 1.0.0
*/
class WP_CONSENT_API_COOKIE_INFO {
/**
* An array of information about registered cookies.
*
* @var array
*/
public $registered_cookies;
/**
* The Singleton.
*
* @var self
*/
private static $instance;
/**
* Creates a new instance.
*/
public function __construct() {
if ( isset( self::$instance ) ) {
// translators: %s the name of the PHP Class used.
wp_die( esc_html( sprintf( __( '%s is a singleton class and you cannot create a second instance.', 'wp-consent-api' ), get_class( $this ) ) ) );
}
self::$instance = $this;
}
/**
* Retrieves the current instance.
*
* @return self
*/
public static function this() {
return self::$instance;
}
/**
* Wrapper function for the registration of a cookie with WordPress.
*
* @param string $name The name of the cookie.
* @param string $plugin_or_service Plugin or service that sets cookie (e.g. Google Maps).
* @param string $category One of 'functional', 'preferences', 'statistics-anonymous', 'statistics', or 'marketing'.
* @param string $expires Time until the cookie expires.
* @param string $function What the cookie is meant to do (e.g. 'Store a unique User ID').
* @param string $collected_personal_data Type of personal data that is collected. Only needs to be filled in if `$is_personal_data` is `true`.
* @param bool $member_cookie Whether the cookie is relevant for members of the site only.
* @param bool $administrator_cookie Whether the cookie is relevant for administrators only.
* @param string $type One of 'HTTP', 'LOCALSTORAGE', or 'API'.
* @param string $domain Optional. Domain on which the cookie is set. Defaults to the current site URL.
*/
public function add_cookie_info( $name, $plugin_or_service, $category, $expires, $function, $collected_personal_data = '', $member_cookie = false, $administrator_cookie = false, $type = 'HTTP', $domain = '' ) {
// If the domain is not passed, we assume it's first party, from this domain.
if ( empty( $domain ) ) {
$domain = site_url();
}
$this->registered_cookies[ $name ] = array(
'plugin_or_service' => sanitize_text_field( $plugin_or_service ),
'category' => wp_validate_consent_category( $category ),
'expires' => sanitize_text_field( $expires ),
'function' => sanitize_text_field( $function ),
'collectedPersonalData' => sanitize_text_field( $collected_personal_data ),
'memberCookie' => (bool) $member_cookie,
'administratorCookie' => (bool) $administrator_cookie,
'domain' => esc_url_raw( $domain ),
'type' => sanitize_text_field( $type ),
);
}
/**
* Get cookie info for one specific cookie, or for all cookies registered.
*
* @param string|bool $name The name of the cookie.
*
* @return array
*/
public function get_cookie_info( $name = false ) {
if ( $name && isset( $this->registered_cookies[ $name ] ) ) {
return $this->registered_cookies[ $name ];
}
return $this->registered_cookies;
}
}
}