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 +120 -37 1.1.41.2.2 View file →
@@ -12,10 +12,10 @@
12 12 static $categorized_cookies = array();
13 13
14 14 static function enqueue_scripts(){
15 15 global $wpdb;
16 -
17 - $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
16 +
17 + $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
18 18 $policy = cookieadmin_load_policy();
19 19 $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
20 20 //cookieadmin_r_print($view);
21 21 //cookieadmin_r_print($policy);
@@ -23,9 +23,17 @@
23 23 if(!empty($policy) && !empty($view) && !cookieadmin_is_editor_mode()){
24 24
25 25 wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/consent.css', [], COOKIEADMIN_VERSION);
26 26
27 - wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', [], COOKIEADMIN_VERSION, 'async');
27 + $js_deps = [];
28 + // Free consent.js is the base script from where the functionality gets triggered
29 + // So we need to make sure the dependencies of free script gets loaded first
30 + // Like the pro/consent.js is a dependency of the free one.
31 + if(defined('COOKIEADMIN_PREMIUM')){
32 + $js_deps[] = 'cookieadmin_pro_js';
33 + }
34 +
35 + wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', $js_deps, COOKIEADMIN_VERSION);
28 36
29 37 $policy[$view]['ajax_url'] = admin_url('admin-ajax.php');
30 38 $policy[$view]['nonce'] = wp_create_nonce('cookieadmin_js_nonce');
31 39 $policy[$view]['http_cookies'] = self::$http_cookies;
@@ -39,12 +47,13 @@
39 47
40 48 // Used for setting cookie
41 49 $policy[$view]['base_path'] = $base_path;
42 50
43 - $policy[$view]['lang']['show_more'] = __('show more', 'cookieadmin');
44 - $policy[$view]['lang']['show_less'] = __('show less', 'cookieadmin');
51 + // NOTE: Check the polylang string registration if changing these
52 + $policy[$view]['lang']['show_less'] = __('Show less', 'cookieadmin');
45 53 $policy[$view]['lang']['duration'] = __('Duration', 'cookieadmin');
46 54 $policy[$view]['lang']['session'] = __('Session', 'cookieadmin');
55 + $policy[$view]['lang']['days'] = __('Days', 'cookieadmin');
47 56
48 57 // cookieadmin_r_print($policy);die();
49 58
50 59 $rows = $wpdb->get_results("SELECT cookie_name, category, expires, description, patterns FROM {$table_name}");
@@ -55,8 +64,10 @@
55 64 }
56 65
57 66 $policy[$view]['categorized_cookies'] = self::$categorized_cookies = $cookie_data;
58 67
68 + $policy[$view] = apply_filters('cookieadmin_before_localize', $policy[$view]);
69 +
59 70 wp_localize_script('cookieadmin_js', 'cookieadmin_policy', $policy[$view]);
60 71
61 72 }
62 73 }
@@ -81,64 +92,136 @@
81 92
82 93 self::$http_cookies = $http_cookies;
83 94 } */
84 95
85 - static function check_if_cookies_allowed($tag, $handle, $src){
96 + static function block_scripts(){
86 97
98 + if(wp_doing_ajax() || is_admin() || defined('REST_REQUEST') || defined('COOKIEADMIN_SCANNER') || cookieadmin_is_editor_mode()){
99 + return;
100 + }
101 +
102 + $settings = get_option('cookieadmin_settings');
103 +
104 + if(empty($settings) || empty($settings['block_scripts'])){
105 + return;
106 + }
107 +
108 + $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
109 + $policy = cookieadmin_load_policy();
110 + if(empty($policy) || empty($view)){
111 + return;
112 + }
113 +
114 + ob_start([__CLASS__, 'update_tracking_scripts']);
115 + }
116 +
117 + static function update_tracking_scripts($html){
118 +
119 + if(stripos($html, '<script') === false){
120 + return $html;
121 + }
122 +
123 + if(empty(self::$categorized_cookies)){
124 + return $html;
125 + }
126 +
87 127 $cookieadmin_consent = isset($_COOKIE['cookieadmin_consent'])
88 128 ? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true)
89 129 : [];
90 130
131 + // Sanitizing cookies
91 132 array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) {
92 133 $sanitized_key = sanitize_key( $key );
93 134 $cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value);
94 135 } );
95 -
96 - foreach (self::$categorized_cookies as $item) {
97 - $category = strtolower($item->category);
98 - $patterns = json_decode($item->patterns, true);
99 -
100 - if (!empty($patterns) && !empty($category)) {
101 - foreach ($patterns as $pattern) {
102 - if (strpos($src, $pattern) !== false) {
103 -
104 - if ( $category !== 'necessary' &&
136 +
137 + $html = preg_replace_callback(
138 + '/<script\b([^>]*)>([\s\S]*?)<\/script>/i',
139 + function($match) use ($cookieadmin_consent){
140 + $attrs = $match[1];
141 + $content = $match[2];
142 + $full_tag = $match[0];
143 +
144 + if(preg_match('/\btype\s*=\s*["\']text\/plain["\']/i', $attrs)){
145 + return $full_tag;
146 + }
147 +
148 + if(preg_match('/\b(id|src)\s*=\s*["\'][^"\']*cookieadmin[^"\']*["\']/i', $attrs)){
149 + return $full_tag;
150 + }
151 +
152 + if(preg_match('/\btype\s*=\s*["\']([^"\']+)["\']/i', $attrs, $type_match)){
153 + $type = strtolower(trim($type_match[1]));
154 + if($type !== 'text/javascript' && $type !== 'module'){
155 + return $full_tag;
156 + }
157 + }
158 +
159 + $src = '';
160 + if(preg_match('/\bsrc\s*=\s*["\']([^"\']*)["\']/i', $attrs, $src_match)){
161 + $src = $src_match[1];
162 + }
163 +
164 + $match_against = !empty($src) ? $src : trim($attrs . ' ' . $content);
165 +
166 + if(empty($match_against)){
167 + return $full_tag;
168 + }
169 +
170 + foreach (self::$categorized_cookies as $item) {
171 + $category = !empty($item->category) ? strtolower($item->category) : '';
172 + $patterns = !empty($item->patterns) ? json_decode($item->patterns, true) : '';
173 +
174 + if(empty($patterns) || empty($category)){
175 + continue;
176 + }
177 +
178 + foreach ($patterns as $pattern) {
179 + if(strpos($match_against, $pattern) !== false){
180 + if($category !== 'necessary' &&
105 181 (empty($cookieadmin_consent) ||
106 182 (!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') ||
107 183 (!empty($cookieadmin_consent['reject']) && $cookieadmin_consent['reject'] == 'true')
108 184 )
109 - ) {
110 -
111 - // User has NOT consented -> block the script
112 -
113 - // Option 1 - completely remove script:
114 - // return '';
115 -
116 - // Option 2 - transform to type="text/plain"
117 - $tag = str_replace(
118 - '<script ',
119 - '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '" ',
120 - $tag
121 - );
122 -
123 - return $tag;
185 + ){
186 + if($attrs === ''){
187 + return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '">' . $content . '</script>';
188 + }
189 + return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '"' . $attrs . '>' . $content . '</script>';
190 + }
124 191 }
125 192 }
126 193 }
127 - }
128 - }
129 194
130 - return $tag;
195 + return $full_tag;
196 + },
197 + $html
198 + );
199 +
200 + return $html;
131 201 }
132 202
133 203 static function cookieadmin_show_banner(){
204 +
205 + $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
206 + $policy = cookieadmin_load_policy();
207 +
208 + // Do not show banner if banner is off via Geo rule
209 + if(empty($view)){
210 + return;
211 + }
134 212
135 - $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
136 - $policy = cookieadmin_load_policy();
213 + $raw_template = cookieadmin_load_consent_template($policy[$view], $view);
214 +
215 + if(!is_array($raw_template) || empty($raw_template)){
216 + return false;
217 + }
137 218
138 - $templates = implode("", cookieadmin_load_consent_template($policy[$view], $view));
219 + $templates = implode('', $raw_template);
139 220
140 221 $allowed_tags = cookieadmin_kses_allowed_html();
222 +
223 + $templates = apply_filters('cookieadmin_after_banner', $templates);
141 224
142 225 // var_dump($policy[$view]);
143 226 echo wp_kses($templates, $allowed_tags);
144 227 }