$fbes_api_key,
'secret' => $fbes_api_secret,
'cookie' => true,
));
$facebook->setAccessToken($fbes_access_token);
$ret = array();
foreach ($fbes_api_uids as $key => $value) {
if($value!='') {
$result = $facebook->api('/'.$value.'/events', 'GET', array(
'fields' => 'name,start_time,end_time,place,description,timezone',
'date_format' => 'U'
));
$result = $result['data'];
foreach($result as $k => $v){
$result[$k]['uid'] = $value;
$result[$k]['start_time'] = strtotime($result[$k]['start_time']);
$result[$k]['end_time'] = strtotime($result[$k]['end_time']);
if($result[$k]['place']){
if($result[$k]['place']['name']){
$result[$k]['location'] = $result[$k]['place']['name'];
}else{
$result[$k]['location'] = $result[$k]['place'];
}
}
}
$ret = array_merge($ret, $result);
}
}
return $ret;
}
function fbes_segments($url='') {
$parsed_url = parse_url($url);
$path = trim($parsed_url['path'],'/');
return explode('/',$path);
}
function fbes_send_events($events) {
$eids = array();
$query = new WP_Query(array(
'post_type'=>'tribe_events',
'posts_per_page'=>'-1'
));
foreach($query->posts as $post) {
if(!empty($post->to_ping)) {
$segments = fbes_segments($post->to_ping);
$eid = array_pop($segments);
$eids[$eid] = $post->ID;
}
//if you're reading this and you want to delete all those duplicate events, uncomment this temporarially. Note, it will also delete all manually made events since June 13
//http://codex.wordpress.org/Version_3.4 - June 13, 2012
//depending on many duplicates you had, you might end up re-loading this script a bunch of times after it times out. Me, I had 14k duplicates. Had to run the script like 10 times.
/*
else {
$post_date = trim(substr($post->post_date, 0, 10));
if($post->post_date > '2012-06-12')
wp_delete_post($post->ID);
}
*/
}
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fbevent.log', print_r(array(time(),$events,$eids),1)."\n".str_repeat('=',40)."\n", FILE_APPEND);
foreach($events as $event) {
$args['post_title'] = $event['name'];
$offset = get_option('gmt_offset')*3600;
$offsetStart = $event['start_time']+$offset;
$offsetEnd = $event['end_time']+$offset;
//don't update or insert events from the past.
if($offsetStart > time()) {
$args['EventStartDate'] = date("Y-m-d", $offsetStart);
$args['EventStartHour'] = date("H", $offsetStart);
$args['EventStartMinute'] = date("i", $offsetStart);
$args['EventEndDate'] = date("Y-m-d", $offsetEnd);
$args['EventEndHour'] = date("H", $offsetEnd);
$args['EventEndMinute'] = date("i", $offsetEnd);
$args['post_content'] = $event['description'];
$args['Venue']['Venue'] = $event['location'];
$args['post_status'] = "Publish";
$args['post_type'] = "tribe_events";
//$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4
$args['to_ping'] = 'https://www.facebook.com/events/'.$event['id'].'/';
$inserting = $post_id = false;
if (!array_key_exists($event['id'], $eids)) {
//double check
$already_exists = false;
foreach($query->posts as $post) {
if($post->to_ping == $args['to_ping'] || trim($post->pinged) == $args['to_ping']) {
$already_exists = true;
}
}
if(!$already_exists) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fbevent.log', print_r(array(time(),'creating', $args, $eids, $query->posts),1)."\n".str_repeat('=',40)."\n", FILE_APPEND);
$post_id = tribe_create_event($args);
echo "
Inserting: ".$post_id;
$inserting = true;
}
}
if(!$inserting) {
$post_id = $eids[$event['id']];
echo "$post_id";
tribe_update_event($post_id, $args);
echo "
Updating: ".$eids[$event['id']];
}
if($post_id)
update_metadata('post', $post_id, 'fb_event_obj', $event);
//eid, name, start_time, end_time, location, description
}
if($eids)
reset($eids);
}
}
function fbes_options_page() {
$fbes_api_uids = array();
#Get option values
$fbes_api_key = get_option('fbes_api_key');
$fbes_api_secret = get_option('fbes_api_secret');
$fbes_api_uid = get_option('fbes_api_uid');
$fbes_api_uids = get_option('fbes_api_uids');
$fbes_frequency = get_option('fbes_frequency');
#Get new updated option values, and save them
if( !empty($_POST['update']) ) {
$fbes_api_key = $_POST['fbes_api_key'];
update_option('fbes_api_key', $fbes_api_key);
$fbes_api_secret = $_POST['fbes_api_secret'];
update_option('fbes_api_secret', $fbes_api_secret);
$fbes_api_uid = $_POST['fbes_api_uid'];
update_option('fbes_api_uid', $fbes_api_uid);
$fbes_frequency = $_POST['fbes_frequency'];
update_option('fbes_frequency', $fbes_frequency);
$events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids);
// $events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_api_uids);
update_schedule($fbes_frequency);
$msg = "Syncronization of Events from Facebook Complete.";
?>
".json_encode($events, JSON_PRETTY_PRINT).""; ?>