Skip to content

Commit

Permalink
Added offset to event started and event past methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 12, 2023
1 parent ec6f4f0 commit 650c0f7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
30 changes: 22 additions & 8 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,18 @@ public function is_same_date(): bool {
*
* @since 1.0.0
*
* @param int $offset The time offset, in minutes, to adjust the consideration of the event end time.
* A positive value extends the period of considering the event ongoing,
* while a negative value checks for an earlier end.
* Default is 0, checking if the event is ongoing at the exact current time.
* @return bool True if the event is in the future, false otherwise.
*/
public function has_event_started(): bool {
public function has_event_started( int $offset = 0 ): bool {
$data = $this->get_datetime();
$start = $data['datetime_start_gmt'];
$current = time();

return ( ! empty( $start ) && $current >= strtotime( $start ) );
return ( ! empty( $start ) && $current >= ( strtotime( $start ) + ( $offset * 60 ) ) );
}

/**
Expand All @@ -302,14 +306,17 @@ public function has_event_started(): bool {
*
* @since 1.0.0
*
* @param int $offset The time offset, in minutes, to adjust the consideration of the event start time.
* A positive value delays the event start, while a negative value checks for an earlier start.
* Default is 0, checking if the event has started at the exact current time.
* @return bool True if the event is in the past, false otherwise.
*/
public function has_event_past(): bool {
public function has_event_past( int $offset = 0 ): bool {
$data = $this->get_datetime();
$end = $data['datetime_end_gmt'];
$current = time();
$current = time() - ( $offset * 60 );

return ( ! empty( $end ) && $current > strtotime( $end ) );
return ( ! empty( $end ) && $current > ( strtotime( $end ) + ( $offset * 60 ) ) );
}

/**
Expand All @@ -319,10 +326,17 @@ public function has_event_past(): bool {
*
* @since 1.0.0
*
* @param int $started_offset The time offset, in minutes, to adjust the consideration of the event start time.
* A positive value delays the event start, while a negative value checks for an earlier start.
* Default is 0, checking if the event has started at the exact current time.
* @param int $past_offset The time offset, in minutes, to adjust the consideration of the event end time.
* A positive value extends the period of considering the event ongoing,
* while a negative value checks for an earlier end.
* Default is 0, checking if the event is ongoing at the exact current time.
* @return bool True if the event has started and is not in the past, false otherwise.
*/
public function is_event_happening(): bool {
return $this->has_event_started() && ! $this->has_event_past();
public function is_event_happening( int $started_offset = 0, int $past_offset = 0 ): bool {
return $this->has_event_started( $started_offset ) && ! $this->has_event_past( $past_offset );
}

/**
Expand Down Expand Up @@ -923,7 +937,7 @@ public function maybe_get_online_event_link(): string {
if (
! isset( $user['status'] ) ||
'attending' !== $user['status'] ||
! $this->is_event_happening()
! $this->is_event_happening( -5 )
) {
return '';
}
Expand Down
79 changes: 74 additions & 5 deletions test/unit/php/includes/core/classes/class-test-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,37 @@ public function test_has_event_started(): void {

$output = $event->has_event_started();

$this->assertTrue( $output );
$this->assertTrue(
$output,
'Failed to assert that event has started.'
);

$start = new DateTime( 'now' );
$end = new DateTime( 'now' );

$start->modify( '+2 minutes' );
$end->modify( '+2 hours' );

$params = array(
'datetime_start' => $start->format( Event::DATETIME_FORMAT ),
'datetime_end' => $end->format( Event::DATETIME_FORMAT ),
);

$event->save_datetimes( $params );

$output = $event->has_event_started();

$this->assertFalse(
$output,
'Failed to assert that event has not started.'
);

$output = $event->has_event_started( -3 );

$this->assertTrue(
$output,
'Failed to assert that event has started with offset.'
);

$start = new DateTime( 'now' );
$end = new DateTime( 'now' );
Expand All @@ -617,7 +647,10 @@ public function test_has_event_started(): void {

$output = $event->has_event_started();

$this->assertFalse( $output );
$this->assertFalse(
$output,
'Failed to assert that event has not started.'
);
}

/**
Expand Down Expand Up @@ -649,7 +682,10 @@ public function test_has_event_past(): void {

$output = $event->has_event_past();

$this->assertTrue( $output );
$this->assertTrue(
$output,
'Failed to assert that event has past.'
);

$start = new DateTime( 'now' );
$end = new DateTime( 'now' );
Expand All @@ -667,6 +703,33 @@ public function test_has_event_past(): void {
$output = $event->has_event_past();

$this->assertFalse( $output );

$start = new DateTime( 'now' );
$end = new DateTime( 'now' );

$start->modify( '-1 hour' );
$end->modify( '-1 minute' );

$params = array(
'datetime_start' => $start->format( Event::DATETIME_FORMAT ),
'datetime_end' => $end->format( Event::DATETIME_FORMAT ),
);

$event->save_datetimes( $params );

$output = $event->has_event_past();

$this->assertTrue(
$output,
'Failed to assert that event has past.'
);

$output = $event->has_event_past( 5 );

$this->assertFalse(
$output,
'Failed to assert that event has not past with offset.'
);
}

/**
Expand Down Expand Up @@ -697,7 +760,10 @@ public function test_is_event_happening(): void {

$output = $event->is_event_happening();

$this->assertTrue( $output );
$this->assertTrue(
$output,
'Failed to assert that event is happening'
);

$start = new DateTime( 'now' );
$end = new DateTime( 'now' );
Expand All @@ -714,7 +780,10 @@ public function test_is_event_happening(): void {

$output = $event->is_event_happening();

$this->assertFalse( $output );
$this->assertFalse(
$output,
'Failed to assert event is not happening.'
);
}

/**
Expand Down

0 comments on commit 650c0f7

Please sign in to comment.