I am writing a customized plugin for reservations. I have to generate an .ics file that must be despatched to a predefined e-mail handle.
I’ve tried some library like ZContent iCalendar and a PHP script that I discovered on github that I’ve included inside my plugin code. The script works properly and I can receive a legitimate output. The issue is I can not determine how one can generate the ics file and fasten it to an e-mail utilizing the wp_mail perform.
Right here is my code:
<?php
/**
* ICS.php
* =======
* Use this class to create an .ics file.
*
* Utilization
* -----
* Primary utilization - generate ics file contents (see beneath for accessible properties):
* $ics = new ICS($props);
* $ics_file_contents = $ics->to_string();
*
* Setting properties after instantiation
* $ics = new ICS();
* $ics->set('abstract', 'My superior occasion');
*
* You can too set a number of properties on the similar time by utilizing an array:
* $ics->set(array(
* 'dtstart' => 'now + half-hour',
* 'dtend' => 'now + 1 hour'
* ));
*
* Out there properties
* --------------------
* description
* String description of the occasion.
* dtend
* A date/time stamp designating the top of the occasion. You should utilize both a
* DateTime object or a PHP datetime format string (e.g. "now + 1 hour").
* dtstart
* A date/time stamp designating the beginning of the occasion. You should utilize both a
* DateTime object or a PHP datetime format string (e.g. "now + 1 hour").
* location
* String handle or description of the situation of the occasion.
* abstract
* String quick abstract of the occasion - often used because the title.
* url
* A url to connect to the the occasion. Make certain so as to add the protocol (http://
* or https://).
*/
class WP_ICS{
const DT_FORMAT = 'YmdTHisZ';
protected $properties = array();
personal $available_properties = array(
'description',
'dtend',
'dtstart',
'location',
'abstract',
'url'
);
public perform __construct($props) {
$this->set($props);
}
public perform set($key, $val = false) {
if (is_array($key)) {
foreach ($key as $ok => $v) {
$this->set($ok, $v);
}
} else {
if (in_array($key, $this->available_properties)) {
$this->properties[$key] = $this->sanitize_val($val, $key);
}
}
}
public perform to_string() {
$rows = $this->build_props();
return implode("rn", $rows);
}
personal perform build_props() {
// Construct ICS properties - add header
$ics_props = array(
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//hacksw/handcal//NONSGML v1.0//EN',
'CALSCALE:GREGORIAN',
'BEGIN:VEVENT'
);
// Construct ICS properties - add header
$props = array();
foreach($this->properties as $ok => $v) {
$props[strtoupper($k . ($k === 'url' ? ';VALUE=URI' : ''))] = $v;
}
// Set some default values
$props['DTSTAMP'] = $this->format_timestamp('now');
$props['UID'] = uniqid();
// Append properties
foreach ($props as $ok => $v) {
$ics_props[] = "$ok:$v";
}
// Construct ICS properties - add footer
$ics_props[] = 'END:VEVENT';
$ics_props[] = 'END:VCALENDAR';
return $ics_props;
}
personal perform sanitize_val($val, $key = false) {
swap($key) {
case 'dtend':
case 'dtstamp':
case 'dtstart':
$val = $this->format_timestamp($val);
break;
default:
$val = $this->escape_string($val);
}
return $val;
}
personal perform format_timestamp($timestamp) {
$dt = new DateTime($timestamp);
return $dt->format(self::DT_FORMAT);
}
personal perform escape_string($str) {
return preg_replace('/([,;])/','$1', $str);
}
}
class iCalBooking{
public perform init(){
add_action('admin_post_forward_booking_request', array($this, 'send_booking_request'));
add_action('admin_post_nopriv_forward_booking_request', array($this, 'send_booking_request'));
#add_action();
add_shortcode('reserving', array($this, 'display_booking_form'));
}
public perform display_booking_form(){
include_once 'booking-form.php';
}
personal perform has_valid_nonce() {
if(!isset( $_POST['booking_nonce'] )){
return false;
}
$area = wp_unslash( $_POST['booking_nonce'] );
$motion = 'validate_booking_request';
return wp_verify_nonce( $area, $motion );
}
public perform send_booking_request(){
if(! ( $this->has_valid_nonce() ) ){
return;
}
$fname = $_POST['client_fname'];
$lname = $_POST['client_lname'];
$cellphone = $_POS['client_phone'];
$e-mail = $_POST['client_email'];
$title = 'Prenotazione';
$description = $fname . $lname . $cellphone . $e-mail;
$ics = new WP_ICS(array(
'location' => '',
'description' => $description,
'dtstart' => $_POST['client_checkin'],
'dtend' => $_POST['client_checkout'],
'abstract' => '',
'url' => ''
));
file_put_contents('reservation.ics', $ics->to_string() );
#header('Content material-Sort: textual content/calendar; charset=utf-8');
#header('Content material-Disposition: attachment; filename=reservation.ics');
}
}
$reserving = new iCalBooking;
$booking->init();
?>