wp-go/app/theme/wp/components/widget/recentcomments.go

69 lines
2.3 KiB
Go
Raw Permalink Normal View History

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/cmd/reload"
"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-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}
`
2023-03-28 13:45:20 +00:00
func RecentComments(h *wp.Handle, id string) string {
2023-05-03 15:57:49 +00:00
conf := configs(recentCommentConf, "widget_recent-comments", int64(2))
2023-03-28 13:45:20 +00:00
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
commentsArgs := recentCommentsArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, 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
})
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>发表在
<a href="/p/%v#comment-%v">%s</a>
</li>`, t.CommentAuthor, t.CommentPostId, t.CommentId, t.PostTitle)
2023-03-10 11:38:32 +00:00
})
s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n"))
return h.ComponentFilterFnHook(widgets.RecentComments, str.Replace(s, args))
2023-03-10 11:38:32 +00:00
}