Skip to content

Commit

Permalink
Updated how guests are handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Feb 26, 2024
1 parent f8236ee commit 0ec0c12
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build/blocks/events-list/events-list.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '544ac5b97eb81e966fc0');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => 'aaadde62898a94c068b8');
2 changes: 1 addition & 1 deletion build/blocks/events-list/events-list.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/blocks/events-list/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '35e2c4d2f2ca7b3e6127');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'be8fa887ef84e3cc047a');
2 changes: 1 addition & 1 deletion build/blocks/events-list/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/blocks/rsvp/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '88c85e55e18e2f6ad31d');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'acb0e6d45ce6a75cf18c');
2 changes: 1 addition & 1 deletion build/blocks/rsvp/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/blocks/rsvp/rsvp.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '191c05a2d94775548970');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '1b32d1d9a264c82b2758');
2 changes: 1 addition & 1 deletion build/blocks/rsvp/rsvp.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion includes/core/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,14 @@ public function update_rsvp( WP_REST_Request $request ): WP_REST_Response {
}
}

// Guests could have changed, so we need to get guests from what was saved in the record and use it below.
// @todo good to look this over, maybe we get all the record data back from rsvp->save rather than just status.
$record = $event->rsvp->get( $user_id ) ?? [];
$response = array(
'event_id' => $post_id,
'success' => $success,
'status' => $status,
'guests' => $guests,
'guests' => $record['guests'] ?? 0,
'anonymous' => $anonymous,
'responses' => $event->rsvp->responses(),
'online_link' => $event->maybe_get_online_event_link(),
Expand Down
48 changes: 21 additions & 27 deletions includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public function get( int $user_id ): array {
public function save( int $user_id, string $status, int $anonymous = 0, int $guests = 0 ): string {
global $wpdb;

$data = array(
'post_id' => 0,
'user_id' => 0,
'timestamp' => '0000-00-00 00:00:00',
'status' => 'no_status',
'guests' => 0,
'anonymous' => 0,
);

$post_id = $this->event->ID;
$updated_status = '';

Expand All @@ -156,8 +165,16 @@ public function save( int $user_id, string $status, int $anonymous = 0, int $gue
$current_response = $this->get( $user_id );
$limit_reached = $this->attending_limit_reached( $current_response, $guests );

if ( in_array( $status, array( 'attending', 'waiting_list' ), true ) ) {
$status = ! $limit_reached ? 'attending' : 'waiting_list';
if ( 'attending' === $status && $limit_reached ) {
$guests = $current_response['guests'];
}

if ( 'attending' !== $current_response['status'] && $limit_reached ) {
$status = 'waiting_list';
}

if ( 'waiting_list' === $status ) {
$guests = 0;
}

$data = array(
Expand Down Expand Up @@ -221,9 +238,6 @@ public function check_waiting_list(): int {
// People who are longest on the waiting_list should be added first.
usort( $waiting_list, array( $this, 'sort_by_timestamp' ) );

// People with most guests are put at bottom of waiting list.
usort( $waiting_list, array( $this, 'sort_by_guests' ) );

$total = $this->max_attending_limit - intval( $responses['attending']['count'] );

while ( $i < $total ) {
Expand All @@ -233,7 +247,7 @@ public function check_waiting_list(): int {
}

$response = $waiting_list[ $i ];
$this->save( $response['id'], 'attending', $response['anonymous'], $response['guests'] );
$this->save( $response['id'], 'attending', $response['anonymous'] );
++$i;
}
}
Expand Down Expand Up @@ -451,26 +465,6 @@ static function ( $role ) {
* @return bool True if the first response's timestamp is earlier than the second response's timestamp; otherwise, false.
*/
public function sort_by_timestamp( array $first, array $second ): bool {
return ( strtotime( $first['timestamp'] ) < strtotime( $second['timestamp'] ) );
}

/**
* Sorts array elements based on the number of guests.
*
* This method compares two array elements by the number of guests specified in each,
* allowing for sorting of an array based on guest counts. It's designed to be used
* with PHP's usort() function or similar array sorting functions that require a
* comparison function. The method returns true if the first element should be
* considered less than the second (i.e., has fewer guests), making it suitable for
* use in ascending order sorts.
*
* @since 1.0.0
*
* @param array $first The first array element to compare, expected to have a 'guests' key with an integer value.
* @param array $second The second array element to compare, also expected to have a 'guests' key with an integer value.
* @return bool True if the first element has fewer guests than the second, false otherwise.
*/
public function sort_by_guests( array $first, array $second ): bool {
return ( intval( $first['guests'] ) < intval( $second['guests'] ) );
return ( strtotime( $first['timestamp'] ) > strtotime( $second['timestamp'] ) );
}
}
12 changes: 7 additions & 5 deletions src/components/Rsvp.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ const Rsvp = ({ postId, currentUser = '', type, enableAnonymousRsvp }) => {
)
)}
</div>
{!rsvpAnonymous && 'not_attending' !== rsvpStatus && (
{!rsvpAnonymous && 'attending' === rsvpStatus && (
<div className="gp-modal__guests">
<label htmlFor="gp-guests">
{__('Number of guests?', 'gatherpress')}
Expand All @@ -263,15 +263,17 @@ const Rsvp = ({ postId, currentUser = '', type, enableAnonymousRsvp }) => {
type="number"
min="0"
max="5"
onChange={(e) =>
onChange={(e) => {
const value = Number(e.target.value);
setRsvpGuests(value);
onAnchorClick(
e,
rsvpStatus,
rsvpAnonymous,
Number(e.target.value),
value,
false
)
}
);
}}
defaultValue={rsvpGuests}
/>
</div>
Expand Down
40 changes: 2 additions & 38 deletions test/unit/php/includes/core/classes/class-test-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,49 +314,13 @@ public function test_sort_by_timestamp(): void {
$newer = array( 'timestamp' => '2023-05-11 08:30:00' );
$older = array( 'timestamp' => '2022-05-11 08:30:00' );

$this->assertFalse(
$this->assertTrue(
$rsvp->sort_by_timestamp( $newer, $older ),
'Failed to assert correct sorting of timestamp.'
);
$this->assertTrue(
$this->assertFalse(
$rsvp->sort_by_timestamp( $older, $newer ),
'Failed to assert correct sorting of timestamp.'
);
}

/**
* Coverage for sort_by_guests method.
*
* @covers ::sort_by_guests
*
* @return void
*/
public function test_sort_by_guests(): void {
$post = $this->mock->post(
array(
'post_type' => 'gp_event',
)
)->get();
$rsvp = new Rsvp( $post->ID );
$guests_0 = array( 'guests' => 0 );
$guests_1 = array( 'guests' => 1 );
$guests_2 = array( 'guests' => 2 );

$this->assertTrue(
$rsvp->sort_by_guests( $guests_0, $guests_1 ),
'Failed to assert correct sorting of guests.'
);
$this->assertTrue(
$rsvp->sort_by_guests( $guests_1, $guests_2 ),
'Failed to assert correct sorting of guests.'
);
$this->assertTrue(
$rsvp->sort_by_guests( $guests_0, $guests_2 ),
'Failed to assert correct sorting of guests.'
);
$this->assertFalse(
$rsvp->sort_by_guests( $guests_2, $guests_0 ),
'Failed to assert correct sorting of guests.'
);
}
}

0 comments on commit 0ec0c12

Please sign in to comment.