buffer
1 year ago
script_loader_tag
1 year ago
traits
1 year ago
Account_Service.php
1 year ago
Consent_API_Helper.php
1 year ago
Cookie_Consent.php
1 year ago
Cookie_Consent_Interface.php
1 year ago
Cookiebot_Activated.php
1 year 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
1 year ago
Cookiebot_Review.php
1 year ago
Cookiebot_WP.php
1 year 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
1 year ago
Cookiebot_Javascript_Helper.php
405 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 | // Simplified banner display check |
| 39 | $banner_enabled = get_option( 'cookiebot-banner-enabled', '1' ); |
| 40 | $user_data = get_option( 'cookiebot-user-data', array() ); |
| 41 | |
| 42 | if ( $banner_enabled === '0' ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if ( empty( $user_data ) ) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | // Check verification and trial status in one pass |
| 51 | if ( isset( $user_data['email_verification_status'] ) && |
| 52 | $user_data['email_verification_status'] === 'unverified' ) { |
| 53 | |
| 54 | // Check trial expiration |
| 55 | $trial_start = $this->parse_date( isset( $user_data['trial_start_date'] ) ? $user_data['trial_start_date'] : '' ); |
| 56 | if ( $trial_start ) { |
| 57 | $fourteen_days_later = clone $trial_start; |
| 58 | $fourteen_days_later->modify( '+14 days' ); |
| 59 | |
| 60 | if ( new \DateTime() > $fourteen_days_later ) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Check trial status |
| 67 | if ( isset( $user_data['subscription_status'] ) && |
| 68 | $user_data['subscription_status'] === 'in_trial_non_billable' ) { |
| 69 | $trial_end = $this->parse_date( isset( $user_data['trial_end_date'] ) ? $user_data['trial_end_date'] : '' ); |
| 70 | if ( $trial_end && new \DateTime() > $trial_end ) { |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | // Helper function for date parsing |
| 79 | private function parse_date( $date_string ) { |
| 80 | if ( empty( $date_string ) ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | $date_string = str_replace( ' T', ' ', $date_string ); |
| 85 | $date = \DateTime::createFromFormat( 'd-m-Y H:i:s', $date_string ); |
| 86 | return $date ? $date : false; |
| 87 | } |
| 88 | |
| 89 | private function debug_log( $message ) { |
| 90 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 91 | // phpcs:ignore |
| 92 | error_log( '[Cookiebot] ' . $message ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public function include_uc_cmp_js( $return_html = false ) { |
| 97 | $cbid = Cookiebot_WP::get_cbid(); |
| 98 | if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) { |
| 99 | $this->debug_log( 'CBID exists and COOKIEBOT_DISABLE_ON_PAGE is not defined' ); |
| 100 | |
| 101 | // Add verification check |
| 102 | if ( ! $this->should_show_banner() ) { |
| 103 | $this->debug_log( 'Banner should not be shown - returning empty' ); |
| 104 | return ''; |
| 105 | } |
| 106 | $this->debug_log( 'Banner should be shown' ); |
| 107 | |
| 108 | if ( |
| 109 | // Is multisite - and disabled output is checked as network setting |
| 110 | ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) || |
| 111 | // Do not show JS - output disabled |
| 112 | ( get_option( 'cookiebot-nooutput', false ) && ! $return_html ) |
| 113 | ) { |
| 114 | return ''; |
| 115 | } |
| 116 | |
| 117 | // Get the banner script |
| 118 | $banner_script = Cookiebot_WP::get_banner_script(); |
| 119 | |
| 120 | if ( empty( $banner_script ) ) { |
| 121 | $this->debug_log( 'Failed to generate banner script - returning empty' ); |
| 122 | return ''; |
| 123 | } |
| 124 | |
| 125 | $this->debug_log( 'Final script to be injected: ' . substr( $banner_script, 0, 100 ) . '...' ); |
| 126 | |
| 127 | if ( $return_html ) { |
| 128 | $this->debug_log( 'Returning HTML' ); |
| 129 | return wp_kses_post( $banner_script ); |
| 130 | } else { |
| 131 | $this->debug_log( 'Echoing HTML' ); |
| 132 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script HTML is generated internally and is safe |
| 133 | echo $banner_script; |
| 134 | } |
| 135 | } else { |
| 136 | $this->debug_log( 'CBID is empty or COOKIEBOT_DISABLE_ON_PAGE is defined' ); |
| 137 | } |
| 138 | return ''; |
| 139 | } |
| 140 | |
| 141 | private function is_tcf_enabled() { |
| 142 | return ! empty( get_option( 'cookiebot-iab' ) ); |
| 143 | } |
| 144 | |
| 145 | private function get_data_regions() { |
| 146 | $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ? |
| 147 | get_option( 'cookiebot-multiple-config' ) : |
| 148 | false; |
| 149 | $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ? |
| 150 | get_option( 'cookiebot-second-banner-regions' ) : |
| 151 | false; |
| 152 | $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ? |
| 153 | get_option( 'cookiebot-second-banner-id' ) : |
| 154 | false; |
| 155 | |
| 156 | $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ? |
| 157 | get_option( 'cookiebot-multiple-banners' ) : |
| 158 | false; |
| 159 | |
| 160 | $regions = array(); |
| 161 | |
| 162 | if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) { |
| 163 | $regions[ $second_banner_id ] = $second_banner_region; |
| 164 | } |
| 165 | |
| 166 | if ( $is_multi_config !== false && $extra_banners ) { |
| 167 | foreach ( $extra_banners as $data ) { |
| 168 | $regions[ $data['group'] ] = $data['region']; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return $regions; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Cookiebot_WP Add Cookiebot JS to <head> |
| 177 | * |
| 178 | * @param false $return_html |
| 179 | * |
| 180 | * @return string |
| 181 | * @throws InvalidArgumentException |
| 182 | */ |
| 183 | public function include_cookiebot_js( $return_html = false ) { |
| 184 | $cbid = Cookiebot_WP::get_cbid(); |
| 185 | if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) { |
| 186 | // Add verification check |
| 187 | if ( ! $this->should_show_banner() ) { |
| 188 | return ''; |
| 189 | } |
| 190 | |
| 191 | if ( |
| 192 | // Is multisite - and disabled output is checked as network setting |
| 193 | ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) || |
| 194 | // Do not show JS - output disabled |
| 195 | get_option( 'cookiebot-nooutput', false ) || |
| 196 | // Do not show js if logged in output is disabled |
| 197 | ( |
| 198 | Cookiebot_WP::get_cookie_blocking_mode() === 'auto' && |
| 199 | Cookiebot_WP::can_current_user_edit_theme() && |
| 200 | $return_html === '' && |
| 201 | ( |
| 202 | get_option( 'cookiebot-output-logged-in' ) === false || |
| 203 | get_option( 'cookiebot-output-logged-in' ) === '' |
| 204 | ) |
| 205 | ) |
| 206 | ) { |
| 207 | return ''; |
| 208 | } |
| 209 | |
| 210 | $lang = cookiebot_get_language_from_setting(); |
| 211 | |
| 212 | if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) { |
| 213 | $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' ); |
| 214 | } else { |
| 215 | $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' ); |
| 216 | } |
| 217 | |
| 218 | $view_path = 'frontend/scripts/cb_frame/cookiebot-js.php'; |
| 219 | $view_args = array( |
| 220 | 'cbid' => $cbid, |
| 221 | 'lang' => $lang, |
| 222 | 'tag_attr' => $tag_attr, |
| 223 | 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(), |
| 224 | 'data_regions' => self::get_data_regions(), |
| 225 | 'tcf' => self::get_tcf_attribute(), |
| 226 | ); |
| 227 | |
| 228 | if ( $return_html ) { |
| 229 | return get_view_html( $view_path, $view_args ); |
| 230 | } else { |
| 231 | include_view( $view_path, $view_args ); |
| 232 | } |
| 233 | } |
| 234 | return ''; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Cookiebot_WP Add Google Tag Manager JS to <head> |
| 239 | * |
| 240 | * @param bool $return_html |
| 241 | * |
| 242 | * @return string |
| 243 | * @throws InvalidArgumentException |
| 244 | */ |
| 245 | public function include_google_tag_manager_js( $return_html = false ) { |
| 246 | $option = get_option( 'cookiebot-gtm' ); |
| 247 | $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 248 | $cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 249 | |
| 250 | if ( $option !== false && $option !== '' ) { |
| 251 | $cookiebot_gtm_id = get_option( 'cookiebot-gtm-id' ); |
| 252 | $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 253 | $iab = $cb_frame === true && ! empty( get_option( 'cookiebot-iab' ) ) ? |
| 254 | get_option( 'cookiebot-iab' ) : |
| 255 | false; |
| 256 | $cookie_categories = get_option( 'cookiebot-gtm-cookies' ); |
| 257 | |
| 258 | $view_path = 'frontend/scripts/common/google-tag-manager-js.php'; |
| 259 | |
| 260 | $view_args = array( |
| 261 | 'gtm_id' => $cookiebot_gtm_id, |
| 262 | 'data_layer' => $data_layer, |
| 263 | 'consent_attribute' => $cb_frame === true ? |
| 264 | self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 265 | false, |
| 266 | 'iab' => $iab, |
| 267 | 'script_type' => 'text/javascript', |
| 268 | ); |
| 269 | |
| 270 | if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 271 | $view_args['script_type'] = 'text/plain'; |
| 272 | } |
| 273 | |
| 274 | if ( $return_html ) { |
| 275 | return get_view_html( $view_path, $view_args ); |
| 276 | } else { |
| 277 | include_view( $view_path, $view_args ); |
| 278 | } |
| 279 | } |
| 280 | return ''; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Cookiebot_WP Add Google Consent Mode JS to <head> |
| 285 | * |
| 286 | * @param bool $return_html |
| 287 | * |
| 288 | * @return string |
| 289 | * @throws InvalidArgumentException |
| 290 | */ |
| 291 | public function include_google_consent_mode_js( $return_html = false ) { |
| 292 | $option = get_option( 'cookiebot-gcm' ); |
| 293 | $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 294 | $cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 295 | |
| 296 | if ( $option !== false && $option !== '' ) { |
| 297 | $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 298 | $is_url_passthrough_enabled = $cb_frame === true && ! empty( get_option( 'cookiebot-gcm-url-passthrough' ) ) ? |
| 299 | get_option( 'cookiebot-gcm-url-passthrough' ) : |
| 300 | false; |
| 301 | $cookie_categories = get_option( 'cookiebot-gcm-cookies' ); |
| 302 | |
| 303 | $view_path = 'frontend/scripts/common/google-consent-mode-js.php'; |
| 304 | |
| 305 | $view_args = array( |
| 306 | 'data_layer' => $data_layer, |
| 307 | 'url_passthrough' => $is_url_passthrough_enabled, |
| 308 | 'consent_attribute' => $cb_frame === true ? |
| 309 | self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 310 | false, |
| 311 | 'script_type' => 'text/javascript', |
| 312 | ); |
| 313 | |
| 314 | if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 315 | $view_args['script_type'] = 'text/plain'; |
| 316 | } |
| 317 | |
| 318 | if ( $return_html ) { |
| 319 | return get_view_html( $view_path, $view_args ); |
| 320 | } else { |
| 321 | include_view( $view_path, $view_args ); |
| 322 | } |
| 323 | } |
| 324 | return ''; |
| 325 | } |
| 326 | |
| 327 | public function include_publisher_restrictions_js( $return_html = false ) { |
| 328 | $view_path = 'frontend/scripts/cb_frame/publisher-restrictions-js.php'; |
| 329 | |
| 330 | $custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' ); |
| 331 | $custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' ); |
| 332 | $custom_tcf_features = get_option( 'cookiebot-tcf-features' ); |
| 333 | $custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' ); |
| 334 | $custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' ); |
| 335 | $custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' ); |
| 336 | |
| 337 | $view_args = array( |
| 338 | 'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ), |
| 339 | 'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ), |
| 340 | 'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ), |
| 341 | 'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ), |
| 342 | 'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ), |
| 343 | 'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ), |
| 344 | 'vendor_restrictions' => self::get_custom_tcf_restrictions(), |
| 345 | ); |
| 346 | if ( $return_html ) { |
| 347 | return get_view_html( $view_path, $view_args ); |
| 348 | } else { |
| 349 | include_view( $view_path, $view_args ); |
| 350 | } |
| 351 | return ''; |
| 352 | } |
| 353 | |
| 354 | private function get_custom_tcf_ids( $option ) { |
| 355 | if ( empty( $option ) ) { |
| 356 | return ''; |
| 357 | } |
| 358 | |
| 359 | return implode( ', ', $option ); |
| 360 | } |
| 361 | |
| 362 | private function get_custom_tcf_restrictions() { |
| 363 | if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) { |
| 364 | return ''; |
| 365 | } |
| 366 | |
| 367 | $custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' ); |
| 368 | |
| 369 | $attribute = array(); |
| 370 | |
| 371 | foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) { |
| 372 | $purposes = is_array( $restrictions ) && array_key_exists( 'purposes', $restrictions ) ? $restrictions['purposes'] : array(); |
| 373 | $attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $purposes ) . ']}'; |
| 374 | } |
| 375 | |
| 376 | return implode( ',', $attribute ); |
| 377 | } |
| 378 | |
| 379 | private function get_consent_attribute( $blocking_mode, $categories ) { |
| 380 | $attribute = false; |
| 381 | |
| 382 | if ( $blocking_mode === 'auto' ) { |
| 383 | $attribute = 'ignore'; |
| 384 | } |
| 385 | |
| 386 | if ( $categories && is_array( $categories ) ) { |
| 387 | $attribute = join( ', ', $categories ); |
| 388 | } |
| 389 | |
| 390 | return $attribute; |
| 391 | } |
| 392 | |
| 393 | public static function get_tcf_attribute() { |
| 394 | $attribute = false; |
| 395 | $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) ); |
| 396 | $tcf_version = get_option( 'cookiebot-tcf-version' ); |
| 397 | |
| 398 | if ( $iab_enabled ) { |
| 399 | $attribute = $tcf_version; |
| 400 | } |
| 401 | |
| 402 | return $attribute; |
| 403 | } |
| 404 | } |
| 405 |