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