Tribe__Tickets_Plus__Tickets_View::save_meta( int $event_id )

Saves the Attendee Information from the front-end My Tickets editing form.


Parameters

$event_id

(int) (Required) The event this change applies to.


Top ↑

Source

File: src/Tribe/Tickets_View.php

	public function save_meta( $event_id ) {
		$user_id = get_current_user_id();

		// this block only runs for Tickets
		if ( isset( $_POST['attendee'] ) && ! empty( $_POST['event_id'] ) ) {
			$event_id = absint( $_POST['event_id'] );

			$attendees_by_order = $this->get_event_attendees_by_order( $event_id, $user_id );

			foreach ( $_POST['attendee'] as $order_id => $order_data ) {
				if ( ! isset( $attendees_by_order[ $order_id ] ) ) {
					continue;
				}

				$first_attendee = reset( $attendees_by_order[ $order_id ] );

				if ( ! isset( $first_attendee['provider'] ) ) {
					continue;
				}

				$optout = empty( $_POST['optout'][ $order_id ] ) ? false : true;

				$provider = call_user_func( array( $first_attendee['provider'], 'get_instance' ) );

				foreach ( $attendees_by_order[ $order_id ] as $attendee ) {
					if ( $user_id !== (int) $attendee['user_id'] ) {
						continue;
					}

					$provider_class = $attendee['provider'];

					if ( ! defined( "{$provider_class}::ATTENDEE_OPTOUT_KEY" ) ) {
						$attendee_optout_key = call_user_func( array( $provider_class, 'get_key' ), 'ATTENDEE_OPTOUT_KEY' );
					} else {
						$attendee_optout_key = constant( "{$provider_class}::ATTENDEE_OPTOUT_KEY" );
					}

					update_post_meta( $attendee['attendee_id'], $attendee_optout_key, $optout );
				}
			}
		}

		// If we don't have the Meta we skip the rest
		if ( empty( $_POST['tribe-tickets-meta'] ) ) {
			return;
		}

		$attendees_data = $_POST['tribe-tickets-meta'];

		foreach ( $attendees_data as $attendee_id => $data ) {
			$attendee_owner = $this->get_attendee_owner( $attendee_id );

			// Only saves if this user is the owner
			if ( $user_id != $attendee_owner ) {
				continue;
			}

			/**
			 * Allow developers to prevent users to update specific Attendees or Events
			 * @param boolean $is_meta_update_allowed If is allowed or not
			 * @param int     $event_id               Which event this applies to
			 * @param int     $attendee_id            Which attendee this update will be done to
			 * @param array   $data                   Data that will be saved
			 */
			$is_meta_restricted = apply_filters( 'event_tickets_plus_is_meta_restricted', false, $event_id, $attendee_id, $data );

			// Just skip if this is not allowed
			if ( $is_meta_restricted ) {
				continue;
			}

			$args = [
				'by' => [
					'id' => $attendee_id,
				],
			];

			$attendee_data = Tribe__Tickets__Tickets::get_event_attendees_by_args( $event_id, $args );

			// Attendee not found.
			if ( ! $attendee_data['attendees'] ) {
				continue;
			}

			$attendee = current( $attendee_data['attendees'] );

			$fields = Tribe__Tickets_Plus__Meta::instance()->get_meta_fields_by_ticket( $attendee['product_id'] );

			foreach ( $fields as $field ) {
				// Don't remove the data if not restricted
				if ( ! $field->is_restricted( $attendee_id ) ) {
					continue;
				}

				if ( 'checkbox' === $field->type ) {
					foreach ( $field->extra['options'] as $label ) {
						$name = $field->slug . '_' . sanitize_title( $label );

						if ( isset( $data[ $name ] ) ) {
							unset( $data[ $name ] );
						}
					}
				} elseif ( isset( $data[ $field->slug ] ) ) {
					unset( $data[ $field->slug ] );
				}
			}

			// Updates the meta information associated with individual attendees
			update_post_meta( $attendee_id, Tribe__Tickets_Plus__Meta::META_KEY, $data );
		}
	}

Top ↑

Changelog

Changelog
Version Description
5.10.2 Refactored logic for generating the key for checkboxes and radio buttons.
4.10.7 Introduced.