1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
Sync-Facebook-Events/sync-facebook-events.php

331 lines
11 KiB
PHP
Raw Permalink Normal View History

2013-04-22 06:29:25 +02:00
<?php
/*
Plugin Name: Sync Facebook Events
Plugin URI: http://pdxt.com
2015-12-10 16:06:44 +01:00
Description: Sync Facebook Events to The Events Calendar Plugin
2013-04-22 06:29:25 +02:00
Author: Mark Nelson
2015-12-10 15:40:02 +01:00
Version: 1.0.9
2013-04-22 06:29:25 +02:00
Author URI: http://pdxt.com
*/
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
/* Copyright 2012 PDX Technologies, LLC. (mark.nelson@pdxt.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
register_activation_hook(__FILE__,'activate_fbes');
register_deactivation_hook(__FILE__,'deactivate_fbes');
function activate_fbes() { wp_schedule_event(time(), 'daily', 'fbes_execute_sync'); }
function deactivate_fbes() { wp_clear_scheduled_hook('fbes_execute_sync'); }
add_action('fbes_execute_sync', 'fbes_process_events');
function update_schedule($fbes_frequency) {
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
wp_clear_scheduled_hook('fbes_execute_sync');
wp_schedule_event(time(), $fbes_frequency, 'fbes_execute_sync');
}
function fbes_add_page() { add_options_page('Sync FB Events', 'Sync FB Events', 8, __FILE__, 'fbes_options_page'); }
add_action('admin_menu', 'fbes_add_page');
function fbes_process_events() {
#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');
2015-12-10 16:06:44 +01:00
$fbes_api_uids = get_option('fbes_api_uids');
2013-04-22 06:29:25 +02:00
$fbes_frequency = get_option('fbes_frequency');
2015-12-10 22:13:33 +01:00
// $fbes_access_token = $fbes_api_key+"|"+$fbes_api_secret;
2013-04-23 02:22:57 +02:00
$fbes_access_token = get_option('fbes_access_token'); // modified
2013-04-22 06:29:25 +02:00
2015-12-10 16:06:44 +01:00
2013-04-23 02:22:57 +02:00
$events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids);
2013-04-22 06:29:25 +02:00
fbes_send_events($events);
}
2013-04-23 02:22:57 +02:00
function fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids) {
2015-12-10 16:06:44 +01:00
require_once 'facebook-php-sdk/facebook.php'; //modified
2013-04-22 06:29:25 +02:00
$facebook = new Facebook(array(
'appId' => $fbes_api_key,
'secret' => $fbes_api_secret,
'cookie' => true,
));
$facebook->setAccessToken($fbes_access_token);
2013-04-23 02:22:57 +02:00
$ret = array();
foreach ($fbes_api_uids as $key => $value) {
if($value!='') {
$result = $facebook->api('/'.$value.'/events', 'GET', array(
2015-12-10 22:13:33 +01:00
'fields' => 'name,start_time,end_time,place,description,timezone',
'date_format' => 'U'
2013-04-23 02:22:57 +02:00
));
$result = $result['data'];
2015-12-10 22:13:33 +01:00
foreach($result as $k => $v){
2013-04-23 02:22:57 +02:00
$result[$k]['uid'] = $value;
2015-12-10 22:13:33 +01:00
$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'];
}
}
}
2013-04-23 02:22:57 +02:00
$ret = array_merge($ret, $result);
}
}
2015-12-10 22:13:33 +01:00
return $ret;
2013-04-22 06:29:25 +02:00
}
function fbes_segments($url='') {
$parsed_url = parse_url($url);
$path = trim($parsed_url['path'],'/');
return explode('/',$path);
}
function fbes_send_events($events) {
2015-12-10 22:13:33 +01:00
$eids = array();
2013-04-22 06:29:25 +02:00
$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);
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
foreach($events as $event) {
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$args['post_title'] = $event['name'];
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$offset = get_option('gmt_offset')*3600;
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$offsetStart = $event['start_time']+$offset;
$offsetEnd = $event['end_time']+$offset;
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
//don't update or insert events from the past.
2015-12-10 22:13:33 +01:00
if($offsetStart > time()) {
2015-12-10 16:06:44 +01:00
2015-12-10 22:13:33 +01:00
$args['EventStartDate'] = date("Y-m-d", $offsetStart);
2013-04-22 06:29:25 +02:00
$args['EventStartHour'] = date("H", $offsetStart);
$args['EventStartMinute'] = date("i", $offsetStart);
2015-12-10 16:06:44 +01:00
2015-12-10 22:13:33 +01:00
$args['EventEndDate'] = date("Y-m-d", $offsetEnd);
2013-04-22 06:29:25 +02:00
$args['EventEndHour'] = date("H", $offsetEnd);
$args['EventEndMinute'] = date("i", $offsetEnd);
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$args['post_content'] = $event['description'];
$args['Venue']['Venue'] = $event['location'];
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$args['post_status'] = "Publish";
$args['post_type'] = "tribe_events";
//$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4
2015-12-10 22:13:33 +01:00
$args['to_ping'] = 'https://www.facebook.com/events/'.$event['id'].'/';
2013-04-22 06:29:25 +02:00
$inserting = $post_id = false;
2015-12-10 22:13:33 +01:00
if (!array_key_exists($event['id'], $eids)) {
2013-04-22 06:29:25 +02:00
//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 "<br />Inserting: ".$post_id;
$inserting = true;
}
}
if(!$inserting) {
2015-12-10 22:13:33 +01:00
$post_id = $eids[$event['id']];
echo "$post_id";
2013-04-22 06:29:25 +02:00
tribe_update_event($post_id, $args);
2015-12-10 22:13:33 +01:00
echo "<br />Updating: ".$eids[$event['id']];
2013-04-22 06:29:25 +02:00
}
2015-12-10 16:06:44 +01:00
if($post_id)
2013-04-22 06:29:25 +02:00
update_metadata('post', $post_id, 'fb_event_obj', $event);
//eid, name, start_time, end_time, location, description
}
2015-12-10 22:13:33 +01:00
if($eids)
reset($eids);
2013-04-22 06:29:25 +02:00
}
}
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');
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
#Get new updated option values, and save them
if( !empty($_POST['update']) ) {
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
$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);
2015-12-10 16:06:44 +01:00
2015-12-10 22:13:33 +01:00
$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);
2013-04-22 06:29:25 +02:00
update_schedule($fbes_frequency);
$msg = "Syncronization of Events from Facebook Complete.";
?>
<div id="message" class="updated fade"><p><strong><?php echo $msg; ?></strong></p></div>
<?php
} elseif( !empty($_POST['add-uid']) ) {
if(!in_array($_POST['fbes_api_uid'], $fbes_api_uids)) {
$fbes_api_uids[] = $_POST['fbes_api_uid'];
update_option('fbes_api_uids', $fbes_api_uids);
}
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
} elseif( !empty($_GET['r']) ) {
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
foreach ($fbes_api_uids as $key => $value)
if($fbes_api_uids[$key] == $_GET['r'])
unset($fbes_api_uids[$key]);
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
update_option('fbes_api_uids', $fbes_api_uids);
2015-12-10 16:06:44 +01:00
}
2013-04-23 02:22:57 +02:00
2015-12-10 16:06:44 +01:00
require_once 'facebook-php-sdk/facebook.php'; //modified
2013-04-23 02:22:57 +02:00
$facebook = new Facebook(array(
'appId' => $fbes_api_key,
'secret' => $fbes_api_secret,
'cookie' => true,
));
$facebook->setAccessToken($fbes_access_token);
$events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids);
echo "<pre>".json_encode($events, JSON_PRETTY_PRINT)."</pre>";
2013-04-22 06:29:25 +02:00
?>
<div class="wrap">
<br /><div class="icon32" id="icon-plugins"><br/></div>
<h2 style="margin-bottom:10px;">Sync Facebook Events</h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<input type="hidden" name="update" />
<?php
2015-12-10 16:06:44 +01:00
echo '<form action="'. $_SERVER["REQUEST_URI"] .'" method="post"><table style="width:475px;">';
2013-04-22 06:29:25 +02:00
echo '<tr><td>Facebook App ID:</td><td><input type="text" id="fbes_api_key" name="fbes_api_key" value="'.htmlentities($fbes_api_key).'" size="35" /></td><tr>';
echo '<tr><td>Facebook App Secret:</td><td><input type="text" id="fbes_api_secret" name="fbes_api_secret" value="'.htmlentities($fbes_api_secret) .'" size="35" /></td><tr>';
2015-12-10 16:06:44 +01:00
echo '<tr><td>Update Fequency:</td><td><select id="fbes_frequency" name="fbes_frequency">';
2013-04-22 06:29:25 +02:00
if(htmlentities($fbes_frequency)=="daily") {
echo '<option value="daily" SELECTED>Daily</option>';
} else {
echo '<option value="daily">Daily</option>';
2015-12-10 16:06:44 +01:00
}
2013-04-22 06:29:25 +02:00
if(htmlentities($fbes_frequency)=="twicedaily") {
echo '<option value="twicedaily" SELECTED>Twice Daily</option>';
} else {
echo '<option value="twicedaily">Twice Daily</option>';
}
if(htmlentities($fbes_frequency)=="hourly") {
echo '<option value="hourly" SELECTED>Hourly</option>';
} else {
echo '<option value="hourly">Hourly</option>';
}
echo '</select>';
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
echo '<tr><td>Add Facebook Page UID:</td><td><input type="text" id="fbes_api_uid" name="fbes_api_uid" value="" size="15" />';
echo '<input type="submit" value="Add" class="button-secondary" name="add-uid" /></td></tr>';
echo '<tr><td style="vertical-align:top;"></td><td>';
foreach ($fbes_api_uids as $value) {
if($value!='')
echo '&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;<a href="'.$_SERVER["REQUEST_URI"].'&r='.$value.'">remove</a><br />';
}
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
echo '</td></tr>';
2015-12-10 16:06:44 +01:00
2013-04-22 06:29:25 +02:00
echo '<tr><td colspan="2"></td></tr><tr><td colspan="2"><br /><input type="submit" value="Update" class="button-primary"';
echo ' name="update" /></td></tr></table>';
?>
</form>
2013-04-23 02:22:57 +02:00
<?php
$code = $_REQUEST["code"];
if(!empty($code)){
update_option('fbes_access_token', $facebook->getAccessToken());
}
else if(!get_option('fbes_access_token')){
$params = array(
'scope' => 'manage_pages',
'redirect_uri' => curPageURL()
);
$loginUrl = $facebook->getLoginUrl($params);
echo "<a href='$loginUrl'>Authorize Facebook</a>";
}
?>
2013-04-22 06:29:25 +02:00
</div>
<?php if(isset($events)) { ?>
<div style="margin-top:20px;font-size:14px;color:#444;border:1px solid #999;padding:15px;width:95%;font-face:couriernew;">
<span style="color:red;">Updaing all facebook events...</span><br />
<?php fbes_send_events($events); ?><br />
<span style="color:red;">Events Calendar updated with current Facebook events.</span><br /><br />
</div>
2015-12-10 15:50:58 +01:00
<?php } ?>
2015-12-10 16:06:44 +01:00
<?php
2013-04-22 06:29:25 +02:00
}
2013-04-23 02:22:57 +02:00
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
2013-04-22 06:29:25 +02:00
?>