PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.3.2
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.3.2
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 3 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 3 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
211 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 add_action( 'wp_head', array( $this, 'include_cookiebot_js' ), - 9999 );
17 add_action( 'wp_head', array( $this, 'include_google_consent_mode_js' ), - 9998 );
18 add_action( 'wp_head', array( $this, 'include_google_tag_manager_js' ), - 9997 );
19 ( new Cookiebot_Declaration_Shortcode() )->register_hooks();
20 }
21
22 private function get_data_regions() {
23 $is_multi_config = ! empty( get_option( 'cookiebot-multiple-config' ) ) ?
24 get_option( 'cookiebot-multiple-config' ) :
25 false;
26 $second_banner_region = ! empty( get_option( 'cookiebot-second-banner-regions' ) ) ?
27 get_option( 'cookiebot-second-banner-regions' ) :
28 false;
29 $second_banner_id = ! empty( get_option( 'cookiebot-second-banner-id' ) ) ?
30 get_option( 'cookiebot-second-banner-id' ) :
31 false;
32
33 $extra_banners = ! empty( get_option( 'cookiebot-multiple-banners' ) ) ?
34 get_option( 'cookiebot-multiple-banners' ) :
35 false;
36
37 $regions = array();
38
39 if ( $is_multi_config !== false && $second_banner_region && $second_banner_id ) {
40 $regions[ $second_banner_id ] = $second_banner_region;
41 }
42
43 if ( $is_multi_config !== false && $extra_banners ) {
44 foreach ( $extra_banners as $data ) {
45 $regions[ $data['group'] ] = $data['region'];
46 }
47 }
48
49 return $regions;
50 }
51
52 /**
53 * Cookiebot_WP Add Cookiebot JS to <head>
54 *
55 * @param false $return_html
56 *
57 * @return string
58 * @throws InvalidArgumentException
59 */
60 public function include_cookiebot_js( $return_html = false ) {
61 $cbid = Cookiebot_WP::get_cbid();
62 if ( ! empty( $cbid ) && ! defined( 'COOKIEBOT_DISABLE_ON_PAGE' ) ) {
63 if (
64 // Is multisite - and disabled output is checked as network setting
65 ( is_multisite() && get_site_option( 'cookiebot-nooutput', false ) ) ||
66 // Do not show JS - output disabled
67 get_option( 'cookiebot-nooutput', false ) ||
68 // Do not show js if logged in output is disabled
69 (
70 Cookiebot_WP::get_cookie_blocking_mode() === 'auto' &&
71 Cookiebot_WP::can_current_user_edit_theme() &&
72 $return_html === '' &&
73 (
74 get_site_option( 'cookiebot-output-logged-in' ) === false ||
75 get_site_option( 'cookiebot-output-logged-in' ) === ''
76 )
77 )
78 ) {
79 return '';
80 }
81
82 $lang = cookiebot_get_language_from_setting();
83
84 if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-uc-attribute', 'custom' ) === 'custom' ) {
85 $tag_attr = get_option( 'cookiebot-script-tag-uc-attribute', 'async' );
86 } else {
87 $tag_attr = get_site_option( 'cookiebot-script-tag-uc-attribute' );
88 }
89
90 $view_path = 'frontend/scripts/cookiebot-js.php';
91 $view_args = array(
92 'cbid' => $cbid,
93 'lang' => $lang,
94 'tag_attr' => $tag_attr,
95 'cookie_blocking_mode' => Cookiebot_WP::get_cookie_blocking_mode(),
96 'data_regions' => self::get_data_regions(),
97 'tcf' => self::get_tcf_attribute(),
98 );
99
100 if ( $return_html ) {
101 return get_view_html( $view_path, $view_args );
102 } else {
103 include_view( $view_path, $view_args );
104 }
105 }
106 return '';
107 }
108
109 /**
110 * Cookiebot_WP Add Google Tag Manager JS to <head>
111 *
112 * @param bool $return_html
113 *
114 * @return string
115 * @throws InvalidArgumentException
116 */
117 public function include_google_tag_manager_js( $return_html = false ) {
118 $option = get_option( 'cookiebot-gtm' );
119 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
120 $cookie_categories = get_option( 'cookiebot-gtm-cookies' );
121
122 if ( $option !== false && $option !== '' ) {
123 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
124 $data_layer = 'dataLayer';
125 } else {
126 $data_layer = get_option( 'cookiebot-data-layer' );
127 }
128
129 $view_path = 'frontend/scripts/google-tag-manager-js.php';
130
131 $view_args = array(
132 'data_layer' => $data_layer,
133 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
134 );
135 if ( $return_html ) {
136 return get_view_html( $view_path, $view_args );
137 } else {
138 include_view( $view_path, $view_args );
139 }
140 }
141 return '';
142 }
143
144 /**
145 * Cookiebot_WP Add Google Consent Mode JS to <head>
146 *
147 * @param bool $return_html
148 *
149 * @return string
150 * @throws InvalidArgumentException
151 */
152 public function include_google_consent_mode_js( $return_html = false ) {
153 $option = get_option( 'cookiebot-gcm' );
154 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
155 $is_url_passthrough_enabled = get_option( 'cookiebot-gcm-url-passthrough' );
156 $cookie_categories = get_option( 'cookiebot-gcm-cookies' );
157
158 if ( $option !== false && $option !== '' ) {
159 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
160 $data_layer = 'dataLayer';
161 } else {
162 $data_layer = get_option( 'cookiebot-data-layer' );
163 }
164
165 $view_path = 'frontend/scripts/google-consent-mode-js.php';
166
167 $view_args = array(
168 'data_layer' => $data_layer,
169 'url_passthrough' => $is_url_passthrough_enabled,
170 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
171 );
172 if ( $return_html ) {
173 return get_view_html( $view_path, $view_args );
174 } else {
175 include_view( $view_path, $view_args );
176 }
177 }
178 return '';
179 }
180
181 private function get_consent_attribute( $blocking_mode, $categories ) {
182 $attribute = false;
183
184 if ( $blocking_mode === 'auto' ) {
185 $attribute = 'ignore';
186 }
187
188 if ( $categories && is_array( $categories ) ) {
189 $attribute = join( ', ', $categories );
190 }
191
192 return $attribute;
193 }
194
195 public static function get_tcf_attribute() {
196 $attribute = false;
197 $iab_enabled = ! empty( get_option( 'cookiebot-iab' ) );
198 $tcf_version = get_option( 'cookiebot-tcf-version' );
199
200 if ( $iab_enabled ) {
201 if ( empty( $tcf_version ) ) {
202 $attribute = 'IAB';
203 } else {
204 $attribute = $tcf_version;
205 }
206 }
207
208 return $attribute;
209 }
210 }
211