Advertisement
Guest User

Sync Facebook Events Fix

a guest
Apr 25th, 2013
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.36 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Sync Facebook Events
  4. Plugin URI: http://pdxt.com
  5. Description: Sync Facebook Events to The Events Calendar Plugin
  6. Author: Mark Nelson
  7. Version: 1.0.6
  8. Revision by: Sam Haines (Wordpress username shaines1)
  9. Author URI: http://pdxt.com
  10. */
  11.  
  12. /*  Copyright 2012 PDX Technologies, LLC. (mark.nelson@pdxt.com)
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License as published by
  16.     the Free Software Foundation; either version 2 of the License, or
  17.     (at your option) any later version.
  18.  
  19.     This program is distributed in the hope that it will be useful,
  20.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22.     GNU General Public License for more details.
  23.  
  24.     You should have received a copy of the GNU General Public License
  25.     along with this program; if not, write to the Free Software
  26.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28.  
  29. register_activation_hook(__FILE__,'activate_fbes');
  30. register_deactivation_hook(__FILE__,'deactivate_fbes');
  31. function activate_fbes() { wp_schedule_event(time(), 'daily', 'fbes_execute_sync'); }
  32. function deactivate_fbes() { wp_clear_scheduled_hook('fbes_execute_sync'); }
  33. add_action('fbes_execute_sync', 'fbes_process_events');
  34.  
  35. function update_schedule($fbes_frequency) {
  36.    
  37.         wp_clear_scheduled_hook('fbes_execute_sync');
  38.         wp_schedule_event(time(), $fbes_frequency, 'fbes_execute_sync');
  39. }
  40.  
  41. function fbes_add_page() { add_options_page('Sync FB Events', 'Sync FB Events', 8, __FILE__, 'fbes_options_page'); }
  42. add_action('admin_menu', 'fbes_add_page');
  43.  
  44. function fbes_process_events() {
  45.  
  46.     #Get option values
  47.     $fbes_api_key = get_option('fbes_api_key');
  48.     $fbes_api_secret = get_option('fbes_api_secret');
  49.     $fbes_api_uid = get_option('fbes_api_uid');
  50.     $fbes_api_uids = get_option('fbes_api_uids');  
  51.     $fbes_frequency = get_option('fbes_frequency');
  52.  
  53.    
  54.     $events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_api_uids);
  55.     fbes_send_events($events);
  56. }
  57.  
  58. function fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_api_uids) {
  59.  
  60.     require 'facebook.php';
  61.    
  62.     $facebook = new Facebook(array(
  63.         'appId'  =>  $fbes_api_key,
  64.         'secret' =>  $fbes_api_secret,
  65.         'cookie' => true,
  66.     ));
  67. Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
  68. Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
  69.  
  70.     $ret = array();
  71.     foreach ($fbes_api_uids as $key => $value) {
  72.  
  73.         if($value!='') {
  74.             //https://developers.facebook.com/docs/reference/fql/event/
  75.             $fql = "SELECT eid, name, start_time, end_time, location, description
  76.                     FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = $value )
  77.                     ORDER BY start_time desc";
  78.  
  79.             $param  =   array(
  80.                 'method'    => 'fql.query',
  81.                 'query'     => $fql,
  82.                 'callback'  => ''
  83.             );
  84.    
  85.             $result = $facebook->api($param);
  86.             foreach($result as $k => $v)
  87.                 $result[$k]['uid'] = $value;
  88.             $ret = array_merge($ret, $result);
  89.         }
  90.     }
  91.    
  92.        
  93.     return $ret;
  94. }
  95.  
  96. function fbes_segments($url='') {
  97.     $parsed_url = parse_url($url);
  98.     $path = trim($parsed_url['path'],'/');
  99.     return explode('/',$path);
  100. }
  101.  
  102. function fbes_send_events($events) {
  103.  
  104.     $query = new WP_Query(array(
  105.         'post_type'=>'tribe_events',
  106.         'posts_per_page'=>'-1'
  107.     ));
  108.    
  109.    
  110.    
  111.     foreach($query->posts as $post) {
  112.         if(!empty($post->to_ping)) {
  113.             $segments = fbes_segments($post->to_ping);
  114.             $eid = array_pop($segments);
  115.             $eids[$eid] = $post->ID;
  116.         }
  117. //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
  118. //http://codex.wordpress.org/Version_3.4 - June 13, 2012
  119. //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.
  120. /*
  121.         else {
  122.             $post_date = trim(substr($post->post_date, 0, 10));
  123.             if($post->post_date > '2012-06-12')
  124.                 wp_delete_post($post->ID);
  125.         }
  126. */
  127.     }
  128.     //file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fbevent.log', print_r(array(time(),$events,$eids),1)."\n".str_repeat('=',40)."\n", FILE_APPEND);
  129.    
  130.     foreach($events as $event) {
  131.         $args['post_title'] = $event['name'];
  132.        
  133.         $offset = get_option('gmt_offset')*3600;
  134.        
  135.         $offsetStart = strtotime($event['start_time'])+$offset;
  136.         $offsetEnd = $event['end_time']+$offset;
  137.        
  138.         //don't update or insert events from the past.
  139.         if($offsetStart > time()) {
  140.             $args['EventStartDate'] = date("m/d/Y", $offsetStart);
  141.             $args['EventStartHour'] = date("H", $offsetStart);
  142.             $args['EventStartMinute'] = date("i", $offsetStart);
  143.            
  144.             $args['EventEndDate'] = date("m/d/Y", $offsetEnd);
  145.             $args['EventEndHour'] = date("H", $offsetEnd);
  146.             $args['EventEndMinute'] = date("i", $offsetEnd);
  147.    
  148.             $args['post_content'] = $event['description'];
  149.             $args['Venue']['Venue'] = $event['location'];
  150.            
  151.             $args['post_status'] = "Publish";
  152.             $args['post_type'] = "tribe_events";
  153.             //$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4
  154.             $args['to_ping'] = 'https://www.facebook.com/events/'.$event['eid'].'/';
  155.            
  156.             if($args['EventStartHour'] == '22' && $event['uid'] == '256763181050120') { //why are UT events 2 hours off???
  157.                 $args['EventStartHour'] = '20';
  158.                 $args['EventEndHour'] = '22';
  159.                 $args['EventEndDate'] = date('m/d/Y',strtotime($args['EventEndDate'], '-1 day'));
  160.             }
  161.  
  162.             $inserting = $post_id = false;
  163.             if (!array_key_exists($event['eid'], $eids)) {
  164.                 //double check
  165.                 $already_exists = false;
  166.                 foreach($query->posts as $post) {
  167.                     if($post->to_ping == $args['to_ping'] || trim($post->pinged) == $args['to_ping']) {
  168.                         $already_exists = true;
  169.                     }
  170.                 }
  171.                 if(!$already_exists) {
  172.                     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);
  173.                     $post_id = tribe_create_event($args);
  174.                     echo "<br />Inserting: ".$post_id;
  175.                     $inserting = true;
  176.                 }
  177.             }
  178.             if(!$inserting) {
  179.                 $post_id = $eids[$event['eid']];
  180.                 tribe_update_event($post_id, $args);
  181.                 echo "<br />Updating: ".$eids[$event['eid']];
  182.             }
  183.             if($post_id)
  184.                 update_metadata('post', $post_id, 'fb_event_obj', $event);
  185.                 //eid, name, start_time, end_time, location, description
  186.         }
  187.         //reset($eids);
  188.     }
  189. }
  190.  
  191. function fbes_options_page() {
  192.  
  193.     $fbes_api_uids = array();
  194.  
  195.     #Get option values
  196.     $fbes_api_key = get_option('fbes_api_key');
  197.     $fbes_api_secret = get_option('fbes_api_secret');
  198.     $fbes_api_uid = get_option('fbes_api_uid');
  199.     $fbes_api_uids = get_option('fbes_api_uids');
  200.     $fbes_frequency = get_option('fbes_frequency');
  201.    
  202.     #Get new updated option values, and save them
  203.     if( !empty($_POST['update']) ) {
  204.    
  205.         $fbes_api_key = $_POST['fbes_api_key'];
  206.         update_option('fbes_api_key', $fbes_api_key);
  207.  
  208.         $fbes_api_secret = $_POST['fbes_api_secret'];
  209.         update_option('fbes_api_secret', $fbes_api_secret);
  210.  
  211.         $fbes_api_uid = $_POST['fbes_api_uid'];
  212.         update_option('fbes_api_uid', $fbes_api_uid);
  213.  
  214.         $fbes_frequency = $_POST['fbes_frequency'];
  215.         update_option('fbes_frequency', $fbes_frequency);
  216.        
  217.         $events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_api_uids);
  218.  
  219.         update_schedule($fbes_frequency);
  220.  
  221.         $msg = "Syncronization of Events from Facebook Complete.";
  222. ?>
  223.         <div id="message" class="updated fade"><p><strong><?php echo $msg; ?></strong></p></div>
  224. <?php
  225.     } elseif( !empty($_POST['add-uid']) ) {
  226.  
  227.         if(!in_array($_POST['fbes_api_uid'], $fbes_api_uids)) {
  228.             $fbes_api_uids[] = $_POST['fbes_api_uid'];
  229.             update_option('fbes_api_uids', $fbes_api_uids);
  230.         }
  231.        
  232.     } elseif( !empty($_GET['r']) ) {
  233.        
  234.         foreach ($fbes_api_uids as $key => $value)
  235.             if($fbes_api_uids[$key] == $_GET['r'])
  236.                 unset($fbes_api_uids[$key]);
  237.                
  238.         update_option('fbes_api_uids', $fbes_api_uids);
  239.     }  
  240. ?>
  241.     <div class="wrap">
  242.         <br /><div class="icon32" id="icon-plugins"><br/></div>
  243.         <h2 style="margin-bottom:10px;">Sync Facebook Events</h2>
  244.         <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
  245.         <input type="hidden" name="update" />
  246.         <?php
  247.         echo '<form action="'. $_SERVER["REQUEST_URI"] .'" method="post"><table style="width:475px;">';
  248.         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>';
  249.         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>';
  250.  
  251.         echo '<tr><td>Update Fequency:</td><td><select id="fbes_frequency" name="fbes_frequency">';    
  252.         if(htmlentities($fbes_frequency)=="daily") {
  253.             echo '<option value="daily" SELECTED>Daily</option>';
  254.         } else {
  255.             echo '<option value="daily">Daily</option>';
  256.         }  
  257.         if(htmlentities($fbes_frequency)=="twicedaily") {
  258.             echo '<option value="twicedaily" SELECTED>Twice Daily</option>';
  259.         } else {
  260.             echo '<option value="twicedaily">Twice Daily</option>';
  261.         }
  262.         if(htmlentities($fbes_frequency)=="hourly") {
  263.             echo '<option value="hourly" SELECTED>Hourly</option>';
  264.         } else {
  265.             echo '<option value="hourly">Hourly</option>';
  266.         }
  267.         echo '</select>';
  268.        
  269.         echo '<tr><td>Add Facebook Page UID:</td><td><input type="text" id="fbes_api_uid" name="fbes_api_uid" value="" size="15" />';
  270.         echo '<input type="submit" value="Add" class="button-secondary" name="add-uid" /></td></tr>';
  271.  
  272.         echo '<tr><td style="vertical-align:top;"></td><td>';
  273.  
  274.         foreach ($fbes_api_uids as $value) {
  275.             if($value!='')
  276.                 echo '&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;<a href="'.$_SERVER["REQUEST_URI"].'&r='.$value.'">remove</a><br />';
  277.         }
  278.        
  279.         echo '</td></tr>';
  280.        
  281.         echo '<tr><td colspan="2"></td></tr><tr><td colspan="2"><br /><input type="submit" value="Update" class="button-primary"';
  282.         echo ' name="update" /></td></tr></table>';
  283.         ?>
  284.         </form>
  285.     </div>
  286.     <?php if(isset($events)) { ?>
  287.         <div style="margin-top:20px;font-size:14px;color:#444;border:1px solid #999;padding:15px;width:95%;font-face:couriernew;">
  288.         <span style="color:red;">Updating all facebook events...</span><br />
  289.         <?php fbes_send_events($events); ?><br />
  290.         <span style="color:red;">Events Calendar updated with current Facebook events.</span><br /><br />
  291.         </div>
  292.     <?php } ?>
  293. <?php  
  294. }
  295. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement