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

90 lines
2.8 KiB
Go
Raw Permalink Normal View History

2023-03-12 12:41:10 +00:00
package widget
2023-03-10 10:20:38 +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"
"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"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-03-10 10:20:38 +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"
)
var recentPostsTemplate = `{$before_widget}
{$nav}
2023-03-10 10:33:09 +00:00
{$title}
2023-03-10 10:20:38 +00:00
<ul>
{$li}
</ul>
{$navCloser}
{$after_widget}
`
2023-03-17 11:51:53 +00:00
func recentPostsArgs() map[string]string {
return map[string]string{
2023-03-12 12:28:57 +00:00
"{$before_sidebar}": "",
"{$after_sidebar}": "",
"{$nav}": "",
"{$navCloser}": "",
"{$title}": "",
2023-03-17 11:51:53 +00:00
}
}
func recentConf() map[any]any {
return map[any]any{
2023-03-12 12:28:57 +00:00
"number": int64(5),
"show_date": false,
"title": "近期文章",
2023-03-17 11:51:53 +00:00
}
}
2023-03-10 10:20:38 +00:00
2023-03-28 13:45:20 +00:00
func RecentPosts(h *wp.Handle, id string) string {
2023-03-27 04:59:29 +00:00
conf := reload.GetAnyValBys("widget-recent-posts-conf", h, func(h *wp.Handle) map[any]any {
recent := recentConf()
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2))
conf = maps.FilterZeroMerge(recent, conf)
return conf
})
2023-03-28 13:45:20 +00:00
args := reload.GetAnyValBys("widget-recent-posts-args", h, func(h *wp.Handle) map[string]string {
recent := recentPostsArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent)
args = maps.FilterZeroMerge(recent, CommonArgs(), commonArgs, args)
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("recent-posts-", id), str.Join("widget widget_", "recent_entries"))
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-10 10:20:38 +00:00
currentPostId := uint64(0)
if h.Scene() == constraints.Detail {
2023-03-10 10:33:09 +00:00
currentPostId = str.ToInteger(h.C.Param("id"), uint64(0))
2023-03-10 10:20:38 +00:00
}
posts := slice.Map(cache.RecentPosts(h.C, int(conf["number"].(int64))), func(t models.Posts) string {
2023-03-12 12:41:10 +00:00
t = wp.ProjectTitle(t)
2023-03-10 10:20:38 +00:00
date := ""
2023-03-12 06:25:22 +00:00
if v, ok := conf["show_date"].(bool); ok && v {
2023-03-10 10:20:38 +00:00
date = fmt.Sprintf(`<span class="post-date">%s</span>`, t.PostDateGmt.Format("2006年01月02日"))
}
ariaCurrent := ""
if currentPostId == t.Id {
ariaCurrent = ` aria-current="page"`
}
2023-03-10 10:33:09 +00:00
return fmt.Sprintf(` <li>
2023-03-10 11:38:32 +00:00
<a href="/p/%v"%s>%s</a>
2023-03-10 10:33:09 +00:00
%s
2023-03-10 11:38:32 +00:00
</li>`, t.Id, ariaCurrent, t.PostTitle, date)
2023-03-10 10:20:38 +00:00
})
s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n"))
return h.ComponentFilterFnHook(widgets.RecentPosts, str.Replace(s, args))
2023-03-10 10:20:38 +00:00
}