PluginProbe
CookieAdmin – Cookie Consent Banner / 1.0.8
CookieAdmin – Cookie Consent Banner v1.0.8
1.2.3 1.2.2 1.2.1 1.2.0 1.1.9 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
cookieadmin / includes / enduser.php

enduser.php in CookieAdmin – Cookie Consent Banner 1.0.8, at includes/enduser.php

234 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CookieAdmin;
4
5 if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){
6 die('Hacking Attempt');
7 }
8
9 class Enduser{
10
11 static $http_cookies = array();
12 static $categorized_cookies = array();
13
14 static function enqueue_scripts(){
15 global $wpdb;
16
17 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
18 $policy = cookieadmin_load_policy();
19 $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
20 //cookieadmin_r_print($view);
21 //cookieadmin_r_print($policy);
22
23 if(!empty($policy) && !empty($view)){
24
25 wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/consent.css', [], COOKIEADMIN_VERSION);
26
27 wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', [], COOKIEADMIN_VERSION, 'async');
28
29 $policy[$view]['ajax_url'] = admin_url('admin-ajax.php');
30 $policy[$view]['nonce'] = wp_create_nonce('cookieadmin_js_nonce');
31 $policy[$view]['http_cookies'] = self::$http_cookies;
32 $policy[$view]['home_url'] = home_url();
33 $policy[$view]['plugin_url'] = COOKIEADMIN_URL;
34 $policy[$view]['is_pro'] = (defined('COOKIEADMIN_PREMIUM') ? COOKIEADMIN_PREMIUM : 0);
35 $policy[$view]['ssl'] = is_ssl();
36
37 $base_path = parse_url(home_url(), PHP_URL_PATH) ?: '/';
38 $base_path = ($base_path !== '/') ? rtrim($base_path, '/') . '/' : '/';
39
40 // Used for setting cookie
41 $policy[$view]['base_path'] = $base_path;
42
43 // cookieadmin_r_print($policy);die();
44
45 $rows = $wpdb->get_results("SELECT cookie_name, category, expires, description, patterns FROM {$table_name}");
46 $cookie_data = array();
47
48 foreach ($rows as $row) {
49 $cookie_data[$row->cookie_name] = $row;
50 }
51
52 $policy[$view]['categorized_cookies'] = self::$categorized_cookies = $cookie_data;
53
54 wp_localize_script('cookieadmin_js', 'cookieadmin_policy', $policy[$view]);
55
56 }
57 }
58
59 /* static function cookieadmin_block_cookie_init_php(){
60
61 //New - To catch, remove and send cookies in WP enqueue
62 $http_cookies = array();
63 $headers = headers_list();
64
65 foreach($headers as $header) {
66
67 if (stripos(trim($header), 'Set-Cookie:') === 0) {
68 $header = trim(substr($header, strlen('Set-Cookie:')));
69 $name = trim(explode('=', $header)[0]);
70 $http_cookies[$name]['string'] = trim($header);
71 setcookie($name, '', time() - 999999, '/');
72 }
73 }
74
75 $http_cookies['cookieadmin_consent'] = ["string" => "cookieadmin_consent=CookieAdmin Cookie Initialization"];
76
77 self::$http_cookies = $http_cookies;
78 } */
79
80 static function check_if_cookies_allowed($tag, $handle, $src){
81
82 $cookieadmin_consent = isset($_COOKIE['cookieadmin_consent'])
83 ? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true)
84 : [];
85
86 array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) {
87 $sanitized_key = sanitize_key( $key );
88 $cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value);
89 } );
90
91 foreach (self::$categorized_cookies as $item) {
92 $category = strtolower($item->category);
93 $patterns = json_decode($item->patterns, true);
94
95 if (!empty($patterns) && !empty($category)) {
96 foreach ($patterns as $pattern) {
97 if (strpos($src, $pattern) !== false) {
98
99 if ( $category !== 'necessary' &&
100 (empty($cookieadmin_consent) ||
101 (!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') ||
102 $cookieadmin_consent['reject'] == 'true')
103 ) {
104
105 // User has NOT consented -> block the script
106
107 // Option 1 - completely remove script:
108 // return '';
109
110 // Option 2 - transform to type="text/plain"
111 $tag = str_replace(
112 '<script ',
113 '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '" ',
114 $tag
115 );
116
117 return $tag;
118 }
119 }
120 }
121 }
122 }
123
124 return $tag;
125 }
126
127 static function cookieadmin_show_banner(){
128
129 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
130 $policy = cookieadmin_load_policy();
131
132 $allowed_tags = wp_kses_allowed_html( 'post' );
133
134 // Add input tag for cookie consent form
135 $allowed_tags['input'] = array(
136 'type' => true,
137 'name' => true,
138 'value' => true,
139 'class' => true,
140 'id' => true,
141 'checked' => true,
142 'disabled' => true,
143 'placeholder' => true,
144 );
145
146 $templates = wp_kses(implode("", cookieadmin_load_consent_template($policy[$view], $view)), $allowed_tags);
147
148 // var_dump($policy[$view]);
149 echo $templates;
150 }
151
152 static function cookieadmin_table_exists($table_name) {
153 global $wpdb;
154
155 $query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name);
156
157 return $wpdb->get_var($query) === $table_name;
158 }
159
160 static function wp_head() {
161
162 $policy = cookieadmin_load_policy();
163
164 $law = get_option('cookieadmin_law', 'cookieadmin_gdpr');
165
166 $cookieadmin_default_allowed = (!empty($policy[$law]['preload']) ? $policy[$law]['preload'] : []);
167 $cookieadmin_default_categories = ['functional', 'analytics', 'marketing', 'accept', 'reject'];
168
169 $cookieadmin_js_preferences = [];
170 foreach ($cookieadmin_default_categories as $category) {
171 $cookieadmin_js_preferences[$category] = (!empty($cookieadmin_default_allowed) && in_array($category, $cookieadmin_default_allowed) ? true : false);
172 }
173
174 $cookieadmin_js_preferences_json = json_encode($cookieadmin_js_preferences);
175 $inline_js = "
176
177 window.dataLayer = window.dataLayer || [];
178 function gtag(){dataLayer.push(arguments);}
179
180 function cookieadmin_update_gcm(update) {
181
182 let cookieadmin_preferences = $cookieadmin_js_preferences_json;
183
184 const cookieAdminMatch = document.cookie.match(/(?:^|; )cookieadmin_consent=([^;]*)/);
185
186 if (cookieAdminMatch) {
187 try {
188 const cookieadmin_parsed = JSON.parse(decodeURIComponent(cookieAdminMatch[1]));
189 cookieadmin_preferences.functional = cookieadmin_parsed.functional === 'true';
190 cookieadmin_preferences.analytics = cookieadmin_parsed.analytics === 'true';
191 cookieadmin_preferences.marketing = cookieadmin_parsed.marketing === 'true';
192 cookieadmin_preferences.accept = cookieadmin_parsed.accept === 'true';
193 cookieadmin_preferences.reject = cookieadmin_parsed.reject === 'true';
194 } catch (err) {
195
196 }
197 }
198
199 if (typeof gtag === 'function') {
200
201 let cookieadmin_gtag_mode = update === 1 ? 'update' : 'default';
202
203 try {
204
205 gtag('consent', cookieadmin_gtag_mode, {
206 'ad_storage': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
207 'analytics_storage': cookieadmin_preferences.analytics || cookieadmin_preferences.accept ? 'granted' : 'denied',
208 'ad_user_data': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
209 'ad_personalization': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
210 'personalization_storage': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
211 'security_storage': 'granted',
212 'functionality_storage': 'granted'
213 });
214
215 } catch (e) {
216
217 }
218 }
219 }
220
221 cookieadmin_update_gcm(0);
222 ";
223
224 wp_register_script('cookieadmin-gcm', '', [], null, false);
225
226 wp_add_inline_script('cookieadmin-gcm', $inline_js);
227
228 wp_enqueue_script('cookieadmin-gcm');
229
230 }
231
232 }
233
234