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

Rsvp form token field #522

Merged
merged 25 commits into from
Feb 7, 2024
Merged

Conversation

jmarx
Copy link
Collaborator

@jmarx jmarx commented Jan 29, 2024

Description of the Change

Administrators can now add/remove attendees to events.

Closes #485

How to test the Change

Changelog Entry

Added - New feature

Credits

Props @linusx @jmarx @mauteri

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change(pending).
  • All new and existing tests pass(pending).

@jmarx
Copy link
Collaborator Author

jmarx commented Jan 29, 2024

@mauteri I am throwing this pr up here as this is as far as I got. It includes most of the heavy lifting we need to do here. I have a tight week and off to AZ for a week. So I figured maybe someone could work on it in case I can't get back to it until I return next week.

@@ -251,7 +251,7 @@ protected function events_list_route(): array {
* @return bool True if the parameter is a valid RSVP status, false otherwise.
*/
public function validate_rsvp_status( $param ): bool {
return ( 'attend' === $param || 'attending' === $param || 'not_attending' === $param );
return ( 'attend' === $param || 'attending' === $param || 'not_attending' === $param || 'remove' === $param );
Copy link
Contributor

@mauteri mauteri Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of remove when I was working on anonymous feature. I ended up just going with attend so we don't have to add another status. This can be confusing though for removal.

I think we can update things a bit and remove attend and remove and replace it with no_status. I think that word can be used in both cases without confusion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to update this #525

@@ -646,6 +647,10 @@ public function update_rsvp( WP_REST_Request $request ): WP_REST_Response {
}
}

if ( $status === 'remove') {
$event->rsvp->remove( $user_id, $responses );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be just $event->rsvp->remove( $user_id ); In the database user id and post id are the 2 unique fields to remove an RSVP. When the RSVP class is instantiated here, it is done so by an event, so you have that in the object that you can use. This will also be faster so you don't need to loop through the responses.

* @param int $user_id The ID of the user to remove the RSVP for.
* @param array $responses Array of RSVP responses containing the entry to remove.
*/
public function remove( int $user_id, array $responses ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above. Remove responses. Let's have this return boolean based on success.

public function remove( int $user_id ): bool {

*/
public function remove( int $user_id, array $responses ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return $responses;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false;

$result = $wpdb->delete( $table, array( 'user_id' => $value['id'] ) );
}
}
return $responses;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 143 to 150 can simply be something like...

global $wpdb;
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );

return $wpdb->delete( $table, array( 'post_id' => $this->event->ID, 'user_id' => $user_id ) );

Copy link
Contributor

@linusx linusx Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mauteri wpdb->delete will return an int on success, not boolean. Might have to check the $result if false, otherwise return true. Also, this is in a loop of responses. So adding a return in here will kill the loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@linusx Missed this response earlier. You don't need a loop as you have the post ID. With that and the user ID, you just add those to your where and delete.

If it returns 1 then we just need to force bool on that return then so it matches type.

(cc @jmarx)

<BlockControls>
<ToolbarGroup>
<ToolbarButton
label="Edit Attendees"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to just Edit. Make sure to add to translation function. {__('Edit', 'gatherpress')}. Super important we do this. We already have the plugin translated to 3 languages and we'll need to update when this feature goes out to ensure text is translated.

label="Edit Attendees"
text={
editMode
? 'Preview Attendees'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Preview. Add translation function.

text={
editMode
? 'Preview Attendees'
: 'Edit Attendees'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Edit. Add translation function.

if (!items.some((item) => item.value === rsvpStatus)) {
setRsvpStatus(defaultStatus);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this file got messed up. These changes should remain. Think something happened when you updated from main after I pushed code. Looks like the file can just be reverted to what's on main.

@jmarx jmarx requested a review from mauteri February 5, 2024 22:46
* @param array $responses Array of RSVP responses containing the entry to remove.
* @return bool True if the RSVP was removed, false otherwise.
*/
public function remove( int $user_id, array $responses ): bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to pass $responses just pass $user_id. The only 2 pieces of data you need to remove someone is their user ID and the post ID. The object knows the post ID.

@@ -620,6 +620,7 @@ public function update_rsvp( WP_REST_Request $request ): WP_REST_Response {
$guests = intval( $params['guests'] );
$anonymous = intval( $params['anonymous'] );
$event = new Event( $post_id );
$responses = $event->rsvp->responses();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need $responses can remove this.

@@ -641,7 +642,7 @@ public function update_rsvp( WP_REST_Request $request ): WP_REST_Response {
if (
$user_id &&
is_user_member_of_blog( $user_id ) &&
! $event->has_event_past()
! $event->has_event_past() && ( 'remove' !== $status )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change remove to no_status

@@ -650,6 +651,10 @@ public function update_rsvp( WP_REST_Request $request ): WP_REST_Response {
}
}

if ( 'remove' !== $status ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change remove to no_status

$result = $wpdb->delete( $table, array( 'user_id' => $value['id'] ) );
}
}
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines 144-151 can just be

global $wpdb;
$table  = sprintf( static::TABLE_FORMAT, $wpdb->prefix );

return $wpdb->delete( $table, array( 'post_id' => $this->event->ID, 'user_id' => $user_id ) );

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mauteri I just removed the remove function and wrapped it all in with the save function.

$result = $wpdb->delete( $table, array( 'user_id' => $value['id'] ) );
}
}
return $responses;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@linusx Missed this response earlier. You don't need a loop as you have the post ID. With that and the user ID, you just add those to your where and delete.

If it returns 1 then we just need to force bool on that return then so it matches type.

(cc @jmarx)

@mauteri mauteri marked this pull request as ready for review February 7, 2024 01:34
@mauteri mauteri changed the title WIP: Rsvp form token field Rsvp form token field Feb 7, 2024
@mauteri mauteri merged commit 6e5022b into GatherPress:main Feb 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to change attendees in RSVP Response block
3 participants