From e10dc4e45e0951f5b170dae6615c4cddf3e394b0 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 28 Mar 2023 21:45:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96widget=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/wp/components/block.go | 6 +- .../theme/wp/components/block/category.go | 6 +- .../theme/wp/components/widget/archive.go | 27 ++++---- internal/theme/wp/components/widget/args.go | 4 +- .../theme/wp/components/widget/category.go | 26 ++++---- internal/theme/wp/components/widget/fn.go | 9 +++ internal/theme/wp/components/widget/meta.go | 41 +++++------- .../wp/components/widget/recentcomments.go | 34 ++++------ .../theme/wp/components/widget/recentposts.go | 34 +++++----- internal/theme/wp/components/widget/search.go | 60 ++++++++--------- .../theme/wp/components/widgetareadata.go | 66 +++++++------------ 11 files changed, 139 insertions(+), 174 deletions(-) create mode 100644 internal/theme/wp/components/widget/fn.go diff --git a/internal/theme/wp/components/block.go b/internal/theme/wp/components/block.go index 29409ed..389e412 100644 --- a/internal/theme/wp/components/block.go +++ b/internal/theme/wp/components/block.go @@ -8,11 +8,11 @@ import ( "strings" ) -var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock, map[string]string) (func() string, error){ +var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() string, error){ "core/categories": block.Category, } -func Block(id string, args map[string]string) func(*wp.Handle) string { +func Block(id string) func(*wp.Handle) string { content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content") if content == "" { return nil @@ -23,7 +23,7 @@ func Block(id string, args map[string]string) func(*wp.Handle) string { for _, parserBlock := range v.Output { fn, ok := blockFn[parserBlock.Name] if ok { - s, err := fn(h, id, parserBlock, args) + s, err := fn(h, id, parserBlock) if err != nil { continue } diff --git a/internal/theme/wp/components/block/category.go b/internal/theme/wp/components/block/category.go index aa75de5..5c88434 100644 --- a/internal/theme/wp/components/block/category.go +++ b/internal/theme/wp/components/block/category.go @@ -10,6 +10,7 @@ import ( "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" constraints2 "github.com/fthvgb1/wp-go/internal/pkg/constraints" + "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/theme/wp" @@ -70,7 +71,7 @@ func parseAttr(attr map[any]any) string { return strings.Join(attrs, " ") } -func Category(h *wp.Handle, id string, blockParser ParserBlock, args map[string]string) (func() string, error) { +func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, error) { counter := number.Counters[int]() var err error conf := reload.GetAnyValBys("block-category-conf", h, func(h *wp.Handle) map[any]any { @@ -115,7 +116,8 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock, args map[string] if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") { h.C.Set("showOnlyTopLevel", true) } - args = reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string { + args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) return maps.FilterZeroMerge(categoryDefaultArgs(), args) }) diff --git a/internal/theme/wp/components/widget/archive.go b/internal/theme/wp/components/widget/archive.go index 52a5a2d..dda8515 100644 --- a/internal/theme/wp/components/widget/archive.go +++ b/internal/theme/wp/components/widget/archive.go @@ -24,10 +24,6 @@ var archiveTemplate = `{$before_widget} func archiveArgs() map[string]string { return map[string]string{ - "{$before_widget}": `", - "{$before_title}": `

`, - "{$after_title}": "

", "{$before_sidebar}": "", "{$after_sidebar}": "", "{$nav}": "", @@ -47,24 +43,23 @@ func archivesConfig() map[any]any { } } -func Archive(h *wp.Handle) string { - args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string { - archiveArgs := archiveArgs() - args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs) - args = maps.FilterZeroMerge(archiveArgs, args) - return args - }) - +func Archive(h *wp.Handle, id string) string { conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any { archivesConfig := archivesConfig() conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2)) return 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}"]) - } + args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string { + archiveArgs := archiveArgs() + commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs()) + args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs) + args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args) + args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive")) + args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) + return args + }) + s := archiveTemplate if int64(1) == conf["dropdown"].(int64) { s = strings.ReplaceAll(s, "{$html}", archiveDropDown(h, conf, args, cache.Archives(h.C))) diff --git a/internal/theme/wp/components/widget/args.go b/internal/theme/wp/components/widget/args.go index fb4e516..3ead77e 100644 --- a/internal/theme/wp/components/widget/args.go +++ b/internal/theme/wp/components/widget/args.go @@ -1,8 +1,8 @@ package widget -func commonArgs() map[string]string { +func CommonArgs() map[string]string { return map[string]string{ - "{$before_widget}": ``, - "{$aria_label}": "", - "{$title}": "", - "{$before_title}": `

`, - "{$after_title}": `

