<?php // Get RSS Feed
include_once"wp-config.php";
include_once"wp-load.php";
include_once"wp-includes/wp-db.php";
global $wpdb;
$feeds = ('http://www.trumba.com/calendars/vd.rss?mixin=236385%2c236392%2c236393%2c236288');
$xml = simplexml_load_file($feeds);
$post_fields = array('event_slug','event_owner','event_name','event_attributes','post_id','post_content');
$event_metas = array(); //restart metas
$i=0;
foreach($xml->channel->item as $item) {
//Data for each event entry
$title = $item->title;
$slug = strtolower(str_replace(" ", "-", $title));
$description = $item->description;
$weblink = $item->children('x-trumba', true)->weblink;
$location = $item->children('xCal', true)->location;
$location = substr($location,0,-1); //Only for VailDaily Feed
$sdate = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localstart)),0, -9);
$edate = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localend)),0, -9);
$stime = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localstart)),-8);
$etime = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localend)),-8);
$category = $item->children('x-trumba', true)->customfield;
//Insert New WP Post
$post_array = array();
$post_array['post_type'] = 'event';
$post_array['post_title'] = wp_strip_all_tags($title);
$post_array['post_content'] = $description;
$post_array['post_status'] = 'pending';
$post_array['post_author'] = 1;
$event['start_ts'] = $sdate;
$event['end_ts'] = $edate;
//Save post, register post id in index
$post_id = wp_insert_post($post_array);
if( is_wp_error($post_id) || $post_id == 0 ){ $post_id = 999999999999999999; }
if( $post_id != 999999999999999999 ){
$wpdb->query('UPDATE '.EM_EVENTS_TABLE." SET post_id='$post_id' WHERE event_id='{$event['event_id']}'");
//meta
foreach($event as $meta_key => $meta_val){
if( !in_array($meta_key, $post_fields) && $meta_key != 'event_attributes' ){
$event_metas[] = $wpdb->prepare("(%d, '%s', '%s')", array($post_id, '_'.$meta_key, $meta_val));
}elseif($meta_key == 'event_attributes'){
$event_attributes = unserialize($meta_val); //from em table it's serialized
if( is_array($event_attributes) ){
foreach($event_attributes as $att_key => $att_val){
$event_metas[] = $wpdb->prepare("(%d, '%s', '%s')", array($post_id, $att_key, $att_val));
}
}
}
}
}
//Find possible existing location via text query
$query = 'SELECT location_id FROM wp_em_locations WHERE location_name ="'.$location.'"';
$locresult = mysql_query($query);
echo mysql_error();
//Set location_id to query returned
if (mysql_num_rows($locresult) > 0) {
while ($row = mysql_fetch_assoc($locresult)) {
$location_id = $row['location_id'];
}
} else { $location_id = NULL; }
//Insert INTO EM Table
$sql = "INSERT INTO wp_em_events (event_owner, event_status, event_name, event_start_time, event_end_time, event_start_date, event_end_date, post_content, event_rsvp, post_id, location_id)
values (
1,
0,
'".mysql_real_escape_string($title)."' ,
'".$stime."' ,
'".$etime."' ,
'".$sdate."' ,
'".$edate."' ,
'".mysql_real_escape_string($description)."',
0,
'".$post_id."',
'".$location_id."'
)";
mysql_query($sql);
echo mysql_error();
//Display Results
echo '<div>TITLE: '.$title.'</div>';
echo '<div>LOCATION: '.$location.'</div>';
echo '<div>START: '.$sdate.'</div>';
echo '<div>END: ' .$edate.'</div>';
echo '<div>START: '.$stime.'</div>';
echo '<div>END: ' .$etime.'</div>';
// echo '<div>LINK: ' .$weblink.'</div>';
// echo '<div>CATEGORY: '.$category.'</div>';
if (mysql_num_rows($locresult) > 0) {
while ($row = mysql_fetch_assoc($locresult)) {
echo $row['location_id'];
}
} else {
echo "No existing location matched this in the Database";
}
echo '<hr>';
//TODO Delete this for production or tweak to a manageable count
$i++;
if($i==3) break;
}
//insert the metas in one go, faster than one by one
if( count($event_metas) > 0 ){
$result = $wpdb->query("INSERT INTO ".$wpdb->postmeta." (post_id,meta_key,meta_value) VALUES ".implode(',',$event_metas));
}
//Confirmation
echo "<br><strong>Events have been inserted into the database</strong>";
?>