2023-03-12 12:41:10 +00:00
|
|
|
package widget
|
2023-03-10 11:38:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/theme/wp"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-03-10 11:38:32 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-03-17 11:51:53 +00:00
|
|
|
func recentCommentsArgs() map[string]string {
|
|
|
|
return map[string]string{
|
2023-03-12 12:28:57 +00:00
|
|
|
"{$before_sidebar}": "",
|
|
|
|
"{$after_sidebar}": "",
|
|
|
|
"{$nav}": "",
|
|
|
|
"{$navCloser}": "",
|
|
|
|
"{$title}": "",
|
|
|
|
"{$recent_comments_id}": "recentcomments",
|
2023-03-17 11:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:57:49 +00:00
|
|
|
var recentCommentConf = map[any]any{
|
|
|
|
"number": int64(5),
|
|
|
|
"title": "近期评论",
|
2023-03-17 11:51:53 +00:00
|
|
|
}
|
2023-03-10 11:38:32 +00:00
|
|
|
|
|
|
|
var recentCommentsTemplate = `{$before_widget}
|
|
|
|
{$nav}
|
|
|
|
{$title}
|
|
|
|
<ul id="{$recent_comments_id}">
|
|
|
|
{$li}
|
|
|
|
</ul>
|
|
|
|
{$navCloser}
|
|
|
|
{$after_widget}
|
|
|
|
`
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetRecentCommentConf = BuildconfigFn(recentCommentConf, "widget_recent-comments", int64(2))
|
2023-05-03 15:57:49 +00:00
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetRecentCommentArgs = reload.BuildValFnWithAnyParams("widget-recent-comment-args", RecentCommentArgs)
|
|
|
|
|
|
|
|
func RecentCommentArgs(a ...any) map[string]string {
|
|
|
|
h := a[0].(*wp.Handle)
|
|
|
|
conf := a[1].(map[any]any)
|
|
|
|
id := a[2].(string)
|
|
|
|
commentsArgs := recentCommentsArgs()
|
|
|
|
commonArgs := wp.GetComponentsArgs(widgets.Widget, map[string]string{})
|
|
|
|
args := wp.GetComponentsArgs(widgets.RecentComments, commentsArgs)
|
|
|
|
args = maps.FilterZeroMerge(commentsArgs, CommonArgs(), commonArgs, args)
|
|
|
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("recent-comments-", id), str.Join("widget widget_", "recent_comments"))
|
|
|
|
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
|
|
|
|
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
|
|
|
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
|
|
|
args["{$navCloser}"] = "</nav>"
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
func RecentComments(h *wp.Handle, id string) string {
|
|
|
|
conf := GetRecentCommentConf()
|
|
|
|
args := GetRecentCommentArgs(h, conf, id)
|
2023-03-27 04:59:29 +00:00
|
|
|
|
2023-03-10 11:38:32 +00:00
|
|
|
comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string {
|
|
|
|
return fmt.Sprintf(` <li>
|
|
|
|
<span class="comment-author-link">%s</span>发表在《
|
2024-01-10 15:50:23 +00:00
|
|
|
<a href="%s">%s</a>
|
2023-03-10 11:38:32 +00:00
|
|
|
》
|
2024-01-10 15:50:23 +00:00
|
|
|
</li>`, t.CommentAuthor, t.CommentAuthorUrl, t.PostTitle)
|
2023-03-10 11:38:32 +00:00
|
|
|
})
|
|
|
|
s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n"))
|
2023-08-26 14:01:20 +00:00
|
|
|
return h.DoActionFilter(widgets.RecentComments, str.Replace(s, args))
|
2023-03-10 11:38:32 +00:00
|
|
|
}
|