Home / Admin / Show a Notice When an Event Has Ended
Duplicate Snippet

Embed Snippet on Your Site

Show a Notice When an Event Has Ended

A finished event page looks the same as a live one. Someone arriving from a search result or an old link sees the date and time with nothing telling them the event is over. This adds a short notice above the event details once it has finished.
Recurring series use the end of the whole series, not the end of the first occurrence, so a weekly series that is still running is never marked as ended.

Code Preview
php
<?php
/**
 * Show a notice on the single event page once the event is over.
 *
 * Recurring events use the end of the SERIES, not the end of the first
 * occurrence. The end column describes only the first occurrence, so a weekly
 * series running since 2019 would otherwise report as ended on day one.
 *
 * A recurring series with no end date never ends, so it never shows the notice.
 *
 * Renders as a core badge on versions that have the badge seam, and as a
 * self-styled banner on older versions where that seam and its CSS do not exist.
 */
/**
 * The notice text.
 *
 * @return string
 */
if ( ! function_exists( 'sc_snippets_event_ended_text' ) ) :
function sc_snippets_event_ended_text() {
	return __( 'Event has ended', 'sugar-snippets' );
}
endif;
/**
 * Whether this event is over.
 *
 * Compares in UTC, because Sugar Calendar stores and compares the start and end
 * columns in UTC and sugar_calendar_get_request_time() does the same for core's
 * own past/upcoming queries.
 *
 * @param \Sugar_Calendar\Event $event The event.
 *
 * @return bool
 */
if ( ! function_exists( 'sc_snippets_event_has_ended' ) ) :
function sc_snippets_event_has_ended( $event ) {
	if ( ! is_object( $event ) || empty( $event->id ) ) {
		return false;
	}
	$is_recurring = ! empty( $event->recurrence );
	if ( $is_recurring ) {
		// No recurrence end means the series runs forever.
		if ( empty( $event->recurrence_end ) || $event->is_empty_date( $event->recurrence_end ) ) {
			return false;
		}
		$end = $event->recurrence_end_date( 'Y-m-d H:i:s' );
	} else {
		$end = $event->end_date( 'Y-m-d H:i:s' );
	}
	if ( empty( $end ) || $event->is_empty_date( $end ) ) {
		return false;
	}
	return strtotime( $end . ' UTC' ) < sugar_calendar_get_request_time( 'timestamp', 'UTC' );
}
endif;
/**
 * Whether core offers the badge seam.
 *
 * @return bool
 */
if ( ! function_exists( 'sc_snippets_event_ended_has_badges' ) ) :
function sc_snippets_event_ended_has_badges() {
	return method_exists( '\Sugar_Calendar\Frontend\Loader', 'render_event_badges' );
}
endif;
/**
 * Badge path.
 *
 * @param array                 $badges Existing badges.
 * @param \Sugar_Calendar\Event $event  The event.
 *
 * @return array
 */
if ( ! function_exists( 'sc_snippets_event_ended_badge' ) ) :
function sc_snippets_event_ended_badge( $badges, $event ) {
	if ( sc_snippets_event_has_ended( $event ) ) {
		$badges[] = [
			'text'  => sc_snippets_event_ended_text(),
			'color' => 'grey',
		];
	}
	return $badges;
}
endif;
add_filter( 'sugar_calendar_frontend_event_badges', 'sc_snippets_event_ended_badge', 10, 2 );
/**
 * Banner path, for versions without the badge seam.
 *
 * @param \Sugar_Calendar\Event $event The event.
 */
if ( ! function_exists( 'sc_snippets_event_ended_banner' ) ) :
function sc_snippets_event_ended_banner( $event ) {
	if ( sc_snippets_event_ended_has_badges() || ! sc_snippets_event_has_ended( $event ) ) {
		return;
	}
	printf(
		'<div class="sc-snippets-event-ended" style="background:#f0f0f1;border-radius:3px;color:#8c8f94;display:inline-flex;align-items:center;height:24px;padding:0 8px;font-size:12px;font-weight:500;line-height:1;margin-bottom:10px;">%s</div>',
		esc_html( sc_snippets_event_ended_text() )
	);
}
endif;
add_action( 'sugar_calendar_frontend_event_details_before', 'sc_snippets_event_ended_banner', 10 );

Comments

Add a Comment