Abstract_Meetings::ajax_create( string|null $nonce = null )

Handles the request to generate a Zoom meeting.


Parameters

$nonce

(string|null) (Optional) The nonce that should accompany the request.

Default value: null


Top ↑

Return

(bool) Whether the request was handled or not.


Top ↑

Source

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

	public function ajax_create( $nonce = null ) {
		if ( ! $this->check_ajax_nonce( static::$create_action, $nonce ) ) {
			return false;
		}

		$event = $this->check_ajax_post();

		if ( ! $event ) {
			return false;
		}

		$zoom_host_id = tribe_get_request_var( 'zoom_host_id' );
		// If no host id found, fail the request as account level apps do not support 'me'
		if ( empty( $zoom_host_id ) ) {

			wp_die();

			return false;
		}

		// Load the account.
		$zoom_account_id = tribe_get_request_var( 'zoom_account_id' );
		// if no id, fail the request.
		if ( empty( $zoom_account_id ) ) {
			$error_message = _x( 'The Zoom Account ID is missing to access the API.', 'Account ID is missing error message.', 'events-virtual' );
			$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );

			wp_die();

			return false;
		}

		$this->api->load_account_by_id( $zoom_account_id );
		// If there is no token, then stop as the connection will fail.
		if ( ! $this->api->get_token_authorization_header() ) {
			$error_message = _x( 'The Zoom Account could not be loaded to access to API.', 'Zoom account loading error message.', 'events-virtual' );
			$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );

			wp_die();

			return false;
		}

		$post_id = $event->ID;
		$cached  = $this->encryption->decrypt( get_post_meta( $post_id, Virtual_Events_Meta::$prefix . 'zoom_meeting_data', true ), true );

		/**
		 * Filters whether to force the recreation of the Zoom meetings link on each request or not.
		 *
		 * If the filters returns a truthy value, then each request, even for events that already had a Zoom meeting
		 * generated, will generate a new link, without re-using the previous one.
		 *
		 * @since 1.0.0
		 *
		 * @param bool $force   Whether to force the regeneration of Zoom Meeting links or not.
		 * @param int  $post_id The post ID of the event the Meeting is being generated for.
		 */
		$force = apply_filters(
			"tribe_events_virtual_meetings_zoom_{$this::$meeting_type}_force_recreate",
			true,
			$post_id
		);

		if ( ! $force && ! empty( $cached ) ) {
			$this->classic_editor->render_meeting_link_generator( $event, true, false, $zoom_account_id  );

			wp_die();

			return true;
		}

		// Get the password requirements for Meetings.
		$password_requirements = $this->password->get_password_requirements();

		/**
		 * Filters the password for the Zoom Meeting.
		 *
		 * @since 1.0.2
		 *
		 * @param null|string|int   The password for the Zoom Meeting.
		 * @param array    $password_requirements An array of password requirements from Zoom.
		 * @param \WP_Post $event                 The event post object, as decorated by the `tribe_get_event` function.
		 */
		$password = apply_filters(
			"tribe_events_virtual_meetings_zoom_{$this::$meeting_type}_password",
			null,
			$password_requirements,
			$event
		);

		/**
		 * If this is a new post, then the duration will not be available.
		 * Since meetings that have a duration of 0 will not be editable after their creation,
		 * let's ensure a default 60 minutes duration to come back and edit the meeting later.
		 */
		$duration = (int) ceil( (int) $event->duration / 60 );
		$duration = $duration ? (int) $duration : 60;

		$body = [
			'topic'      => $event->post_title,
			'type'       => self::TYPE_MEETING_SCHEDULED,
			'start_time' => $event->dates->start->format( 'Y-m-d\TH:i:s' ),
			'timezone'   => $event->timezone,
			'duration'   => $duration,
			'password'   => $password,
		];

		/**
		 * Filters the contents of the request that will be made to the Zoom API to generate a meeting link.
		 *
		 * @since 1.0.0
		 *
		 * @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}_request_body",
			$body,
			$event,
			$this
		);

		$success = false;

		$this->api->post(
			Api::$api_base . "users/{$zoom_host_id}/{$this::$api_endpoint}",
			[
				'headers' => [
					'authorization' => $this->api->get_token_authorization_header(),
					'content-type'  => 'application/json; charset=utf-8',
				],
				'body'    => wp_json_encode( $body ),
			],
			Api::POST_RESPONSE_CODE
		)->then(
			function ( array $response ) use ( $post_id, &$success, &$zoom_account_id ) {
				$this->process_meeting_creation_response( $response, $post_id );

				$event = tribe_get_event( $post_id, OBJECT, 'raw', true );
				$this->classic_editor->render_meeting_link_generator( $event, true, false, $zoom_account_id  );

				$success = true;

				wp_die();
			}
		)->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'];
				}

				$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );

				wp_die();
			}
		);

		return $success;
	}

Top ↑

Changelog

Changelog
Version Description
1.1.1 Moved to the TribeEventsVirtualMeetingsZoomAbstract_Meetings class.
1.0.0 Introduced.