2019-11-07 16:29:36 +00:00
< ? php
/**
* WooCommerce Admin Usage Tracking Opt In Note Provider .
*
2020-05-22 13:48:40 +00:00
* Adds a Usage Tracking Opt In extension note .
2019-11-07 16:29:36 +00:00
*/
2022-03-08 13:55:27 +00:00
namespace Automattic\WooCommerce\Internal\Admin\Notes ;
2019-11-07 16:29:36 +00:00
defined ( 'ABSPATH' ) || exit ;
2022-03-08 13:55:27 +00:00
use \Automattic\WooCommerce\Admin\Notes\Note ;
use \Automattic\WooCommerce\Admin\Notes\NoteTraits ;
2019-11-07 16:29:36 +00:00
/**
2020-09-28 04:35:10 +00:00
* Tracking_Opt_In
2019-11-07 16:29:36 +00:00
*/
2020-10-28 17:12:14 +00:00
class TrackingOptIn {
2019-11-07 16:29:36 +00:00
/**
* 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 () {
2020-01-09 02:10:29 +00:00
add_action ( 'woocommerce_note_action_tracking-opt-in' , array ( $this , 'opt_in_to_tracking' ) );
2019-11-07 16:29:36 +00:00
}
/**
2020-05-22 13:48:40 +00:00
* Get the note .
2020-09-28 04:35:10 +00:00
*
* @ return Note | null
2019-11-07 16:29:36 +00:00
*/
2020-05-22 13:48:40 +00:00
public static function get_note () {
2019-11-07 16:29:36 +00:00
// 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.
2021-06-24 14:32:02 +00:00
if ( ! self :: is_wc_admin_active_in_date_range ( 'week-1-4' ) ) {
2019-11-07 16:29:36 +00:00
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§ion=woocommerce_com' ) ) . '" target="_blank">' ,
2021-08-03 20:56:43 +00:00
'<a href="https://woocommerce.com/usage-tracking?utm_medium=product" target="_blank">' ,
2019-11-07 16:29:36 +00:00
'</a>'
);
2020-09-28 04:35:10 +00:00
$note = new Note ();
2019-11-07 16:29:36 +00:00
$note -> set_title ( __ ( 'Help WooCommerce improve with usage tracking' , 'woocommerce-admin' ) );
$note -> set_content ( $note_content );
$note -> set_content_data ( ( object ) array () );
2020-09-28 04:35:10 +00:00
$note -> set_type ( Note :: E_WC_ADMIN_NOTE_INFORMATIONAL );
2019-11-07 16:29:36 +00:00
$note -> set_name ( self :: NOTE_NAME );
$note -> set_source ( 'woocommerce-admin' );
2020-09-28 04:35:10 +00:00
$note -> add_action ( 'tracking-opt-in' , __ ( 'Activate usage tracking' , 'woocommerce-admin' ), false , Note :: E_WC_ADMIN_NOTE_ACTIONED , true );
2020-05-22 13:48:40 +00:00
return $note ;
2019-11-07 16:29:36 +00:00
}
/**
* Opt in to usage tracking when note is actioned .
*
2020-09-28 04:35:10 +00:00
* @ param Note $note Note being acted upon .
2019-11-07 16:29:36 +00:00
*/
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 ) );
}
}
}