Files
wrapartamenty.pl/wp-content/plugins/mphb-ical-sync/includes/Exporter.php
Jacek Pyziak c49d3b39d4 Add MPHB iCal Sync plugin for automatic iCal synchronization and booking management
- Implement SyncCron class for scheduling iCal sync every 15 minutes.
- Create main plugin file with initialization and activation hooks.
- Add admin booking modal for managing reservations, including AJAX functionality for searching available rooms and creating bookings.
- Include necessary CSS and JS for modal functionality and user interaction.
- Ensure compatibility with MotoPress Hotel Booking plugin.
2026-03-02 18:14:50 +01:00

104 lines
2.7 KiB
PHP

<?php
namespace MphbIcalSync;
/**
* Generuje plik VCALENDAR z potwierdzonymi rezerwacjami danego pokoju.
*/
class Exporter {
public function export( int $roomId ): string {
$events = $this->getEventsForRoom( $roomId );
return $this->buildIcal( $roomId, $events );
}
private function getEventsForRoom( int $roomId ): array {
// Znajdź wszystkie mphb_reserved_room powiązane z tym pokojem
$reservedRoomIds = get_posts( [
'post_type' => 'mphb_reserved_room',
'meta_key' => '_mphb_room_id',
'meta_value' => $roomId,
'fields' => 'ids',
'numberposts' => -1,
'post_status' => 'any',
'no_found_rows' => true,
] );
if ( empty( $reservedRoomIds ) ) {
return [];
}
// Zbierz unikalne booking IDs
$bookingIds = [];
foreach ( $reservedRoomIds as $reservedRoomId ) {
$bookingId = wp_get_post_parent_id( $reservedRoomId );
if ( $bookingId && ! in_array( $bookingId, $bookingIds, true ) ) {
$bookingIds[] = $bookingId;
}
}
$host = parse_url( home_url(), PHP_URL_HOST );
$events = [];
foreach ( $bookingIds as $bookingId ) {
$booking = get_post( $bookingId );
if ( ! $booking || $booking->post_status !== 'confirmed' ) {
continue;
}
$checkIn = get_post_meta( $bookingId, 'mphb_check_in_date', true );
$checkOut = get_post_meta( $bookingId, 'mphb_check_out_date', true );
if ( ! $checkIn || ! $checkOut ) {
continue;
}
$events[] = [
'uid' => $bookingId . '@' . $host,
'dtstart' => $checkIn,
'dtend' => $checkOut,
'summary' => 'Zarezerwowane',
];
}
return $events;
}
private function buildIcal( int $roomId, array $events ): string {
$now = gmdate( 'Ymd\THis\Z' );
$lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//wrapartamenty.pl//MPHB iCal Sync ' . MPHB_ICAL_SYNC_VERSION . '//PL',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'X-WR-CALNAME:Room ' . $roomId,
];
foreach ( $events as $event ) {
$dtstart = str_replace( '-', '', $event['dtstart'] );
$dtend = str_replace( '-', '', $event['dtend'] );
$lines[] = 'BEGIN:VEVENT';
$lines[] = 'UID:' . $event['uid'];
$lines[] = 'DTSTAMP:' . $now;
$lines[] = 'DTSTART;VALUE=DATE:' . $dtstart;
$lines[] = 'DTEND;VALUE=DATE:' . $dtend;
$lines[] = 'SUMMARY:' . $this->escapeIcalText( $event['summary'] );
$lines[] = 'END:VEVENT';
}
$lines[] = 'END:VCALENDAR';
return implode( "\r\n", $lines ) . "\r\n";
}
private function escapeIcalText( string $text ): string {
$text = str_replace( '\\', '\\\\', $text );
$text = str_replace( ';', '\;', $text );
$text = str_replace( ',', '\,', $text );
$text = str_replace( "\n", '\\n', $text );
return $text;
}
}