PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.2.12
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.2.12
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 3 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 3 years ago Cookiebot_Review.php 2 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 3 years ago
Cookiebot_Javascript_Helper.php
194 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 );
98
99 if ( $return_html ) {
100 return get_view_html( $view_path, $view_args );
101 } else {
102 include_view( $view_path, $view_args );
103 }
104 }
105 return '';
106 }
107
108 /**
109 * Cookiebot_WP Add Google Tag Manager JS to <head>
110 *
111 * @param bool $return_html
112 *
113 * @return string
114 * @throws InvalidArgumentException
115 */
116 public function include_google_tag_manager_js( $return_html = false ) {
117 $option = get_option( 'cookiebot-gtm' );
118 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
119 $cookie_categories = get_option( 'cookiebot-gtm-cookies' );
120
121 if ( $option !== false && $option !== '' ) {
122 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
123 $data_layer = 'dataLayer';
124 } else {
125 $data_layer = get_option( 'cookiebot-data-layer' );
126 }
127
128 $view_path = 'frontend/scripts/google-tag-manager-js.php';
129
130 $view_args = array(
131 'data_layer' => $data_layer,
132 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
133 );
134 if ( $return_html ) {
135 return get_view_html( $view_path, $view_args );
136 } else {
137 include_view( $view_path, $view_args );
138 }
139 }
140 return '';
141 }
142
143 /**
144 * Cookiebot_WP Add Google Consent Mode JS to <head>
145 *
146 * @param bool $return_html
147 *
148 * @return string
149 * @throws InvalidArgumentException
150 */
151 public function include_google_consent_mode_js( $return_html = false ) {
152 $option = get_option( 'cookiebot-gcm' );
153 $blocking_mode = get_option( 'cookiebot-cookie-blocking-mode' );
154 $is_url_passthrough_enabled = '1' === (string) get_option( 'cookiebot-gcm-url-passthrough', 1 );
155 $cookie_categories = get_option( 'cookiebot-gcm-cookies' );
156
157 if ( $option !== false && $option !== '' ) {
158 if ( empty( get_option( 'cookiebot-data-layer' ) ) ) {
159 $data_layer = 'dataLayer';
160 } else {
161 $data_layer = get_option( 'cookiebot-data-layer' );
162 }
163
164 $view_path = 'frontend/scripts/google-consent-mode-js.php';
165
166 $view_args = array(
167 'data_layer' => $data_layer,
168 'url_passthrough' => $is_url_passthrough_enabled,
169 'consent_attribute' => self::get_consent_attribute( $blocking_mode, $cookie_categories ),
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 private function get_consent_attribute( $blocking_mode, $categories ) {
181 $attribute = false;
182
183 if ( $blocking_mode === 'auto' ) {
184 $attribute = 'ignore';
185 }
186
187 if ( $categories && is_array( $categories ) ) {
188 $attribute = join( ', ', $categories );
189 }
190
191 return $attribute;
192 }
193 }
194