Skip to content

Commit

Permalink
Merge pull request #698 from GatherPress/fix-max-attendance
Browse files Browse the repository at this point in the history
Max attendance setting not working
  • Loading branch information
mauteri authored Jun 21, 2024
2 parents df842b0 + a87a67f commit d8eda14
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build/panels.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => '251cae0a24819d959b98');
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => '24014ba7345244e47c2e');
2 changes: 1 addition & 1 deletion build/panels.js

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions includes/core/classes/class-event-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function setup_hooks(): void {
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'register_post_meta' ) );
add_action( 'delete_post', array( $this, 'delete_event' ) );
add_action( sprintf( 'save_post_%s', Event::POST_TYPE ), array( $this, 'check_waiting_list' ) );
add_action(
sprintf( 'manage_%s_posts_custom_column', Event::POST_TYPE ),
array( $this, 'custom_columns' ),
Expand Down Expand Up @@ -160,10 +161,10 @@ public function register_post_meta(): void {
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
},
'sanitize_callback' => 'sanitize_text_field',
'sanitize_callback' => 'absint',
'show_in_rest' => true,
'single' => true,
'type' => 'number',
'type' => 'integer',
),
'gatherpress_enable_anonymous_rsvp' => array(
'auth_callback' => function () {
Expand Down Expand Up @@ -212,6 +213,23 @@ public function register_post_meta(): void {
}
}

/**
* Checks and updates the waiting list for the given event.
*
* This function initializes an RSVP object for the given post ID
* and checks the waiting list associated with that post.
*
* @since 1.0.0
*
* @param int $post_id The ID of the post for which the waiting list should be checked.
* @return void
*/
public function check_waiting_list( int $post_id ): void {
$rsvp = new Rsvp( $post_id );

$rsvp->check_waiting_list();
}

/**
* Delete event record from custom table when an event is deleted.
*
Expand Down
16 changes: 10 additions & 6 deletions includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Rsvp {
* @since 1.0.0
* @var int Represents the maximum number of attendees allowed for an event.
*/
protected int $max_attending_limit;
protected int $max_attendance_limit;

/**
* The event post object associated with this RSVP instance.
Expand All @@ -81,8 +81,8 @@ class Rsvp {
* @param int $post_id The event post ID.
*/
public function __construct( int $post_id ) {
$this->event = get_post( $post_id );
$this->max_attending_limit = Settings::get_instance()->get_value( 'general', 'general', 'max_attending_limit' );
$this->event = get_post( $post_id );
$this->max_attendance_limit = intval( get_post_meta( $post_id, 'gatherpress_max_attendance_limit', true ) );
}

/**
Expand Down Expand Up @@ -247,15 +247,15 @@ public function check_waiting_list(): int {
$i = 0;

if (
intval( $responses['attending']['count'] ) < $this->max_attending_limit
intval( $responses['attending']['count'] ) < $this->max_attendance_limit
&& intval( $responses['waiting_list']['count'] )
) {
$waiting_list = $responses['waiting_list']['responses'];

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

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

while ( $i < $total ) {
// Check that we have enough on the waiting_list to run this.
Expand Down Expand Up @@ -295,6 +295,10 @@ public function attending_limit_reached( array $current_response, int $guests =
$responses = $this->responses();
$user_count = 1;

if ( empty( $this->max_attendance_limit ) ) {
return false;
}

// If the user record was previously attending adjust numbers to figure out new limit.
if ( 'attending' === $current_response['status'] ) {
$guests = $guests - intval( $current_response['guests'] );
Expand All @@ -303,7 +307,7 @@ public function attending_limit_reached( array $current_response, int $guests =

if (
! empty( $responses['attending'] ) &&
intval( $responses['attending']['count'] ) + $user_count + $guests > $this->max_attending_limit
intval( $responses['attending']['count'] ) + $user_count + $guests > $this->max_attendance_limit
) {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions includes/core/classes/settings/class-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ protected function get_sections(): array {
),
),
),
'max_attending_limit' => array(
'max_attendance_limit' => array(
'labels' => array(
'name' => __( 'Maximum Attending Limit', 'gatherpress' ),
'name' => __( 'Maximum Attendance Limit', 'gatherpress' ),
),
'description' => __( 'Set this as your default, but you can still override it for each event as you like.', 'gatherpress' ),
'description' => __( 'Set this as your default, but you can still override it for each event as you like. If you set it to 0, there will not be any limit.', 'gatherpress' ),
'field' => array(
'label' => __( 'The default maximum limit of attendees to an event.', 'gatherpress' ),
'type' => 'number',
Expand Down
10 changes: 5 additions & 5 deletions src/components/GuestLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { getFromGlobal } from '../helpers/globals';

/**
* AnonymousRsvp component.
* GuestLimit component.
*
* This component renders a checkbox control that allows toggling the anonymous RSVP feature for an event.
* This component renders a number input control that allows setting the maximum number of guests for an event.
* It handles the state and updates the post's metadata accordingly. When creating a new event, the default
* state of the checkbox is determined by a global setting. For existing events, it uses the event's current
* value of the input is determined by a global setting. For existing events, it uses the event's current
* setting. The component ensures that changes are reflected in the post's metadata and also unlocks post saving.
*
* @return {JSX.Element} A checkbox control for enabling or disabling anonymous RSVPs.
* @return {JSX.Element} A number input control for setting the maximum number of guests.
*/
const GuestLimit = () => {
const { editPost, unlockPostSaving } = useDispatch('core/editor');
Expand Down Expand Up @@ -57,7 +57,7 @@ const GuestLimit = () => {
);

useEffect(() => {
if (isNewEvent && defaultGuestLimit !== 0) {
if (isNewEvent && 0 !== defaultGuestLimit) {
updateGuestLimit(defaultGuestLimit);
}
}, [isNewEvent, defaultGuestLimit, updateGuestLimit]);
Expand Down
42 changes: 27 additions & 15 deletions src/components/MaxAttendanceLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* WordPress dependencies.
*/
import { __ } from '@wordpress/i18n';
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
import { __experimentalNumberControl as NumberControl } from '@wordpress/components';
import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalNumberControl as NumberControl,
} from '@wordpress/components';
import { useState, useEffect, useCallback } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';

Expand All @@ -28,7 +30,6 @@ const MaxAttendanceLimit = () => {
return select('core/editor').isCleanNewPost();
}, []);

// eslint-disable-next-line no-shadow
let defaultMaxAttendanceLimit = useSelect((select) => {
return select('core/editor').getEditedPostAttribute('meta')
.gatherpress_max_attendance_limit;
Expand All @@ -40,34 +41,45 @@ const MaxAttendanceLimit = () => {
);
}

if (false === defaultMaxAttendanceLimit) {
defaultMaxAttendanceLimit = 0;
}

const [maxAttendanceLimit, setMaxAttendanceLimit] = useState(
defaultMaxAttendanceLimit
);

const updateMaxAttendanceLimit = useCallback(
(value) => {
const meta = { max_attendance: Number(value) };
const meta = { gatherpress_max_attendance_limit: Number(value) };

setMaxAttendanceLimit(value);
editPost({ meta });
unlockPostSaving(); // Call `unlockPostSaving` here to unlock the save button after updating the meta
unlockPostSaving();
},
[editPost, unlockPostSaving]
);

useEffect(() => {
setMaxAttendanceLimit(defaultMaxAttendanceLimit);
}, [defaultMaxAttendanceLimit]);
if (isNewEvent && 0 !== defaultMaxAttendanceLimit) {
updateMaxAttendanceLimit(defaultMaxAttendanceLimit);
}
}, [isNewEvent, defaultMaxAttendanceLimit, updateMaxAttendanceLimit]);

