PluginProbe
CookieAdmin – Cookie Consent Banner / 1.2.2
CookieAdmin – Cookie Consent Banner v1.2.2
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
← All changes | includes/enduser.php +12 -212 trunk1.2.2 View file →
@@ -20,10 +20,8 @@
20 20 //cookieadmin_r_print($view);
21 21 //cookieadmin_r_print($policy);
22 22
23 23 if(!empty($policy) && !empty($view) && !cookieadmin_is_editor_mode()){
24 -
25 - add_filter('mepr_design_style_handle_prefixes', '\CookieAdmin\Enduser::memberpress_allow_style_prefix');
26 24
27 25 wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/consent.css', [], COOKIEADMIN_VERSION);
28 26
29 27 $js_deps = [];
@@ -73,210 +71,28 @@
73 71
74 72 }
75 73 }
76 74
77 - static function cookieadmin_block_cookie_init_php(){
75 + /* static function cookieadmin_block_cookie_init_php(){
78 76
79 - if(headers_sent() || (is_admin() && !wp_doing_ajax()) || defined('COOKIEADMIN_SCANNER') || cookieadmin_is_editor_mode()){
80 - return;
81 - }
82 -
83 - $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
84 -
85 - if(empty($view)){
86 - return;
87 - }
88 -
77 + //New - To catch, remove and send cookies in WP enqueue
89 78 $http_cookies = array();
90 - $set_cookie_headers = array();
79 + $headers = headers_list();
91 80
92 - foreach(headers_list() as $header) {
93 - if(stripos(trim($header), 'Set-Cookie:') === 0){
81 + foreach($headers as $header) {
82 +
83 + if (stripos(trim($header), 'Set-Cookie:') === 0) {
94 84 $header = trim(substr($header, strlen('Set-Cookie:')));
95 - $set_cookie_headers[] = $header;
85 + $name = trim(explode('=', $header)[0]);
86 + $http_cookies[$name]['string'] = trim($header);
87 + setcookie($name, '', time() - 999999, '/');
96 88 }
97 89 }
98 90
99 - if(empty($set_cookie_headers)){
100 - return;
101 - }
102 -
103 - $policy = cookieadmin_load_policy();
104 -
105 - if(empty($policy) || empty($policy[$view])){
106 - return;
107 - }
108 -
109 - $categories = self::get_cookie_categories();
110 - $consent = self::get_consent();
111 - $preload = self::get_preload_categories($policy, $view);
112 - $allowed_headers = array();
113 -
114 - foreach($set_cookie_headers as $set_cookie){
115 - $name = self::get_cookie_name_from_header($set_cookie);
116 -
117 - if(empty($name)){
118 - continue;
119 - }
120 -
121 - if(self::is_response_cookie_allowed($set_cookie, $categories, $consent, $preload)){
122 - $allowed_headers[] = $set_cookie;
123 - continue;
124 - }
125 -
126 - $http_cookies[$name]['string'] = trim($set_cookie);
127 - }
128 -
129 - header_remove('Set-Cookie');
130 -
131 - foreach($allowed_headers as $set_cookie){
132 - header('Set-Cookie: ' . $set_cookie, false);
133 - }
134 -
135 91 $http_cookies['cookieadmin_consent'] = ["string" => "cookieadmin_consent=CookieAdmin Cookie Initialization"];
136 92
137 93 self::$http_cookies = $http_cookies;
138 - }
139 -
140 - static function get_cookie_categories(){
141 - global $wpdb;
142 -
143 - $table_name = $wpdb->prefix . 'cookieadmin_cookies';
144 - if(!self::cookieadmin_table_exists($table_name)){
145 - return array();
146 - }
147 -
148 - $categories = array(
149 - 'exact' => array(),
150 - 'prefix' => array(),
151 - );
152 - $rows = $wpdb->get_results("SELECT cookie_name, raw_name, category FROM {$table_name}");
153 -
154 - foreach($rows as $row){
155 - if(!empty($row->cookie_name) && !empty($row->category)){
156 - $category = strtolower($row->category);
157 - $categories['exact'][$row->cookie_name] = $category;
158 -
159 - if(!empty($row->raw_name)){
160 - $categories['exact'][$row->raw_name] = $category;
161 -
162 - if($row->raw_name !== $row->cookie_name && strpos($row->raw_name, $row->cookie_name) === 0){
163 - $categories['prefix'][$row->cookie_name] = $category;
164 - }
165 - }
166 - }
167 - }
168 -
169 - uksort($categories['prefix'], function($a, $b){
170 - return strlen($b) - strlen($a);
171 - });
172 -
173 - return $categories;
174 - }
175 -
176 - static function get_consent(){
177 - if(empty($_COOKIE['cookieadmin_consent'])){
178 - return array();
179 - }
180 -
181 - $consent = json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true);
182 - if(!is_array($consent)){
183 - return array();
184 - }
185 -
186 - $sanitized = array();
187 - foreach($consent as $key => $value){
188 - $sanitized[sanitize_key($key)] = sanitize_text_field($value);
189 - }
190 -
191 - return $sanitized;
192 - }
193 -
194 - static function get_preload_categories($policy, $view){
195 - if(empty($policy) || empty($policy[$view]['preload']) || !is_array($policy[$view]['preload'])){
196 - return array();
197 - }
198 -
199 - $preload = array();
200 - foreach($policy[$view]['preload'] as $category){
201 - $preload[] = strtolower(sanitize_key($category));
202 - }
203 -
204 - return $preload;
205 - }
206 -
207 - static function get_cookie_name_from_header($set_cookie){
208 - $parts = explode('=', $set_cookie, 2);
209 - $name = trim($parts[0]);
210 -
211 - if(empty($name)){
212 - return '';
213 - }
214 -
215 - return $name;
216 - }
217 -
218 - static function is_response_cookie_allowed($set_cookie, $categories, $consent, $preload = array()){
219 - $name = self::get_cookie_name_from_header($set_cookie);
220 -
221 - if(empty($name)){
222 - return false;
223 - }
224 -
225 - // A deletion never adds a cookie and must not be prevented.
226 - if(preg_match('/(?:^|;)\s*max-age\s*=\s*(-?\d+)/i', $set_cookie, $age) && (int) $age[1] <= 0){
227 - return true;
228 - }
229 -
230 - if(preg_match('/(?:^|;)\s*expires\s*=\s*([^;]+)/i', $set_cookie, $expires)){
231 - $expires_at = strtotime($expires[1]);
232 - if($expires_at !== false && $expires_at < time()){
233 - return true;
234 - }
235 - }
236 -
237 - if($name === 'cookieadmin_consent'){
238 - return true;
239 - }
240 -
241 - $category = self::get_cookie_category($name, $categories);
242 -
243 - if(empty($category)){
244 - return !empty($consent['accept']) && $consent['accept'] === 'true';
245 - }
246 -
247 - if($category === 'necessary' || in_array($category, $preload, true)){
248 - return true;
249 - }
250 -
251 - if(!empty($consent['reject']) && $consent['reject'] === 'true'){
252 - return false;
253 - }
254 -
255 - if(!empty($consent['accept']) && $consent['accept'] === 'true'){
256 - return true;
257 - }
258 -
259 - return !empty($consent[$category]) && $consent[$category] === 'true';
260 - }
261 -
262 - static function get_cookie_category($name, $categories){
263 - if(!empty($categories['exact'][$name])){
264 - return $categories['exact'][$name];
265 - }
266 -
267 - if(empty($categories['prefix'])){
268 - return '';
269 - }
270 -
271 - foreach($categories['prefix'] as $prefix => $category){
272 - if(strpos($name, $prefix) === 0){
273 - return $category;
274 - }
275 - }
276 -
277 - return '';
278 - }
94 + } */
279 95
280 96 static function block_scripts(){
281 97
282 98 if(wp_doing_ajax() || is_admin() || defined('REST_REQUEST') || defined('COOKIEADMIN_SCANNER') || cookieadmin_is_editor_mode()){
@@ -298,9 +114,8 @@
298 114 ob_start([__CLASS__, 'update_tracking_scripts']);
299 115 }
300 116
301 117 static function update_tracking_scripts($html){
302 - global $cookieadmin_settings;
303 118
304 119 if(stripos($html, '<script') === false){
305 120 return $html;
306 121 }
@@ -320,9 +135,9 @@
320 135 } );
321 136
322 137 $html = preg_replace_callback(
323 138 '/<script\b([^>]*)>([\s\S]*?)<\/script>/i',
324 - function($match) use ($cookieadmin_consent, $cookieadmin_settings){
139 + function($match) use ($cookieadmin_consent){
325 140 $attrs = $match[1];
326 141 $content = $match[2];
327 142 $full_tag = $match[0];
328 143
@@ -350,19 +165,8 @@
350 165
351 166 if(empty($match_against)){
352 167 return $full_tag;
353 168 }
354 -
355 - if(!empty($cookieadmin_settings['cookieadmin_google_advance_consent_mode'])){
356 - // External Google tag loader
357 - if(!empty($src) && stripos($src, 'googletagmanager.com/gtag/js') !== false){
358 - return $full_tag;
359 - }
360 - // Inline gtag() snippet
361 - if(empty($src) && stripos($content, 'gtag(') !== false){
362 - return $full_tag;
363 - }
364 - }
365 169
366 170 foreach (self::$categorized_cookies as $item) {
367 171 $category = !empty($item->category) ? strtolower($item->category) : '';
368 172 $patterns = !empty($item->patterns) ? json_decode($item->patterns, true) : '';
@@ -428,10 +232,6 @@
428 232 $query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name);
429 233
430 234 return $wpdb->get_var($query) === $table_name;
431 235 }
432 -
433 - static function memberpress_allow_style_prefix($prefix_arr){
434 - $prefix_arr[] = 'cookieadmin';
435 - return $prefix_arr;
436 - }
437 236 }
237 +