Skip to content

Commit

Permalink
WIP ical feed rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Sep 15, 2024
1 parent e72d25c commit bc2b246
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 41 deletions.
80 changes: 59 additions & 21 deletions includes/templates/endpoints/ical-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,71 @@
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Calendars;
use GatherPress\Core\Event;

// Start collecting all output.
ob_start();

// Prepare event data.
$gatherpress_event = new Event( get_queried_object_id() );
$gatherpress_filename = $gatherpress_event->get_datetime_start( 'Y-m-d' ) . '_' . $gatherpress_event->event->post_name . '.ics';
// Output the event as an iCalendar (.ics) file.
function gatherpress_output_ics_file() {
// Start output buffering to capture all output.
ob_start();

// Send file headers.
header( 'Content-Description: .ics for ' . $gatherpress_event->event->post_title );
header( 'Content-Disposition: inline; filename=' . $gatherpress_filename );
header( 'Content-type: text/calendar; charset=' . strtolower( get_option( 'blog_charset' ) ) . ';' );
header( 'Pragma: 0' );
header( 'Expires: 0' );
// Get the event and prepare the filename.
$event = new Event( get_queried_object_id() );
$filename = gatherpress_generate_ics_filename( $event );

// Generate ical.
echo wp_kses_post( $gatherpress_event->get_ics_calendar_download() );
// Send headers for downloading the .ics file.
gatherpress_send_ics_headers( $filename );

// Get collected output and render it.
$gatherpress_ics_file = ob_get_contents();
// Output the generated iCalendar content.
echo wp_kses_post( Calendars::get_ics_calendar_download() );

// Calculate and send the file size.
$gatherpress_filesize = strlen( $gatherpress_ics_file );
header( 'Content-Length: ' . $gatherpress_filesize );
// Get the generated output and calculate file size.
$ics_content = ob_get_contents();
$filesize = strlen( $ics_content );

ob_end_clean();
echo wp_kses_post( $gatherpress_ics_file );
// Send the file size in the header.
header( 'Content-Length: ' . $filesize );

exit();
// End output buffering and clean up.
ob_end_clean();

// Output the iCalendar content.
echo wp_kses_post( $ics_content );

exit(); // Terminate the script after the file has been output.
}

// Generate the .ics filename based on the event date and name.
function gatherpress_generate_ics_filename( Event $event ) {
$date = $event->get_datetime_start( 'Y-m-d' );
$post_name = $event->event->post_name;
return $date . '_' . $post_name . '.ics';
}

// Send the necessary headers for the iCalendar file download.
function gatherpress_send_ics_headers( $filename ) {

$charset = strtolower( get_option( 'blog_charset' ) );

// Content description
header( 'Content-Description: File Transfer' );

// Ensure proper content type for the calendar file
header( 'Content-Type: text/calendar; charset=' . $charset );

// Force download in most browsers while keeping inline for compatibility.
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

// Disable caching to avoid browser caching issues.
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

// Prevent content sniffing which might lead to MIME type mismatch.
header( 'X-Content-Type-Options: nosniff' );
}

// Call the function to output the .ics file.
gatherpress_output_ics_file();
83 changes: 63 additions & 20 deletions includes/templates/endpoints/ical-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,73 @@
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

// Start collecting all output.
ob_start();
use GatherPress\Core\Calendars;
use GatherPress\Core\Event;

// Prepare event data.
$gatherpress_filename = 'all-events.ics';

// Send file headers.
header( 'Content-Description: .ics for all events' );
header( 'Content-Disposition: attachment; filename=' . $gatherpress_filename );
header( 'Content-type: text/calendar; charset=' . get_option( 'blog_charset' ) . ';' );
header( 'Pragma: 0' );
header( 'Expires: 0' );
// Output the event as an iCalendar (.ics) file.
function gatherpress_output_ics_file() {
// Start output buffering to capture all output.
ob_start();

// Generate ical.
echo wp_kses_post( 'hello subscribeable ical feed world.' );
// Get the event and prepare the filename.
// $event = new Event( get_queried_object_id() );
// $filename = gatherpress_generate_ics_filename( $event );
$filename = 'calendar.ics';

// Get collected output and render it.
$gatherpress_ics_file = ob_get_contents();
// Send headers for downloading the .ics file.
gatherpress_send_ics_headers( $filename );

// Calculate and send the file size.
$gatherpress_filesize = strlen( $gatherpress_ics_file );
header( 'Content-Length: ' . $gatherpress_filesize );
// Output the generated iCalendar content.
echo wp_kses_post( Calendars::get_ics_calendar_feed() );

ob_end_clean();
echo wp_kses_post( $gatherpress_ics_file );
// Get the generated output and calculate file size.
$ics_content = ob_get_contents();
$filesize = strlen( $ics_content );

exit();
// Send the file size in the header.
header( 'Content-Length: ' . $filesize );

// End output buffering and clean up.
ob_end_clean();

// Output the iCalendar content.
echo wp_kses_post( $ics_content );

exit(); // Terminate the script after the file has been output.
}

// Generate the .ics filename based on the event date and name.
/*
function gatherpress_generate_ics_filename( Event $event ) {
$date = $event->get_datetime_start( 'Y-m-d' );
$post_name = $event->event->post_name;
return $date . '_' . $post_name . '.ics';
} */

// Send the necessary headers for the iCalendar file download.
function gatherpress_send_ics_headers( $filename ) {

$charset = strtolower( get_option( 'blog_charset' ) );

// Content description
header( 'Content-Description: File Transfer' );

// Ensure proper content type for the calendar file
header( 'Content-Type: text/calendar; charset=' . $charset );

// Force download in most browsers while keeping inline for compatibility.
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

// Disable caching to avoid browser caching issues.
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

// Prevent content sniffing which might lead to MIME type mismatch.
header( 'X-Content-Type-Options: nosniff' );
}

// Call the function to output the .ics file.
gatherpress_output_ics_file();

0 comments on commit bc2b246

Please sign in to comment.