Abstract_Meetings::update( WP_Post|int $event )

Handles update of Zoom meeting when Event details change.


Parameters

$event

(WP_Post|int) (Required) The event (or event ID) we're updating the meeting for.


Top ↑

Source

File: src/Tribe/Meetings/Zoom/Abstract_Meetings.php

	public function update( $event ) {
		// Get event if not an object.
		if ( ! ( $event instanceof \WP_Post ) ) {
			$event = tribe_get_event( $event );
		}

		// There is no meeting to update.
		if ( ! ( $event instanceof \WP_Post ) || empty( $event->zoom_meeting_id ) ) {
			return;
		}

		$start_date = tribe_get_request_var( 'EventStartDate', $event->start_date );
		$start_time = tribe_get_request_var( 'EventStartTime', $event->start_time );
		$time_zone  = tribe_get_request_var( 'EventTimezone', $event->timezone );
		$end_date   = tribe_get_request_var( 'EventEndDate', $event->end_date );
		$end_time   = tribe_get_request_var( 'EventEndTime', $event->end_time );

		// Get the duration of the event from the field values instead of the event object, which has previously saved values.
		$duration = $this->calculate_duration( $start_date, $start_time, $end_date, $end_time, $time_zone );
		if ( empty( $duration ) ) {
			$duration = $event->duration;
		}

		$zoom_date         = $this->format_date_for_zoom( $start_date, $start_time, $time_zone );
		$alternative_hosts = (array) tribe_get_request_var( 'tribe-events-virtual-zoom-alt-host' );

		// Note the time format - because Zoom stores all dates as UTC with the trailing 'Z'.
		$event_body = [
			'topic'             => $event->post_title,
			'start_time'        => $zoom_date,
			'timezone'          => $time_zone,
			'duration'          => (int) ceil( (int) $duration / 60 ),
			'alternative_hosts' => esc_attr( implode( ";", $alternative_hosts ) ),
		];

		$meeting_data = $this->encryption->decrypt( get_post_meta( $event->ID, Virtual_Events_Meta::$prefix . 'zoom_meeting_data', true ), true );
		$meeting_body = [
			'topic'             => $meeting_data['topic'],
			'start_time'        => $meeting_data['start_time'],
			'timezone'          => $meeting_data['timezone'],
			'duration'          => $meeting_data['duration'],
			'alternative_hosts' => $meeting_data['settings']['alternative_hosts'],
		];

		$diff = array_diff_assoc( $event_body, $meeting_body );

		// Nothing to update.
		if ( empty( $diff ) ) {
			return;
		}

		$post_id = $event->ID;

		// Unset the alternative hosts and set in expected location for the API.
		unset( $event_body['alternative_hosts'] );
		$event_body['settings']['alternative_hosts'] = esc_attr( implode( ";", $alternative_hosts ) );

		/**
		 * Filters the contents of the request that will be made to the Zoom API to update a meeting link.
		 *
		 * @since 1.0.2
		 *
		 * @param array<string,mixed> The current content of the request body.
		 * @param \WP_Post $event The event post object, as decorated by the `tribe_get_event` function.
		 * @param Meetings $this  The current API handler object instance.
		 */
		$body = apply_filters(
			"tribe_events_virtual_meetings_zoom_{$this::$meeting_type}_update_request_body",
			$event_body,
			$event,
			$this
		);

		// Load the account.
		$zoom_account_id = $this->api->get_account_id_in_admin( $post_id );
		if ( empty( $zoom_account_id ) ) {
			return;
		}

		$this->api->load_account_by_id( $zoom_account_id );
		if ( ! $this->api->get_token_authorization_header() ) {
			return;
		}

		// Update.
		$this->api->patch(
			Api::$api_base . "{$this::$api_endpoint}/{$event->zoom_meeting_id}",
			[
				'headers' => [
					'Authorization' => $this->api->get_token_authorization_header(),
					'Content-Type'  => 'application/json; charset=utf-8',
				],
				'body'    => wp_json_encode( $body ),
			],
			Api::PATCH_RESPONSE_CODE
		)->then(
			function ( array $response ) use ( $post_id, $event ) {
				$this->process_meeting_update_response( $response, $event, $post_id );
			}
		)->or_catch(
			function ( \WP_Error $error ) use ( $event ) {
				do_action(
					'tribe_log',
					'error',
					__CLASS__,
					[
						'action'  => __METHOD__,
						'code'    => $error->get_error_code(),
						'message' => $error->get_error_message(),
					]
				);

				$error_data    = wp_json_encode( $error->get_error_data() );
				$decoded       = json_decode( $error_data, true );
				$error_message = null;
				if ( false !== $decoded && is_array( $decoded ) && isset( $decoded['message'] ) ) {
					$error_message = $decoded['message'];
				}

				// Do something to indicate failure with $error_message?
				$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
			}
		);
	}

Top ↑

Changelog

Changelog
Version Description
1.2.0 Utilize the datepicker format when parse the Event Date to prevent the wrong date in Zoom.
1.0.2 Introduced.