Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into release/2.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmorb committed May 24, 2024
2 parents 095db4c + b9779db commit 3cfdf04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/Service/iCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ public static function getCurrentUserCalendarLink() {
);
}

/**
* Get the ics file for an existing booking. Will be called, when the "Add to Calendar" button on the booking page is pressed
*
* @param $bookingID
*
* @return void
* @throws Exception
*/
public static function downloadICS( $bookingID ): void {
$postID = $bookingID;
$booking = new Booking( $postID );
$template_objects = [
'booking' => $booking,
'item' => $booking->getItem(),
'location' => $booking->getLocation(),
'user' => $booking->getUserData(),
];

$eventTitle = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-title' );
$eventTitle = commonsbooking_sanitizeHTML( commonsbooking_parse_template( $eventTitle, $template_objects ) );
$eventDescription = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-description' );
$eventDescription = commonsbooking_sanitizeHTML( strip_tags( commonsbooking_parse_template( $eventDescription, $template_objects ) ) );
$calendar = $booking->getiCal( $eventTitle, $eventDescription );
header( 'Content-Type: text/calendar; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="booking.ics"' );
echo $calendar;
}

/**
* Adds Model\Booking to Calendar.
* This will take all the information like title, description, location, start and end date and add it to the calendar as an event.
Expand Down
36 changes: 12 additions & 24 deletions src/Wordpress/CustomPostType/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ public static function handleBookingRequest(
?string $postType,
int $overbookedDays = 0
): int {

if ( isset ( $_POST['calendar-download'] ) ) {
try {
iCalendar::downloadICS( $post_ID );
} catch ( Exception $e ) {
//redirect to booking page and do nothing
return $post_ID;
}
exit;
}

if ( $itemId === null || ! get_post( $itemId ) ) {
// translators: $s = id of the item
throw new BookingDeniedException( sprintf( __( 'Item does not exist. (%s)', 'commonsbooking' ), $itemId ) );
Expand Down Expand Up @@ -346,30 +357,7 @@ public static function handleBookingRequest(
PHP_EOL . $postId->get_error_messages()
);
}
elseif (
function_exists( 'wp_verify_nonce' ) &&
isset( $_REQUEST[ static::getWPNonceId() ] ) &&
wp_verify_nonce( $_REQUEST[ static::getWPNonceId() ], static::getWPAction() ) &&
isset ( $_POST['calendar-download'] )
){
$postID = intval($_POST['post_ID']);
$booking = New \CommonsBooking\Model\Booking( $postID );
$template_objects = [
'booking' => $booking,
'item' => $booking->getItem(),
'location' => $booking->getLocation(),
'user' => $booking->getUserData(),
];
$eventTitle = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-title' );
$eventTitle = commonsbooking_sanitizeHTML ( commonsbooking_parse_template ( $eventTitle, $template_objects ) );
$eventDescription = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-description' );
$eventDescription = commonsbooking_sanitizeHTML ( strip_tags ( commonsbooking_parse_template ( $eventDescription, $template_objects ) ) );
$calendar = $booking->getiCal($eventTitle,$eventDescription);
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="booking.ics"');
echo $calendar;
exit;
}

return $postId;
}

Expand Down

0 comments on commit 3cfdf04

Please sign in to comment.