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

Fix waiting list check when max limit is 0 meaning it has no limit. #999

Merged
merged 4 commits into from
Jan 18, 2025
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
23 changes: 23 additions & 0 deletions includes/core/classes/class-rsvp-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ protected function __construct() {
*/
protected function setup_hooks(): void {
add_action( 'init', array( $this, 'register_taxonomy' ) );
add_action( 'wp_after_insert_post', array( $this, 'maybe_process_waiting_list' ) );

add_filter( 'get_comments_number', array( $this, 'adjust_comments_number' ), 10, 2 );
}

Expand Down Expand Up @@ -101,4 +103,25 @@ public function adjust_comments_number( int $comments_number, int $post_id ): in

return $comment_count['approved'];
}

/**
* Process the waiting list for an event after it has been saved.
*
* Checks if the saved post is an event and not an autosave,
* then processes any waiting list entries if applicable.
*
* @since 1.0.0
*
* @param int $post_id The ID of the post being saved.
* @return void
*/
public function maybe_process_waiting_list( int $post_id ): void {
if ( Event::POST_TYPE !== get_post_type( $post_id ) ) {
return;
}

$rsvp = new Rsvp( $post_id );

$rsvp->check_waiting_list();
}
}
20 changes: 15 additions & 5 deletions includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,28 @@ public function save( int $user_id, string $status, int $anonymous = 0, int $gue
* @return int The number of responses from the waiting list that were moved to attending.
*/
public function check_waiting_list(): int {
$responses = $this->responses();
$i = 0;
$responses = $this->responses();
$attending_count = intval( $responses['attending']['count'] );
$waiting_list_count = intval( $responses['waiting_list']['count'] );
$i = 0;

if (
intval( $responses['attending']['count'] ) < $this->max_attendance_limit
&& intval( $responses['waiting_list']['count'] )
$waiting_list_count &&
(
empty( $this->max_attendance_limit ) ||
$attending_count < $this->max_attendance_limit
)
) {
$waiting_list = $responses['waiting_list']['records'];

// People who are longest on the waiting_list should be added first.
usort( $waiting_list, array( $this, 'sort_by_timestamp' ) );

$total = $this->max_attendance_limit - intval( $responses['attending']['count'] );
if ( ! empty( $this->max_attendance_limit ) ) {
$total = $this->max_attendance_limit - intval( $responses['attending']['count'] );
} else {
$total = $waiting_list_count;
}

while ( $i < $total ) {
// Check that we have enough on the waiting_list to run this.
Expand All @@ -321,6 +330,7 @@ public function check_waiting_list(): int {
}

$response = $waiting_list[ $i ];

$this->save( $response['id'], 'attending', $response['anonymous'] );
++$i;
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit/php/includes/core/classes/class-test-rsvp-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function test_setup_hooks(): void {
'priority' => 10,
'callback' => array( $instance, 'register_taxonomy' ),
),
array(
'type' => 'action',
'name' => 'wp_after_insert_post',
'priority' => 10,
'callback' => array( $instance, 'maybe_process_waiting_list' ),
),
array(
'type' => 'filter',
'name' => 'get_comments_number',
Expand Down
Loading