fixes to work
This commit is contained in:
		
						commit
						af593be2e7
					
				|  | @ -2,12 +2,12 @@ | ||||||
| /* | /* | ||||||
| Plugin Name: Sync Facebook Events | Plugin Name: Sync Facebook Events | ||||||
| Plugin URI: http://pdxt.com | Plugin URI: http://pdxt.com | ||||||
| Description: Sync Facebook Events to The Events Calendar Plugin  | Description: Sync Facebook Events to The Events Calendar Plugin | ||||||
| Author: Mark Nelson | Author: Mark Nelson | ||||||
| Version: 1.0.9 | Version: 1.0.9 | ||||||
| Author URI: http://pdxt.com | Author URI: http://pdxt.com | ||||||
| */ | */ | ||||||
|   | 
 | ||||||
| /*  Copyright 2012 PDX Technologies, LLC. (mark.nelson@pdxt.com) | /*  Copyright 2012 PDX Technologies, LLC. (mark.nelson@pdxt.com) | ||||||
| 
 | 
 | ||||||
|     This program is free software; you can redistribute it and/or modify |     This program is free software; you can redistribute it and/or modify | ||||||
|  | @ -32,7 +32,7 @@ function deactivate_fbes() { wp_clear_scheduled_hook('fbes_execute_sync'); } | ||||||
| add_action('fbes_execute_sync', 'fbes_process_events'); | add_action('fbes_execute_sync', 'fbes_process_events'); | ||||||
| 
 | 
 | ||||||
| function update_schedule($fbes_frequency) { | function update_schedule($fbes_frequency) { | ||||||
| 	 | 
 | ||||||
| 		wp_clear_scheduled_hook('fbes_execute_sync'); | 		wp_clear_scheduled_hook('fbes_execute_sync'); | ||||||
| 		wp_schedule_event(time(), $fbes_frequency, 'fbes_execute_sync'); | 		wp_schedule_event(time(), $fbes_frequency, 'fbes_execute_sync'); | ||||||
| } | } | ||||||
|  | @ -46,18 +46,19 @@ function fbes_process_events() { | ||||||
| 	$fbes_api_key = get_option('fbes_api_key'); | 	$fbes_api_key = get_option('fbes_api_key'); | ||||||
| 	$fbes_api_secret = get_option('fbes_api_secret'); | 	$fbes_api_secret = get_option('fbes_api_secret'); | ||||||
| 	$fbes_api_uid = get_option('fbes_api_uid'); | 	$fbes_api_uid = get_option('fbes_api_uid'); | ||||||
| 	$fbes_api_uids = get_option('fbes_api_uids');	 | 	$fbes_api_uids = get_option('fbes_api_uids'); | ||||||
| 	$fbes_frequency = get_option('fbes_frequency'); | 	$fbes_frequency = get_option('fbes_frequency'); | ||||||
|  | //	$fbes_access_token = $fbes_api_key+"|"+$fbes_api_secret;
 | ||||||
| 	$fbes_access_token = get_option('fbes_access_token'); // modified
 | 	$fbes_access_token = get_option('fbes_access_token'); // modified
 | ||||||
| 
 | 
 | ||||||
| 	 | 
 | ||||||
| 	$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_access_token, $fbes_api_uids); | ||||||
| 	fbes_send_events($events); | 	fbes_send_events($events); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids) { | function fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $fbes_api_uids) { | ||||||
| 
 | 
 | ||||||
| 	require 'facebook-php-sdk/facebook.php'; //modified
 | 	require_once 'facebook-php-sdk/facebook.php'; //modified
 | ||||||
| 
 | 
 | ||||||
| 	$facebook = new Facebook(array( | 	$facebook = new Facebook(array( | ||||||
| 		'appId'  =>  $fbes_api_key, | 		'appId'  =>  $fbes_api_key, | ||||||
|  | @ -71,16 +72,26 @@ function fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_access_token, $f | ||||||
|   foreach ($fbes_api_uids as $key => $value) { |   foreach ($fbes_api_uids as $key => $value) { | ||||||
|     if($value!='') { |     if($value!='') { | ||||||
|       $result = $facebook->api('/'.$value.'/events', 'GET', array( |       $result = $facebook->api('/'.$value.'/events', 'GET', array( | ||||||
|         'fields' => 'name,start_time,end_time,location,description,timezone' |         'fields' => 'name,start_time,end_time,place,description,timezone', | ||||||
|  | 	'date_format' => 'U' | ||||||
|       )); |       )); | ||||||
|       $result = $result['data']; |       $result = $result['data']; | ||||||
|       foreach($result as $k => $v) |       foreach($result as $k => $v){ | ||||||
|         $result[$k]['uid'] = $value; |         $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); |       $ret = array_merge($ret, $result); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 |   return $ret; | ||||||
| 	return $ret; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function fbes_segments($url='') { | function fbes_segments($url='') { | ||||||
|  | @ -90,12 +101,11 @@ function fbes_segments($url='') { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function fbes_send_events($events) { | function fbes_send_events($events) { | ||||||
| 
 | 	$eids = array(); | ||||||
| 	$query = new WP_Query(array( | 	$query = new WP_Query(array( | ||||||
| 		'post_type'=>'tribe_events', | 		'post_type'=>'tribe_events', | ||||||
| 		'posts_per_page'=>'-1' | 		'posts_per_page'=>'-1' | ||||||
| 	)); | 	)); | ||||||
| 
 |  | ||||||
| 	foreach($query->posts as $post) { | 	foreach($query->posts as $post) { | ||||||
| 		if(!empty($post->to_ping)) { | 		if(!empty($post->to_ping)) { | ||||||
| 			$segments = fbes_segments($post->to_ping); | 			$segments = fbes_segments($post->to_ping); | ||||||
|  | @ -114,43 +124,37 @@ function fbes_send_events($events) { | ||||||
| */ | */ | ||||||
| 	} | 	} | ||||||
| 	//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fbevent.log', print_r(array(time(),$events,$eids),1)."\n".str_repeat('=',40)."\n", FILE_APPEND);
 | 	//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) { | 	foreach($events as $event) { | ||||||
