Last active
August 29, 2015 14:05
-
-
Save stephenharris/7a79d65e2b9fbf22fc6b to your computer and use it in GitHub Desktop.
Attach an iCal 'invite' to booking confirmation. Please note this snippet is for when you are selling tickets by **series** or you only have non-recurring events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Please note this snippet is for when you are selling tickets by **series** or you only have non-recurring events. | |
* This is because the attached iCal file will detail all dates of the event booked for. | |
* | |
* If you are selling tickets by date, then you should change the get_ical() method so that it returns an iCal feed, which details | |
* only the event (date) that has been booked | |
* | |
* | |
* ### Instructions ### | |
* This snippet should go in a dedicated plug-in. | |
* Alternatively, it will work in your theme's functions.php. | |
*/ | |
class Myprefix_iCal_Email_Attacher{ | |
protected $booking_id = false; | |
function __construct(){ | |
add_filter( 'eventorganiser_booking_confirmed_email_attachments', array( $this, 'setup' ), 10, 2 ); | |
} | |
function setup( $attachments, $booking_id ){ | |
$this->booking_id = $booking_id; | |
add_action( 'phpmailer_init', array( $this, 'attach_ical' ), 10 ); | |
add_action( 'phpmailer_init', array( $this, 'cleanup' ), 11 ); | |
return $attachments; | |
} | |
function attach_ical( $phpmailer ){ | |
if( $this->booking_id ){ | |
$event_id = eo_get_booking_meta( $this->booking_id , 'event_id' ); | |
$title = esc_attr( get_the_title( $event_id ) ); | |
$filename = sanitize_file_name( $title.'-'.$this->booking_id.'.ics' ); | |
$ical_string = $this->get_ical( $event_id ); | |
$phpmailer->AddStringAttachment( $ical_string, $filename ); | |
$this->booking_id = false; | |
} | |
} | |
function get_ical( $event_id ){ | |
//Generate ical string for this event. | |
query_posts( array( 'post_type' => 'event', 'p' => $event_id, 'group_events_by' => 'series' ) ); | |
ob_start(); | |
eo_locate_template( 'ical.php', true );; | |
$string = ob_get_contents(); | |
ob_end_clean(); | |
wp_reset_query(); | |
return $string; | |
} | |
function cleanup(){ | |
remove_action( 'phpmailer_init', array( $this, 'attach_ical' ), 10 ); | |
} | |
} | |
$ical_attacher = new Myprefix_iCal_Email_Attacher(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment