Skip to content

Commit 77c4409

Browse files
Add tracking nag for users with the required capability if it's not enabled yet.
1 parent 6a1965b commit 77c4409

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

includes/admin/class-admin.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ class MC4WP_Lite_Admin
2323
*/
2424
public function __construct() {
2525

26+
2627
$this->plugin_file = plugin_basename( MC4WP_LITE_PLUGIN_FILE );
2728
$this->mailchimp = new MC4WP_MailChimp();
2829
$this->load_translations();
2930
$this->setup_hooks();
3031
$this->listen();
32+
33+
// Instantiate Usage Tracking nag
34+
$options = mc4wp_get_options( 'general' );
35+
if( ! $options['allow_usage_tracking'] ) {
36+
$usage_tracking_nag = new MC4WP_Usage_Tracking_Nag( $this->get_required_capability() );
37+
$usage_tracking_nag->add_hooks();
38+
}
3139
}
3240

3341
/**
@@ -188,17 +196,27 @@ public function add_plugin_meta_links( $links, $file ) {
188196
}
189197

190198
/**
191-
* Register the setting pages and their menu items
192-
*/
193-
public function build_menu() {
199+
* @return string
200+
*/
201+
public function get_required_capability() {
194202

195203
/**
196204
* @filter mc4wp_settings_cap
197205
* @expects string A valid WP capability like 'manage_options' (default)
198206
*
199207
* Use to customize the required user capability to access the MC4WP settings pages
200208
*/
201-
$required_cap = apply_filters( 'mc4wp_settings_cap', 'manage_options' );
209+
$required_cap = (string) apply_filters( 'mc4wp_settings_cap', 'manage_options' );
210+
211+
return $required_cap;
212+
}
213+
214+
/**
215+
* Register the setting pages and their menu items
216+
*/
217+
public function build_menu() {
218+
219+
$required_cap = $this->get_required_capability();
202220

203221
$menu_items = array(
204222
array(
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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> &nbsp;
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+
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'MC4WP_Tools' => $baseDir . '/includes/class-tools.php',
3333
'MC4WP_Unsubscribe_Request' => $baseDir . '/includes/class-unsubscribe-request.php',
3434
'MC4WP_Usage_Tracking' => $baseDir . '/includes/admin/class-usage-tracking.php',
35+
'MC4WP_Usage_Tracking_Nag' => $baseDir . '/includes/admin/class-usage-tracking-nag.php',
3536
'MC4WP_User_Integration' => $baseDir . '/includes/integrations/class-user-integration.php',
3637
'MC4WP_WooCommerce_Integration' => $baseDir . '/includes/integrations/class-woocommerce.php',
3738
'MC4WP_bbPress_Integration' => $baseDir . '/includes/integrations/class-bbpress.php',

0 commit comments

Comments
 (0)