PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.2.6
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.2.6
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
567 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( esc_html( $type . ' `' . $deprecated_name . '` is deprecated. Use `' . $alternative_name . '` instead.' ), E_USER_DEPRECATED );
17 }
18
19 /**
20 * Check if a cache plugin is activated and in function.
21 *
22 * @return boolean True If attributes always should be added
23 * False If attributes only should be added if consent no given
24 */
25 function cookiebot_addons_enabled_cache_plugin() {
26 if ( defined( 'WP_ROCKET_PATH' ) ) {
27 return true; // WP Rocket - We need to ensure we not cache tags without attributes
28 }
29 if ( defined( 'W3TC' ) ) {
30 return true; // W3 Total Cache
31 }
32 if ( defined( 'WPCACHEHOME' ) ) {
33 return true; // WP Super Cache
34 }
35 if ( defined( 'WPFC_WP_PLUGIN_DIR' ) ) {
36 return true; // WP Fastest Cache
37 }
38 if ( defined( 'LSCWP_CONTENT_DIR' ) ) {
39 return true; // Litespeed Cache
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 ) {
67 if ( is_array( $def['function'] ) ) {
68 if ( is_string( $def['function'][0] ) !== false ) {
69 $def_class = $def['function'][0];
70 } else {
71 $def_class = get_class( $def['function'][0] );
72 }
73
74 if ( $def_class === $class ) {
75 if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) {
76 $wp_filter[ $action ]->remove_filter( $action, $name, $priority );
77 } else {
78 unset( $wp_filter[ $action ][ $priority ][ $name ] );
79 }
80 $deleted = true;
81 }
82 }
83 }
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 = preg_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 $replacement = '<script type="text/plain" data-cookieconsent="' . $cookie_types . '"';
156 $script_tag_open = preg_replace( '/<script/', $replacement, $script_tag_open );
157
158 /**
159 * reconstruct the script and break the foreach loop
160 */
161 $script = $script_tag_open . $script_tag_inner . $script_tag_close;
162 }
163 }
164
165 /**
166 * return the reconstructed script
167 */
168 return $script;
169 },
170 $buffer
171 );
172
173 /**
174 * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT
175 *
176 * @version 2.0.4
177 * @since 2.0.4
178 */
179 if ( $updated_scripts === null ) {
180 $updated_scripts = $buffer;
181
182 if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) {
183 update_option( 'cookiebot_regex_stacklimit', 1 );
184 }
185 }
186
187 return $updated_scripts;
188 }
189
190 /**
191 * @param array $cookie_types
192 * @param $cookie_type
193 */
194 function cookiebot_addons_checked_selected_helper( array $cookie_types, $cookie_type ) {
195 if ( in_array( $cookie_type, $cookie_types, true ) ) {
196 echo " checked='checked'";
197 }
198 }
199
200 /**
201 * Returns cookie types in a string
202 * Default is statistics
203 *
204 * @param $cookie_types
205 *
206 * @return string
207 *
208 * @since 1.3.0
209 * @version 3.9.1
210 */
211 function cookiebot_addons_output_cookie_types( $cookie_types ) {
212 if ( is_array( $cookie_types ) && count( $cookie_types ) > 0 ) {
213 return implode(
214 ', ',
215 array_map(
216 function ( $value ) {
217 return cookiebot_translate_type_name( $value );
218 },
219 $cookie_types
220 )
221 );
222 } elseif ( is_string( $cookie_types ) && ! empty( $cookie_types ) ) {
223 return cookiebot_translate_type_name( $cookie_types );
224 }
225
226 return cookiebot_translate_type_name( 'statistics' );
227 }
228
229 /**
230 * Translates the cookie type to different language
231 *
232 * @param $type string
233 *
234 * @return string
235 *
236 * @since 3.9.1
237 */
238 function cookiebot_translate_type_name( $type ) {
239 switch ( $type ) {
240 case 'marketing':
241 return esc_html__( 'marketing', 'cookiebot' );
242 case 'statistics':
243 return esc_html__( 'statistics', 'cookiebot' );
244 case 'preferences':
245 return esc_html__( 'preferences', 'cookiebot' );
246 case 'necessary':
247 return esc_html__( 'necessary', 'cookiebot' );
248 default:
249 return $type;
250 }
251 }
252
253 /**
254 * @param $cookie_types
255 *
256 * @return string
257 *
258 * @version 3.9.0
259 */
260 function cookiebot_addons_cookieconsent_optout( $cookie_types ) {
261 $output = '';
262
263 foreach ( $cookie_types as $cookie_type ) {
264 $output .= 'cookieconsent-optout-' . $cookie_type . ' ';
265 }
266
267 return trim( $output );
268 }
269
270 /**
271 * Returns current site language
272 *
273 * @return mixed|string
274 *
275 * @since 1.9.0
276 */
277 function cookiebot_get_current_site_language() {
278 $lang = get_locale(); // Gets language in en-US format
279
280 /**
281 * Add support for 3rd party plugins
282 */
283 return apply_filters( 'cybot_cookiebot_addons_language', $lang );
284 }
285
286 /**
287 * Cookiebot_WP Get the language code for Cookiebot
288 *
289 * @version 1.4.0
290 * @since 1.4.0
291 */
292 function cookiebot_get_language_from_setting( $only_from_setting = false ) {
293 // Get language set in setting page - if empty use WP language info
294 $lang = get_option( 'cookiebot-language' );
295 if ( ! empty( $lang ) ) {
296 if ( $lang !== '_wp' ) {
297 return $lang;
298 }
299 }
300
301 if ( $only_from_setting ) {
302 return $lang; // We want only to get if already set
303 }
304
305 // Language not set - use WP language
306 if ( $lang === '_wp' ) {
307 $lang = get_bloginfo( 'language' ); // Gets language in en-US format
308 if ( ! empty( $lang ) ) {
309 list( $lang ) = explode( '-', $lang ); // Changes format from eg. en-US to en.
310 }
311 }
312
313 return $lang;
314 }
315
316 /**
317 * @param array $cookie_names
318 *
319 * @return array
320 */
321 function cookiebot_translate_cookie_names( $cookie_names ) {
322 $translated_cookie_names = array(
323 'preferences' => esc_html__( 'preferences', 'cookiebot' ),
324 'statistics' => esc_html__( 'statistics', 'cookiebot' ),
325 'marketing' => esc_html__( 'marketing', 'cookiebot' ),
326 );
327
328 return array_map(
329 function ( $cookie_name ) use ( $translated_cookie_names ) {
330 $cookie_name = trim( $cookie_name );
331 if ( isset( $translated_cookie_names[ $cookie_name ] ) ) {
332 return $translated_cookie_names[ $cookie_name ];
333 }
334
335 return $cookie_name;
336 },
337 $cookie_names
338 );
339 }
340
341 /**
342 * @param string $placeholder
343 *
344 * @return string
345 */
346 function cookiebot_translate_placeholder( $placeholder ) {
347 $translated_placeholder = array(
348 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
349 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies.', 'cookiebot' ),
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 to enable tracking.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable tracking.', 'cookiebot' ),
352 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
353 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.', 'cookiebot' ),
354 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
355 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to view this element.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to view this element.', 'cookiebot' ),
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 to watch this video.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.', 'cookiebot' ),
358 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
359 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Services.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Services.', 'cookiebot' ),
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 facebook shopping feature.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable facebook shopping feature.', 'cookiebot' ),
362 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
363 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.', 'cookiebot' ),
364 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
365 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Analytics.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Analytics.', 'cookiebot' ),
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 instagram feed.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable instagram feed.', 'cookiebot' ),
368 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
369 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Facebook Pixel.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Facebook Pixel.', 'cookiebot' ),
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 Social Share buttons.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to Social Share buttons.', 'cookiebot' ),
372 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
373 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.', 'cookiebot' ),
374 // translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder
375 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.' => esc_html__( 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.', 'cookiebot' ),
376 );
377
378 return $translated_placeholder[ $placeholder ];
379 }
380
381 /**
382 * Show languages in a select field
383 *
384 * @param $class
385 * @param $name
386 * @param $selected
387 *
388 * @return string
389 *
390 * @since 1.8.0
391 */
392 function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) {
393 $args = array(
394 'name' => $name,
395 'selected' => $selected,
396 'show_option_site_default' => true,
397 'echo' => false,
398 'languages' => get_available_languages(),
399 );
400 $dropdown = wp_dropdown_languages( $args );
401
402 $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown );
403
404 return (string) str_replace( 'value="" ', 'value="en_US" ', $output );
405 }
406
407 /**
408 * @param string $url
409 *
410 * @return string
411 *
412 * @throws Exception
413 * @since 3.11.0
414 */
415 function cookiebot_addons_get_domain_from_url( $url ) {
416 $parsed_url = wp_parse_url( $url );
417
418 // relative url does not have host so use home url domain
419 $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : cookiebot_addons_get_home_url_domain();
420
421 $url_parts = explode( '.', $host );
422
423 $url_parts = array_slice( $url_parts, - 2 );
424
425 return implode( '.', $url_parts );
426 }
427
428 /**
429 * @return string
430 * @throws Exception
431 *
432 * @since 3.11.0
433 */
434 function cookiebot_addons_get_home_url_domain() {
435 $home_url = wp_parse_url( home_url() );
436 /** @var $host string */
437 $host = $home_url['host'];
438
439 if ( empty( $host ) ) {
440 throw new Exception( 'Home url domain is not found.' );
441 }
442
443 return $host;
444 }
445
446 /**
447 * @param $file_path
448 *
449 * @return false|string
450 * @throws Exception
451 */
452 function cookiebot_get_local_file_contents( $file_path ) {
453 if ( ! file_exists( $file_path ) ) {
454 throw new Exception( 'File ' . $file_path . ' does not exist' );
455 }
456
457 ob_start();
458 include $file_path;
459
460 return ob_get_clean();
461 }
462
463 /**
464 * @param string $relative_path
465 * @throws InvalidArgumentException
466 */
467 function include_view( $relative_path, array $view_args = array() ) {
468 if ( isset( $view_args['absolute_path'] ) ) {
469 throw new InvalidArgumentException( 'Param $view_args array should not include an "absolute_path" key' );
470 }
471 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/' . $relative_path;
472 if ( ! file_exists( $absolute_path ) ) {
473 throw new InvalidArgumentException( 'View could not be loaded from "' . $absolute_path . '"' );
474 }
475 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
476 extract( $view_args );
477 include $absolute_path;
478 }
479
480 /**
481 * @param string $relative_path
482 * @throws InvalidArgumentException
483 */
484 function get_view_html( $relative_path, array $view_args = array() ) {
485 ob_start();
486 include_view( $relative_path, $view_args );
487 return (string) ob_get_clean();
488 }
489
490 /**
491 * @param string $relative_path
492 *
493 * @return string
494 * @throws InvalidArgumentException
495 */
496 function asset_path( $relative_path ) {
497 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path;
498 if ( ! file_exists( $absolute_path ) ) {
499 throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' );
500 }
501 return $absolute_path;
502 }
503
504 /**
505 * @param string $relative_path
506 *
507 * @return string
508 * @throws InvalidArgumentException
509 */
510 function asset_url( $relative_path ) {
511 $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path;
512 $url = esc_url( CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/' . $relative_path );
513 if ( ! file_exists( $absolute_path ) || empty( $url ) ) {
514 throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' );
515 }
516
517 return $url;
518 }
519
520 /**
521 * Helper function to update your scripts
522 *
523 * @param string|string[] $type
524 *
525 * @return string
526 */
527 function cookiebot_assist( $type = 'statistics' ) {
528 $type_array = array_filter(
529 is_array( $type ) ? $type : array( $type ),
530 function ( $type ) {
531 return in_array( $type, array( 'marketing', 'statistics', 'preferences' ), true );
532 }
533 );
534
535 if ( count( $type_array ) > 0 ) {
536 return ' type="text/plain" data-cookieconsent="' . implode( ',', $type_array ) . '"';
537 }
538
539 return '';
540 }
541
542 /**
543 * Helper function to check if cookiebot is active.
544 * Useful for other plugins adding support for Cookiebot.
545 *
546 * @return bool
547 * @since 1.2
548 * @version 2.2.2
549 */
550 function cookiebot_active() {
551 $cbid = Cookiebot_WP::get_cbid();
552 return ! empty( $cbid );
553 }
554
555 /**
556 * Returns the main instance of Cookiebot_WP to prevent the need to use globals.
557 *
558 * @return Cookiebot_WP
559 * @throws RuntimeException
560 * @version 1.0.0
561 * @since 1.0.0
562 */
563 function cookiebot() {
564 return Cookiebot_WP::instance();
565 }
566 }
567