buffer
1 year ago
script_loader_tag
1 year ago
traits
1 year ago
Account_Service.php
10 months ago
Consent_API_Helper.php
1 year ago
Cookie_Consent.php
1 year ago
Cookie_Consent_Interface.php
1 year ago
Cookiebot_Activated.php
10 months ago
Cookiebot_Admin_Links.php
1 year ago
Cookiebot_Automatic_Updates.php
1 year ago
Cookiebot_Deactivated.php
1 year ago
Cookiebot_Frame.php
1 year ago
Cookiebot_Javascript_Helper.php
10 months ago
Cookiebot_Review.php
1 year ago
Cookiebot_WP.php
9 months ago
Dependency_Container.php
1 year ago
Settings_Page_Tab.php
1 year ago
Settings_Service.php
1 year ago
Settings_Service_Interface.php
1 year ago
Supported_Languages.php
1 year ago
Supported_Regions.php
1 year ago
WP_Rocket_Helper.php
1 year ago
Widgets.php
1 year ago
global-deprecations.php
1 year ago
helper.php
10 months ago
Cookiebot_Javascript_Helper.php
347 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\lib; |
| 4 | |
| 5 | use cybot\cookiebot\shortcode\Cookiebot_Declaration_Shortcode; |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | class Cookiebot_Javascript_Helper { |
| 9 | |
| 10 | public function register_hooks() { |
| 11 | // Simplified hooks registration |
| 12 | self::get_hooks_by_frame(); |
| 13 | } |
| 14 | |
| 15 | |
| 16 | private function get_hooks_by_frame() { |
| 17 | $frame = Cookiebot_Frame::is_cb_frame_type(); |
| 18 | |
| 19 | if ( $frame === true ) { |
| 20 | // add JS |
| 21 | if ( self::is_tcf_enabled() ) { |
| 22 | add_action( 'wp_head', array( $this, 'include_publisher_restrictions_js' ), -9999 ); |
| 23 | } |
| 24 | add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), -9998 ); |
| 25 | add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), -9997 ); |
| 26 | add_action( 'wp_head', array( $this, 'include_cookiebot_js' ), -9996 ); |
| 27 | ( new Cookiebot_Declaration_Shortcode() )->register_hooks(); |
| 28 | } |
| 29 | |
| 30 | if ( $frame === false ) { |
| 31 | add_action( 'wp_head', array( $this, 'include_uc_cmp_js' ), -9998 ); |
| 32 | add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), -9997 ); |
| 33 | add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), -9996 ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private function should_show_banner() { |
| 38 | $banner_enabled = get_option( 'cookiebot-banner-enabled', '1' ); |
| 39 | |
| 40 | if ( $banner_enabled !== '1' ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | $user_data = get_option( 'cookiebot-user-data' ); |
| 45 | |
| 46 | if ( empty( $user_data ) ) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | // Check if trial has expired using Cookiebot_WP method |
| 51 | if ( Cookiebot_WP::is_trial_expired() ) { |
| 52 | self::debug_log( 'Trial has expired - banner should not be shown' ); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | private function debug_log( $message ) { |
| 60 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 61 | // phpcs:ignore |
| 62 | error_log( '[Cookiebot] ' . $message ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function include_uc_cmp_js( $return_html = false ) { |
| 67 | $cbid = Cookiebot_WP::get_cbid(); |
| 68 | if ( ! empty( $cbid ) ) { |
| 69 | $this->debug_log( 'CBID exists and COOKIEBOT_DISABLE_ON_PAGE is not defined' ); |
| 70 | |
| 71 | // Add verification check |
| 72 | if ( ! $this->should_show_banner() ) { |
| 73 | $this->debug_log( 'Banner should not be shown - returning empty' ); |
| 74 | return ''; |
| 75 | } |
| 76 | $this->debug_log( 'Banner should be shown' ); |
| 77 | |
| 78 | // Get the banner script |
| 79 | $banner_script = Cookiebot_WP::get_banner_script(); |
| 80 | |
| 81 | if ( empty( $banner_script ) ) { |
| 82 | $this->debug_log( 'Failed to generate banner script - returning empty' ); |
| 83 | return ''; |
| 84 | } |
| 85 | |
| 86 | $this->debug_log( 'Final script to be injected: ' . substr( $banner_script, 0, 100 ) . '...' ); |
| 87 | |
| 88 | if ( $return_html ) { |
| 89 | $this->debug_log( 'Returning HTML' ); |
| 90 | return wp_kses_post( $banner_script ); |
| 91 | } else { |
| 92 | $this->debug_log( 'Echoing HTML' ); |
| 93 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script HTML is generated internally and is safe |
| 94 | echo $banner_script; |
| 95 | } |
| 96 | } else { |
| 97 | $this->debug_log( 'CBID is empty or COOKIEBOT_DISABLE_ON_PAGE is defined' ); |
| 98 | } |
| 99 | return ''; |
| 100 | } |
| 101 | |
| 102 | private function is_tcf_enabled() { |
| 103 | return ! empty( get_option( 'cookiebot-iab' ) ); |
| 104 | } |
| 105 | |
| 106 | private function get_data_regions() { |
| 107 | $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ? |
| 108 | get_option( 'cookiebot-multiple-config' ) : |
| 109 | false; |
| 110 | $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ? |
| 111 | get_option( 'cookiebot-second-banner-regions' ) : |
| 112 | false; |
| 113 | $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ? |
| 114 | get_option( 'cookiebot-second-banner-id' ) : |
| 115 | false; |
| 116 | |
| 117 | $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ? |
| 118 | get_option( 'cookiebot-multiple-banners' ) : |
| 119 | false; |
| 120 | |
| 121 | $regions = array(); |
| 122 | |
| 123 | if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) { |
| 124 | $regions[ $second_banner_id ] = $second_banner_region; |
| 125 | } |
| 126 | |
| 127 | if ( $is_multi_config !== false && $extra_banners ) { |
| 128 | foreach ( $extra_banners as $data ) { |
| 129 | $regions[ $data['group'] ] = $data['region']; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $regions; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Cookiebot_WP Add Cookiebot JS to <head> |
| 138 | * |
| 139 | * @param false $return_html |
| 140 | * |
| 141 | * @return string |
| 142 | * @throws InvalidArgumentException |
| 143 | */ |
| 144 | public function include_cookiebot_js( $return_html = false ) { |
| 145 | $cbid = Cookiebot_WP::get_cbid(); |
| 146 | if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) { |
| 147 | // Add verification check |
| 148 | if ( ! $this->should_show_banner() ) { |
| 149 | return ''; |
| 150 | } |
| 151 | |
| 152 | $lang = cookiebot_get_language_from_setting(); |
| 153 | |
| 154 | if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) { |
| 155 | $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' ); |
| 156 | } else { |
| 157 | $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' ); |
| 158 | } |
| 159 | |
| 160 | $view_path = 'frontend/scripts/cb_frame/cookiebot-js.php'; |
| 161 | $view_args = array( |
| 162 | 'cbid' => $cbid, |
| 163 | 'lang' => $lang, |
| 164 | 'tag_attr' => $tag_attr, |
| 165 | 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(), |
| 166 | 'data_regions' => self::get_data_regions(), |
| 167 | 'tcf' => self::get_tcf_attribute(), |
| 168 | ); |
| 169 | |
| 170 | if ( $return_html ) { |
| 171 | return get_view_html( $view_path, $view_args ); |
| 172 | } else { |
| 173 | include_view( $view_path, $view_args ); |
| 174 | } |
| 175 | } |
| 176 | return ''; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Cookiebot_WP Add Google Tag Manager JS to <head> |
| 181 | * |
| 182 | * @param bool $return_html |
| 183 | * |
| 184 | * @return string |
| 185 | * @throws InvalidArgumentException |
| 186 | */ |
| 187 | public function include_google_tag_manager_js( $return_html = false ) { |
| 188 | $option = get_option( 'cookiebot-gtm' ); |
| 189 | $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 190 | $cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 191 | |
| 192 | if ( $option !== false && $option !== '' ) { |
| 193 | $cookiebot_gtm_id = get_option( 'cookiebot-gtm-id' ); |
| 194 | $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 195 | $iab = $cb_frame === true && ! empty( get_option( 'cookiebot-iab' ) ) ? |
| 196 | get_option( 'cookiebot-iab' ) : |
| 197 | false; |
| 198 | $cookie_categories = get_option( 'cookiebot-gtm-cookies' ); |
| 199 | |
| 200 | $view_path = 'frontend/scripts/common/google-tag-manager-js.php'; |
| 201 | |
| 202 | $view_args = array( |
| 203 | 'gtm_id' => $cookiebot_gtm_id, |
| 204 | 'data_layer' => $data_layer, |
| 205 | 'consent_attribute' => $cb_frame === true ? |
| 206 | self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 207 | false, |
| 208 | 'iab' => $iab, |
| 209 | 'script_type' => 'text/javascript', |
| 210 | ); |
| 211 | |
| 212 | if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 213 | $view_args['script_type'] = 'text/plain'; |
| 214 | } |
| 215 | |
| 216 | if ( $return_html ) { |
| 217 | return get_view_html( $view_path, $view_args ); |
| 218 | } else { |
| 219 | include_view( $view_path, $view_args ); |
| 220 | } |
| 221 | } |
| 222 | return ''; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Cookiebot_WP Add Google Consent Mode JS to <head> |
| 227 | * |
| 228 | * @param bool $return_html |
| 229 | * |
| 230 | * @return string |
| 231 | * @throws InvalidArgumentException |
| 232 | */ |
| 233 | public function include_google_consent_mode_js( $return_html = false ) { |
| 234 | $option = get_option( 'cookiebot-gcm' ); |
| 235 | $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 236 | $cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 237 | |
| 238 | if ( $option !== false && $option !== '' ) { |
| 239 | $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 240 | $is_url_passthrough_enabled = $cb_frame === true && ! empty( get_option( 'cookiebot-gcm-url-passthrough' ) ) ? |
| 241 | get_option( 'cookiebot-gcm-url-passthrough' ) : |
| 242 | false; |
| 243 | $cookie_categories = get_option( 'cookiebot-gcm-cookies' ); |
| 244 | |
| 245 | $view_path = 'frontend/scripts/common/google-consent-mode-js.php'; |
| 246 | |
| 247 | $view_args = array( |
| 248 | 'data_layer' => $data_layer, |
| 249 | 'url_passthrough' => $is_url_passthrough_enabled, |
| 250 | 'consent_attribute' => $cb_frame === true ? |
| 251 | self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 252 | false, |
| 253 | 'script_type' => 'text/javascript', |
| 254 | ); |
| 255 | |
| 256 | if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 257 | $view_args['script_type'] = 'text/plain'; |
| 258 | } |
| 259 | |
| 260 | if ( $return_html ) { |
| 261 | return get_view_html( $view_path, $view_args ); |
| 262 | } else { |
| 263 | include_view( $view_path, $view_args ); |
| 264 | } |
| 265 | } |
| 266 | return ''; |
| 267 | } |
| 268 | |
| 269 | public function include_publisher_restrictions_js( $return_html = false ) { |
| 270 | $view_path = 'frontend/scripts/cb_frame/publisher-restrictions-js.php'; |
| 271 | |
| 272 | $custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' ); |
| 273 | $custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' ); |
| 274 | $custom_tcf_features = get_option( 'cookiebot-tcf-features' ); |
| 275 | $custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' ); |
| 276 | $custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' ); |
| 277 | $custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' ); |
| 278 | |
| 279 | $view_args = array( |
| 280 | 'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ), |
| 281 | 'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ), |
| 282 | 'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ), |
| 283 | 'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ), |
| 284 | 'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ), |
| 285 | 'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ), |
| 286 | 'vendor_restrictions' => self::get_custom_tcf_restrictions(), |
| 287 | ); |
| 288 | if ( $return_html ) { |
| 289 | return get_view_html( $view_path, $view_args ); |
| 290 | } else { |
| 291 | include_view( $view_path, $view_args ); |
| 292 | } |
| 293 | return ''; |
| 294 | } |
| 295 | |
| 296 | private function get_custom_tcf_ids( $option ) { |
| 297 | if ( empty( $option ) ) { |
| 298 | return ''; |
| 299 | } |
| 300 | |
| 301 | return implode( ', ', $option ); |
| 302 | } |
| 303 | |
| 304 | private function get_custom_tcf_restrictions() { |
| 305 | if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) { |
| 306 | return ''; |
| 307 | } |
| 308 | |
| 309 | $custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' ); |
| 310 | |
| 311 | $attribute = array(); |
| 312 | |
| 313 | foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) { |
| 314 | $purposes = is_array( $restrictions ) && array_key_exists( 'purposes', $restrictions ) ? $restrictions['purposes'] : array(); |
| 315 | $attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $purposes ) . ']}'; |
| 316 | } |
| 317 | |
| 318 | return implode( ',', $attribute ); |
| 319 | } |
| 320 | |
| 321 | private function get_consent_attribute( $blocking_mode, $categories ) { |
| 322 | $attribute = false; |
| 323 | |
| 324 | if ( $blocking_mode === 'auto' ) { |
| 325 | $attribute = 'ignore'; |
| 326 | } |
| 327 | |
| 328 | if ( $categories && is_array( $categories ) ) { |
| 329 | $attribute = join( ', ', $categories ); |
| 330 | } |
| 331 | |
| 332 | return $attribute; |
| 333 | } |
| 334 | |
| 335 | public static function get_tcf_attribute() { |
| 336 | $attribute = false; |
| 337 | $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) ); |
| 338 | $tcf_version = get_option( 'cookiebot-tcf-version' ); |
| 339 | |
| 340 | if ( $iab_enabled ) { |
| 341 | $attribute = $tcf_version; |
| 342 | } |
| 343 | |
| 344 | return $attribute; |
| 345 | } |
| 346 | } |
| 347 |