Files
laralog/app/Support/helpers.php
T
ak f1b9d1e1c3 refactor: 外链中转拆为内置插件 neatstudio.link-tracker
- 插件承载全部逻辑:redirects 迁移(hasTable 兼容旧库)、Redirect 模型、UrlRedirector、/go 路由+控制器、Filament 外链中转资源
- 核心只留 hook 接入点:
  - tracked_url() 助手走 link.redirect 过滤器(评论正文/作者网址/友链模板统一调用)
  - 正文外链改写移到插件的 post.rendered 过滤器(优先级 20,晚于会员付费过滤)
- 停用插件:链接恢复直连、内容不受影响;后台「管理 → 外链中转」查看点击量/启停
- 测试更新为插件上下文(含无过滤器降级断言);97 通过
2026-08-13 02:05:32 +08:00

74 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Blog\Support\ThemeManager;
if (! function_exists('theme_view')) {
/**
* 渲染主题视图:激活主题目录已前置到全局视图路径,主题缺失时自动回退默认视图。
*/
function theme_view(string $view, array $data = []): \Illuminate\Contracts\View\View
{
return view($view, $data);
}
}
if (! function_exists('theme')) {
/**
* 主题辅助函数:theme() / theme('asset', 'style.css') / theme('var', 'key', default)
*/
function theme(?string $method = null, ...$args): mixed
{
$manager = app(ThemeManager::class);
return match ($method) {
'asset' => $manager->assetUrl($args[0] ?? '', $args[1] ?? null),
'var' => $manager->variable($args[0] ?? '', $args[1] ?? null),
'vars' => $manager->variables(),
'name' => $manager->active(),
default => $manager,
};
}
}
if (! function_exists('blog_setting')) {
/**
* 读取博客设置(Setting 表,带缓存)。
*/
function blog_setting(string $key, mixed $default = null): mixed
{
return \App\Models\Setting::get($key, $default);
}
}
if (! function_exists('tracked_url')) {
/**
* 外链追踪钩子:核心视图统一调用,由插件(外链中转)改写为 /go/{hash}
* 插件停用时原样返回。站内链接等由插件自行判断。
*/
function tracked_url(string $url): string
{
return app(\App\Blog\Support\PluginManager::class)->applyFilters('link.redirect', $url);
}
}
if (! function_exists('comment_body')) {
/**
* 评论正文:转义 + 自动链接 URL(经 tracked_url 走中转,nofollow)。
*/
function comment_body(string $text): string
{
$text = e($text);
$text = preg_replace_callback(
'#(https?://[^\s<>"\'()]+)#i',
fn (array $m) => '<a href="'.e(tracked_url($m[1])).'" target="_blank" rel="nofollow noopener">'.$m[1].'</a>',
$text
);
return nl2br($text);
}
}