Separators::should_have_day( array $events, WP_Post|int $event, string|DateTimeInterface|null $request_date = null )

Determines if a given event from a list of events should have a day separator for some List view template structures (such as month view mobile/widget).

Note that events will NOT be sorted by date for this check: this is by design. There are other criteria by which events might be sorted this method should not interfere with. The method will perform the check using the "display" date of the events since this is a front-end facing method.


Parameters

$events

(array) (Required) WP_Post or numeric ID for events.

$event

(WP_Post|int) (Required) Event we want to check.

$request_date

(string|DateTimeInterface|null) (Optional) A request date that should be used as context for the evaluation.

Default value: null


Top ↑

Return

(boolean) Whether the event, in the context of this event set and request date, should show the separator or not.


Top ↑

Source

File: src/Tribe/Views/V2/Utils/Separators.php

	public static function should_have_day( $events, $event, $request_date = null ) {
		if ( ! is_array( $events ) ) {
			return false;
		}

		$events = array_filter( array_map( 'tribe_get_event', $events ), static function ( $event ) {
			return $event instanceof \WP_Post;
		} );

		$event = tribe_get_event( $event );

		if ( empty( $events ) || ! $event instanceof \WP_Post ) {
			return false;
		}

		if ( $event->ID === reset( $events )->ID ) {
			// The first event in a set should always trigger the separator display.
			return true;
		}

		if ( null !== $request_date ) {
			$request_date = Dates::build_date_object( $request_date );
		}

		// Reduce events to only keep the starting ones.
		$start_events_ids = array_unique(
			array_combine(
				wp_list_pluck( $events, 'ID' ),
				array_map( static function ( \WP_Post $event ) use ( $request_date ) {
					/*
					 * If we have a request date we "move the event forward" to it.
					 * If the event is in this set, then we assume it fits.
					 * This is usually the case w/ multi-day events that start "in the past" in relation to a request
					 * date; in that case we display them not on their original day, but on the request date.
					 */
					$the_date = null !== $request_date
						? max( $event->dates->start_display, $request_date )
						: $event->dates->start_display;

					return $the_date->format( 'd' );
				}, $events )
			)
		);

		return $event->ID === array_search( $event->dates->start_display->format( 'd' ), $start_events_ids, true );
	}

Top ↑

Changelog

Changelog
Version Description
4.6.0 Introduced.