| 		 | 
 | ||||||
| 		$args['post_title'] = $event['name']; | 		$args['post_title'] = $event['name']; | ||||||
| 		 | 
 | ||||||
| 		$offset = get_option('gmt_offset')*3600; | 		$offset = get_option('gmt_offset')*3600; | ||||||
| 		 | 
 | ||||||
| 		$offsetStart = $event['start_time']+$offset; | 		$offsetStart = $event['start_time']+$offset; | ||||||
| 		$offsetEnd = $event['end_time']+$offset; | 		$offsetEnd = $event['end_time']+$offset; | ||||||
| 		 | 
 | ||||||
| 		//don't update or insert events from the past.
 | 		//don't update or insert events from the past.
 | ||||||
| 		if($offsetEnd > time()) { | 		if($offsetStart > time()) { | ||||||
| 				 | 
 | ||||||
| 			$args['EventStartDate'] = date("m/d/Y", $offsetStart); | 			$args['EventStartDate'] = date("Y-m-d", $offsetStart); | ||||||
| 			$args['EventStartHour'] = date("H", $offsetStart); | 			$args['EventStartHour'] = date("H", $offsetStart); | ||||||
| 			$args['EventStartMinute'] = date("i", $offsetStart); | 			$args['EventStartMinute'] = date("i", $offsetStart); | ||||||
| 			 | 
 | ||||||
| 			$args['EventEndDate'] = date("m/d/Y", $offsetEnd); | 			$args['EventEndDate'] = date("Y-m-d", $offsetEnd); | ||||||
| 			$args['EventEndHour'] = date("H", $offsetEnd); | 			$args['EventEndHour'] = date("H", $offsetEnd); | ||||||
| 			$args['EventEndMinute'] = date("i", $offsetEnd); | 			$args['EventEndMinute'] = date("i", $offsetEnd); | ||||||
| 	 | 
 | ||||||
| 			$args['post_content'] = $event['description']; | 			$args['post_content'] = $event['description']; | ||||||
| 			$args['Venue']['Venue'] = $event['location']; | 			$args['Venue']['Venue'] = $event['location']; | ||||||
| 			 | 
 | ||||||
| 			$args['post_status'] = "Publish"; | 			$args['post_status'] = "Publish"; | ||||||
| 			$args['post_type'] = "tribe_events"; | 			$args['post_type'] = "tribe_events"; | ||||||
| 			//$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4
 | 			//$args['to_ping'] = $event['eid']; //damn you, sanitize_trackback_urls in 3.4
 | ||||||
| 			$args['to_ping'] = 'https://www.facebook.com/events/'.$event['eid'].'/'; | 			$args['to_ping'] = 'https://www.facebook.com/events/'.$event['id'].'/'; | ||||||
| 			 |  | ||||||
| 			if($args['EventStartHour'] == '22' && $event['uid'] == '256763181050120') { //why are UT events 2 hours off???
 |  | ||||||
| 				$args['EventStartHour'] = '20'; |  | ||||||
| 				$args['EventEndHour'] = '22'; |  | ||||||
| 				$args['EventEndDate'] = date('m/d/Y',strtotime($args['EventEndDate'], '-1 day')); |  | ||||||
| 			} |  | ||||||
| 
 | 
 | ||||||
| 			$inserting = $post_id = false; | 			$inserting = $post_id = false; | ||||||
| 			if (!array_key_exists($event['eid'], $eids)) { | 			if (!array_key_exists($event['id'], $eids)) { | ||||||
| 				//double check
 | 				//double check
 | ||||||
| 				$already_exists = false; | 				$already_exists = false; | ||||||
| 				foreach($query->posts as $post) { | 				foreach($query->posts as $post) { | ||||||
|  | @ -166,15 +170,17 @@ function fbes_send_events($events) { | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			if(!$inserting) { | 			if(!$inserting) { | ||||||
| 				$post_id = $eids[$event['eid']]; | 				$post_id = $eids[$event['id']]; | ||||||
|  | 				echo "$post_id"; | ||||||
| 				tribe_update_event($post_id, $args); | 				tribe_update_event($post_id, $args); | ||||||
| 				echo "<br />Updating: ".$eids[$event['eid']]; | 				echo "<br />Updating: ".$eids[$event['id']]; | ||||||
| 			} | 			} | ||||||
| 			if($post_id)  | 			if($post_id) | ||||||
| 				update_metadata('post', $post_id, 'fb_event_obj', $event); | 				update_metadata('post', $post_id, 'fb_event_obj', $event); | ||||||
| 				//eid, name, start_time, end_time, location, description
 | 				//eid, name, start_time, end_time, location, description
 | ||||||
| 		} | 		} | ||||||
| 		reset($eids); | 		if($eids) | ||||||
|  | 			reset($eids); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -188,10 +194,10 @@ function fbes_options_page() { | ||||||
| 	$fbes_api_uid = get_option('fbes_api_uid'); | 	$fbes_api_uid = get_option('fbes_api_uid'); | ||||||
| 	$fbes_api_uids = get_option('fbes_api_uids'); | 	$fbes_api_uids = get_option('fbes_api_uids'); | ||||||
| 	$fbes_frequency = get_option('fbes_frequency'); | 	$fbes_frequency = get_option('fbes_frequency'); | ||||||
| 	 | 
 | ||||||
| 	#Get new updated option values, and save them
 | 	#Get new updated option values, and save them
 | ||||||
| 	if( !empty($_POST['update']) ) { | 	if( !empty($_POST['update']) ) { | ||||||
| 	 | 
 | ||||||
| 		$fbes_api_key = $_POST['fbes_api_key']; | 		$fbes_api_key = $_POST['fbes_api_key']; | ||||||
| 		update_option('fbes_api_key', $fbes_api_key); | 		update_option('fbes_api_key', $fbes_api_key); | ||||||
| 
 | 
 | ||||||
|  | @ -203,8 +209,9 @@ function fbes_options_page() { | ||||||
| 
 | 
 | ||||||
| 		$fbes_frequency = $_POST['fbes_frequency']; | 		$fbes_frequency = $_POST['fbes_frequency']; | ||||||
| 		update_option('fbes_frequency', $fbes_frequency); | 		update_option('fbes_frequency', $fbes_frequency); | ||||||
| 		 | 
 | ||||||
| 		$events = fbes_get_events($fbes_api_key, $fbes_api_secret, $fbes_api_uids); | 		$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); | 		update_schedule($fbes_frequency); | ||||||
| 
 | 
 | ||||||
|  | @ -218,17 +225,17 @@ function fbes_options_page() { | ||||||
| 			$fbes_api_uids[] = $_POST['fbes_api_uid']; | 			$fbes_api_uids[] = $_POST['fbes_api_uid']; | ||||||
| 			update_option('fbes_api_uids', $fbes_api_uids); | 			update_option('fbes_api_uids', $fbes_api_uids); | ||||||
| 		} | 		} | ||||||
| 		 | 
 | ||||||
| 	} elseif( !empty($_GET['r']) ) { | 	} elseif( !empty($_GET['r']) ) { | ||||||
| 		 | 
 | ||||||
| 		foreach ($fbes_api_uids as $key => $value) | 		foreach ($fbes_api_uids as $key => $value) | ||||||
| 			if($fbes_api_uids[$key] == $_GET['r']) | 			if($fbes_api_uids[$key] == $_GET['r']) | ||||||
| 				unset($fbes_api_uids[$key]); | 				unset($fbes_api_uids[$key]); | ||||||
| 				 |  | ||||||
| 		update_option('fbes_api_uids', $fbes_api_uids); |  | ||||||
| 	}	 |  | ||||||
| 
 | 
 | ||||||
| 	require 'facebook-php-sdk/facebook.php'; //modified
 | 		update_option('fbes_api_uids', $fbes_api_uids); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	require_once 'facebook-php-sdk/facebook.php'; //modified
 | ||||||
| 
 | 
 | ||||||
| 	$facebook = new Facebook(array( | 	$facebook = new Facebook(array( | ||||||
| 		'appId'  =>  $fbes_api_key, | 		'appId'  =>  $fbes_api_key, | ||||||
|  | @ -246,16 +253,16 @@ function fbes_options_page() { | ||||||
| 		<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>"> | 		<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>"> | ||||||
| 		<input type="hidden" name="update" /> | 		<input type="hidden" name="update" /> | ||||||
| 		<?php | 		<?php | ||||||
| 		echo '<form action="'. $_SERVER["REQUEST_URI"] .'" method="post"><table style="width:475px;">';  | 		echo '<form action="'. $_SERVER["REQUEST_URI"] .'" method="post"><table style="width:475px;">'; | ||||||
| 		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 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>'; | 		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>'; | ||||||
| 
 | 
 | ||||||
| 		echo '<tr><td>Update Fequency:</td><td><select id="fbes_frequency" name="fbes_frequency">';		 | 		echo '<tr><td>Update Fequency:</td><td><select id="fbes_frequency" name="fbes_frequency">'; | ||||||
| 		if(htmlentities($fbes_frequency)=="daily") { | 		if(htmlentities($fbes_frequency)=="daily") { | ||||||
| 			echo '<option value="daily" SELECTED>Daily</option>'; | 			echo '<option value="daily" SELECTED>Daily</option>'; | ||||||
| 		} else { | 		} else { | ||||||
| 			echo '<option value="daily">Daily</option>'; | 			echo '<option value="daily">Daily</option>'; | ||||||
| 		}	 | 		} | ||||||
| 		if(htmlentities($fbes_frequency)=="twicedaily") { | 		if(htmlentities($fbes_frequency)=="twicedaily") { | ||||||
| 			echo '<option value="twicedaily" SELECTED>Twice Daily</option>'; | 			echo '<option value="twicedaily" SELECTED>Twice Daily</option>'; | ||||||
| 		} else { | 		} else { | ||||||
|  | @ -267,7 +274,7 @@ function fbes_options_page() { | ||||||
| 			echo '<option value="hourly">Hourly</option>'; | 			echo '<option value="hourly">Hourly</option>'; | ||||||
| 		} | 		} | ||||||
| 		echo '</select>'; | 		echo '</select>'; | ||||||
| 		 | 
 | ||||||
| 		echo '<tr><td>Add Facebook Page UID:</td><td><input type="text" id="fbes_api_uid" name="fbes_api_uid" value="" size="15" />'; | 		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 '<input type="submit" value="Add" class="button-secondary" name="add-uid" /></td></tr>'; | ||||||
| 
 | 
 | ||||||
|  | @ -277,9 +284,9 @@ function fbes_options_page() { | ||||||
| 			if($value!='') | 			if($value!='') | ||||||
| 		    	echo '  '.$value.'  <a href="'.$_SERVER["REQUEST_URI"].'&r='.$value.'">remove</a><br />'; | 		    	echo '  '.$value.'  <a href="'.$_SERVER["REQUEST_URI"].'&r='.$value.'">remove</a><br />'; | ||||||
| 		} | 		} | ||||||
| 		 | 
 | ||||||
| 		echo '</td></tr>'; | 		echo '</td></tr>'; | ||||||
| 		 | 
 | ||||||
| 		echo '<tr><td colspan="2"></td></tr><tr><td colspan="2"><br /><input type="submit" value="Update" class="button-primary"'; | 		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>'; | 		echo ' name="update" /></td></tr></table>'; | ||||||
| 		?>
 | 		?>
 | ||||||
|  | @ -306,7 +313,7 @@ function fbes_options_page() { | ||||||
| 		<span style="color:red;">Events Calendar updated with current Facebook events.</span><br /><br /> | 		<span style="color:red;">Events Calendar updated with current Facebook events.</span><br /><br /> | ||||||
| 		</div> | 		</div> | ||||||
| 	<?php } ?>
 | 	<?php } ?>
 | ||||||
| <?php	 | <?php | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function curPageURL() { | function curPageURL() { | ||||||
|  |  | ||||||
		Reference in New Issue