1
+ <?php
2
+
3
+ class MC4WP_Usage_Tracking_Nag {
4
+
5
+ /**
6
+ * @var int
7
+ */
8
+ public $ shown = 0 ;
9
+
10
+ /**
11
+ * @var string
12
+ */
13
+ protected $ required_capability = 'manage_options ' ;
14
+
15
+ /**
16
+ * The name of the option to store whether this nag was shown in
17
+ */
18
+ const OPTION_NAME = 'mc4wp_usage_tracking_nag_shown ' ;
19
+
20
+ /**
21
+ * @param string $required_capability
22
+ */
23
+ public function __construct ( $ required_capability = '' ) {
24
+ $ this ->shown = get_option ( self ::OPTION_NAME , 0 );
25
+
26
+ if ( ! empty ( $ required_capability ) ) {
27
+ $ this ->required_capability = $ required_capability ;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Add hooks
33
+ */
34
+ public function add_hooks () {
35
+
36
+ // Don't add unneeded hooks
37
+ if ( $ this ->shown ) {
38
+ return ;
39
+ }
40
+
41
+ add_action ( 'admin_notices ' , array ( $ this , 'show ' ) );
42
+ add_action ( 'admin_init ' , array ( $ this , 'listen ' ) );
43
+ }
44
+
45
+ /**
46
+ *
47
+ */
48
+ public function show () {
49
+
50
+ // only show this nag if tracking is not already enabled or notice was shown before
51
+ if ( $ this ->shown ) {
52
+ return ;
53
+ }
54
+
55
+ ?>
56
+ <div class="updated notice">
57
+ <p>
58
+ <strong>Help us improve the MailChimp for WordPress plugin.</strong><br />
59
+ Allow us to anonymously track how this plugin is used to help us make it better fit your needs. No sensitive data is tracked.
60
+ </p>
61
+ <form method="post">
62
+ <p>
63
+ <button type="submit" class="button button-primary" name="allow" value="1">Allow usage tracking</button>
64
+ <button type="submit" class="button" name="allow" value="0">Dismiss</button>
65
+ </p>
66
+
67
+ <input type="hidden" name="mc4wp-usage-tracking-nag" value="1" />
68
+ </form>
69
+
70
+ </div>
71
+ <?php
72
+ }
73
+
74
+ /**
75
+ * Listen for uses of the form in the nag notice.
76
+ */
77
+ public function listen () {
78
+
79
+ if ( ! isset ( $ _POST ['mc4wp-usage-tracking-nag ' ] ) ) {
80
+ return ;
81
+ }
82
+
83
+ if ( ! current_user_can ( $ this ->required_capability ) ) {
84
+ return ;
85
+ }
86
+
87
+ $ allow = ( isset ( $ _POST ['allow ' ] ) ) ? (bool ) $ _POST ['allow ' ] : false ;
88
+
89
+
90
+ if ( $ allow ) {
91
+ // update plugin options
92
+ $ options = (array ) get_option ( 'mc4wp_lite ' , array () );
93
+ $ options ['allow_usage_tracking ' ] = 1 ;
94
+ update_option ( 'mc4wp_lite ' , $ options );
95
+
96
+ // toggle tracking
97
+ MC4WP_Usage_Tracking::instance ()->toggle ( true );
98
+ }
99
+
100
+ // make sure notice never appears again
101
+ update_option ( self ::OPTION_NAME , 1 );
102
+ $ this ->shown = 1 ;
103
+ }
104
+ }
0 commit comments