Add Elementor event loop preset library and separate releases
All checks were successful
WordPress release / release (push) Successful in 5s
Loop Builder release / release (push) Successful in 4s

This commit is contained in:
josh
2026-11-13 11:00:14 -05:00
parent db4747d85f
commit d15dce1e84
20 changed files with 3617 additions and 17 deletions

View File

@@ -0,0 +1,215 @@
<?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>';
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Events;
defined( 'ABSPATH' ) || exit;
final class Library {
private const PAGE = 'eventlabs-loop-presets';
public static function register(): void {
add_action( 'admin_menu', [ self::class, 'menu' ] );
add_action( 'manage_posts_extra_tablenav', [ self::class, 'list_link' ] );
}
private static function url( array $filters = [] ): string {
return add_query_arg( [ 'post_type' => Events::EVENT, 'page' => self::PAGE ] + $filters, admin_url( 'edit.php' ) );
}
public static function menu(): void {
$label = __( 'Loop Presets', 'eventlabs-loop-builder' );
$page = add_submenu_page( 'edit.php?post_type=' . Events::EVENT, $label, $label, Templates::CAPABILITY, self::PAGE, [ self::class, 'render' ] );
if ( $page ) {
add_action( 'admin_enqueue_scripts', static function ( string $screen ) use ( $page ): void {
if ( $page === $screen ) {
wp_enqueue_style( ASSET . '-library', plugins_url( 'assets/library.css', FILE ), [ ASSET . '-presets' ], VERSION );
}
} );
}
}
public static function list_link( string $position ): void {
$screen = get_current_screen();
if ( 'top' === $position && $screen && Templates::POST_TYPE === $screen->post_type && current_user_can( Templates::CAPABILITY ) ) {
echo '<a class="button" href="' . esc_url( self::url() ) . '">' . esc_html__( 'Browse loop presets', 'eventlabs-loop-builder' ) . '</a>';
}
}
private static function selected( string $key, array $options ): string {
$value = isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ? sanitize_key( wp_unslash( $_GET[ $key ] ) ) : '';
return isset( $options[ $value ] ) ? $value : '';
}
public static function render(): void {
if ( ! current_user_can( Templates::CAPABILITY ) ) {
wp_die( esc_html__( 'You cannot view loop presets.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] );
}
$layouts = Presets::layouts();
$families = Presets::families();
$layout = self::selected( 'ell_layout', $layouts );
$family = self::selected( 'ell_family', $families );
$presets = array_filter( Presets::catalog(), static fn( array $style ): bool => ( ! $layout || $layout === $style['layout'] ) && ( ! $family || $family === $style['family'] ) );
?>
<div class="wrap ell-library">
<h1><?php esc_html_e( 'Loop Presets', 'eventlabs-loop-builder' ); ?></h1>
<p><?php esc_html_e( 'Choose a style to create an editable copy in Elementor. You can also select any preset directly in the Event Loop widget.', 'eventlabs-loop-builder' ); ?></p>
<div class="ell-library-toolbar">
<form class="ell-library-filters" method="get" action="<?php echo esc_url( admin_url( 'edit.php' ) ); ?>">
<input type="hidden" name="post_type" value="<?php echo esc_attr( Events::EVENT ); ?>">
<input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE ); ?>">
<?php self::select( 'ell_layout', __( 'Layout', 'eventlabs-loop-builder' ), [ '' => __( 'All layouts', 'eventlabs-loop-builder' ) ] + $layouts, $layout ); ?>
<?php self::select( 'ell_family', __( 'Style', 'eventlabs-loop-builder' ), [ '' => __( 'All styles', 'eventlabs-loop-builder' ) ] + $families, $family ); ?>
<button class="button" type="submit"><?php esc_html_e( 'Filter presets', 'eventlabs-loop-builder' ); ?></button>
<?php if ( $layout || $family ) : ?>
<a href="<?php echo esc_url( self::url() ); ?>"><?php esc_html_e( 'Clear filters', 'eventlabs-loop-builder' ); ?></a>
<?php endif; ?>
</form>
<a class="button" href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . Templates::POST_TYPE ) ); ?>"><?php esc_html_e( 'My loop templates', 'eventlabs-loop-builder' ); ?></a>
</div>
<p class="ell-library-count"><?php
/* translators: %s: number of presets. */
printf( esc_html( _n( '%s preset', '%s presets', count( $presets ), 'eventlabs-loop-builder' ) ), esc_html( number_format_i18n( count( $presets ) ) ) );
?></p>
<div class="ell-library-grid">
<?php foreach ( $presets as $slug => $style ) : ?>
<article class="ell-library-item">
<div class="ell-preset-preview" aria-hidden="true"><?php echo Presets::sample( $slug ); // Preset renderer escapes its content. ?></div>
<div class="ell-library-details">
<p class="ell-library-type"><?php echo esc_html( $families[ $style['family'] ] . ' · ' . $layouts[ $style['layout'] ] ); ?></p>
<h2><?php echo esc_html( $style['title'] ); ?></h2>
<p class="ell-library-description"><?php echo esc_html( $style['description'] ); ?></p>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
<input type="hidden" name="action" value="<?php echo esc_attr( Templates::STARTER_ACTION ); ?>">
<input type="hidden" name="preset" value="<?php echo esc_attr( $slug ); ?>">
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( Templates::STARTER_ACTION ) ); ?>">
<button class="button button-primary" type="submit" aria-label="<?php
/* translators: 1: style family, 2: layout, 3: preset name. */
printf( esc_attr__( 'Use %1$s %2$s %3$s', 'eventlabs-loop-builder' ), esc_attr( $families[ $style['family'] ] ), esc_attr( $layouts[ $style['layout'] ] ), esc_attr( $style['title'] ) );
?>"><?php esc_html_e( 'Use preset', 'eventlabs-loop-builder' ); ?></button>
</form>
</div>
</article>
<?php endforeach; ?>
</div>
</div>
<?php
}
private static function select( string $name, string $label, array $options, string $current ): void {
?>
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $label ); ?></label>
<select id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>">
<?php foreach ( $options as $value => $title ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current, $value ); ?>><?php echo esc_html( $title ); ?></option>
<?php endforeach; ?>
</select>
<?php
}
}

View File

