$request['first_name'], 'confirmation_sent' => ! empty( $request['confirmation_sent'] ), ]; } function lobbychat_send_setup_request( $request, $request_id ) { $response = wp_remote_post( LOBBYCHAT_API_BASE . '/api/public/plugin/setup-requests', [ 'timeout' => 30, 'redirection' => 0, 'limit_response_size' => 32768, 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json' ], 'body' => wp_json_encode( [ 'requestId' => $request_id, 'name' => $request['name'], 'email' => $request['email'], 'website' => $request['website'], 'wordpressSite' => $request['wordpress_site'], 'pluginVersion' => LOBBYCHAT_VERSION, ] ), ] ); if ( is_wp_error( $response ) ) { return [ 'error' => 'Your request could not reach Lobbychat. Please check your site can make outbound HTTPS connections and try again.' ]; } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code < 200 || $code >= 300 || ! is_array( $body ) || empty( $body['success'] ) || ! isset( $body['data']['requestId'] ) || ! is_string( $body['data']['requestId'] ) || ! preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $body['data']['requestId'] ) ) { $error = isset( $body['error'] ) && is_string( $body['error'] ) ? sanitize_text_field( $body['error'] ) : 'Lobbychat could not confirm that your request was saved. Please try again.'; return [ 'error' => $error ]; } return [ 'remote_id' => $body['data']['requestId'], 'confirmation_sent' => ! empty( $body['data']['confirmation_sent'] ), ]; } function lobbychat_ajax_setup_request() { check_ajax_referer( 'lobbychat_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => 'You do not have permission to submit this request.' ], 403 ); } if ( trim( get_option( 'lobbychat_widget_id', '' ) ) !== '' ) { wp_send_json_error( [ 'message' => 'This installation already has a Lobbychat ID connected.' ], 409 ); } $name = sanitize_text_field( lobbychat_setup_field( 'name' ) ); $email = lobbychat_setup_field( 'email' ); $website = lobbychat_setup_field( 'website' ); $scheme = wp_parse_url( $website, PHP_URL_SCHEME ); if ( $name === '' || strlen( $name ) > 480 ) { wp_send_json_error( [ 'message' => 'Please enter your name.' ], 400 ); } if ( strlen( $email ) > 254 || ! is_email( $email ) ) { wp_send_json_error( [ 'message' => 'Please enter a valid email address.' ], 400 ); } if ( strlen( $website ) > 2048 || ! in_array( strtolower( (string) $scheme ), [ 'http', 'https' ], true ) || ! filter_var( $website, FILTER_VALIDATE_URL ) || wp_parse_url( $website, PHP_URL_USER ) !== null || wp_parse_url( $website, PHP_URL_PASS ) !== null ) { wp_send_json_error( [ 'message' => 'Please enter a valid hotel website URL starting with http:// or https://.' ], 400 ); } $email = sanitize_email( $email ); $website = esc_url_raw( $website, [ 'http', 'https' ] ); // add_option is atomic: prevent two requests from emailing simultaneously. $lock_key = 'lobbychat_setup_request_lock'; $old_lock = get_option( $lock_key, false ); if ( $old_lock && (int) $old_lock < time() - 300 ) { delete_option( $lock_key ); } if ( ! add_option( $lock_key, time(), '', false ) ) { wp_send_json_error( [ 'message' => 'Your setup request is being submitted. Please wait a moment.' ], 409 ); } $result = null; $error = null; try { $existing = get_option( 'lobbychat_setup_request', false ); $new_request = lobbychat_setup_field( 'new_request' ) === '1'; $same_request = $existing && $existing['name'] === $name && $existing['email'] === $email && $existing['website'] === $website; if ( $existing && ( ! $new_request || ( $same_request && ! empty( $existing['remote_id'] ) ) ) ) { // No automatic resubmission of old receipts after upgrading. $result = lobbychat_setup_result( $existing ); } else { $request = [ 'name' => $name, 'first_name' => lobbychat_setup_first_name( $name ), 'website' => $website, 'email' => $email, 'wordpress_site' => home_url( '/' ), 'submitted_at' => current_time( 'mysql' ), 'confirmation_sent' => false, ]; // Keep the same key across a timeout/retry so the server cannot create duplicates. $fingerprint = hash( 'sha256', wp_json_encode( [ $name, $email, $website, $request['wordpress_site'] ] ) ); $attempt = get_option( 'lobbychat_setup_request_attempt', false ); if ( ! is_array( $attempt ) || ( $attempt['fingerprint'] ?? '' ) !== $fingerprint ) { $attempt = [ 'fingerprint' => $fingerprint, 'request_id' => wp_generate_uuid4() ]; update_option( 'lobbychat_setup_request_attempt', $attempt, false ); } $remote = lobbychat_send_setup_request( $request, $attempt['request_id'] ); if ( isset( $remote['error'] ) ) { $error = $remote['error']; } else { $request['remote_id'] = $remote['remote_id']; $request['confirmation_sent'] = $remote['confirmation_sent']; update_option( 'lobbychat_setup_request', $request, false ); delete_option( 'lobbychat_setup_request_attempt' ); $result = lobbychat_setup_result( $request ); } } } finally { delete_option( $lock_key ); } if ( $error ) { wp_send_json_error( [ 'message' => $error ], 500 ); } wp_send_json_success( $result ); }