__( 'List', 'burn-events' ), 'month' => __( 'Month', 'burn-events' ),
'week' => __( 'Week', 'burn-events' ), 'day' => __( 'Day', 'burn-events' ),
'photo' => __( 'Photo', 'burn-events' ), 'map' => __( 'Map', 'burn-events' ),
'summary' => __( 'Summary', 'burn-events' ),
];
}
public static function request( string $key ): string {
return isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : '';
}
public static function archive_url(): string {
return (string) get_post_type_archive_link( Events::EVENT );
}
public static function route( string $path ): string {
if ( is_feed() || is_embed() ) {
return $path;
}
if ( get_query_var( 'be_community' ) ) {
$view = (string) get_query_var( 'be_community' );
if ( ! in_array( $view, [ 'submit', 'mine' ], true ) ) {
return $path;
}
self::$content = 'mine' === $view ? Community::mine() : Community::form();
} elseif ( is_singular( Events::EVENT ) ) {
self::$content = self::single( get_queried_object() );
} elseif ( is_singular( array_values( Events::RELATIONS ) ) ) {
self::$content = self::linked( get_queried_object() );
} elseif ( is_post_type_archive( Events::EVENT ) || is_tax( Events::CATEGORY ) ) {
$term = is_tax( Events::CATEGORY ) ? get_queried_object() : null;
$term_link = $term ? get_term_link( $term ) : self::archive_url();
self::$content = self::render( [
'category' => $term->slug ?? '',
'title' => $term->name ?? __( 'Events', 'burn-events' ),
'url' => is_string( $term_link ) ? $term_link : self::archive_url(),
] );
} else {
return $path;
}
return dirname( __DIR__ ) . '/templates/front.php';
}
public static function content(): string {
return self::$content . self::$schema;
}
public static function render( $attributes = [] ): string {
$options = Admin::options();
$attributes = shortcode_atts( [
'view' => $options['view'], 'title' => __( 'Events', 'burn-events' ), 'url' => '',
'category' => '', 'tag' => '', 'location' => '', 'featured' => 'no',
'limit' => $options['per_page'], 'filters' => 'yes', 'relation' => null, 'scope' => '',
], (array) $attributes, 'burn_events' );
$interactive = 'no' !== $attributes['filters'];
$selected = $interactive ? ( self::request( 'be_view' ) ?: (string) get_query_var( 'be_view' ) ) : '';
$view = $selected ?: $attributes['view'];
$view = in_array( $view, self::VIEWS, true ) ? $view : 'list';
$day_value = $interactive ? self::request( 'be_date' ) : '';
$day = self::date( $day_value );
$search = $interactive ? self::request( 'be_search' ) : '';
$location = $attributes['location'] ?: ( $interactive ? self::request( 'be_location' ) : '' );
$category = sanitize_title( $attributes['category'] ?: ( $interactive ? self::request( 'be_category' ) : '' ) );
$page = $interactive ? max( 1, min( 10000, absint( self::request( 'be_page' ) ) ) ) : 1;
$past_requested = $interactive && '1' === self::request( 'be_past' );
$past = $past_requested && in_array( $view, [ 'list', 'photo', 'summary' ], true );
$limit = max( 1, min( 50, absint( $attributes['limit'] ) ) );
$grid = in_array( $view, [ 'month', 'week' ], true );
$map = 'map' === $view;
$map_period = $map ? self::map_period( $day, $interactive, $past_requested ) : null;
$range = self::range( $view, $day );
$from = $map_period['start'] ?? $range[0] ?? ( $day_value ? $day : new DateTimeImmutable( 'now', wp_timezone() ) );
$to = $map ? $map_period['end']?->modify( '+1 day' ) : ( $range[1] ?? null );
$max_rows = $grid || $map ? 600 : $limit;
$utc = new DateTimeZone( 'UTC' );
$filters = [
'from' => $past ? null : $from->setTimezone( $utc )->format( 'Y-m-d H:i:s' ),
'to' => $past ? gmdate( 'Y-m-d H:i:s' ) : ( $to ? $to->setTimezone( $utc )->format( 'Y-m-d H:i:s' ) : null ),
'past' => $past, 'search' => $search, 'category' => $category, 'tag' => sanitize_title( $attributes['tag'] ),
'scope' => sanitize_text_field( $attributes['scope'] ),
'location' => $location, 'relation' => is_array( $attributes['relation'] ) ? $attributes['relation'] : null, 'featured' => 'yes' === $attributes['featured'],
'offset' => $grid || $map ? 0 : ( $page - 1 ) * $limit, 'limit' => $max_rows + 1,
];
if ( $grid ) {
$rows = Calendar_Grid::occurrences( $filters, $range[0], $range[1] );
} else {
$rows = ! empty( $map_period['error'] ) ? [] : Events::query( $filters );
}
$more = count( $rows ) > $max_rows;
$rows = array_slice( $rows, 0, $max_rows );
$base = $attributes['url'] ?: ( is_singular() ? ( get_permalink() ?: self::archive_url() ) : self::archive_url() );
$state = array_filter( [
'be_view' => $view, 'be_date' => $day_value ? $day->format( 'Y-m-d' ) : '',
'be_search' => $search, 'be_category' => $category, 'be_location' => $location, 'be_past' => $past ? '1' : '',
] );
ob_start();
?>
' . esc_html__( 'Showing the first 600 occurrences in this date range.', 'burn-events' ) . '
';
}
} elseif ( ! empty( $map_period['error'] ) ) {
echo '' . esc_html( $map_period['error'] ) . '
';
} elseif ( ! $rows ) {
echo '' . esc_html__( 'No events found for this selection.', 'burn-events' ) . '
';
} elseif ( $map ) {
Map_View::archive( $rows );
if ( $more ) {
echo '' . esc_html__( 'Showing the first 600 occurrences. Choose a shorter date range to see all matching locations.', 'burn-events' ) . '
';
}
} else {
echo '';
foreach ( $rows as $row ) {
self::card( $row, $view );
}
echo '
';
}
?>
1 || $more ) ) : ?>
format( 'Y-m-d' ) === $value ? $day : null;
}
private static function map_period( DateTimeImmutable $day, bool $interactive, bool $past ): array {
$start_input = $interactive ? self::request( 'be_start' ) : '';
$end_input = $interactive ? self::request( 'be_end' ) : '';
$start = $start_input ? self::parse_date( $start_input ) : ( $past ? $day->modify( '-29 days' ) : $day );
$end = $end_input ? self::parse_date( $end_input ) : $start?->modify( '+29 days' );
$error = '';
if ( ! $start || ! $end ) {
$error = __( 'Enter a valid start and end date.', 'burn-events' );
} elseif ( $end < $start ) {
$error = __( 'The end date must be on or after the start date.', 'burn-events' );
}
return [ 'start' => $start, 'end' => $end, 'error' => $error ];
}
private static function range( string $view, DateTimeImmutable $day ): ?array {
if ( 'day' === $view ) {
return [ $day, $day->modify( '+1 day' ) ];
}
if ( ! in_array( $view, [ 'month', 'week' ], true ) ) {
return null;
}
$start_day = (int) get_option( 'start_of_week', 1 );
$first = 'month' === $view ? $day->modify( 'first day of this month' ) : $day;
$diff = ( (int) $first->format( 'w' ) - $start_day + 7 ) % 7;
$start = $first->modify( "-{$diff} days" );
if ( 'week' === $view ) {
return [ $start, $start->modify( '+7 days' ) ];
}
$last = $day->modify( 'last day of this month' );
$remaining = ( $start_day - (int) $last->format( 'w' ) + 7 ) % 7;
return [ $start, $last->modify( '+' . ( $remaining ?: 7 ) . ' days' ) ];
}
private static function search_form( string $url, array $state, string $fixed_category, ?array $map_period = null ): void {
$terms = $fixed_category ? [] : get_terms( [ 'taxonomy' => Events::CATEGORY, 'hide_empty' => true ] );
$query = [];
parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
?>
modify( 'first day of last month' );
$next = $day->modify( 'first day of next month' );
$title = wp_date( 'F Y', $day->getTimestamp() );
} else {
$step = 'week' === $view ? 7 : 1;
$previous = $range[0]->modify( "-{$step} days" );
$next = $range[1];
$title = wp_date( 'M j, Y', $range[0]->getTimestamp() );
if ( 7 === $step ) {
$title = wp_date( 'M j', $range[0]->getTimestamp() ) . ' – ' . wp_date( 'M j, Y', $range[1]->modify( '-1 day' )->getTimestamp() );
}
}
?>
‹
›
'lazy' ] ); ?>
getTimestamp(), $start->getTimezone() ) ); ?> format( 'j' ) ); ?>
' . esc_html( Events::statuses()[ $data['status'] ] ?? '' ) . '';
} elseif ( $data['featured'] ) {
echo '' . esc_html__( 'Featured', 'burn-events' ) . ' ';
}
}
public static function period( array $row, array $data ): string {
$zone = $data['schedule']['timezone'] ?? wp_timezone_string();
$start = Events::local_time( $row['starts_at'], $zone );
$end = Events::local_time( $row['ends_at'], $zone );
$timezone = $start->getTimezone();
$date = (string) get_option( 'date_format' );
$time = (string) get_option( 'time_format' );
$start_label = wp_date( $date, $start->getTimestamp(), $timezone );
if ( ! empty( $data['schedule']['all_day'] ) ) {
$last = $end->modify( '-1 day' );
return ( $start->format( 'Y-m-d' ) === $last->format( 'Y-m-d' ) ? $start_label : $start_label . ' – ' . wp_date( $date, $last->getTimestamp(), $timezone ) )
. ' · ' . __( 'All day', 'burn-events' );
}
$end_label = $start->format( 'Y-m-d' ) === $end->format( 'Y-m-d' ) ? '' : wp_date( $date, $end->getTimestamp(), $timezone ) . ', ';
return $start_label . ' · ' . wp_date( $time, $start->getTimestamp(), $timezone ) . ' – ' . $end_label . wp_date( $time . ' T', $end->getTimestamp(), $timezone );
}
private static function linked( WP_Post $post ): string {
if ( post_password_required( $post ) ) {
return '' . get_the_password_form( $post ) . ' ';
}
$type = array_search( $post->post_type, Events::RELATIONS, true );
$data = Events::location_data( $post->ID );
ob_start();
?>
[ $type, $post->ID ], 'title' => __( 'Upcoming events', 'burn-events' ) ] );
return ob_get_clean();
}
private static function contact( array $data ): void {
$address = array_filter( array_intersect_key( $data, array_flip( [ 'address', 'city', 'region', 'postal', 'country' ] ) ) );
if ( $address ) {
echo '' . esc_html( implode( ', ', $address ) ) . '
';
}
if ( $data['phone'] ) {
echo '' . esc_html( $data['phone'] ) . '
';
}
if ( $data['email'] ) {
echo '' . esc_html( $data['email'] ) . '
';
}
if ( $data['website'] ) {
echo '' . esc_html__( 'Website', 'burn-events' ) . '
';
}
}
private static function single( WP_Post $event ): string {
if ( post_password_required( $event ) ) {
return '' . esc_html( get_the_title( $event ) ) . ' ' . get_the_password_form( $event ) . ' ';
}
$row = Events::occurrence( $event->ID, self::request( 'be_occ' ) );
$data = Events::event_data( $event->ID );
self::schema( $event, $row, $data );
$layout = Single_Event_Templates::render( $event, $row );
if ( $layout ) {
if ( ! str_contains( $layout, 'data-be-event-map="' . $event->ID . '"' ) ) {
$map = Map_View::venues( Events::locations( $data['venue'], Events::VENUE ), $event->ID );
if ( $map ) {
$layout .= '' . esc_html__( 'Event location', 'burn-events' ) . ' ' . $map . ' ';
}
}
return '' . $layout . ' ';
}
ob_start();
?>
__( 'Event title', 'burn-events' ), 'schedule' => __( 'Date and time', 'burn-events' ),
'description' => __( 'Description', 'burn-events' ), 'image' => __( 'Featured image', 'burn-events' ),
'status' => __( 'Status', 'burn-events' ), 'cost' => __( 'Cost', 'burn-events' ),
'venue' => __( 'Venue', 'burn-events' ), 'organizer' => __( 'Organizers', 'burn-events' ), 'series' => __( 'Series', 'burn-events' ),
'website' => __( 'Website', 'burn-events' ), 'fields' => __( 'Custom fields', 'burn-events' ),
'terms' => __( 'Categories and tags', 'burn-events' ), 'related' => __( 'Related events', 'burn-events' ),
'export' => __( 'Add to calendar', 'burn-events' ), 'back' => __( 'All events link', 'burn-events' ),
];
}
public static function field( string $field, WP_Post $event, ?array $row ): string {
$data = Events::event_data( $event->ID );
ob_start();
switch ( $field ) {
case 'title':
echo '' . esc_html( get_the_title( $event ) ) . ' ';
break;
case 'back':
echo '← ' . esc_html__( 'All events', 'burn-events' ) . ' ';
break;
case 'schedule':
if ( $row ) {
echo '' . esc_html( self::period( $row, $data ) ) . '
';
}
break;
case 'status':
self::status( $data );
break;
case 'image':
if ( has_post_thumbnail( $event ) ) {
echo '' . get_the_post_thumbnail( $event, 'large' ) . ' ';
}
break;
case 'description':
self::description( $event );
break;
case 'cost':
if ( '' !== $data['cost'] ) {
echo '' . esc_html( '0' === $data['cost'] ? __( 'Free', 'burn-events' ) : $data['cost'] ) . '
';
}
break;
case 'venue':
case 'organizer':
case 'series':
self::relation( $field, $data, $event->ID );
break;
case 'website':
if ( $data['url'] ) {
echo '' . esc_html__( 'Event website', 'burn-events' ) . '
';
}
break;
case 'fields':
self::custom_fields( $data );
break;
case 'terms':
foreach ( [ Events::CATEGORY, 'post_tag' ] as $taxonomy ) {
$terms = get_the_term_list( $event->ID, $taxonomy, '', ' · ', '
' );
if ( is_string( $terms ) ) {
echo wp_kses_post( $terms );
}
}
break;
case 'export':
if ( $row ) {
echo '' . esc_html__( 'Download .ics', 'burn-events' ) . ' ' . esc_html__( 'Google Calendar', 'burn-events' ) . '
';
}
break;
case 'related':
self::related( $event );
break;
}
return ob_get_clean();
}
private static function description( WP_Post $event ): void {
static $rendering = false;
if ( $rendering || '' === $event->post_content ) {
return;
}
global $post;
$previous = $post;
$post = $event;
$rendering = true;
try {
echo '' . apply_filters( 'the_content', $event->post_content ) . '
';
} finally {
$post = $previous;
$rendering = false;
}
}
private static function relation( string $type, array $data, int $event_id ): void {
$posts = Events::locations( $data[ $type ], Events::RELATIONS[ $type ] );
if ( ! $posts && ( 'venue' !== $type || ! $data['location'] ) ) {
return;
}
echo '';
}
private static function custom_fields( array $data ): void {
$display = '';
foreach ( Admin::options()['fields'] as $key => $field ) {
$value = $data['fields'][ $key ] ?? '';
if ( '' === $value ) {
continue;
}
$display .= '' . esc_html( $field['label'] ) . ' ';
$display .= 'url' === $field['type']
? '' . esc_html( $value ) . ' '
: esc_html( 'checkbox' === $field['type'] ? __( 'Yes', 'burn-events' ) : $value );
$display .= ' ';
}
if ( $display ) {
echo '' . $display . ' ';
}
}
private static function related( WP_Post $event ): void {
$terms = wp_get_post_terms( $event->ID, Events::CATEGORY, [ 'fields' => 'slugs' ] );
if ( is_wp_error( $terms ) || ! $terms ) {
return;
}
$rows = Events::query( [ 'category' => $terms[0], 'from' => gmdate( 'Y-m-d H:i:s' ), 'limit' => 25 ] );
$related = [ $event->ID => true ];
ob_start();
foreach ( $rows as $row ) {
if ( ! isset( $related[ $row['event_id'] ] ) ) {
self::card( $row, 'summary' );
$related[ $row['event_id'] ] = true;
}
if ( count( $related ) >= 4 ) {
break;
}
}
$html = ob_get_clean();
if ( $html ) {
echo '';
}
}
private static function schema( WP_Post $post, ?array $row, array $data ): void {
if ( ! $row || 'publish' !== $post->post_status || $post->post_password ) {
return;
}
$zone = $data['schedule']['timezone'];
$format = $data['schedule']['all_day'] ? 'Y-m-d' : DATE_ATOM;
$details = [
'@context' => 'https://schema.org', '@type' => 'Event', 'name' => get_the_title( $post ),
'url' => Events::event_url( $row ), 'startDate' => Events::local_time( $row['starts_at'], $zone )->format( $format ),
'endDate' => Events::local_time( $row['ends_at'], $zone )->format( $format ),
'eventStatus' => 'https://schema.org/' . ( [ 'cancelled' => 'EventCancelled', 'postponed' => 'EventPostponed', 'moved_online' => 'EventMovedOnline' ][ $data['status'] ] ?? 'EventScheduled' ),
];
$image = get_the_post_thumbnail_url( $post, 'large' );
if ( $image ) {
$details['image'] = $image;
}
$venue = Events::locations( $data['venue'], Events::VENUE )[0] ?? null;
if ( $venue ) {
$details['location'] = [ '@type' => 'Place', 'name' => get_the_title( $venue ), 'url' => get_permalink( $venue ) ];
}
self::$schema = '';
}
public static function register_api(): void {
register_rest_route( 'burn-events/v1', '/events', [
'methods' => 'GET', 'permission_callback' => '__return_true',
'callback' => static function ( \WP_REST_Request $request ): \WP_REST_Response {
$limit = max( 1, min( 50, absint( $request->get_param( 'limit' ) ?: 20 ) ) );
$rows = Events::query( [
'from' => gmdate( 'Y-m-d H:i:s' ),
'search' => sanitize_text_field( $request->get_param( 'search' ) ?? '' ),
'category' => sanitize_title( (string) $request->get_param( 'category' ) ),
'limit' => $limit,
'offset' => ( max( 1, min( 10000, absint( $request->get_param( 'page' ) ) ) ) - 1 ) * $limit,
] );
return rest_ensure_response( array_map( static function ( array $row ): array {
return [
'id' => (int) $row['event_id'], 'title' => get_the_title( (int) $row['event_id'] ),
'start_utc' => gmdate( DATE_ATOM, strtotime( $row['starts_at'] . ' UTC' ) ),
'end_utc' => gmdate( DATE_ATOM, strtotime( $row['ends_at'] . ' UTC' ) ),
'url' => Events::event_url( $row ),
];
}, $rows ) );
},
] );
}
}