170 lines
6.6 KiB
PHP
170 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace BurnEvents;
|
|
|
|
use WP_Post;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Map_View {
|
|
public const SCRIPT = 'burn-events-map';
|
|
public const STYLE = 'burn-events-leaflet';
|
|
|
|
public static function register_assets(): void {
|
|
$base = plugins_url( 'assets/', BURN_EVENTS_FILE );
|
|
wp_register_style( self::STYLE, $base . 'vendor/leaflet/leaflet.css', [], '1.9.4' );
|
|
wp_register_script( self::STYLE, $base . 'vendor/leaflet/leaflet.js', [], '1.9.4', true );
|
|
wp_register_script( self::SCRIPT, $base . 'maps.js', [ self::STYLE ], BURN_EVENTS_VERSION, true );
|
|
}
|
|
|
|
public static function venues( array $venues, int $event_id = 0 ): string {
|
|
if ( ! Admin::options()['maps'] ) {
|
|
return '';
|
|
}
|
|
$pins = [];
|
|
foreach ( $venues as $venue ) {
|
|
$place = self::place( $venue );
|
|
if ( $place ) {
|
|
self::add_pin( $pins, $place );
|
|
}
|
|
}
|
|
return $pins ? self::canvas( $pins, $event_id ? __( 'Event location map', 'burn-events' ) : __( 'Venue map', 'burn-events' ), true, $event_id ) : '';
|
|
}
|
|
|
|
public static function archive( array $rows ): void {
|
|
$pins = $places = $unmapped = [];
|
|
$mapped = 0;
|
|
update_meta_cache( 'post', array_unique( array_column( $rows, 'event_id' ) ) );
|
|
foreach ( $rows as $row ) {
|
|
$id = (int) $row['event_id'];
|
|
$data = Events::event_data( $id );
|
|
$event = [
|
|
'title' => self::title( $id ),
|
|
'date' => Calendar::period( $row, $data ),
|
|
'url' => esc_url_raw( Events::event_url( $row ), [ 'http', 'https' ] ),
|
|
];
|
|
$located = false;
|
|
foreach ( Events::locations( $data['venue'], Events::VENUE ) as $venue ) {
|
|
if ( ! array_key_exists( $venue->ID, $places ) ) {
|
|
$places[ $venue->ID ] = self::place( $venue );
|
|
}
|
|
if ( $places[ $venue->ID ] ) {
|
|
self::add_pin( $pins, $places[ $venue->ID ], $event );
|
|
$located = true;
|
|
}
|
|
}
|
|
if ( $located ) {
|
|
++$mapped;
|
|
} else {
|
|
$unmapped[] = $event;
|
|
}
|
|
}
|
|
|
|
if ( $pins ) {
|
|
$enabled = Admin::options()['maps'];
|
|
$counts = [
|
|
sprintf( _n( '%s event date', '%s event dates', $mapped, 'burn-events' ), number_format_i18n( $mapped ) ),
|
|
sprintf( _n( '%s location', '%s locations', count( $pins ), 'burn-events' ), number_format_i18n( count( $pins ) ) ),
|
|
];
|
|
echo '<p class="be-map-count">' . esc_html( implode( ' · ', $counts ) ) . '</p>';
|
|
if ( ! $enabled ) {
|
|
echo '<p class="be-empty">' . esc_html__( 'Maps are disabled on this site.', 'burn-events' ) . '</p>';
|
|
}
|
|
?>
|
|
<div class="be-map-layout<?php echo $enabled ? '' : ' be-map-layout-list'; ?>">
|
|
<?php if ( $enabled ) { echo self::canvas( $pins, __( 'Event locations', 'burn-events' ) ); } ?>
|
|
<section class="be-map-locations" aria-label="<?php esc_attr_e( 'Locations in this date range', 'burn-events' ); ?>">
|
|
<?php foreach ( $pins as $key => $pin ) : ?>
|
|
<details class="be-map-location">
|
|
<summary><span><?php echo esc_html( implode( ' / ', array_column( $pin['places'], 'name' ) ) ); ?></span>
|
|
<span class="be-map-location-count"><?php echo esc_html( number_format_i18n( count( $pin['events'] ) ) ); ?></span>
|
|
</summary>
|
|
<div class="be-map-location-body">
|
|
<?php self::event_links( $pin['events'] ); ?>
|
|
<?php if ( $enabled ) : ?><button class="be-map-focus" type="button" data-be-map-focus="<?php echo esc_attr( $key ); ?>" hidden><?php esc_html_e( 'Center map here', 'burn-events' ); ?></button><?php endif; ?>
|
|
</div>
|
|
</details>
|
|
<?php endforeach; ?>
|
|
</section>
|
|
</div>
|
|
<?php
|
|
} else {
|
|
echo '<p class="be-empty">' . esc_html__( 'No events have mapped venues in this date range.', 'burn-events' ) . '</p>';
|
|
}
|
|
if ( $unmapped ) {
|
|
echo '<details class="be-map-location be-map-unmapped"><summary>' . esc_html__( 'Events without a map pin', 'burn-events' ) . '</summary><div class="be-map-location-body">';
|
|
self::event_links( $unmapped );
|
|
echo '</div></details>';
|
|
}
|
|
}
|
|
|
|
private static function title( int $id ): string {
|
|
return html_entity_decode( wp_strip_all_tags( get_the_title( $id ) ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
|
|
}
|
|
|
|
private static function place( WP_Post $venue ): ?array {
|
|
$data = Events::location_data( $venue->ID );
|
|
if ( ! Maps::valid_coordinates( $data['latitude'], $data['longitude'] ) ) {
|
|
return null;
|
|
}
|
|
return [
|
|
'id' => $venue->ID, 'name' => self::title( $venue->ID ), 'address' => Maps::address( $data ),
|
|
'latitude' => (float) $data['latitude'], 'longitude' => (float) $data['longitude'],
|
|
'url' => esc_url_raw( get_permalink( $venue ), [ 'http', 'https' ] ), 'geocode' => $data['geocode'],
|
|
];
|
|
}
|
|
|
|
private static function add_pin( array &$pins, array $place, ?array $event = null ): void {
|
|
$key = sprintf( '%.6F,%.6F', $place['latitude'], $place['longitude'] );
|
|
if ( ! isset( $pins[ $key ] ) ) {
|
|
$pins[ $key ] = [
|
|
'key' => $key, 'latitude' => $place['latitude'], 'longitude' => $place['longitude'],
|
|
'places' => [], 'events' => [],
|
|
];
|
|
}
|
|
$pins[ $key ]['places'][ $place['id'] ] = $place;
|
|
if ( $event ) {
|
|
$pins[ $key ]['events'][ $event['url'] ] = $event;
|
|
}
|
|
}
|
|
|
|
private static function canvas( array $pins, string $label, bool $compact = false, int $event_id = 0 ): string {
|
|
wp_enqueue_style( self::STYLE );
|
|
wp_enqueue_script( self::SCRIPT );
|
|
ob_start();
|
|
?>
|
|
<div class="be-map<?php echo $compact ? ' be-map-compact' : ''; ?>" data-be-map<?php if ( $event_id ) : ?> data-be-event-map="<?php echo esc_attr( (string) $event_id ); ?>"<?php endif; ?>>
|
|
<div class="be-map-canvas" role="region" aria-label="<?php echo esc_attr( $label ); ?>"></div>
|
|
<script type="application/json" class="be-map-pins"><?php echo wp_json_encode( array_values( $pins ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); ?></script>
|
|
<p class="be-map-error" hidden><?php esc_html_e( 'The map tiles are temporarily unavailable.', 'burn-events' ); ?></p>
|
|
<noscript><p><?php esc_html_e( 'Enable JavaScript to view the map.', 'burn-events' ); ?></p></noscript>
|
|
<?php
|
|
$credits = [];
|
|
foreach ( $pins as $pin ) {
|
|
foreach ( $pin['places'] as $place ) {
|
|
$geocode = $place['geocode'];
|
|
if ( $geocode ) {
|
|
$key = ( $geocode['provider'] ?? '' ) . '|' . ( $geocode['attribution'] ?? '' );
|
|
$credits[ $key ] = $place;
|
|
}
|
|
}
|
|
}
|
|
foreach ( $credits as $place ) {
|
|
Maps::attribution( $place );
|
|
}
|
|
?>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
private static function event_links( array $events ): void {
|
|
echo '<ul class="be-map-event-links">';
|
|
foreach ( $events as $event ) {
|
|
echo '<li><a href="' . esc_url( $event['url'] ) . '">' . esc_html( $event['title'] ) . '</a>';
|
|
echo '<span>' . esc_html( $event['date'] ) . '</span></li>';
|
|
}
|
|
echo '</ul>';
|
|
}
|
|
}
|