Skip to content

Commit

Permalink
DRY out sending the ics to the browser (for files and feeds)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Sep 16, 2024
1 parent cdea924 commit bacb2e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 67 deletions.
37 changes: 36 additions & 1 deletion includes/core/classes/class-calendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public static function generate_ics_filename() {
}
}

return $file_name . '.ics';
return $filename . '.ics';
}

/**
Expand Down Expand Up @@ -711,6 +711,41 @@ public static function send_ics_headers( string $filename ) {
header( 'X-Content-Type-Options: nosniff' );
}

/**
* Output the event(s) as an iCalendar (.ics) file.
*
* @return void
*/
public static function send_ics_file() {
// Start output buffering to capture all output.
ob_start();

// Prepare the filename.
$filename = Calendars::generate_ics_filename();

// Send headers for downloading the .ics file.
Calendars::send_ics_headers( $filename );

// Output the generated iCalendar content.
$get_ical_method = ( is_feed() ) ? 'get_ical_feed' : 'get_ical_file';
echo wp_kses_post( Calendars::$get_ical_method() );

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

// 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.
}

/**
* @author Stephen Harris (@stephenharris)
* @source https://github.com/stephenharris/Event-Organiser/blob/develop/includes/event-organiser-utility-functions.php#L1663
Expand Down
34 changes: 1 addition & 33 deletions includes/templates/endpoints/ical-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,6 @@
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

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


// Output the event as an iCalendar (.ics) file.
function gatherpress_output_ics_file() {
// Start output buffering to capture all output.
ob_start();

// Prepare the filename.
$filename = Calendars::generate_ics_filename();

// Send headers for downloading the .ics file.
Calendars::send_ics_headers( $filename );

// Output the generated iCalendar content.
echo wp_kses_post( Calendars::get_ical_file() );

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

// 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.
}

// Call the function to output the .ics file.
gatherpress_output_ics_file();
Calendars::send_ics_file();
34 changes: 1 addition & 33 deletions includes/templates/endpoints/ical-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,6 @@
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

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


// Output the event as an iCalendar (.ics) file.
function gatherpress_output_ics_file() {
// Start output buffering to capture all output.
ob_start();

// Prepare the filename.
$filename = Calendars::generate_ics_filename();

// Send headers for downloading the .ics file.
Calendars::send_ics_headers( $filename );

// Output the generated iCalendar content.
echo wp_kses_post( Calendars::get_ical_feed() );

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

// 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.
}

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

0 comments on commit bacb2e9

Please sign in to comment.