return (
<NumberControl
label={__('Maximum Attendance Limit', 'gatherpress')}
value={maxAttendanceLimit ?? 50}
min={0}
onChange={(value) => {
updateMaxAttendanceLimit(value);
}}
/>
<>
<NumberControl
label={__('Maximum Attendance Limit', 'gatherpress')}
value={maxAttendanceLimit}
min={0}
onChange={(value) => {
updateMaxAttendanceLimit(value);
}}
/>
<p className="description">
{__('A value of 0 indicates no limit.', 'gatherpress')}
</p>
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function test_get_members(): void {
$event = new Event( $event_id );
$members = $instance->get_members( $send, $event_id );

Utility::set_and_get_hidden_property( $event->rsvp, 'max_attending_limit', 2 );
Utility::set_and_get_hidden_property( $event->rsvp, 'max_attendance_limit', 2 );

$this->assertEmpty( $members );

Expand Down
8 changes: 4 additions & 4 deletions test/unit/php/includes/core/classes/class-test-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function test_save(): void {

$rsvp = new Rsvp( $post->ID );

Utility::set_and_get_hidden_property( $rsvp, 'max_attending_limit', 1 );
Utility::set_and_get_hidden_property( $rsvp, 'max_attendance_limit', 1 );

$user_1_id = $this->factory->user->create();
$user_2_id = $this->factory->user->create();
Expand Down Expand Up @@ -128,7 +128,7 @@ public function test_check_waiting_list(): void {

$this->assertEquals( 0, $rsvp->check_waiting_list(), 'Failed to assert expected waiting list value.' );

Utility::set_and_get_hidden_property( $rsvp, 'max_attending_limit', 2 );
Utility::set_and_get_hidden_property( $rsvp, 'max_attendance_limit', 2 );

$user_1_id = $this->factory->user->create();
$user_2_id = $this->factory->user->create();
Expand Down Expand Up @@ -156,7 +156,7 @@ public function test_check_waiting_list(): void {

$rsvp->save( $user_1_id, 'attending' );

Utility::set_and_get_hidden_property( $rsvp, 'max_attending_limit', 5 );
Utility::set_and_get_hidden_property( $rsvp, 'max_attendance_limit', 5 );

$this->assertEquals( 2, $rsvp->check_waiting_list(), 'Failed to assert expected waiting list value.' );

Expand All @@ -181,7 +181,7 @@ public function test_attending_limit_reached(): void {
)->get();
$rsvp = new Rsvp( $post->ID );

Utility::set_and_get_hidden_property( $rsvp, 'max_attending_limit', 1 );
Utility::set_and_get_hidden_property( $rsvp, 'max_attendance_limit', 1 );

$current_response = array(
'status' => 'waiting_list',
Expand Down

0 comments on commit d8eda14

Please sign in to comment.