Abstract_Meetings::ajax_create( string|null $nonce = null )
Handles the request to generate a Webex meeting.
Contents
Parameters
- $nonce
-
(string|null) (Optional) The nonce that should accompany the request.
Default value: null
Return
(bool) Whether the request was handled or not.
Source
File: src/Tribe/Meetings/Webex/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;
}
$host_email = tribe_get_request_var( 'host_id' );
// If no host id found, fail the request as account level apps do not support 'me'
if ( empty( $host_email ) ) {
$error_message = _x( 'The Webex Host Email to access the API is missing.', 'Webex Host Email is missing error message.', 'events-virtual' );
$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
wp_die();
}
// Load the account.
$account_id = tribe_get_request_var( 'account_id' );
// if no id, fail the request.
if ( empty( $account_id ) ) {
$error_message = _x( 'The Webex Account ID to access the API is missing.', 'Account ID is missing error message.', 'events-virtual' );
$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
wp_die();
}
$this->api->load_account_by_id( $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 Webex Account to access to API could not be loaded.', 'Webex account loading error message.', 'events-virtual' );
$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
wp_die();
}
$all_day = tribe_get_request_var( 'allDayCheckbox', $event->all_day );
if ( $all_day ) {
$error_message = _x( 'The Webex API does not support all day events, please set a start date and end date with time for both.', 'Webex all day error message.', 'events-virtual' );
$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
wp_die();
}
$post_id = $event->ID;
$cached = get_post_meta( $post_id, Virtual_Events_Meta::$prefix . 'webex_meeting_data', true );
/**
* Filters whether to force the recreation of the Webex meetings link on each request or not.
*
* If the filters returns a truthy value, then each request, even for events that already had a Webex meeting
* generated, will generate a new link, without re-using the previous one.
*
* @since 1.9.0
*
* @param bool $force Whether to force the regeneration of Webex Meeting links or not.
* @param int $post_id The post ID of the event the Meeting is being generated for.
*/
$force = apply_filters(
"tec_events_virtual_meetings_webex_{$this::$meeting_type}_force_recreate",
true,
$post_id
);
if ( ! $force && ! empty( $cached ) ) {
$this->classic_editor->render_meeting_link_generator( $event, true, false, $account_id );
wp_die();
}
// Get the event times from the ajax script or fallback to the event object.
$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 );
$start_datetime = $this->format_date_for_webex( $start_date, $start_time, $time_zone );
$end_datetime = $this->format_date_for_webex( $end_date, $end_time, $time_zone );
if ( $start_datetime === $end_datetime ) {
$error_message = _x( 'To create an event please set the event end time at least 10 minutes from the start time.', 'Webex no duration error message.', 'events-virtual' );
$this->classic_editor->render_meeting_generation_error_details( $event, $error_message, true );
wp_die();
}
/**
* Password is not included as a random password conforming to the site's password rules will be generated automatically.
* @see: https://developer.webex.com/docs/api/v1/meetings/create-a-meeting
*/
$body = [
'title' => $event->post_title,
'start' => $start_datetime,
'end' => $end_datetime,
'timezone' => $time_zone,
'hostEmail' => $host_email,
];
/**
* Filters the contents of the request that will be made to the Webex API to generate a meeting link.
*
* @since 1.9.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(
"tec_events_virtual_meetings_webex_{$this::$meeting_type}_request_body",
$body,
$event,
$this
);
$success = false;
$this->api->post(
Api::$api_base . 'meetings',
[
'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, &$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, $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;
}
Changelog
| Version | Description |
|---|---|
| 1.9.0 | Introduced. |