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 = '';
}
if ( $image && 'yes' === ( $settings['link_image'] ?? '' ) ) {
$image = '' . $image . '';
}
return $image ? '
' . esc_html( $value ) . '
'; } private function date_badge(): string { $start = $this->start(); if ( ! $start ) { return ''; } return ''; } 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 '