Skip to content

Change the format of the {current_interval} and {next_interval} placeholders if the interval is less than 24 hours #53

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

Merged
merged 2 commits into from
Sep 17, 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
5 changes: 3 additions & 2 deletions src/OrderLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ public function get_placeholders( $setting = '', $message = '' ) {
$time_format = get_option( 'time_format' );
$current = $this->get_interval_start();
$next = $this->get_next_interval_start();
$within24hr = $next->getTimestamp() - $current->getTimestamp() < DAY_IN_SECONDS;
$placeholders = [
'{current_interval}' => $current->format( $date_format ),
'{current_interval}' => $current->format( $within24hr ? $time_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}' => $next->format( $within24hr ? $time_format : $date_format ),
'{next_interval:date}' => $next->format( $date_format ),
'{next_interval:time}' => $next->format( $time_format ),
'{timezone}' => $next->format( 'T' ),
Expand Down
44 changes: 32 additions & 12 deletions tests/OrderLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public function get_message_should_not_expose_other_settings() {
public function get_message_should_replace_current_interval_placeholder( $placeholder ) {
update_option( 'date_format', 'F j, Y' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'weekly',
'interval' => 'monthly',
'customer_notice' => "This started on {$placeholder}",
] );

$now = new \DateTimeImmutable( 'now', wp_timezone() );
$now = new \DateTimeImmutable( '2020-04-28 00:00:00', wp_timezone() );
$limiter = new OrderLimiter( $now );

$this->assertSame(
Expand All @@ -143,11 +143,11 @@ public function get_message_should_replace_current_interval_placeholder( $placeh
public function get_message_should_replace_current_interval_time_placeholder() {
update_option( 'time_format', 'g:ia' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
'interval' => 'monthly',
'customer_notice' => "This started at {current_interval:time}",
] );

$now = new \DateTimeImmutable( 'now', wp_timezone() );
$now = new \DateTimeImmutable( '2020-04-28 00:00:00', wp_timezone() );
$limiter = new OrderLimiter( $now );

$this->assertSame(
Expand Down Expand Up @@ -184,11 +184,11 @@ public function get_message_should_replace_limit_placeholder() {
public function get_message_should_replace_next_interval_placeholder( $placeholder ) {
update_option( 'date_format', 'F j, Y' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'weekly',
'interval' => 'monthly',
'customer_notice' => "Check back on {$placeholder}",
] );

$now = new \DateTimeImmutable( 'now', wp_timezone() );
$now = new \DateTimeImmutable( '2020-04-28 00:00:00', wp_timezone() );
$limiter = new OrderLimiter( $now );

$this->assertSame(
Expand All @@ -205,11 +205,11 @@ public function get_message_should_replace_next_interval_placeholder( $placehold
public function get_message_should_replace_next_interval_time_placeholder() {
update_option( 'time_format', 'g:ia' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
'interval' => 'monthly',
'customer_notice' => "Check back at {next_interval:time}",
] );

$now = new \DateTimeImmutable( 'now', wp_timezone() );
$now = new \DateTimeImmutable( '2020-04-27 00:00:00', wp_timezone() );
$limiter = new OrderLimiter( $now );

$this->assertSame(
Expand All @@ -228,12 +228,12 @@ public function get_placeholders_should_return_an_array_of_default_placeholders(
update_option( 'date_format', 'F j, Y' );
update_option( 'time_format', 'g:ia' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
'interval' => 'daily',
] );

$now = new \DateTimeImmutable( '2020-04-27 12:15:00', wp_timezone() );
$current = new \DateTimeImmutable( '2020-04-27 12:00:00', wp_timezone() );
$next = new \DateTimeImmutable( '2020-04-27 13:00:00', wp_timezone() );
$now = new \DateTimeImmutable( '2020-04-27 00:00:00', wp_timezone() );
$current = new \DateTimeImmutable( '2020-04-27 00:00:00', wp_timezone() );
$next = new \DateTimeImmutable( '2020-04-28 00:00:00', wp_timezone() );
$placeholders = ( new OrderLimiter( $now ) )->get_placeholders();

$this->assertSame( $current->format( 'F j, Y' ), $placeholders['{current_interval}'] );
Expand Down Expand Up @@ -265,6 +265,26 @@ public function time_placeholders_should_replace_00_with_midnight() {
$this->assertSame( __( 'midnight', 'limit-orders' ), $placeholders['{next_interval:time}'] );
}

/**
* @test
* @group Placeholders
* @ticket https://github.com/nexcess/limit-orders/issues/51
*/
public function placeholders_should_switch_between_date_and_time_based_on_interval_length() {
update_option( 'time_format', 'g:ia' );
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
] );

$now = new \DateTimeImmutable( '2020-09-17 12:15:00', wp_timezone() );
$current = new \DateTimeImmutable( '2020-09-17 12:00:00', wp_timezone() );
$next = new \DateTimeImmutable( '2020-09-17 13:00:00', wp_timezone() );
$placeholders = ( new OrderLimiter( $now ) )->get_placeholders();

$this->assertSame( $current->format( 'g:ia' ), $placeholders['{current_interval}'] );
$this->assertSame( $next->format( 'g:ia' ), $placeholders['{next_interval}'] );
}

/**
* @test
* @group Placeholders
Expand Down