add_field_verify ($wpdb->prefix.$this->form_table, 'attachical'); $cp_appb_plugin->add_field_verify ($wpdb->prefix.$this->form_table, 'base_summary'); $cp_appb_plugin->add_field_verify ($wpdb->prefix.$this->form_table, 'base_description'); // Insertion in database if( isset( $_REQUEST[ 'CPAPPB_icalexport_id' ] ) ) { $wpdb->delete( $wpdb->prefix.$this->form_table, array( 'formid' => $form_id ), array( '%d' ) ); $wpdb->insert( $wpdb->prefix.$this->form_table, array( 'formid' => $form_id, 'observe_day_light' => $_REQUEST["observe_day_light"], 'ical_daylight_zone' => $_REQUEST["ical_daylight_zone"], 'cal_time_zone_modify' => $_REQUEST["cal_time_zone_modify"], 'attachical' => $_REQUEST["attachical"], 'base_summary' => $_REQUEST["base_summary"], 'base_description' => $_REQUEST["base_description"] ), array( '%d', '%s', '%s', '%s', '%s', '%s', '%s' ) ); } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix.$this->form_table." WHERE formid=%d", $form_id ) ); if (!count($rows)) { $row["observe_day_light"] = "true"; $row["ical_daylight_zone"] = "EUROPE"; $row["cal_time_zone_modify"] = ''; $row["attachical"] = ''; $row["base_summary"] = 'Booking for %email%'; $row["base_description"] = 'Booking for %email%'; } else { $row["observe_day_light"] = $rows[0]->observe_day_light; $row["ical_daylight_zone"] = $rows[0]->ical_daylight_zone; $row["cal_time_zone_modify"] = $rows[0]->cal_time_zone_modify; $row["attachical"] = $rows[0]->attachical; $row["base_summary"] = $rows[0]->base_summary; $row["base_description"] = $rows[0]->base_description; } ?>

name; ?>

iCal link:
getiCalLink($form_id); ?>

To export the iCal link with Google Calendar on a regular basis, please read the instructions on this Google page:

https://support.google.com/calendar/answer/37100?hl=en

This will automatically export the bookings stored in the plugin to the Google Calendar (one way sync).



Note: Based in your computer timezone the recommended setting for this field is hours

* Important note: The folder "/wp-content/uploads/" must exist and have enough permissions to generate the iCal file to be attached.

* Note: You can get the field IDs/tags from the form builder.

