Skip to content

Instantly share code, notes, and snippets.

@niklasvincent
Created August 26, 2011 10:20
Show Gist options
  • Save niklasvincent/1173143 to your computer and use it in GitHub Desktop.
Save niklasvincent/1173143 to your computer and use it in GitHub Desktop.
Facebook Event WP Plugin
<?php
/*
Plugin Name: Facebook Event
Plugin URI: http://www.studentlund.se
Description: Detect Facebook event URL in post and update post content to match the event
Author: Niklas Lindblad
Version: 0.1
Author URI: http://niklas.lindblad.info
*/
function is_facebook_event( $url )
{
return ( preg_match("/http(s?):\/\/www\.facebook\.com\/event.php\?eid=[0-9]*\.*/", $url) == 1 );
}
function get_facebook_event ( $url )
{
$m = explode('=', $url);
$eid = preg_replace("/[^0-9]/", "", $m[1]);
// Cache entry ID
$key = 'facebook_event_' . $eid;
$cache = '/tmp/' . $key;
// Check if alredy in cache
if ( file_exists($cache) ) {
error_log('Got event info from cache');
return @json_decode(file_get_contents($cache));
}
// Create the stream context
$context = stream_context_create(array(
'http' => array(
'timeout' => 10 // Timeout in seconds
)
));
error_log('Got event info from Facebook.com');
$content = @file_get_contents('https://graph.facebook.com/' . $eid, 0, $context);
// Store in cache
@file_put_contents($cache, $content);
if ( empty($content) ) {
return false;
}
$event = @json_decode($content);
if ( $event == null ) {
return false;
}
return $event;
}
function process_facebook_event( $post_ID )
{
error_log('I was called');
$post = get_post($post_ID, ARRAY_A);
if ( is_facebook_event($post['post_content']) ) {
$url = $post['post_content'];
} else if ( is_facebook_event($post['post_title']) ) {
$url = $post['post_title'];
} else {
return $post_ID;
}
$event = get_facebook_event($url);
if ( $event == false ) {
return $post_ID;
}
$updated_post = array();
// Update the post
$updated_post['ID'] = $post_ID;
$updated_post['post_title'] = $event->name;
$updated_post['post_content'] = sprintf('<b>%s</b><br />%s<br /><br />', $event->location, $event->venue->street);
$updated_post['post_content'] .= $event->description;
wp_update_post($post);
// Specific for Studentlund.se
update_post_meta($post_ID, 'datum', date('Y-m-d', strtotime($event->start_time)));
}
//add_action('publish_post', 'process_facebook_event');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment