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 ? '
' . $image . '
' : ''; } 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 ? '
' . $map . '
' : ''; } $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 = '' . $content . ''; } return '<' . $tag . ' class="ell-field ell-title">' . $content . ''; } 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 '' . esc_html( $label ) . ''; } if ( in_array( $field, [ 'categories', 'tags' ], true ) ) { $terms = $this->terms( 'categories' === $field ? Events::CATEGORY : 'post_tag' ); $chips = array_map( static fn( string $term ): string => '' . esc_html( $term ) . '', $terms ); return '
' . implode( '', $chips ) . '
'; } if ( 'schedule' === $field && ( $start = $this->start() ) ) { return ''; } return '

' . 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 '
' . esc_html__( 'Share', 'eventlabs-loop-builder' ) . '
' . '' . '' . '' . esc_html__( 'Email', 'eventlabs-loop-builder' ) . '
'; } public function built_in(): string { $fields = [ 'categories', 'schedule', 'title', 'venue', 'excerpt' ]; $meta = $this->field( 'cost' ) . $this->field( 'status' ); return '
' . '
' . $this->field( 'image', [ 'show_placeholder' => 'yes' ] ) . '
' . '
' . implode( '', array_map( $this->field( ... ), $fields ) ) . ( $meta ? '
' . $meta . '
' : '' ) . $this->field( 'link' ) . '
' . '
'; } }