| 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 |
$front_language = get_option( 'cookiebot-front-language' ); |
| 306 |
|
| 307 |
if ( ! empty( $front_language ) ) { |
| 308 |
$lang = get_locale(); // Gets language in en-US format |
| 309 |
if ( ! empty( $lang ) ) { |
| 310 |
list($lang) = explode( '_', $lang ); // Changes format from eg. en-US to en. |
| 311 |
} |
| 312 |
return $lang; |
| 313 |
} |
| 314 |
|
| 315 |
if ( ! empty( $lang ) && $lang !== '_wp' ) { |
| 316 |
return $lang; |
| 317 |
} |
| 318 |
|
| 319 |
if ( $only_from_setting ) { |
| 320 |
return $lang; // We want only to get if already set |
| 321 |
} |
| 322 |
|
| 323 |
// Language not set - use WP language |
| 324 |
if ( $lang === '_wp' ) { |
| 325 |
$lang = get_bloginfo( 'language' ); // Gets language in en-US format |
| 326 |
if ( ! empty( $lang ) ) { |
| 327 |
list($lang) = explode( '-', $lang ); // Changes format from eg. en-US to en. |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return $lang; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* @param array $cookie_names |
| 336 |
* |
| 337 |
* @return array |
| 338 |
*/ |
| 339 |
function cookiebot_translate_cookie_names( $cookie_names ) { |
| 340 |
$translated_cookie_names = array( |
| 341 |
'preferences' => esc_html__( 'preferences', 'cookiebot' ), |
| 342 |
'statistics' => esc_html__( 'statistics', 'cookiebot' ), |
| 343 |
'marketing' => esc_html__( 'marketing', 'cookiebot' ), |
| 344 |
); |
| 345 |
|
| 346 |
return array_map( |
| 347 |
function ( $cookie_name ) use ( $translated_cookie_names ) { |
| 348 |
$cookie_name = trim( $cookie_name ); |
| 349 |
if ( isset( $translated_cookie_names[ $cookie_name ] ) ) { |
| 350 |
return $translated_cookie_names[ $cookie_name ]; |
| 351 |
} |
| 352 |
|
| 353 |
return $cookie_name; |
| 354 |
}, |
| 355 |
$cookie_names |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* @param string $placeholder |
| 361 |
* |
| 362 |
* @return string |
| 363 |
*/ |
| 364 |
function cookiebot_translate_placeholder( $placeholder ) { |
| 365 |
$translated_placeholder = array( |
| 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.' => esc_html__( |
| 368 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies.', |
| 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 enable tracking.' => esc_html__( |
| 373 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable tracking.', |
| 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 enable Social Share buttons.' => esc_html__( |
| 378 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.', |
| 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 view this element.' => esc_html__( |
| 383 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to view this element.', |
| 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 watch this video.' => esc_html__( |
| 388 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.', |
| 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 enable Google Services.' => esc_html__( |
| 393 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Services.', |
| 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 facebook shopping feature.' => esc_html__( |
| 398 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable facebook shopping feature.', |
| 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 track for google analytics.' => esc_html__( |
| 403 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.', |
| 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 Google Analytics.' => esc_html__( |
| 408 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Google Analytics.', |
| 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 enable instagram feed.' => esc_html__( |
| 413 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable instagram feed.', |
| 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 enable Facebook Pixel.' => esc_html__( |
| 418 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Facebook Pixel.', |
| 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 Social Share buttons.' => esc_html__( |
| 423 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to Social Share buttons.', |
| 424 |
'cookiebot' |
| 425 |
), |
| 426 |
// translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder |
| 427 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.' => esc_html__( |
| 428 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to allow Matomo statistics.', |
| 429 |
'cookiebot' |
| 430 |
), |
| 431 |
// translators: %cookie_types refers to the list of cookie types assigned to the addon placeholder |
| 432 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.' => esc_html__( |
| 433 |
'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable saving user information.', |
| 434 |
'cookiebot' |
| 435 |
), |
| 436 |
); |
| 437 |
|
| 438 |
return empty( $translated_placeholder[ $placeholder ] ) ? $placeholder : $translated_placeholder[ $placeholder ]; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Show languages in a select field |
| 443 |
* |
| 444 |
* @param $class |
| 445 |
* @param $name |
| 446 |
* @param $selected |
| 447 |
* |
| 448 |
* @return string |
| 449 |
* |
| 450 |
* @since 1.8.0 |
| 451 |
*/ |
| 452 |
function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) { |
| 453 |
$args = array( |
| 454 |
'name' => $name, |
| 455 |
'selected' => $selected, |
| 456 |
'show_option_site_default' => true, |
| 457 |
'echo' => false, |
| 458 |
'languages' => get_available_languages(), |
| 459 |
); |
| 460 |
$dropdown = wp_dropdown_languages( $args ); |
| 461 |
|
| 462 |
$output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown ); |
| 463 |
|
| 464 |
return (string) str_replace( 'value="" ', 'value="en_US" ', $output ); |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* @param string $url |
| 469 |
* |
| 470 |
* @return string |
| 471 |
* |
| 472 |
* @throws Exception |
| 473 |
* @since 3.11.0 |
| 474 |
*/ |
| 475 |
function cookiebot_addons_get_domain_from_url( $url ) { |
| 476 |
$parsed_url = wp_parse_url( $url ); |
| 477 |
|
| 478 |
// relative url does not have host so use home url domain |
| 479 |
$host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : cookiebot_addons_get_home_url_domain(); |
| 480 |
|
| 481 |
$url_parts = explode( '.', $host ); |
| 482 |
|
| 483 |
$url_parts = array_slice( $url_parts, -2 ); |
| 484 |
|
| 485 |
return implode( '.', $url_parts ); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* @return string |
| 490 |
* @throws Exception |
| 491 |
* |
| 492 |
* @since 3.11.0 |
| 493 |
*/ |
| 494 |
function cookiebot_addons_get_home_url_domain() { |
| 495 |
$home_url = wp_parse_url( home_url() ); |
| 496 |
/** @var $host string */ |
| 497 |
$host = $home_url['host']; |
| 498 |
|
| 499 |
if ( empty( $host ) ) { |
| 500 |
throw new DomainException( 'Home url domain is not found.' ); |
| 501 |
} |
| 502 |
|
| 503 |
return $host; |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* @param $file_path |
| 508 |
* |
| 509 |
* @return false|string |
| 510 |
* @throws Exception |
| 511 |
*/ |
| 512 |
function cookiebot_get_local_file_contents( $file_path ) { |
| 513 |
if ( ! file_exists( $file_path ) ) { |
| 514 |
throw new InvalidArgumentException( 'File ' . esc_html( $file_path ) . ' does not exist' ); |
| 515 |
} |
| 516 |
|
| 517 |
ob_start(); |
| 518 |
include $file_path; |
| 519 |
|
| 520 |
return ob_get_clean(); |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* @param string $relative_path |
| 525 |
* @throws InvalidArgumentException |
| 526 |
*/ |
| 527 |
function include_view( $relative_path, array $view_args = array() ) { |
| 528 |
if ( isset( $view_args['absolute_path'] ) ) { |
| 529 |
throw new InvalidArgumentException( 'Param $view_args array should not include an "absolute_path" key' ); |
| 530 |
} |
| 531 |
$absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/' . $relative_path; |
| 532 |
if ( ! file_exists( $absolute_path ) ) { |
| 533 |
throw new InvalidArgumentException( 'View could not be loaded from "' . esc_html( $absolute_path ) . '"' ); |
| 534 |
} |
| 535 |
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 536 |
extract( $view_args ); |
| 537 |
include $absolute_path; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* @param string $relative_path |
| 542 |
* @throws InvalidArgumentException |
| 543 |
*/ |
| 544 |
function get_view_html( $relative_path, array $view_args = array() ) { |
| 545 |
ob_start(); |
| 546 |
include_view( $relative_path, $view_args ); |
| 547 |
return (string) ob_get_clean(); |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* @param string $relative_path |
| 552 |
* |
| 553 |
* @return string |
| 554 |
* @throws InvalidArgumentException |
| 555 |
*/ |
| 556 |
function asset_path( $relative_path ) { |
| 557 |
$absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . CYBOT_COOKIEBOT_PLUGIN_ASSETS_DIR . $relative_path; |
| 558 |
if ( ! file_exists( $absolute_path ) ) { |
| 559 |
throw new InvalidArgumentException( 'Asset could not be loaded from "' . esc_html( $absolute_path ) . '"' ); |
| 560 |
} |
| 561 |
return $absolute_path; |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* @param string $relative_path |
| 566 |
* |
| 567 |
* @return string |
| 568 |
* @throws InvalidArgumentException |
| 569 |
*/ |
| 570 |
function asset_url( $relative_path ) { |
| 571 |
$absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . CYBOT_COOKIEBOT_PLUGIN_ASSETS_DIR . $relative_path; |
| 572 |
$url = esc_url( CYBOT_COOKIEBOT_PLUGIN_URL . CYBOT_COOKIEBOT_PLUGIN_ASSETS_DIR . $relative_path ); |
| 573 |
if ( ! file_exists( $absolute_path ) || empty( $url ) ) { |
| 574 |
throw new InvalidArgumentException( 'Asset could not be loaded from "' . esc_html( $absolute_path ) . '"' ); |
| 575 |
} |
| 576 |
return $url; |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* @return string |
| 581 |
* @throws InvalidArgumentException |
| 582 |
*/ |
| 583 |
function logo_url() { |
| 584 |
$absolute_path = CYBOT_COOKIEBOT_PLUGIN_DIR . CYBOT_COOKIEBOT_PLUGIN_LOGO_FILE; |
| 585 |
$url = esc_url( CYBOT_COOKIEBOT_PLUGIN_URL . CYBOT_COOKIEBOT_PLUGIN_LOGO_FILE ); |
| 586 |
if ( ! file_exists( $absolute_path ) || empty( $url ) ) { |
| 587 |
throw new InvalidArgumentException( 'Asset could not be loaded from "' . esc_html( $absolute_path ) . '"' ); |
| 588 |
} |
| 589 |
return $url; |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Helper function to update your scripts |
| 594 |
* |
| 595 |
* @param string|string[] $type |
| 596 |
* |
| 597 |
* @return string |
| 598 |
*/ |
| 599 |
function cookiebot_assist( $type = 'statistics' ) { |
| 600 |
$type_array = array_filter( |
| 601 |
is_array( $type ) ? $type : array( $type ), |
| 602 |
function ( $type ) { |
| 603 |
return in_array( $type, array( 'marketing', 'statistics', 'preferences' ), true ); |
| 604 |
} |
| 605 |
); |
| 606 |
|
| 607 |
if ( count( $type_array ) > 0 ) { |
| 608 |
return ' type="text/plain" data-cookieconsent="' . implode( ',', $type_array ) . '"'; |
| 609 |
} |
| 610 |
|
| 611 |
return ''; |
| 612 |
} |
| 613 |
|
| 614 |
/** |
| 615 |
* Helper function to check if cookiebot is active. |
| 616 |
* Useful for other plugins adding support for Cookiebot. |
| 617 |
* |
| 618 |
* @return bool |
| 619 |
* @since 1.2 |
| 620 |
* @version 2.2.2 |
| 621 |
*/ |
| 622 |
function cookiebot_active() { |
| 623 |
$cbid = Cookiebot_WP::get_cbid(); |
| 624 |
return ! empty( $cbid ); |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* Returns the main instance of Cookiebot_WP to prevent the need to use globals. |
| 629 |
* |
| 630 |
* @return Cookiebot_WP |
| 631 |
* @throws RuntimeException |
| 632 |
* @version 1.0.0 |
| 633 |
* @since 1.0.0 |
| 634 |
*/ |
| 635 |
function cookiebot() { |
| 636 |
return Cookiebot_WP::instance(); |
| 637 |
} |
| 638 |
|
| 639 |
const CYBOT_COOKIEBOT_PLUGIN_ASSETS_DIR = 'assets/'; |
| 640 |
const CYBOT_COOKIEBOT_PLUGIN_LOGO_FILE = 'logo.svg'; |
| 641 |
} |
| 642 |
|