优化 fix bug

This commit is contained in:
xing 2023-03-17 19:51:53 +08:00
parent 14aad9e15b
commit 57a21010a8
8 changed files with 114 additions and 110 deletions

View File

@ -10,18 +10,19 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var archivesConfig = func() safety.Var[map[any]any] {
v := safety.Var[map[any]any]{}
v.Store(map[any]any{
"count": int64(0),
"dropdown": int64(0),
"title": "归档",
})
archiveArgs.Store(map[string]string{
var archiveTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
func archiveArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
@ -34,24 +35,23 @@ var archivesConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份",
})
return v
}()
}
}
var archiveArgs = safety.Var[map[string]string]{}
var archiveTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
func archivesConfig() map[any]any {
return map[any]any{
"count": int64(0),
"dropdown": int64(0),
"title": "归档",
}
}
func Archive(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs.Load())
args = maps.FilterZeroMerge(archiveArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2))
archiveArgs := archiveArgs()
archivesConfig := archivesConfig()
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs)
args = maps.FilterZeroMerge(archiveArgs, args)
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
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}"])

View File

@ -0,0 +1,10 @@
package widget
func commonArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="%s" class="widget widget_%s">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": "</h2>",
}
}

View File

@ -12,21 +12,29 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"net/http"
"strings"
)
var categoryArgs safety.Var[map[string]string]
var categoryConfig = func() safety.Var[map[any]any] {
v := safety.Var[map[any]any]{}
v.Store(map[any]any{
var categoryTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
func categoryConfig() map[any]any {
return map[any]any{
"count": int64(0),
"dropdown": int64(0),
"hierarchical": int64(0),
"title": "分类",
})
categoryArgs.Store(map[string]string{
}
}
func categoryArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="categories-2" class="widget widget_categories">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
@ -44,23 +52,14 @@ var categoryConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份",
})
return v
}()
var categoryTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
}
}
func Category(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs.Load())
args = maps.FilterZeroMerge(categoryArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2))
conf = maps.FilterZeroMerge(categoryConfig.Load(), conf)
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
args = maps.FilterZeroMerge(categoryArgs(), args)
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
conf = maps.FilterZeroMerge(categoryConfig(), conf)
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
t := categoryTemplate
dropdown := conf["dropdown"].(int64)
@ -240,7 +239,7 @@ func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
}
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs.Load())
cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs())
name, ok := cate["{$name}"]
if !ok || name == "" {
return

View File

@ -8,7 +8,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
@ -21,9 +20,8 @@ var metaTemplate = `{$before_widget}
{$navCloser}
{$after_widget}`
var metaArgs = func() safety.Var[map[string]string] {
v := safety.Var[map[string]string]{}
v.Store(map[string]string{
func metaArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`,
"{$after_widget}": `</aside>`,
"{$aria_label}": "",
@ -31,13 +29,13 @@ var metaArgs = func() safety.Var[map[string]string] {
"": "",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`,
})
return v
}()
}
}
func Meta(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs.Load())
args = maps.FilterZeroMerge(metaArgs.Load(), args)
metaArgs := metaArgs()
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
args = maps.FilterZeroMerge(metaArgs, args)
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_meta", "其它操作", int64(2), "title")
if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])

View File

@ -10,13 +10,11 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var recentCommentsArgs = func() safety.Var[map[string]string] {
v := safety.Var[map[string]string]{}
v.Store(map[string]string{
func recentCommentsArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
@ -27,15 +25,15 @@ var recentCommentsArgs = func() safety.Var[map[string]string] {
"{$navCloser}": "",
"{$title}": "",
"{$recent_comments_id}": "recentcomments",
})
recentCommentConf.Store(map[any]any{
}
}
func recentCommentConf() map[any]any {
return map[any]any{
"number": int64(5),
"title": "近期评论",
})
return v
}()
var recentCommentConf = safety.Var[map[any]any]{}
}
}
var recentCommentsTemplate = `{$before_widget}
{$nav}
@ -48,10 +46,12 @@ var recentCommentsTemplate = `{$before_widget}
`
func RecentComments(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs.Load())
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2))
conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf)
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}"])

View File

@ -11,12 +11,9 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var recentPostsArgs = safety.Var[map[string]string]{}
var recentPostsTemplate = `{$before_widget}
{$nav}
{$title}
@ -27,9 +24,8 @@ var recentPostsTemplate = `{$before_widget}
{$after_widget}
`
var recentConf = func() safety.Var[map[any]any] {
recentPostsArgs.Store(map[string]string{
func recentPostsArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
@ -39,21 +35,24 @@ var recentConf = func() safety.Var[map[any]any] {
"{$nav}": "",
"{$navCloser}": "",
"{$title}": "",
})
v := safety.Var[map[any]any]{}
v.Store(map[any]any{
}
}
func recentConf() map[any]any {
return map[any]any{
"number": int64(5),
"show_date": false,
"title": "近期文章",
})
return v
}()
}
}
func RecentPosts(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs.Load())
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args)
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf.Load(), int64(2))
conf = maps.FilterZeroMerge(recentConf.Load(), conf)
recentPostsArgs := recentPostsArgs()
recentConf := recentConf()
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs)
args = maps.FilterZeroMerge(recentPostsArgs, args)
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
conf = maps.FilterZeroMerge(recentConf, conf)
if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
}

View File

@ -9,7 +9,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
@ -18,24 +17,6 @@ var searchTemplate = `{$before_widget}
{$form}
{$after_widget}`
var searchArgs = func() safety.Var[map[string]string] {
v := safety.Var[map[string]string]{}
v.Store(map[string]string{
"{$id}": "2",
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
"{$after_widget}": `</aside>`,
"{$aria_label}": "",
"{$title}": "",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`,
"{$form}": "",
"{$button}": "搜索",
"{$placeholder}": "搜索&hellip;",
"{$label}": "搜索:",
})
return v
}()
var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/">
<label>
<span class="screen-reader-text">{$label}</span>
@ -51,9 +32,26 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
</div>
</form>`
func SearchForm(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Search, searchArgs.Load())
args = maps.FilterZeroMerge(searchArgs.Load(), args)
func searchArgs() map[string]string {
return map[string]string{
"{$id}": "2",
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
"{$after_widget}": `</aside>`,
"{$aria_label}": "",
"{$title}": "",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`,
"{$form}": "",
"{$button}": "搜索",
"{$placeholder}": "搜索&hellip;",
"{$label}": "搜索:",
}
}
func Search(h *wp.Handle) string {
searchArgs := searchArgs()
args := wp.GetComponentsArgs(h, widgets.Search, searchArgs)
args = maps.FilterZeroMerge(searchArgs, args)
if args["{$title}"] == "" {
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
}

View File

@ -13,7 +13,7 @@ import (
)
var widgetFn = map[string]func(*wp.Handle) string{
"search": widget.SearchForm,
"search": widget.Search,
"recent-posts": widget.RecentPosts,
"recent-comments": widget.RecentComments,
"archives": widget.Archive,