| 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 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script HTML is generated internally and is safe |
| 91 |
return $banner_script; |
| 92 |
} else { |
| 93 |
$this->debug_log( 'Echoing HTML' ); |
| 94 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script HTML is generated internally and is safe |
| 95 |
echo $banner_script; |
| 96 |
} |
| 97 |
} else { |
| 98 |
$this->debug_log( 'CBID is empty or COOKIEBOT_DISABLE_ON_PAGE is defined' ); |
| 99 |
} |
| 100 |
return ''; |
| 101 |
} |
| 102 |
|
| 103 |
private function is_tcf_enabled() { |
| 104 |
return ! empty( get_option( 'cookiebot-iab' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
private function get_data_regions() { |
| 108 |
$is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ? |
| 109 |
get_option( 'cookiebot-multiple-config' ) : |
| 110 |
false; |
| 111 |
$second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ? |
| 112 |
get_option( 'cookiebot-second-banner-regions' ) : |
| 113 |
false; |
| 114 |
$second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ? |
| 115 |
get_option( 'cookiebot-second-banner-id' ) : |
| 116 |
false; |
| 117 |
|
| 118 |
$extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ? |
| 119 |
get_option( 'cookiebot-multiple-banners' ) : |
| 120 |
false; |
| 121 |
|
| 122 |
$regions = array(); |
| 123 |
|
| 124 |
if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) { |
| 125 |
$regions[ $second_banner_id ] = $second_banner_region; |
| 126 |
} |
| 127 |
|
| 128 |
if ( $is_multi_config !== false && $extra_banners ) { |
| 129 |
foreach ( $extra_banners as $data ) { |
| 130 |
$regions[ $data['group'] ] = $data['region']; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $regions; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Cookiebot_WP Add Cookiebot JS to <head> |
| 139 |
* |
| 140 |
* @param false $return_html |
| 141 |
* |
| 142 |
* @return string |
| 143 |
* @throws InvalidArgumentException |
| 144 |
*/ |
| 145 |
public function include_cookiebot_js( $return_html = false ) { |
| 146 |
$cbid = Cookiebot_WP::get_cbid(); |
| 147 |
if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) { |
| 148 |
// Add verification check |
| 149 |
if ( ! $this->should_show_banner() ) { |
| 150 |
return ''; |
| 151 |
} |
| 152 |
|
| 153 |
$lang = cookiebot_get_language_from_setting(); |
| 154 |
|
| 155 |
if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) { |
| 156 |
$tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' ); |
| 157 |
} else { |
| 158 |
$tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' ); |
| 159 |
} |
| 160 |
|
| 161 |
$view_path = 'frontend/scripts/cb_frame/cookiebot-js.php'; |
| 162 |
$view_args = array( |
| 163 |
'cbid' => $cbid, |
| 164 |
'lang' => $lang, |
| 165 |
'tag_attr' => $tag_attr, |
| 166 |
'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(), |
| 167 |
'data_regions' => self::get_data_regions(), |
| 168 |
'tcf' => self::get_tcf_attribute(), |
| 169 |
); |
| 170 |
|
| 171 |
if ( $return_html ) { |
| 172 |
return get_view_html( $view_path, $view_args ); |
| 173 |
} else { |
| 174 |
include_view( $view_path, $view_args ); |
| 175 |
} |
| 176 |
} |
| 177 |
return ''; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Cookiebot_WP Add Google Tag Manager JS to <head> |
| 182 |
* |
| 183 |
* @param bool $return_html |
| 184 |
* |
| 185 |
* @return string |
| 186 |
* @throws InvalidArgumentException |
| 187 |
*/ |
| 188 |
public function include_google_tag_manager_js( $return_html = false ) { |
| 189 |
$option = get_option( 'cookiebot-gtm' ); |
| 190 |
$blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 191 |
$cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 192 |
|
| 193 |
if ( $option !== false && $option !== '' ) { |
| 194 |
$cookiebot_gtm_id = get_option( 'cookiebot-gtm-id' ); |
| 195 |
$data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 196 |
$iab = $cb_frame === true && ! empty( get_option( 'cookiebot-iab' ) ) ? |
| 197 |
get_option( 'cookiebot-iab' ) : |
| 198 |
false; |
| 199 |
$cookie_categories = get_option( 'cookiebot-gtm-cookies' ); |
| 200 |
|
| 201 |
$view_path = 'frontend/scripts/common/google-tag-manager-js.php'; |
| 202 |
|
| 203 |
$view_args = array( |
| 204 |
'gtm_id' => $cookiebot_gtm_id, |
| 205 |
'data_layer' => $data_layer, |
| 206 |
'consent_attribute' => $cb_frame === true ? |
| 207 |
self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 208 |
false, |
| 209 |
'iab' => $iab, |
| 210 |
'script_type' => 'text/javascript', |
| 211 |
); |
| 212 |
|
| 213 |
if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 214 |
$view_args['script_type'] = 'text/plain'; |
| 215 |
} |
| 216 |
|
| 217 |
if ( $return_html ) { |
| 218 |
return get_view_html( $view_path, $view_args ); |
| 219 |
} else { |
| 220 |
include_view( $view_path, $view_args ); |
| 221 |
} |
| 222 |
} |
| 223 |
return ''; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Cookiebot_WP Add Google Consent Mode JS to <head> |
| 228 |
* |
| 229 |
* @param bool $return_html |
| 230 |
* |
| 231 |
* @return string |
| 232 |
* @throws InvalidArgumentException |
| 233 |
*/ |
| 234 |
public function include_google_consent_mode_js( $return_html = false ) { |
| 235 |
$option = get_option( 'cookiebot-gcm' ); |
| 236 |
$blocking_mode = Cookiebot_WP::get_cookie_blocking_mode(); |
| 237 |
$cb_frame = Cookiebot_Frame::is_cb_frame_type(); |
| 238 |
|
| 239 |
if ( $option !== false && $option !== '' ) { |
| 240 |
$data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' ); |
| 241 |
$is_url_passthrough_enabled = $cb_frame === true && ! empty( get_option( 'cookiebot-gcm-url-passthrough' ) ) ? |
| 242 |
get_option( 'cookiebot-gcm-url-passthrough' ) : |
| 243 |
false; |
| 244 |
$cookie_categories = get_option( 'cookiebot-gcm-cookies' ); |
| 245 |
|
| 246 |
$view_path = 'frontend/scripts/common/google-consent-mode-js.php'; |
| 247 |
|
| 248 |
$view_args = array( |
| 249 |
'data_layer' => $data_layer, |
| 250 |
'url_passthrough' => $is_url_passthrough_enabled, |
| 251 |
'consent_attribute' => $cb_frame === true ? |
| 252 |
self::get_consent_attribute( $blocking_mode, $cookie_categories ) : |
| 253 |
false, |
| 254 |
'script_type' => 'text/javascript', |
| 255 |
); |
| 256 |
|
| 257 |
if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) { |
| 258 |
$view_args['script_type'] = 'text/plain'; |
| 259 |
} |
| 260 |
|
| 261 |
if ( $return_html ) { |
| 262 |
return get_view_html( $view_path, $view_args ); |
| 263 |
} else { |
| 264 |
include_view( $view_path, $view_args ); |
| 265 |
} |
| 266 |
} |
| 267 |
return ''; |
| 268 |
} |
| 269 |
|
| 270 |
public function include_publisher_restrictions_js( $return_html = false ) { |
| 271 |
$view_path = 'frontend/scripts/cb_frame/publisher-restrictions-js.php'; |
| 272 |
|
| 273 |
$custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' ); |
| 274 |
$custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' ); |
| 275 |
$custom_tcf_features = get_option( 'cookiebot-tcf-features' ); |
| 276 |
$custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' ); |
| 277 |
$custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' ); |
| 278 |
$custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' ); |
| 279 |
|
| 280 |
$view_args = array( |
| 281 |
'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ), |
| 282 |
'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ), |
| 283 |
'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ), |
| 284 |
'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ), |
| 285 |
'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ), |
| 286 |
'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ), |
| 287 |
'vendor_restrictions' => self::get_custom_tcf_restrictions(), |
| 288 |
); |
| 289 |
if ( $return_html ) { |
| 290 |
return get_view_html( $view_path, $view_args ); |
| 291 |
} else { |
| 292 |
include_view( $view_path, $view_args ); |
| 293 |
} |
| 294 |
return ''; |
| 295 |
} |
| 296 |
|
| 297 |
private function get_custom_tcf_ids( $option ) { |
| 298 |
if ( empty( $option ) ) { |
| 299 |
return ''; |
| 300 |
} |
| 301 |
|
| 302 |
return implode( ', ', $option ); |
| 303 |
} |
| 304 |
|
| 305 |
private function get_custom_tcf_restrictions() { |
| 306 |
if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) { |
| 307 |
return ''; |
| 308 |
} |
| 309 |
|
| 310 |
$custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' ); |
| 311 |
|
| 312 |
$attribute = array(); |
| 313 |
|
| 314 |
foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) { |
| 315 |
$purposes = is_array( $restrictions ) && array_key_exists( 'purposes', $restrictions ) ? $restrictions['purposes'] : array(); |
| 316 |
$attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $purposes ) . ']}'; |
| 317 |
} |
| 318 |
|
| 319 |
return implode( ',', $attribute ); |
| 320 |
} |
| 321 |
|
| 322 |
private function get_consent_attribute( $blocking_mode, $categories ) { |
| 323 |
$attribute = false; |
| 324 |
|
| 325 |
if ( $blocking_mode === 'auto' ) { |
| 326 |
$attribute = 'ignore'; |
| 327 |
} |
| 328 |
|
| 329 |
if ( $categories && is_array( $categories ) ) { |
| 330 |
$attribute = join( ', ', $categories ); |
| 331 |
} |
| 332 |
|
| 333 |
return $attribute; |
| 334 |
} |
| 335 |
|
| 336 |
public static function get_tcf_attribute() { |
| 337 |
$attribute = false; |
| 338 |
$iab_enabled = ! empty( get_option( 'cookiebot-iab' ) ); |
| 339 |
$tcf_version = get_option( 'cookiebot-tcf-version' ); |
| 340 |
|
| 341 |
if ( $iab_enabled ) { |
| 342 |
$attribute = $tcf_version; |
| 343 |
} |
| 344 |
|
| 345 |
return $attribute; |
| 346 |
} |
| 347 |
} |
| 348 |
|