package widget
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"strings"
)
func recentCommentsArgs() map[string]string {
return map[string]string{
"{$before_widget}": `",
"{$before_title}": `
",
"{$before_sidebar}": "",
"{$after_sidebar}": "",
"{$nav}": "",
"{$navCloser}": "",
"{$title}": "",
"{$recent_comments_id}": "recentcomments",
}
}
func recentCommentConf() map[any]any {
return map[any]any{
"number": int64(5),
"title": "近期评论",
}
}
var recentCommentsTemplate = `{$before_widget}
{$nav}
{$title}
{$navCloser}
{$after_widget}
`
func RecentComments(h *wp.Handle) string {
recentCommentsArgs := recentCommentsArgs()
recentCommentConf := recentCommentConf()
args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs)
args = maps.FilterZeroMerge(recentCommentsArgs, args)
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf, int64(2))
conf = maps.FilterZeroMerge(recentCommentConf, conf)
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
}
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`"
}
comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string {
return fmt.Sprintf(`
发表在《
%s
》
`, t.CommentAuthor, t.CommentId, t.CommentPostId, t.PostTitle)
})
s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n"))
return h.ComponentFilterFnHook(widgets.RecentComments, str.Replace(s, args))
}