edisplay
9/24/2015 - 4:20 PM

get the proper Facebook link for events, venue and organizers. Please note if the id is an event it will build the event by the meta id, how

get the proper Facebook link for events, venue and organizers. Please note if the id is an event it will build the event by the meta id, however the second param gives the option to prettify the link but will require a request to Facebook's API which could slow response

<?php

/**
 * get the proper Facebook link for events, venue and organizers. if the id is an event
 * it will build the event by the meta id, however the second param gives the option to 
 * prettify the link but will require a request to Facebook's API which could slow response
 * @param  int  $post_id
 * @param  boolean $lookup
 * @return string
 */
function tribe_event_get_facebook_link( $post_id = null, $lookup = false ){
  $post_id = TribeEvents::postIdHelper( $post_id );
	$link = '';
	$prefix = 'https://www.facebook.com/';
	if( tribe_is_event( $post_id ) && $event_id = get_post_meta( $post_id, '_FacebookID', true ) ){
		$link = trailingslashit( $prefix . 'events/' . $event_id );
	} elseif ( tribe_is_venue( $post_id ) && $venue_id = get_post_meta( $post_id, '_VenueFacebookID', true ) ){
		if( $lookup ) {
			$venue_object = Tribe_FB_Importer::instance()->get_facebook_object( $venue_id );
			$link = $venue_object->link;
		} else {
			$link = trailingslashit( $prefix . $venue_id );
		}
	} elseif ( TribeEvents::instance()->isOrganizer( $post_id ) && $organizer_id = get_post_meta( $post_id, '_OrganizerFacebookID', true ) ){
		if( $lookup ) {
			$organizer_object = Tribe_FB_Importer::instance()->get_facebook_object( $organizer_id );
			$link = $organizer_object->link;
		} else {
			$link = trailingslashit( $prefix . $organizer_id );
		}
	}
	return !empty( $link ) ? $link : null;
}


/**
 * BELOW IS HOW TO INTEGRATE INTO SINGLE VIEW
 */


// add custom facebook meta items
add_action('tribe_events_before_view','link_to_facebook');
function link_to_facebook(){
  global $post;
	// force this to run only on single event views
	if( is_single() && tribe_is_event( $post->ID ) ){
		$links = '';
		$event_link = tribe_event_get_facebook_link( $post->ID );
		$venue_link = tribe_event_get_facebook_link( tribe_get_venue_id( $post->ID ) );
		$organizer_link = tribe_event_get_facebook_link( tribe_get_organizer_id( $post->ID ) );
		if( !empty($event_link) )
			$links[] = sprintf( '<a href="%s" target="_blank">Event</a>', $event_link );
		if( !empty($venue_link) )
			$links[] = sprintf( '<a href="%s" target="_blank">Venue</a>', $venue_link );
		if( !empty($organizer_link) )
			$links[] = sprintf( '<a href="%s" target="_blank">Organizer</a>', $organizer_link );
		if( !empty( $links ) ) {
			tribe_register_meta( 'tribe_link_to_facebook', array(
				'label' => __( 'Facebook Event:', 'tribe-events-calendar' ),
				'meta_value' => implode( '<br />', $links ),
				'group' => 'tribe_event_details'
			) );
		}
	}
}
 
// add the new custom meta into the template keys for custom templates on single event views
add_action( 'tribe_events_single_event_meta_template_keys', 'custom_meta_keys');
function custom_meta_keys( $keys ){
	$keys[] = 'tribe_link_to_facebook';
  // any new meta items you create, add them here to be included in the template reset by single-event.php
	return $keys;
}