buffer
4 years ago
ioc
6 years ago
script-loader-tag
4 years ago
autoloader.php
7 years ago
cookie-consent-interface.php
7 years ago
cookie-consent.php
4 years ago
dependency-container.php
4 years ago
helper.php
4 years ago
settings-service-interface.php
4 years ago
settings-service.php
4 years ago
theme-settings-service.php
4 years ago
helper.php
475 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 | |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Removes action with class in callback |
| 32 | * |
| 33 | * @param $action string action name |
| 34 | * @param $class string class name |
| 35 | * @param $method string method name |
| 36 | * @param $priority integer action priority number |
| 37 | * |
| 38 | * @return boolean True if the action hook is deleted |
| 39 | * False If the action hook is not deleted |
| 40 | * |
| 41 | * @since 1.2.0 |
| 42 | */ |
| 43 | function cookiebot_addons_remove_class_action( $action, $class, $method, $priority = 10 ) { |
| 44 | global $wp_filter; |
| 45 | $deleted = false; |
| 46 | |
| 47 | if ( isset( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ][ $priority ] ) ) { |
| 48 | $len = strlen( $method ); |
| 49 | foreach ( $wp_filter[ $action ][ $priority ] as $name => $def ) { |
| 50 | if ( substr( $name, - $len ) == $method ) { |
| 51 | if ( is_array( $def['function'] ) ) { |
| 52 | if ( is_string( $def['function'][0] ) !== false ) { |
| 53 | $def_class = $def['function'][0]; |
| 54 | } else { |
| 55 | $def_class = get_class( $def['function'][0] ); |
| 56 | } |
| 57 | |
| 58 | if ( $def_class == $class ) { |
| 59 | if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) { |
| 60 | $wp_filter[ $action ]->remove_filter( $action, $name, $priority ); |
| 61 | $deleted = true; |
| 62 | } else { |
| 63 | unset( $wp_filter[ $action ][ $priority ][ $name ] ); |
| 64 | $deleted = true; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $deleted; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Custom manipulation of the script |
| 77 | * |
| 78 | * @param $buffer |
| 79 | * @param $keywords |
| 80 | * @param $cookie_type |
| 81 | * |
| 82 | * @return mixed|null|string|string[] |
| 83 | * |
| 84 | * @version 2.0.4 |
| 85 | * @since 1.2.0 |
| 86 | */ |
| 87 | function cookiebot_addons_manipulate_script( $buffer, $keywords ) { |
| 88 | /** |
| 89 | * normalize potential self-closing script tags |
| 90 | */ |
| 91 | |
| 92 | $normalized_buffer = preg_replace( '/(<script(.*?)\/>)/is', '<script$2></script>', $buffer ); |
| 93 | |
| 94 | if ( $normalized_buffer !== null ) { |
| 95 | $buffer = $normalized_buffer; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Pattern to get all scripts |
| 100 | * |
| 101 | * @version 2.0.4 |
| 102 | * @since 1.2.0 |
| 103 | */ |
| 104 | $pattern = '/(<script(?:.*?)>)(.*?)(<\/script>)/is'; |
| 105 | |
| 106 | /** |
| 107 | * Get all scripts and add cookieconsent if it does match with the criterion |
| 108 | */ |
| 109 | $updated_scripts = preg_replace_callback( |
| 110 | $pattern, |
| 111 | function ( $matches ) use ( $keywords ) { |
| 112 | |
| 113 | $script = $matches[0]; // the full script html |
| 114 | $script_tag_open = $matches[1]; // only the script open tag with all attributes |
| 115 | $script_tag_inner = $matches[2]; // only the script's innerText |
| 116 | $script_tag_close = $matches[3]; // only the script closing tag |
| 117 | |
| 118 | /** |
| 119 | * Check if the script contains the keywords, checks keywords one by one |
| 120 | * |
| 121 | * If one match, then the rest of the keywords will be skipped. |
| 122 | **/ |
| 123 | foreach ( $keywords as $needle => $cookie_type ) { |
| 124 | /** |
| 125 | * The script contains the needle |
| 126 | **/ |
| 127 | if ( strpos( $script, $needle ) !== false ) { |
| 128 | /** |
| 129 | * replace all single quotes with double quotes in the open tag |
| 130 | * remove previously set data-cookieconsent attribute |
| 131 | * remove type attribute |
| 132 | */ |
| 133 | $script_tag_open = preg_replace( '/\'/', '"', $script_tag_open ); |
| 134 | $script_tag_open = preg_replace( '/\sdata-cookieconsent=\"(?:.*?)\"/', '', $script_tag_open ); |
| 135 | $script_tag_open = preg_replace( '/\stype=\"(?:.*?)\"/', '', $script_tag_open ); |
| 136 | |
| 137 | /** |
| 138 | * set the type attribute to text/plain to prevent javascript execution |
| 139 | * add data-cookieconsent attribute |
| 140 | */ |
| 141 | $cookie_types = cookiebot_addons_output_cookie_types( $cookie_type ); |
| 142 | $replacement = '<script type="text/plain" data-cookieconsent="' . $cookie_types . '"'; |
| 143 | $script_tag_open = preg_replace( '/<script/', $replacement, $script_tag_open ); |
| 144 | |
| 145 | /** |
| 146 | * reconstruct the script and break the foreach loop |
| 147 | */ |
| 148 | $script = $script_tag_open . $script_tag_inner . $script_tag_close; |
| 149 | continue; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * return the reconstructed script |
| 155 | */ |
| 156 | return $script; |
| 157 | }, |
| 158 | $buffer |
| 159 | ); |
| 160 | |
| 161 | /** |
| 162 | * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT |
| 163 | * |
| 164 | * @version 2.0.4 |
| 165 | * @since 2.0.4 |
| 166 | */ |
| 167 | if ( $updated_scripts === null ) { |
| 168 | $updated_scripts = $buffer; |
| 169 | |
| 170 | if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) { |
| 171 | update_option( 'cookiebot_regex_stacklimit', 1 ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return $updated_scripts; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Compares array to string to add checked attribute in checkbox |
| 180 | * |
| 181 | * @param $helper |
| 182 | * @param $current |
| 183 | * @param bool $echo |
| 184 | * @param string $type |
| 185 | * |
| 186 | * @return string |
| 187 | * |
| 188 | * @since 1.3.0 |
| 189 | */ |
| 190 | function cookiebot_addons_checked_selected_helper( $helper, $current, $echo = true, $type = 'checked' ) { |
| 191 | $current_in_helper_array = is_array( $helper ) && in_array( $current, $helper, true ); |
| 192 | $current_equals_helper_string = is_string( $helper ) && is_string( $current ) && $helper === $current; |
| 193 | |
| 194 | if ( $current_in_helper_array || $current_equals_helper_string ) { |
| 195 | if ( $echo ) { |
| 196 | echo ' ' . esc_attr( $type ) . '=\'' . esc_attr( $type ) . '\''; |
| 197 | } |
| 198 | |
| 199 | return " $type='$type'"; |
| 200 | } |
| 201 | |
| 202 | return ''; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns cookie types in a string |
| 207 | * Default is statistics |
| 208 | * |
| 209 | * @param $cookie_types |
| 210 | * |
| 211 | * @return string |
| 212 | * |
| 213 | * @since 1.3.0 |
| 214 | * @version 3.9.1 |
| 215 | */ |
| 216 | function cookiebot_addons_output_cookie_types( $cookie_types ) { |
| 217 | if ( is_array( $cookie_types ) && count( $cookie_types ) > 0 ) { |
| 218 | return implode( |
| 219 | ', ', |
| 220 | array_map( |
| 221 | function ( $value ) { |
| 222 | return cookiebot_translate_type_name( $value ); |
| 223 | }, |
| 224 | $cookie_types |
| 225 | ) |
| 226 | ); |
| 227 | } elseif ( is_string( $cookie_types ) && $cookie_types != '' ) { |
| 228 | return cookiebot_translate_type_name( $cookie_types ); |
| 229 | } |
| 230 | |
| 231 | return cookiebot_translate_type_name( 'statistics' ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Translates the cookie type to different language |
| 236 | * |
| 237 | * @param $type string |
| 238 | * |
| 239 | * @return string |
| 240 | * |
| 241 | * @since 3.9.1 |
| 242 | */ |
| 243 | function cookiebot_translate_type_name( $type ) { |
| 244 | switch ( $type ) { |
| 245 | case 'marketing': |
| 246 | return esc_html__( 'marketing', 'cookiebot' ); |
| 247 | case 'statistics': |
| 248 | return esc_html__( 'statistics', 'cookiebot' ); |
| 249 | case 'preferences': |
| 250 | return esc_html__( 'preferences', 'cookiebot' ); |
| 251 | case 'necessary': |
| 252 | return esc_html__( 'necessary', 'cookiebot' ); |
| 253 | default: |
| 254 | return $type; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Return 1 cookie type if more than 1 is selected |
| 260 | * |
| 261 | * @param $cookie_types |
| 262 | * |
| 263 | * @return string |
| 264 | * |
| 265 | * @since 1.3.0 |
| 266 | */ |
| 267 | function cookiebot_addons_get_one_cookie_type( $cookie_types ) { |
| 268 | if ( is_array( $cookie_types ) ) { |
| 269 | if ( in_array( 'marketing', $cookie_types ) ) { |
| 270 | return 'marketing'; |
| 271 | } elseif ( in_array( 'statistics', $cookie_types ) ) { |
| 272 | return 'statistics'; |
| 273 | } elseif ( in_array( 'preferences', $cookie_types ) ) { |
| 274 | return 'preferences'; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return ''; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @param $cookie_types |
| 283 | * |
| 284 | * @return string |
| 285 | * |
| 286 | * @version 3.9.0 |
| 287 | */ |
| 288 | function cookiebot_addons_cookieconsent_optout( $cookie_types ) { |
| 289 | $output = ''; |
| 290 | |
| 291 | foreach ( $cookie_types as $cookie_type ) { |
| 292 | $output .= 'cookieconsent-optout-' . $cookie_type . ' '; |
| 293 | } |
| 294 | |
| 295 | return trim( $output ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns current site language |
| 300 | * |
| 301 | * @return mixed|string |
| 302 | * |
| 303 | * @since 1.9.0 |
| 304 | */ |
| 305 | function cookiebot_addons_get_language() { |
| 306 | $lang = get_locale(); //Gets language in en-US format |
| 307 | |
| 308 | /** |
| 309 | * Add support for 3rd party plugins |
| 310 | */ |
| 311 | $lang = apply_filters( 'cookiebot_addons_language', $lang ); |
| 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 ( string $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 | * Get supported languages by the cookiebot |
| 343 | * |
| 344 | * @return array |
| 345 | * |
| 346 | * @since 1.9.0 |
| 347 | */ |
| 348 | function cookiebot_addons_get_supported_languages() { |
| 349 | $cookiebot = cookiebot(); |
| 350 | |
| 351 | return $cookiebot->get_supported_languages(); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Returns an escaped HTML "select" element |
| 356 | * Show languages in a select field |
| 357 | * |
| 358 | * @param $class |
| 359 | * @param $name |
| 360 | * @param $selected |
| 361 | * |
| 362 | * @return string |
| 363 | * |
| 364 | * @since 1.8.0 |
| 365 | */ |
| 366 | function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) { |
| 367 | $args = array( |
| 368 | 'name' => $name, |
| 369 | 'selected' => $selected, |
| 370 | 'show_option_site_default' => true, |
| 371 | 'echo' => false, |
| 372 | 'languages' => get_available_languages(), |
| 373 | ); |
| 374 | $dropdown = wp_dropdown_languages( $args ); |
| 375 | $output = str_replace( '<select ', '<select class="' . esc_attr( $class ) . '" ', $dropdown ); |
| 376 | |
| 377 | return str_replace( ' value="" ', 'value="en_US" ', $output ); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Run actions when the cookiebot plugin is deactivated |
| 382 | * |
| 383 | * @since 2.2.0 |
| 384 | */ |
| 385 | function cookiebot_addons_plugin_deactivated() { |
| 386 | $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance(); |
| 387 | $cookiebot_addons->cookiebot_deactivated(); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Run actions when the cookiebot plugin is deactivated |
| 392 | * |
| 393 | * @since 3.6.3 |
| 394 | */ |
| 395 | function cookiebot_addons_plugin_activated() { |
| 396 | $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance(); |
| 397 | $cookiebot_addons->cookiebot_activated(); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * @param string $url |
| 402 | * |
| 403 | * @return string |
| 404 | * |
| 405 | * @since 3.11.0 |
| 406 | */ |
| 407 | function cookiebot_addons_get_domain_from_url( $url ) { |
| 408 | $parsed_url = parse_url( $url ); |
| 409 | |
| 410 | // relative url does not have host so use home url domain |
| 411 | $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : cookiebot_addons_get_home_url_domain(); |
| 412 | |
| 413 | $url_parts = explode( '.', $host ); |
| 414 | |
| 415 | $url_parts = array_slice( $url_parts, - 2 ); |
| 416 | |
| 417 | return implode( '.', $url_parts ); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * @return string |
| 422 | * @throws Exception |
| 423 | * |
| 424 | * @since 3.11.0 |
| 425 | */ |
| 426 | function cookiebot_addons_get_home_url_domain() { |
| 427 | $home_url = parse_url( home_url() ); |
| 428 | /** @var $host string */ |
| 429 | $host = $home_url['host']; |
| 430 | |
| 431 | if ( empty( $host ) ) { |
| 432 | throw new Exception( 'Home url domain is not found.' ); |
| 433 | } |
| 434 | |
| 435 | return $host; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * @param $file_path |
| 440 | * |
| 441 | * @return false|string |
| 442 | * @throws Exception |
| 443 | */ |
| 444 | function cookiebot_get_local_file_contents( $file_path ) { |
| 445 | if ( ! file_exists( $file_path ) ) { |
| 446 | throw new Exception( 'File ' . $file_path . ' does not exist' ); |
| 447 | } |
| 448 | |
| 449 | ob_start(); |
| 450 | include $file_path; |
| 451 | |
| 452 | return ob_get_clean(); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * @param $file_path |
| 457 | * |
| 458 | * @return array |
| 459 | * @throws Exception |
| 460 | */ |
| 461 | function cookiebot_get_local_file_json_contents( $file_path ) { |
| 462 | $json = cookiebot_get_local_file_contents( $file_path ); |
| 463 | |
| 464 | $decoded_json = json_decode( $json ); |
| 465 | |
| 466 | if ( ! is_a( $decoded_json, stdClass::class ) ) { |
| 467 | throw new Exception( 'Filepath ' . $file_path . ' could not be parsed as json file' ); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @var array $decoded_json |
| 472 | */ |
| 473 | return $decoded_json; |
| 474 | } |
| 475 |