PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.5.5
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.5.5
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / src / lib / Cookiebot_Javascript_Helper.php
cookiebot / src / lib Last commit date
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
353 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 if (
79 ( is_multisite() && ! $return_html )
80 ) {
81 return '';
82 }
83
84 // Get the banner script
85 $banner_script = Cookiebot_WP::get_banner_script();
86
87 if ( empty( $banner_script ) ) {
88 $this->debug_log( 'Failed to generate banner script - returning empty' );
89 return '';
90 }
91
92 $this->debug_log( 'Final script to be injected: ' . substr( $banner_script, 0, 100 ) . '...' );
93
94 if ( $return_html ) {
95 $this->debug_log( 'Returning HTML' );
96 return wp_kses_post( $banner_script );
97 } else {
98 $this->debug_log( 'Echoing HTML' );
99 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Script HTML is generated internally and is safe
100 echo $banner_script;
101 }
102 } else {
103 $this->debug_log( 'CBID is empty or COOKIEBOT_DISABLE_ON_PAGE is defined' );
104 }
105 return '';
106 }
107
108 private function is_tcf_enabled() {
109 return ! empty( get_option( 'cookiebot-iab' ) );
110 }
111
112 private function get_data_regions() {
113 $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ?
114 get_option( 'cookiebot-multiple-config' ) :
115 false;
116 $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ?
117 get_option( 'cookiebot-second-banner-regions' ) :
118 false;
119 $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ?
120 get_option( 'cookiebot-second-banner-id' ) :
121 false;
122
123 $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ?
124 get_option( 'cookiebot-multiple-banners' ) :
125 false;
126
127 $regions = array();
128
129 if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) {
130 $regions[ $second_banner_id ] = $second_banner_region;
131 }
132
133 if ( $is_multi_config !== false && $extra_banners ) {
134 foreach ( $extra_banners as $data ) {
135 $regions[ $data['group'] ] = $data['region'];
136 }
137 }
138
139 return $regions;
140 }
141
142 /**
143 * Cookiebot_WP Add Cookiebot JS to <head>
144 *
145 * @param false $return_html
146 *
147 * @return string
148 * @throws InvalidArgumentException
149 */
150 public function include_cookiebot_js( $return_html = false ) {
151 $cbid = Cookiebot_WP::get_cbid();
152 if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) {
153 // Add verification check
154 if ( ! $this->should_show_banner() ) {
155 return '';
156 }
157
158 $lang = cookiebot_get_language_from_setting();
159
160 if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) {
161 $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' );
162 } else {
163 $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' );
164 }
165
166 $view_path = 'frontend/scripts/cb_frame/cookiebot-js.php';
167 $view_args = array(
168 'cbid' => $cbid,
169 'lang' => $lang,
170 'tag_attr' => $tag_attr,
171 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(),
172 'data_regions' => self::get_data_regions(),
173 'tcf' => self::get_tcf_attribute(),
174 );
175
176 if ( $return_html ) {
177 return get_view_html( $view_path, $view_args );
178 } else {
179 include_view( $view_path, $view_args );
180 }
181 }
182 return '';
183 }
184
185 /**
186 * Cookiebot_WP Add Google Tag Manager JS to <head>
187 *
188 * @param bool $return_html
189 *
190 * @return string
191 * @throws InvalidArgumentException
192 */
193 public function include_google_tag_manager_js( $return_html = false ) {
194 $option = get_option( 'cookiebot-gtm' );
195 $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode();
196 $cb_frame = Cookiebot_Frame::is_cb_frame_type();
197
198 if ( $option !== false && $option !== '' ) {
199 $cookiebot_gtm_id = get_option( 'cookiebot-gtm-id' );
200 $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' );
201 $iab = $cb_frame === true && ! empty( get_option( 'cookiebot-iab' ) ) ?
202 get_option( 'cookiebot-iab' ) :
203 false;
204 $cookie_categories = get_option( 'cookiebot-gtm-cookies' );
205
206 $view_path = 'frontend/scripts/common/google-tag-manager-js.php';
207
208 $view_args = array(
209 'gtm_id' => $cookiebot_gtm_id,
210 'data_layer' => $data_layer,
211 'consent_attribute' => $cb_frame === true ?
212 self::get_consent_attribute( $blocking_mode, $cookie_categories ) :
213 false,
214 'iab' => $iab,
215 'script_type' => 'text/javascript',
216 );
217
218 if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) {
219 $view_args['script_type'] = 'text/plain';
220 }
221
222 if ( $return_html ) {
223 return get_view_html( $view_path, $view_args );
224 } else {
225 include_view( $view_path, $view_args );
226 }
227 }
228 return '';
229 }
230
231 /**
232 * Cookiebot_WP Add Google Consent Mode JS to <head>
233 *
234 * @param bool $return_html
235 *
236 * @return string
237 * @throws InvalidArgumentException
238 */
239 public function include_google_consent_mode_js( $return_html = false ) {
240 $option = get_option( 'cookiebot-gcm' );
241 $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode();
242 $cb_frame = Cookiebot_Frame::is_cb_frame_type();
243
244 if ( $option !== false && $option !== '' ) {
245 $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' );
246 $is_url_passthrough_enabled = $cb_frame === true && ! empty( get_option( 'cookiebot-gcm-url-passthrough' ) ) ?
247 get_option( 'cookiebot-gcm-url-passthrough' ) :
248 false;
249 $cookie_categories = get_option( 'cookiebot-gcm-cookies' );
250
251 $view_path = 'frontend/scripts/common/google-consent-mode-js.php';
252
253 $view_args = array(
254 'data_layer' => $data_layer,
255 'url_passthrough' => $is_url_passthrough_enabled,
256 'consent_attribute' => $cb_frame === true ?
257 self::get_consent_attribute( $blocking_mode, $cookie_categories ) :
258 false,
259 'script_type' => 'text/javascript',
260 );
261
262 if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) {
263 $view_args['script_type'] = 'text/plain';
264 }
265
266 if ( $return_html ) {
267 return get_view_html( $view_path, $view_args );
268 } else {
269 include_view( $view_path, $view_args );
270 }
271 }
272 return '';
273 }
274
275 public function include_publisher_restrictions_js( $return_html = false ) {
276 $view_path = 'frontend/scripts/cb_frame/publisher-restrictions-js.php';
277
278 $custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' );
279 $custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' );
280 $custom_tcf_features = get_option( 'cookiebot-tcf-features' );
281 $custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' );
282 $custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' );
283 $custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' );
284
285 $view_args = array(
286 'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ),
287 'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ),
288 'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ),
289 'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ),
290 'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ),
291 'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ),
292 'vendor_restrictions' => self::get_custom_tcf_restrictions(),
293 );
294 if ( $return_html ) {
295 return get_view_html( $view_path, $view_args );
296 } else {
297 include_view( $view_path, $view_args );
298 }
299 return '';
300 }
301
302 private function get_custom_tcf_ids( $option ) {
303 if ( empty( $option ) ) {
304 return '';
305 }
306
307 return implode( ', ', $option );
308 }
309
310 private function get_custom_tcf_restrictions() {
311 if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) {
312 return '';
313 }
314
315 $custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' );
316
317 $attribute = array();
318
319 foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) {
320 $purposes = is_array( $restrictions ) && array_key_exists( 'purposes', $restrictions ) ? $restrictions['purposes'] : array();
321 $attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $purposes ) . ']}';
322 }
323
324 return implode( ',', $attribute );
325 }
326
327 private function get_consent_attribute( $blocking_mode, $categories ) {
328 $attribute = false;
329
330 if ( $blocking_mode === 'auto' ) {
331 $attribute = 'ignore';
332 }
333
334 if ( $categories && is_array( $categories ) ) {
335 $attribute = join( ', ', $categories );
336 }
337
338 return $attribute;
339 }
340
341 public static function get_tcf_attribute() {
342 $attribute = false;
343 $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) );
344 $tcf_version = get_option( 'cookiebot-tcf-version' );
345
346 if ( $iab_enabled ) {
347 $attribute = $tcf_version;
348 }
349
350 return $attribute;
351 }
352 }
353