buffer
4 years ago
script_loader_tag
4 years ago
traits
4 years ago
Consent_API_Helper.php
4 years ago
Cookie_Consent.php
4 years ago
Cookie_Consent_Interface.php
4 years ago
Cookiebot_Activated.php
4 years ago
Cookiebot_Automatic_Updates.php
4 years ago
Cookiebot_Deactivated.php
4 years ago
Cookiebot_Javascript_Helper.php
4 years ago
Cookiebot_WP.php
4 years ago
Dependency_Container.php
4 years ago
Settings_Page_Tab.php
4 years ago
Settings_Service.php
4 years ago
Settings_Service_Interface.php
4 years ago
Supported_Languages.php
4 years ago
WP_Rocket_Helper.php
4 years ago
Widgets.php
4 years ago
global-deprecations.php
4 years ago
helper.php
4 years ago
helper.php
526 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 | * Show languages in a select field |
| 343 | * |
| 344 | * @param $class |
| 345 | * @param $name |
| 346 | * @param $selected |
| 347 | * |
| 348 | * @return string |
| 349 | * |
| 350 | * @since 1.8.0 |
| 351 | */ |
| 352 | function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) { |
| 353 | $args = array( |
| 354 | 'name' => $name, |
| 355 | 'selected' => $selected, |
| 356 | 'show_option_site_default' => true, |
| 357 | 'echo' => false, |
| 358 | 'languages' => get_available_languages(), |
| 359 | ); |
| 360 | $dropdown = wp_dropdown_languages( $args ); |
| 361 | |
| 362 | $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown ); |
| 363 | |
| 364 | return (string) str_replace( 'value="" ', 'value="en_US" ', $output ); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * @param string $url |
| 369 | * |
| 370 | * @return string |
| 371 | * |
| 372 | * @throws Exception |
| 373 | * @since 3.11.0 |
| 374 | */ |
| 375 | function cookiebot_addons_get_domain_from_url( $url ) { |
| 376 | $parsed_url = wp_parse_url( $url ); |
| 377 | |
| 378 | // relative url does not have host so use home url domain |
| 379 | $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : cookiebot_addons_get_home_url_domain(); |
| 380 | |
| 381 | $url_parts = explode( '.', $host ); |
| 382 | |
| 383 | $url_parts = array_slice( $url_parts, - 2 ); |
| 384 | |
| 385 | return implode( '.', $url_parts ); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * @return string |
| 390 | * @throws Exception |
| 391 | * |
| 392 | * @since 3.11.0 |
| 393 | */ |
| 394 | function cookiebot_addons_get_home_url_domain() { |
| 395 | $home_url = wp_parse_url( home_url() ); |
| 396 | /** @var $host string */ |
| 397 | $host = $home_url['host']; |
| 398 | |
| 399 | if ( empty( $host ) ) { |
| 400 | throw new Exception( 'Home url domain is not found.' ); |
| 401 | } |
| 402 | |
| 403 | return $host; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @param $file_path |
| 408 | * |
| 409 | * @return false|string |
| 410 | * @throws Exception |
| 411 | */ |
| 412 | function cookiebot_get_local_file_contents( $file_path ) { |
| 413 | if ( ! file_exists( $file_path ) ) { |
| 414 | throw new Exception( 'File ' . $file_path . ' does not exist' ); |
| 415 | } |
| 416 | |
| 417 | ob_start(); |
| 418 | include $file_path; |
| 419 | |
| 420 | return ob_get_clean(); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @param string $relative_path |
| 425 | * @throws InvalidArgumentException |
| 426 | */ |
| 427 | function include_view( $relative_path, array $view_args = array() ) { |
| 428 | if ( isset( $view_args['absolute_path'] ) ) { |
| 429 | throw new InvalidArgumentException( 'Param $view_args array should not include an "absolute_path" key' ); |
| 430 | } |
| 431 | $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/' . $relative_path; |
| 432 | if ( ! file_exists( $absolute_path ) ) { |
| 433 | throw new InvalidArgumentException( 'View could not be loaded from "' . $absolute_path . '"' ); |
| 434 | } |
| 435 | // phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 436 | extract( $view_args ); |
| 437 | include $absolute_path; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @param string $relative_path |
| 442 | * @throws InvalidArgumentException |
| 443 | */ |
| 444 | function get_view_html( $relative_path, array $view_args = array() ) { |
| 445 | ob_start(); |
| 446 | include_view( $relative_path, $view_args ); |
| 447 | return (string) ob_get_clean(); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * @param string $relative_path |
| 452 | * |
| 453 | * @return string |
| 454 | * @throws InvalidArgumentException |
| 455 | */ |
| 456 | function asset_path( $relative_path ) { |
| 457 | $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path; |
| 458 | if ( ! file_exists( $absolute_path ) ) { |
| 459 | throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' ); |
| 460 | } |
| 461 | return $absolute_path; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * @param string $relative_path |
| 466 | * |
| 467 | * @return string |
| 468 | * @throws InvalidArgumentException |
| 469 | */ |
| 470 | function asset_url( $relative_path ) { |
| 471 | $absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'assets/' . $relative_path; |
| 472 | $url = esc_url( CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/' . $relative_path ); |
| 473 | if ( ! file_exists( $absolute_path ) || empty( $url ) ) { |
| 474 | throw new InvalidArgumentException( 'Asset could not be loaded from "' . $absolute_path . '"' ); |
| 475 | } |
| 476 | |
| 477 | return $url; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Helper function to update your scripts |
| 482 | * @param string|string[] $type |
| 483 | * |
| 484 | * @return string |
| 485 | */ |
| 486 | function cookiebot_assist( $type = 'statistics' ) { |
| 487 | $type_array = array_filter( |
| 488 | is_array( $type ) ? $type : array( $type ), |
| 489 | function ( $type ) { |
| 490 | return in_array( $type, array( 'marketing', 'statistics', 'preferences' ), true ); |
| 491 | } |
| 492 | ); |
| 493 | |
| 494 | if ( count( $type_array ) > 0 ) { |
| 495 | return ' type="text/plain" data-cookieconsent="' . implode( ',', $type ) . '"'; |
| 496 | } |
| 497 | |
| 498 | return ''; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Helper function to check if cookiebot is active. |
| 503 | * Useful for other plugins adding support for Cookiebot. |
| 504 | * |
| 505 | * @return bool |
| 506 | * @since 1.2 |
| 507 | * @version 2.2.2 |
| 508 | */ |
| 509 | function cookiebot_active() { |
| 510 | $cbid = Cookiebot_WP::get_cbid(); |
| 511 | return ! empty( $cbid ); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Returns the main instance of Cookiebot_WP to prevent the need to use globals. |
| 516 | * |
| 517 | * @return Cookiebot_WP |
| 518 | * @throws RuntimeException |
| 519 | * @version 1.0.0 |
| 520 | * @since 1.0.0 |
| 521 | */ |
| 522 | function cookiebot() { |
| 523 | return Cookiebot_WP::instance(); |
| 524 | } |
| 525 | } |
| 526 |