`, - "{$form}": "", - "{$button}": "搜索", - "{$placeholder}": "搜索…", - "{$label}": "搜索:", + "{$aria_label}": "", + "{$title}": "", + "{$form}": "", + "{$button}": "搜索", + "{$placeholder}": "搜索…", + "{$label}": "搜索:", } } -func Search(h *wp.Handle) string { +func Search(h *wp.Handle, id string) string { + form := html5SearchForm args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string { search := searchArgs() + commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.Search, search) - args = maps.FilterZeroMerge(search, args) + args = maps.FilterZeroMerge(search, CommonArgs(), commonArgs, args) + args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("search-", id), str.Join("widget widget_", "search")) + if args["{$title}"] == "" { + args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title") + } + + if args["{$title}"] != "" { + args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"]) + } + if args["{$form}"] != "" { + form = args["{$form}"] + delete(args, "{$form}") + } + if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { + form = xmlSearchForm + } + return args }) - if args["{$title}"] == "" { - args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title") - } - if id, ok := args["{$id}"]; ok && id != "" { - args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"]) - } - if args["{$title}"] != "" { - args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"]) - } + s := strings.ReplaceAll(searchTemplate, "{$form}", form) args["{$value}"] = "" if h.Scene() == constraints.Search { args["{$value}"] = html.SpecialChars(h.Index.Param.Search) } - form := html5SearchForm - if args["{$form}"] != "" { - form = args["{$form}"] - delete(args, "{$form}") - } - - if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { - form = xmlSearchForm - } - s := strings.ReplaceAll(searchTemplate, "{$form}", form) return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args)) } diff --git a/internal/theme/wp/components/widgetareadata.go b/internal/theme/wp/components/widgetareadata.go index da88b1e..ea4ca85 100644 --- a/internal/theme/wp/components/widgetareadata.go +++ b/internal/theme/wp/components/widgetareadata.go @@ -1,25 +1,27 @@ package components import ( - "fmt" "github.com/fthvgb1/wp-go/helper/slice" - str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/constraints" - "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/components/widget" "github.com/fthvgb1/wp-go/internal/wpconfig" "strings" ) -var widgetFn = map[string]wp.Components[string]{ - "search": {Fn: widget.Search}, - "recent-posts": {Fn: widget.RecentPosts}, - "recent-comments": {Fn: widget.RecentComments}, - "archives": {Fn: widget.Archive}, - "categories": {Fn: widget.Category}, - "meta": {Fn: widget.Meta, CacheKey: "widgetMeta"}, +var widgetFn = map[string]widgetComponent{ + "search": {fn: widget.Search}, + "recent-posts": {fn: widget.RecentPosts}, + "recent-comments": {fn: widget.RecentComments}, + "archives": {fn: widget.Archive}, + "categories": {fn: widget.Category}, + "meta": {fn: widget.Meta, cacheKey: "widgetMeta"}, +} + +type widgetComponent struct { + fn func(h *wp.Handle, id string) string + cacheKey string } func WidgetArea(h *wp.Handle) { @@ -27,49 +29,29 @@ func WidgetArea(h *wp.Handle) { h.PushComponents(constraints.SidebarsWidgets, sidebar...) } -func sidebars(h *wp.Handle) []wp.Components[string] { - args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) - beforeWidget, ok := args["{$before_widget}"] - if !ok { - beforeWidget = "" - } +func sidebars(*wp.Handle) []wp.Components[string] { v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) { vv := t.(string) ss := strings.Split(vv, "-") id := ss[len(ss)-1] name := strings.Join(ss[0:len(ss)-1], "-") - components, ok := widgetFn[name] + widgetComponents, ok := widgetFn[name] if name != "block" && !ok { - return components, false - } - if id != "2" { - wp.SetComponentsArgsForMap(h, name, "{$id}", id) - } - names := str.Join("widget-", name) - - if beforeWidget != "" && name != "block" { - n := strings.ReplaceAll(name, "-", "_") - if name == "recent-posts" { - n = "recent_entries" - } - n = str.Join("widget widget_", n) - delete(args, "{$before_widget}") - wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n)) - } else { - args["{$before_widget}"] = beforeWidget - } - for k, val := range args { - wp.SetComponentsArgsForMap(h, names, k, val) + return wp.Components[string]{}, false } + var component wp.Components[string] if name == "block" { - fn := Block(id, args) + fn := Block(id) if fn == nil { - return wp.Components[string]{}, false + return component, false } - components = wp.Components[string]{Fn: fn, Order: 10} + component.Fn = fn + } else { + component.Fn = widget.Fn(id, widgetComponents.fn) + component.CacheKey = widgetComponents.cacheKey } - components.Order = 10 - return components, true + component.Order = 10 + return component, true }) }