__( 'Airmeet', 'advanced-form-integration' ), 'tasks' => array( 'register_attendee' => __( 'Register Attendee', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_airmeet_settings_tab', 10, 1 ); function adfoin_airmeet_settings_tab( $providers ) { $providers['airmeet'] = __( 'Airmeet', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_airmeet_settings_view', 10, 1 ); function adfoin_airmeet_settings_view( $current_tab ) { if ( 'airmeet' !== $current_tab ) { return; } $title = __( 'Airmeet', 'advanced-form-integration' ); $key = 'airmeet'; if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once ADVANCED_FORM_INTEGRATION_INCLUDES . '/class-adfoin-account-manager.php'; } $fields = array( array( 'name' => 'apiKey', 'label' => __( 'Airmeet Access Key', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => false, 'show_in_table' => true ), array( 'name' => 'apiSecret', 'label' => __( 'Airmeet Secret Key', 'advanced-form-integration' ), 'type' => 'password', 'required' => true, 'mask' => true, 'show_in_table' => true ), array( 'name' => 'region', 'label' => __( 'Region', 'advanced-form-integration' ), 'type' => 'select', 'required' => false, 'show_in_table' => true, 'options' => adfoin_airmeet_regions(), 'description' => __( 'Where your Airmeet community is hosted. EU and US communities must pick their region.', 'advanced-form-integration' ), ), ); $instructions = sprintf( '
  1. %1$s
  2. %2$s
  3. %3$s

%4$s

', esc_html__( 'Navigate to https://airmeet.com and go to the CM dashboard.', 'advanced-form-integration' ), esc_html__( 'Click on the Integrations tab and then click on API access keys.', 'advanced-form-integration' ), esc_html__( 'Copy the Access Key and Secret Key, paste them here, then click "Save & Authenticate".', 'advanced-form-integration' ), esc_html__( 'Use the saved credential when configuring an Airmeet action. Provide the Airmeet ID from the event URL or dashboard when mapping a form.', 'advanced-form-integration' ) ); ADFOIN_Account_Manager::render_settings_view( $key, $title, $fields, $instructions ); } /* * Airmeet regions => label. The default (empty) is the original global gateway. */ function adfoin_airmeet_regions() { return array( '' => __( 'Default (Global / India)', 'advanced-form-integration' ), 'eu' => __( 'Europe', 'advanced-form-integration' ), 'us' => __( 'United States', 'advanced-form-integration' ), ); } function adfoin_airmeet_base_url( $credentials ) { $region = isset( $credentials['region'] ) ? $credentials['region'] : ''; if ( 'eu' === $region || 'us' === $region ) { return 'https://api-gateway-prod.' . $region . '.airmeet.com/prod/'; } return 'https://api-gateway.airmeet.com/prod/'; } /* * Airmeet's error text from a response body, e.g. "API access is not included in your plan". */ function adfoin_airmeet_error_text( $body, $fallback ) { $data = json_decode( (string) $body, true ); if ( ! empty( $data['errors'] ) && is_array( $data['errors'] ) ) { $parts = array(); array_walk_recursive( $data['errors'], function ( $value ) use ( &$parts ) { if ( is_scalar( $value ) ) { $parts[] = (string) $value; } } ); if ( $parts ) { return implode( ' ', $parts ); } } foreach ( array( 'message', 'error' ) as $key ) { if ( ! empty( $data[ $key ] ) && is_string( $data[ $key ] ) ) { return $data[ $key ]; } } return $body ? $body : $fallback; } add_action( 'adfoin_action_fields', 'adfoin_airmeet_action_fields' ); function adfoin_airmeet_action_fields() { ?> get_error_message() ); } $response = adfoin_airmeet_request( $credentials, 'airmeets?size=500', 'GET' ); if ( is_wp_error( $response ) ) { wp_send_json_error( $response->get_error_message() ); } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( ! isset( $data['data'] ) || ! is_array( $data['data'] ) ) { wp_send_json_error( __( 'Invalid response from Airmeet API.', 'advanced-form-integration' ) ); } $airmeets = array(); foreach ( $data['data'] as $airmeet ) { if ( isset( $airmeet['uid'], $airmeet['name'] ) ) { $airmeets[] = array( 'uid' => $airmeet['uid'], 'name' => $airmeet['name'], 'status' => isset( $airmeet['status'] ) ? $airmeet['status'] : '', ); } } wp_send_json_success( $airmeets ); } add_action( 'wp_ajax_adfoin_get_airmeet_fields', 'adfoin_get_airmeet_fields' ); function adfoin_get_airmeet_fields() { adfoin_verify_nonce(); $fields = array( array( 'key' => 'email', 'value' => __( 'Email *', 'advanced-form-integration' ), 'required' => true, 'description' => __( 'Required field', 'advanced-form-integration' ), ), array( 'key' => 'firstName', 'value' => __( 'First Name *', 'advanced-form-integration' ), 'required' => true, 'description' => __( 'Required field (max 35 characters)', 'advanced-form-integration' ), ), array( 'key' => 'lastName', 'value' => __( 'Last Name *', 'advanced-form-integration' ), 'required' => true, 'description' => __( 'Required field (max 35 characters)', 'advanced-form-integration' ), ), array( 'key' => 'organisation', 'value' => __( 'Organisation', 'advanced-form-integration' ), 'description' => __( 'Max 70 characters', 'advanced-form-integration' ), ), array( 'key' => 'designation', 'value' => __( 'Designation / Job Title', 'advanced-form-integration' ), 'description' => __( 'Max 70 characters', 'advanced-form-integration' ), ), array( 'key' => 'city', 'value' => __( 'City', 'advanced-form-integration' ), 'description' => __( 'Max 70 characters', 'advanced-form-integration' ), ), array( 'key' => 'country', 'value' => __( 'Country', 'advanced-form-integration' ), 'description' => __( 'Max 70 characters', 'advanced-form-integration' ), ), array( 'key' => 'attendance_type', 'value' => __( 'Attendance Type', 'advanced-form-integration' ), 'description' => __( 'IN-PERSON or VIRTUAL (mandatory for Hybrid Conference)', 'advanced-form-integration' ), ), array( 'key' => 'registerAttendee', 'value' => __( 'Register Attendee', 'advanced-form-integration' ), 'description' => __( 'true = confirmed registration, false = invitation only (default: false)', 'advanced-form-integration' ), ), array( 'key' => 'sendEmailInvite', 'value' => __( 'Send Email Invite', 'advanced-form-integration' ), 'description' => __( 'true = send email with calendar invite (default: true)', 'advanced-form-integration' ), ), ); wp_send_json_success( $fields ); } add_action( 'adfoin_airmeet_job_queue', 'adfoin_airmeet_job_queue_handler', 10, 1 ); function adfoin_airmeet_job_queue_handler( $data ) { adfoin_airmeet_send_data( $data['record'], $data['posted_data'] ); } function adfoin_airmeet_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) { return; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $cred_id = isset( $field_data['credId'] ) ? $field_data['credId'] : ''; if ( ! $cred_id ) { adfoin_add_to_log( new WP_Error( 'airmeet_no_account', __( 'Not sent: no Airmeet account is selected in the integration.', 'advanced-form-integration' ) ), '', array(), $record ); return; } $credentials = adfoin_airmeet_get_credentials( $cred_id ); if ( is_wp_error( $credentials ) ) { adfoin_add_to_log( $credentials, '', array(), $record ); return; } $airmeet_id = adfoin_airmeet_parse_value( $field_data, 'airmeetId', $posted_data ); if ( '' === $airmeet_id ) { adfoin_add_to_log( new WP_Error( 'airmeet_missing_id', __( 'Airmeet ID is required.', 'advanced-form-integration' ) ), '', array(), $record ); return; } $payload = adfoin_airmeet_collect_payload( $field_data, $posted_data ); if ( is_wp_error( $payload ) ) { adfoin_add_to_log( $payload, '', array(), $record ); return; } $endpoint = sprintf( 'airmeet/%s/attendee', rawurlencode( $airmeet_id ) ); // adfoin_airmeet_request() logs the response (and login failures) itself. adfoin_airmeet_request( $credentials, $endpoint, 'POST', $payload, $record ); } function adfoin_airmeet_collect_payload( $field_data, $posted_data ) { $email = adfoin_airmeet_parse_value( $field_data, 'email', $posted_data ); $firstName = adfoin_airmeet_parse_value( $field_data, 'firstName', $posted_data ); $lastName = adfoin_airmeet_parse_value( $field_data, 'lastName', $posted_data ); if ( '' === $email ) { return new WP_Error( 'airmeet_missing_email', __( 'Email is required.', 'advanced-form-integration' ) ); } if ( ! is_email( $email ) ) { return new WP_Error( 'airmeet_invalid_email', sprintf( __( 'Not sent: "%s" is not a valid email address.', 'advanced-form-integration' ), $email ) ); } if ( '' === $firstName ) { return new WP_Error( 'airmeet_missing_firstname', __( 'First Name is required.', 'advanced-form-integration' ) ); } if ( '' === $lastName ) { return new WP_Error( 'airmeet_missing_lastname', __( 'Last Name is required.', 'advanced-form-integration' ) ); } $payload = array( 'email' => $email, 'firstName' => substr( $firstName, 0, 35 ), 'lastName' => substr( $lastName, 0, 35 ), ); $map = array( 'organisation' => 'organisation', 'designation' => 'designation', 'city' => 'city', 'country' => 'country', 'attendance_type' => 'attendance_type', ); foreach ( $map as $key => $api_key ) { $value = adfoin_airmeet_parse_value( $field_data, $key, $posted_data ); if ( '' === $value ) { continue; } if ( in_array( $key, array( 'organisation', 'designation', 'city', 'country' ), true ) ) { $value = substr( $value, 0, 70 ); } $payload[ $api_key ] = $value; } $register_attendee = adfoin_airmeet_parse_value( $field_data, 'registerAttendee', $posted_data ); if ( '' !== $register_attendee ) { $payload['registerAttendee'] = adfoin_airmeet_normalize_boolean( $register_attendee ); } $send_email_invite = adfoin_airmeet_parse_value( $field_data, 'sendEmailInvite', $posted_data ); if ( '' !== $send_email_invite ) { $payload['sendEmailInvite'] = adfoin_airmeet_normalize_boolean( $send_email_invite ); } return $payload; } function adfoin_airmeet_parse_value( $field_data, $key, $posted_data ) { if ( ! isset( $field_data[ $key ] ) ) { return ''; } $value = adfoin_get_parsed_values( $field_data[ $key ], $posted_data ); if ( is_array( $value ) ) { return ''; } return is_string( $value ) ? trim( $value ) : ''; } function adfoin_airmeet_normalize_boolean( $value ) { $value = strtolower( trim( (string) $value ) ); return in_array( $value, array( '1', 'true', 'yes', 'on' ), true ); } function adfoin_airmeet_credentials_list() { $credentials = adfoin_read_credentials( 'airmeet' ); foreach ( $credentials as $option ) { printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) ); } } function adfoin_airmeet_get_credentials( $cred_id ) { $credentials = adfoin_get_credentials_by_id( 'airmeet', $cred_id ); if ( empty( $credentials ) ) { return new WP_Error( 'airmeet_missing_credentials', __( 'Airmeet credentials not found.', 'advanced-form-integration' ) ); } return $credentials; } function adfoin_airmeet_get_access_token( $credentials ) { $api_key = isset( $credentials['apiKey'] ) ? trim( $credentials['apiKey'] ) : ''; $api_secret = isset( $credentials['apiSecret'] ) ? trim( $credentials['apiSecret'] ) : ''; if ( '' === $api_key || '' === $api_secret ) { return new WP_Error( 'airmeet_missing_auth', __( 'Airmeet API key or secret is missing.', 'advanced-form-integration' ) ); } $transient_key = adfoin_airmeet_token_key( $credentials ); $cached_token = get_transient( $transient_key ); if ( false !== $cached_token ) { return $cached_token; } $url = adfoin_airmeet_base_url( $credentials ) . 'auth'; $args = array( 'timeout' => 30, 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Airmeet-Access-Key' => $api_key, 'X-Airmeet-Secret-Key' => $api_secret, ), ); $response = wp_remote_request( esc_url_raw( $url ), $args ); if ( is_wp_error( $response ) ) { return $response; } $status = wp_remote_retrieve_response_code( $response ); if ( $status >= 400 ) { $message = adfoin_airmeet_error_text( wp_remote_retrieve_body( $response ), __( 'Airmeet authentication failed.', 'advanced-form-integration' ) ); return new WP_Error( 'airmeet_auth_error', sprintf( __( 'Airmeet login failed: %s', 'advanced-form-integration' ), $message ), array( 'status' => $status ) ); } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( ! isset( $data['token'] ) || empty( $data['token'] ) ) { return new WP_Error( 'airmeet_invalid_token', __( 'Failed to retrieve access token from Airmeet.', 'advanced-form-integration' ) ); } $token = $data['token']; set_transient( $transient_key, $token, 25 * DAY_IN_SECONDS ); return $token; } /* * Cache key of the login token; includes the region, since a token is only valid on its region's server. */ function adfoin_airmeet_token_key( $credentials ) { $api_key = isset( $credentials['apiKey'] ) ? trim( $credentials['apiKey'] ) : ''; $api_secret = isset( $credentials['apiSecret'] ) ? trim( $credentials['apiSecret'] ) : ''; $region = isset( $credentials['region'] ) ? $credentials['region'] : ''; return 'airmeet_token_' . md5( $api_key . $api_secret . $region ); } function adfoin_airmeet_request( $credentials, $endpoint, $method = 'POST', $data = array(), $record = array(), $retried = false ) { $access_token = adfoin_airmeet_get_access_token( $credentials ); if ( is_wp_error( $access_token ) ) { if ( $record ) { adfoin_add_to_log( $access_token, adfoin_airmeet_base_url( $credentials ) . 'auth', array( 'method' => 'POST' ), $record ); } return $access_token; } $url = adfoin_airmeet_base_url( $credentials ) . ltrim( $endpoint, '/' ); $args = array( 'timeout' => 30, 'method' => strtoupper( $method ), 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Airmeet-Access-Token' => $access_token, ), ); if ( 'GET' === strtoupper( $method ) ) { if ( ! empty( $data ) ) { $url = add_query_arg( $data, $url ); } } else { $args['body'] = wp_json_encode( $data ); } $response = wp_remote_request( esc_url_raw( $url ), $args ); $status = is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ); // An expired (412) or rejected (401) token: forget it and log in once more. if ( ! $retried && in_array( $status, array( 401, 412 ), true ) ) { delete_transient( adfoin_airmeet_token_key( $credentials ) ); return adfoin_airmeet_request( $credentials, $endpoint, $method, $data, $record, true ); } if ( $record ) { $logged = $response; if ( ! is_wp_error( $logged ) ) { if ( $status >= 400 ) { $logged['response']['message'] = adfoin_airmeet_error_text( wp_remote_retrieve_body( $logged ), $logged['response']['message'] ); } elseif ( 202 === $status ) { $logged['response']['message'] = __( 'Accepted: Airmeet is still processing this request and may need it again in about 5 minutes', 'advanced-form-integration' ); } } adfoin_add_to_log( $logged, $url, $args, $record ); } if ( is_wp_error( $response ) ) { return $response; } if ( $status >= 400 ) { return new WP_Error( 'airmeet_http_error', adfoin_airmeet_error_text( wp_remote_retrieve_body( $response ), __( 'Airmeet request failed.', 'advanced-form-integration' ) ), array( 'status' => $status ) ); } return $response; }