Fixed event tracking for merchant email notes (https://github.com/woocommerce/woocommerce-admin/pull/6616)

* Modified MerchantEmailNotifications class

This commit modifies the class "MerchantEmailNotifications" in order to set the current user before the events tracking

# Conflicts:
#	src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php
#	src/Notes/MerchantEmailNotifications/NotificationEmail.php
#	tests/notes/class-wc-tests-email-notes.php

* Added changelog

# Conflicts:
#	readme.txt

* Added testing instructions

* Fixed changelog

Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
This commit is contained in:
Fernando 2021-03-29 16:00:23 -03:00 committed by GitHub
parent c97cf5deb9
commit 74d1afaded
5 changed files with 37 additions and 9 deletions

View File

@ -4,6 +4,19 @@
## 2.2.0
### Fixed event tracking for merchant email notes #6616
- Create a brand new site.
- Install a plugin to log every sent email (you can use [WP mail logging](https://wordpress.org/plugins/wp-mail-logging/)).
- Install and active [this gist](https://gist.github.com/octaedro/864315edaf9c6a2a6de71d297be1ed88) to create an email note. Just download the file and install it as a plugin.
- After activating the plugin, press `Add Email Notes` to create a note.
- Now go to WooCommerce > Settings > Email (`/wp-admin/admin.php?page=wc-settings&tab=email`) and check the checkbox `Enable email insights` and save changes.
- You will need to run the cron so you can install a plugin like [WP Crontol](https://wordpress.org/plugins/wp-crontrol/)
- Go to Tools > Cron events (`/wp-admin/tools.php?page=crontrol_admin_manage_page`).
- Call the hook `wc_admin_daily` by pressing its `Run Now` link. (https://user-images.githubusercontent.com/1314156/111530634-4929ce80-8742-11eb-8b53-de936ceea76e.png)
- Go to Tools > WP Mail Logging Log (`/wp-admin/tools.php?page=wpml_plugin_log`) and verify the testing email note was sent.
- View the message and press `Test action` (a broken image will be visible under the button, but that's expected and only visible in a test environment).
### Payments task: include Mercado Pago #6572
- Create a brand new store.

View File

@ -76,6 +76,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
== Unreleased ==
- Tweak: Add check to see if value for contains is array, show warning if not. #6645
- Fix: Event tracking for merchant email notes #6616
== 2.2.0 3/26/2021 ==

View File

@ -103,7 +103,7 @@ class Notes extends \WC_REST_CRUD_Controller {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/tracker/(?P<note_id>[\d-]+)',
'/' . $this->rest_base . '/tracker/(?P<note_id>[\d-]+)/user/(?P<user_id>[\d-]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
@ -475,7 +475,11 @@ class Notes extends \WC_REST_CRUD_Controller {
if ( ! $note ) {
return;
}
// We need to set the current user for tracking reasons. And unset user after tracking.
wp_set_current_user( $request->get_param( 'user_id' ) );
wc_admin_record_tracks_event( 'wcadmin_email_note_opened', array( 'note_name' => $note->get_name() ) );
wp_set_current_user( 0 );
}
/**

View File

@ -30,6 +30,7 @@ class MerchantEmailNotifications {
if (
! isset( $_GET['external_redirect'] ) ||
1 !== intval( $_GET['external_redirect'] ) ||
! isset( $_GET['user'] ) ||
! isset( $_GET['note'] ) ||
! isset( $_GET['action'] )
) {
@ -37,6 +38,7 @@ class MerchantEmailNotifications {
}
$note_id = intval( $_GET['note'] );
$action_id = intval( $_GET['action'] );
$user_id = intval( $_GET['user'] );
/* phpcs:enable */
$note = Notes::get_note( $note_id );
@ -51,7 +53,10 @@ class MerchantEmailNotifications {
return;
}
// We need to set the current user for tracking reasons. And unset user after tracking.
wp_set_current_user( $user_id );
Notes::trigger_note_action( $note, $triggered_action );
wp_set_current_user( 0 );
$url = $triggered_action->query;
@ -82,7 +87,6 @@ class MerchantEmailNotifications {
self::send_merchant_notification( $note );
$note->set_status( 'sent' );
$note->save();
wc_admin_record_tracks_event( 'wcadmin_email_note_sent', array( 'note_name' => $note->get_name() ) );
}
}
}
@ -99,7 +103,7 @@ class MerchantEmailNotifications {
foreach ( $users as $user ) {
if ( is_email( $user->user_email ) ) {
$name = self::get_merchant_preferred_name( $user );
$email->trigger( $user->user_email, $name );
$email->trigger( $user->user_email, $user->ID, $name );
}
}
}
@ -126,7 +130,7 @@ class MerchantEmailNotifications {
* Get users by role to notify.
*
* @param object $note The note to send.
* @return array Emails to notify
* @return array Users to notify
*/
public static function get_notification_recipients( $note ) {
$content_data = $note->get_content_data();

View File

@ -176,19 +176,22 @@ class NotificationEmail extends \WC_Email {
* Trigger the sending of this email.
*
* @param string $user_email Email to send the note.
* @param int $user_id User id to to track the note.
* @param string $user_name User's name.
*/
public function trigger( $user_email, $user_name ) {
public function trigger( $user_email, $user_id, $user_name ) {
$this->recipient = $user_email;
$this->opened_tracking_url = sprintf(
'%1$s/wp-json/wc-analytics/admin/notes/tracker/%2$d',
'%1$s/wp-json/wc-analytics/admin/notes/tracker/%2$d/user/%3$d',
site_url(),
$this->note->get_id()
$this->note->get_id(),
$user_id
);
$this->trigger_note_action_url = sprintf(
'%1$s&external_redirect=1&note=%2$d&action=',
'%1$s&external_redirect=1&note=%2$d&user=%3$d&action=',
wc_admin_url(),
$this->note->get_id()
$this->note->get_id(),
$user_id
);
if ( $user_name ) {
@ -203,5 +206,8 @@ class NotificationEmail extends \WC_Email {
$this->get_headers(),
$this->get_attachments()
);
wp_set_current_user( $user_id );
wc_admin_record_tracks_event( 'wcadmin_email_note_sent', array( 'note_name' => $this->note->get_name() ) );
wp_set_current_user( 0 );
}
}