* Note: You can get the field IDs/tags from the form builder.
description = __("The add-on adds support for exporting iCal files.", 'appointment-hour-booking' ); // Check if the plugin is active if( !$this->addon_is_active() ) return; add_action( 'init', array( &$this, 'pp_iCalExport_update_status' ), 10, 1 ); add_filter( 'cpappb_email_attachments', array( &$this, 'attach_ical_file' ), 10, 3 ); $this->update_database(); $passcode = get_option('CPAHB_PASSCODE',""); if ($passcode == '') { $passcode = openssl_random_pseudo_bytes(10); update_option( 'CPAHB_PASSCODE', $passcode); } } // End __construct /** * Create the database tables */ protected function update_database() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix.$this->form_table." ( id mediumint(9) NOT NULL AUTO_INCREMENT, formid INT NOT NULL, cal_time_zone_modify varchar(255) DEFAULT '' NOT NULL , observe_day_light varchar(255) DEFAULT '' NOT NULL , ical_daylight_zone varchar(255) DEFAULT '' NOT NULL , attachical varchar(10) DEFAULT '' NOT NULL , base_summary TEXT DEFAULT '' NOT NULL , base_description TEXT DEFAULT '' NOT NULL , UNIQUE KEY id (id) ) $charset_collate;"; $wpdb->query($sql); } // end update_database /************************ PRIVATE METHODS *****************************/ private function getiCalLink($form_id) { global $cp_appb_plugin; return $cp_appb_plugin->get_site_url()."?cpappb_app=calfeed&id=".$form_id."&verify=".substr(md5($form_id.get_option('CPAHB_PASSCODE',"")),0,10); } /** * mark the item as paid */ private function _log($adarray = array()) { $h = fopen( __DIR__.'/logs.txt', 'a' ); $log = ""; foreach( $_REQUEST as $KEY => $VAL ) { $log .= $KEY.": ".$VAL."\n"; } foreach( $adarray as $KEY => $VAL ) { $log .= $KEY.": ".$VAL."\n"; } $log .= "================================================\n"; fwrite( $h, $log ); fclose( $h ); } public function pp_iCalExport_update_status( ) { global $wpdb, $cp_appb_plugin; if (!isset($_GET["cpappb_app"]) || $_GET["cpappb_app"] != 'calfeed') return; if (isset($_GET["id"]) && isset($_GET["verify"]) && substr(md5($_GET["id"].get_option('CPAHB_PASSCODE',"")),0,10) == $_GET["verify"]) $this->export_iCal($_GET["id"]); else echo 'Access denied - verify value is not correct.'; exit (); } private function export_iCal( $form_id, $date_from = 'today -1 month', $date_to = 'today +10 years') { global $wpdb, $cp_appb_plugin; header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=events".date("Y-M-D_H.i.s").".ics"); echo "BEGIN:VCALENDAR\n"; echo "PRODID:-//CodePeople//Appointment Hour Booking for WordPress//EN\n"; echo "VERSION:2.0\n"; $icalSettings = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix.$this->form_table." WHERE formid=%d", $form_id ) ); $from = date("Y-m-d",strtotime($date_from)); $to = date("Y-m-d",strtotime($date_to)); $rows = $wpdb->get_results( $wpdb->prepare("SELECT id,time,notifyto,posted_data,data,ipaddr FROM ".$wpdb->prefix.$cp_appb_plugin->table_messages." WHERE ".($form_id?'formid='.intval($form_id).' AND ':'')."time<=%s ORDER BY time DESC LIMIT 0,1000", $to) ); foreach($rows as $item) if ($item->ipaddr != 'remote-ical-file') { $data = unserialize($item->posted_data); $ct = 0; foreach($data["apps"] as $app) if ($app["date"] >= $from && $app["date"] <= $to) { $ct++; $time = explode("/", $app["slot"]); $datetime = $app["date"]." ".trim($time[0]); $duration = "+".$app["duration"]." minutes"; $submissiontime = strtotime($item->time); if ($icalSettings[0]->observe_day_light) { $full_date = gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify)); $year = substr($full_date,0,4); if (strtoupper($icalSettings[0]->ical_daylight_zone) == 'EUROPE') { $dst_start = strtotime('last Sunday GMT', strtotime("1 April $year GMT")); $dst_stop = strtotime('last Sunday GMT', strtotime("1 November $year GMT")); } else { // USA $dst_start = strtotime('first Sunday GMT', strtotime("1 April $year GMT")); $dst_stop = strtotime('last Sunday GMT', strtotime("1 November $year GMT")); } if ($full_date >= gmdate("Ymd",$dst_start) && $full_date < gmdate("Ymd",$dst_stop)) $datetime = date("Y-m-d H:i",strtotime($datetime." -1 hour")); } $base_summary = $icalSettings[0]->base_summary; $base_description = $icalSettings[0]->base_description; $base_summary = str_replace ('<%', '%', $base_summary); $base_description = str_replace ('<%', '%', $base_description); $base_summary = str_replace ('%>', '%', $base_summary); $base_description = str_replace ('%>', '%', $base_description); $base_summary = str_replace('%apps%', $cp_appb_plugin->get_appointments_text($data["apps"]), $base_summary); $base_description = str_replace('%apps%', $cp_appb_plugin->get_appointments_text($data["apps"]), $base_description); foreach ($data as $itemgt => $value) { $base_summary = str_replace('%'.$itemgt.'%',(is_array($value)?(implode(", ",$value)):($value)),$base_summary); $base_description = str_replace('%'.$itemgt.'%',(is_array($value)?(implode(", ",$value)):($value)),$base_description); } $base_summary = str_replace("
",'\n',str_replace("
",'\n',str_replace("\r",'',str_replace("\n",'\n',$base_summary)) )); $base_description = str_replace("
",'\n',str_replace("
",'\n',str_replace("\r",'',str_replace("\n",'\n',$base_description)) )); echo "BEGIN:VEVENT\n"; echo "DTSTART:".gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify))."T".gmdate("His",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify))."Z\n"; echo "DTEND:".gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify.$duration))."T".gmdate("His",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify.$duration))."Z\n"; echo "DTSTAMP:".gmdate("Ymd",$submissiontime)."T".gmdate("His",$submissiontime)."Z\n"; echo "UID:uid".$item->id.'_'.$ct."@".$_SERVER["SERVER_NAME"]."\n"; echo "DESCRIPTION:".$base_description."\n"; echo "LAST-MODIFIED:".gmdate("Ymd",$submissiontime)."T".gmdate("His",$submissiontime)."Z\n"; echo "LOCATION:\n"; echo "SEQUENCE:0\n"; echo "STATUS:CONFIRMED\n"; echo "SUMMARY:".$base_summary."\n"; echo "TRANSP:OPAQUE\n"; echo "END:VEVENT\n"; } } echo 'END:VCALENDAR'; exit (); } function attach_ical_file( $attachments, $params, $form_id) { global $wpdb, $cp_appb_plugin; $buffer = "BEGIN:VCALENDAR\n"; $buffer .= "PRODID:-//CodePeople//Appointment Hour Booking for WordPress//EN\n"; $buffer .= "VERSION:2.0\n"; $icalSettings = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix.$this->form_table." WHERE formid=%d", $form_id ) ); if ($icalSettings[0]->attachical != '1') return $attachments; $data = $params; $ct = 0; foreach($data["apps"] as $app) { $ct++; $time = explode("/", $app["slot"]); $datetime = $app["date"]." ".trim($time[0]); $duration = "+".$app["duration"]." minutes"; $submissiontime = time(); if ($icalSettings[0]->observe_day_light) { $full_date = gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify)); $year = substr($full_date,0,4); if (strtoupper($icalSettings[0]->ical_daylight_zone) == 'EUROPE') { $dst_start = strtotime('last Sunday GMT', strtotime("1 April $year GMT")); $dst_stop = strtotime('last Sunday GMT', strtotime("1 November $year GMT")); } else { // USA $dst_start = strtotime('first Sunday GMT', strtotime("1 April $year GMT")); $dst_stop = strtotime('last Sunday GMT', strtotime("1 November $year GMT")); } if ($full_date >= gmdate("Ymd",$dst_start) && $full_date < gmdate("Ymd",$dst_stop)) $datetime = date("Y-m-d H:i",strtotime($datetime." -1 hour")); } $base_summary = $icalSettings[0]->base_summary; $base_description = $icalSettings[0]->base_description; $base_summary = str_replace ('<%', '%', $base_summary); $base_description = str_replace ('<%', '%', $base_description); $base_summary = str_replace ('%>', '%', $base_summary); $base_description = str_replace ('%>', '%', $base_description); $base_summary = str_replace('%apps%', $cp_appb_plugin->get_appointments_text($data["apps"]), $base_summary); $base_description = str_replace('%apps%', $cp_appb_plugin->get_appointments_text($data["apps"]), $base_description); foreach ($data as $itemgt => $value) { $base_summary = str_replace('%'.$itemgt.'%',@(is_array($value)?(implode(", ",$value)):($value)),$base_summary); $base_description = str_replace('%'.$itemgt.'%',@(is_array($value)?(implode(", ",$value)):($value)),$base_description); } $base_summary = str_replace("
",'\n',str_replace("
",'\n',str_replace("\r",'',str_replace("\n",'\n',$base_summary)) )); $base_description = str_replace("
",'\n',str_replace("
",'\n',str_replace("\r",'',str_replace("\n",'\n',$base_description)) )); $buffer .= "BEGIN:VEVENT\n"; $buffer .= "DTSTART:".gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify))."T".gmdate("His",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify))."Z\n"; $buffer .= "DTEND:".gmdate("Ymd",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify.$duration))."T".gmdate("His",strtotime($datetime.$icalSettings[0]->cal_time_zone_modify.$duration))."Z\n"; $buffer .= "DTSTAMP:".gmdate("Ymd",$submissiontime)."T".gmdate("His",$submissiontime)."Z\n"; $buffer .= "UID:uid".$params["itemnumber"].'_'.$ct."@".$_SERVER["SERVER_NAME"]."\n"; $buffer .= "DESCRIPTION:".$base_description."\n"; $buffer .= "LAST-MODIFIED:".gmdate("Ymd",$submissiontime)."T".gmdate("His",$submissiontime)."Z\n"; $buffer .= "LOCATION:\n"; $buffer .= "SEQUENCE:0\n"; $buffer .= "STATUS:CONFIRMED\n"; $buffer .= "ORGANIZER;CN=\"".$_SERVER["HTTP_HOST"]."\":MAILTO:".$cp_appb_plugin->get_option('fp_from_email')."\r\n"; $buffer .= "SUMMARY:".$base_summary."\n"; $buffer .= "TRANSP:OPAQUE\n"; $buffer .= "END:VEVENT\n"; } $buffer .= 'END:VCALENDAR'; $filename1 = sanitize_file_name('Appointment'.'_'.$params["itemnumber"].'_admin.ics'); $filename1 = WP_CONTENT_DIR . '/uploads/'.$filename1; $handle = fopen($filename1, 'w'); fwrite($handle,$buffer); fclose($handle); $attachments[] = $filename1; return $attachments; } } // End Class // Main add-on code $cpappb_iCalExport_obj = new CPAPPB_iCalExport(); // Add addon object to the objects list global $cpappb_addons_objs_list; $cpappb_addons_objs_list[ $cpappb_iCalExport_obj->get_addon_id() ] = $cpappb_iCalExport_obj; }