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/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-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
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}
|
|
|
|
`
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func DefaultRecentPostsArgs() map[string]string {
|
2023-03-17 11:51:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func DefaultRecentConf() map[any]any {
|
2023-03-17 11:51:53 +00:00
|
|
|
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
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetRecentPostConf = reload.BuildValFnWithAnyParams("widget-recent-posts-conf", RecentPostConf)
|
2023-03-27 04:59:29 +00:00
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
func RecentPostConf(_ ...any) map[any]any {
|
|
|
|
recent := DefaultRecentConf()
|
|
|
|
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
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
var GetRecentPostArgs = reload.BuildValFnWithAnyParams("widget-recent-posts-args", ParseRecentPostArgs)
|
|
|
|
|
|
|
|
func ParseRecentPostArgs(a ...any) map[string]string {
|
|
|
|
h := a[0].(*wp.Handle)
|
|
|
|
conf := a[1].(map[any]any)
|
|
|
|
id := a[2].(string)
|
|
|
|
recent := DefaultRecentPostsArgs()
|
|
|
|
commonArgs := wp.GetComponentsArgs(widgets.Widget, map[string]string{})
|
|
|
|
args := wp.GetComponentsArgs(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
|
|
|
|
}
|
|
|
|
|
|
|
|
func RecentPosts(h *wp.Handle, id string) string {
|
|
|
|
conf := GetRecentPostConf()
|
|
|
|
args := GetRecentPostArgs(h, conf, id)
|
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"))
|
2023-08-26 14:01:20 +00:00
|
|
|
return h.DoActionFilter(widgets.RecentPosts, str.Replace(s, args))
|
2023-03-10 10:20:38 +00:00
|
|
|
}
|