Add inbox note prompting to opt in to tracking (https://github.com/woocommerce/woocommerce-admin/pull/3112)

* Leave wc-admin install timestamp intact once set.

* Add inbox note for opting in to usage tracking.

* Remove markup from translated string.

* Allow tracking opt in note to be dismissed.

* Remove whitespace.
This commit is contained in:
Jeff Stieler 2019-11-07 09:29:36 -07:00 committed by GitHub
parent 810cef19f3
commit 9a9d812e60
4 changed files with 104 additions and 1 deletions

View File

@ -15,6 +15,7 @@ use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Facebook_Extension;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Giving_Feedback_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Mobile_App;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_New_Sales_Record;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Tracking_Opt_In;
/**
* WC_Admin_Events Class.
@ -64,5 +65,6 @@ class Events {
WC_Admin_Notes_Mobile_App::possibly_add_mobile_app_note();
WC_Admin_Notes_Facebook_Extension::possibly_add_facebook_note();
WC_Admin_Notes_Add_First_Product::possibly_add_first_product_note();
WC_Admin_Notes_Tracking_Opt_In::possibly_add_tracking_opt_in_note();
}
}

View File

@ -15,6 +15,7 @@ use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Historical_Data;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Order_Milestones;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Woo_Subscriptions_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Tracking_Opt_In;
/**
* Feature plugin main class.
@ -156,6 +157,7 @@ class FeaturePlugin {
new WC_Admin_Notes_Order_Milestones();
new WC_Admin_Notes_Welcome_Message();
new WC_Admin_Notes_Facebook_Extension();
new WC_Admin_Notes_Tracking_Opt_In();
}
/**

View File

@ -83,7 +83,9 @@ class Install {
delete_transient( 'wc_admin_installing' );
update_option( 'wc_admin_install_timestamp', time() );
// Use add_option() here to avoid overwriting this value with each
// plugin version update. We base plugin age off of this value.
add_option( 'wc_admin_install_timestamp', time() );
do_action( 'wc_admin_installed' );
}

View File

@ -0,0 +1,97 @@
<?php
/**
* WooCommerce Admin Usage Tracking Opt In Note Provider.
*
* Adds a note to the merchant's inbox showing the benefits of the Facebook extension.
*
* @package WooCommerce Admin
*/
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
/**
* WC_Admin_Notes_Tracking_Opt_In
*/
class WC_Admin_Notes_Tracking_Opt_In {
/**
* Note traits.
*/
use NoteTraits;
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-admin-usage-tracking-opt-in';
/**
* Attach hooks.
*/
public function __construct() {
add_action( 'woocommerce_admin_note_action_tracking-opt-in', array( $this, 'opt_in_to_tracking' ) );
}
/**
* Possibly add Usage Tracking Opt In extension note.
*/
public static function possibly_add_tracking_opt_in_note() {
// Only show this note to stores that are opted out.
if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
return;
}
// We want to show the note after one week.
if ( ! self::wc_admin_active_for( WEEK_IN_SECONDS ) ) {
return;
}
$data_store = \WC_Data_Store::load( 'admin-note' );
// We already have this note? Then exit, we're done.
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_ids ) ) {
return;
}
/* translators: 1: open link to WooCommerce.com settings, 2: open link to WooCommerce.com tracking documentation, 3: close link tag. */
$content_format = __(
'Gathering usage data allows us to improve WooCommerce. Your store will be considered as we evaluate new features, judge the quality of an update, or determine if an improvement makes sense. You can always visit the %1$sSettings%3$s and choose to stop sharing data. %2$sRead more%3$s about what data we collect.',
'woocommerce-admin'
);
$note_content = sprintf(
$content_format,
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=woocommerce_com' ) ) . '" target="_blank">',
'<a href="https://woocommerce.com/usage-tracking" target="_blank">',
'</a>'
);
$note = new WC_Admin_Note();
$note->set_title( __( 'Help WooCommerce improve with usage tracking', 'woocommerce-admin' ) );
$note->set_content( $note_content );
$note->set_content_data( (object) array() );
$note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_icon( 'info' );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'tracking-dismiss', __( 'Dismiss', 'woocommerce-admin' ), false, WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED, false );
$note->add_action( 'tracking-opt-in', __( 'Activate usage tracking', 'woocommerce-admin' ), false, WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED, true );
$note->save();
}
/**
* Opt in to usage tracking when note is actioned.
*
* @param WC_Admin_Note $note Note being acted upon.
*/
public function opt_in_to_tracking( $note ) {
if ( self::NOTE_NAME === $note->get_name() ) {
// Opt in to tracking and schedule the first data update.
// Same mechanism as in WC_Admin_Setup_Wizard::wc_setup_store_setup_save().
update_option( 'woocommerce_allow_tracking', 'yes' );
wp_schedule_single_event( time() + 10, 'woocommerce_tracker_send_event', array( true ) );
}
}
}