339 lines
18 KiB
PHP
339 lines
18 KiB
PHP
<?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>';
|
|
}
|
|
}
|
|
}
|