buffer
5 years ago
ioc
6 years ago
script-loader-tag
7 years ago
autoloader.php
7 years ago
cookie-consent-interface.php
7 years ago
cookie-consent.php
7 years ago
helper.php
5 years ago
settings-service-interface.php
5 years ago
settings-service.php
5 years ago
theme-settings-service.php
5 years ago
helper.php
369 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 | return false; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Removes action with class in callback |
| 31 | * |
| 32 | * @param $action string action name |
| 33 | * @param $class string class name |
| 34 | * @param $method string method name |
| 35 | * @param $priority integer action priority number |
| 36 | * |
| 37 | * @return boolean True if the action hook is deleted |
| 38 | * False If the action hook is not deleted |
| 39 | * |
| 40 | * @since 1.2.0 |
| 41 | */ |
| 42 | function cookiebot_addons_remove_class_action( $action, $class, $method, $priority = 10 ) { |
| 43 | global $wp_filter; |
| 44 | $deleted = false; |
| 45 | |
| 46 | if ( isset( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ][ $priority ] ) ) { |
| 47 | $len = strlen( $method ); |
| 48 | foreach ( $wp_filter[ $action ][ $priority ] as $name => $def ) { |
| 49 | if ( substr( $name, - $len ) == $method ) { |
| 50 | if ( is_array( $def['function'] ) ) { |
| 51 | if ( is_string( $def['function'][0] ) !== false ) { |
| 52 | $def_class = $def['function'][0]; |
| 53 | } else { |
| 54 | $def_class = get_class( $def['function'][0] ); |
| 55 | } |
| 56 | |
| 57 | if ( $def_class == $class ) { |
| 58 | if ( is_object( $wp_filter[ $action ] ) && isset( $wp_filter[ $action ]->callbacks ) ) { |
| 59 | $wp_filter[ $action ]->remove_filter( $action, $name, $priority ); |
| 60 | $deleted = true; |
| 61 | } else { |
| 62 | unset( $wp_filter[ $action ][ $priority ][ $name ] ); |
| 63 | $deleted = true; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return $deleted; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Custom manipulation of the script |
| 76 | * |
| 77 | * @param $buffer |
| 78 | * @param $keywords |
| 79 | * @param $cookie_type |
| 80 | * |
| 81 | * @return mixed|null|string|string[] |
| 82 | * |
| 83 | * @version 2.0.4 |
| 84 | * @since 1.2.0 |
| 85 | */ |
| 86 | function cookiebot_addons_manipulate_script( $buffer, $keywords ) { |
| 87 | /** |
| 88 | * normalize potential self-closing script tags |
| 89 | */ |
| 90 | |
| 91 | $normalized_buffer = preg_replace('/(<script(.*?)\/>)/is', '<script$2></script>', $buffer); |
| 92 | |
| 93 | if($normalized_buffer !== null) { |
| 94 | $buffer = $normalized_buffer; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Pattern to get all scripts |
| 101 | * |
| 102 | * @version 2.0.4 |
| 103 | * @since 1.2.0 |
| 104 | */ |
| 105 | $pattern = '/(<script(?:.*?)>)(.*?)(<\/script>)/is'; |
| 106 | |
| 107 | /** |
| 108 | * Get all scripts and add cookieconsent if it does match with the criterion |
| 109 | */ |
| 110 | $updated_scripts = preg_replace_callback( $pattern, function ( $matches ) use ( $keywords ) { |
| 111 | |
| 112 | $script = $matches[0]; // the full script html |
| 113 | $script_tag_open = $matches[1]; // only the script open tag with all attributes |
| 114 | $script_tag_inner = $matches[2]; // only the script's innerText |
| 115 | $script_tag_close = $matches[3]; // only the script closing tag |
| 116 | |
| 117 | /** |
| 118 | * Check if the script contains the keywords, checks keywords one by one |
| 119 | * |
| 120 | * If one match, then the rest of the keywords will be skipped. |
| 121 | **/ |
| 122 | foreach ( $keywords as $needle => $cookie_type ) { |
| 123 | /** |
| 124 | * The script contains the needle |
| 125 | **/ |
| 126 | if ( strpos( $script, $needle ) !== false ) { |
| 127 | /** |
| 128 | * replace all single quotes with double quotes in the open tag |
| 129 | * remove previously set data-cookieconsent attribute |
| 130 | * remove type attribute |
| 131 | */ |
| 132 | $script_tag_open = preg_replace('/\'/', '"', $script_tag_open); |
| 133 | $script_tag_open = preg_replace('/\sdata-cookieconsent=\"(?:.*?)\"/', '', $script_tag_open); |
| 134 | $script_tag_open = preg_replace( '/\stype=\"(?:.*?)\"/', '', $script_tag_open ); |
| 135 | |
| 136 | /** |
| 137 | * set the type attribute to text/plain to prevent javascript execution |
| 138 | * add data-cookieconsent attribute |
| 139 | */ |
| 140 | $cookie_types = cookiebot_addons_output_cookie_types( $cookie_type ); |
| 141 | $replacement = '<script type="text/plain" data-cookieconsent="' . $cookie_types . '"'; |
| 142 | $script_tag_open = preg_replace( '/<script/', $replacement, $script_tag_open ); |
| 143 | |
| 144 | /** |
| 145 | * reconstruct the script and break the foreach loop |
| 146 | */ |
| 147 | $script = $script_tag_open . $script_tag_inner . $script_tag_close; |
| 148 | continue; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * return the reconstructed script |
| 154 | */ |
| 155 | return $script; |
| 156 | }, $buffer ); |
| 157 | |
| 158 | /** |
| 159 | * Fallback when the regex fails to work due to PCRE_ERROR_JIT_STACKLIMIT |
| 160 | * |
| 161 | * @version 2.0.4 |
| 162 | * @since 2.0.4 |
| 163 | */ |
| 164 | if ( $updated_scripts === null ) { |
| 165 | $updated_scripts = $buffer; |
| 166 | |
| 167 | if ( get_option( 'cookiebot_regex_stacklimit' ) === false ) { |
| 168 | update_option( 'cookiebot_regex_stacklimit', 1 ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return $updated_scripts; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Compares array to string to add checked attribute in checkbox |
| 177 | * |
| 178 | * @param $helper |
| 179 | * @param $current |
| 180 | * @param bool $echo |
| 181 | * @param string $type |
| 182 | * |
| 183 | * @return string |
| 184 | * |
| 185 | * @since 1.3.0 |
| 186 | */ |
| 187 | function cookiebot_addons_checked_selected_helper( $helper, $current, $echo = true, $type = 'checked' ) { |
| 188 | if ( is_array( $helper ) && in_array( $current, $helper ) ) { |
| 189 | $result = " $type='$type'"; |
| 190 | } elseif ( is_string( $helper ) && is_string( $current ) && $helper === $current ) { |
| 191 | $result = " $type='$type'"; |
| 192 | } else { |
| 193 | $result = ''; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | if ( $echo ) { |
| 198 | echo $result; |
| 199 | } |
| 200 | |
| 201 | return $result; |
| 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( ', ', array_map( function ( $value ) { |
| 218 | return cookiebot_translate_type_name( $value ); |
| 219 | }, $cookie_types ) ); |
| 220 | } elseif ( is_string( $cookie_types ) && $cookie_types != '' ) { |
| 221 | return cookiebot_translate_type_name( $cookie_types ); |
| 222 | } |
| 223 | |
| 224 | return cookiebot_translate_type_name( 'statistics' ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Translates the cookie type to different language |
| 229 | * |
| 230 | * @param $type string |
| 231 | * |
| 232 | * @return string |
| 233 | * |
| 234 | * @since 3.9.1 |
| 235 | */ |
| 236 | function cookiebot_translate_type_name( $type ) { |
| 237 | switch ( $type ) { |
| 238 | case 'marketing': |
| 239 | return esc_html__( 'marketing', 'cookiebot' ); |
| 240 | case 'statistics': |
| 241 | return esc_html__( 'statistics', 'cookiebot' ); |
| 242 | case 'preferences': |
| 243 | return esc_html__( 'preferences', 'cookiebot' ); |
| 244 | case 'necessary': |
| 245 | return esc_html__( 'necessary', 'cookiebot' ); |
| 246 | default: |
| 247 | return $type; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Return 1 cookie type if more than 1 is selected |
| 253 | * |
| 254 | * @param $cookie_types |
| 255 | * |
| 256 | * @return string |
| 257 | * |
| 258 | * @since 1.3.0 |
| 259 | */ |
| 260 | function cookiebot_addons_get_one_cookie_type( $cookie_types ) { |
| 261 | if ( is_array( $cookie_types ) ) { |
| 262 | if ( in_array( 'marketing', $cookie_types ) ) { |
| 263 | return 'marketing'; |
| 264 | } elseif ( in_array( 'statistics', $cookie_types ) ) { |
| 265 | return 'statistics'; |
| 266 | } elseif ( in_array( 'preferences', $cookie_types ) ) { |
| 267 | return 'preferences'; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return ''; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @param $cookie_types |
| 276 | * |
| 277 | * @return string |
| 278 | * |
| 279 | * @version 3.9.0 |
| 280 | */ |
| 281 | function cookiebot_addons_cookieconsent_optout( $cookie_types ) { |
| 282 | $output = ''; |
| 283 | |
| 284 | foreach ( $cookie_types as $cookie_type ) { |
| 285 | $output .= 'cookieconsent-optout-' . $cookie_type . ' '; |
| 286 | } |
| 287 | |
| 288 | return trim( $output ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Returns current site language |
| 293 | * |
| 294 | * @return mixed|string |
| 295 | * |
| 296 | * @since 1.9.0 |
| 297 | */ |
| 298 | function cookiebot_addons_get_language() { |
| 299 | $lang = get_locale(); //Gets language in en-US format |
| 300 | |
| 301 | /** |
| 302 | * Add support for 3rd party plugins |
| 303 | */ |
| 304 | $lang = apply_filters( 'cookiebot_addons_language', $lang ); |
| 305 | |
| 306 | return $lang; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get supported languages by the cookiebot |
| 311 | * |
| 312 | * @return array |
| 313 | * |
| 314 | * @since 1.9.0 |
| 315 | */ |
| 316 | function cookiebot_addons_get_supported_languages() { |
| 317 | $cookiebot = cookiebot(); |
| 318 | |
| 319 | return $cookiebot->get_supported_languages(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Show languages in a select field |
| 324 | * |
| 325 | * @param $class |
| 326 | * @param $name |
| 327 | * @param $selected |
| 328 | * |
| 329 | * @return mixed |
| 330 | * |
| 331 | * @since 1.8.0 |
| 332 | */ |
| 333 | function cookiebot_addons_get_dropdown_languages( $class, $name, $selected ) { |
| 334 | $args = array( |
| 335 | 'name' => $name, |
| 336 | 'selected' => $selected, |
| 337 | 'show_option_site_default' => true, |
| 338 | 'echo' => false, |
| 339 | 'languages' => get_available_languages() |
| 340 | ); |
| 341 | $dropdown = wp_dropdown_languages( $args ); |
| 342 | |
| 343 | $output = str_replace( 'select ', 'select class="' . $class . '" ', $dropdown ); |
| 344 | |
| 345 | $output = str_replace( 'value="" ', 'value="en_US" ', $output ); |
| 346 | |
| 347 | return $output; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Run actions when the cookiebot plugin is deactivated |
| 352 | * |
| 353 | * @since 2.2.0 |
| 354 | */ |
| 355 | function cookiebot_addons_plugin_deactivated( ) { |
| 356 | $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance(); |
| 357 | $cookiebot_addons->cookiebot_deactivated(); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Run actions when the cookiebot plugin is deactivated |
| 362 | * |
| 363 | * @since 3.6.3 |
| 364 | */ |
| 365 | function cookiebot_addons_plugin_activated( ) { |
| 366 | $cookiebot_addons = \cookiebot_addons\Cookiebot_Addons::instance(); |
| 367 | $cookiebot_addons->cookiebot_activated(); |
| 368 | } |
| 369 |