Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Last active February 28, 2018 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryhughes/28bce63adaec3d7bd14930309c53ff16 to your computer and use it in GitHub Desktop.
Save barryhughes/28bce63adaec3d7bd14930309c53ff16 to your computer and use it in GitHub Desktop.
Example/starting point for custom import jobs using Eventbrite Tickets 4.4.8
<?php
/**
* Example of searching Eventbrite for events and importing them via
* Eventbrite Tickets.
*
* By design, Eventbrite Tickets 4.4.x allows importing from whichever user
* account it has been linked with as well as individual events belonging to
* other accounts, which can be specified via their ID or URL.
*
* This is an example of searching for and importing a broader swathe of
* events than those and could be set up to run via a scheduled event or on
* demand via some other trigger.
*
* It's just a starting point! It does not impose any limit on the number of
* events it attempts to import in a single run. Additionally, it was
* written to work with Eventbrite Tickets 4.4.8. Your mileage may vary with
* other versions, particularly the next major version.
*
* @see https://theeventscalendar.com/product/wordpress-eventbrite-tickets/
*/
/**
* Performs a custom search using the Eventbrite Tickets API.
*
* @param $search_term
*
* @return object|WP_Error
*/
function custom_eventbrite_search( $search_term ) {
if ( ! class_exists( 'Tribe__Events__Tickets__Eventbrite__API' ) ) {
return new WP_Error;
}
$api = Tribe__Events__Tickets__Eventbrite__API::instance();
if ( ! $api->is_ready() ) {
return new WP_Error;
}
$url = $api->get_base_url( 'events/search' );
return $api->request( $url, [ 'q' => $search_term ], 'get' );
}
/**
* Given an array of Eventbrite event objects, imports each event.
*
* @param array $events
*
* @return int|WP_Error
*/
function custom_eventbrite_import( array $events ) {
if ( ! class_exists( 'Tribe__Events__Tickets__Eventbrite__Main' ) ) {
return new WP_Error;
}
$count = 0;
try {
$eventbrite = Tribe__Events__Tickets__Eventbrite__Main::instance();
foreach ( $events as $event ) {
// In Eventbrite Tickets 4.4.8 and earlier, there is no way to pass the
// event object across: we need to communicate the event we wish to import
// by setting the following superglobal
$_REQUEST['eventbrite_id'] = $event->id;
$eventbrite->import_existing_events();
$count++;
}
}
catch ( Exception $e ) {
return new WP_Error;
}
return $count;
}
// Search for events mentioning "dance"
$search_results = custom_eventbrite_search( 'dance' );
if (
is_object( $search_results )
&& property_exists( $search_results, 'events' )
&& ! empty( $search_results->events )
) {
custom_eventbrite_import( $search_results->events );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment