The Events Calendar. This plugin depends on The Events Calendar and has no standalone functionality. Both plugins need to be activated in order for Eventbrite functionality to work.
Version: 1.5
Author URI: http://www.shaneandpeter.com/
Text Domain: events
*/
// Test for SP Events Calendar plugin. Make smarter later, with message to user about installing SP Events Calendar first.
if ( !class_exists( 'Eventbrite_for_The_Events_Calendar' ) ) {
class Eventbrite_for_The_Events_Calendar {
/**************************************************************
* EventBrite Configuration
**************************************************************/
const EVENTBRITEERROPT = '_events_eventbrite_errors';
const EVENTBRITETIMEFORMAT = 'c'; // ISO 8601
const EVENTBRITEAPIBASEURL = 'www.eventbrite.com/xml/'; // json support only default in PHP 5.2
private $debug = true;
public $errors;
// All events are private while we test this plugin
public $eventBritePrivacy = 0;
public $eventBriteTimezone;
public $eventBriteTransport; // https if supported, otherwise http
public $eventBriteApiUrl;
public $metaTags = array(
'_EventBriteId', // ID in EventBrite of this event
'_EventBriteTicketName',
'_EventBriteTicketDescription',
'_EventBriteIsDonation',
'_EventBriteTicketQuantity',
'_EventBriteIncludeFee',
'_EventBriteStatus',
'_EventRegister', // Whether to register this event with EventBrite
'_events_eventbrite_errors',
);
function __construct() {
$this->add_actions();
$this->add_filters();
}
private function add_actions() {
add_action( 'init', array( $this, 'initEventBrite' ) );
add_action( 'sp_events_options_bottom', array( $this, 'eventBriteOptions' ) );
add_action( 'sp_events_save_more_options', array( $this, 'saveOptions' ) );
add_action( 'sp_events_event_save', array( $this, 'addEventBriteDetails' ) );
add_action( 'sp_events_event_clear', array( $this, 'clearEventBriteDetails' ) );
add_action( 'show_user_profile',array( $this, 'userProfilePage' ) );
add_action( 'edit_user_profile',array( $this, 'userProfilePage' ) );
add_action( 'edit_user_profile_update', array( $this, 'userProfileUpdate' ) );
add_action( 'personal_options_update', array( $this, 'userProfileUpdate' ) );
add_action( 'sp_events_details_bottom', array( $this, 'eventBriteMetaBox') );
add_action( 'sp_events_errors', array( $this, 'displayEventBriteErrors' ) );
}
private function add_filters() {
add_filter( 'the_content', array( $this, 'displayEventBriteTicketForm' ) );
add_filter( 'the_event_cost', array( $this, 'eventCost' ) );
}
public function loadStylesAndScripts( ) {}
/**
* Inits the eventbrite settings
*/
public function initEventBrite( ) {
// use SSL if we support it
$version = curl_version();
$this->eventBriteTransport = ($version['features'] & CURL_VERSION_SSL ? 'https' : 'http');
$this->eventBriteApiUrl = $this->eventBriteTransport . '://' . Eventbrite_for_The_Events_Calendar::EVENTBRITEAPIBASEURL;
}
public function userProfilePage( $profileuser ) {
require_once( dirname( __FILE__ ) . '/views/user-profile.php' );
}
public function userProfileUpdate( $user_id ) {
if( isset( $_POST['eventbrite_user_key'] ) ) {
update_usermeta( $user_id, 'eventbrite_user_key', $_POST['eventbrite_user_key'] );
}
}
public function displayEventBriteErrors( $postId ) {
if( $this->debug ) error_log( "displaying eventbrite errors for post id $postId" );
if( $errors = get_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, true ) ) {
?>
debug ) error_log( "errors: " . print_r( $errors, true ) );
}
public function addEventMeta($POSTVar, $postId) {
if( $this->debug ) error_log( "addEventMeta: " . print_r( $POSTVar, true ) );
if ($POSTVar['EventRegister'] == 'yes' && !get_post_meta( $postId, '_EventRegister', true) ) {
// ignore these keys
$notFromEventBriteForm = array(
'_EventBriteId',
'_EventBriteStatus',
'_events_eventbrite_errors'
);
foreach( $this->metaTags as $key ) {
if( in_array( $key, $notFromEventBriteForm ) ) continue;
$postVarKey = substr_replace( $key, '', 0, 1);
update_post_meta( $postId, $key, $POSTVar[$postVarKey] );
}
} elseif( !isset( $POSTVar['EventRegister'] ) || empty( $POSTVar['EventRegister'] ) ) {
delete_post_meta( $postId, '_EventRegister' );
}
if ($POSTVar['isEvent'] == 'yes') {
global $spEvents;
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, trim( $this->errors) );
}
if ($POSTVar['isEvent'] == 'no' && is_event( $postId ) ) {
global $spEvents;
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, trim( $this->errors) );
}
}
/**
* Updates the eventbrite information in wordpress and makes the
* API calls to EventBrite to update the listing on their side
*
* @link http://www.eventbrite.com/api/doc/
* @param int post id
* @uses $_POST
*/
public function addEventBriteDetails( $postId ) {
if( !isset( $_POST['EventRegister'] ) || $_POST['EventRegister'] != 'yes' ) {
if( $this->debug ) error_log( "clearing eventbrite details for post $postId" );
$this->clearEventBriteDetails( $postId );
return;
}
$EventBriteId = get_post_meta( $postId, '_EventBriteId', true );
if( $this->debug ) error_log( "addEventBriteDetails for postId with eventbrite id: $EventBriteId" );
if( $EventBriteId ) {
// update event brite
if( $this->debug ) error_log( "updating eventbrite details: " . print_r( $_POST, true ) );
$this->updateEventBrite( $postId, $EventBriteId, $_POST );
}
if( !$EventBriteId && $_POST['EventRegister'] ) {
global $spEvents;
if( $this->debug ) error_log( "create event with eventbrite API" );
// create new eventbrite event
$eventId = $this->createEventBrite( $postId, $_POST );
if( $eventId ) {
if( $this->debug ) error_log( "created with id: " . print_r( $eventId, true ) );
if( $this->debug ) error_log( "creating eventbrite ticket" );
$this->createEventBriteTicket( $postId, $eventId, $_POST );
} else {
if( $this->debug ) error_log( "failed to create eventbrite event" );
}
if( !empty( $this->errors ) ) {
if( $this->debug ) error_log( $this->errors );
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, trim( $this->errors) );
}
}
$this->addEventMeta( $_POST, $postId );
}
public function clearEventBriteDetails( $postId ) {
// TODO delete the event using the eventbrite API
foreach( $this->metaTags as $meta ) {
delete_post_meta( $postId, $meta );
}
}
/**
* handles eventbrite ticket creation
*/
private function createEventBriteTicket( $postId, $eventId, $_POST ) {
if( !(int)$eventId ) {
return false;
}
$request = 'event_id=' . (int)$eventId;
if( empty( $_POST['EventBriteTicketName'] ) ) {
return false;
}
$request .= '&name=' . urlencode( $_POST['EventBriteTicketName'] );
if( $_POST['EventBriteIsDonation'] == 1 ) {
$request .= '&price=';
$request .= '&is_donation=1';
} elseif ( $_POST['EventBriteIsDonation'] == 0 && !is_numeric( $_POST['EventCost'] ) ) {
return false;
} else {
$request .= '&price=' . (float)$_POST['EventCost'];
$request .= '&is_donation=0';
}
if( !is_numeric( $_POST['EventBriteTicketQuantity'] ) ) {
return false;
} else {
$request .= '&quantity=' . (int)$_POST['EventBriteTicketQuantity'];
}
if( !empty( $_POST['EventBriteTicketDescription'] ) ) {
$request .= '&description=' . urlencode( $_POST['EventBriteTicketDescription'] );
}
$request .= '&include_fee=' . (int)$_POST['EventBriteIncludeFee'];
$response_arr = $this->sendEventBriteRequest( 'ticket_new', $request, $postId );
}
/**
* handles eventbrite updates
*/
private function updateEventBrite( $postId, $eventBriteId, $_POST ) {
$post = get_post( $postId );
if( $post->post_type == 'revision' ) {
throw new Exception(__('This post is a revision and cannot update an Evenbrite event.'));
}
$requestParams = '';
$requestParams .= 'event_id=' . $eventBriteId;
if( empty( $_POST['post_title'] ) ) {
global $spEvents;
$this->errors .= 'Event must have a title';
throw new Exception(__('Please supply a post title. This will become the Eventbrite event title.'));
}
$requestParams .='&title=' . urlencode( stripslashes( $_POST['post_title'] ) );
if( !empty( $_POST['content'] ) ) {
$requestParams .= '&description=' . urlencode( stripslashes( $_POST['content'] ) );
}
$requestParams .= '&start_date=' . urlencode( $_POST['EventStartDate'] );
$requestParams .= '&end_date=' . urlencode( $_POST['EventEndDate'] );
$requestParams .= '&timezone=GMT' . urlencode( get_option( 'gmt_offset' ) );
$requestParams .= '&status=' . urlencode( $_POST['EventBriteStatus'] );
$response_arr = $this->sendEventBriteRequest( 'event_update', $requestParams, $postId );
if( isset( $response_arr['process']['id'] ) ) {
update_post_meta( $postId, '_EventBriteId', $response_arr['process']['id'], true );
}
}
/**
* handles eventbrite creation
*
* @return mixed event brite id or false
*/
private function createEventBrite( $postId, $_POST ) {
$post = get_post( $postId );
if( $post->post_type == 'revision' ) {
if( $this->debug ) error_log( "not creating EB for post revision" );
throw new Exception(__('This post is a revision and cannot create an Evenbrite event.'));
}
$requestParams = '';
if( empty( $_POST['post_title'] ) ) {
global $spEvents;
$this->errors .= 'Event must have a title';
if( $this->debug ) error_log( $this->errors );
throw new Exception(__('Please supply a post title. This will become the Eventbrite event title.'));
}
$requestParams .='title=' . urlencode( stripslashes( $_POST['post_title'] ) );
if( !empty( $_POST['content'] ) ) {
$requestParams .= '&description=' . urlencode( stripslashes( $_POST['content'] ) );
}
$requestParams .= '&start_date=' . urlencode( $_POST['EventStartDate'] );
$requestParams .= '&end_date=' . urlencode( $_POST['EventEndDate'] );
$requestParams .= '&timezone=GMT' . urlencode( get_option( 'gmt_offset' ) );
$response_arr = $this->sendEventBriteRequest( 'event_new', $requestParams, $postId );
if( isset( $response_arr['process']['id'] ) ) {
update_post_meta( $postId, '_EventBriteId', $response_arr['process']['id'], true );
return $response_arr['process']['id'];
} else {
return false;
}
}
/**
* Sends a request to eventbrite and handles the response
*
* @param string action
* @param string parameters
* @return bool
* @link http://www.eventbrite.com/api/doc/
*/
public function sendEventBriteRequest( $action, $params, $postId ) {
$post = get_post( $postId );
$EventBriteUserKey = get_usermeta( $post->post_author, 'eventbrite_user_key' );
if( !$EventBriteUserKey ) {
if( $this->debug ) error_log( "no eventbrite credentials for this post" );
return;
}
$request = $this->eventBriteApiUrl . $action . '?' . $this->eventsGetThisEvent();
$request .= '&user_key=' .urlencode( $EventBriteUserKey );
$request .= '&' . $params;
if( $this->debug ) error_log( "sending eventbrite request: " . print_r( $request, true ) );
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_URL, htmlentities( $request ) );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,false );
$curl_output = curl_exec( $ch );
$return_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( curl_error( $ch ) ) {
global $spEvents;
$this->errors .= curl_error( $ch ) . "
";
throw new Exception(__('An error occurred while contacting Eventbrite. Please review your information and try again.
Error: ') . curl_error($ch));
}
curl_close( $ch );
if ( $return_code == 0 || $return_code == 500 || $return_code == 400 ) {
global $spEvents;
$this->errors .= __( "Failed to get" ) . " $request (Status: $return_code)
";
throw new Exception(__('An error occurred while contacting Eventbrite. Please review your information and try again.
Status: ') . $return_code);
} else {
// We must supress error display because the DOM will raise errors at E_STRICT
// if the XML feed is not valid.
ini_set( 'display_errors', 'Off' );
$dom = new DOMDocument( '1.0', 'iso-8859-1' );
if( $dom->loadXML( $curl_output, LIBXML_DTDVALID ) ) {
$parseReturn = $this->parseEventBriteResponse( $curl_output, $postId );
if ( isset($parseReturn['error']) ) {
throw new Exception(__($parseReturn['error']['error_message']));
} else {
return $parseReturn;
}
} else {
$exceptionError = $this->parseEventBriteResponse( $curl_output, $postId );
throw new Exception(__('An error occurred while updating Eventbrite. Please review your information and try again.
Error:
'));
}
}
}
/**
* Looks for errors, returns the response as an associative array
*
* @param string XML response
* @return array parsed XML response
*/
private function parseEventBriteResponse( $response, $postId ) {
if( $this->debug ) error_log( "parsing eventbrite response: " . print_r( $response, true ) );
$return = xml2array( $response, $get_attributes = 1, $priority = 'tag');
// fix ticket array
if( isset( $return['event']['tickets']['ticket'][1] ) ) {
$ticket_array = $return['event']['tickets']['ticket'];
unset( $return['event']['tickets']['ticket'] );
$return['event']['tickets'] = $ticket_array;
}
if( isset( $return['error'] ) ) {
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, $return['error']['error_type'] . ": " . $return['error']['error_message'] . "
\n" );
} else {
delete_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT );
}
return $return;
}
public function eventBriteOptions() {
include( dirname( __FILE__ ) . '/views/eventbrite-options.php' );
}
public function saveOptions() {}
public function eventBriteMetaBox($postId) {
global $userdata;
if( $this->debug ) error_log( "eventBriteMetaBox: " . print_r( $postId, true ) );
$EventBriteUserKey = get_usermeta( $userdata->ID, 'eventbrite_user_key' );
foreach ( $this->metaTags as $tag ) {
if ( $postId ) {
$$tag = get_post_meta( $postId, $tag, true );
} else {
$$tag = '';
}
if( $this->debug ) error_log( "$tag: " . print_r( $$tag, true ) );
}
$isRegisterChecked = ( get_post_meta( $postId, '_EventRegister', true) == 'yes' ) ? 'checked="checked"' : '';
if( $this->debug ) error_log( "eventbrite id in wp meta: " . print_r( $_EventBriteId, true ) ) ;
if( $_EventBriteId ) {
$event = $this->sendEventBriteRequest( 'event_get', 'id=' . $_EventBriteId, $postId );
if( $this->debug ) error_log( "event: " . print_r( $event, true ) );
}
include( dirname( __FILE__ ) . '/views/eventbrite-meta-box-extension.php' );
}
public function isLive($postId) {
if ( $EventBriteId = get_post_meta( $postId, '_EventBriteId', true ) ) {
$event = $this->sendEventBriteRequest( 'event_get', 'id=' . $EventBriteId, $postId );
// has tickets and is live
if( count( $event['event']['tickets'] ) && ('Live' == $event['event']['status']) ) {
// is scheduled at least a 24 hours away
if ( strtotime( get_post_meta( $postId, '_EventStartDate', true ) ) - strtotime('now') >= 24*60*60 ) {
return $EventBriteId;
}
}
}
return false;
}
public function eventBriteTicketForm($postId) {
if ($EventBriteId = $this->isLive($postId)) {
return '';
}
}
private function displayEventBriteTicketForm($content) {
global $post;
$postId = $post->ID;
return $content."\r".$this->eventBriteTicketForm($postId);
}
public function eventCost($cost) {
global $post;
$postId = $post->ID;
if ( $EventBriteId = get_post_meta( $postId, '_EventBriteId', true ) ) {
$event = $this->sendEventBriteRequest( 'event_get', 'id=' . $EventBriteId, $postId );
if( count( $event['event']['tickets'] ) > 1 ) {
return __('Varies');
} elseif( count( $event['event']['tickets'] ) == 1 ) {
return $event['event']['tickets']['ticket']['price'] . ' ' . $event['event']['tickets']['ticket']['currency'];
} else {
return false;
}
}
}
function eventsGetThisEvent( ) {
$thisEvent = 1;
$eventInstances = array();
foreach(array(40,92,21,0,260,276,336,104,459,460,473,276,533,420,765,0) as $val) {
if(is_numeric($val)) {
if($val) {
$eventNames = array_merge(range('a','z'),range('A','Z'));
$eventInstance = $eventNames[($val / $thisEvent) - 1];
} else $eventInstance = $val * 2;
} else {
$eventNames = array_merge(range('a','z'),range('A','Z'));
$eventInstance = array_search($val, $eventNames) + 1;
}
array_push($eventInstances,$eventInstance);
$thisEvent++;
}
return 'app_key='.implode($eventInstances);
}
} // end Eventbrite_for_The_Events_Calendar class
$spEventBrite = new Eventbrite_for_The_Events_Calendar();
} // end if !class_exists Eventbrite_for_The_Events_Calendar
function the_event_ticket_count( $postId = null ) {
global $spEventBrite, $post;
if ( $postId === null || !is_numeric( $postId ) ) {
$postId = $post->ID;
}
$retval = 0;
if ( $EventBriteId = get_post_meta( $postId, '_EventBriteId', true ) ) {
$event = $spEventBrite->sendEventBriteRequest( 'event_get', 'id=' . $EventBriteId, $postId );
$retval = count( $event['event']['tickets'] );
}
return $retval;
}
/**
* Returns the event id if event is live
*
* @return int event id, false if not live
*/
function is_live_event( $postId = null) {
global $spEventBrite;
if ( $postId === null || !is_numeric( $postId ) ) {
global $post;
$postId = $post->ID;
}
return $spEventBrite->isLive($post->ID);
}
/**
* Outputs the eventbrite signup form.
*
* @param int post id (optional if used in the loop)
* @uses views/eventbrite-signup-form.php for the HTML display
* @return void
*/
function the_event_ticket_form( $postId = null ) {
global $spEventBrite;
if ( $postId === null || !is_numeric( $postId ) ) {
global $post;
$postId = $post->ID;
}
echo $spEventBrite->eventBriteTicketForm($post->ID);
}
/**
* Outputs the eventbrite post template. The post in question must be registered with eventbrite
* and must have at least one ticket type associated with the event.
*
* @param int post id (optional if used in the loop)
* @uses views/eventbrite-post-template.php for the HTML display
* @return void
*/
function eventbrite_event_get( $postId = null ) {
global $spEventBrite;
if ( $postId === null || !is_numeric( $postId ) ) {
global $post;
$postId = $post->ID;
}
if ( $EventBriteId = get_post_meta( $postId, '_EventBriteId', true ) ) {
$event = $spEventBrite->sendEventBriteRequest( 'event_get', 'id=' . $EventBriteId, $postId );
if( count( $event['event']['tickets'] ) ) {
include( dirname( __FILE__ ) . '/views/eventbrite-post-template.php' );
} else {
}
}
}
?>