PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / trunk
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) vtrunk
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / classes / class-analytify-user-optout.php

class-analytify-user-optout.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) trunk, at classes/class-analytify-user-optout.php

153 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Analytify User Optout Class
4 *
5 * This class handles user opt-out functionality for the Analytify plugin,
6 * providing shortcodes and methods for users to opt out of analytics tracking.
7 *
8 * @package WP_Analytify
9 * @since 1.0.0
10 */
11
12 /**
13 * User Optout.
14 */
15 class Analytify_User_Optout {
16
17 /**
18 * Constructor.
19 */
20 public function __construct() {
21
22 add_filter( 'analytiy_user_optout_message', array( $this, 'user_optout_message' ) );
23 add_filter( 'analytiy_user_optin_message', array( $this, 'user_optin_message' ) );
24 add_shortcode( 'analytify_user_optout', array( $this, 'user_optout_shortcode' ) );
25 add_shortcode( 'analytify_user_optin', array( $this, 'user_optin_shortcode' ) );
26 }
27
28 /**
29 * Add [analytify_user_optout] Shortcode.
30 *
31 * @param mixed $atts Shortcode attributes.
32 * @param mixed $content Shortcode content.
33 * @return string
34 * @since 2.1.16
35 */
36 public function user_optout_shortcode( $atts, $content = '' ) {
37
38 $ua_code = WP_ANALYTIFY_FUNCTIONS::get_UA_code();
39 $is_authenticate_in = get_option( 'pa_google_token' );
40
41 if ( ! $ua_code || ! $is_authenticate_in ) {
42 if ( current_user_can( 'manage_options' ) ) {
43 return ' "Analytify Profile is not selected" ';
44 } else {
45 return '<!-- Analytify Profile is not selected. -->';
46 }
47 }
48
49 ob_start();
50 ?>
51 <script>
52 var analytify_optout_string = 'ga-disable-' + '<?php echo esc_js( $ua_code ); ?>';
53 if ( document.cookie.indexOf( analytify_optout_string + '=true' ) > -1 ) {
54 window[ analytify_optout_string ] = true;
55 }
56
57 function analytify_analytics_optout() {
58 var exp_date = new Date;
59 exp_date.setFullYear(exp_date.getFullYear() + 10);
60
61 document.cookie = analytify_optout_string + '=true; expires=' + exp_date.toGMTString() + '; path=/';
62 window[ analytify_optout_string ] = true;
63 <?php echo esc_js( apply_filters( 'analytiy_user_optout_message', '' ) ); ?>
64 }
65 </script>
66 <?php
67 $output = ob_get_contents();
68 ob_end_clean();
69
70 if ( '' === $content ) {
71 $content = __( 'Click here to opt-out.', 'wp-analytify' );
72 }
73
74 $output .= '<a class="analytify-opt-out" href="javascript:analytify_analytics_optout();">' . $content . '</a>';
75
76 return $output;
77 }
78
79 /**
80 * Alert message for user opt-out.
81 *
82 * @return string
83 * @since 2.1.16
84 */
85 public function user_optout_message() {
86
87 return "alert('" . __( 'Thanks. Google Analytics data collection is disabled for you.', 'wp-analytify' ) . "')";
88 }
89
90 /**
91 * Add [analytify_user_optin] Shortcode.
92 *
93 * @param mixed $atts Shortcode attributes.
94 * @param mixed $content Shortcode content.
95 * @return string
96 * @since 2.1.22
97 */
98 public function user_optin_shortcode( $atts, $content = '' ) {
99 $ua_code = WP_ANALYTIFY_FUNCTIONS::get_UA_code();
100 $is_authenticate_in = get_option( 'pa_google_token' );
101
102 if ( ! $ua_code || ! $is_authenticate_in ) {
103 if ( current_user_can( 'manage_options' ) ) {
104 return ' "Analytify Profile is not selected" ';
105 } else {
106 return '<!-- Analytify Profile is not selected. -->';
107 }
108 }
109
110 ob_start();
111 ?>
112 <script>
113 var analytify_optout_string = 'ga-disable-' + '<?php echo esc_js( $ua_code ); ?>';
114
115
116 function analytify_analytics_optin() {
117
118 var exp_date = new Date;
119 exp_date.setFullYear(exp_date.getFullYear() - 30);
120
121 document.cookie = analytify_optout_string + '=true; expires=' + exp_date.toGMTString() + '; path=/';
122 window[ analytify_optout_string ] = true;
123
124
125 <?php echo esc_js( apply_filters( 'analytiy_user_optin_message', '' ) ); ?>
126 }
127 </script>
128 <?php
129 $output = ob_get_contents();
130 ob_end_clean();
131
132 if ( '' === $content ) {
133 $content = __( 'Click here to opt-in.', 'wp-analytify' );
134 }
135
136 $output .= '<a class="analytify-opt-in" href="javascript:analytify_analytics_optin();">' . $content . '</a>';
137
138 return $output;
139 }
140
141 /**
142 * Alert message for user opt-in.
143 *
144 * @return string
145 * @since 2.1.22
146 */
147 public function user_optin_message() {
148 return "alert('" . __( 'Thanks. Google Analytics data collection is enabled for you.', 'wp-analytify' ) . "')";
149 }
150 }
151
152 new Analytify_User_Optout();
153