-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking-consent.php
More file actions
427 lines (363 loc) · 13.8 KB
/
tracking-consent.php
File metadata and controls
427 lines (363 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/*
Plugin Name: Tracking Consent
Description: GDPR-compliant tool set to disable or re-enable tracking.
Version: 1.0.0
Author: Matthias Kittsteiner
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: tracking-consent
Domain Path: /languages
Tracking Consent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
Tracking Consent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tracking Consent. If not, see https://www.gnu.org/licenses/gpl-3.0.html.
*/
// exit if ABSPATH is not defined
defined( 'ABSPATH' ) || exit;
/**
* Load text domain.
*/
function tracking_consent_load_textdomain() {
load_plugin_textdomain( 'tracking-consent', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'tracking_consent_load_textdomain' );
/**
* Add the JavaScript and CSS to the head.
*/
function tracking_consent_add_assets() {
// don’t do anything if site is not tracking
if ( ! tracking_consent_site_is_tracking() || tracking_consent_disable() ) return;
// don’t add javascript if the gdpr cookie is set to true
// phpcs:disable WordPress.VIP.ValidatedSanitizedInput.MissingUnslash, WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_COOKIE['mws-gdpr'] ) || ! $_COOKIE['mws-gdpr'] ) :
// phpcs:enable
// add javascript
// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
$javascript = file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/js/gdpr-notice.min.js' );
// phpcs:enable
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
?>
<script><?php echo trim( str_replace( '//# sourceMappingURL=gdpr-notice.min.js.map', '', $javascript ) ); ?></script>
<?php
// phpcs:enable
endif;
// check for cookie
if ( isset( $_COOKIE['mws-gdpr'] ) ) return;
// add stylesheet
// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
$stylesheet = file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/style/style.min.css' );
// phpcs:enable
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
?>
<style><?php echo trim( str_replace( '/*# sourceMappingURL=style.min.css.map */', '', $stylesheet ) ); ?></style>
<?php
// phpcs:enable
}
add_action( 'wp_head', 'tracking_consent_add_assets', 100 );
/**
* Add the “cookie” information notice.
*/
function tracking_consent_add_info_notice() {
// don’t do anything if site is not tracking
if ( ! tracking_consent_site_is_tracking() || tracking_consent_disable() ) return;
// check for cookie
if ( isset( $_COOKIE['mws-gdpr'] ) ) return;
// get privacy link
$privacy_link = get_option( 'tracking_consent_privacy_link' );
if ( $privacy_link === false && defined( 'RH_CONFIG' ) ) {
add_option( 'tracking_consent_privacy_link', '/datenschutz/' );
$privacy_link = get_option( 'tracking_consent_privacy_link' );
}
else if ( $privacy_link === false && ! defined( 'RH_CONFIG' ) ) {
$privacy_link = get_privacy_policy_url();
}
// get classes
$classes = 'gdpr-notice';
if ( wp_is_mobile() ) {
$classes .= ' gdpr-mobile';
}
else {
$classes .= ' gdpr-desktop';
}
if ( ! get_option( 'tracking_consent_design_bottom' ) ) {
$classes .= ' fullscreen';
}
if ( defined( 'RH_CONFIG' ) ) {
$classes .= ' rh';
}
?>
<div id="gdpr-notice" class="<?php echo esc_attr( $classes ); ?>">
<div class="container wrapper">
<div class="notice-content">
<p><?php esc_html_e( 'In order to be able to offer you the best possible user experience on this website in the future, we would like to activate tracking services such as Google Analytics, which uses cookies to anonymously store and analyse your user behaviour. For this, we need your consent, which you can revoke at any time.', 'tracking-consent' ); ?><br>
<?php
/* translators: %s: link to privacy policy */
printf( esc_html__( 'For more information about the services used, please, see our %s.', 'tracking-consent' ), '<a href="' . esc_attr( $privacy_link ) . '" class="datenschutz-open-close">' . esc_html__( 'privacy policy', 'tracking-consent' ) . '</a>' );
?></p>
</div>
<?php // phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found ?>
<?php // phpcs:enable ?>
<div class="notice-buttons">
<a id="gdpr-yes" class="btn btn-primary"><?php esc_html_e( 'Allow', 'tracking-consent' ); ?></a>
<a id="gdpr-no" class="gdpr-no-button"><?php esc_html_e( 'Prohibit', 'tracking-consent' ); ?></a>
</div>
</div>
</div>
<?php
}
add_action( 'wp_footer', 'tracking_consent_add_info_notice' );
if ( ! defined( 'RH_CONFIG' ) ) :
/**
* Customizer settings.
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object
*/
function tracking_consent_customizer_register( $wp_customize ) {
$wp_customize->add_section( 'tracking_consent', [
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => __( 'Tracking', 'tracking-consent' ),
] );
$wp_customize->add_setting( 'tracking_js_textarea', [
'default' => '',
'type' => 'option',
'capability' => 'edit_theme_options',
'transport' => '',
] );
$wp_customize->add_control( 'tracking_js_textarea', [
'type' => 'code_editor',
'setting' => 'js',
'input_attrs' => [
'class' => 'code',
// Ensures contents displayed as LTR instead of RTL.
],
'priority' => 10,
'section' => 'tracking_consent',
'label' => __( 'Tracking JavaScript code', 'tracking-consent' ),
'description' => __( 'Don’t forget to add a beginning <script> and an ending </script>.', 'tracking-consent' ),
] );
$wp_customize->add_setting( 'tracking_consent_design_bottom', [
'default' => 0,
'type' => 'option',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'rh_sanitize_checkbox',
'transport' => '',
] );
$wp_customize->add_control( 'tracking_consent_design_bottom', [
'priority' => 10,
'section' => 'tracking_consent',
'label' => __( 'Discreet design', 'tracking-consent' ),
'type' => 'checkbox',
] );
}
add_action( 'customize_register', 'tracking_consent_customizer_register', 20 );
/**
* Add the tracking code to the footer on Zephyr projects.
*/
function tracking_consent_tracking_code() {
$disabled = tracking_consent_check_gdpr_cookie();
$options = '';
if ( get_option( 'tracking_js_textarea' ) ) {
// remove every html comment
$options .= preg_replace( '/<!--((.|\n)*?)-->/', '', get_option( 'tracking_js_textarea' ) );
}
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
echo '<div id="tracking-consent-tracking">' . ( $disabled ? '<!--' : '' ) . $options . ( $disabled ? '-->' : '' ) . '</div>';
// phpcs:enable
}
add_action( 'wp_footer', 'tracking_consent_tracking_code' );
endif;
// only on zephyr projects
// see: https://docs.aurora.ci/handbook/environment-variable.html
if ( defined( 'RH_CONFIG' ) && RH_CONFIG['version'] === 'zephyr' ) :
/**
* Customizer settings.
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object
*/
function tracking_consent_customizer_register( $wp_customize ) {
$wp_customize->add_setting( 'tracking_js_textarea', [
'default' => '',
'type' => 'option',
'capability' => 'edit_theme_options',
'transport' => '',
] );
$wp_customize->add_control( 'tracking_js_textarea', [
'type' => 'code_editor',
'setting' => 'js',
'input_attrs' => [
'class' => 'code',
// Ensures contents displayed as LTR instead of RTL.
],
'priority' => 10,
'section' => 'seo_analytics',
'label' => __( 'Tracking JavaScript code', 'tracking-consent' ),
'description' => __( 'Don’t forget to add a beginning <script> and an ending </script>.', 'tracking-consent' ),
] );
}
add_action( 'customize_register', 'tracking_consent_customizer_register', 20 );
/**
* Add the tracking code to the footer on Zephyr projects.
*/
function tracking_consent_zephyr_tracking_code() {
$disabled = tracking_consent_check_gdpr_cookie();
$options = '';
if ( get_option( 'tracking_js_textarea' ) ) {
// remove every html comment
$options .= preg_replace( '/<!--((.|\n)*?)-->/', '', get_option( 'tracking_js_textarea' ) );
}
if ( get_option( 'rh_analytics_id' ) && ! get_theme_mod( 'rh_analytics_disable' ) ) {
$options .= "
<script>
// disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-" . get_option( 'rh_analytics_id' ) . "';
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '" . get_option( 'rh_analytics_id' ) . "', 'auto');
ga('create', 'UA-63619645-1', 'auto', 'clientTracker');
ga('send', 'pageview');
ga('set', 'anonymizeIp', true);
ga('clientTracker.send', 'pageview');
" . do_action( 'rh_analytics_after_include' ) . "
</script>
";
}
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
echo '<div id="tracking-consent-tracking">' . ( $disabled ? '<!--' : '' ) . $options . ( $disabled ? '-->' : '' ) . '</div>';
// phpcs:enable
}
add_action( 'wp_footer', 'tracking_consent_zephyr_tracking_code' );
endif;
// only on aster projects
// see: https://docs.aurora.ci/handbook/environment-variable.html
if ( defined( 'RH_CONFIG' ) && RH_CONFIG['version'] === 'aster' ) :
/**
* Add the tracking code to the footer on Aster projects.
*/
function tracking_consent_aster_tracking_code() {
global $tocki_redux_themeoptions;
$disabled = tracking_consent_check_gdpr_cookie();
if ( tracking_consent_site_is_tracking() ) {
// remove every html comment
$option = preg_replace( '/<!--((.|\n)*?)-->/', '', $tocki_redux_themeoptions['tocki_redux_footer'] );
// set theme options "empty" to avoid our default tracking
$tocki_redux_themeoptions['tocki_redux_footer'] = '<script></script>';
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
echo '<div id="tracking-consent-tracking">' . ( $disabled ? '<!--' : '' ) . $option . ( $disabled ? '-->' : '' ) . '</div>';
// phpcs:enable
}
else {
// set theme options "empty" to avoid our default tracking
$tocki_redux_themeoptions['tocki_redux_footer'] = '<script></script>';
echo '<div id="tracking-consent-tracking"></div>';
}
}
add_action( 'wp_footer', 'tracking_consent_aster_tracking_code' );
endif;
/**
* Check if a GDPR cookie is set.
*
* @return bool
*/
function tracking_consent_check_gdpr_cookie() {
$disabled = false;
// disable if tracking nor allowed neither already asked for
// phpcs:disable WordPress.VIP.ValidatedSanitizedInput.MissingUnslash, WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset( $_COOKIE['mws-gdpr'] ) || ! $_COOKIE['mws-gdpr'] ) {
$disabled = true;
}
// phpcs:enable
return $disabled;
}
/**
* Check if a website has enabled tracking scripts.
*
* @return bool
*/
function tracking_consent_site_is_tracking() {
global $tocki_redux_themeoptions;
$is_tracking = true;
if ( defined( 'RH_CONFIG' ) && RH_CONFIG['version'] === 'zephyr' ) {
// get all options that enable or disable tracking
$analytics_disabled = get_theme_mod( 'rh_analytics_disable' );
$analytics_id = get_option( 'rh_analytics_id' );
$tracking_js = get_option( 'tracking_js_textarea' );
if ( $tracking_js || ( $analytics_id && ! $analytics_disabled ) ) {
$is_tracking = true;
}
else {
$is_tracking = false;
}
}
else if ( defined( 'RH_CONFIG' ) && RH_CONFIG['version'] === 'aster' ) {
$is_tracking = (bool) ! empty( $tocki_redux_themeoptions['tocki_redux_footer'] );
}
return $is_tracking;
}
/**
* Detect tracking scripts in the tracking JavaScript.
*/
function tracking_consent_detect_tracking_scripts() {
$option = [];
$script_content = get_option( 'tracking_js_textarea' );
// Google Analytics
if (
strpos( $script_content, 'analytics' ) !== false
|| get_option( 'rh_analytics_id' )
) {
$option[] = 'google-analytics';
}
// Google Tag Manager
if (
strpos( $script_content, 'gtag' ) !== false
|| strpos( $script_content, 'googletagmanager' )
) {
$option[] = 'google-tag-manager';
}
// Google Remarketing
if (
strpos( $script_content, 'gtag(\'config\', \'AW-' ) !== false
|| strpos( $script_content, 'google_conversion_id' ) !== false
|| strpos( $script_content, 'google_remarketing' ) !== false
|| strpos( $script_content, 'goog_report_conversion' ) !== false
|| strpos( $script_content, 'conversion_async' ) !== false
) {
$option[] = 'google-remarketing';
}
// Google Click Identifier
if ( strpos( $script_content, 'gclid' ) !== false ) {
$option[] = 'google-click-identifier';
}
// Facebook Pixel
if ( strpos( $script_content, 'connect.facebook.net/en_US/fbevents.js' ) !== false ) {
$option[] = 'facebook-pixel';
}
// Bing Tracking
if ( strpos( $script_content, 'bat.bing.com' ) !== false ) {
$option[] = 'bing-tracking';
}
update_option( 'tracking_consent_tracking_scripts', $option );
}
add_action( 'customize_save_after', 'tracking_consent_detect_tracking_scripts' );
/**
* Check if Tracking Consent should be disabled.
*
* @return bool
*/
function tracking_consent_disable() {
// phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification
if ( isset( $_GET['tracking_consent_disable'] ) && $_GET['tracking_consent_disable'] === 'true' ) {
return true;
}
// phpcs:enable
return false;
}