PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.3.6
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.3.6
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 3 years ago script_loader_tag 2 years ago traits 3 years ago Consent_API_Helper.php 2 years ago Cookie_Consent.php 3 years ago Cookie_Consent_Interface.php 4 years ago Cookiebot_Activated.php 3 years ago Cookiebot_Automatic_Updates.php 3 years ago Cookiebot_Deactivated.php 4 years ago Cookiebot_Javascript_Helper.php 2 years ago Cookiebot_Review.php 3 years ago Cookiebot_WP.php 2 years ago Dependency_Container.php 3 years ago Settings_Page_Tab.php 3 years ago Settings_Service.php 3 years ago Settings_Service_Interface.php 3 years ago Supported_Languages.php 4 years ago Supported_Regions.php 2 years ago WP_Rocket_Helper.php 3 years ago Widgets.php 3 years ago global-deprecations.php 3 years ago helper.php 2 years ago
Cookiebot_Javascript_Helper.php
269 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 public function register_hooks() {
10 if ( is_admin() && ! Cookiebot_WP::cookiebot_disabled_in_admin() ) {
11 // adding cookie banner in admin area too
12 add_action( 'admin_head', array( $this, 'include_cookiebot_js' ), - 9999 );
13 }
14
15 // add JS
16 if ( self::is_tcf_enabled() ) {
17 add_action( 'wp_head', array( $this, 'include_publisher_restrictions_js' ), -9999 );
18 }
19 add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), - 9998 );
20 add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), - 9997 );
21 add_action( 'wp_head', array( $this, 'include_cookiebot_js' ), - 9996 );
22 ( new Cookiebot_Declaration_Shortcode() )->register_hooks();
23 }
24
25 private function is_tcf_enabled() {
26 return ! empty( get_option( 'cookiebot-iab' ) );
27 }
28
29 private function get_data_regions() {
30 $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ?
31 get_option( 'cookiebot-multiple-config' ) :
32 false;
33 $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ?
34 get_option( 'cookiebot-second-banner-regions' ) :
35 false;
36 $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ?
37 get_option( 'cookiebot-second-banner-id' ) :
38 false;
39
40 $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ?
41 get_option( 'cookiebot-multiple-banners' ) :
42 false;
43
44 $regions = array();
45
46 if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) {
47 $regions[ $second_banner_id ] = $second_banner_region;
48 }
49
50 if ( $is_multi_config !== false && $extra_banners ) {
51 foreach ( $extra_banners as $data ) {
52 $regions[ $data['group'] ] = $data['region'];
53 }
54 }
55
56 return $regions;
57 }
58
59 /**
60 * Cookiebot_WP Add Cookiebot JS to <head>
61 *
62 * @param false $return_html
63 *
64 * @return string
65 * @throws InvalidArgumentException
66 */
67 public function include_cookiebot_js( $return_html = false ) {
68 $cbid = Cookiebot_WP::get_cbid();
69 if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) {
70 if (
71 // Is multisite - and disabled output is checked as network setting
72 ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) ||
73 // Do not show JS - output disabled
74 get_option( 'cookiebot-nooutput', false ) ||
75 // Do not show js if logged in output is disabled
76 (
77 Cookiebot_WP::get_cookie_blocking_mode() === 'auto' &&
78 Cookiebot_WP::can_current_user_edit_theme() &&
79 $return_html === '' &&
80 (
81 get_site_option( 'cookiebot-output-logged-in' ) === false ||
82 get_site_option( 'cookiebot-output-logged-in' ) === ''
83 )
84 )
85 ) {
86 return '';
87 }
88
89 $lang = cookiebot_get_language_from_setting();
90
91 if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) {
92 $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' );
93 } else {
94 $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' );
95 }
96
97 $view_path = 'frontend/scripts/cookiebot-js.php';
98 $view_args = array(
99 'cbid' => $cbid,
100 'lang' => $lang,
101 'tag_attr' => $tag_attr,
102 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(),
103 'data_regions' => self::get_data_regions(),
104 'tcf' => self::get_tcf_attribute(),
105 );
106
107 if ( $return_html ) {
108 return get_view_html( $view_path, $view_args );
109 } else {
110 include_view( $view_path, $view_args );
111 }
112 }
113 return '';
114 }
115
116 /**
117 * Cookiebot_WP Add Google Tag Manager JS to <head>
118 *
119 * @param bool $return_html
120 *
121 * @return string
122 * @throws InvalidArgumentException
123 */
124 public function include_google_tag_manager_js( $return_html = false ) {
125 $option = get_option( 'cookiebot-gtm' );
126 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
127 $cookie_categories = get_option( 'cookiebot-gtm-cookies' );
128
129 if ( $option !== false && $option !== '' ) {
130 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
131 $data_layer = 'dataLayer';
132 } else {
133 $data_layer = get_option( 'cookiebot-data-layer' );
134 }
135
136 $view_path = 'frontend/scripts/google-tag-manager-js.php';
137
138 $view_args = array(
139 'data_layer' => $data_layer,
140 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
141 );
142 if ( $return_html ) {
143 return get_view_html( $view_path, $view_args );
144 } else {
145 include_view( $view_path, $view_args );
146 }
147 }
148 return '';
149 }
150
151 /**
152 * Cookiebot_WP Add Google Consent Mode JS to <head>
153 *
154 * @param bool $return_html
155 *
156 * @return string
157 * @throws InvalidArgumentException
158 */
159 public function include_google_consent_mode_js( $return_html = false ) {
160 $option = get_option( 'cookiebot-gcm' );
161 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
162 $is_url_passthrough_enabled = get_option( 'cookiebot-gcm-url-passthrough' );
163 $cookie_categories = get_option( 'cookiebot-gcm-cookies' );
164
165 if ( $option !== false && $option !== '' ) {
166 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
167 $data_layer = 'dataLayer';
168 } else {
169 $data_layer = get_option( 'cookiebot-data-layer' );
170 }
171
172 $view_path = 'frontend/scripts/google-consent-mode-js.php';
173
174 $view_args = array(
175 'data_layer' => $data_layer,
176 'url_passthrough' => $is_url_passthrough_enabled,
177 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
178 );
179 if ( $return_html ) {
180 return get_view_html( $view_path, $view_args );
181 } else {
182 include_view( $view_path, $view_args );
183 }
184 }
185 return '';
186 }
187
188 public function include_publisher_restrictions_js( $return_html = false ) {
189 $view_path = 'frontend/scripts/publisher-restrictions-js.php';
190
191 $custom_tcf_purposes = get_option( 'cookiebot-tcf-purposes' );
192 $custom_tcf_special_purposes = get_option( 'cookiebot-tcf-special-purposes' );
193 $custom_tcf_features = get_option( 'cookiebot-tcf-features' );
194 $custom_tcf_special_features = get_option( 'cookiebot-tcf-special-features' );
195 $custom_tcf_vendors = get_option( 'cookiebot-tcf-vendors' );
196 $custom_tcf_ac_vendors = get_option( 'cookiebot-tcf-ac-vendors' );
197
198 $view_args = array(
199 'allowed_purposes' => self::get_custom_tcf_ids( $custom_tcf_purposes ),
200 'allowed_special_purposes' => self::get_custom_tcf_ids( $custom_tcf_special_purposes ),
201 'allowed_features' => self::get_custom_tcf_ids( $custom_tcf_features ),
202 'allowed_special_features' => self::get_custom_tcf_ids( $custom_tcf_special_features ),
203 'allowed_vendors' => self::get_custom_tcf_ids( $custom_tcf_vendors ),
204 'allowed_google_ac_vendors' => self::get_custom_tcf_ids( $custom_tcf_ac_vendors ),
205 'vendor_restrictions' => self::get_custom_tcf_restrictions(),
206 );
207 if ( $return_html ) {
208 return get_view_html( $view_path, $view_args );
209 } else {
210 include_view( $view_path, $view_args );
211 }
212 return '';
213 }
214
215 private function get_custom_tcf_ids( $option ) {
216 if ( empty( $option ) ) {
217 return '';
218 }
219
220 return implode( ', ', $option );
221 }
222
223 private function get_custom_tcf_restrictions() {
224 if ( empty( get_option( 'cookiebot-tcf-disallowed' ) ) ) {
225 return '';
226 }
227
228 $custom_tcf_restrictions = get_option( 'cookiebot-tcf-disallowed' );
229
230 $attribute = array();
231
232 foreach ( $custom_tcf_restrictions as $vendor => $restrictions ) {
233 $attribute [] = '{"VendorId":' . $vendor . ',"DisallowPurposes":[' . implode( ', ', $restrictions['purposes'] ) . ']}';
234 }
235
236 return implode( ',', $attribute );
237 }
238
239 private function get_consent_attribute( $blocking_mode, $categories ) {
240 $attribute = false;
241
242 if ( $blocking_mode === 'auto' ) {
243 $attribute = 'ignore';
244 }
245
246 if ( $categories && is_array( $categories ) ) {
247 $attribute = join( ', ', $categories );
248 }
249
250 return $attribute;
251 }
252
253 public static function get_tcf_attribute() {
254 $attribute = false;
255 $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) );
256 $tcf_version = get_option( 'cookiebot-tcf-version' );
257
258 if ( $iab_enabled ) {
259 if ( empty( $tcf_version ) ) {
260 $attribute = 'IAB';
261 } else {
262 $attribute = $tcf_version;
263 }
264 }
265
266 return $attribute;
267 }
268 }
269