Tribe__Events__JSON_LD__Event::get_data( $posts = null, array $args = array() )

Fetches the JSON-LD data for this type of object


Parameters

$post

(int|WP_Post|null) (Required) The post/event

$args

(array) (Optional)

Default value: array()


Top ↑

Return

(array)


Top ↑

Source

File: src/Tribe/JSON_LD/Event.php

	public function get_data( $posts = null, $args = array() ) {
		// Fetch the global post object if no posts are provided
		if ( ! is_array( $posts ) && empty( $posts ) ) {
			$posts = array( $GLOBALS['post'] );
		}
		// If we only received a single post object, wrap it in an array
		else {
			$posts = ( $posts instanceof WP_Post ) ? array( $posts ) : (array) $posts;
		}

		$return = array();

		foreach ( $posts as $i => $post ) {
			// We may have been passed a post ID - let's ensure we have the post object
			if ( is_numeric( $post ) ) {
				$post = get_post( $post );
			}

			// If we don't have a valid post object, skip to the next item
			if ( ! $post instanceof WP_Post ) {
				continue;
			}

			$data = parent::get_data( $post, $args );

			// If we have an Empty data we just skip
			if ( empty( $data ) ) {
				continue;
			}

			// Fetch first key
			$post_id = key( $data );

			// Fetch first Value
			$data = reset( $data );

			$event_tz_string = get_post_meta( $post_id, '_EventTimezone', true );
			$tz_mode         = tribe_get_option( 'tribe_events_timezone_mode', 'event' );
			$tz_string       = $event_tz_string && $tz_mode === 'event' ? $event_tz_string : Tribe__Events__Timezones::wp_timezone_string();

			$data->startDate = Tribe__Events__Timezones::to_utc( tribe_get_start_date( $post_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT ), $tz_string, 'c' );
			$data->endDate   = Tribe__Events__Timezones::to_utc( tribe_get_end_date( $post_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT ), $tz_string, 'c' );

			// @todo once #90984 is resolved this extra step should not be required
			if ( ! empty( $tz_string ) ) {
				$data->startDate = $this->get_localized_iso8601_string( $data->startDate, $tz_string );
				$data->endDate   = $this->get_localized_iso8601_string( $data->endDate, $tz_string );
			}

			if ( tribe_has_venue( $post_id ) ) {
				$venue_id       = tribe_get_venue_id( $post_id );
				$venue_data     = Tribe__Events__JSON_LD__Venue::instance()->get_data( $venue_id );
				$data->location = reset( $venue_data );
			}

			if ( tribe_has_organizer( $post_id ) ) {
				$organizer_id    = tribe_get_organizer_id( $post_id );
				$organizer_data  = Tribe__Events__JSON_LD__Organizer::instance()->get_data( $organizer_id );
				$data->organizer = reset( $organizer_data );
			}

			$price = tribe_get_cost( $post_id );
			$price = $this->normalize_price( $price );
			if ( '' !== $price ) {

				// The currency has to be in ISO 4217
				$event_currency = get_post_meta( $post_id, '_EventCurrencySymbol', true );
				$currency       = ( '' !== $event_currency ) ? $event_currency : 'USD';

				// Manually Include the Price for non Event Tickets
				$data->offers = (object) [
					'@type'         => 'Offer',
					'price'         => $price,
					'priceCurrency' => $currency,
					// Use the same url as the event
					'url'           => $data->url,
					'category'      => 'primary',
					'availability'  => 'inStock',
					'validFrom'     => date( DateTime::ATOM, strtotime( get_the_date( '', $post_id ) ) ),
				];
			}

			// Setting a default parameter here to avoid Google console errors
			$data->performer = 'Organization';

			$data = $this->apply_object_data_filter( $data, $args, $post );
			$return[ $post_id ] = $data;
		}

		return $return;
	}