PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.2.8
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.2.8
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 / src / lib / helper.php
cookiebot / src / lib Last commit date
buffer 3 years ago script_loader_tag 3 years ago traits 3 years ago Consent_API_Helper.php 3 years ago Cookie_Consent.php 3 years ago Cookie_Consent_Interface.php 4 years ago Cookiebot_Activated.php 3 years ago Cookiebot_Automatic_Updates.php 3 years ago Cookiebot_Deactivated.php 4 years ago Cookiebot_Javascript_Helper.php 3 years ago Cookiebot_WP.php 3 years ago Dependency_Container.php 3 years ago Settings_Page_Tab.php 3 years ago Settings_Service.php 3 years ago Settings_Service_Interface.php 3 years ago Supported_Languages.php 4 years ago Supported_Regions.php 3 years ago WP_Rocket_Helper.php 3 years ago Widgets.php 3 years ago global-deprecations.php 3 years ago helper.php 3 years ago
helper.php
611 lines
1 <?php
2
3 namespace cybot\cookiebot\lib {
4
5 use Exception;
6 use InvalidArgumentException;
7 use RuntimeException;
8
9 /**
10 * @param string $type
11 * @param string $deprecated_name
12 * @param string $alternative_name
13 */
14 function deprecation_error( $type, $deprecated_name, $alternative_name ) {
15 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
16 trigger_error(
17 esc_html( $type . ' `' . $deprecated_name . '` is deprecated. Use `' . $alternative_name . '` instead.' ),
18 E_USER_DEPRECATED
19 );
20 }
21
22 /**
23 * Check if a cache plugin is activated and in function.
24 *
25 * @return boolean True If attributes always should be added
26 * False If attributes only should be added if consent no given
27 */
28 function cookiebot_addons_enabled_cache_plugin() {
29 // WP Rocket - We need to ensure we not cache tags without attributes
30 if ( defined( 'WP_ROCKET_PATH' ) ||
31 // W3 Total Cache
32 defined( 'W3TC' ) ||
33 // WP Super Cache
34 defined( 'WPCACHEHOME' ) ||
35 // WP Fastest Cache
36 defined( 'WPFC_WP_PLUGIN_DIR' ) ||
37 // Litespeed Cache
38 defined( 'LSCWP_CONTENT_DIR' ) ) {
39 return true;
40 }
41
42 return false;
43 }
44
45
46 /**
47 * Removes action with class in callback
48 *
49 * @param $action string action name
50 * @param $class string class name
51 * @param $method string method name
52 * @param $priority integer action priority number
53 *
54 * @return boolean True if the action hook is deleted
55 * False If the action hook is not deleted
56 *
57 * @since 1.2.0
58 */
59 function cookiebot_addons_remove_class_action( $action, $class, $method, $priority = 10 ) {
60 global $wp_filter;
61 $deleted = false;
62
63 if ( isset( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ][ $priority ] ) ) {
64 $len = strlen( $method );
65 foreach ( $wp_filter[ $action ][ $priority ] as $name => $def ) {
66 if ( substr( $name, - $len ) === $method && is_array( $def['function'] ) ) {
67 if ( is_string( $def['function'][0] ) !== false ) {
68 $def_class = $def['function'][0];
69 } else {
70 $def_class = get_class( $def['function'][0] );
71 }
72
73 if ( $def_class === $class ) {
74 if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) {
75 $wp_filter[ $action ]->remove_filter( $action, $name, $priority );
76 } else {
77 unset( $wp_filter[ $action ][ $priority ][ $name ] );
78 }
79 $deleted = true;
80 }
81 }
82 }
83 }
84
85 return $deleted;
86 }
87
88 /**
89 * Custom manipulation of the script
90 *
91 * @param $buffer
92 * @param $keywords
93 *
94 * @return mixed|null|string|string[]
95 *
96 * @version 2.0.4
97 * @since 1.2.0
98 */
99 function cookiebot_addons_manipulate_script( $buffer, $keywords ) {
100 /**
101 * normalize potential self-closing script tags
102 */
103
104 $normalized_buffer = preg_replace( '/(<script(.*?)\/>)/is', '<script$2></script>', $buffer );
105
106 if ( $normalized_buffer !== null ) {
107 $buffer = $normalized_buffer;
108 }
109
110 /**
111 * Pattern to get all scripts
112 *
113 * @version 2.0.4
114 * @since 1.2.0
115 */
116 $pattern = '/(<script.*?>)(.*?)(<\/script>)/is';
117
118 /**
119 * Get all scripts and add cookieconsent if it does match with the criterion
120 */
121 $updated_scripts = preg_replace_callback(
122 $pattern,
123 function ( $matches ) use ( $keywords ) {
124 $script = $matches[0]; // the full script html
125 $script_tag_open = $matches[1]; // only the script open tag with all attributes
126 $script_tag_inner = $matches[2]; // only the script's innerText
127 $script_tag_close = $matches[3]; // only the script closing tag
128
129 /**
130 * Check if the script contains the keywords, checks keywords one by one
131 *
132 * If one match, then the rest of the keywords will be skipped.
133 */
134 foreach ( $keywords as $needle => $cookie_type ) {
135 /**
136 * The script contains the needle
137 */
138 if ( strpos( $script, $needle ) !== false ) {
139 /**
140 * replace all single quotes with double quotes in the open tag
141 * remove previously set data-cookieconsent attribute
142 * remove type attribute
143 */
144 $script_tag_open = str_replace( '\'', '"', $script_tag_open );
145 $script_tag_open = preg_replace( '/\sdata-cookieconsent=\".*?\"/', '', $script_tag_open );
146 $script_tag_open = preg_replace( '/\stype=\".*?\"/', '', $script_tag_open );
147
148 /**
149 * set the type attribute to text/plain to prevent javascript execution
150 * add data-cookieconsent attribute
151 */
152 $cookie_types = cookiebot_addons_output_cookie_types( $cookie_type );
153 $replacement = '<script type="text/plain" data-cookieconsent="' . $cookie_types . '"';
154 $script_tag_open = str_replace( '<script', $replacement, $script_tag_open );
155
156 /**
157 * reconstruct the script and break the foreach loop
158 */
159 $script = $script_tag_open . $script_tag_inner . $script_tag_close;
160 }
161 }
162
163 /**
164 * return the reconstructed script
165 */
166 return $script;
167 },
168 $buffer
169 );
170
171 /**
172 * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT
173 *
174 * @version 2.0.4
175 * @since 2.0.4
176 */
177 if ( $updated_scripts === null ) {
178 $updated_scripts = $buffer;
179
180 if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) {
181 update_option( 'cookiebot_regex_stacklimit', 1 );
182 }
183 }
184
185 return $updated_scripts;
186 }
187
188 /**
189 * @param array $cookie_types
190 * @param $cookie_type
191 */
192 function cookiebot_addons_checked_selected_helper( array $cookie_types, $cookie_type ) {
193 if ( in_array( $cookie_type, $cookie_types, true ) ) {
194 echo " checked='checked'";
195 }
196 }
197
198 /**
199 * Returns cookie types in a string
200 * Default is statistics
201 *
202 * @param $cookie_types
203 *
204 * @return string
205 *
206 * @since 1.3.0
207 * @version 3.9.1
208 */
209 function cookiebot_addons_output_cookie_types( $cookie_types ) {
210 if ( is_array( $cookie_types ) && count( $cookie_types ) > 0 ) {
211 return implode(
212 ', ',
213 array_map(
214 function ( $value ) {
215 return cookiebot_translate_type_name( $value );
216 },
217 $cookie_types
218 )
219 );
220 } elseif ( is_string( $cookie_types ) && ! empty( $cookie_types ) ) {
221 return cookiebot_translate_type_name( $cookie_types );
222 }
223
224 return cookiebot_translate_type_name( 'statistics' );
225 }
226
227 /**
228 * Translates the cookie type to different language
229 *
230 * @param $type string
231 *
232 * @return string
233 *
234 * @since 3.9.1
235 */
236 function cookiebot_translate_type_name( $type ) {
237 switch ( $type ) {
238 case 'marketing':
239 $translated_name = esc_html__( 'marketing', 'cookiebot' );
240 break;
241 case 'statistics':
242 $translated_name = esc_html__( 'statistics', 'cookiebot' );
243 break;
244 case 'preferences':
245 $translated_name = esc_html__( 'preferences', 'cookiebot' );
246 break;
247 case 'necessary':
248 $translated_name = esc_html__( 'necessary', 'cookiebot' );
249 break;
250 default:
251 $translated_name = $type;
252 }
253
254 return $translated_name;
255 }
256
257 /**
258 * @param $cookie_types
259 *
260 * @return string
261 *
262 * @version 3.9.0
263 */
264 function cookiebot_addons_cookieconsent_optout( $cookie_types ) {
265 $output = '';
266
267 foreach ( $cookie_types as $cookie_type ) {
268 $output .= 'cookieconsent-optout-' . $cookie_type . ' ';
269 }
270
271 return trim( $output );
272 }
273
274 /**
275 * Returns current site language
276 *
277 * @return mixed|string
278 *
279 * @since 1.9.0
280 */
281 function cookiebot_get_current_site_language() {
282 $lang = get_locale(); // Gets language in en-US format
283
284 /**
285 * Add support for 3rd party plugins
286 */
287 return apply_filters( 'cybot_cookiebot_addons_language', $lang );
288 }
289
290 /**
291 * Cookiebot_WP Get the language code for Cookiebot
292 *
293 * @version 1.4.0
294 * @since 1.4.0
295 */
296 function cookiebot_get_language_from_setting( $only_from_setting = false ) {
297 // Get language set in setting page - if empty use WP language info
298 $lang = get_option( 'cookiebot-language' );
299 if ( ! empty( $lang ) && $lang !== '_wp' ) {
300 return $lang;
301 }
302
303 if ( $only_from_setting ) {
304 return $lang; // We want only to get if already set
305 }
306
307 // Language not set - use WP language
308 if ( $lang === '_wp' ) {
309 $lang = get_bloginfo( 'language' ); // Gets language in en-US format
310 if ( ! empty( $lang ) ) {
311 list( $lang ) = explode( '-', $lang ); // Changes format from eg. en-US to en.
312 }
313 }
314
315 return $lang;
316 }
317
318 /**
319 * @param array $cookie_names
320 *
321 * @return array
322 */
323 function cookiebot_translate_cookie_names( $cookie_names ) {
324 $translated_cookie_names = array(
325 'preferences' => esc_html__( 'preferences', 'cookiebot' ),
326 'statistics' => esc_html__( 'statistics', 'cookiebot' ),
327 'marketing' => esc_html__( 'marketing', 'cookiebot' ),
328 );
329
330 return array_map(
331 function ( $cookie_name ) use ( $translated_cookie_names ) {
332 $cookie_name = trim( $cookie_name );
333 if ( isset( $translated_cookie_names[ $cookie_name ] ) ) {
334 return $translated_cookie_names[ $cookie_name ];
335 }
336
337 return $cookie_name;
338 },
339 $cookie_names
340 );
341 }
342
343 /**
344 * @param string $placeholder
345 *
346 * @return string
347 */
348 function cookiebot_translate_placeholder( $placeholder ) {
349 $translated_placeholder = array(
350 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
351 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies.' => esc_html__(
352 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies.',
353 'cookiebot'
354 ),
355 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
356 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable tracking.' => esc_html__(
357 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable tracking.',
358 'cookiebot'
359 ),
360 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
361 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.' => esc_html__(
362 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.',
363 'cookiebot'
364 ),
365 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
366 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to view this element.' => esc_html__(
367 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to view this element.',
368 'cookiebot'
369 ),
370 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
371 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.' => esc_html__(
372 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.',
373 'cookiebot'
374 ),
375 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
376 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Services.' => esc_html__(
377 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Services.',
378 'cookiebot'
379 ),
380 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
381 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable facebook shopping feature.' => esc_html__(
382 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable facebook shopping feature.',
383 'cookiebot'
384 ),
385 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
386 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.' => esc_html__(
387 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.',
388 'cookiebot'
389 ),
390 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
391 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Analytics.' => esc_html__(
392 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Analytics.',
393 'cookiebot'
394 ),
395 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
396 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable instagram feed.' => esc_html__(
397 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable instagram feed.',
398 'cookiebot'
399 ),
400 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
401 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Facebook Pixel.' => esc_html__(
402 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Facebook Pixel.',
403 'cookiebot'
404 ),
405 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
406 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to Social Share buttons.' => esc_html__(
407 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to Social Share buttons.',
408 'cookiebot'
409 ),
410 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
411 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.' => esc_html__(
412 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.',
413 'cookiebot'
414 ),
415 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
416 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.' => esc_html__(
417 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.',
418 'cookiebot'
419 ),
420 );
421
422 return empty( $translated_placeholder[ $placeholder ] ) ? $placeholder : $translated_placeholder[ $placeholder ];
423 }
424
425 /**
426 * Show languages in a select field
427 *
428 * @param $class
429 * @param $name
430 * @param $selected
431 *
432 * @return string
433 *
434 * @since 1.8.0
435 */
436 function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) {
437 $args = array(
438 'name' => $name,
439 'selected' => $selected,
440 'show_option_site_default' => true,
441 'echo' => false,
442 'languages' => get_available_languages(),
443 );
444 $dropdown = wp_dropdown_languages( $args );
445
446 $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown );
447
448 return (string) str_replace( 'value="" ', 'value="en_US" ', $output );
449 }
450
451 /**
452 * @param string $url
453 *
454 * @return string
455 *
456 * @throws Exception
457 * @since 3.11.0
458 */
459 function cookiebot_addons_get_domain_from_url( $url ) {
460 $parsed_url = wp_parse_url( $url );
461
462 // relative url does not have host so use home url domain
463 $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : cookiebot_addons_get_home_url_domain();
464
465 $url_parts = explode( '.', $host );
466
467 $url_parts = array_slice( $url_parts, - 2 );
468
469 return implode( '.', $url_parts );
470 }
471
472 /**
473 * @return string
474 * @throws Exception
475 *
476 * @since 3.11.0
477 */
478 function cookiebot_addons_get_home_url_domain() {
479 $home_url = wp_parse_url( home_url() );
480 /** @var $host string */
481 $host = $home_url['host'];
482
483 if ( empty( $host ) ) {
484 throw new Exception( 'Home url domain is not found.' );
485 }
486
487 return $host;
488 }
489
490 /**
491 * @param $file_path
492 *
493 * @return false|string
494 * @throws Exception
495 */
496 function cookiebot_get_local_file_contents( $file_path ) {
497 if ( ! file_exists( $file_path ) ) {
498 throw new Exception( 'File ' . $file_path . ' does not exist' );
499 }
500
501 ob_start();
502 include $file_path;
503
504 return ob_get_clean();
505 }
506
507 /**
508 * @param string $relative_path
509 * @throws InvalidArgumentException
510 */
511 function include_view( $relative_path, array $view_args = array() ) {
512 if ( isset( $view_args['absolute_path'] ) ) {
513 throw new InvalidArgumentException( 'Param $view_args array should not include an "absolute_path" key' );
514 }
515 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/' . $relative_path;
516 if ( ! file_exists( $absolute_path ) ) {
517 throw new InvalidArgumentException( 'View could not be loaded from "' . $absolute_path . '"' );
518 }
519 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
520 extract( $view_args );
521 include $absolute_path;
522 }
523
524 /**
525 * @param string $relative_path
526 * @throws InvalidArgumentException
527 */
528 function get_view_html( $relative_path, array $view_args = array() ) {
529 ob_start();
530 include_view( $relative_path, $view_args );
531 return (string) ob_get_clean();
532 }
533
534 /**
535 * @param string $relative_path
536 *
537 * @return string
538 * @throws InvalidArgumentException
539 */
540 function asset_path( $relative_path ) {
541 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path;
542 if ( ! file_exists( $absolute_path ) ) {
543 throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' );
544 }
545 return $absolute_path;
546 }
547
548 /**
549 * @param string $relative_path
550 *
551 * @return string
552 * @throws InvalidArgumentException
553 */
554 function asset_url( $relative_path ) {
555 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path;
556 $url = esc_url( CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/' . $relative_path );
557 if ( ! file_exists( $absolute_path ) || empty( $url ) ) {
558 throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' );
559 }
560
561 return $url;
562 }
563
564 /**
565 * Helper function to update your scripts
566 *
567 * @param string|string[] $type
568 *
569 * @return string
570 */
571 function cookiebot_assist( $type = 'statistics' ) {
572 $type_array = array_filter(
573 is_array( $type ) ? $type : array( $type ),
574 function ( $type ) {
575 return in_array( $type, array( 'marketing', 'statistics', 'preferences' ), true );
576 }
577 );
578
579 if ( count( $type_array ) > 0 ) {
580 return ' type="text/plain" data-cookieconsent="' . implode( ',', $type_array ) . '"';
581 }
582
583 return '';
584 }
585
586 /**
587 * Helper function to check if cookiebot is active.
588 * Useful for other plugins adding support for Cookiebot.
589 *
590 * @return bool
591 * @since 1.2
592 * @version 2.2.2
593 */
594 function cookiebot_active() {
595 $cbid = Cookiebot_WP::get_cbid();
596 return ! empty( $cbid );
597 }
598
599 /**
600 * Returns the main instance of Cookiebot_WP to prevent the need to use globals.
601 *
602 * @return Cookiebot_WP
603 * @throws RuntimeException
604 * @version 1.0.0
605 * @since 1.0.0
606 */
607 function cookiebot() {
608 return Cookiebot_WP::instance();
609 }
610 }
611