The Events Calendar. This plugin depends on The Events Calendar. Both plugins need to be activated in order for Eventbrite functionality to work although you may use this plugin to display Eventbrite widgets via shortcode in your content without The Events Calendar.
Version: 1.5.4
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
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 $pluginDir;
public $pluginDomain = 'eventbrite-for-the-events-calendar';
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->pluginDir = basename(dirname(__FILE__));
$this->add_actions();
$this->add_filters();
$this->add_shortcodes();
}
private function add_actions() {
add_action( 'init', array( $this, 'initEventBrite' ) );
add_action( 'init', array( $this, 'loadPluginTextDomain' ) );
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_update_meta', array( $this, 'addEventMeta' ) );
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_above_donate', array( $this, 'eventBriteMetaBox') );
add_action( 'sp_events_errors', array( $this, 'displayEventBriteErrors' ) );
}
private function add_filters() {
add_filter( 'the_content', array( $this, 'displayEventBriteTicketForm' ) );
}
private function add_shortcodes() {
add_shortcode( 'eventbrite' , array( $this, 'shortCodeforTickets' ) );
}
public function loadPluginTextDomain() {
load_plugin_textdomain( $this->pluginDomain, false, $this->pluginDir . '/lang/');
}
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( WP_DEBUG ) error_log( "displaying eventbrite errors for post id $postId" );
if( $errors = get_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, true ) ) {
?>
metaTags as $key ) {
if( in_array( $key, $notFromEventBriteForm ) ) continue;
$postVarKey = substr_replace( $key, '', 0, 1);
update_post_meta( $postId, $key, $_POST[$postVarKey] );
}
} elseif( !isset( $_POST['EventRegister'] ) || empty( $_POST['EventRegister'] ) ) {
delete_post_meta( $postId, '_EventRegister' );
}
// TODO necessary?
/*
if ($_POST['isEvent'] == 'yes') {
global $spEvents;
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, trim( $this->errors) );
}
if ($_POST['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( WP_DEBUG ) error_log( "clearing eventbrite details for post $postId" );
$this->clearEventBriteDetails( $postId );
return;
}
$EventBriteId = get_post_meta( $postId, '_EventBriteId', true );
if( WP_DEBUG ) error_log( "addEventBriteDetails for postId with eventbrite id: $EventBriteId" );
if( $EventBriteId ) {
// update event brite
if( WP_DEBUG ) error_log( "updating eventbrite details: " . print_r( $_POST, true ) );
$this->updateEventBrite( $postId, $EventBriteId, $_POST );
}
if( !$EventBriteId && $_POST['EventRegister'] ) {
global $spEvents;
if( WP_DEBUG ) error_log( "create event with eventbrite API" );
// create new eventbrite event
$eventId = $this->createEventBrite( $postId, $_POST );
if( $eventId ) {
if( WP_DEBUG ) error_log( "created with id: " . print_r( $eventId, true ) );
if( WP_DEBUG ) error_log( "creating eventbrite ticket" );
$this->createEventBriteTicket( $postId, $eventId, $_POST );
} else {
if( WP_DEBUG ) error_log( "failed to create eventbrite event" );
}
if( !empty( $this->errors ) ) {
if( WP_DEBUG ) error_log( $this->errors );
update_post_meta( $postId, Eventbrite_for_The_Events_Calendar::EVENTBRITEERROPT, trim( $this->errors) );
}
}
}
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( WP_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( WP_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( WP_DEBUG ) error_log( "no eventbrite credentials for this post" );
return;
}
$request = $this->eventBriteApiUrl . $action . '?' . $this->eventsGetThisEvent();
$request .= '&user_key=' .urlencode( $EventBriteUserKey );
$request .= '&' . $params;
if( WP_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 );
if( !ini_get('safe_mode') ) {
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 );
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( WP_DEBUG ) error_log( "parsing eventbrite response: " . print_r( $response, true ) );
$return = spEBxml2Array( $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'] ) ) {
throw new Exception(__($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( WP_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( WP_DEBUG ) error_log( "$tag: " . print_r( $$tag, true ) );
}
if( WP_DEBUG ) error_log( "eventbrite id in wp meta: " . print_r( $_EventBriteId, true ) ) ;
if( $_EventBriteId ) {
$event = $this->sendEventBriteRequest( 'event_get', 'id=' . $_EventBriteId, $postId );
if( WP_DEBUG ) error_log( "event: " . print_r( $event, true ) );
}
$isRegisterChecked = ( isset( $event['event']['status'] ) && $event['event']['status'] == 'Draft' || $event['event']['status'] == 'Live' ) ? true : false;
include( dirname( __FILE__ ) . '/views/eventbrite-meta-box-extension.php' );
}
public function isLive( $postId = false ) {
if (!$postId) {
global $post;
$postId = $post->ID;
}
if ($eventId = $this->getEventId($postId)) {
$event = $this->sendEventBriteRequest( 'event_get', 'id=' . $eventId, $postId );
if (WP_DEBUG) error_log( "event\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( $event, true ) . "\r" );
// has tickets and is live
if( count( $event['event']['tickets'] ) && ('Live' == $event['event']['status']) ) {
// is scheduled in the future
if ( strtotime( get_post_meta( $postId, '_EventEndDate', true ) ) > strtotime('now') ) {
return true;
}
}
}
return false;
}
public function getEventId( $postId = false ) {
if (!$postId) {
global $post;
$postId = $post->ID;
}
if( WP_DEBUG ) error_log( "postId\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( 'live:'.$postId, true ) . "\r" );
if ( $EventBriteId = get_post_meta( $postId, '_EventBriteId', true ) ) {
if( WP_DEBUG ) error_log( "EventBriteId\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( $EventBriteId, true ) . "\r" );
return $EventBriteId;
}
return false;
}
public function eventBriteTicketForm( $eventId = false, $width = false, $height = false ) {
if (!$eventId) {
global $post;
$eventId = $this->getEventId($post->ID);
if( WP_DEBUG ) error_log( "evetnid\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( $eventId, true ) . "\r" );
}
if (!$width) { $width = '100%'; }
if (!$height) { $height = '210'; }
if( WP_DEBUG ) error_log( "live\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( 'live:'.$eventId, true ) . "\r" );
if ($eventId) {
return '';
}
}
public function eventBriteRegistrationForm( $eventId = false, $width = false, $height = false ) {
if (!$eventId) {
global $post;
$eventId = $this->getEventId($post->ID);
}
if (!$width) { $width = '100%'; }
if (!$height) { $height = '600'; }
if( WP_DEBUG ) error_log( "live b\r" . __FILE__ . " Line:" . __LINE__ . "\r" . print_r( 'live:'.$eventId, true ) . "\r" );
if ($eventId) {
return '';
}
}
public function displayEventBriteTicketForm($content) {
global $post;
if ($this->isLive($post->ID)) {
$eventId = $this->getEventId($post->ID);
return $content."\r".$this->eventBriteTicketForm($eventId);
} else {
return $content;
}
}
public function the_event_cost($postId) {
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);
}
// Shortcode accepts eventid, width, and height. If eventid is not set, shortcode will attempt to identify the eventid of the post.
function shortCodeforTickets($atts, $content = null) {
$defaults = array(
'eventid' => false,
'width' => false,
'height' => false,
'type' => 'ticket',
);
$atts = shortcode_atts($defaults, $atts);
switch ($atts['type']) {
case 'registration' :
return $this->eventBriteRegistrationForm($atts['eventid'], $atts['width'], $atts['height']);
case 'ticket' :
default :
return $this->eventBriteTicketForm($atts['eventid'], $atts['width'], $atts['height']);
}
}
} // end Eventbrite_for_The_Events_Calendar class
$spEventBrite = new Eventbrite_for_The_Events_Calendar();
} // end if !class_exists Eventbrite_for_The_Events_Calendar
if( !function_exists('the_event_ticket_count') ) {
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 for the post
*
* @param int post id (optional if used in the loop)
* @return int event id, false if no event is associated with post
*/
function get_event_id( $postId = null) {
global $spEventBrite;
return $spEventBrite->getEventId($postId);
}
/**
* Determine if an event is live
*
* @param int post id (optional if used in the loop)
* @return boolean
*/
function is_live_event( $postId = null) {
global $spEventBrite;
return $spEventBrite->isLive($postId);
}
/**
* Outputs the eventbrite ticket form.
*
* @param int event id (optional if used in the loop)
* @return void
*/
function the_eventbrite_ticket_form( $eventId = false, $width = false, $height = false ) {
global $spEventBrite;
echo $spEventBrite->eventBriteTicketForm($eventId, $width, $height);
}
/**
* Outputs the eventbrite registration form.
*
* @param int event id (optional if used in the loop)
* @return void
*/
function the_eventbrite_registration_form( $eventId = false, $width = false, $height = false ) {
global $spEventBrite;
echo $spEventBrite->eventBriteRegistrationForm($eventId, $width, $height);
}
/**
* 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 {
}
}
}
/**
* Returns the eventbrite attendee data for display
*
* @return string with html event attendees
*/
function eventbrite_event_list_attendees($id, $user, $password) {
$base_url = "https://www.eventbrite.com/xml/event_list_attendees?" . $this->eventsGetThisEvent() . "&user=".$user."&password=".$password."&id=".$id;
// Load the XML with a cURL request
$xml = load_xml($base_url);
if ($xml->error_message != '') { echo $xml->error_message;}
else
{
$cnt = count($xml->attendee);
echo 'For a detailed list of attendees, visit eventbrite.
| Attendee | Paid | Qty | Purchase Date |
';
for($i=0; $i<$cnt; $i++)
{
$firstname = $xml->attendee[$i]->first_name;
$lastname = $xml->attendee[$i]->last_name;
$email = $xml->attendee[$i]->email;
$quantity = $xml->attendee[$i]->quantity;
$created = date_create($xml->attendee[$i]->created);
$currency = $xml->attendee[$i]->currency;
$amount_paid = $xml->attendee[$i]->amount_paid;
echo '| '.$firstname.' '.$lastname.' | '.$currency.' '.$amount_paid.' | '.$quantity.' | '.date_format($created,__("m.d.Y")).' |
';
}
echo "
";
//print_r($xml);
}
return;
}
/**
* spEBxml2Array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/spEBxml2Array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
* Examples: $array = spEBxml2Array(file_get_contents('feed.xml'));
* $array = spEBxml2Array(file_get_contents('feed.xml', 1, 'attribute'));
*/
function spEBxml2Array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}
return($xml_array);
}
} // end if !function_exists('the_event_ticket_count')
?>