@@ -0,0 +1,259 @@
<?php
namespace EventLabs\LoopBuilder;
use Elementor\Utils;
defined( 'ABSPATH' ) || exit;
final class Presets {
public static function families(): array {
return [
'classic' => __( 'Classic', 'eventlabs-loop-builder' ),
'minimal' => __( 'Minimal', 'eventlabs-loop-builder' ),
'modern' => __( 'Modern', 'eventlabs-loop-builder' ),
];
}
public static function layouts(): array {
return [
'grid' => __( 'Grid', 'eventlabs-loop-builder' ),
'list' => __( 'List', 'eventlabs-loop-builder' ),
'carousel' => __( 'Carousel', 'eventlabs-loop-builder' ),
];
}
public static function catalog(): array {
static $catalog;
if ( isset( $catalog ) ) {
return $catalog;
}
$catalog = [];
foreach ( self::recipes() as $family => $recipes ) {
foreach ( array_keys( self::layouts() ) as $layout ) {
foreach ( $recipes as $name => $recipe ) {
$catalog[ "$family-$layout-$name" ] = [
'title' => $recipe['title'],
'description' => $recipe['description'],
'family' => $family,
'layout' => $layout,
'recipe' => $name,
];
}
}
}
return $catalog;
}
public static function elements( string $slug ): array {
$tree = self::tree( $slug );
return $tree ? [ self::element( $tree, false ) ] : [];
}
public static function render( string $slug, Card $card ): string {
$tree = self::tree( $slug );
return $tree ? self::markup( $tree, $card ) : '';
}
public static function sample( string $slug ): string {
$tree = self::tree( $slug );
return $tree ? self::markup( $tree, null ) : '';
}
private static function recipes(): array {
static $recipes;
if ( isset( $recipes ) ) {
return $recipes;
}
$group = static fn( string $name, array $children ): array => [ 'classes' => 'ell-preset-' . $name, 'children' => $children ];
$photo = static fn( string $size = 'large' ): array => $group( 'media', [
[ 'image', [ 'show_placeholder' => 'yes', 'image_size' => $size ] ],
] );
$excerpt = static fn( int $words ): array => [ 'excerpt', [ 'excerpt_words' => $words ] ];
$recipes = [
'classic' => [
'program' => [
'title' => __( 'Program', 'eventlabs-loop-builder' ),
'description' => __( 'Wide photo, event details, and a button.', 'eventlabs-loop-builder' ),
'parts' => [
$photo(),
$group( 'body', [ 'categories', 'schedule', 'title', 'venue', $excerpt( 24 ), 'link' ] ),
],
],
'gazette' => [
'title' => __( 'Gazette', 'eventlabs-loop-builder' ),
'description' => __( 'Editorial headline with a framed photo and venue address.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'body', [ 'categories', 'title', 'schedule', $excerpt( 20 ), 'address', 'link' ] ),
$photo( 'medium_large' ),
],
],
'invitation' => [
'title' => __( 'Invitation', 'eventlabs-loop-builder' ),
'description' => __( 'Centered type with a date medallion and event details.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'body', [ 'date_badge', 'categories', 'title', 'venue', 'schedule', 'link' ] ),
],
],
'ticket' => [
'title' => __( 'Ticket', 'eventlabs-loop-builder' ),
'description' => __( 'Perforated date stub beside the title, venue, and price.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'rail', [ 'date_badge', 'cost' ] ),
$group( 'body', [ 'categories', 'title', 'schedule', 'venue', 'link' ] ),
],
],
],
'minimal' => [
'lineup' => [
'title' => __( 'Lineup', 'eventlabs-loop-builder' ),
'description' => __( 'Date marker, title, and venue in a compact row.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'rail', [ 'date_badge' ] ),
$group( 'body', [ 'title', 'time', 'venue', 'link' ] ),
],
],
'bulletin' => [
'title' => __( 'Bulletin', 'eventlabs-loop-builder' ),
'description' => __( 'Text-led card with a headline, schedule, and short description.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'body', [ 'categories', 'title', 'schedule', $excerpt( 19 ), 'link' ] ),
],
],
'snapshot' => [
'title' => __( 'Snapshot', 'eventlabs-loop-builder' ),
'description' => __( 'Small square photo beside the headline and schedule.', 'eventlabs-loop-builder' ),
'parts' => [
$photo( 'medium' ),
$group( 'body', [ 'title', 'schedule', 'venue' ] ),
],
],
'agenda' => [
'title' => __( 'Agenda', 'eventlabs-loop-builder' ),
'description' => __( 'Schedule strip above a simple title and location.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'strip', [ 'schedule' ] ),
$group( 'body', [ 'title', 'venue', 'address', 'link' ] ),
],
],
],
'modern' => [
'cover' => [
'title' => __( 'Cover', 'eventlabs-loop-builder' ),
'description' => __( 'Edge-to-edge photo with the title and schedule overlaid.', 'eventlabs-loop-builder' ),
'parts' => [
$photo(),
$group( 'body', [ 'categories', 'title', 'schedule', 'venue', 'link' ] ),
],
],
'split' => [
'title' => __( 'Split', 'eventlabs-loop-builder' ),
'description' => __( 'Photo and a dark detail panel with event actions.', 'eventlabs-loop-builder' ),
'parts' => [
$photo(),
$group( 'body', [
'categories', 'title', 'schedule', 'venue', $excerpt( 18 ),
$group( 'actions', [ 'link', 'share' ] ),
] ),
],
],
'panorama' => [
'title' => __( 'Panorama', 'eventlabs-loop-builder' ),
'description' => __( 'Photo with a floating date badge and a light details panel.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'hero', [ $photo(), $group( 'badge', [ 'date_badge' ] ) ] ),
$group( 'body', [ 'categories', 'title', 'schedule', 'venue', $group( 'actions', [ 'cost', 'link' ] ) ] ),
],
],
'studio' => [
'title' => __( 'Studio', 'eventlabs-loop-builder' ),
'description' => __( 'Bold text card with a color accent, schedule, and price.', 'eventlabs-loop-builder' ),
'parts' => [
$group( 'strip', [ 'categories', 'status' ] ),
$group( 'body', [ 'title', 'schedule', $excerpt( 22 ), 'organizer', $group( 'actions', [ 'cost', 'link' ] ) ] ),
],
],
],
];
return $recipes;
}
private static function tree( string $slug ): array {
$preset = self::catalog()[ $slug ] ?? null;
if ( ! $preset ) {
return [];
}
[ 'family' => $family, 'layout' => $layout, 'recipe' => $recipe ] = $preset;
return [
'classes' => "ell-preset ell-preset-family-$family ell-preset-layout-$layout ell-preset-style-$recipe",
'children' => self::recipes()[ $family ][ $recipe ]['parts'],
];
}
private static function field( string|array $node ): array {
return is_string( $node ) ? [ $node, [] ] : $node;
}
private static function element( string|array $node, bool $inner = true ): array {
$id = Utils::generate_random_string();
if ( isset( $node['children'] ) ) {
return [
'id' => $id, 'elType' => 'container', 'isInner' => $inner,
'settings' => [ 'content_width' => 'full', 'css_classes' => $node['classes'] ],
'elements' => array_map( self::element( ... ), $node['children'] ),
];
}
[ $key, $settings ] = self::field( $node );
return [
'id' => $id, 'elType' => 'widget', 'widgetType' => 'eventlabs-loop-field',
'settings' => [ 'field' => $key ] + $settings,
'elements' => [],
];
}
private static function markup( string|array $node, ?Card $card ): string {
if ( isset( $node['children'] ) ) {
$children = '';
foreach ( $node['children'] as $child ) {
$children .= self::markup( $child, $card );
}
return '' === $children ? '' : '<div class="' . esc_attr( $node['classes'] ) . '">' . $children . '</div>';
}
[ $key, $settings ] = self::field( $node );
return $card ? $card->field( $key, $settings ) : self::sample_field( $key, $settings );
}
private static function sample_field( string $key, array $settings ): string {
switch ( $key ) {
case 'image':
return '<figure class="ell-field ell-image"><span class="ell-preset-sample-photo" aria-hidden="true"></span></figure>';
case 'date_badge':
return '<span class="ell-field ell-date-badge"><span class="ell-date-month">' . esc_html__( 'Jul', 'eventlabs-loop-builder' )
. '</span><strong class="ell-date-day">18</strong><span class="ell-date-weekday">' . esc_html__( 'Sat', 'eventlabs-loop-builder' ) . '</span></span>';
case 'categories':
return '<div class="ell-field ell-categories"><span class="ell-chip">' . esc_html__( 'Live music', 'eventlabs-loop-builder' ) . '</span></div>';
case 'title':
return '<p class="ell-field ell-title">' . esc_html__( 'Music in the gardens', 'eventlabs-loop-builder' ) . '</p>';
case 'schedule':
return '<span class="ell-field ell-schedule">' . esc_html__( 'July 18 · 7:00 pm', 'eventlabs-loop-builder' ) . '</span>';
case 'link':
return '<span class="ell-field ell-link">' . esc_html__( 'View event', 'eventlabs-loop-builder' ) . '</span>';
}
$label = match ( $key ) {
'time' => __( '7:00 pm', 'eventlabs-loop-builder' ),
'venue' => __( 'Botanical Garden', 'eventlabs-loop-builder' ),
'address' => __( '24 Garden Lane', 'eventlabs-loop-builder' ),
'organizer' => __( 'Garden Arts Collective', 'eventlabs-loop-builder' ),
'excerpt' => wp_trim_words( __( 'An evening of live music among the garden paths and summer blooms.', 'eventlabs-loop-builder' ), $settings['excerpt_words'] ?? 25 ),
'cost' => __( 'Free', 'eventlabs-loop-builder' ),
'share' => __( 'Share', 'eventlabs-loop-builder' ),
'status' => __( 'Featured', 'eventlabs-loop-builder' ),
default => '',
};
return '' === $label ? '' : '<p class="ell-field ell-' . esc_attr( $key ) . '">' . esc_html( $label ) . '</p>';
}
}

View File

