wpdb = $wpdb; } /** * Fetch Amelia bookings linked to an IvyForms entry. * * @return array */ public function getBookingsByEntryId(int $entryId): array { $prefix = $this->wpdb->prefix; $customerBookingsTable = $prefix . 'amelia_customer_bookings'; $packagesToCustomersTable = $prefix . 'amelia_packages_to_customers'; if ( !$this->tableExists($customerBookingsTable) || !$this->tableExists($packagesToCustomersTable) ) { return []; } $query = $this->wpdb->prepare( "SELECT appointmentId, id, NULL AS purchaseId FROM {$customerBookingsTable} WHERE ivyEntryId = %d UNION ALL SELECT NULL AS appointmentId, NULL AS id, id AS purchaseId FROM {$packagesToCustomersTable} WHERE ivyEntryId = %d", $entryId, $entryId ); $rows = $this->wpdb->get_results($query, ARRAY_A); return is_array($rows) ? $rows : []; } private function tableExists(string $tableName): bool { $like = $this->wpdb->esc_like($tableName); $result = $this->wpdb->get_var( $this->wpdb->prepare('SHOW TABLES LIKE %s', $like) ); return $result === $tableName; } }