Last active
February 11, 2020 12:45
-
-
Save npapratovic/26fdfb6e6c1d076a693ba121b4a64977 to your computer and use it in GitHub Desktop.
Import data to WordPress from .ics feed of most popular booking websites (airbnb.com, booking.com, expediapartnercentral.com)
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 | |
error_reporting(-1); // reports all errors | |
ini_set("display_errors", "1"); // shows all errors | |
ini_set("log_errors", 1); | |
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)'); | |
// https://github.com/MartinThoma/ics-parser/blob/master/class.iCalReader.php | |
require_once 'class.iCalReader.php'; | |
// require wp-load.php to use built-in WordPress functions | |
require_once('../wp-load.php'); | |
//first fetch data from airbnb | |
$accommodations = array( | |
array( | |
'name' => 'magnolia', | |
'url' => 'https://hr.airbnb.com/calendar/ical/23570938.ics?s=52b74ac8160bd546239c3beb13f93933' | |
), | |
array( | |
'name' => 'agava', | |
'url' => 'https://hr.airbnb.com/calendar/ical/23588482.ics?s=a6b6b8d370c4a863b1f95d1705b14b7f' | |
) | |
... | |
); | |
foreach($accommodations as $accommodation) { | |
$accommodation_name = $accommodation['name']; | |
switch ($accommodation_name) { | |
case 'magnolia': | |
$accommodation_type = '5836'; | |
break; | |
case 'agava': | |
$accommodation_type = '5834'; | |
break; | |
default: | |
$accommodation_type = '5836'; | |
break; | |
} | |
$accommodation_url = $accommodation['url']; | |
$accomodation_filename = ''.$accommodation_name.'.ics'; | |
$ch = curl_init($accommodation_url); | |
$fp = fopen($accomodation_filename, "w"); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
$curl = curl_exec($ch); | |
if(curl_error($ch)) { | |
fwrite($fp, curl_error($ch)); | |
} | |
curl_close($ch); | |
fclose($fp); | |
$ical = new ical($accomodation_filename); | |
$reservations = $ical->events(); | |
foreach ($reservations as $single_reservation) { | |
$d_start = $single_reservation['DTSTART']; | |
$year_start = substr($d_start, 0, 4); | |
$month_start = substr($d_start, 4, 2); | |
$day_start = substr($d_start, 6, 2); | |
$date_start = $year_start.'-'.$month_start.'-'.$day_start; | |
$d_end = $single_reservation['DTEND']; | |
$year_end = substr($d_end, 0, 4); | |
$month_end = substr($d_end, 4, 2); | |
$day_end = substr($d_end, 6, 2); | |
$date_end = $year_end.'-'.$month_end.'-'.$day_end; | |
$post_title = '[Airbnb - '.$accommodation_name.'] '.$day_start.'.'.$month_start.'.'.$year_start.'.'.' - '.$day_end.'.'.$month_end.'.'.$year_end.'.'.''; | |
//lets check if date is already blocked: | |
$block = get_page_by_title($post_title, OBJECT, 'blocked_dates'); | |
if (empty($block)) { | |
$new_post = array( | |
'post_title' => $post_title, | |
'post_type' => 'blocked_dates', | |
'post_status' => 'publish' | |
); | |
// WordPress wp_insert_post | |
$insert_post = wp_insert_post($new_post, true); | |
$blocked_dates_meta = array( | |
'room_type' => $accommodation_type, | |
'from' => $date_start, // set attribute name | |
'to' => $date_end, // set attribute value | |
'weekday_mon' => '1', | |
'weekday_tue' => '1', | |
'weekday_wed' => '1', | |
'weekday_thu' => '1', | |
'weekday_fri' => '1', | |
'weekday_sat' => '1', | |
'weekday_sun' => '1', | |
); | |
update_post_meta($insert_post, '_blocked_dates_meta', $blocked_dates_meta); | |
echo $post_title . ' inserted as new blocked date<br>'; | |
} else { | |
echo $post_title . ' is already imported as blocked date<br>'; | |
} | |
} | |
} | |
//lets fetch data from expedia central group: | |
$accommodations = array( | |
array( | |
'name' => 'magnolia', | |
'url' => 'https://www.expediapartnercentral.com/calendars/export/971e62a3c09944a493a2d75759a85c45.ics' | |
), | |
array( | |
'name' => 'agava', | |
'url' => 'https://www.expediapartnercentral.com/calendars/export/221c2aa5b8de4c87937ff28b820da930.ics' | |
), | |
... | |
); | |
foreach($accommodations as $accommodation) { | |
$accommodation_name = $accommodation['name']; | |
switch ($accommodation_name) { | |
case 'magnolia': | |
$accommodation_type = '5836'; | |
break; | |
case 'agava': | |
$accommodation_type = '5834'; | |
break; | |
default: | |
$accommodation_type = '5836'; | |
break; | |
} | |
$accommodation_url = $accommodation['url']; | |
$accomodation_filename = 'expedia-'.$accommodation_name.'.ics'; | |
$ch = curl_init($accommodation_url); | |
$fp = fopen($accomodation_filename, "w"); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
$curl = curl_exec($ch); | |
var_dump($curl); | |
if(curl_error($ch)) { | |
fwrite($fp, curl_error($ch)); | |
} | |
curl_close($ch); | |
fclose($fp); | |
$ical = new ical($accomodation_filename); | |
$reservations = $ical->events(); | |
foreach ($reservations as $single_reservation) { | |
$d_start = $single_reservation['DTSTART']; | |
$year_start = substr($d_start, 0, 4); | |
$month_start = substr($d_start, 4, 2); | |
$day_start = substr($d_start, 6, 2); | |
$date_start = $year_start.'-'.$month_start.'-'.$day_start; | |
$d_end = $single_reservation['DTEND']; | |
$year_end = substr($d_end, 0, 4); | |
$month_end = substr($d_end, 4, 2); | |
$day_end = substr($d_end, 6, 2); | |
$date_end = $year_end.'-'.$month_end.'-'.$day_end; | |
$post_title = '[Expediagroupcentral.com - '.$accommodation_name.'] '.$day_start.'.'.$month_start.'.'.$year_start.'.'.' - '.$day_end.'.'.$month_end.'.'.$year_end.'.'.''; | |
//lets check if date is already blocked: | |
$block = get_page_by_title($post_title, OBJECT, 'blocked_dates'); | |
if (empty($block)) { | |
$new_post = array( | |
'post_title' => $post_title, | |
'post_type' => 'blocked_dates', | |
'post_status' => 'publish' | |
); | |
// WordPress wp_insert_post | |
$insert_post = wp_insert_post($new_post, true); | |
$blocked_dates_meta = array( | |
'room_type' => $accommodation_type, | |
'from' => $date_start, // set attribute name | |
'to' => $date_end, // set attribute value | |
'weekday_mon' => '1', | |
'weekday_tue' => '1', | |
'weekday_wed' => '1', | |
'weekday_thu' => '1', | |
'weekday_fri' => '1', | |
'weekday_sat' => '1', | |
'weekday_sun' => '1', | |
); | |
update_post_meta($insert_post, '_blocked_dates_meta', $blocked_dates_meta); | |
echo $post_title . ' inserted as new blocked date<br>'; | |
} else { | |
echo $post_title . ' is already imported as blocked date<br>'; | |
} | |
} | |
} | |
//lets fetch data from booking.com | |
$accommodations = array( | |
array( | |
'name' => 'magnolia', | |
'url' => 'https://admin.booking.com/hotel/hoteladmin/ical.html?t=941d8e9d-d275-4104-9493-1c21d26e90a9' | |
), | |
array( | |
'name' => 'agava', | |
'url' => 'https://admin.booking.com/hotel/hoteladmin/ical.html?t=638a93b5-41b9-47f2-a799-57d1257a597d' | |
), | |
... | |
); | |
foreach($accommodations as $accommodation) { | |
$accommodation_name = $accommodation['name']; | |
switch ($accommodation_name) { | |
case 'magnolia': | |
$accommodation_type = '5836'; | |
break; | |
case 'agava': | |
$accommodation_type = '5834'; | |
break; | |
default: | |
$accommodation_type = '5836'; | |
break; | |
} | |
$accommodation_url = $accommodation['url']; | |
$accomodation_filename = 'booking.com-'.$accommodation_name.'.ics'; | |
$ch = curl_init($accommodation_url); | |
$fp = fopen($accomodation_filename, "w"); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
$curl = curl_exec($ch); | |
// var_dump($curl); | |
if(curl_error($ch)) { | |
fwrite($fp, curl_error($ch)); | |
} | |
curl_close($ch); | |
fclose($fp); | |
$ical = new ical($accomodation_filename); | |
$reservations = $ical->events(); | |
foreach ($reservations as $single_reservation) { | |
$d_start = $single_reservation['DTSTART']; | |
$year_start = substr($d_start, 0, 4); | |
$month_start = substr($d_start, 4, 2); | |
$day_start = substr($d_start, 6, 2); | |
$date_start = $year_start.'-'.$month_start.'-'.$day_start; | |
$d_end = $single_reservation['DTEND']; | |
$year_end = substr($d_end, 0, 4); | |
$month_end = substr($d_end, 4, 2); | |
$day_end = substr($d_end, 6, 2); | |
$date_end = $year_end.'-'.$month_end.'-'.$day_end; | |
$post_title = '[Booking.com - '.$accommodation_name.'] '.$day_start.'.'.$month_start.'.'.$year_start.'.'.' - '.$day_end.'.'.$month_end.'.'.$year_end.'.'.''; | |
//lets check if date is already blocked: | |
$block = get_page_by_title($post_title, OBJECT, 'blocked_dates'); | |
if (empty($block)) { | |
$new_post = array( | |
'post_title' => $post_title, | |
'post_type' => 'blocked_dates', | |
'post_status' => 'publish' | |
); | |
// WordPress wp_insert_post | |
$insert_post = wp_insert_post($new_post, true); | |
$blocked_dates_meta = array( | |
'room_type' => $accommodation_type, | |
'from' => $date_start, // set attribute name | |
'to' => $date_end, // set attribute value | |
'weekday_mon' => '1', | |
'weekday_tue' => '1', | |
'weekday_wed' => '1', | |
'weekday_thu' => '1', | |
'weekday_fri' => '1', | |
'weekday_sat' => '1', | |
'weekday_sun' => '1', | |
); | |
update_post_meta($insert_post, '_blocked_dates_meta', $blocked_dates_meta); | |
echo $post_title . ' inserted as new blocked date<br>'; | |
} else { | |
echo $post_title . ' is already imported as blocked date<br>'; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment