WP_Community_Events::trim_events() WordPress Method

The WP_Community_Events::trim_events() method is used to trim events that have already been fetched from the WordPress database. This is useful for ensuring that events are not duplicated when they are displayed on a website.

WP_Community_Events::trim_events( array $events ) #

Prepares the event list for presentation.


Description

Discards expired events, and makes WordCamps "sticky." Attendees need more advanced notice about WordCamps than they do for meetups, so camps should appear in the list sooner. If a WordCamp is coming up, the API will "stick" it in the response, even if it wouldn’t otherwise appear. When that happens, the event will be at the end of the list, and will need to be moved into a higher position, so that it doesn’t get trimmed off.


Top ↑

Parameters

$events

(array)(Required)The events that will be prepared.


Top ↑

Return

(array) The response body with events trimmed.


Top ↑

Source

File: wp-admin/includes/class-wp-community-events.php

	protected function trim_events( array $events ) {
		$future_events = array();

		foreach ( $events as $event ) {
			/*
			 * The API's `date` and `end_date` fields are in the _event's_ local timezone, but UTC is needed so
			 * it can be converted to the _user's_ local time.
			 */
			$end_time = (int) $event['end_unix_timestamp'];

			if ( time() < $end_time ) {
				// Decode HTML entities from the event title.
				$event['title'] = html_entity_decode( $event['title'], ENT_QUOTES, 'UTF-8' );

				array_push( $future_events, $event );
			}
		}

		$future_wordcamps = array_filter(
			$future_events,
			static function( $wordcamp ) {
				return 'wordcamp' === $wordcamp['type'];
			}
		);

		$future_wordcamps    = array_values( $future_wordcamps ); // Remove gaps in indices.
		$trimmed_events      = array_slice( $future_events, 0, 3 );
		$trimmed_event_types = wp_list_pluck( $trimmed_events, 'type' );

		// Make sure the soonest upcoming WordCamp is pinned in the list.
		if ( $future_wordcamps && ! in_array( 'wordcamp', $trimmed_event_types, true ) ) {
			array_pop( $trimmed_events );
			array_push( $trimmed_events, $future_wordcamps[0] );
		}

		return $trimmed_events;
	}


Top ↑

Changelog

Changelog
VersionDescription
6.0.0Decode HTML entities from the event title.
5.5.2Accepts and returns only the events, rather than an entire HTTP response.
4.9.7Stick a WordCamp to the final list.
4.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.