Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Hourly" as a default interval #20

Merged
merged 12 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

* Added "hourly" as a default interval for stores ([#20]).
* Added new placeholders to user-facing messaging ([#20]):
- `{current_interval:date}` (alias of `{current_interval}`)
- `{current_interval:time}`
- `{next_interval:date}` (alias of `{next_interval}`)
- `{next_interval:time}`

### Updated

* The settings screen will now show custom placeholders that have been registered via the "limit_orders_message_placeholders" filter ([#20]).

## [Version 1.1.2] - 2020-04-17

### Fixed
Expand Down Expand Up @@ -44,3 +59,4 @@ Initial plugin release.
[#8]: https://github.com/nexcess/limit-orders/pull/8
[#10]: https://github.com/nexcess/limit-orders/pull/10
[#13]: https://github.com/nexcess/limit-orders/pull/13
[#20]: https://github.com/nexcess/limit-orders/pull/20
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Configuration for Limit Orders for WooCommerce is available through WooCommerce
<dd>Customers will be unable to checkout after this number of orders are received.</dd>
<dd>Shop owners will still be able to create orders via WP Admin, even after the limit has been reached.</dd>
<dt>Interval</dt>
<dd>How often the limit is reset. By default, this can be "daily", "weekly", or "monthly".</dd>
<dd>How often the limit is reset. By default, this can be "hourly", daily", "weekly", or "monthly".</dd>
<dd>When choosing "weekly", the plugin will respect the value of <a href="https://wordpress.org/support/article/settings-general-screen/#week-starts-on">the store's "week starts on" setting</a>.</dd>
</dl>

Expand All @@ -61,8 +61,16 @@ In any of these messages, you may also use the following variables:
<dd>The maximum number of orders accepted.</dd>
<dt>{current_interval}</dt>
<dd>The date the current interval started.</dd>
<dt>{current_interval:date}</dt>
<dd>An alias of <var>{current_interval}</var></dd>
<dt>{current_interval:time}</dt>
<dd>The time the current interval started.</dd>
<dt>{next_interval}</dt>
<dd>The date the next interval will begin (e.g. when orders will be accepted again).</dd>
<dt>{next_interval:date}</dt>
<dd>An alias of <var>{next_interval}</var></dd>
<dt>{next_interval:time}</dt>
<dd>The time the next interval will begin.</dd>
</dl>

Both `{current_interval}` and `{next_interval}` will be formatted [according to the "date format" setting for your store](https://wordpress.org/support/article/settings-general-screen/#date-format).
Dates and times will be formatted [according to the "date format" and "time format" settings for your store](https://wordpress.org/support/article/settings-general-screen/#date-format), respectively.
17 changes: 15 additions & 2 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,33 @@ public function admin_notice() {
return;
}

// Change what text we show based on how far off it is.
$next_interval = $this->limiter->get_next_interval_start();
$midnight = current_datetime()->setTime( 24, 0, 0 );

// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
if ( $next_interval == $midnight ) {
$next = _x( 'midnight', 'beginning of the next day/interval', 'limit-orders' );
} elseif ( $next_interval < $midnight ) {
$next = $next_interval->format( get_option( 'time_format' ) );
} else {
$next = $next_interval->format( get_option( 'date_format' ) );
}

echo '<div class="notice notice-warning"><p>';

if ( current_user_can( 'manage_options' ) ) {
echo wp_kses_post( sprintf(
/* Translators: %1$s is the settings page URL, %2$s is the reset date for order limiting. */
__( '<a href="%1$s">Based on your store\'s configuration</a>, new orders have been put on hold until %2$s.', 'limit-orders' ),
$this->get_settings_url(),
$this->limiter->get_next_interval_start()->format( get_option( 'date_format' ) )
$next
) );
} else {
echo esc_html( sprintf(
/* Translators: %1$s is the reset date for order limiting. */
__( 'Based on your store\'s configuration, new orders have been put on hold until %1$s.', 'limit-orders' ),
$this->limiter->get_next_interval_start()->format( get_option( 'date_format' ) )
$next
) );
}
echo '</p></div>';
Expand Down
63 changes: 49 additions & 14 deletions src/OrderLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function init() {
* @return bool
*/
public function is_enabled() {
return (bool) $this->get_setting( 'enabled' );
return (bool) $this->get_setting( 'enabled', false );
}

/**
Expand All @@ -67,7 +67,7 @@ public function is_enabled() {
* @return string The order limiter's interval.
*/
public function get_interval() {
return $this->get_setting( 'interval' );
return $this->get_setting( 'interval', 'daily' );
}

/**
Expand Down Expand Up @@ -108,11 +108,34 @@ public function get_message( string $setting ) {
}

// Perform simple placeholder replacements.
$placeholders = $this->get_placeholders( $setting, $message );

return str_replace( array_keys( $placeholders ), array_values( $placeholders ), $message );
}

/**
* Retrieve eligible placeholders for front-end messaging.
*
* Note that the parameters are only included for the sake of the filter.
*
* @param string $setting Optional. The current setting that's being retrieved. Default is empty.
* @param string $message Optional. The current message being constructed. Default is empty.
*
* @return array An array of placeholder => replacements.
*/
public function get_placeholders( $setting = '', $message = '' ) {
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
$current = $this->get_interval_start();
$next = $this->get_next_interval_start();
$placeholders = [
'{current_interval}' => $this->get_interval_start()->format( $date_format ),
'{limit}' => $this->get_limit(),
'{next_interval}' => $this->get_next_interval_start()->format( $date_format ),
'{current_interval}' => $current->format( $date_format ),
'{current_interval:date}' => $current->format( $date_format ),
'{current_interval:time}' => $current->format( $time_format ),
'{limit}' => $this->get_limit(),
'{next_interval}' => $next->format( $date_format ),
'{next_interval:date}' => $next->format( $date_format ),
'{next_interval:time}' => $next->format( $time_format ),
];

/**
Expand All @@ -122,9 +145,7 @@ public function get_message( string $setting ) {
* @param string $setting The current message's setting key.
* @param string $message The current message to display.
*/
$placeholders = apply_filters( 'limit_orders_message_placeholders', $placeholders, $setting, $message );

return str_replace( array_keys( $placeholders ), array_values( $placeholders ), $message );
return apply_filters( 'limit_orders_message_placeholders', $placeholders, $setting, $message );
}

/**
Expand Down Expand Up @@ -161,6 +182,16 @@ public function get_interval_start() {
$start = $this->now;

switch ( $interval ) {
case 'hourly':
// Start at the top of the current hour.
$start = $start->setTime( (int) $start->format( 'h' ), 0, 0 );
break;

case 'daily':
// Start at midnight.
$start = $start->setTime( 0, 0, 0 );
break;

case 'weekly':
$start_of_week = (int) get_option( 'week_starts_on' );
$current_dow = (int) $start->format( 'w' );
Expand All @@ -175,16 +206,16 @@ public function get_interval_start() {
if ( 0 !== $diff ) {
$start = $start->sub( new \DateInterval( 'P' . $diff . 'D' ) );
}

$start = $start->setTime( 0, 0, 0 );
break;

case 'monthly':
$start = $start->setDate( (int) $start->format( 'Y' ), (int) $start->format( 'm' ), 1 );
$start = $start->setDate( (int) $start->format( 'Y' ), (int) $start->format( 'm' ), 1 )
->setTime( 0, 0, 0 );
break;
}

// Start everything at midnight.
$start = $start->setTime( 0, 0, 0 );

/**
* Filter the DateTime object representing the start of the current interval.
*
Expand All @@ -205,6 +236,10 @@ public function get_next_interval_start() {
$start = clone $current;

switch ( $interval ) {
case 'hourly':
$start = $start->add( new \DateInterval( 'PT1H' ) );
break;

case 'daily':
$start = $start->add( new \DateInterval( 'P1D' ) );
break;
Expand Down Expand Up @@ -338,11 +373,11 @@ protected function count_qualifying_orders() {
*
* @return mixed The value of $setting, or null $setting is undefined.
*/
protected function get_setting( string $setting ) {
protected function get_setting( string $setting, $default = null ) {
if ( null === $this->settings ) {
$this->settings = get_option( self::OPTION_KEY, [] );
}

return isset( $this->settings[ $setting ] ) ? $this->settings[ $setting ] : null;
return isset( $this->settings[ $setting ] ) ? $this->settings[ $setting ] : $default;
}
}
13 changes: 12 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function __construct( OrderLimiter $limiter ) {
* @return array
*/
public function get_settings() {
$placeholders = (array) $this->limiter->get_placeholders();
$available_placeholders = '';

// Build a list of available placeholders.
if ( ! empty( $placeholders ) ) {
$available_placeholders = __( 'Available placeholders:', 'limit-orders' ) . ' <var>';
$available_placeholders .= implode( '</var>, <var>', array_keys( $placeholders ) );
$available_placeholders .= '</var>';
}

return apply_filters( 'woocommerce_get_settings_' . $this->id, [
[
'id' => 'limit-orders-general',
Expand Down Expand Up @@ -75,7 +85,7 @@ public function get_settings() {
'id' => 'limit-orders-messaging',
'type' => 'title',
'name' => _x( 'Customer messaging', 'settings section title', 'limit-orders' ),
'desc' => '<p>' . __( 'Customize the messages shown to customers once ordering is disabled.', 'limit-orders' ) . '</p><p>' . __( 'Available placeholders: <var>{limit}</var>, <var>{current_interval}</var>, <var>{next_interval}</var>.', 'limit-orders' ) . '</p>',
'desc' => '<p>' . __( 'Customize the messages shown to customers once ordering is disabled.', 'limit-orders' ) . '</p>' . $available_placeholders ? '<p>' . $available_placeholders . '</p>' : '',
],
[
'id' => OrderLimiter::OPTION_KEY . '[customer_notice]',
Expand Down Expand Up @@ -116,6 +126,7 @@ protected function get_intervals() {
global $wp_locale;

$intervals = [
'hourly' => _x( 'Hourly (resets at the top of every hour)', 'order threshold interval', 'limit-orders' ),
'daily' => _x( 'Daily (resets every day)', 'order threshold interval', 'limit-orders' ),
'weekly' => sprintf(
/* Translators: %1$s is the first day of the week, based on site configuration. */
Expand Down
54 changes: 51 additions & 3 deletions tests/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function admin_notices_should_not_be_shown_if_no_limits_have_been_reached
$limiter = $this->getMockBuilder( OrderLimiter::class )
->setMethods( [ 'has_reached_limit' ] )
->getMock();

$limiter->method( 'has_reached_limit' )
->willReturn( false );

Expand All @@ -48,7 +47,6 @@ public function admin_notices_should_be_shown_once_limits_are_reached() {
$limiter = $this->getMockBuilder( OrderLimiter::class )
->setMethods( [ 'has_reached_limit' ] )
->getMock();

$limiter->method( 'has_reached_limit' )
->willReturn( true );

Expand All @@ -71,7 +69,6 @@ public function admin_notices_should_not_include_links_to_settings_for_non_admin
$limiter = $this->getMockBuilder( OrderLimiter::class )
->setMethods( [ 'has_reached_limit' ] )
->getMock();

$limiter->method( 'has_reached_limit' )
->willReturn( true );

Expand All @@ -81,4 +78,55 @@ public function admin_notices_should_not_include_links_to_settings_for_non_admin

$this->assertNotContains( admin_url( 'admin.php?page=wc-settings' ), $output );
}

/**
* @test
* @group Intervals
* @ticket https://github.com/nexcess/limit-orders/issues/18
*/
public function intervals_of_less_than_a_day_should_use_time_instead_of_date_in_the_admin_notice() {
wp_set_current_user( $this->factory->user->create( [
'role' => 'editor',
] ) );

$next = ( new \DateTime( 'now' ) )->setTime( 7, 0, 0 );
$limiter = $this->getMockBuilder( OrderLimiter::class )
->setMethods( [ 'has_reached_limit', 'get_next_interval_start' ] )
->getMock();
$limiter->method( 'has_reached_limit' )
->willReturn( true );
$limiter->method( 'get_next_interval_start' )
->willReturn( $next );

ob_start();
( new Admin( $limiter ) )->admin_notice();
$output = ob_get_clean();

$this->assertContains( $next->format( get_option( 'time_format' ) ), $output );
}

/**
* @test
* @group Intervals
*/
public function admin_notices_should_use_midnight_instead_of_dates_for_daily_interval() {
wp_set_current_user( $this->factory->user->create( [
'role' => 'editor',
] ) );

$next = ( new \DateTime( 'now' ) )->setTime( 24, 0, 0 ); // Midnight.
$limiter = $this->getMockBuilder( OrderLimiter::class )
->setMethods( [ 'has_reached_limit', 'get_next_interval_start' ] )
->getMock();
$limiter->method( 'has_reached_limit' )
->willReturn( true );
$limiter->method( 'get_next_interval_start' )
->willReturn( $next );

ob_start();
( new Admin( $limiter ) )->admin_notice();
$output = ob_get_clean();

$this->assertContains( __( 'midnight' ), $output );
}
}
Loading