answer( $event_id, $event_name ); } /** * Work out the answer. * * @param int|null $event_id Event ID. * @param string|null $event_name Event title. * @return array */ private function answer( ?int $event_id, ?string $event_name ): array { $check = $this->guard( 'etn_manage_event' ); if ( $check ) { return $check; } $events_page = $this->page( __( 'Events', 'aisentic' ), '/events' ); if ( ( null === $event_id || $event_id <= 0 ) && '' === trim( (string) $event_name ) ) { return $this->unclear( 'No event was named.', $events_page ); } list( $event_id, $title, $stop ) = $this->resolve_event( $event_id, $event_name, $events_page ); if ( null !== $stop ) { return $stop; } try { $tickets = $this->tickets( $event_id ); $booked = $this->query->count_records( 'attendee', [ 'event_id' => $event_id, 'status' => 'success', ], 'ticket' ); $bookings = $this->query->count_records( 'booking', [ 'event_id' => $event_id ], 'status' ); foreach ( $tickets as &$ticket ) { $ticket['booked'] = $booked['groups'][ $ticket['name'] ] ?? 0; } unset( $ticket ); $revenue = $this->query->event_revenue( $event_id ); return [ 'status' => 'ok', 'event' => $this->details( $event_id, $title ), 'tickets' => $tickets, 'booked_seats' => $booked['total'], 'bookings' => [ 'total' => $bookings['total'], 'by_status' => $bookings['groups'], ], 'revenue' => null === $revenue ? [ 'status' => 'not_available', 'say' => 'Revenue cannot be read on this site. Say so and offer to open the event page. Never estimate it from ticket prices.', ] : [ 'net' => round( $revenue, 2 ), 'currency' => function_exists( 'etn_currency' ) ? (string) etn_currency() : '', 'note' => 'Completed bookings after refunds, as on the Eventin dashboard.', ], 'page' => $this->event_page( $event_id, $title ), ]; } catch ( \Throwable $e ) { Log::error( 'Eventin_Event_Summary_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Could not read the event: ' . $e->getMessage() ]; } } /** * Basic details of the event. * * @param int $event_id Event ID. * @param string $title Event title. * @return array */ private function details( int $event_id, string $title ): array { $location = get_post_meta( $event_id, 'etn_event_location', true ); if ( is_array( $location ) ) { $location = (string) ( $location['address'] ?? ( $location['custom_url'] ?? '' ) ); } return [ 'id' => $event_id, 'title' => $title, 'post_status' => get_post_status( $event_id ), 'start_date' => (string) get_post_meta( $event_id, 'etn_start_date', true ), 'end_date' => (string) get_post_meta( $event_id, 'etn_end_date', true ), 'start_time' => (string) get_post_meta( $event_id, 'etn_start_time', true ), 'end_time' => (string) get_post_meta( $event_id, 'etn_end_time', true ), 'timezone' => (string) get_post_meta( $event_id, 'event_timezone', true ), 'event_type' => (string) get_post_meta( $event_id, 'event_type', true ), 'location' => is_string( $location ) ? $location : '', ]; } /** * Ticket types of the event. * * @param int $event_id Event ID. * @return array */ private function tickets( int $event_id ): array { $variations = get_post_meta( $event_id, 'etn_ticket_variations', true ); $tickets = []; foreach ( is_array( $variations ) ? $variations : [] as $variation ) { if ( ! is_array( $variation ) ) { continue; } // Eventin spells the key "avaiilable". Capacity is null when unlimited // (-1 or the unlimited flag) or not set (0 or empty), never "0". $capacity = (int) ( $variation['etn_avaiilable_tickets'] ?? 0 ); $unlimited = ! empty( $variation['etn_unlimited_tickets'] ) || -1 === $capacity; $tickets[] = [ 'name' => (string) ( $variation['etn_ticket_name'] ?? '' ), 'price' => (float) ( $variation['etn_ticket_price'] ?? 0 ), 'capacity' => ( $unlimited || $capacity <= 0 ) ? null : $capacity, 'unlimited' => $unlimited, ]; } return $tickets; } }