Publish eventLabs 0.5.0

This commit is contained in:
josh
2026-11-11 21:30:20 -05:00
parent 31a327d16f
commit 258aee939c
41 changed files with 6737 additions and 0 deletions

624
includes/class-calendar.php Normal file
View File

@@ -0,0 +1,624 @@
<?php
namespace BurnEvents;
use DateTimeImmutable;
use DateTimeZone;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Calendar {
public const VIEWS = [ 'list', 'month', 'week', 'day', 'photo', 'map', 'summary' ];
private static string $content = '';
private static string $schema = '';
public static function register(): void {
add_shortcode( 'burn_events', [ self::class, 'render' ] );
add_filter( 'query_vars', static function ( array $vars ): array {
return array_merge( $vars, [ 'be_view', 'be_date', 'be_community' ] );
} );
add_rewrite_rule( '^events/(list|month|week|day|photo|map|summary)/?$', 'index.php?post_type=burn_event&be_view=$matches[1]', 'top' );
add_filter( 'template_include', [ self::class, 'route' ], 90 );
}
public static function view_labels(): array {
return [
'list' => __( '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();
?>
<section class="be-calendar">
<?php if ( $attributes['title'] ) : ?>
<header class="be-calendar-header">
<h1><?php echo esc_html( $attributes['title'] ); ?></h1>
<?php if ( $interactive ) : ?><a class="be-calendar-subscribe" href="<?php echo esc_url( Transfer::ics_url() ); ?>"><?php esc_html_e( 'Subscribe', 'burn-events' ); ?></a><?php endif; ?>
</header>
<?php endif; ?>
<?php if ( $interactive ) : ?>
<?php self::search_form( $base, $state, $attributes['category'], $map_period ); ?>
<div class="be-calendar-toolbar">
<nav class="be-view-tabs" aria-label="<?php esc_attr_e( 'Calendar views', 'burn-events' ); ?>">
<?php foreach ( self::view_labels() as $key => $label ) : ?>
<?php
$tab = [ 'be_view' => $key, 'be_past' => false ];
if ( 'map' === $key && $map_period ) {
$tab += [ 'be_start' => $map_period['start']?->format( 'Y-m-d' ), 'be_end' => $map_period['end']?->format( 'Y-m-d' ) ];
}
?>
<a <?php if ( $key === $view ) { echo 'aria-current="page"'; } ?> href="<?php echo esc_url( add_query_arg( array_replace( $state, $tab ), $base ) ); ?>"><?php echo esc_html( $label ); ?></a>
<?php endforeach; ?>
</nav>
<?php if ( ! $range && ! $map ) : ?>
<a href="<?php echo esc_url( add_query_arg( array_replace( $state, [ 'be_past' => $past ? false : '1', 'be_date' => false ] ), $base ) ); ?>"><?php echo esc_html( $past ? __( 'Upcoming events', 'burn-events' ) : __( 'Past events', 'burn-events' ) ); ?></a>
<?php endif; ?>
</div>
<?php if ( $range ) { self::date_navigation( $view, $day, $range, $base, $state ); } ?>
<?php endif; ?>
<?php
if ( $grid ) {
Calendar_Grid::render( $rows, $range[0], $range[1], $day, 'month' === $view, $category );
if ( $more ) {
echo '<p>' . esc_html__( 'Showing the first 600 occurrences in this date range.', 'burn-events' ) . '</p>';
}
} elseif ( ! empty( $map_period['error'] ) ) {
echo '<p class="be-empty" role="alert">' . esc_html( $map_period['error'] ) . '</p>';
} elseif ( ! $rows ) {
echo '<p class="be-empty">' . esc_html__( 'No events found for this selection.', 'burn-events' ) . '</p>';
} elseif ( $map ) {
Map_View::archive( $rows );
if ( $more ) {
echo '<p>' . esc_html__( 'Showing the first 600 occurrences. Choose a shorter date range to see all matching locations.', 'burn-events' ) . '</p>';
}
} else {
echo '<div class="be-event-list be-view-' . esc_attr( $view ) . '">';
foreach ( $rows as $row ) {
self::card( $row, $view );
}
echo '</div>';
}
?>
<?php if ( $interactive && ! $grid && ! $map && ( $page > 1 || $more ) ) : ?>
<nav class="be-pagination" aria-label="<?php esc_attr_e( 'Event pages', 'burn-events' ); ?>">
<?php if ( $page > 1 ) : ?><a href="<?php echo esc_url( add_query_arg( $state + [ 'be_page' => $page - 1 ], $base ) ); ?>"><?php esc_html_e( 'Previous events', 'burn-events' ); ?></a><?php endif; ?>
<?php if ( $more ) : ?><a href="<?php echo esc_url( add_query_arg( $state + [ 'be_page' => $page + 1 ], $base ) ); ?>"><?php esc_html_e( 'More events', 'burn-events' ); ?></a><?php endif; ?>
</nav>
<?php endif; ?>
</section>
<?php
return ob_get_clean();
}
private static function date( string $value ): DateTimeImmutable {
return self::parse_date( $value ) ?? new DateTimeImmutable( 'today', wp_timezone() );
}
private static function parse_date( string $value ): ?DateTimeImmutable {
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/D', $value ) ) {
return null;
}
$day = DateTimeImmutable::createFromFormat( '!Y-m-d', $value, wp_timezone() );
return $day && $day->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 );
?>
<form class="be-search" method="get" action="<?php echo esc_url( strtok( $url, '?' ) ); ?>" role="search" aria-label="<?php esc_attr_e( 'Search events', 'burn-events' ); ?>">
<?php foreach ( $query + array_intersect_key( $state, array_flip( [ 'be_view', 'be_date', 'be_past' ] ) ) as $key => $value ) : ?>
<?php if ( is_scalar( $value ) ) : ?><input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( (string) $value ); ?>"><?php endif; ?>
<?php endforeach; ?>
<label><?php esc_html_e( 'Search events', 'burn-events' ); ?><input name="be_search" type="search" placeholder="<?php esc_attr_e( 'Name or keyword', 'burn-events' ); ?>" value="<?php echo esc_attr( $state['be_search'] ?? '' ); ?>"></label>
<label><?php esc_html_e( 'Location', 'burn-events' ); ?><input name="be_location" type="search" placeholder="<?php esc_attr_e( 'City or venue', 'burn-events' ); ?>" value="<?php echo esc_attr( $state['be_location'] ?? '' ); ?>"></label>
<?php if ( $terms && ! is_wp_error( $terms ) ) : ?>
<label><?php esc_html_e( 'Category', 'burn-events' ); ?><select name="be_category"><option value=""><?php esc_html_e( 'All categories', 'burn-events' ); ?></option>
<?php foreach ( $terms as $term ) : ?><option value="<?php echo esc_attr( $term->slug ); ?>" <?php selected( $state['be_category'] ?? '', $term->slug ); ?>><?php echo esc_html( $term->name ); ?></option><?php endforeach; ?>
</select></label>
<?php endif; ?>
<?php if ( $map_period ) : ?>
<label><?php esc_html_e( 'Start date', 'burn-events' ); ?><input name="be_start" type="date" value="<?php echo esc_attr( $map_period['start']?->format( 'Y-m-d' ) ?? '' ); ?>" required></label>
<label><?php esc_html_e( 'End date', 'burn-events' ); ?><input name="be_end" type="date" value="<?php echo esc_attr( $map_period['end']?->format( 'Y-m-d' ) ?? '' ); ?>" required></label>
<?php endif; ?>
<button class="be-button" type="submit"><?php echo $map_period ? esc_html__( 'Update map', 'burn-events' ) : esc_html__( 'Find events', 'burn-events' ); ?></button>
</form>
<?php
}
private static function date_navigation( string $view, DateTimeImmutable $day, array $range, string $base, array $state ): void {
if ( 'month' === $view ) {
$previous = $day->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() );
}
}
?>
<nav class="be-date-nav" aria-label="<?php esc_attr_e( 'Calendar dates', 'burn-events' ); ?>">
<a href="<?php echo esc_url( add_query_arg( array_replace( $state, [ 'be_date' => $previous->format( 'Y-m-d' ) ] ), $base ) ); ?>" aria-label="<?php esc_attr_e( 'Previous date range', 'burn-events' ); ?>">‹</a>
<h2><?php echo esc_html( $title ); ?></h2>
<a href="<?php echo esc_url( add_query_arg( array_replace( $state, [ 'be_date' => $next->format( 'Y-m-d' ) ] ), $base ) ); ?>" aria-label="<?php esc_attr_e( 'Next date range', 'burn-events' ); ?>">›</a>
<a class="be-today" href="<?php echo esc_url( add_query_arg( array_replace( $state, [ 'be_date' => false ] ), $base ) ); ?>"><?php esc_html_e( 'Today', 'burn-events' ); ?></a>
</nav>
<?php
}
public static function event_location( array $data ): string {
$venue = Events::locations( $data['venue'], Events::VENUE )[0] ?? null;
return $venue ? get_the_title( $venue ) : $data['location'];
}
public static function event_excerpt( int $id ): string {
$excerpt = get_post_field( 'post_excerpt', $id ) ?: get_post_field( 'post_content', $id );
return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 27 );
}
private static function card( array $row, string $view ): void {
$id = (int) $row['event_id'];
$data = Events::event_data( $id );
$zone = $data['schedule']['timezone'] ?? wp_timezone_string();
$start = Events::local_time( $row['starts_at'], $zone );
$link = Events::event_url( $row );
$location = self::event_location( $data );
?>
<article class="be-event-card">
<?php if ( 'photo' === $view && has_post_thumbnail( $id ) ) : ?><a class="be-card-image" href="<?php echo esc_url( $link ); ?>" tabindex="-1" aria-hidden="true"><?php echo get_the_post_thumbnail( $id, 'large', [ 'loading' => 'lazy' ] ); ?></a><?php endif; ?>
<time class="be-date-tile" datetime="<?php echo esc_attr( $start->format( DATE_ATOM ) ); ?>"><span><?php echo esc_html( wp_date( 'M', $start->getTimestamp(), $start->getTimezone() ) ); ?></span><strong><?php echo esc_html( $start->format( 'j' ) ); ?></strong></time>
<div class="be-card-body">
<p class="be-meta"><?php echo esc_html( self::period( $row, $data ) ); ?></p>
<h3><a href="<?php echo esc_url( $link ); ?>"><?php echo esc_html( get_the_title( $id ) ); ?></a></h3>
<?php if ( $location ) : ?><p class="be-meta"><?php echo esc_html( $location ); ?></p><?php endif; ?>
<?php self::status( $data ); ?>
<?php if ( 'list' === $view || 'photo' === $view ) : ?><p><?php echo esc_html( self::event_excerpt( $id ) ); ?></p><?php endif; ?>
</div>
</article>
<?php
}
private static function status( array $data ): void {
if ( 'scheduled' !== $data['status'] ) {
echo '<span class="be-status">' . esc_html( Events::statuses()[ $data['status'] ] ?? '' ) . '</span>';
} elseif ( $data['featured'] ) {
echo '<span class="be-status">' . esc_html__( 'Featured', 'burn-events' ) . '</span>';
}
}
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 '<main class="be-single">' . get_the_password_form( $post ) . '</main>';
}
$type = array_search( $post->post_type, Events::RELATIONS, true );
$data = Events::location_data( $post->ID );
ob_start();
?>
<header class="be-linked-header">
<a href="<?php echo esc_url( self::archive_url() ); ?>"><?php esc_html_e( 'All events', 'burn-events' ); ?></a>
<h1><?php echo esc_html( get_the_title( $post ) ); ?></h1>
<?php self::contact( $data ); ?>
<?php
$view = self::request( 'be_view' ) ?: (string) get_query_var( 'be_view' ) ?: Admin::options()['view'];
if ( Events::VENUE === $post->post_type && 'map' !== $view ) {
echo Map_View::venues( [ $post ] );
}
?>
<?php echo apply_filters( 'the_content', $post->post_content ); ?>
</header>
<?php
echo self::render( [ 'relation' => [ $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 '<p>' . esc_html( implode( ', ', $address ) ) . '</p>';
}
if ( $data['phone'] ) {
echo '<p><a href="tel:' . esc_attr( preg_replace( '/[^0-9+]/', '', $data['phone'] ) ) . '">' . esc_html( $data['phone'] ) . '</a></p>';
}
if ( $data['email'] ) {
echo '<p><a href="' . esc_url( 'mailto:' . $data['email'] ) . '">' . esc_html( $data['email'] ) . '</a></p>';
}
if ( $data['website'] ) {
echo '<p><a href="' . esc_url( $data['website'] ) . '">' . esc_html__( 'Website', 'burn-events' ) . '</a></p>';
}
}
private static function single( WP_Post $event ): string {
if ( post_password_required( $event ) ) {
return '<article class="be-single"><h1>' . esc_html( get_the_title( $event ) ) . '</h1>' . get_the_password_form( $event ) . '</article>';
}
$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 .= '<section class="be-single-map"><h2>' . esc_html__( 'Event location', 'burn-events' ) . '</h2>' . $map . '</section>';
}
}
return '<article class="be-single be-custom-single">' . $layout . '</article>';
}
ob_start();
?>
<article class="be-single">
<header class="be-single-header">
<?php echo self::field( 'back', $event, $row ); ?>
<?php echo self::field( 'title', $event, $row ); ?>
<?php echo self::field( 'schedule', $event, $row ); ?>
<?php echo self::field( 'status', $event, $row ); ?>
</header>
<?php echo self::field( 'image', $event, $row ); ?>
<div class="be-single-columns">
<div>
<?php echo self::field( 'description', $event, $row ); ?>
<?php echo self::field( 'fields', $event, $row ); ?>
<?php echo self::field( 'export', $event, $row ); ?>
</div>
<aside class="be-single-details" aria-label="<?php esc_attr_e( 'Event details', 'burn-events' ); ?>">
<?php foreach ( [ 'cost', 'venue', 'organizer', 'series', 'website', 'terms' ] as $field ) { echo self::field( $field, $event, $row ); } ?>
</aside>
</div>
<?php echo self::field( 'related', $event, $row ); ?>
</article>
<?php
return ob_get_clean();
}
public static function fields(): array {
return [
'title' => __( '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 '<h1 class="be-event-title">' . esc_html( get_the_title( $event ) ) . '</h1>';
break;
case 'back':
echo '<a class="be-back" href="' . esc_url( self::archive_url() ) . '">← ' . esc_html__( 'All events', 'burn-events' ) . '</a>';
break;
case 'schedule':
if ( $row ) {
echo '<p class="be-single-date">' . esc_html( self::period( $row, $data ) ) . '</p>';
}
break;
case 'status':
self::status( $data );
break;
case 'image':
if ( has_post_thumbnail( $event ) ) {
echo '<figure class="be-single-image">' . get_the_post_thumbnail( $event, 'large' ) . '</figure>';
}
break;
case 'description':
self::description( $event );
break;
case 'cost':
if ( '' !== $data['cost'] ) {
echo '<p class="be-cost">' . esc_html( '0' === $data['cost'] ? __( 'Free', 'burn-events' ) : $data['cost'] ) . '</p>';
}
break;
case 'venue':
case 'organizer':
case 'series':
self::relation( $field, $data, $event->ID );
break;
case 'website':
if ( $data['url'] ) {
echo '<p><a class="be-button" href="' . esc_url( $data['url'] ) . '">' . esc_html__( 'Event website', 'burn-events' ) . '</a></p>';
}
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, '<p class="be-term-links">', ' · ', '</p>' );
if ( is_string( $terms ) ) {
echo wp_kses_post( $terms );
}
}
break;
case 'export':
if ( $row ) {
echo '<p class="be-export-links"><a href="' . esc_url( Transfer::ics_url( $row ) ) . '">' . esc_html__( 'Download .ics', 'burn-events' ) . '</a><a href="' . esc_url( Transfer::google_url( $event, $row, $data ) ) . '">' . esc_html__( 'Google Calendar', 'burn-events' ) . '</a></p>';
}
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 '<div class="be-description">' . apply_filters( 'the_content', $event->post_content ) . '</div>';
} 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 '<section class="be-related-detail"><h2>' . esc_html( self::fields()[ $type ] ) . '</h2>';
if ( 'venue' === $type && $data['location'] ) {
echo '<p>' . esc_html( $data['location'] ) . '</p>';
}
foreach ( $posts as $item ) {
echo '<p><a href="' . esc_url( get_permalink( $item ) ) . '">' . esc_html( get_the_title( $item ) ) . '</a></p>';
if ( 'series' !== $type ) {
$place = Events::location_data( $item->ID );
self::contact( $place );
}
}
if ( 'venue' === $type ) {
echo Map_View::venues( $posts, $event_id );
}
echo '</section>';
}
private static function custom_fields( array $data ): void {
$display = '';
foreach ( Admin::options()['fields'] as $key => $field ) {
$value = $data['fields'][ $key ] ?? '';
if ( '' === $value ) {
continue;
}
$display .= '<dt>' . esc_html( $field['label'] ) . '</dt><dd>';
$display .= 'url' === $field['type']
? '<a href="' . esc_url( $value ) . '">' . esc_html( $value ) . '</a>'
: esc_html( 'checkbox' === $field['type'] ? __( 'Yes', 'burn-events' ) : $value );
$display .= '</dd>';
}
if ( $display ) {
echo '<dl class="be-custom-fields">' . $display . '</dl>';
}
}
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 '<section class="be-related-events"><h2>' . esc_html__( 'Related events', 'burn-events' ) . '</h2><div class="be-event-list be-view-summary">' . $html . '</div></section>';
}
}
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 = '<script type="application/ld+json">' . wp_json_encode( $details, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . '</script>';
}
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 ) );
},
] );
}
}