-
-
Save slimndap/1df69ab8d6b8f2861fd074cecf5f98d3 to your computer and use it in GitHub Desktop.
Add support for a custom dates summary, tickets url and venue for productions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function itr_get_events_for_production( $production_id ) { | |
$production = new WPT_Production( $production_id ); | |
$events = $production->events(); | |
return $events; | |
} | |
function itr_get_dates_for_production( $production_id ) { | |
$events = itr_get_events_for_production( $production_id ); | |
if ( empty( $events ) ) { | |
return; | |
} | |
if ( 1 == count( $events ) ) { | |
echo $events[0]->startdate(); | |
} else { | |
$datetime_start = $events[0]->datetime() + get_option( 'gmt_offset' ) * 3600; | |
$datetime_end = $events[ count( $events ) - 1 ]->datetime() + get_option( 'gmt_offset' ) * 3600; | |
$year_start = date( 'Y', $datetime_start ); | |
$year_end = date( 'Y', $datetime_end ); | |
if ( $year_start != $year_end ) { | |
printf( | |
'%s - %s', | |
date( 'D j F Y', $datetime_start ), | |
date( 'D j F Y', $datetime_end ) | |
); | |
} else { | |
$month_start = date( 'm', $datetime_start ); | |
$month_end = date( 'm', $datetime_end ); | |
if ( $month_start != $month_end ) { | |
printf( | |
'%s - %s', | |
date( 'D j F', $datetime_start ), | |
date( 'D j F Y', $datetime_end ) | |
); | |
} else { | |
$day_start = date( 'd', $datetime_start ); | |
$day_end = date( 'd', $datetime_end ); | |
if ( $day_start != $day_end ) { | |
printf( | |
'%s - %s', | |
date( 'D j F', $datetime_start ), | |
date( 'D j F Y', $datetime_end ) | |
); | |
} else { | |
echo $events[0]->startdate(); | |
} | |
} | |
} | |
} | |
return ob_get_clean(); | |
} | |
function itr_get_location_for_production( $production_id ) { | |
if ( !function_exists( 'get_field' ) ) { | |
return; | |
} | |
$venue = get_field( 'venue', $production_id ); | |
if ( !empty( $venue ) ) { | |
return $venue; | |
} | |
} | |
function itr_get_tickets_url_for_production( $production_id ) { | |
if ( !function_exists( 'get_field' ) ) { | |
return; | |
} | |
$tickets_text = get_field( 'tickets_text', $production_id ); | |
$tickets_url = get_field( 'tickets_url', $production_id ); | |
$class = WPT_Event::post_type_name.'_tickets_url'; | |
ob_start(); | |
if ( empty( $tickets_url ) && !empty( $tickets_text ) ) { | |
?><span class="<?php echo $class; ?>"><?php echo $tickets_text; ?></span><?php | |
} elseif ( !empty( $tickets_url ) && empty( $tickets_text ) ) { | |
?><a href="<?php echo $tickets_url; ?>" class="<?php echo $class; ?>"><?php _e( 'Tickets', 'theatre' ); ?></a><?php | |
} elseif ( !empty( $tickets_url ) && !empty( $tickets_text ) ) { | |
?><a href="<?php echo $tickets_url; ?>" class="<?php echo $class; ?>"><?php echo $tickets_text; ?></a><?php | |
} | |
return ob_get_clean(); | |
} | |
function itr_prepend_content_with_event_details( $content ) { | |
if( class_exists( 'WPT_Production' ) && is_singular( WPT_Production::post_type_name ) && is_main_query() ) { | |
ob_start(); | |
?><div class="event_details"> | |
<div class="event_dates"><?php echo itr_get_dates_for_production( get_the_id() ); ?></div> | |
<div class="event_location"><?php echo itr_get_location_for_production( get_the_id() ); ?></div> | |
<div class="event_tickets"><?php echo itr_get_tickets_url_for_production( get_the_id() ); ?></div> | |
</div><?php | |
echo $content; | |
$content = ob_get_clean(); | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'itr_prepend_content_with_event_details', 11 ); | |
function itr_prepend_title_with_subtitle( $title, $post_id ) { | |
if ( function_exists( 'get_field' ) && is_singular( WPT_Production::post_type_name ) && is_main_query() && in_the_loop() ) { | |
ob_start(); | |
?><div class="subtitle"><?php the_field( 'subtitle' ); ?></div><?php | |
echo $title; | |
$title = ob_get_clean(); | |
} | |
return $title; | |
} | |
add_filter( 'the_title', 'itr_prepend_title_with_subtitle', 10, 2 ); | |
function itr_add_acf_fields() { | |
if(function_exists("register_field_group")) | |
{ | |
register_field_group(array ( | |
'id' => 'acf_production-details', | |
'title' => 'Production details', | |
'fields' => array ( | |
array ( | |
'key' => 'field_5b3c6b64d5f16', | |
'label' => 'Tickets URL', | |
'name' => 'tickets_url', | |
'type' => 'text', | |
'default_value' => '', | |
'placeholder' => 'https://', | |
'prepend' => '', | |
'append' => '', | |
'formatting' => 'none', | |
'maxlength' => '', | |
), | |
array ( | |
'key' => 'field_itr_tickets_text', | |
'label' => 'Text on tickets button', | |
'name' => 'tickets_text', | |
'type' => 'text', | |
'default_value' => '', | |
'placeholder' => __( 'Tickets', 'theatre' ), | |
'prepend' => '', | |
'append' => '', | |
'formatting' => 'none', | |
'maxlength' => '', | |
'instructions' => sprintf( 'Leave blank for \'%s\'', __( 'Tickets', 'theatre' ) ), | |
), | |
array ( | |
'key' => 'field_5b3c9244c9795', | |
'label' => 'Subtitle', | |
'name' => 'subtitle', | |
'type' => 'text', | |
'instructions' => 'Shown above event title on detail page.', | |
'default_value' => '', | |
'placeholder' => 'Eg. Royal NZ Ballet presents', | |
'prepend' => '', | |
'append' => '', | |
'formatting' => 'html', | |
'maxlength' => '', | |
), | |
array ( | |
'key' => 'field_5b3c6b3cd5f15', | |
'label' => 'Venue', | |
'name' => 'venue', | |
'type' => 'text', | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'formatting' => 'none', | |
'maxlength' => '', | |
), | |
), | |
'location' => array ( | |
array ( | |
array ( | |
'param' => 'post_type', | |
'operator' => '==', | |
'value' => 'wp_theatre_prod', | |
'order_no' => 0, | |
'group_no' => 0, | |
), | |
), | |
), | |
'options' => array ( | |
'position' => 'normal', | |
'layout' => 'default', | |
'hide_on_screen' => array ( | |
), | |
), | |
'menu_order' => 0, | |
)); | |
} | |
} | |
add_action( 'init', 'itr_add_acf_fields' ); | |
function itr_change_default_template_for_productions( $template ) { | |
return '{{thumbnail|permalink}} {{title|permalink}} {{itr_dates}} {{venue}} {{tickets}}'; | |
} | |
add_filter( 'wpt/production/template/default', 'itr_change_default_template_for_productions' ); | |
function itr_get_production_tickets( $value, $field, $production ) { | |
return itr_get_tickets_url_for_production( $production->ID ); | |
} | |
add_filter( 'wpt_production_tickets', 'itr_get_production_tickets', 10, 3); | |
function itr_get_production_dates( $value, $field, WPT_Production $production ) { | |
return itr_get_dates_for_production( $production->ID ); | |
} | |
add_filter( 'wpt_production_itr_dates', 'itr_get_production_dates', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment