From 9a9d812e60efaf99b9a185d6d7d10a80acf2679f Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Thu, 7 Nov 2019 09:29:36 -0700 Subject: [PATCH] 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. --- plugins/woocommerce-admin/src/Events.php | 2 + .../woocommerce-admin/src/FeaturePlugin.php | 2 + plugins/woocommerce-admin/src/Install.php | 4 +- .../Notes/WC_Admin_Notes_Tracking_Opt_In.php | 97 +++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Tracking_Opt_In.php diff --git a/plugins/woocommerce-admin/src/Events.php b/plugins/woocommerce-admin/src/Events.php index 3654221acb0..a39107dca68 100644 --- a/plugins/woocommerce-admin/src/Events.php +++ b/plugins/woocommerce-admin/src/Events.php @@ -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(); } } diff --git a/plugins/woocommerce-admin/src/FeaturePlugin.php b/plugins/woocommerce-admin/src/FeaturePlugin.php index 7953c0b35d0..ec7ffddbb2f 100644 --- a/plugins/woocommerce-admin/src/FeaturePlugin.php +++ b/plugins/woocommerce-admin/src/FeaturePlugin.php @@ -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(); } /** diff --git a/plugins/woocommerce-admin/src/Install.php b/plugins/woocommerce-admin/src/Install.php index fbaed7b1d16..053e4321457 100644 --- a/plugins/woocommerce-admin/src/Install.php +++ b/plugins/woocommerce-admin/src/Install.php @@ -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' ); } diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Tracking_Opt_In.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Tracking_Opt_In.php new file mode 100644 index 00000000000..8a2216d8bef --- /dev/null +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Tracking_Opt_In.php @@ -0,0 +1,97 @@ +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, + '', + '', + '' + ); + + $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 ) ); + } + } +}