10, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Bearer ' . $key, 'Accept' => 'application/json', ), )); if (is_wp_error($response)) { /* translators: %s: connection error */ return new WP_Error('bunstream_http', sprintf(__('تعذّر الاتصال بـ BunStream: %s', 'bunstream'), $response->get_error_message())); } $code = (int) wp_remote_retrieve_response_code($response); $body = json_decode((string) wp_remote_retrieve_body($response), true); if ($code === 401) { return new WP_Error('bunstream_auth', __('المفتاح غير صحيح أو تم إلغاؤه. اعمل مفتاح WordPress جديد من صفحة «ربط المنصة» في BunStream.', 'bunstream')); } if ($code === 429) { return new WP_Error('bunstream_throttled', __('طلبات كثيرة في دقيقة واحدة. حاول بعد قليل.', 'bunstream')); } if ($code < 200 || $code >= 300 || !is_array($body) || empty($body['success'])) { $message = is_array($body) && !empty($body['message']) ? (string) $body['message'] : 'HTTP ' . $code; /* translators: %s: error message from the server */ return new WP_Error('bunstream_api', sprintf(__('BunStream رجّع خطأ: %s', 'bunstream'), $message)); } $result = array('data' => isset($body['data']) ? $body['data'] : array()); if (isset($body['meta'])) { $result['meta'] = $body['meta']; } set_transient($cache_key, $result, self::CACHE_TTL); return $result; } /** * The account details, refreshed from the API, and remembered in an * option for the front end (which never calls the API). * * @return array|WP_Error */ public static function refresh_account($fresh = false) { $result = self::get('account', array(), $fresh); if (is_wp_error($result)) { return $result; } $data = is_array($result['data']) ? $result['data'] : array(); $account = array( 'name' => isset($data['name']) ? sanitize_text_field($data['name']) : '', 'require_signed_student_id' => !empty($data['require_signed_student_id']), 'allowed_domains' => isset($data['allowed_domains']) && is_array($data['allowed_domains']) ? array_values(array_map('sanitize_text_field', $data['allowed_domains'])) : array(), 'player_embed_base' => isset($data['player_embed_base']) ? esc_url_raw($data['player_embed_base']) : '', 'checked_at' => time(), ); update_option(BunStream_Options::ACCOUNT_OPTION, $account, false); return $account; } /** Drop every cached answer (after the key or base URL changes). */ public static function flush_cache() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- transients have no delete-by-prefix API. $wpdb->query($wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like('_transient_bunstream_') . '%', $wpdb->esc_like('_transient_timeout_bunstream_') . '%' )); } }