201 lines
9.3 KiB
PHP
201 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace BurnEvents;
|
|
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Calendar_Grid {
|
|
public const SCRIPT = 'burn-events-grid';
|
|
public const COLOR_META = '_burn_events_category_color';
|
|
|
|
public static function register_assets(): void {
|
|
wp_register_script( self::SCRIPT, plugins_url( 'assets/calendar-grid.js', BURN_EVENTS_FILE ), [], BURN_EVENTS_VERSION, true );
|
|
}
|
|
|
|
public static function occurrences( array $filters, DateTimeImmutable $start, DateTimeImmutable $end ): array {
|
|
$first = $start->format( 'Y-m-d' );
|
|
$after = $end->format( 'Y-m-d' );
|
|
$utc = new DateTimeZone( 'UTC' );
|
|
// The index stores UTC. Leave room for events whose local dates cross the UTC boundary.
|
|
$filters['from'] = ( new DateTimeImmutable( $first, $utc ) )->modify( '-1 day' )->format( 'Y-m-d H:i:s' );
|
|
$filters['to'] = ( new DateTimeImmutable( $after, $utc ) )->modify( '+1 day' )->format( 'Y-m-d H:i:s' );
|
|
$rows = [];
|
|
do {
|
|
$batch = Events::query( $filters );
|
|
update_meta_cache( 'post', array_unique( array_column( $batch, 'event_id' ) ) );
|
|
foreach ( $batch as $row ) {
|
|
$data = Events::event_data( (int) $row['event_id'] );
|
|
$zone = $data['schedule']['timezone'] ?? wp_timezone_string();
|
|
$from = Events::local_time( $row['starts_at'], $zone )->format( 'Y-m-d' );
|
|
// An occurrence ending at midnight stops before the next day's cell.
|
|
$end_utc = new DateTimeImmutable( $row['ends_at'], $utc );
|
|
$last = $end_utc->modify( '-1 second' )->setTimezone( new DateTimeZone( $zone ) )->format( 'Y-m-d' );
|
|
if ( $last >= $first && $from < $after ) {
|
|
$rows[] = $row + [ 'first_day' => $from, 'last_day' => $last ];
|
|
if ( count( $rows ) === $filters['limit'] ) {
|
|
return $rows;
|
|
}
|
|
}
|
|
}
|
|
$filters['offset'] += count( $batch );
|
|
} while ( count( $batch ) === $filters['limit'] );
|
|
return $rows;
|
|
}
|
|
|
|
public static function render( array $rows, DateTimeImmutable $start, DateTimeImmutable $end, DateTimeImmutable $day, bool $month, string $category ): void {
|
|
$dates = [];
|
|
for ( $date = $start; $date < $end; $date = $date->modify( '+1 day' ) ) {
|
|
$dates[] = $date;
|
|
}
|
|
$indices = array_flip( array_map( static fn( DateTimeImmutable $date ): string => $date->format( 'Y-m-d' ), $dates ) );
|
|
$first = $start->format( 'Y-m-d' );
|
|
$last = end( $dates )->format( 'Y-m-d' );
|
|
$weeks = array_chunk( $dates, 7 );
|
|
$segments = array_fill( 0, count( $weeks ), [] );
|
|
$posts = $events = $legend = [];
|
|
if ( $rows ) {
|
|
wp_enqueue_script( self::SCRIPT );
|
|
update_object_term_cache( array_unique( array_column( $rows, 'event_id' ) ), Events::EVENT );
|
|
}
|
|
foreach ( $rows as $index => $row ) {
|
|
$id = (int) $row['event_id'];
|
|
$posts[ $id ] ??= self::event( $id, $category );
|
|
$event = $posts[ $id ];
|
|
$legend[ $event['category_id'] ] = [ 'name' => $event['category'], 'color' => $event['color'] ];
|
|
$events[ $index ] = $event + [
|
|
'url' => Events::event_url( $row ),
|
|
'period' => Calendar::period( $row, $event['data'] ),
|
|
'preview' => wp_unique_id( 'be-event-preview-' ),
|
|
];
|
|
$from = $indices[ max( $first, $row['first_day'] ) ];
|
|
$through = $indices[ min( $last, $row['last_day'] ) ];
|
|
for ( $week = intdiv( $from, 7 ); $week <= intdiv( $through, 7 ); ++$week ) {
|
|
$offset = $week * 7;
|
|
$segments[ $week ][] = [
|
|
'event' => $index,
|
|
'first' => max( 0, $from - $offset ),
|
|
'last' => min( 6, $through - $offset ),
|
|
'before' => $row['first_day'] < $dates[ $offset ]->format( 'Y-m-d' ),
|
|
'after' => $row['last_day'] > $dates[ $offset + 6 ]->format( 'Y-m-d' ),
|
|
];
|
|
}
|
|
}
|
|
ksort( $legend );
|
|
?>
|
|
<?php if ( $legend && [ 0 ] !== array_keys( $legend ) ) : ?>
|
|
<ul class="be-grid-legend" aria-label="<?php esc_attr_e( 'Event categories', 'burn-events' ); ?>">
|
|
<?php foreach ( $legend as $term ) : ?>
|
|
<li class="be-grid-color" style="<?php echo esc_attr( $term['color'] ); ?>"><?php echo esc_html( $term['name'] ); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
<div class="be-grid-scroll" role="region" aria-label="<?php esc_attr_e( 'Calendar grid', 'burn-events' ); ?>" tabindex="0">
|
|
<div class="be-grid">
|
|
<div class="be-grid-weekdays" aria-hidden="true">
|
|
<?php foreach ( $weeks[0] as $date ) : ?>
|
|
<div class="be-weekday"><?php echo esc_html( wp_date( 'D', $date->getTimestamp(), $date->getTimezone() ) ); ?></div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php foreach ( $weeks as $week => $days ) { self::week( $days, $segments[ $week ], $events, $month ? $day->format( 'Y-m' ) : '' ); } ?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
foreach ( $events as $event ) {
|
|
self::preview( $event );
|
|
}
|
|
if ( ! $rows ) {
|
|
echo '<p class="be-empty">' . esc_html__( 'No events in this date range.', 'burn-events' ) . '</p>';
|
|
}
|
|
}
|
|
|
|
private static function event( int $id, string $category ): array {
|
|
$terms = get_the_terms( $id, Events::CATEGORY );
|
|
$terms = is_array( $terms ) ? $terms : [];
|
|
usort( $terms, static fn( $left, $right ): int => $left->term_id <=> $right->term_id );
|
|
$term = $terms[0] ?? null;
|
|
foreach ( $terms as $candidate ) {
|
|
if ( $candidate->slug === $category ) {
|
|
$term = $candidate;
|
|
break;
|
|
}
|
|
}
|
|
$term_id = (int) ( $term->term_id ?? 0 );
|
|
$data = Events::event_data( $id );
|
|
$label = $term->name ?? __( 'Uncategorized', 'burn-events' );
|
|
$color = $term_id ? sanitize_hex_color( (string) get_term_meta( $term_id, self::COLOR_META, true ) ) : '';
|
|
return [
|
|
'title' => get_the_title( $id ),
|
|
'data' => $data,
|
|
'location' => Calendar::event_location( $data ),
|
|
'excerpt' => Calendar::event_excerpt( $id ),
|
|
'image' => get_the_post_thumbnail_url( $id, 'thumbnail' ),
|
|
'category_id' => $term_id,
|
|
'category' => $label,
|
|
'categories' => $terms ? implode( ' · ', array_column( $terms, 'name' ) ) : $label,
|
|
'color' => $color ? '--be-category-custom:' . $color : ( $term_id ? sprintf( '--be-category-hue:%.2F;--be-category-saturation:63%%', fmod( $term_id * 137.508, 360 ) ) : '' ),
|
|
];
|
|
}
|
|
|
|
private static function week( array $days, array $segments, array $events, string $month ): void {
|
|
usort( $segments, static function ( array $left, array $right ): int {
|
|
return $left['first'] <=> $right['first'] ?: $right['last'] <=> $left['last'] ?: $left['event'] <=> $right['event'];
|
|
} );
|
|
$ends = [];
|
|
foreach ( $segments as $index => $segment ) {
|
|
$track = 0;
|
|
while ( isset( $ends[ $track ] ) && $ends[ $track ] >= $segment['first'] ) {
|
|
++$track;
|
|
}
|
|
$ends[ $track ] = $segment['last'];
|
|
$segments[ $index ]['track'] = $track + 2;
|
|
}
|
|
$today = wp_date( 'Y-m-d' );
|
|
?>
|
|
<div class="be-grid-week" style="--be-tracks:<?php echo max( 1, count( $ends ) ); ?>">
|
|
<?php foreach ( $days as $column => $date ) : ?>
|
|
<?php $key = $date->format( 'Y-m-d' ); ?>
|
|
<div class="be-grid-day<?php echo $month && $date->format( 'Y-m' ) !== $month ? ' be-grid-other' : ''; ?>" style="--be-day:<?php echo $column + 1; ?>">
|
|
<time datetime="<?php echo esc_attr( $key ); ?>" aria-label="<?php echo esc_attr( wp_date( 'l, F j', $date->getTimestamp(), $date->getTimezone() ) ); ?>"<?php if ( $today === $key ) : ?> aria-current="date"<?php endif; ?>><?php echo esc_html( $date->format( 'j' ) ); ?></time>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php foreach ( $segments as $segment ) : ?>
|
|
<?php
|
|
$event = $events[ $segment['event'] ];
|
|
$style = sprintf( '--be-start:%d;--be-span:%d;--be-track:%d;%s', $segment['first'] + 1, $segment['last'] - $segment['first'] + 1, $segment['track'], $event['color'] );
|
|
?>
|
|
<a class="be-grid-event be-grid-color<?php echo $segment['before'] ? ' be-grid-continued' : ''; ?><?php echo $segment['after'] ? ' be-grid-continues' : ''; ?>"
|
|
href="<?php echo esc_url( $event['url'] ); ?>" data-be-preview="<?php echo esc_attr( $event['preview'] ); ?>"
|
|
style="<?php echo esc_attr( $style ); ?>" aria-label="<?php echo esc_attr( wp_strip_all_tags( html_entity_decode( $event['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ) . ', ' . $event['period'] . ', ' . $event['categories'] ); ?>">
|
|
<span><?php echo esc_html( $event['title'] ); ?></span>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private static function preview( array $event ): void {
|
|
$data = $event['data'];
|
|
?>
|
|
<template id="<?php echo esc_attr( $event['preview'] ); ?>">
|
|
<div class="be-preview-content be-grid-color" style="<?php echo esc_attr( $event['color'] ); ?>">
|
|
<div class="be-preview-heading">
|
|
<?php if ( $event['image'] ) : ?><img src="<?php echo esc_url( $event['image'] ); ?>" alt="" width="56" height="56" decoding="async"><?php endif; ?>
|
|
<div>
|
|
<p class="be-preview-category"><?php echo esc_html( $event['categories'] ); ?></p>
|
|
<strong><?php echo esc_html( $event['title'] ); ?></strong>
|
|
</div>
|
|
</div>
|
|
<p class="be-preview-time"><?php echo esc_html( $event['period'] ); ?></p>
|
|
<?php if ( $event['location'] ) : ?><p><?php echo esc_html( $event['location'] ); ?></p><?php endif; ?>
|
|
<?php if ( '' !== $data['cost'] ) : ?><p class="be-preview-cost"><?php echo esc_html( '0' === $data['cost'] ? __( 'Free', 'burn-events' ) : $data['cost'] ); ?></p><?php endif; ?>
|
|
<?php if ( 'scheduled' !== $data['status'] ) : ?><p class="be-preview-cost"><?php echo esc_html( Events::statuses()[ $data['status'] ] ?? '' ); ?></p><?php endif; ?>
|
|
<?php if ( $event['excerpt'] ) : ?><p class="be-preview-excerpt"><?php echo esc_html( $event['excerpt'] ); ?></p><?php endif; ?>
|
|
</div>
|
|
</template>
|
|
<?php
|
|
}
|
|
}
|