* Add notes helper method for date ranges

* Update two of the notes with date ranges

* Add tests and fix range logic

* Add changelog

* Update comments

* Update range name and added exception if invalid range passed in.

* Fix php unit tests

* Fix another test
This commit is contained in:
louwie17 2021-05-13 09:41:21 -03:00 committed by GitHub
parent a7db89ff4f
commit 295cd6b2c9
6 changed files with 143 additions and 6 deletions

View File

@ -81,6 +81,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Add: Optional children prop to SummaryNumber component #6748
- Add: Extend payment gateways REST endpoint #6919
- Add: Add remote payment gateway recommendations initial docs #6962
- Add: Note date range logic for GivingFeedback, and InsightFirstSale note. #6969
- Dev: Add data source filter to remote inbox notification system #6794
- Dev: Add A/A test #6669
- Dev: Add support for nonces in note actions #6726

View File

@ -31,9 +31,7 @@ class GivingFeedbackNotes {
* @return Note
*/
protected static function get_note() {
// We need to show Admin Giving feeback notification after 8 days of install.
$eight_days_in_seconds = 8 * DAY_IN_SECONDS;
if ( ! self::wc_admin_active_for( $eight_days_in_seconds ) ) {
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}

View File

@ -31,8 +31,7 @@ class InsightFirstSale {
* @return Note
*/
public static function get_note() {
// We want to show the note after eight days.
if ( ! self::wc_admin_active_for( 8 * DAY_IN_SECONDS ) ) {
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}

View File

@ -21,10 +21,20 @@ trait NoteTraits {
* @param int $seconds Time in seconds to check.
* @return bool Whether or not WooCommerce admin has been active for $seconds.
*/
public static function wc_admin_active_for( $seconds ) {
private static function wc_admin_active_for( $seconds ) {
return WCAdminHelper::is_wc_admin_active_for( $seconds );
}
/**
* Test if WooCommerce Admin has been active within a pre-defined range.
*
* @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
* @return bool Whether or not WooCommerce admin has been active within the range.
*/
private static function is_wc_admin_active_in_date_range( $range ) {
return WCAdminHelper::is_wc_admin_active_in_date_range( $range );
}
/**
* Check if the note has been previously added.
*

View File

@ -18,6 +18,28 @@ class WCAdminHelper {
*/
const WC_ADMIN_TIMESTAMP_OPTION = 'woocommerce_admin_install_timestamp';
const WC_ADMIN_STORE_AGE_RANGES = array(
'week-1' => array(
'start' => 0,
'end' => WEEK_IN_SECONDS,
),
'week-1-4' => array(
'start' => WEEK_IN_SECONDS,
'end' => WEEK_IN_SECONDS * 4,
),
'month-1-3' => array(
'start' => MONTH_IN_SECONDS,
'end' => MONTH_IN_SECONDS * 3,
),
'month-3-6' => array(
'start' => MONTH_IN_SECONDS * 3,
'end' => MONTH_IN_SECONDS * 6,
),
'month-6+' => array(
'start' => MONTH_IN_SECONDS * 6,
),
);
/**
* Get the number of seconds that the store has been active.
*
@ -46,4 +68,30 @@ class WCAdminHelper {
return ( $wc_admin_active_for >= $seconds );
}
/**
* Test if WooCommerce Admin has been active within a pre-defined range.
*
* @param string $range range available in WC_ADMIN_STORE_AGE_RANGES.
* @throws \InvalidArgumentException Throws exception when invalid $range is passed in.
* @return bool Whether or not WooCommerce admin has been active within the range.
*/
public static function is_wc_admin_active_in_date_range( $range ) {
if ( ! array_key_exists( $range, self::WC_ADMIN_STORE_AGE_RANGES ) ) {
throw new \InvalidArgumentException(
sprintf(
'"%s" range is not supported, use one of: %s',
$range,
implode( ', ', array_keys( self::WC_ADMIN_STORE_AGE_RANGES ) )
)
);
}
$wc_admin_active_for = self::get_wcadmin_active_for_in_seconds();
$range_data = self::WC_ADMIN_STORE_AGE_RANGES[ $range ];
if ( $range_data && $wc_admin_active_for >= $range_data['start'] ) {
return isset( $range_data['end'] ) ? $wc_admin_active_for < $range_data['end'] : true;
}
return false;
}
}

View File

@ -0,0 +1,81 @@
<?php
/**
* WCAdminHelper tests
*
* @package WooCommerce\Admin\Tests\WCAdminHelper
*/
use \Automattic\WooCommerce\Admin\WCAdminHelper;
/**
* WC_Admin_Tests_Admin_Helper Class
*
* @package WooCommerce\Admin\Tests\WCAdminHelper
*/
class WC_Admin_Tests_Admin_Helper extends WP_UnitTestCase {
/**
* Test wc_admin_active_for one hour
*/
public function test_is_wc_admin_active_for_one_hour() {
update_option( WCAdminHelper::WC_ADMIN_TIMESTAMP_OPTION, time() - ( HOUR_IN_SECONDS * 10 ) );
// Active for one hour - true.
$active_for = WCAdminHelper::is_wc_admin_active_for( HOUR_IN_SECONDS );
$this->assertEquals( true, $active_for );
}
/**
* Test wc_admin_active_for 7 days
*/
public function test_is_wc_admin_active_for_7_days() {
update_option( WCAdminHelper::WC_ADMIN_TIMESTAMP_OPTION, time() - ( HOUR_IN_SECONDS * 10 ) );
// Active for 7 days - false.
$active_for = WCAdminHelper::is_wc_admin_active_for( DAY_IN_SECONDS * 7 );
$this->assertEquals( false, $active_for );
}
/**
* Test wc_admin_active_in_date_range with invalid range
*/
public function test_is_wc_admin_active_in_date_range_with_invalid_range() {
$this->expectException( \InvalidArgumentException::class );
$this->expectExceptionMessage( '"random-range" range is not supported, use one of: week-1, week-1-4, month-1-3, month-3-6, month-6+' );
WCAdminHelper::is_wc_admin_active_in_date_range( 'random-range' );
}
/**
* @dataProvider range_provider
* Test wc_admin_active_in_date_range with data provided from range_provider.
*
* @param number $store_age age in seconds of store.
* @param string $range expected store range.
* @param boolean $expected expected boolean value.
*/
public function test_is_wc_admin_active_in_date_range( $store_age, $range, $expected ) {
// 1 day.
update_option( WCAdminHelper::WC_ADMIN_TIMESTAMP_OPTION, time() - $store_age );
$active_for = WCAdminHelper::is_wc_admin_active_in_date_range( $range );
$this->assertEquals( $expected, $active_for );
}
/**
* @return array[] list of range options.
*/
public function range_provider() {
return array(
'1 day old store within week?' => array( DAY_IN_SECONDS, 'week-1', true ),
'10 day old store not within week?' => array( 10 * DAY_IN_SECONDS, 'week-1', false ),
'10 day old store within 1-4 weeks?' => array( 10 * DAY_IN_SECONDS, 'week-1-4', true ),
'1 day old store not within 1-4 weeks?' => array( DAY_IN_SECONDS, 'week-1-4', false ),
'2 month old store within 1-3 months?' => array( 2 * MONTH_IN_SECONDS, 'month-1-3', true ),
'5 month old store not within 1-3 months?' => array( 5 * MONTH_IN_SECONDS, 'month-1-3', false ),
'5 month old store within 3-6 months?' => array( 5 * MONTH_IN_SECONDS, 'month-3-6', true ),
'7 month old store not within 3-6 months?' => array( 7 * MONTH_IN_SECONDS, 'month-3-6', false ),
'9 month old store within 6+ months?' => array( 9 * MONTH_IN_SECONDS, 'month-6+', true ),
'2 month old store not within 6+ months?' => array( 2 * MONTH_IN_SECONDS, 'month-6+', false ),
);
}
}