From 6821835294a05d4ac366d8056eabe90ddce36423 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 18 Feb 2025 22:05:32 +0100 Subject: [PATCH] View/Booking: Adds an null/assoc check for results of the commonsbooking_booking_filter Adds also an WP_DEBUG logging entry --- src/View/Booking.php | 29 +++++++++++++++++++++++++-- tests/php/View/BookingTest.php | 36 +++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/View/Booking.php b/src/View/Booking.php index c1af07f15..0978ae6f5 100755 --- a/src/View/Booking.php +++ b/src/View/Booking.php @@ -230,8 +230,33 @@ public static function getBookingListData( int $postsPerPage = 6, \WP_User $user // If search term was submitted, filter for it. if ( ! $search || count( preg_grep( '/.*' . $search . '.*/i', $rowData ) ) > 0 ) { - $rowData['actions'] = $actions; - $bookingDataArray['data'][] = apply_filters( 'commonsbooking_booking_filter', $rowData, $booking ); + $rowData['actions'] = $actions; + + /** + * Default assoc array of row data and the booking object, which gets added to the booking list data result. + * + * See $rowData in this function, for the valid keys. + * + * @since 2.7.3 + * + * @param array $rowData assoc array of one row booking data + * @param \CommonsBooking\Model\Booking $booking booking model of one row booking data + */ + $filteredRowData = apply_filters( 'commonsbooking_booking_filter', $rowData, $booking ); + + // Only includes valid row data objects + if ( isset( $filteredRowData ) && is_array( $filteredRowData ) ) { + if ( WP_DEBUG ) { + // Logs absent keys, relative to the original row data keys, could cause problems + $absentKeys = array_diff_key( $filteredRowData, $rowData ); + if ( count( $absentKeys ) > 0 ) { + error_log( 'After commonsbooking_booking_filter: Filtered rows have missing keys: ' . join( ',', array_keys( $absentKeys ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log + } + } + } else { + continue; + } + $bookingDataArray['data'][] = $filteredRowData; } } diff --git a/tests/php/View/BookingTest.php b/tests/php/View/BookingTest.php index 2c38f1523..9f9072584 100644 --- a/tests/php/View/BookingTest.php +++ b/tests/php/View/BookingTest.php @@ -7,9 +7,11 @@ final class BookingTest extends CustomPostTypeTest { + private int $bookingId; + protected function setUp(): void { parent::setUp(); - $this->createBooking( + $this->bookingId = $this->createBooking( $this->locationId, $this->itemId, time() - 86400, @@ -26,4 +28,36 @@ public function testGetBookingListData() { $bookings = Booking::getBookingListData(); $this->assertTrue( $bookings['total'] == 1 ); } + + public function testGetBookingListData_withNullFilter() { + wp_set_current_user( self::USER_ID ); + + $nullCallback = function ( $rowData, $bookingObject ) { + return NULL; + }; + + add_filter( 'commonsbooking_booking_filter', $nullCallback ); + + $bookings = Booking::getBookingListData(); + $this->assertTrue( $bookings['total'] == 0 ); + + remove_filter( 'commonsbooking_booking_filter', $nullCallback ); + } + + public function testGetBookingListData_withSimpleFilter() { + wp_set_current_user( self::USER_ID ); + + $simpleFilter = function ( $rowData, $bookingObject ) { + return $rowData['postID'] === $this->bookingId; + }; + + add_filter( 'commonsbooking_booking_filter', $simpleFilter ); + + $bookings = Booking::getBookingListData(); + $this->assertTrue( $bookings['total'] == 1 ); + + remove_filter( 'commonsbooking_booking_filter', $simpleFilter ); + } + + }