优化 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/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var archivesConfig = func() safety.Var[map[any]any] { var archiveTemplate = `{$before_widget}
v := safety.Var[map[any]any]{} {$title}
v.Store(map[any]any{ {$nav}
"count": int64(0), {$html}
"dropdown": int64(0), {$navCloser}
"title": "归档", {$after_widget}
}) `
archiveArgs.Store(map[string]string{
func archiveArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`, "{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -34,24 +35,23 @@ var archivesConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2", "{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly", "{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份", "{$dropdown_label}": "选择月份",
}) }
return v }
}()
var archiveArgs = safety.Var[map[string]string]{} func archivesConfig() map[any]any {
return map[any]any{
var archiveTemplate = `{$before_widget} "count": int64(0),
{$title} "dropdown": int64(0),
{$nav} "title": "归档",
{$html} }
{$navCloser} }
{$after_widget}
`
func Archive(h *wp.Handle) string { func Archive(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs.Load()) archiveArgs := archiveArgs()
args = maps.FilterZeroMerge(archiveArgs.Load(), args) archivesConfig := archivesConfig()
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2)) 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}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$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/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"net/http" "net/http"
"strings" "strings"
) )
var categoryArgs safety.Var[map[string]string] var categoryTemplate = `{$before_widget}
var categoryConfig = func() safety.Var[map[any]any] { {$title}
v := safety.Var[map[any]any]{} {$nav}
v.Store(map[any]any{ {$html}
{$navCloser}
{$after_widget}
`
func categoryConfig() map[any]any {
return map[any]any{
"count": int64(0), "count": int64(0),
"dropdown": int64(0), "dropdown": int64(0),
"hierarchical": int64(0), "hierarchical": int64(0),
"title": "分类", "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">`, "{$before_widget}": `<aside id="categories-2" class="widget widget_categories">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -44,23 +52,14 @@ var categoryConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2", "{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly", "{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份", "{$dropdown_label}": "选择月份",
}) }
return v }
}()
var categoryTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
func Category(h *wp.Handle) string { func Category(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs.Load()) args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
args = maps.FilterZeroMerge(categoryArgs.Load(), args) args = maps.FilterZeroMerge(categoryArgs(), args)
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2)) conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
conf = maps.FilterZeroMerge(categoryConfig.Load(), conf) conf = maps.FilterZeroMerge(categoryConfig(), conf)
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
t := categoryTemplate t := categoryTemplate
dropdown := conf["dropdown"].(int64) 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) { 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}"] name, ok := cate["{$name}"]
if !ok || name == "" { if !ok || name == "" {
return return

View File

@ -8,7 +8,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
@ -21,9 +20,8 @@ var metaTemplate = `{$before_widget}
{$navCloser} {$navCloser}
{$after_widget}` {$after_widget}`
var metaArgs = func() safety.Var[map[string]string] { func metaArgs() map[string]string {
v := safety.Var[map[string]string]{} return map[string]string{
v.Store(map[string]string{
"{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`, "{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`,
"{$after_widget}": `</aside>`, "{$after_widget}": `</aside>`,
"{$aria_label}": "", "{$aria_label}": "",
@ -31,13 +29,13 @@ var metaArgs = func() safety.Var[map[string]string] {
"": "", "": "",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`, "{$after_title}": `</h2>`,
}) }
return v }
}()
func Meta(h *wp.Handle) string { func Meta(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs.Load()) metaArgs := metaArgs()
args = maps.FilterZeroMerge(metaArgs.Load(), args) args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
args = maps.FilterZeroMerge(metaArgs, args)
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_meta", "其它操作", int64(2), "title") args["{$title}"] = wpconfig.GetPHPArrayVal("widget_meta", "其它操作", int64(2), "title")
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$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/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var recentCommentsArgs = func() safety.Var[map[string]string] { func recentCommentsArgs() map[string]string {
v := safety.Var[map[string]string]{} return map[string]string{
v.Store(map[string]string{
"{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`, "{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -27,15 +25,15 @@ var recentCommentsArgs = func() safety.Var[map[string]string] {
"{$navCloser}": "", "{$navCloser}": "",
"{$title}": "", "{$title}": "",
"{$recent_comments_id}": "recentcomments", "{$recent_comments_id}": "recentcomments",
}) }
recentCommentConf.Store(map[any]any{ }
func recentCommentConf() map[any]any {
return map[any]any{
"number": int64(5), "number": int64(5),
"title": "近期评论", "title": "近期评论",
}) }
return v }
}()
var recentCommentConf = safety.Var[map[any]any]{}
var recentCommentsTemplate = `{$before_widget} var recentCommentsTemplate = `{$before_widget}
{$nav} {$nav}
@ -48,10 +46,12 @@ var recentCommentsTemplate = `{$before_widget}
` `
func RecentComments(h *wp.Handle) string { func RecentComments(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs.Load()) recentCommentsArgs := recentCommentsArgs()
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args) recentCommentConf := recentCommentConf()
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2)) args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs)
conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf) 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}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$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/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var recentPostsArgs = safety.Var[map[string]string]{}
var recentPostsTemplate = `{$before_widget} var recentPostsTemplate = `{$before_widget}
{$nav} {$nav}
{$title} {$title}
@ -27,9 +24,8 @@ var recentPostsTemplate = `{$before_widget}
{$after_widget} {$after_widget}
` `
var recentConf = func() safety.Var[map[any]any] { func recentPostsArgs() map[string]string {
return map[string]string{
recentPostsArgs.Store(map[string]string{
"{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`, "{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -39,21 +35,24 @@ var recentConf = func() safety.Var[map[any]any] {
"{$nav}": "", "{$nav}": "",
"{$navCloser}": "", "{$navCloser}": "",
"{$title}": "", "{$title}": "",
}) }
v := safety.Var[map[any]any]{} }
v.Store(map[any]any{
func recentConf() map[any]any {
return map[any]any{
"number": int64(5), "number": int64(5),
"show_date": false, "show_date": false,
"title": "近期文章", "title": "近期文章",
}) }
return v }
}()
func RecentPosts(h *wp.Handle) string { func RecentPosts(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs.Load()) recentPostsArgs := recentPostsArgs()
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args) recentConf := recentConf()
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf.Load(), int64(2)) args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs)
conf = maps.FilterZeroMerge(recentConf.Load(), conf) 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 != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$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/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
@ -18,24 +17,6 @@ var searchTemplate = `{$before_widget}
{$form} {$form}
{$after_widget}` {$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="/"> var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/">
<label> <label>
<span class="screen-reader-text">{$label}</span> <span class="screen-reader-text">{$label}</span>
@ -51,9 +32,26 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
</div> </div>
</form>` </form>`
func SearchForm(h *wp.Handle) string { func searchArgs() map[string]string {
args := wp.GetComponentsArgs(h, widgets.Search, searchArgs.Load()) return map[string]string{
args = maps.FilterZeroMerge(searchArgs.Load(), args) "{$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}"] == "" { if args["{$title}"] == "" {
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "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{ var widgetFn = map[string]func(*wp.Handle) string{
"search": widget.SearchForm, "search": widget.Search,
"recent-posts": widget.RecentPosts, "recent-posts": widget.RecentPosts,
"recent-comments": widget.RecentComments, "recent-comments": widget.RecentComments,
"archives": widget.Archive, "archives": widget.Archive,