';
$fragments =& pending_fragments();
$fragments[$marker] = $html;
return $marker;
}
function restore_fragments(string $content): string {
$fragments =& pending_fragments();
foreach ($fragments as $marker => $html) {
if (str_contains($content, $marker)) {
$content = str_replace($marker, $html, $content);
unset($fragments[$marker]);
}
}
return $content;
}
function notice_html(string $message, bool $retry = false): string {
return '
' . esc_html($message) . '
'
. ($retry ? '
' : '')
. '
';
}
function render_ssr(string $id, string $theme = '', ?int $minimum_version = null): string {
static $spent = 0.0;
$origin = service_config()['render'];
if ($origin === '') {
return notice_html(__('The LiveblogAI service is not configured correctly.', 'liveblogai'));
}
$remaining = PAGE_REQUEST_BUDGET - $spent;
if ($remaining < 0.5) {
return notice_html(__('Live coverage could not be loaded in time.', 'liveblogai'), true);
}
$started = microtime(true);
$url = $origin . '/v1/liveblogs/' . rawurlencode($id) . '/fragment';
if ($theme !== '') {
$url = add_query_arg('theme', $theme, $url);
}
if ($minimum_version !== null) {
$url = add_query_arg('version', $minimum_version, $url);
}
$response = request_service($url, min(REQUEST_TIMEOUT, $remaining), MAX_HTML_BYTES + 1);
$spent += microtime(true) - $started;
if (is_wp_error($response)) {
return notice_html(__('Live coverage could not be loaded. Please try again.', 'liveblogai'), true);
}
$status = wp_remote_retrieve_response_code($response);
if ($status === 404) {
return notice_html(__('This liveblog is not available to readers yet, or does not exist.', 'liveblogai'), true);
}
if ($status === 410) {
return notice_html(__('This liveblog has been taken offline.', 'liveblogai'));
}
$html = wp_remote_retrieve_body($response);
$type = wp_remote_retrieve_header($response, 'content-type');
if ($status !== 200 || !is_string($type) || !preg_match('/\Atext\/html(?:\s*;|\s*\z)/i', $type)
|| $html === '' || strlen($html) > MAX_HTML_BYTES
|| !preg_match('/\A(?: