PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.4.1
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.4.1
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 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
334 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 self::get_hooks_by_frame();
12 }
13
14 private function get_hooks_by_frame() {
15 $frame = Cookiebot_Frame::is_cb_frame_type();
16
17 if ( $frame === true ) {
18 // add JS
19 if ( self::is_tcf_enabled() ) {
20 add_action( 'wp_head', array( $this, 'include_publisher_restrictions_js' ), -9999 );
21 }
22 add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), -9998 );
23 add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), -9997 );
24 add_action( 'wp_head', array( $this, 'include_cookiebot_js' ), -9996 );
25 ( new Cookiebot_Declaration_Shortcode() )->register_hooks();
26 }
27
28 if ( $frame === false ) {
29 add_action( 'wp_head', array( $this, 'include_uc_cmp_js' ), -9998 );
30 add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), -9997 );
31 add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), -9996 );
32 }
33 }
34
35 public function include_uc_cmp_js( $return_html = false ) {
36 $cbid = Cookiebot_WP::get_cbid();
37
38 if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) {
39 if (
40 // Is multisite - and disabled output is checked as network setting
41 ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) ||
42 // Do not show JS - output disabled
43 ( get_option( 'cookiebot-nooutput', false ) && ! $return_html ) ||
44 // Do not show js if logged in output is disabled
45 (
46 Cookiebot_WP::can_current_user_edit_theme() &&
47 $return_html === '' &&
48 (
49 get_option( 'cookiebot-output-logged-in' ) === false ||
50 get_option( 'cookiebot-output-logged-in' ) === ''
51 )
52 )
53 ) {
54 return '';
55 }
56
57 $view_path = 'frontend/scripts/uc_frame/uc-cmp-js.php';
58 $view_args = array(
59 'cbid' => $cbid,
60 'ruleset_id' => ! empty( get_option( 'cookiebot-ruleset-id' ) ) ?
61 get_option( 'cookiebot-ruleset-id' ) : 'settings',
62 'iab_enabled' => ! empty( get_option( 'cookiebot-iab' ) ),
63 'auto' => Cookiebot_WP::get_cookie_blocking_mode() === 'auto',
64 );
65
66 if ( $return_html ) {
67 return get_view_html( $view_path, $view_args );
68 } else {
69 include_view( $view_path, $view_args );
70 }
71 }
72 return '';
73 }
74
75 private function is_tcf_enabled() {
76 return ! empty( get_option( 'cookiebot-iab' ) );
77 }
78
79 private function get_data_regions() {
80 $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ?
81 get_option( 'cookiebot-multiple-config' ) :
82 false;
83 $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ?
84 get_option( 'cookiebot-second-banner-regions' ) :
85 false;
86 $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ?
87 get_option( 'cookiebot-second-banner-id' ) :
88 false;
89
90 $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ?
91 get_option( 'cookiebot-multiple-banners' ) :
92 false;
93
94 $regions = array();
95
96 if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) {
97 $regions[ $second_banner_id ] = $second_banner_region;
98 }
99
100 if ( $is_multi_config !== false && $extra_banners ) {
101 foreach ( $extra_banners as $data ) {
102 $regions[ $data['group'] ] = $data['region'];
103 }
104 }
105
106 return $regions;
107 }
108
109 /**
110 * Cookiebot_WP Add Cookiebot JS to <head>
111 *
112 * @param false $return_html
113 *
114 * @return string
115 * @throws InvalidArgumentException
116 */
117 public function include_cookiebot_js( $return_html = false ) {
118 $cbid = Cookiebot_WP::get_cbid();
119 if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) {
120 if (
121 // Is multisite - and disabled output is checked as network setting
122 ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) ||
123 // Do not show JS - output disabled
124 get_option( 'cookiebot-nooutput', false ) ||
125 // Do not show js if logged in output is disabled
126 (
127 Cookiebot_WP::get_cookie_blocking_mode() === 'auto' &&
128 Cookiebot_WP::can_current_user_edit_theme() &&
129 $return_html === '' &&
130 (
131 get_option( 'cookiebot-output-logged-in' ) === false ||
132 get_option( 'cookiebot-output-logged-in' ) === ''
133 )
134 )
135 ) {
136 return '';
137 }
138
139 $lang = cookiebot_get_language_from_setting();
140
141 if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) {
142 $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' );
143 } else {
144 $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' );
145 }
146
147 $view_path = 'frontend/scripts/cb_frame/cookiebot-js.php';
148 $view_args = array(
149 'cbid' => $cbid,
150 'lang' => $lang,
151 'tag_attr' => $tag_attr,
152 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(),
153 'data_regions' => self::get_data_regions(),
154 'tcf' => self::get_tcf_attribute(),
155 );
156
157 if ( $return_html ) {
158 return get_view_html( $view_path, $view_args );
159 } else {
160 include_view( $view_path, $view_args );
161 }
162 }
163 return '';
164 }
165
166 /**
167 * Cookiebot_WP Add Google Tag Manager JS to <head>
168 *
169 * @param bool $return_html
170 *
171 * @return string
172 * @throws InvalidArgumentException
173 */
174 public function include_google_tag_manager_js( $return_html = false ) {
175 $option = get_option( 'cookiebot-gtm' );
176 $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode();
177 $cb_frame = Cookiebot_Frame::is_cb_frame_type();
178
179 if ( $option !== false && $option !== '' ) {
180 $cookiebot_gtm_id = get_option( 'cookiebot-gtm-id' );
181 $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' );
182 $iab = $cb_frame === true && ! empty( get_option( 'cookiebot-iab' ) ) ?
183 get_option( 'cookiebot-iab' ) :
184 false;
185 $cookie_categories = get_option( 'cookiebot-gtm-cookies' );
186
187 $view_path = 'frontend/scripts/common/google-tag-manager-js.php';
188
189 $view_args = array(
190 'gtm_id' => $cookiebot_gtm_id,
191 'data_layer' => $data_layer,
192 'consent_attribute' => $cb_frame === true ?
193 self::get_consent_attribute( $blocking_mode, $cookie_categories ) :
194 false,
195 'iab' => $iab,
196 'script_type' => 'text/javascript',
197 );
198
199 if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) {
200 $view_args['script_type'] = 'text/plain';
201 }
202
203 if ( $return_html ) {
204 return get_view_html( $view_path, $view_args );
205 } else {
206 include_view( $view_path, $view_args );
207 }
208 }
209 return '';
210 }
211
212 /**
213 * Cookiebot_WP Add Google Consent Mode JS to <head>
214 *
215 * @param bool $return_html
216 *
217 * @return string
218 * @throws InvalidArgumentException
219 */
220 public function include_google_consent_mode_js( $return_html = false ) {
221 $option = get_option( 'cookiebot-gcm' );
222 $blocking_mode = Cookiebot_WP::get_cookie_blocking_mode();
223 $cb_frame = Cookiebot_Frame::is_cb_frame_type();
224
225 if ( $option !== false && $option !== '' ) {
226 $data_layer = empty( get_option( 'cookiebot-data-layer' ) ) ? 'dataLayer' : get_option( 'cookiebot-data-layer' );
227 $is_url_passthrough_enabled = $cb_frame === true && ! empty( get_option( 'cookiebot-gcm-url-passthrough' ) ) ?
228 get_option( 'cookiebot-gcm-url-passthrough' ) :
229 false;
230 $cookie_categories = get_option( 'cookiebot-gcm-cookies' );
231
232 $view_path = 'frontend/scripts/common/google-consent-mode-js.php';
233
234 $view_args = array(
235 'data_layer' => $data_layer,
236 'url_passthrough' => $is_url_passthrough_enabled,
237 'consent_attribute' => $cb_frame === true ?
238 self::get_consent_attribute( $blocking_mode, $cookie_categories ) :
239 false,
240 'script_type' => 'text/javascript',
241 );
242
243 if ( $cb_frame === true && $blocking_mode !== 'auto' && ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) {
244 $view_args['script_type'] = 'text/plain';
245 }
246
247 if ( $return_html ) {
248 return get_view_html( $view_path, $view_args );
249 } else {
250 include_view( $view_path, $view_args );
251 }
252 }
253 return '';
254 }
255
256 public function include_publisher_restrictions_js( $return_html = false ) {
257 $view_path = 'frontend/scripts/cb_frame/publisher-restrictions-js.php';
258
259 $custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' );
260 $custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' );
261 $custom_tcf_features = get_option( 'cookiebot-tcf-features' );
262 $custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' );
263 $custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' );
264 $custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' );
265
266 $view_args = array(
267 'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ),
268 'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ),
269 'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ),
270 'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ),
271 'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ),
272 'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ),
273 'vendor_restrictions' => self::get_custom_tcf_restrictions(),
274 );
275 if ( $return_html ) {
276 return get_view_html( $view_path, $view_args );
277 } else {
278 include_view( $view_path, $view_args );
279 }
280 return '';
281 }
282
283 private function get_custom_tcf_ids( $option ) {
284 if ( empty( $option ) ) {
285 return '';
286 }
287
288 return implode( ', ', $option );
289 }
290
291 private function get_custom_tcf_restrictions() {
292 if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) {
293 return '';
294 }
295
296 $custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' );
297
298 $attribute = array();
299
300 foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) {
301 $purposes = is_array( $restrictions ) && array_key_exists( 'purposes', $restrictions ) ? $restrictions['purposes'] : array();
302 $attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $purposes ) . ']}';
303 }
304
305 return implode( ',', $attribute );
306 }
307
308 private function get_consent_attribute( $blocking_mode, $categories ) {
309 $attribute = false;
310
311 if ( $blocking_mode === 'auto' ) {
312 $attribute = 'ignore';
313 }
314
315 if ( $categories && is_array( $categories ) ) {
316 $attribute = join( ', ', $categories );
317 }
318
319 return $attribute;
320 }
321
322 public static function get_tcf_attribute() {
323 $attribute = false;
324 $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) );
325 $tcf_version = get_option( 'cookiebot-tcf-version' );
326
327 if ( $iab_enabled ) {
328 $attribute = $tcf_version;
329 }
330
331 return $attribute;
332 }
333 }
334