PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 3.8.0
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v3.8.0
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / addons / lib / helper.php
cookiebot / addons / lib Last commit date
buffer 5 years ago ioc 6 years ago script-loader-tag 7 years ago autoloader.php 7 years ago cookie-consent-interface.php 7 years ago cookie-consent.php 7 years ago helper.php 5 years ago settings-service-interface.php 5 years ago settings-service.php 5 years ago
helper.php
325 lines
1 <?php
2 /**
3 * Check if a cache plugin is activated and in function.
4 *
5 * @return boolean True If attributes always should be added
6 * False If attributes only should be added if consent no given
7 */
8
9 function cookiebot_addons_enabled_cache_plugin() {
10 if( defined( "WP_ROCKET_PATH" ) ) {
11 return true; //WP Rocket - We need to ensure we not cache tags without attributes
12 }
13 if( defined( "W3TC" ) ) {
14 return true; //W3 Total Cache
15 }
16 if( defined( "WPCACHEHOME" ) ) {
17 return true; //WP Super Cache
18 }
19 if( defined( "WPFC_WP_PLUGIN_DIR" ) ) {
20 return true; //WP Fastest Cache
21 }
22 if( defined( "LSCWP_CONTENT_DIR" ) ) {
23 return true; //Litespeed Cache
24 }
25 return false;
26 }
27
28
29 /**
30 * Removes action with class in callback
31 *
32 * @param $action string action name
33 * @param $class string class name
34 * @param $method string method name
35 * @param $priority integer action priority number
36 *
37 * @return boolean True if the action hook is deleted
38 * False If the action hook is not deleted
39 *
40 * @since 1.2.0
41 */
42 function cookiebot_addons_remove_class_action( $action, $class, $method, $priority = 10 ) {
43 global $wp_filter;
44 $deleted = false;
45
46 if ( isset( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ][ $priority ] ) ) {
47 $len = strlen( $method );
48 foreach ( $wp_filter[ $action ][ $priority ] as $name => $def ) {
49 if ( substr( $name, - $len ) == $method ) {
50 if ( is_array( $def['function'] ) ) {
51 if ( is_string( $def['function'][0] ) !== false ) {
52 $def_class = $def['function'][0];
53 } else {
54 $def_class = get_class( $def['function'][0] );
55 }
56
57 if ( $def_class == $class ) {
58 if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) {
59 $wp_filter[ $action ]->remove_filter( $action, $name, $priority );
60 $deleted = true;
61 } else {
62 unset( $wp_filter[ $action ][ $priority ][ $name ] );
63 $deleted = true;
64 }
65 }
66 }
67 }
68 }
69 }
70
71 return $deleted;
72 }
73
74 /**
75 * Custom manipulation of the script
76 *
77 * @param $buffer
78 * @param $keywords
79 * @param $cookie_type
80 *
81 * @return mixed|null|string|string[]
82 *
83 * @version 2.0.4
84 * @since 1.2.0
85 */
86 function cookiebot_addons_manipulate_script( $buffer, $keywords ) {
87 /**
88 * normalize potential self-closing script tags
89 */
90
91 $normalized_buffer = preg_replace('/(<script(.*?)\/>)/is', '<script$2></script>', $buffer);
92
93 if($normalized_buffer !== null) {
94 $buffer = $normalized_buffer;
95 }
96
97
98
99 /**
100 * Pattern to get all scripts
101 *
102 * @version 2.0.4
103 * @since 1.2.0
104 */
105 $pattern = '/(<script(?:.*?)>)(.*?)(<\/script>)/is';
106
107 /**
108 * Get all scripts and add cookieconsent if it does match with the criterion
109 */
110 $updated_scripts = preg_replace_callback( $pattern, function ( $matches ) use ( $keywords ) {
111
112 $script = $matches[0]; // the full script html
113 $script_tag_open = $matches[1]; // only the script open tag with all attributes
114 $script_tag_inner = $matches[2]; // only the script's innerText
115 $script_tag_close = $matches[3]; // only the script closing tag
116
117 /**
118 * Check if the script contains the keywords, checks keywords one by one
119 *
120 * If one match, then the rest of the keywords will be skipped.
121 **/
122 foreach ( $keywords as $needle => $cookie_type ) {
123 /**
124 * The script contains the needle
125 **/
126 if ( strpos( $script, $needle ) !== false ) {
127 /**
128 * replace all single quotes with double quotes in the open tag
129 * remove previously set data-cookieconsent attribute
130 * remove type attribute
131 */
132 $script_tag_open = preg_replace('/\'/', '"', $script_tag_open);
133 $script_tag_open = preg_replace('/\sdata-cookieconsent=\"(?:.*?)\"/', '', $script_tag_open);
134 $script_tag_open = preg_replace( '/\stype=\"(?:.*?)\"/', '', $script_tag_open );
135
136 /**
137 * set the type attribute to text/plain to prevent javascript execution
138 * add data-cookieconsent attribute
139 */
140 $cookie_types = cookiebot_addons_output_cookie_types( $cookie_type );
141 $replacement = '<script type="text/plain" data-cookieconsent="' . $cookie_types . '"';
142 $script_tag_open = preg_replace( '/<script/', $replacement, $script_tag_open );
143
144 /**
145 * reconstruct the script and break the foreach loop
146 */
147 $script = $script_tag_open . $script_tag_inner . $script_tag_close;
148 continue;
149 }
150 }
151
152 /**
153 * return the reconstructed script
154 */
155 return $script;
156 }, $buffer );
157
158 /**
159 * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT
160 *
161 * @version 2.0.4
162 * @since 2.0.4
163 */
164 if ( $updated_scripts === null ) {
165 $updated_scripts = $buffer;
166
167 if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) {
168 update_option( 'cookiebot_regex_stacklimit', 1 );
169 }
170 }
171
172 return $updated_scripts;
173 }
174
175 /**
176 * Compares array to string to add checked attribute in checkbox
177 *
178 * @param $helper
179 * @param $current
180 * @param bool $echo
181 * @param string $type
182 *
183 * @return string
184 *
185 * @since 1.3.0
186 */
187 function cookiebot_addons_checked_selected_helper( $helper, $current, $echo = true, $type = 'checked' ) {
188 if ( is_array( $helper ) && in_array( $current, $helper ) ) {
189 $result = " $type='$type'";
190 } elseif ( is_string( $helper ) && is_string( $current ) && $helper === $current ) {
191 $result = " $type='$type'";
192 } else {
193 $result = '';
194 }
195
196
197 if ( $echo ) {
198 echo $result;
199 }
200
201 return $result;
202 }
203
204 /**
205 * Returns cookie types in a string
206 * Default is statistics
207 *
208 * @param $cookie_types
209 *
210 * @return string
211 *
212 * @since 1.3.0
213 */
214 function cookiebot_addons_output_cookie_types( $cookie_types ) {
215 if ( is_array( $cookie_types ) && count( $cookie_types ) > 0 ) {
216 return implode( ', ', $cookie_types );
217 } elseif ( is_string( $cookie_types ) && $cookie_types != '' ) {
218 return $cookie_types;
219 }
220
221 return 'statistics';
222 }
223
224 /**
225 * Return 1 cookie type if more than 1 is selected
226 *
227 * @param $cookie_types
228 *
229 * @return string
230 *
231 * @since 1.3.0
232 */
233 function cookiebot_addons_get_one_cookie_type( $cookie_types ) {
234 if ( is_array( $cookie_types ) ) {
235 if ( in_array( 'marketing', $cookie_types ) ) {
236 return 'marketing';
237 } elseif ( in_array( 'statistics', $cookie_types ) ) {
238 return 'statistics';
239 } elseif ( in_array( 'preferences', $cookie_types ) ) {
240 return 'preferences';
241 }
242 }
243
244 return '';
245 }
246
247 /**
248 * Returns current site language
249 *
250 * @return mixed|string
251 *
252 * @since 1.9.0
253 */
254 function cookiebot_addons_get_language() {
255 $lang = get_locale(); //Gets language in en-US format
256
257 /**
258 * Add support for 3rd party plugins
259 */
260 $lang = apply_filters( 'cookiebot_addons_language', $lang );
261
262 return $lang;
263 }
264
265 /**
266 * Get supported languages by the cookiebot
267 *
268 * @return array
269 *
270 * @since 1.9.0
271 */
272 function cookiebot_addons_get_supported_languages() {
273 $cookiebot = cookiebot();
274
275 return $cookiebot->get_supported_languages();
276 }
277
278 /**
279 * Show languages in a select field
280 *
281 * @param $class
282 * @param $name
283 * @param $selected
284 *
285 * @return mixed
286 *
287 * @since 1.8.0
288 */
289 function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) {
290 $args = array(
291 'name' => $name,
292 'selected' => $selected,
293 'show_option_site_default' => true,
294 'echo' => false,
295 'languages' => get_available_languages()
296 );
297 $dropdown = wp_dropdown_languages( $args );
298
299 $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown );
300
301 $output = str_replace( 'value="" ', 'value="en_US" ', $output );
302
303 return $output;
304 }
305
306 /**
307 * Run actions when the cookiebot plugin is deactivated
308 *
309 * @since 2.2.0
310 */
311 function cookiebot_addons_plugin_deactivated( ) {
312 $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance();
313 $cookiebot_addons->cookiebot_deactivated();
314 }
315
316 /**
317 * Run actions when the cookiebot plugin is deactivated
318 *
319 * @since 3.6.3
320 */
321 function cookiebot_addons_plugin_activated( ) {
322 $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance();
323 $cookiebot_addons->cookiebot_activated();
324 }
325