diff --git a/internal/theme/wp/components/widget/archive.go b/internal/theme/wp/components/widget/archive.go index 7303e43..4aa94f9 100644 --- a/internal/theme/wp/components/widget/archive.go +++ b/internal/theme/wp/components/widget/archive.go @@ -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}": `", "{$before_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}"]) diff --git a/internal/theme/wp/components/widget/args.go b/internal/theme/wp/components/widget/args.go new file mode 100644 index 0000000..fb4e516 --- /dev/null +++ b/internal/theme/wp/components/widget/args.go @@ -0,0 +1,10 @@ +package widget + +func commonArgs() map[string]string { + return map[string]string{ + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + } +} diff --git a/internal/theme/wp/components/widget/category.go b/internal/theme/wp/components/widget/category.go index f66d124..a304a02 100644 --- a/internal/theme/wp/components/widget/category.go +++ b/internal/theme/wp/components/widget/category.go @@ -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}": `", "{$before_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 diff --git a/internal/theme/wp/components/widget/meta.go b/internal/theme/wp/components/widget/meta.go index a0c14e4..28719b1 100644 --- a/internal/theme/wp/components/widget/meta.go +++ b/internal/theme/wp/components/widget/meta.go @@ -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}": ``, "{$aria_label}": "", @@ -31,13 +29,13 @@ var metaArgs = func() safety.Var[map[string]string] { "": "", "{$before_title}": `

`, "{$after_title}": `

`, - }) - 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}"]) diff --git a/internal/theme/wp/components/widget/recentcomments.go b/internal/theme/wp/components/widget/recentcomments.go index 165d613..36d0f8d 100644 --- a/internal/theme/wp/components/widget/recentcomments.go +++ b/internal/theme/wp/components/widget/recentcomments.go @@ -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}": `", "{$before_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}"]) diff --git a/internal/theme/wp/components/widget/recentposts.go b/internal/theme/wp/components/widget/recentposts.go index fe443bf..937264b 100644 --- a/internal/theme/wp/components/widget/recentposts.go +++ b/internal/theme/wp/components/widget/recentposts.go @@ -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}": `", "{$before_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}"]) } diff --git a/internal/theme/wp/components/widget/search.go b/internal/theme/wp/components/widget/search.go index c0ea534..9f79cad 100644 --- a/internal/theme/wp/components/widget/search.go +++ b/internal/theme/wp/components/widget/search.go @@ -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}": ``, - "{$aria_label}": "", - "{$title}": "", - "{$before_title}": `

`, - "{$after_title}": `

`, - "{$form}": "", - "{$button}": "搜索", - "{$placeholder}": "搜索…", - "{$label}": "搜索:", - }) - return v -}() - var html5SearchForm = `