From b5899de2b7965eb0b05967e48a37f1d6dd0c0965 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 23 Dec 2024 17:46:20 +0100 Subject: [PATCH 1/3] feat(Seating) include Seating information in Attendee REST API response --- changelog/SL-264 | 4 + src/Tickets/Seating/Controller.php | 1 + src/Tickets/Seating/REST.php | 92 ++++++++++++++++++ .../REST/V1/Endpoints/Attendee_Archive.php | 12 ++- tests/slr_integration/REST_Test.php | 95 +++++++++++++++++++ 5 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 changelog/SL-264 create mode 100644 src/Tickets/Seating/REST.php create mode 100644 tests/slr_integration/REST_Test.php diff --git a/changelog/SL-264 b/changelog/SL-264 new file mode 100644 index 0000000000..53ff3e0990 --- /dev/null +++ b/changelog/SL-264 @@ -0,0 +1,4 @@ +Significance: patch +Type: feat + +Include Seating information in Attendee archive REST API response. diff --git a/src/Tickets/Seating/Controller.php b/src/Tickets/Seating/Controller.php index adb1a90936..10c59c4abd 100644 --- a/src/Tickets/Seating/Controller.php +++ b/src/Tickets/Seating/Controller.php @@ -159,6 +159,7 @@ function () { } $this->container->register( QR::class ); + $this->container->register( REST::class ); $this->container->register( Health::class ); } diff --git a/src/Tickets/Seating/REST.php b/src/Tickets/Seating/REST.php new file mode 100644 index 0000000000..c2393dea06 --- /dev/null +++ b/src/Tickets/Seating/REST.php @@ -0,0 +1,92 @@ + $data The Attendee archcive REST API response data. + * + * @return array The Attendee archcive REST API response data modified to include ASC related data. + */ + public function inject_attendee_data( $data ) { + if ( ! is_array( $data ) ) { + return $data; + } + + if ( ! tribe( 'tickets.rest-v1.main' )->request_has_manage_access() ) { + return $data; + } + + + $attendees = $data['attendees'] ?? []; + + if ( ! is_array( $attendees ) ) { + return $data; + } + + array_walk( $attendees, function ( &$attendee ) { + if ( ! ( is_array( $attendee ) && isset( $attendee['id'], $attendee['ticket_id'] ) ) ) { + return; + } + + // Let's check the ticket of this Attendee is an ASC one. + $ticket_id = $attendee['ticket_id']; + $uses_assigned_seating = get_post_meta( $ticket_id, Meta::META_KEY_ENABLED, true ); + + if ( ! $uses_assigned_seating ) { + return; + } + + $seat_label = get_post_meta( $attendee['id'], Meta::META_KEY_ATTENDEE_SEAT_LABEL, true ); + $attendee['asc_ticket'] = true; + $attendee['seat_label'] = $seat_label ?: ''; + } ); + + $data['attendees'] = $attendees; + + return $data; + } +} diff --git a/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php b/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php index 5f0fdbf966..537f5a59d7 100644 --- a/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php +++ b/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php @@ -188,7 +188,17 @@ public function get( WP_REST_Request $request ) { 'X-ET-TOTAL-PAGES' => $data['total_pages'], ]; - return new WP_REST_Response( $data, 200, $headers ); + /** + * Filters the data in the Attendee Archive REST response. + * + * @since TBD + * + * @param array $data The data in the Attendee Archive REST response. + * @param WP_REST_Request $request The request object for this endpoint. + */ + $data = apply_filters( 'tec_tickets_rest_attendee_archive_data', $data, $request ); + + return new WP_REST_Response( $data, 200, $headers ); } /** diff --git a/tests/slr_integration/REST_Test.php b/tests/slr_integration/REST_Test.php new file mode 100644 index 0000000000..e5cf889aff --- /dev/null +++ b/tests/slr_integration/REST_Test.php @@ -0,0 +1,95 @@ +post->create(); + $ticket_1 = $this->create_tc_ticket( $non_asc_post, 10 ); + $attendee_1 = $this->create_attendee_for_ticket( $ticket_1, $non_asc_post ); + $asc_post = self::factory()->post->create(); + update_post_meta( $asc_post, Meta::META_KEY_ENABLED, true ); + update_post_meta( $asc_post, Meta::META_KEY_LAYOUT_ID, 'layout-uuid-1' ); + $ticket_2 = $this->create_tc_ticket( $asc_post, 20 ); + update_post_meta( $ticket_2, Meta::META_KEY_SEAT_TYPE, 'seat-type-uuid-1' ); + update_post_meta( $ticket_2, Meta::META_KEY_ENABLED, true ); + [ $attendee_2, $attendee_3 ] = $this->create_many_attendees_for_ticket( 2, $ticket_2, $asc_post ); + update_post_meta( $attendee_2, Meta::META_KEY_SEAT_TYPE, 'seat-type-uuid-1' ); + update_post_meta( $attendee_2, Meta::META_KEY_RESERVATION_ID, 'reservation-uuid-1' ); + update_post_meta( $attendee_2, Meta::META_KEY_ATTENDEE_SEAT_LABEL, 'A-23' ); + update_post_meta( $attendee_3, Meta::META_KEY_SEAT_TYPE, 'seat-type-uuid-1' ); + update_post_meta( $attendee_3, Meta::META_KEY_RESERVATION_ID, 'reservation-uuid-2' ); + delete_post_meta( $attendee_3, Meta::META_KEY_ATTENDEE_SEAT_LABEL ); + $controller = $this->make_controller(); + + // Attendee data is not an array. + $this->assertEquals( 'foo', $controller->inject_attendee_data( 'foo' ) ); + + // Attendee data is an empty array. + $this->assertEquals( [], $controller->inject_attendee_data( [] ) ); + + $good_attendee_1_data = [ + 'id' => $attendee_1, + 'ticket_id' => $ticket_1, + ]; + + // User is has not manage access: data is not modified. + wp_set_current_user( 0 ); + $this->assertEquals( + [ 'attendees' => [ $good_attendee_1_data ] ], + $controller->inject_attendee_data( [ 'attendees' => [ $good_attendee_1_data ] ] ) + ); + + // Set to a user that could edit them. + wp_set_current_user( static::factory()->user->create( [ 'role' => 'administrator' ] ) ); + + // Attendee data is not an array. + $this->assertEquals( + [ 'attendees' => 'not-an-array' ], + $controller->inject_attendee_data( [ 'attendees' => 'not-an-array' ] ) + ); + + // Attendee data is an array, but attendees are not. + $this->assertEquals( + [ 'attendees' => [ 'not-an-array', 'another-thing' ] ], + $controller->inject_attendee_data( [ 'attendees' => [ 'not-an-array', 'another-thing' ] ] ) + ); + + // Attendee data is an array, but attendee is missing id. + $this->assertEquals( + [ 'attendees' => [ [ 'ticket_id' => $ticket_2 ] ] ], + $controller->inject_attendee_data( [ 'attendees' => [ [ 'ticket_id' => $ticket_2 ] ] ] ) + ); + + // Attendee data is an array, but attendee is missing ticket_id. + $this->assertEquals( + [ 'attendees' => [ [ 'id' => $attendee_2 ] ] ], + $controller->inject_attendee_data( [ 'attendees' => [ [ 'id' => $attendee_2 ] ] ] ) + ); + + // Attendee data is correct. + $this->assertEquals( + [ + 'attendees' => [ + [ 'id' => $attendee_2, 'ticket_id' => $ticket_2, 'asc_ticket' => true, 'seat_label' => 'A-23' ], + [ 'id' => $attendee_3, 'ticket_id' => $ticket_2, 'asc_ticket' => true, 'seat_label' => '' ] + ] + ], + $controller->inject_attendee_data( [ + 'attendees' => [ + [ 'id' => $attendee_2, 'ticket_id' => $ticket_2 ], + [ 'id' => $attendee_3, 'ticket_id' => $ticket_2 ] + ] + ] ) + ); + } +} From 05ce8dffe8bae30c862b044314eda41cf650e909 Mon Sep 17 00:00:00 2001 From: Camwyn Date: Wed, 8 Jan 2025 14:35:49 -0500 Subject: [PATCH 2/3] phpcs --- src/Tickets/Seating/REST.php | 39 ++++++++++--------- .../REST/V1/Endpoints/Attendee_Archive.php | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Tickets/Seating/REST.php b/src/Tickets/Seating/REST.php index c2393dea06..ec78d38d61 100644 --- a/src/Tickets/Seating/REST.php +++ b/src/Tickets/Seating/REST.php @@ -18,8 +18,7 @@ * * @package TEC\Tickets\Seating; */ -class REST extends Controller_Contract{ - +class REST extends Controller_Contract { /** * Subscribes the controller to relevant hooks, binds implementations. * @@ -47,9 +46,9 @@ public function unregister(): void { * * @since TBD * - * @param array $data The Attendee archcive REST API response data. + * @param array $data The Attendee archive REST API response data. * - * @return array The Attendee archcive REST API response data modified to include ASC related data. + * @return array The Attendee archive REST API response data modified to include ASC related data. */ public function inject_attendee_data( $data ) { if ( ! is_array( $data ) ) { @@ -60,30 +59,32 @@ public function inject_attendee_data( $data ) { return $data; } - $attendees = $data['attendees'] ?? []; if ( ! is_array( $attendees ) ) { return $data; } - array_walk( $attendees, function ( &$attendee ) { - if ( ! ( is_array( $attendee ) && isset( $attendee['id'], $attendee['ticket_id'] ) ) ) { - return; - } + array_walk( + $attendees, + function ( &$attendee ) { + if ( ! ( is_array( $attendee ) && isset( $attendee['id'], $attendee['ticket_id'] ) ) ) { + return; + } - // Let's check the ticket of this Attendee is an ASC one. - $ticket_id = $attendee['ticket_id']; - $uses_assigned_seating = get_post_meta( $ticket_id, Meta::META_KEY_ENABLED, true ); + // Let's check the ticket of this Attendee is an ASC one. + $ticket_id = $attendee['ticket_id']; + $uses_assigned_seating = get_post_meta( $ticket_id, Meta::META_KEY_ENABLED, true ); - if ( ! $uses_assigned_seating ) { - return; - } + if ( ! $uses_assigned_seating ) { + return; + } - $seat_label = get_post_meta( $attendee['id'], Meta::META_KEY_ATTENDEE_SEAT_LABEL, true ); - $attendee['asc_ticket'] = true; - $attendee['seat_label'] = $seat_label ?: ''; - } ); + $seat_label = get_post_meta( $attendee['id'], Meta::META_KEY_ATTENDEE_SEAT_LABEL, true ); + $attendee['asc_ticket'] = true; + $attendee['seat_label'] = $seat_label ?: ''; + } + ); $data['attendees'] = $attendees; diff --git a/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php b/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php index 537f5a59d7..c4d74398a9 100644 --- a/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php +++ b/src/Tribe/REST/V1/Endpoints/Attendee_Archive.php @@ -198,7 +198,7 @@ public function get( WP_REST_Request $request ) { */ $data = apply_filters( 'tec_tickets_rest_attendee_archive_data', $data, $request ); - return new WP_REST_Response( $data, 200, $headers ); + return new WP_REST_Response( $data, 200, $headers ); } /** From e7c4a89b690c928c7bddc47596acb3078c8aa3bc Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 9 Jan 2025 15:42:21 +0100 Subject: [PATCH 3/3] test(slr_integration) correctly name test case class --- tests/slr_integration/REST_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/slr_integration/REST_Test.php b/tests/slr_integration/REST_Test.php index e5cf889aff..79179f64fd 100644 --- a/tests/slr_integration/REST_Test.php +++ b/tests/slr_integration/REST_Test.php @@ -6,7 +6,7 @@ use Tribe\Tickets\Test\Commerce\Attendee_Maker; use Tribe\Tickets\Test\Commerce\TicketsCommerce\Ticket_Maker; -class QR_Test extends Controller_Test_Case { +class REST_Test extends Controller_Test_Case { use Ticket_Maker; use Attendee_Maker;