validate_service_url(defined('LIVEBLOG_RENDER_ORIGIN') ? constant('LIVEBLOG_RENDER_ORIGIN') : 'https://render.liveblog.ai', true), 'assets' => validate_service_url(defined('LIVEBLOG_ASSET_ORIGIN') ? constant('LIVEBLOG_ASSET_ORIGIN') : 'https://cdn.liveblog.ai', true), 'reader' => validate_service_url(defined('LIVEBLOG_READER_ORIGIN') ? constant('LIVEBLOG_READER_ORIGIN') : 'https://reader.api.liveblog.ai', true), 'script' => validate_service_url(defined('LIVEBLOG_EMBED_SCRIPT_URL') ? constant('LIVEBLOG_EMBED_SCRIPT_URL') : 'https://render.liveblog.ai/embed.js', false), ); } return $config; } function resolve_mode($mode): string { if (in_array($mode, array(MODE_SSR, MODE_JS), true)) { return $mode; } $settings = get_option(OPTION, array()); return is_array($settings) && ($settings['mode'] ?? '') === MODE_JS ? MODE_JS : MODE_SSR; } function sanitize_settings($input): array { return array('mode' => is_array($input) && ($input['mode'] ?? '') === MODE_JS ? MODE_JS : MODE_SSR); } function register_settings(): void { register_setting(OPTION, OPTION, array('type' => 'array', 'sanitize_callback' => __NAMESPACE__ . '\\sanitize_settings', 'default' => array('mode' => MODE_SSR))); } function register_settings_page(): void { add_options_page(__('LiveblogAI', 'liveblogai'), __('LiveblogAI', 'liveblogai'), 'manage_options', 'liveblog', __NAMESPACE__ . '\\settings_page'); } function request_service(string $url, float $timeout, int $limit) { $args = array( 'timeout' => $timeout, 'redirection' => 0, 'limit_response_size' => $limit, 'cookies' => array(), 'headers' => array('Accept' => 'text/html, application/json', 'Cache-Control' => 'no-cache'), 'user-agent' => 'LiveblogAI-WordPress/' . VERSION, 'sslverify' => true, ); return is_local_service($url) ? wp_remote_get($url, $args) : wp_safe_remote_get($url, $args); } function check_connection(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('You cannot manage LiveblogAI settings.', 'liveblogai'), '', array('response' => 403)); } check_admin_referer('liveblog_check_connection'); $origin = service_config()['render']; $response = $origin !== '' ? request_service($origin . '/health', REQUEST_TIMEOUT, 2048) : null; $ok = is_array($response) && wp_remote_retrieve_response_code($response) === 200; wp_safe_redirect(add_query_arg('liveblog_connection', $ok ? 'ok' : 'failed', admin_url('options-general.php?page=liveblog'))); exit; } function settings_page(): void { if (!current_user_can('manage_options')) { return; } $config = service_config(); $mode = resolve_mode(''); ?>