W3cubDocs

/WordPress

WP_Community_Events::trim_events( array $response_body )

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.

Parameters

$response_body

(array) (Required) The response body which contains the events.

Return

(array) The response body with events trimmed.

Source

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

protected function trim_events( $response_body ) {
		if ( isset( $response_body['events'] ) ) {
			$wordcamps = array();
			$today     = current_time( 'Y-m-d' );

			foreach ( $response_body['events'] as $key => $event ) {
				/*
				 * Skip WordCamps, because they might be multi-day events.
				 * Save a copy so they can be pinned later.
				 */
				if ( 'wordcamp' === $event['type'] ) {
					$wordcamps[] = $event;
					continue;
				}

				// We don't get accurate time with timezone from API, so we only take the date part (Y-m-d).
				$event_date = substr( $event['date'], 0, 10 );

				if ( $today > $event_date ) {
					unset( $response_body['events'][ $key ] );
				}
			}

			$response_body['events'] = array_slice( $response_body['events'], 0, 3 );
			$trimmed_event_types     = wp_list_pluck( $response_body['events'], 'type' );

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

		return $response_body;
	}

Changelog

Version Description
4.9.7 Stick a WordCamp to the final list.
4.8.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_community_events/trim_events