Files
eventLabs/addons/eventlabs-loop-builder/includes/class-card.php
josh d15dce1e84
All checks were successful
WordPress release / release (push) Successful in 5s
Loop Builder release / release (push) Successful in 4s
Add Elementor event loop preset library and separate releases
2026-11-13 11:00:14 -05:00

216 lines
10 KiB
PHP

<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Admin;
use BurnEvents\Calendar;
use BurnEvents\Events;
use BurnEvents\Map_View;
use BurnEvents\Maps;
use DateTimeImmutable;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Card {
private array $data;
public function __construct( private readonly WP_Post $event, private readonly ?array $occurrence ) {
$this->data = Events::event_data( $event->ID );
}
public static function fields(): array {
return [
'title' => __( 'Event title', 'eventlabs-loop-builder' ),
'image' => __( 'Featured image', 'eventlabs-loop-builder' ),
'schedule' => __( 'Date and time', 'eventlabs-loop-builder' ),
'date' => __( 'Start date', 'eventlabs-loop-builder' ),
'date_badge' => __( 'Date badge', 'eventlabs-loop-builder' ),
'time' => __( 'Start time', 'eventlabs-loop-builder' ),
'venue' => __( 'Venue', 'eventlabs-loop-builder' ),
'address' => __( 'Venue address', 'eventlabs-loop-builder' ),
'map' => __( 'Venue map', 'eventlabs-loop-builder' ),
'organizer' => __( 'Organizers', 'eventlabs-loop-builder' ),
'series' => __( 'Series', 'eventlabs-loop-builder' ),
'excerpt' => __( 'Excerpt', 'eventlabs-loop-builder' ),
'categories' => __( 'Categories', 'eventlabs-loop-builder' ),
'tags' => __( 'Tags', 'eventlabs-loop-builder' ),
'cost' => __( 'Cost', 'eventlabs-loop-builder' ),
'status' => __( 'Status or featured label', 'eventlabs-loop-builder' ),
'link' => __( 'Event link', 'eventlabs-loop-builder' ),
'website' => __( 'Event website', 'eventlabs-loop-builder' ),
'share' => __( 'Share event', 'eventlabs-loop-builder' ),
'custom' => __( 'Custom event field', 'eventlabs-loop-builder' ),
];
}
public static function text_fields(): array {
return array_diff_key( self::fields(), array_flip( [ 'image', 'link', 'website', 'date_badge', 'share', 'map' ] ) );
}
public static function custom_fields(): array {
return array_map( static fn( array $field ): string => $field['label'], Admin::options()['fields'] );
}
public function link(): string {
return (string) ( $this->occurrence ? Events::event_url( $this->occurrence ) : get_permalink( $this->event ) );
}
public function image(): array {
$id = (int) get_post_thumbnail_id( $this->event );
return [ 'id' => $id, 'url' => $id ? (string) wp_get_attachment_image_url( $id, 'large' ) : '' ];
}
public function month(): ?DateTimeImmutable {
return $this->occurrence ? Events::local_time( $this->occurrence['starts_at'], wp_timezone_string() ) : null;
}
private function start(): ?DateTimeImmutable {
return $this->occurrence ? Events::local_time( $this->occurrence['starts_at'], $this->data['schedule']['timezone'] ?? wp_timezone_string() ) : null;
}
private function formatted_start( string $format ): string {
$start = $this->start();
return $start ? wp_date( $format, $start->getTimestamp(), $start->getTimezone() ) : '';
}
private function relations( string $relation ): string {
$posts = Events::locations( $this->data[ $relation ], Events::RELATIONS[ $relation ] );
return implode( ', ', array_map( 'get_the_title', $posts ) );
}
private function terms( string $taxonomy ): array {
$terms = get_the_terms( $this->event, $taxonomy );
return is_array( $terms ) ? wp_list_pluck( $terms, 'name' ) : [];
}
private function custom_value( string $field ): string {
$settings = Admin::options()['fields'][ $field ] ?? null;
$value = $this->data['fields'][ $field ] ?? '';
if ( ! $settings || '' === $value ) {
return '';
}
return 'checkbox' === $settings['type'] ? __( 'Yes', 'eventlabs-loop-builder' ) : (string) $value;
}
private function address(): string {
$venue = Events::locations( $this->data['venue'], Events::VENUE )[0] ?? null;
return $venue ? Maps::address( Events::location_data( $venue->ID ) ) : $this->data['location'];
}
public function value( string $field, string $custom = '' ): string {
return match ( $field ) {
'title' => get_the_title( $this->event ),
'schedule' => $this->occurrence ? Calendar::period( $this->occurrence, $this->data ) : '',
'date' => $this->formatted_start( (string) get_option( 'date_format' ) ),
'time' => ! empty( $this->data['schedule']['all_day'] ) ? __( 'All day', 'eventlabs-loop-builder' ) : $this->formatted_start( (string) get_option( 'time_format' ) ),
'venue' => Calendar::event_location( $this->data ),
'address' => $this->address(),
'organizer', 'series' => $this->relations( $field ),
'excerpt' => Calendar::event_excerpt( $this->event->ID ),
'categories' => implode( ', ', $this->terms( Events::CATEGORY ) ),
'tags' => implode( ', ', $this->terms( 'post_tag' ) ),
'cost' => '0' === $this->data['cost'] ? __( 'Free', 'eventlabs-loop-builder' ) : $this->data['cost'],
'status' => 'scheduled' !== $this->data['status']
? ( Events::statuses()[ $this->data['status'] ] ?? '' )
: ( $this->data['featured'] ? __( 'Featured', 'eventlabs-loop-builder' ) : '' ),
'custom' => $this->custom_value( $custom ),
'website' => $this->data['url'],
'link' => $this->link(),
default => '',
};
}
public function field( string $field, array $settings = [] ): string {
if ( 'image' === $field ) {
$size = (string) ( $settings['image_size'] ?? 'large' );
$image = get_the_post_thumbnail( $this->event, $size );
if ( ! $image && 'yes' === ( $settings['show_placeholder'] ?? '' ) ) {
$date = $this->formatted_start( 'M j' );
$image = '<span class="ell-image-placeholder" aria-hidden="true">' . esc_html( $date ) . '</span>';
}
if ( $image && 'yes' === ( $settings['link_image'] ?? '' ) ) {
$image = '<a href="' . esc_url( $this->link() ) . '" aria-label="' . esc_attr( get_the_title( $this->event ) ) . '">' . $image . '</a>';
}
return $image ? '<figure class="ell-field ell-image">' . $image . '</figure>' : '';
}
if ( 'date_badge' === $field ) {
return $this->date_badge();
}
if ( 'share' === $field ) {
return $this->share();
}
if ( 'map' === $field ) {
$map = Map_View::venues( Events::locations( $this->data['venue'], Events::VENUE ) );
return $map ? '<div class="ell-field ell-venue-map">' . $map . '</div>' : '';
}
$value = $this->value( $field, (string) ( $settings['custom_key'] ?? '' ) );
if ( 'excerpt' === $field && ! empty( $settings['excerpt_words'] ) ) {
$words = min( 100, max( 1, (int) $settings['excerpt_words'] ) );
$raw = $this->event->post_excerpt ?: $this->event->post_content;
$value = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $raw ) ), $words );
}
if ( '' === $value ) {
return '';
}
if ( 'title' === $field ) {
$tag = $settings['heading_tag'] ?? 'h3';
$tag = in_array( $tag, [ 'h2', 'h3', 'h4', 'h5', 'h6', 'p' ], true ) ? $tag : 'h3';
$content = esc_html( $value );
if ( 'yes' === ( $settings['link_title'] ?? 'yes' ) ) {
$content = '<a href="' . esc_url( $this->link() ) . '">' . $content . '</a>';
}
return '<' . $tag . ' class="ell-field ell-title">' . $content . '</' . $tag . '>';
}
if ( in_array( $field, [ 'link', 'website' ], true ) ) {
$label = trim( (string) ( $settings['link_label'] ?? '' ) );
if ( '' === $label ) {
$label = 'link' === $field ? __( 'View event', 'eventlabs-loop-builder' ) : __( 'Event website', 'eventlabs-loop-builder' );
}
/* translators: 1: link label; 2: event title */
$aria = sprintf( __( '%1$s: %2$s', 'eventlabs-loop-builder' ), $label, $this->value( 'title' ) );
return '<a class="ell-field ell-link" href="' . esc_url( $value ) . '" aria-label="' . esc_attr( $aria ) . '">' . esc_html( $label ) . '</a>';
}
if ( in_array( $field, [ 'categories', 'tags' ], true ) ) {
$terms = $this->terms( 'categories' === $field ? Events::CATEGORY : 'post_tag' );
$chips = array_map( static fn( string $term ): string => '<span class="ell-chip">' . esc_html( $term ) . '</span>', $terms );
return '<div class="ell-field ell-' . esc_attr( $field ) . '">' . implode( '', $chips ) . '</div>';
}
if ( 'schedule' === $field && ( $start = $this->start() ) ) {
return '<time class="ell-field ell-schedule" datetime="' . esc_attr( $start->format( DATE_ATOM ) ) . '">' . esc_html( $value ) . '</time>';
}
return '<p class="ell-field ell-' . esc_attr( $field ) . '">' . esc_html( $value ) . '</p>';
}
private function date_badge(): string {
$start = $this->start();
if ( ! $start ) {
return '';
}
return '<time class="ell-field ell-date-badge" datetime="' . esc_attr( $start->format( DATE_ATOM ) ) . '" aria-label="' . esc_attr( $this->formatted_start( 'l, F j, Y' ) ) . '">'
. '<span class="ell-date-month">' . esc_html( $this->formatted_start( 'M' ) ) . '</span>'
. '<strong class="ell-date-day">' . esc_html( $this->formatted_start( 'j' ) ) . '</strong>'
. '<span class="ell-date-weekday">' . esc_html( $this->formatted_start( 'D' ) ) . '</span></time>';
}
private function share(): string {
$url = $this->link();
$subject = wp_specialchars_decode( $this->value( 'title' ), ENT_QUOTES );
$email = 'mailto:?subject=' . rawurlencode( $subject ) . '&body=' . rawurlencode( $url );
return '<details class="ell-field ell-share"><summary>' . esc_html__( 'Share', 'eventlabs-loop-builder' ) . '</summary><div>'
. '<input type="url" readonly value="' . esc_attr( $url ) . '" aria-label="' . esc_attr__( 'Event URL', 'eventlabs-loop-builder' ) . '">'
. '<button type="button" data-ell-copy data-copied="' . esc_attr__( 'Link copied', 'eventlabs-loop-builder' ) . '">' . esc_html__( 'Copy link', 'eventlabs-loop-builder' ) . '</button>'
. '<a href="' . esc_url( $email ) . '">' . esc_html__( 'Email', 'eventlabs-loop-builder' ) . '</a><span class="ell-share-status" role="status"></span></div></details>';
}
public function built_in(): string {
$fields = [ 'categories', 'schedule', 'title', 'venue', 'excerpt' ];
$meta = $this->field( 'cost' ) . $this->field( 'status' );
return '<div class="ell-starter ell-starter-photo">'
. '<div class="ell-starter-media">' . $this->field( 'image', [ 'show_placeholder' => 'yes' ] ) . '</div>'
. '<div class="ell-starter-body">' . implode( '', array_map( $this->field( ... ), $fields ) )
. ( $meta ? '<div class="ell-starter-meta">' . $meta . '</div>' : '' ) . $this->field( 'link' ) . '</div>'
. '</div>';
}
}