Refactor inbox notes to have get_note method (#34357)

* Refactor NewSalesRecord to have get_note method

* Refactor OrderMilestones to have get_note method

* Add unit tests for notes

* Fix wrong content data return type

* Add changelog
This commit is contained in:
Chi-Hsuan Huang 2022-08-30 09:43:27 +08:00 committed by GitHub
parent 72d8c791e6
commit 9d23439136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 49 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Refactor inbox notes to have get_note method

View File

@ -229,7 +229,7 @@ class Note extends \WC_Data {
* Get note content data (i.e. values that would be needed for re-localization)
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
* @return object
*/
public function get_content_data( $context = 'view' ) {
return $this->get_prop( 'content_data', $context );

View File

@ -95,50 +95,85 @@ class NewSalesRecord {
update_option( self::RECORD_DATE_OPTION_KEY, $yesterday );
update_option( self::RECORD_AMOUNT_OPTION_KEY, $total );
// Use F jS (March 7th) format for English speaking countries.
if ( substr( get_locale(), 0, 2 ) === 'en' ) {
$date_format = 'F jS';
} else {
// otherwise, fallback to the system date format.
$date_format = get_option( 'date_format' );
}
$formatted_yesterday = date_i18n( $date_format, strtotime( $yesterday ) );
$formatted_total = html_entity_decode( wp_strip_all_tags( wc_price( $total ) ) );
$formatted_record_date = date_i18n( $date_format, strtotime( $record_date ) );
$formatted_record_amt = html_entity_decode( wp_strip_all_tags( wc_price( $record_amt ) ) );
$content = sprintf(
/* translators: 1 and 4: Date (e.g. October 16th), 2 and 3: Amount (e.g. $160.00) */
__( 'Woohoo, %1$s was your record day for sales! Net sales was %2$s beating the previous record of %3$s set on %4$s.', 'woocommerce' ),
$formatted_yesterday,
$formatted_total,
$formatted_record_amt,
$formatted_record_date
);
$content_data = (object) array(
'old_record_date' => $record_date,
'old_record_amt' => $record_amt,
'new_record_date' => $yesterday,
'new_record_amt' => $total,
);
// We only want one sales record note at any time in the inbox, so we delete any other first.
Notes::delete_notes_with_name( self::NOTE_NAME );
$report_url = '?page=wc-admin&path=/analytics/revenue&period=custom&compare=previous_year&after=' . $yesterday . '&before=' . $yesterday;
// And now, create our new note.
$note = new Note();
$note->set_title( __( 'New sales record!', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( $content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'view-report', __( 'View report', 'woocommerce' ), $report_url );
$note = self::get_note_with_record_data( $record_date, $record_amt, $yesterday, $total );
$note->save();
}
}
/**
* Get the note with record data.
*
* @param string $record_date record date Y-m-d.
* @param float $record_amt record amount.
* @param string $yesterday yesterday's date Y-m-d.
* @param string $total total sales for yesterday.
*
* @return Note
*/
public static function get_note_with_record_data( $record_date, $record_amt, $yesterday, $total ) {
// Use F jS (March 7th) format for English speaking countries.
if ( substr( get_user_locale(), 0, 2 ) === 'en' ) {
$date_format = 'F jS';
} else {
// otherwise, fallback to the system date format.
$date_format = get_option( 'date_format' );
}
$formatted_yesterday = date_i18n( $date_format, strtotime( $yesterday ) );
$formatted_total = html_entity_decode( wp_strip_all_tags( wc_price( $total ) ) );
$formatted_record_date = date_i18n( $date_format, strtotime( $record_date ) );
$formatted_record_amt = html_entity_decode( wp_strip_all_tags( wc_price( $record_amt ) ) );
$content = sprintf(
/* translators: 1 and 4: Date (e.g. October 16th), 2 and 3: Amount (e.g. $160.00) */
__( 'Woohoo, %1$s was your record day for sales! Net sales was %2$s beating the previous record of %3$s set on %4$s.', 'woocommerce' ),
$formatted_yesterday,
$formatted_total,
$formatted_record_amt,
$formatted_record_date
);
$content_data = (object) array(
'old_record_date' => $record_date,
'old_record_amt' => $record_amt,
'new_record_date' => $yesterday,
'new_record_amt' => $total,
);
$report_url = '?page=wc-admin&path=/analytics/revenue&period=custom&compare=previous_year&after=' . $yesterday . '&before=' . $yesterday;
// And now, create our new note.
$note = new Note();
$note->set_title( __( 'New sales record!', 'woocommerce' ) );
$note->set_content( $content );
$note->set_content_data( $content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note->add_action( 'view-report', __( 'View report', 'woocommerce' ), $report_url );
return $note;
}
/**
* Get the note. This is used for localizing the note.
*
* @return Note
*/
public static function get_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
$content_data = $note->get_content_data();
return self::get_note_with_record_data(
$content_data->old_record_date,
$content_data->old_record_amt,
$content_data->new_record_date,
$content_data->new_record_amt
);
}
}

View File

@ -11,7 +11,6 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Notes\Note;
use \Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Order_Milestones
*/
@ -179,7 +178,7 @@ class OrderMilestones {
* @param int $milestone Order milestone.
* @return string Note title for the milestone.
*/
public function get_note_title_for_milestone( $milestone ) {
public static function get_note_title_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return __( 'First order received', 'woocommerce' );
@ -208,7 +207,7 @@ class OrderMilestones {
* @param int $milestone Order milestone.
* @return string Note content for the milestone.
*/
public function get_note_content_for_milestone( $milestone ) {
public static function get_note_content_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return __( 'Congratulations on getting your first order! Now is a great time to learn how to manage your orders.', 'woocommerce' );
@ -234,7 +233,7 @@ class OrderMilestones {
* @param int $milestone Order milestone.
* @return array Note actoion (name, label, query) for the milestone.
*/
public function get_note_action_for_milestone( $milestone ) {
public static function get_note_action_for_milestone( $milestone ) {
switch ( $milestone ) {
case 1:
return array(
@ -288,6 +287,25 @@ class OrderMilestones {
return $milestone_notes_enabled;
}
/**
* Get the note. This is used for localizing the note.
*
* @return Note
*/
public static function get_note() {
$note = Notes::get_note_by_name( self::NOTE_NAME );
if ( ! $note ) {
return false;
}
$content_data = $note->get_content_data();
if ( ! isset( $content_data->current_milestone ) ) {
return false;
}
return self::get_note_by_milestone(
$content_data->current_milestone
);
}
/**
* Get the note by milestones.
*
@ -295,15 +313,20 @@ class OrderMilestones {
*
* @return Note
*/
public function get_note_by_milestone( $current_milestone ) {
public static function get_note_by_milestone( $current_milestone ) {
$content_data = (object) array(
'current_milestone' => $current_milestone,
);
$note = new Note();
$note->set_title( self::get_note_title_for_milestone( $current_milestone ) );
$note->set_content( self::get_note_content_for_milestone( $current_milestone ) );
$note_action = self::get_note_action_for_milestone( $current_milestone );
$note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] );
$note->set_content_data( $content_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
$note_action = self::get_note_action_for_milestone( $current_milestone );
$note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] );
return $note;
}

View File

@ -9,7 +9,7 @@ use \Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Class WC_Admin_Tests_Marketing_Notes
* Class WC_Admin_Tests_Order_Milestones
*/
class WC_Admin_Tests_Order_Milestones extends WC_Unit_Test_Case {
@ -113,4 +113,22 @@ class WC_Admin_Tests_Order_Milestones extends WC_Unit_Test_Case {
$note = Notes::get_note_by_name( OrderMilestones::NOTE_NAME );
$this->assertEquals( $note->get_content(), "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration." );
}
/**
* Tests get_note method return false when note does not exist in db.
*/
public function test_get_note_when_note_not_exist_in_db() {
$this->assertFalse( OrderMilestones::get_note() );
}
/**
* Tests get_note method return note when note exists in db.
*/
public function test_get_note_when_note_exists_in_db() {
WC_Helper_Order::create_order();
$this->instance->possibly_add_note();
$note = OrderMilestones::get_note();
$this->assertEquals( $note->get_title(), 'First order received' );
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
* NewSalesRecord note tests
*
* @package WooCommerce\Admin\Tests\Notes
*/
use \Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord;
/**
* Class WC_Admin_New_Sales_Record
*/
class WC_Admin_Tests_New_Sales_Record extends WC_Unit_Test_Case {
/**
* Tests get_note method return false when note does not exist in db.
*/
public function test_get_note_when_note_not_exist_in_db() {
$this->assertFalse( NewSalesRecord::get_note() );
}
/**
* Tests get_note method return note when note exists in db.
*/
public function test_get_note_when_note_exists_in_db() {
$record_date = '2022-08-15';
$record_amount = 100;
$yesterday = '2022-08-16';
$total = 200;
$note = NewSalesRecord::get_note_with_record_data( $record_date, $record_amount, $yesterday, $total );
$note->save();
$note = NewSalesRecord::get_note();
$this->assertEquals( $note->get_content(), 'Woohoo, August 16th was your record day for sales! Net sales was $200.00 beating the previous record of $100.00 set on August 15th.' );
}
}