tribe_get_event_capacity( int|WP_Post $post )

Returns the capacity for a given Post/Event.


Parameters #

$post

(int|WP_Post) (Required) Post (event) we are trying to fetch capacity for.


Top ↑

Return #

(int|null)


Top ↑

Source #

File: src/template-tags/tickets.php

	function tribe_get_event_capacity( $post ) {
		// When not dealing with a Instance of Post try to set it up.
		if ( ! $post instanceof WP_Post ) {
			$post = get_post( $post );
		}

		// Bail when it's not a post or ID is 0.
		if ( ! $post instanceof WP_Post || 0 === $post->ID ) {
			return null;
		}

		$post_id = $post->ID;

		// This is really "post types that allow tickets".
		$event_types = Tribe__Tickets__Main::instance()->post_types();

		// Bail when it's not an allowed post type.
		if ( ! in_array( $post->post_type, $event_types, true ) ) {
			return null;
		}

		$rsvp         = Tribe__Tickets__RSVP::get_instance();
		$rsvp_tickets = $rsvp->get_tickets_ids( $post_id );
		$rsvp_cap     = 0;

		foreach ( $rsvp_tickets as $rsvp_ticket ) {
			$cap = tribe_tickets_get_capacity( $rsvp_ticket );

			if ( -1 === $cap || '' === $cap ) {
				$rsvp_cap = -1;
				break;
			}

			$rsvp_cap += $cap;
		}

		$provider_id = Tribe__Tickets__Tickets::get_event_ticket_provider( $post_id );

		// Protect against ticket that exists but is of a type that is not enabled.
		if ( ! method_exists( $provider_id, 'get_instance' ) ) {
			return null;
		}

		$provider = call_user_func( [ $provider_id, 'get_instance' ] );
		$tickets  = $provider->get_tickets_ids( $post_id );

		// We only have RSVPs.
		if ( empty( $tickets ) ) {
			return (int) $rsvp_cap;
		}

		$global_stock       = new \Tribe__Tickets__Global_Stock( $post_id );
		/** @var Tribe__Tickets__Tickets_Handler $tickets_handler */
		$tickets_handler = tribe( 'tickets.handler' );
		$tickets_cap        = 0;
		$added_global_stock = false;

		foreach ( $tickets as $ticket ) {
			// Handle global stock.
			$mode = get_post_meta( $ticket, $global_stock::TICKET_STOCK_MODE, true );
			if (
				$global_stock::GLOBAL_STOCK_MODE !== $mode
				&& $global_stock::CAPPED_STOCK_MODE !== $mode
			) {
				$cap = tribe_tickets_get_capacity( $ticket );

				if ( -1 === $cap ) {
					$tickets_cap = -1;
					break;
				}

				$tickets_cap += $cap;
			} elseif ( ! $added_global_stock ) {
				$global_cap = (int) get_post_meta( $post_id, $tickets_handler->key_capacity, true );
				$tickets_cap += $global_cap;
				$added_global_stock = true;
			}
		}

		// If either is unlimited, it's all unlimited.
		if ( -1 === $tickets_cap || -1 === $rsvp_cap) {
			return -1;
		}

		$value = (int) $rsvp_cap + (int) $tickets_cap;

		return (int) $value;
	}

Top ↑

Changelog #

Changelog
Version Description
4.11.3 Introduced.