From 76184e83fa18e92fd6b86fd012335ec7c7bbbd53 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 17 May 2023 12:09:34 +0200 Subject: bugfix: Return null if gig is not found by id. Also add some error logging if more than one concert is returned for the same id. Should never happen, but in case it will be logged. --- includes/class-giglogadmin-concert.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/includes/class-giglogadmin-concert.php b/includes/class-giglogadmin-concert.php index c6223cb..a4513db 100644 --- a/includes/class-giglogadmin-concert.php +++ b/includes/class-giglogadmin-concert.php @@ -82,7 +82,20 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { * @return null|self */ public static function get( int $id ) : ?self { - return self::find_concerts( array( 'id' => $id ) )[0]; + $gigs = self::find_concerts( array( 'id' => $id ) ); + $count = count( $gigs ); + + if ( $count > 1 ) { + error_log( + "Found more than one concert with the same id ({$id})." + . ' This should not happen!' + ); + wp_die( 'Oops! Something went wrong', 500 ); + } else if ( $count != 1 ) { + return null; + } else { + return array_shift( $gigs ); + } } /** -- cgit v1.2.3