@@ -0,0 +1,444 @@
<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Calendar;
use BurnEvents\Clubs;
use BurnEvents\Events;
use DateTimeImmutable;
use DateTimeZone;
use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use WP_Post;
use WP_Term;
defined( 'ABSPATH' ) || exit;
final class Query {
private const PAGE_LIMIT = 10000;
private const PAGE_SIZE = 50;
private const PUBLIC_KEYS = [ 'search', 'category', 'tag', 'venue', 'organizer', 'city', 'country', 'when', 'start', 'end' ];
public static function controls( Widget_Base $widget ): void {
$widget->add_control( 'source', [
'label' => __( 'Source', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [
'manual' => __( 'Select filters below', 'eventlabs-loop-builder' ),
'current' => __( 'Current event archive', 'eventlabs-loop-builder' ),
'related' => __( 'Related to the current event', 'eventlabs-loop-builder' ),
],
'default' => 'manual',
] );
$widget->add_control( 'period', [
'label' => __( 'Date scope', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [
'upcoming' => __( 'Upcoming and ongoing', 'eventlabs-loop-builder' ),
'past' => __( 'Past events', 'eventlabs-loop-builder' ),
'all' => __( 'All dates', 'eventlabs-loop-builder' ),
'range' => __( 'Date range', 'eventlabs-loop-builder' ),
] + self::periods(),
'default' => 'upcoming',
] );
foreach ( [ 'date_from' => __( 'From date', 'eventlabs-loop-builder' ), 'date_to' => __( 'Through date', 'eventlabs-loop-builder' ) ] as $name => $label ) {
$widget->add_control( $name, [
'label' => $label, 'type' => Controls_Manager::DATE_TIME,
'picker_options' => [ 'enableTime' => false, 'dateFormat' => 'Y-m-d' ],
'condition' => [ 'period' => 'range' ],
] );
}
$widget->add_control( 'order', [
'label' => __( 'Date order', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [ '' => __( 'Automatic', 'eventlabs-loop-builder' ), 'ASC' => __( 'Earliest first', 'eventlabs-loop-builder' ), 'DESC' => __( 'Latest first', 'eventlabs-loop-builder' ) ],
'default' => '',
] );
$widget->add_control( 'per_page', [
'label' => __( 'Occurrences per page', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => self::PAGE_SIZE, 'default' => 6,
] );
$widget->add_control( 'offset', [
'label' => __( 'Skip the first occurrences', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::NUMBER, 'min' => 0, 'max' => self::PAGE_LIMIT, 'default' => 0,
] );
$widget->add_control( 'category', [
'label' => __( 'Categories', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT2,
'options' => self::terms( Events::CATEGORY ), 'default' => [], 'multiple' => true,
] );
$widget->add_control( 'tag', [
'label' => __( 'Tags', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT2,
'options' => self::terms( 'post_tag' ), 'default' => [], 'multiple' => true,
] );
foreach ( self::relation_labels() as $type => $label ) {
$widget->add_control( $type . '_ids', [
'label' => $label, 'type' => Controls_Manager::SELECT2, 'multiple' => true, 'default' => [],
'options' => self::linked_options( Events::RELATIONS[ $type ] ),
] );
}
foreach ( [ 'city' => __( 'Cities', 'eventlabs-loop-builder' ), 'country' => __( 'Countries', 'eventlabs-loop-builder' ) ] as $field => $label ) {
$widget->add_control( $field, [
'label' => $label, 'type' => Controls_Manager::SELECT2, 'multiple' => true, 'default' => [],
'options' => self::places( $field ),
] );
}
$widget->add_control( 'scope', [
'label' => __( 'Club or sub-club', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT2,
'options' => Clubs::options(), 'default' => '',
] );
$widget->add_control( 'keyword', [ 'label' => __( 'Keyword', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::TEXT ] );
$widget->add_control( 'featured', [
'label' => __( 'Event type', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT, 'default' => '',
'options' => [ '' => __( 'All events', 'eventlabs-loop-builder' ), 'yes' => __( 'Featured events', 'eventlabs-loop-builder' ), 'regular' => __( 'Non-featured events', 'eventlabs-loop-builder' ) ],
] );
$widget->add_control( 'empty_message', [
'label' => __( 'Empty message', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::TEXT,
'default' => __( 'No events match these filters.', 'eventlabs-loop-builder' ),
] );
}
public static function filter_controls( Widget_Base $widget ): void {
$widget->add_control( 'public_filters', [
'label' => __( 'Show visitor filters', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER,
'description' => __( 'Visitor selections stay within the Event Query filters.', 'eventlabs-loop-builder' ),
] );
$widget->add_control( 'filter_fields', [
'label' => __( 'Visitor controls', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT2,
'multiple' => true, 'default' => [ 'search', 'category' ], 'condition' => [ 'public_filters' => 'yes' ],
'options' => [
'search' => __( 'Search', 'eventlabs-loop-builder' ), 'category' => __( 'Category', 'eventlabs-loop-builder' ),
'tag' => __( 'Tag', 'eventlabs-loop-builder' ), 'venue' => __( 'Venue', 'eventlabs-loop-builder' ),
'organizer' => __( 'Organizer', 'eventlabs-loop-builder' ), 'city' => __( 'City', 'eventlabs-loop-builder' ),
'country' => __( 'Country', 'eventlabs-loop-builder' ), 'date' => __( 'Date', 'eventlabs-loop-builder' ),
],
] );
}
private static function periods(): array {
return [
'today' => __( 'Today', 'eventlabs-loop-builder' ), 'tomorrow' => __( 'Tomorrow', 'eventlabs-loop-builder' ),
'week' => __( 'This week', 'eventlabs-loop-builder' ), 'next_week' => __( 'Next week', 'eventlabs-loop-builder' ),
'weekend' => __( 'This weekend', 'eventlabs-loop-builder' ), 'next_weekend' => __( 'Next weekend', 'eventlabs-loop-builder' ),
'month' => __( 'This month', 'eventlabs-loop-builder' ), 'next_month' => __( 'Next month', 'eventlabs-loop-builder' ),
];
}
private static function relation_labels(): array {
return [ 'venue' => __( 'Venues', 'eventlabs-loop-builder' ), 'organizer' => __( 'Organizers', 'eventlabs-loop-builder' ), 'series' => __( 'Series', 'eventlabs-loop-builder' ) ];
}
public static function results( array $settings, string $instance, bool $editing ): array {
$result = [ 'rows' => [], 'page' => 1, 'previous' => '', 'next' => '', 'filters' => '', 'error' => '' ];
$prefix = 'el_loop_' . $instance . '_';
$anchor = '#eventlabs-loop-' . $instance;
$pagination = ! $editing && 'none' !== self::pagination( $settings );
$page = $pagination ? max( 1, min( self::PAGE_LIMIT, absint( Calendar::request( $prefix . 'page' ) ) ) ) : 1;
$limit = max( 1, min( self::PAGE_SIZE, absint( $settings['per_page'] ?? 6 ) ) );
$result['page'] = $page;
$period = $settings['period'] ?? 'upcoming';
$now = gmdate( 'Y-m-d H:i:s' );
[ $from, $to, $invalid_range ] = self::date_range( (string) $period, (string) ( $settings['date_from'] ?? '' ), (string) ( $settings['date_to'] ?? '' ) );
if ( 'upcoming' === $period ) {
$from = $now;
}
$public = ! $editing && 'yes' === ( $settings['public_filters'] ?? '' ) ? (array) ( $settings['filter_fields'] ?? [ 'search', 'category' ] ) : [];
$public_options = self::public_options( $public );
$visitor = [];
foreach ( array_keys( $public_options ) as $key ) {
$value = wp_html_excerpt( Calendar::request( $prefix . $key ), 200 );
if ( '' !== $value ) {
$visitor[ $key ] = $value;
}
}
$keyword = wp_html_excerpt( (string) ( $settings['keyword'] ?? '' ), 200 );
if ( '' !== $keyword ) {
unset( $public_options['search'], $visitor['search'] );
}
$categories = self::keys( $settings['category'] ?? [] );
$tags = self::keys( $settings['tag'] ?? [] );
$relations = [];
foreach ( Events::RELATIONS as $type => $post_type ) {
$relations[ $type ] = self::ids( $settings[ $type . '_ids' ] ?? [] );
}
$legacy_type = (string) ( $settings['relation_type'] ?? '' );
if ( isset( Events::RELATIONS[ $legacy_type ] ) && ! $relations[ $legacy_type ] ) {
$relations[ $legacy_type ] = self::ids( $settings[ $legacy_type . '_id' ] ?? [] );
}
$exclude = 0;
$source = $settings['source'] ?? 'manual';
$object = 'manual' === $source ? null : get_queried_object();
$source_error = '';
if ( 'current' === $source ) {
if ( $object instanceof WP_Term && Events::CATEGORY === $object->taxonomy ) {
$categories = self::restrict( $categories, [ $object->slug ] );
} elseif ( $object instanceof WP_Post && ( $type = array_search( $object->post_type, Events::RELATIONS, true ) ) ) {
$relations[ $type ] = self::restrict( $relations[ $type ], [ $object->ID ] );
} elseif ( ! is_post_type_archive( Events::EVENT ) ) {
$source_error = __( 'Current event archive needs an event category, venue, organizer, series, or events archive.', 'eventlabs-loop-builder' );
}
}
if ( 'related' === $source ) {
$related = $object instanceof WP_Post && Events::EVENT === $object->post_type ? wp_get_post_terms( $object->ID, Events::CATEGORY, [ 'fields' => 'slugs' ] ) : [];
if ( is_array( $related ) && $related ) {
$categories = self::restrict( $categories, $related );
$exclude = $object->ID;
} else {
$source_error = __( 'Related events need an event page with at least one event category.', 'eventlabs-loop-builder' );
}
}
foreach ( [ 'category', 'tag', 'venue', 'organizer', 'city', 'country' ] as $key ) {
if ( isset( $visitor[ $key ] ) && ! isset( $public_options[ $key ]['options'][ $visitor[ $key ] ] ) ) {
unset( $visitor[ $key ] );
}
}
$categories = self::restrict( $categories, isset( $visitor['category'] ) ? [ $visitor['category'] ] : [] );
$tags = self::restrict( $tags, isset( $visitor['tag'] ) ? [ $visitor['tag'] ] : [] );
foreach ( [ 'venue', 'organizer' ] as $type ) {
if ( isset( $visitor[ $type ] ) ) {
$relations[ $type ] = self::restrict( $relations[ $type ], [ absint( $visitor[ $type ] ) ] );
}
}
$cities = self::restrict( self::keys( $settings['city'] ?? [] ), isset( $visitor['city'] ) ? [ $visitor['city'] ] : [] );
$countries = self::restrict( self::keys( $settings['country'] ?? [] ), isset( $visitor['country'] ) ? [ $visitor['country'] ] : [] );
if ( false !== $cities && false !== $countries && ( $cities || $countries ) ) {
$matching = [];
foreach ( self::linked_posts( Events::VENUE ) as $venue ) {
$data = Events::location_data( $venue->ID );
if ( ( ! $cities || in_array( sanitize_title( $data['city'] ), $cities, true ) ) && ( ! $countries || in_array( sanitize_title( $data['country'] ), $countries, true ) ) ) {
$matching[] = $venue->ID;
}
}
$relations['venue'] = $matching ? self::restrict( $relations['venue'], $matching ) : false;
}
$when = $visitor['when'] ?? '';
if ( ! isset( self::periods()[ $when ] ) ) {
unset( $visitor['when'] );
$when = isset( $visitor['start'] ) || isset( $visitor['end'] ) ? 'range' : '';
} else {
unset( $visitor['start'], $visitor['end'] );
}
[ $visitor_from, $visitor_to, $visitor_error ] = self::date_range( $when, $visitor['start'] ?? '', $visitor['end'] ?? '' );
$from = $from && $visitor_from ? max( $from, $visitor_from ) : ( $from ?: $visitor_from );
$to = $to && $visitor_to ? min( $to, $visitor_to ) : ( $to ?: $visitor_to );
$invalid = $invalid_range || $visitor_error || ( $from && $to && $from >= $to ) || in_array( false, [ ...array_values( $relations ), $categories, $tags, $cities, $countries ], true );
$past = 'past' === $period;
if ( $invalid_range || $source_error ) {
$result['error'] = $source_error ?: __( 'Enter a valid date range for this loop.', 'eventlabs-loop-builder' );
}
if ( ! $invalid && ! $source_error ) {
$rows = Events::query( [
'from' => $from, 'to' => $to, 'end_by' => $past ? $now : null,
'order' => ( $settings['order'] ?? '' ) ?: ( $past ? 'DESC' : 'ASC' ),
'search' => $keyword ?: ( $visitor['search'] ?? '' ), 'category' => $categories, 'tag' => $tags, 'relations' => $relations,
'scope' => sanitize_text_field( (string) ( $settings['scope'] ?? '' ) ),
'featured' => $settings['featured'] ?? '', 'exclude' => $exclude,
'offset' => ( $page - 1 ) * $limit + min( self::PAGE_LIMIT, absint( $settings['offset'] ?? 0 ) ),
'limit' => $pagination ? $limit + 1 : $limit,
] );
} else {
$rows = [];
}
$result['rows'] = array_slice( $rows, 0, $limit );
$has_next = $pagination && $page < self::PAGE_LIMIT && count( $rows ) > $limit;
if ( $page === 1 && ! $has_next && ! $public_options ) {
return $result;
}
[ $url, $state ] = self::page_state( $prefix );
$filters = [];
foreach ( $visitor as $key => $value ) {
$filters[ $prefix . $key ] = $value;
}
if ( $public_options ) {
$result['filters'] = self::filter_form( $url, $state, $anchor, $prefix, $public_options, $visitor, $visitor_error );
}
$state += $filters;
if ( $page > 1 ) {
$result['previous'] = self::url( $url, $page > 2 ? $state + [ $prefix . 'page' => $page - 1 ] : $state, $anchor );
}
if ( $has_next ) {
$result['next'] = self::url( $url, $state + [ $prefix . 'page' => $page + 1 ], $anchor );
}
return $result;
}
public static function pagination( array $settings ): string {
if ( isset( $settings['pagination_mode'] ) && in_array( $settings['pagination_mode'], [ 'links', 'more', 'none' ], true ) ) {
return $settings['pagination_mode'];
}
return 'yes' === ( $settings['pagination'] ?? 'yes' ) ? 'links' : 'none';
}
private static function restrict( array|false $values, array $subset ): array|false {
if ( false === $values || ! $subset ) {
return $values;
}
return $values ? ( array_values( array_intersect( $values, $subset ) ) ?: false ) : $subset;
}
private static function keys( $values ): array {
return array_values( array_filter( array_map( 'sanitize_title', array_filter( (array) $values, 'is_string' ) ), 'strlen' ) );
}
private static function ids( $values ): array {
return array_values( array_filter( array_map( 'absint', array_filter( (array) $values, 'is_scalar' ) ) ) );
}
private static function date_range( string $period, string $from, string $to ): array {
$today = current_datetime()->setTime( 0, 0 );
$week = $today->modify( '-' . ( ( (int) $today->format( 'w' ) - (int) get_option( 'start_of_week' ) + 7 ) % 7 ) . ' days' );
$saturday = $today->modify( 0 === (int) $today->format( 'w' ) ? '-1 day' : '+' . ( 6 - (int) $today->format( 'w' ) ) . ' days' );
$month = $today->modify( 'first day of this month' );
$error = false;
[ $start, $end ] = match ( $period ) {
'today' => [ $today, $today->modify( '+1 day' ) ],
'tomorrow' => [ $today->modify( '+1 day' ), $today->modify( '+2 days' ) ],
'week' => [ $week, $week->modify( '+7 days' ) ],
'next_week' => [ $week->modify( '+7 days' ), $week->modify( '+14 days' ) ],
'weekend' => [ $saturday, $saturday->modify( '+2 days' ) ],
'next_weekend' => [ $saturday->modify( '+7 days' ), $saturday->modify( '+9 days' ) ],
'month' => [ $month, $month->modify( '+1 month' ) ],
'next_month' => [ $month->modify( '+1 month' ), $month->modify( '+2 months' ) ],
'range' => [ self::date( $from ), self::date( $to )?->modify( '+1 day' ) ],
default => [ null, null ],
};
if ( 'range' === $period ) {
$error = ( ! $start && ! $end ) || ( '' !== $from && ! $start ) || ( '' !== $to && ! $end ) || ( $start && $end && $start >= $end );
}
$utc = new DateTimeZone( 'UTC' );
return [ $start?->setTimezone( $utc )->format( 'Y-m-d H:i:s' ), $end?->setTimezone( $utc )->format( 'Y-m-d H:i:s' ), $error ];
}
private static function date( string $value ): ?DateTimeImmutable {
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/D', $value ) ) {
return null;
}
$date = DateTimeImmutable::createFromFormat( '!Y-m-d', $value, wp_timezone() );
return $date && $date->format( 'Y-m-d' ) === $value ? $date : null;
}
private static function terms( string $taxonomy, bool $hide_empty = false ): array {
$terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => $hide_empty ] );
$options = [];
foreach ( is_array( $terms ) ? $terms : [] as $term ) {
$options[ $term->slug ] = $term->name;
}
return $options;
}
private static function linked_posts( string $type ): array {
static $posts = [];
return $posts[ $type ] ??= get_posts( [
'post_type' => $type, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => -1,
'orderby' => [ 'title' => 'ASC', 'ID' => 'ASC' ], 'update_post_meta_cache' => Events::VENUE === $type, 'update_post_term_cache' => false,
] );
}
private static function linked_options( string $type ): array {
$options = [];
foreach ( self::linked_posts( $type ) as $post ) {
$options[ $post->ID ] = get_the_title( $post );
}
return $options;
}
private static function places( string $field ): array {
$options = [];
foreach ( self::linked_posts( Events::VENUE ) as $venue ) {
$value = Events::location_data( $venue->ID )[ $field ];
if ( $value ) {
$options[ sanitize_title( $value ) ] = $value;
}
}
natcasesort( $options );
return $options;
}
private static function public_options( array $fields ): array {
$options = [];
foreach ( array_unique( $fields, SORT_REGULAR ) as $field ) {
$options += match ( $field ) {
'search' => [ 'search' => [ 'label' => __( 'Search events', 'eventlabs-loop-builder' ), 'type' => 'search' ] ],
'category' => [ 'category' => [ 'label' => __( 'Category', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All categories', 'eventlabs-loop-builder' ) ] + self::terms( Events::CATEGORY, true ) ] ],
'tag' => [ 'tag' => [ 'label' => __( 'Tag', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All tags', 'eventlabs-loop-builder' ) ] + self::terms( 'post_tag', true ) ] ],
'venue' => [ 'venue' => [ 'label' => __( 'Venue', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All venues', 'eventlabs-loop-builder' ) ] + self::linked_options( Events::VENUE ) ] ],
'organizer' => [ 'organizer' => [ 'label' => __( 'Organizer', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All organizers', 'eventlabs-loop-builder' ) ] + self::linked_options( Events::ORGANIZER ) ] ],
'city' => [ 'city' => [ 'label' => __( 'City', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All cities', 'eventlabs-loop-builder' ) ] + self::places( 'city' ) ] ],
'country' => [ 'country' => [ 'label' => __( 'Country', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'All countries', 'eventlabs-loop-builder' ) ] + self::places( 'country' ) ] ],
'date' => [
'when' => [ 'label' => __( 'Date', 'eventlabs-loop-builder' ), 'options' => [ '' => __( 'Any date or use range', 'eventlabs-loop-builder' ) ] + self::periods() ],
'start' => [ 'label' => __( 'From date', 'eventlabs-loop-builder' ), 'type' => 'date' ],
'end' => [ 'label' => __( 'Through date', 'eventlabs-loop-builder' ), 'type' => 'date' ],
],
default => [],
};
}
return $options;
}
private static function page_state( string $prefix ): array {
$url = get_pagenum_link( max( 1, absint( get_query_var( 'paged' ) ) ), false );
parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $params );
$route = [ 'page_id', 'p', 'page', 'paged', 'post_type', 'name', 'pagename', 's', 'cat', 'tag', 'taxonomy', 'term', 'author', 'year', 'monthnum', 'day', 'lang', 'be_occ' ];
$object = get_queried_object();
$type = $object instanceof WP_Term ? get_taxonomy( $object->taxonomy ) : ( $object instanceof WP_Post ? get_post_type_object( $object->post_type ) : null );
if ( $type && $type->query_var ) {
$route[] = $type->query_var;
}
$state = array_filter( array_intersect_key( $params, array_flip( $route ) ), 'is_string' );
$keys = implode( '|', [ ...self::PUBLIC_KEYS, 'page' ] );
$own_keys = array_map( static fn( string $key ): string => $prefix . $key, [ ...self::PUBLIC_KEYS, 'page' ] );
foreach ( array_keys( $_GET ) as $key ) {
if ( is_string( $key ) && ! in_array( $key, $own_keys, true ) && preg_match( '/^el_loop_[a-z0-9_-]{1,80}_(?:' . $keys . ')$/D', $key ) ) {
$value = wp_html_excerpt( Calendar::request( $key ), 200 );
if ( '' !== $value ) {
$state[ $key ] = $value;
}
}
}
return [ explode( '?', $url, 2 )[0], $state ];
}
private static function url( string $base, array $state, string $anchor ): string {
$query = http_build_query( $state, '', '&', PHP_QUERY_RFC3986 );
return $base . ( '' === $query ? '' : '?' . $query ) . $anchor;
}
private static function filter_form( string $url, array $state, string $anchor, string $prefix, array $controls, array $values, bool $error ): string {
ob_start();
?>
<form class="el-loop-filters" action="<?php echo esc_url( $url . $anchor ); ?>" method="get" aria-label="<?php esc_attr_e( 'Filter events', 'eventlabs-loop-builder' ); ?>">
<?php foreach ( $state as $name => $value ) : ?>
<input type="hidden" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>">
<?php endforeach; ?>
<?php if ( $error ) : ?>
<p class="ell-filter-error" role="alert"><?php esc_html_e( 'Enter a valid date range.', 'eventlabs-loop-builder' ); ?></p>
<?php endif; ?>
<?php foreach ( $controls as $key => $control ) : ?>
<?php $name = $prefix . $key; ?>
<div class="el-loop-filter-field">
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $control['label'] ); ?></label>
<?php if ( isset( $control['options'] ) ) : ?>
<select id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>">
<?php foreach ( $control['options'] as $value => $label ) : ?>
<option value="<?php echo esc_attr( (string) $value ); ?>" <?php selected( $values[ $key ] ?? '', $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<?php else : ?>
<input type="<?php echo esc_attr( $control['type'] ); ?>" id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $values[ $key ] ?? '' ); ?>" maxlength="200">
<?php endif; ?>
</div>
<?php endforeach; ?>
<div class="el-loop-filter-actions">
<button type="submit"><?php esc_html_e( 'Filter', 'eventlabs-loop-builder' ); ?></button>
<?php if ( $values ) : ?>
<a href="<?php echo esc_url( self::url( $url, $state, $anchor ) ); ?>"><?php esc_html_e( 'Clear filters', 'eventlabs-loop-builder' ); ?></a>
<?php endif; ?>
</div>
</form>
<?php
return (string) ob_get_clean();
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace EventLabs\LoopBuilder;
use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
defined( 'ABSPATH' ) || exit;
final class Field_Tag extends Tag {
public function get_name(): string {
return 'eventlabs-loop-event-field';
}
public function get_title(): string {
return __( 'Loop event field', 'eventlabs-loop-builder' );
}
public function get_group(): string {
return ASSET;
}
public function get_categories(): array {
return [ Module::TEXT_CATEGORY ];
}
protected function register_controls(): void {
$this->add_control( 'field', [
'label' => __( 'Event field', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => Card::text_fields(), 'default' => 'title',
] );
$this->add_control( 'custom_key', [
'label' => __( 'Custom field', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => Card::custom_fields(), 'condition' => [ 'field' => 'custom' ],
] );
}
public function render(): void {
$card = Templates::active_card();
if ( $card ) {
echo esc_html( $card->value( (string) $this->get_settings( 'field' ), (string) $this->get_settings( 'custom_key' ) ) );
}
}
}
final class Link_Tag extends Tag {
public function get_name(): string {
return 'eventlabs-loop-event-link';
}
public function get_title(): string {
return __( 'Loop event link', 'eventlabs-loop-builder' );
}
public function get_group(): string {
return ASSET;
}
public function get_categories(): array {
return [ Module::URL_CATEGORY ];
}
public function render(): void {
$card = Templates::active_card();
if ( $card ) {
echo esc_url_raw( $card->link() );
}
}
}
final class Image_Tag extends Data_Tag {
public function get_name(): string {
return 'eventlabs-loop-event-image';
}
public function get_title(): string {
return __( 'Loop event image', 'eventlabs-loop-builder' );
}
public function get_group(): string {
return ASSET;
}
public function get_categories(): array {
return [ Module::IMAGE_CATEGORY ];
}
protected function get_value( array $options = [] ): array {
$card = Templates::active_card();
return $card ? $card->image() : [ 'id' => 0, 'url' => '' ];
}
}

View File

@@ -0,0 +1,306 @@
<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Events;
use Elementor\Core\Base\Document;
use Elementor\Plugin as Elementor;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Templates {
public const POST_TYPE = 'eventlabs_loop_item';
public const CAPABILITY = 'edit_theme_options';
public const LAYOUT_META = '_eventlabs_loop_layout';
public const STARTER_ACTION = 'eventlabs_loop_create_starter';
private const PREVIEW_META = '_eventlabs_loop_preview_event';
private const PREVIEW_NONCE = 'eventlabs_loop_preview_nonce';
private const PREVIEW_ACTION = 'eventlabs_loop_save_preview';
private static ?Card $card = null;
private static array $preview_cards = [];
public static function register(): void {
add_action( 'init', [ self::class, 'register_type' ], 11 );
add_action( 'template_redirect', [ self::class, 'protect_preview' ], 0 );
add_action( 'add_meta_boxes_' . self::POST_TYPE, [ self::class, 'add_preview_box' ] );
add_action( 'save_post_' . self::POST_TYPE, [ self::class, 'save_preview' ] );
add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', [ self::class, 'layout_column' ], 10, 2 );
add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', [ self::class, 'columns' ] );
add_filter( 'post_row_actions', [ self::class, 'row_actions' ], 10, 2 );
add_action( 'admin_post_' . self::STARTER_ACTION, [ self::class, 'create_starter' ] );
add_action( 'admin_post_eventlabs_loop_duplicate', [ self::class, 'duplicate' ] );
add_filter( 'plugin_action_links_' . plugin_basename( FILE ), [ self::class, 'links' ] );
}
public static function register_type(): void {
register_post_type( self::POST_TYPE, [
'labels' => [
'name' => __( 'Loop Templates', 'eventlabs-loop-builder' ),
'singular_name' => __( 'Loop Template', 'eventlabs-loop-builder' ),
'add_new' => __( 'Add Loop Template', 'eventlabs-loop-builder' ),
'add_new_item' => __( 'Add Loop Template', 'eventlabs-loop-builder' ),
'edit_item' => __( 'Edit Loop Template', 'eventlabs-loop-builder' ),
'view_item' => __( 'Preview Loop Template', 'eventlabs-loop-builder' ),
'search_items' => __( 'Search loop templates', 'eventlabs-loop-builder' ),
'not_found' => __( 'No loop templates found.', 'eventlabs-loop-builder' ),
],
'public' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=' . Events::EVENT,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'rewrite' => false,
'map_meta_cap' => false,
'capabilities' => array_fill_keys( [
'edit_post', 'read_post', 'delete_post', 'edit_posts', 'edit_others_posts', 'publish_posts',
'read_private_posts', 'create_posts', 'edit_private_posts', 'edit_published_posts',
'delete_posts', 'delete_private_posts', 'delete_others_posts', 'delete_published_posts',
], self::CAPABILITY ),
'supports' => [ 'title', 'editor', 'revisions', 'elementor' ],
] );
}
public static function protect_preview(): void {
$singular = is_singular( self::POST_TYPE );
if ( ! $singular && ! in_array( self::POST_TYPE, (array) get_query_var( 'post_type' ), true ) ) {
return;
}
nocache_headers();
if ( ! $singular || ! current_user_can( 'edit_post', get_queried_object_id() ) ) {
wp_die( esc_html__( 'Page not found.', 'eventlabs-loop-builder' ), '', [ 'response' => 404 ] );
}
}
public static function links( array $links ): array {
if ( current_user_can( self::CAPABILITY ) ) {
$links[] = '<a href="' . esc_url( admin_url( 'edit.php?post_type=' . self::POST_TYPE ) ) . '">'
. esc_html__( 'Loop Templates', 'eventlabs-loop-builder' ) . '</a>';
}
return $links;
}
public static function is_editing(): bool {
$elementor = Elementor::instance();
return $elementor->editor->is_edit_mode() || $elementor->preview->is_preview_mode();
}
private static function is_template( ?WP_Post $post ): bool {
return $post && self::POST_TYPE === $post->post_type;
}
public static function current_template(): ?WP_Post {
$post = get_post();
if ( self::is_template( $post ) ) {
return $post;
}
$document = Elementor::instance()->documents->get_current();
$post = $document ? $document->get_main_post() : null;
return self::is_template( $post ) ? $post : null;
}
public static function active_card(): ?Card {
if ( self::$card ) {
return self::$card;
}
$template = self::current_template();
if ( ! $template || ! current_user_can( 'edit_post', $template->ID ) ) {
return null;
}
if ( ! array_key_exists( $template->ID, self::$preview_cards ) ) {
self::$preview_cards[ $template->ID ] = self::preview_card( $template->ID );
}
return self::$preview_cards[ $template->ID ];
}
public static function is_rendering_card(): bool {
return null !== self::$card;
}
private static function preview_card( int $template_id ): ?Card {
$id = (int) get_post_meta( $template_id, self::PREVIEW_META, true );
$event = $id ? get_post( $id ) : null;
if ( self::is_preview_event( $event ) ) {
return new Card( $event, Events::occurrence( $id ) );
}
$row = Events::query( [ 'from' => gmdate( 'Y-m-d H:i:s' ), 'limit' => 1 ] )[0] ?? null;
$event = $row ? get_post( (int) $row['event_id'] ) : null;
if ( self::is_preview_event( $event ) ) {
return new Card( $event, $row );
}
$event = self::recent_events( 1 )[0] ?? null;
return $event ? new Card( $event, Events::occurrence( $event->ID ) ) : null;
}
private static function is_preview_event( ?WP_Post $event ): bool {
return $event && Events::EVENT === $event->post_type && 'publish' === $event->post_status && '' === $event->post_password;
}
private static function recent_events( int $limit ): array {
return get_posts( [
'post_type' => Events::EVENT, 'post_status' => 'publish', 'has_password' => false,
'posts_per_page' => $limit, 'update_post_meta_cache' => false, 'update_post_term_cache' => false,
] );
}
public static function add_preview_box(): void {
add_meta_box( 'eventlabs-loop-preview', __( 'Preview event', 'eventlabs-loop-builder' ), [ self::class, 'preview_box' ], self::POST_TYPE, 'side' );
}
public static function preview_box( WP_Post $post ): void {
$id = (int) get_post_meta( $post->ID, self::PREVIEW_META, true );
wp_nonce_field( self::PREVIEW_ACTION, self::PREVIEW_NONCE );
?>
<p>
<label for="eventlabs-loop-preview-event"><?php esc_html_e( 'Preview event ID', 'eventlabs-loop-builder' ); ?></label>
<input class="widefat" id="eventlabs-loop-preview-event" name="eventlabs_loop_preview_event" type="text" inputmode="numeric"
pattern="[0-9]+" list="eventlabs-loop-recent-events" aria-describedby="eventlabs-loop-preview-help" value="<?php echo $id ? esc_attr( (string) $id ) : ''; ?>">
<datalist id="eventlabs-loop-recent-events">
<?php foreach ( self::recent_events( 60 ) as $event ) : ?>
<option value="<?php echo esc_attr( (string) $event->ID ); ?>"><?php echo esc_html( get_the_title( $event ) ); ?></option>
<?php endforeach; ?>
</datalist>
</p>
<p class="description" id="eventlabs-loop-preview-help"><?php esc_html_e( 'Choose a recent event or enter the ID of any published event. Leave blank to preview the next event automatically.', 'eventlabs-loop-builder' ); ?></p>
<?php
}
public static function save_preview( int $template_id ): void {
$nonce = $_POST[ self::PREVIEW_NONCE ] ?? '';
if ( ! is_string( $nonce ) || ! wp_verify_nonce( wp_unslash( $nonce ), self::PREVIEW_ACTION )
|| ! current_user_can( 'edit_post', $template_id ) || wp_is_post_autosave( $template_id ) || wp_is_post_revision( $template_id ) ) {
return;
}
$input = $_POST['eventlabs_loop_preview_event'] ?? '';
$id = is_string( $input ) && ctype_digit( $input ) ? (int) $input : 0;
if ( $id && self::is_preview_event( get_post( $id ) ) ) {
update_post_meta( $template_id, self::PREVIEW_META, $id );
} elseif ( '' === $input || '0' === $input ) {
delete_post_meta( $template_id, self::PREVIEW_META );
}
}
public static function columns( array $columns ): array {
return array_slice( $columns, 0, 2, true ) + [ 'ell_layout' => __( 'Layout', 'eventlabs-loop-builder' ) ] + $columns;
}
public static function layout_column( string $column, int $id ): void {
if ( 'ell_layout' === $column ) {
$layout = (string) get_post_meta( $id, self::LAYOUT_META, true );
echo esc_html( Presets::layouts()[ $layout ] ?? __( 'Any layout', 'eventlabs-loop-builder' ) );
}
}
public static function row_actions( array $actions, WP_Post $post ): array {
if ( self::is_template( $post ) && current_user_can( self::CAPABILITY ) && current_user_can( 'edit_post', $post->ID ) && 'trash' !== $post->post_status ) {
$url = add_query_arg( [ 'action' => 'eventlabs_loop_duplicate', 'template_id' => $post->ID ], admin_url( 'admin-post.php' ) );
$actions['ell_duplicate'] = '<a href="' . esc_url( wp_nonce_url( $url, 'eventlabs_loop_duplicate_' . $post->ID ) ) . '">' . esc_html__( 'Duplicate', 'eventlabs-loop-builder' ) . '</a>';
}
return $actions;
}
public static function create_starter(): void {
if ( ! current_user_can( self::CAPABILITY ) ) {
wp_die( esc_html__( 'You cannot create a loop template.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] );
}
check_admin_referer( self::STARTER_ACTION );
$preset = isset( $_POST['preset'] ) && is_string( $_POST['preset'] ) ? sanitize_key( wp_unslash( $_POST['preset'] ) ) : '';
$style = Presets::catalog()[ $preset ] ?? null;
if ( $style ) {
$title = self::preset_title( $style );
$elements = Presets::elements( $preset );
$meta = [ self::LAYOUT_META => $style['layout'] ];
} elseif ( in_array( $preset, [ 'photo', 'compact' ], true ) ) {
$title = 'compact' === $preset ? __( 'Compact event card', 'eventlabs-loop-builder' ) : __( 'Event photo card', 'eventlabs-loop-builder' );
$elements = require __DIR__ . '/starter-layout.php';
$meta = [];
} else {
wp_die( esc_html__( 'Choose a loop preset from the library.', 'eventlabs-loop-builder' ), '', [ 'response' => 400, 'back_link' => true ] );
}
self::create_draft( $title, wp_json_encode( $elements, JSON_THROW_ON_ERROR ), $meta );
}
public static function duplicate(): void {
$id = isset( $_GET['template_id'] ) && is_string( $_GET['template_id'] ) ? absint( $_GET['template_id'] ) : 0;
$post = $id ? get_post( $id ) : null;
if ( ! self::is_template( $post ) || 'trash' === $post->post_status || ! current_user_can( self::CAPABILITY ) || ! current_user_can( 'edit_post', $id ) ) {
wp_die( esc_html__( 'You cannot duplicate this loop template.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] );
}
check_admin_referer( 'eventlabs_loop_duplicate_' . $id );
$meta = array_intersect_key( get_post_meta( $id ), array_flip( [ self::LAYOUT_META, self::PREVIEW_META, '_elementor_page_settings', '_wp_page_template' ] ) );
/* translators: %s: loop template title. */
$title = sprintf( __( '%s (copy)', 'eventlabs-loop-builder' ), $post->post_title );
$settings = array_map( static fn( array $values ) => maybe_unserialize( $values[0] ), $meta );
self::create_draft( $title, (string) get_post_meta( $id, Document::ELEMENTOR_DATA_META_KEY, true ) ?: '[]', $settings );
}
private static function preset_title( array $style ): string {
/* translators: 1: style family, 2: layout, 3: preset name. */
return sprintf( __( '%1$s %2$s · %3$s', 'eventlabs-loop-builder' ), Presets::families()[ $style['family'] ], Presets::layouts()[ $style['layout'] ], $style['title'] );
}
private static function create_draft( string $title, string $elements, array $meta ): void {
$id = wp_insert_post( [
'post_type' => self::POST_TYPE,
'post_status' => 'draft',
'post_title' => wp_slash( $title ),
'meta_input' => wp_slash( [ Document::ELEMENTOR_DATA_META_KEY => $elements ] + $meta ),
], true );
if ( is_wp_error( $id ) ) {
wp_die( esc_html( $id->get_error_message() ), '', [ 'response' => 500, 'back_link' => true ] );
}
$document = Elementor::instance()->documents->get( $id );
if ( ! $document || get_post_meta( $id, Document::ELEMENTOR_DATA_META_KEY, true ) !== $elements ) {
wp_die( esc_html__( 'WordPress could not save the loop layout.', 'eventlabs-loop-builder' ), '', [ 'response' => 500, 'back_link' => true ] );
}
$document->set_is_built_with_elementor( true );
wp_safe_redirect( $document->get_edit_url() );
exit;
}
public static function published_layout( int $id ): ?WP_Post {
$post = $id ? get_post( $id ) : null;
if ( ! self::is_template( $post ) || 'publish' !== $post->post_status || '' !== $post->post_password ) {
return null;
}
$document = Elementor::instance()->documents->get( $id );
return $document && $document->is_built_with_elementor() ? $post : null;
}
public static function options(): array {
$templates = get_posts( [
'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'has_password' => false,
'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'update_post_term_cache' => false,
] );
$options = [ '' => __( 'Built-in event card', 'eventlabs-loop-builder' ) ];
foreach ( Presets::catalog() as $slug => $style ) {
$options[ $slug ] = self::preset_title( $style );
}
foreach ( $templates as $template ) {
if ( self::published_layout( $template->ID ) ) {
$options[ $template->ID ] = get_the_title( $template ) ?: __( '(Untitled template)', 'eventlabs-loop-builder' );
}
}
return $options;
}
public static function render( WP_Post $template, Card $card ): string {
$previous = self::$card;
self::$card = $card;
try {
// Render only the saved layout. The card context supplies the event and occurrence to the dynamic fields.
return Elementor::instance()->frontend->get_builder_content_for_display( $template->ID );
} finally {
self::$card = $previous;
}
}
}

View File

@@ -0,0 +1,338 @@
<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Events;
use BurnEvents\Map_View;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Plugin as Elementor;
use Elementor\Widget_Base;
defined( 'ABSPATH' ) || exit;
abstract class Loop_Builder_Widget extends Widget_Base {
public function get_categories(): array {
return [ 'burn-events' ];
}
public function get_style_depends(): array {
return [ ASSET . '-presets' ];
}
public function get_script_depends(): array {
return [ ASSET ];
}
protected function is_dynamic_content(): bool {
return true;
}
}
final class Loop_Widget extends Loop_Builder_Widget {
public function get_name(): string {
return 'eventlabs-event-loop';
}
public function get_title(): string {
return __( 'Event Loop', 'eventlabs-loop-builder' );
}
public function get_icon(): string {
return 'eicon-posts-grid';
}
public function get_keywords(): array {
return [ 'eventlabs', 'calendar', 'events', 'loop', 'card' ];
}
protected function register_controls(): void {
$this->start_controls_section( 'display', [ 'label' => __( 'Layout', 'eventlabs-loop-builder' ) ] );
$this->add_control( 'template_id', [
'label' => __( 'Loop template', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::SELECT2,
'options' => Templates::options(),
'description' => __( 'Choose a preset or a saved template. Browse editable copies under Events → Loop Presets.', 'eventlabs-loop-builder' ),
'default' => '',
] );
$this->add_control( 'layout', [
'label' => __( 'Display as', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::SELECT,
'options' => [ 'auto' => __( 'Use template layout', 'eventlabs-loop-builder' ) ] + Presets::layouts(),
'default' => 'auto',
] );
$this->add_responsive_control( 'columns', [
'label' => __( 'Columns', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::NUMBER,
'min' => 1, 'max' => 6, 'step' => 1,
'default' => 3, 'tablet_default' => 2, 'mobile_default' => 1,
'condition' => [ 'layout!' => 'list' ],
'selectors' => [ '{{WRAPPER}} > .elementor-widget-container > .ell-loop, {{WRAPPER}} > .ell-loop' => '--ell-columns: {{VALUE}};' ],
] );
$this->add_responsive_control( 'gap', [
'label' => __( 'Card spacing', 'eventlabs-loop-builder' ),
'type' => Controls_Manager::SLIDER,
'size_units' => [ 'px', 'rem' ],
'range' => [ 'px' => [ 'min' => 0, 'max' => 64 ], 'rem' => [ 'min' => 0, 'max' => 4, 'step' => .125 ] ],
'selectors' => [ '{{WRAPPER}} > .elementor-widget-container > .ell-loop, {{WRAPPER}} > .ell-loop' => '--ell-gap: {{SIZE}}{{UNIT}};' ],
] );
$this->add_control( 'equal_height', [ 'label' => __( 'Equal-height cards', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
$this->add_control( 'month_headers', [ 'label' => __( 'Group list by month', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'layout' => [ 'auto', 'list' ] ] ] );
$this->end_controls_section();
$this->start_controls_section( 'query', [ 'label' => __( 'Event query', 'eventlabs-loop-builder' ) ] );
Query::controls( $this );
$this->end_controls_section();
$this->start_controls_section( 'filter_bar', [ 'label' => __( 'Filter bar', 'eventlabs-loop-builder' ) ] );
Query::filter_controls( $this );
$this->end_controls_section();
$this->start_controls_section( 'page_controls', [ 'label' => __( 'Pagination', 'eventlabs-loop-builder' ) ] );
$this->add_control( 'pagination_mode', [
'label' => __( 'Pagination', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [ '' => __( 'Default (page links)', 'eventlabs-loop-builder' ), 'links' => __( 'Page links', 'eventlabs-loop-builder' ), 'more' => __( 'Load more', 'eventlabs-loop-builder' ), 'none' => __( 'None', 'eventlabs-loop-builder' ) ],
'default' => '',
] );
$this->add_control( 'more_label', [ 'label' => __( 'Load more label', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::TEXT, 'default' => __( 'Load more events', 'eventlabs-loop-builder' ), 'condition' => [ 'pagination_mode' => 'more' ] ] );
$this->end_controls_section();
$this->start_controls_section( 'carousel_options', [ 'label' => __( 'Carousel', 'eventlabs-loop-builder' ), 'condition' => [ 'layout' => [ 'auto', 'carousel' ] ] ] );
$this->add_control( 'carousel_arrows', [ 'label' => __( 'Show arrows', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
$this->add_control( 'carousel_indicator', [
'label' => __( 'Slide indicator', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [ '' => __( 'None', 'eventlabs-loop-builder' ), 'dots' => __( 'Dots', 'eventlabs-loop-builder' ), 'fraction' => __( 'Fraction', 'eventlabs-loop-builder' ), 'progress' => __( 'Progress bar', 'eventlabs-loop-builder' ) ],
'default' => 'dots',
] );
$this->add_control( 'carousel_autoplay', [ 'label' => __( 'Autoplay', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER ] );
$this->add_control( 'carousel_interval', [ 'label' => __( 'Seconds between slides', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::NUMBER, 'min' => 2, 'max' => 30, 'default' => 5, 'condition' => [ 'carousel_autoplay' => 'yes' ] ] );
$this->add_control( 'carousel_step', [ 'label' => __( 'Slides per move', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => 6, 'default' => 1 ] );
$this->add_control( 'carousel_loop', [ 'label' => __( 'Wrap at the ends', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER ] );
$this->add_control( 'carousel_effect', [
'label' => __( 'Slide effect', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [ 'slide' => __( 'Slide', 'eventlabs-loop-builder' ), 'focus' => __( 'Focus active card', 'eventlabs-loop-builder' ) ], 'default' => 'slide',
] );
$this->end_controls_section();
$this->start_controls_section( 'loop_style', [ 'label' => __( 'Event Loop', 'eventlabs-loop-builder' ), 'tab' => Controls_Manager::TAB_STYLE ] );
foreach ( [ 'accent' => __( 'Accent color', 'eventlabs-loop-builder' ), 'ink' => __( 'Text color', 'eventlabs-loop-builder' ), 'line' => __( 'Control borders', 'eventlabs-loop-builder' ) ] as $key => $label ) {
$this->add_control( 'loop_' . $key, [ 'label' => $label, 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .ell-loop' => '--ell-' . $key . ': {{VALUE}};' ] ] );
}
$this->end_controls_section();
}
protected function render(): void {
$editing = Templates::is_editing();
if ( Templates::is_rendering_card() || Templates::current_template() ) {
if ( $editing && ! Templates::is_rendering_card() ) {
echo '<p class="ell-empty">' . esc_html__( 'Add the Event Loop widget to a page and use Loop Event Field to design a card.', 'eventlabs-loop-builder' ) . '</p>';
}
return;
}
$settings = $this->get_settings_for_display();
$template_key = (string) ( $settings['template_id'] ?? '' );
$style = Presets::catalog()[ $template_key ] ?? null;
$template = ctype_digit( $template_key ) ? Templates::published_layout( (int) $template_key ) : null;
$template_layout = $style['layout'] ?? ( $template ? get_post_meta( $template->ID, Templates::LAYOUT_META, true ) : '' );
$layout = $settings['layout'] ?? 'auto';
if ( ! isset( Presets::layouts()[ $layout ] ) ) {
$layout = isset( Presets::layouts()[ $template_layout ] ) ? $template_layout : 'grid';
}
$document = Elementor::instance()->documents->get_current();
$document_id = $document ? $document->get_main_id() : get_queried_object_id();
$instance = absint( $document_id ) . '-' . sanitize_key( $this->get_id() );
$id = 'eventlabs-loop-' . $instance;
$result = Query::results( $settings, $instance, $editing );
$cards = self::cards( $result['rows'] );
$group = 'list' === $layout && 'yes' === ( $settings['month_headers'] ?? '' );
$more = 'more' === Query::pagination( $settings );
$carousel = 'carousel' === $layout;
$style_class = 'yes' === ( $settings['equal_height'] ?? 'yes' ) ? '' : ' ell-natural-height';
$style_class .= $carousel && 'focus' === ( $settings['carousel_effect'] ?? '' ) ? ' ell-focus-effect' : '';
?>
<section class="ell-loop ell-layout-<?php echo esc_attr( $layout . $style_class ); ?>" id="<?php echo esc_attr( $id ); ?>" aria-label="<?php esc_attr_e( 'Events', 'eventlabs-loop-builder' ); ?>"<?php if ( $group ) { echo ' data-ell-grouped'; } ?>>
<?php echo $result['filters']; // Escaped by the query renderer. ?>
<?php if ( $editing && $template_key && ! $template && ! $style ) : ?>
<p class="ell-empty"><?php esc_html_e( 'The selected template is unavailable. Choose a published loop template.', 'eventlabs-loop-builder' ); ?></p>
<?php endif; ?>
<?php if ( ! $cards ) : ?>
<p class="ell-empty"><?php echo esc_html( $editing && $result['error'] ? $result['error'] : ( $settings['empty_message'] ?? __( 'No events match these filters.', 'eventlabs-loop-builder' ) ) ); ?></p>
<?php else : ?>
<?php if ( $carousel ) : ?>
<?php self::carousel_controls( $settings, $id, $editing ); ?>
<?php endif; ?>
<div class="ell-items" id="<?php echo esc_attr( $id . '-items' ); ?>"<?php if ( ! $group ) { echo ' role="list"'; } ?><?php if ( 'carousel' === $layout ) { echo ' tabindex="0"'; } ?>>
<?php $last_month = ''; ?>
<?php foreach ( $cards as $card ) : ?>
<?php if ( $group && ( $month = $card->month() ) && $last_month !== $month->format( 'Y-m' ) ) : ?>
<?php $last_month = $month->format( 'Y-m' ); ?>
<h2 class="ell-month" data-ell-month="<?php echo esc_attr( $last_month ); ?>"><?php echo esc_html( wp_date( 'F Y', $month->getTimestamp(), $month->getTimezone() ) ); ?></h2>
<?php endif; ?>
<div class="ell-item"<?php if ( ! $group ) { echo ' role="listitem"'; } ?>>
<?php
$html = $template ? Templates::render( $template, $card ) : ( $style ? Presets::render( $template_key, $card ) : '' );
echo trim( $html ) ? $html : $card->built_in(); // Elementor renders trusted, published template content.
?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ( $result['previous'] || $result['next'] ) : ?>
<nav class="ell-pagination" aria-label="<?php esc_attr_e( 'Event pages', 'eventlabs-loop-builder' ); ?>"<?php if ( $more ) { echo ' data-ell-more'; } ?>>
<?php if ( $result['previous'] ) : ?>
<a class="ell-nav-button" rel="prev" href="<?php echo esc_url( $result['previous'] ); ?>"><?php esc_html_e( 'Previous events', 'eventlabs-loop-builder' ); ?></a>
<?php endif; ?>
<?php
/* translators: %s: event page number */
$page_label = sprintf( __( 'Page %s', 'eventlabs-loop-builder' ), number_format_i18n( $result['page'] ) );
?>
<span class="ell-page" aria-current="page"><?php echo esc_html( $page_label ); ?></span>
<?php if ( $result['next'] ) : ?>
<a class="ell-nav-button" rel="next" href="<?php echo esc_url( $result['next'] ); ?>"><?php echo esc_html( $more ? ( ( $settings['more_label'] ?? '' ) ?: __( 'Load more events', 'eventlabs-loop-builder' ) ) : __( 'Next events', 'eventlabs-loop-builder' ) ); ?></a>
<?php endif; ?>
</nav>
<?php endif; ?>
<?php if ( $more ) : ?>
<p class="ell-more-status" tabindex="-1" role="status" data-ell-more-status data-loaded="<?php esc_attr_e( 'More events loaded.', 'eventlabs-loop-builder' ); ?>" data-done="<?php esc_attr_e( 'All events loaded.', 'eventlabs-loop-builder' ); ?>" data-error="<?php esc_attr_e( 'Unable to load more events. Select the link again to open the next page.', 'eventlabs-loop-builder' ); ?>"></p>
<?php endif; ?>
</section>
<?php
}
private static function carousel_controls( array $settings, string $id, bool $editing ): void {
$arrows = 'yes' === ( $settings['carousel_arrows'] ?? 'yes' );
$indicator = $settings['carousel_indicator'] ?? 'dots';
$autoplay = ! $editing && 'yes' === ( $settings['carousel_autoplay'] ?? '' ) ? max( 2, min( 30, absint( $settings['carousel_interval'] ?? 5 ) ) ) : 0;
$step = max( 1, min( 6, absint( $settings['carousel_step'] ?? 1 ) ) );
?>
<div class="ell-carousel-controls" data-ell-step="<?php echo (int) $step; ?>" data-ell-autoplay="<?php echo (int) $autoplay; ?>" data-ell-wrap="<?php echo 'yes' === ( $settings['carousel_loop'] ?? '' ) ? '1' : '0'; ?>" hidden>
<?php if ( $indicator ) : ?>
<div class="ell-carousel-indicator" data-ell-indicator="<?php echo esc_attr( $indicator ); ?>" data-slide-label="<?php esc_attr_e( 'Go to slide %d', 'eventlabs-loop-builder' ); ?>" aria-label="<?php esc_attr_e( 'Carousel position', 'eventlabs-loop-builder' ); ?>"></div>
<?php endif; ?>
<?php if ( $autoplay ) : ?>
<button class="ell-nav-button ell-carousel-toggle" type="button" data-ell-play data-pause="<?php esc_attr_e( 'Pause', 'eventlabs-loop-builder' ); ?>" data-play="<?php esc_attr_e( 'Play', 'eventlabs-loop-builder' ); ?>"><?php esc_html_e( 'Pause', 'eventlabs-loop-builder' ); ?></button>
<?php endif; ?>
<?php if ( $arrows ) : ?>
<button class="ell-nav-button" type="button" data-ell-direction="-1" aria-label="<?php esc_attr_e( 'Scroll to previous cards', 'eventlabs-loop-builder' ); ?>" aria-controls="<?php echo esc_attr( $id . '-items' ); ?>"><span aria-hidden="true">←</span></button>
<button class="ell-nav-button" type="button" data-ell-direction="1" aria-label="<?php esc_attr_e( 'Scroll to next cards', 'eventlabs-loop-builder' ); ?>" aria-controls="<?php echo esc_attr( $id . '-items' ); ?>"><span aria-hidden="true">→</span></button>
<?php endif; ?>
</div>
<?php
}
private static function cards( array $rows ): array {
if ( ! $rows ) {
return [];
}
$ids = array_unique( array_map( 'intval', array_column( $rows, 'event_id' ) ) );
$posts = get_posts( [
'post_type' => Events::EVENT, 'post_status' => 'publish', 'has_password' => false,
'post__in' => $ids, 'posts_per_page' => count( $ids ),
] );
$events = array_column( $posts, null, 'ID' );
$cards = [];
foreach ( $rows as $row ) {
$event = $events[ (int) $row['event_id'] ] ?? null;
if ( $event ) {
$cards[] = new Card( $event, $row );
}
}
return $cards;
}
}
final class Field_Widget extends Loop_Builder_Widget {
public function get_style_depends(): array {
$styles = parent::get_style_depends();
return 'map' === $this->get_settings( 'field' ) ? [ ...$styles, Map_View::STYLE ] : $styles;
}
public function get_script_depends(): array {
$scripts = parent::get_script_depends();
return 'map' === $this->get_settings( 'field' ) ? [ ...$scripts, Map_View::SCRIPT ] : $scripts;
}
public function get_name(): string {
return 'eventlabs-loop-field';
}
public function get_title(): string {
return __( 'Loop Event Field', 'eventlabs-loop-builder' );
}
public function get_icon(): string {
return 'eicon-dynamic-tags';
}
public function get_keywords(): array {
return [ 'eventlabs', 'event', 'loop', 'dynamic', 'field' ];
}
protected function register_controls(): void {
$this->start_controls_section( 'field_settings', [ 'label' => __( 'Event field', 'eventlabs-loop-builder' ) ] );
$this->add_control( 'field', [
'label' => __( 'Field', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => Card::fields(), 'default' => 'title',
] );
$this->add_control( 'heading_tag', [
'label' => __( 'Heading tag', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [ 'h2' => 'H2', 'h3' => 'H3', 'h4' => 'H4', 'h5' => 'H5', 'h6' => 'H6', 'p' => 'P' ],
'default' => 'h3', 'condition' => [ 'field' => 'title' ],
] );
$this->add_control( 'link_title', [
'label' => __( 'Link the title to the event', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER,
'default' => 'yes', 'condition' => [ 'field' => 'title' ],
] );
$this->add_control( 'image_size', [
'label' => __( 'Image size', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => [
'medium' => __( 'Medium', 'eventlabs-loop-builder' ), 'medium_large' => __( 'Medium large', 'eventlabs-loop-builder' ),
'large' => __( 'Large', 'eventlabs-loop-builder' ), 'full' => __( 'Full size', 'eventlabs-loop-builder' ),
],
'default' => 'large', 'condition' => [ 'field' => 'image' ],
] );
$this->add_control( 'show_placeholder', [
'label' => __( 'Show date tile when there is no image', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER,
'condition' => [ 'field' => 'image' ],
] );
$this->add_control( 'link_image', [
'label' => __( 'Link the image to the event', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'field' => 'image' ],
] );
$this->add_control( 'excerpt_words', [
'label' => __( 'Excerpt word limit', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => 100, 'default' => 27, 'condition' => [ 'field' => 'excerpt' ],
] );
$this->add_control( 'link_label', [
'label' => __( 'Link text', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::TEXT,
'condition' => [ 'field' => [ 'link', 'website' ] ],
] );
$this->add_control( 'custom_key', [
'label' => __( 'Custom field', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::SELECT,
'options' => Card::custom_fields(), 'condition' => [ 'field' => 'custom' ],
] );
$this->end_controls_section();
$this->start_controls_section( 'field_style', [ 'label' => __( 'Field style', 'eventlabs-loop-builder' ), 'tab' => Controls_Manager::TAB_STYLE ] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'typography',
'selector' => '{{WRAPPER}} .ell-field, {{WRAPPER}} .ell-field a',
] );
$this->add_control( 'text_color', [
'label' => __( 'Text color', 'eventlabs-loop-builder' ), 'type' => Controls_Manager::COLOR,
'selectors' => [ '{{WRAPPER}} .ell-field, {{WRAPPER}} .ell-field a' => 'color: {{VALUE}};' ],
] );
$this->end_controls_section();
}
protected function render(): void {
$card = Templates::active_card();
$settings = $this->get_settings_for_display();
$field = (string) ( $settings['field'] ?? 'title' );
$content = $card ? $card->field( $field, $settings ) : '';
if ( $content ) {
echo $content; // Card escaped the field value and attributes.
} elseif ( Templates::is_editing() ) {
$label = Card::fields()[ $field ] ?? __( 'Event field', 'eventlabs-loop-builder' );
echo '<p class="ell-field-placeholder">' . esc_html( $label ) . '</p>';
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
defined( 'ABSPATH' ) || exit;
$field = static function ( string $key, array $settings = [] ): array {
return [
'id' => Elementor\Utils::generate_random_string(), 'elType' => 'widget', 'widgetType' => 'eventlabs-loop-field',
'settings' => [ 'field' => $key ] + $settings, 'elements' => [],
];
};
$container = static function ( string $classes, array $children, bool $inner = true ): array {
return [
'id' => Elementor\Utils::generate_random_string(), 'elType' => 'container', 'isInner' => $inner,
'settings' => [ 'content_width' => 'full', 'css_classes' => $classes ], 'elements' => $children,
];
};
$body = $container( 'ell-starter-body', [
$field( 'categories' ),
$field( 'schedule' ),
$field( 'title' ),
$field( 'venue' ),
$field( 'excerpt' ),
$container( 'ell-starter-meta', [ $field( 'cost' ), $field( 'status' ) ] ),
$field( 'link' ),
] );
return [
'compact' === $preset
? $container( 'ell-starter ell-starter-compact', [ $body ], false )
: $container( 'ell-starter ell-starter-photo', [
$container( 'ell-starter-media', [ $field( 'image', [ 'show_placeholder' => 'yes' ] ) ] ),
$body,
], false ),
];