origin(); if ( is_wp_error( $origin ) ) { return false; } $expected = $origin . '/connect/wordpress'; if ( ! $complete ) { return hash_equals( $expected, $url ); } $parts = wp_parse_url( $url ); if ( ! $parts || isset( $parts['fragment'] ) || ! str_starts_with( $url, $expected . '?' ) ) { return false; } parse_str( $parts['query'] ?? '', $query ); return array( 'user_code' => $code ) === $query; } /** @return array{status:int,data:array,retry_after:int}|WP_Error */ public function request( string $endpoint, array $body = array(), string $token = '' ): array|WP_Error { if ( ! in_array( $endpoint, array( 'device-authorization', 'token', 'account', 'revoke' ), true ) ) { return new WP_Error( 'configuration', __( 'Unsupported API endpoint.', 'waitlistize' ) ); } $origin = $this->origin(); if ( is_wp_error( $origin ) ) { return $origin; } $headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json', ); if ( '' !== $token ) { $headers['Authorization'] = 'Bearer ' . $token; } $response = wp_remote_request( $origin . self::PREFIX . $endpoint, array( 'method' => 'account' === $endpoint ? 'GET' : 'POST', 'headers' => $headers, 'body' => 'account' === $endpoint ? null : wp_json_encode( $body ), 'timeout' => 12, 'redirection' => 0, 'sslverify' => true, 'limit_response_size' => 65536, 'cookies' => array(), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'unavailable', __( 'Waitlistize is temporarily unavailable.', 'waitlistize' ) ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); $retry = wp_remote_retrieve_header( $response, 'retry-after' ); return array( 'status' => (int) wp_remote_retrieve_response_code( $response ), 'data' => is_array( $data ) ? $data : array(), 'retry_after' => is_numeric( $retry ) ? max( 1, (int) $retry ) : max( 5, (int) strtotime( (string) $retry ) - time() ), ); } }