Skip to content

Commit

Permalink
View/Booking: Adds an null/assoc check for results of the commonsbook…
Browse files Browse the repository at this point in the history
…ing_booking_filter

Adds also an WP_DEBUG logging entry
  • Loading branch information
datengraben committed Feb 19, 2025
1 parent f68f3d6 commit 6821835
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/View/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
36 changes: 35 additions & 1 deletion tests/php/View/BookingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );
}


}

0 comments on commit 6821835

Please sign in to comment.