Skip to content

Instantly share code, notes, and snippets.

@slimndap
Last active September 23, 2021 20:28
Show Gist options
  • Select an option

  • Save slimndap/05d442039e8a667ca0d1f152629c1ae7 to your computer and use it in GitHub Desktop.

Select an option

Save slimndap/05d442039e8a667ca0d1f152629c1ae7 to your computer and use it in GitHub Desktop.
Syncs Veezi events with The Event Calendar. For use in conjunction with the Veezi for WordPress plugin.
<?php
function tev_get_venue_id() {
$venue_id = wp_cache_get( 'venue_id', 'tev' );
if ( false === $venue_id ) {
$venue_post = get_page_by_title( 'The Luna Theater', OBJECT, 'tribe_venue' );
if ( !( $venue_post ) ) {
$venue_id = tribe_create_venue( array(
'Venue' => 'The Luna Theater'
));
} else {
$venue_id = $venue_post->ID;
}
wp_cache_set( 'venue_id', $venue_id, 'tev' );
}
return $venue_id;
}
function tev_update_event( $event, $veezi_event ) {
$tev_event_id = get_post_meta( $event->ID, 'tev_event_id', true );
$tev_event = null;
if ( !empty( $tev_event_id ) ) {
$tev_event = tribe_events_get_event( $tev_event_id );
$featured = get_post_meta( $tev_event_id, '_tribe_featured', true );
}
$production = $event->production();
$args = array(
'post_title' => $production->title(),
'post_content' => $production->content(),
'post_status' => 'publish',
'EventStartDate' => date( 'Y-m-d', $event->datetime() + get_option( 'gmt_offset' ) * 3600 ),
'EventStartHour' => date( 'H', $event->datetime() + get_option( 'gmt_offset' ) * 3600 ),
'EventStartMinute' => date( 'i', $event->datetime() + get_option( 'gmt_offset' ) * 3600 ),
'EventURL' => $veezi_event->Url,
'Venue' => array(
'VenueID' => tev_get_venue_id(),
),
);
if ( !empty( $veezi_event->FeatureEndTime ) ) {
$end_time = strtotime( $veezi_event->FeatureEndTime );
$args[ 'EventEndDate' ] = date( 'Y-m-d', $end_time );
$args[ 'EventEndHour' ] = date( 'H', $end_time );
$args[ 'EventEndMinute' ] = date( 'i', $end_time );
}
if ( empty( $tev_event ) ) {
if ( $tev_event_id = tribe_create_event( $args ) ) {
update_post_meta( $event->ID, 'tev_event_id', $tev_event_id );
}
} else {
tribe_update_event( $tev_event_id, $args );
update_post_meta( $tev_event_id, '_tribe_featured', $featured );
}
wp_set_object_terms( $tev_event_id, 'Luna', 'tribe_events_cat', false );
$thumbnail_id = get_post_thumbnail_id( $production->ID );
if ( !empty( $thumbnail_id ) ) {
set_post_thumbnail( $tev_event_id, $thumbnail_id );
}
}
add_filter( 'wpt/veezi/import/event/update', 'tev_update_event', 10, 2 );
function tev_update_thumbnail( WPT_Production $production, $veezi_production ) {
if ( empty( $veezi_production->FilmPosterUrl ) ) {
return;
}
$production_id = $production->ID;
$image_url = $veezi_production->FilmPosterUrl;
$image_desc = $veezi_production->Title;
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$tmp = download_url( $image_url );
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $image_url, $matches);
$file_array['name'] = sanitize_file_name( $image_desc.'.jpg' );
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$thumbnail_id = media_handle_sideload( $file_array, $production_id, $image_desc );
// If error storing permanently, unlink
if ( is_wp_error($thumbnail_id) ) {
@unlink($file_array['tmp_name']);
return $thumbnail_id;
}
return set_post_thumbnail( $production_id, $thumbnail_id );
}
add_action( 'wpt/veezi/import/production/create', 'tev_update_thumbnail', 10, 2 );
add_action( 'wpt/veezi/import/production/update', 'tev_update_thumbnail', 10, 2 );
function tev_change_website_label( $label ) {
ob_start();
?><span class="tribe-events-button">Tickets</span><?php
return ob_get_clean();
}
add_filter( 'tribe_get_event_website_link_label', 'tev_change_website_label' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment