Skip to content

Commit

Permalink
Add tracking nag for users with the required capability if it's not e…
Browse files Browse the repository at this point in the history
…nabled yet.
  • Loading branch information
dannyvankooten committed Oct 9, 2015
1 parent 6a1965b commit 77c4409
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 4 deletions.
26 changes: 22 additions & 4 deletions includes/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ class MC4WP_Lite_Admin
*/
public function __construct() {


$this->plugin_file = plugin_basename( MC4WP_LITE_PLUGIN_FILE );
$this->mailchimp = new MC4WP_MailChimp();
$this->load_translations();
$this->setup_hooks();
$this->listen();

// Instantiate Usage Tracking nag
$options = mc4wp_get_options( 'general' );
if( ! $options['allow_usage_tracking'] ) {
$usage_tracking_nag = new MC4WP_Usage_Tracking_Nag( $this->get_required_capability() );
$usage_tracking_nag->add_hooks();
}
}

/**
Expand Down Expand Up @@ -188,17 +196,27 @@ public function add_plugin_meta_links( $links, $file ) {
}

/**
* Register the setting pages and their menu items
*/
public function build_menu() {
* @return string
*/
public function get_required_capability() {

/**
* @filter mc4wp_settings_cap
* @expects string A valid WP capability like 'manage_options' (default)
*
* Use to customize the required user capability to access the MC4WP settings pages
*/
$required_cap = apply_filters( 'mc4wp_settings_cap', 'manage_options' );
$required_cap = (string) apply_filters( 'mc4wp_settings_cap', 'manage_options' );

return $required_cap;
}

/**
* Register the setting pages and their menu items
*/
public function build_menu() {

$required_cap = $this->get_required_capability();

$menu_items = array(
array(
Expand Down
104 changes: 104 additions & 0 deletions includes/admin/class-usage-tracking-nag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

class MC4WP_Usage_Tracking_Nag {

/**
* @var int
*/
public $shown = 0;

/**
* @var string
*/
protected $required_capability = 'manage_options';

/**
* The name of the option to store whether this nag was shown in
*/
const OPTION_NAME = 'mc4wp_usage_tracking_nag_shown';

/**
* @param string $required_capability
*/
public function __construct( $required_capability = '' ) {
$this->shown = get_option( self::OPTION_NAME, 0 );

if( ! empty( $required_capability ) ) {
$this->required_capability = $required_capability;
}
}

/**
* Add hooks
*/
public function add_hooks() {

// Don't add unneeded hooks
if( $this->shown ) {
return;
}

add_action( 'admin_notices', array( $this, 'show' ) );
add_action( 'admin_init', array( $this, 'listen' ) );
}

/**
*
*/
public function show() {

// only show this nag if tracking is not already enabled or notice was shown before
if( $this->shown ) {
return;
}

?>
<div class="updated notice">
<p>
<strong>Help us improve the MailChimp for WordPress plugin.</strong><br />
Allow us to anonymously track how this plugin is used to help us make it better fit your needs. No sensitive data is tracked.
</p>
<form method="post">
<p>
<button type="submit" class="button button-primary" name="allow" value="1">Allow usage tracking</button> &nbsp;
<button type="submit" class="button" name="allow" value="0">Dismiss</button>
</p>

<input type="hidden" name="mc4wp-usage-tracking-nag" value="1" />
</form>

</div>
<?php
}

/**
* Listen for uses of the form in the nag notice.
*/
public function listen() {

if ( ! isset( $_POST['mc4wp-usage-tracking-nag'] ) ) {
return;
}

if ( ! current_user_can( $this->required_capability ) ) {
return;
}

$allow = ( isset( $_POST['allow'] ) ) ? (bool) $_POST['allow'] : false;


if ( $allow ) {
// update plugin options
$options = (array) get_option( 'mc4wp_lite', array() );
$options['allow_usage_tracking'] = 1;
update_option( 'mc4wp_lite', $options );

// toggle tracking
MC4WP_Usage_Tracking::instance()->toggle( true );
}

// make sure notice never appears again
update_option( self::OPTION_NAME, 1 );
$this->shown = 1;
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'MC4WP_Tools' => $baseDir . '/includes/class-tools.php',
'MC4WP_Unsubscribe_Request' => $baseDir . '/includes/class-unsubscribe-request.php',
'MC4WP_Usage_Tracking' => $baseDir . '/includes/admin/class-usage-tracking.php',
'MC4WP_Usage_Tracking_Nag' => $baseDir . '/includes/admin/class-usage-tracking-nag.php',
'MC4WP_User_Integration' => $baseDir . '/includes/integrations/class-user-integration.php',
'MC4WP_WooCommerce_Integration' => $baseDir . '/includes/integrations/class-woocommerce.php',
'MC4WP_bbPress_Integration' => $baseDir . '/includes/integrations/class-bbpress.php',
Expand Down

0 comments on commit 77c4409

Please sign in to comment.