From bebe6bac81f7fc11d3b82e543100fba0e7892a7b Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 27 Mar 2023 22:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../theme/twentyseventeen/twentyseventeen.go | 2 +- internal/theme/wp/components/block.go | 6 +- .../theme/wp/components/block/category.go | 68 +++++++++++-------- .../theme/wp/components/widgetareadata.go | 11 +-- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 2a054c9..1231bbf 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -61,7 +61,7 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { pushScripts(h) h.SetData("HeaderImage", getHeaderImage(h)) h.SetComponentsArgs(widgets.Widget, map[string]string{ - "{$before_widget}": `
`, + "{$before_widget}": `
`, "{$after_widget}": `
`, }) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) diff --git a/internal/theme/wp/components/block.go b/internal/theme/wp/components/block.go index 389e412..29409ed 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) (func() string, error){ +var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock, map[string]string) (func() string, error){ "core/categories": block.Category, } -func Block(id string) func(*wp.Handle) string { +func Block(id string, args map[string]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) func(*wp.Handle) string { for _, parserBlock := range v.Output { fn, ok := blockFn[parserBlock.Name] if ok { - s, err := fn(h, id, parserBlock) + s, err := fn(h, id, parserBlock, args) if err != nil { continue } diff --git a/internal/theme/wp/components/block/category.go b/internal/theme/wp/components/block/category.go index 9fb2287..aa75de5 100644 --- a/internal/theme/wp/components/block/category.go +++ b/internal/theme/wp/components/block/category.go @@ -2,6 +2,7 @@ package block import ( "encoding/json" + "errors" "fmt" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/number" @@ -9,7 +10,6 @@ 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/blocks" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/theme/wp" @@ -28,8 +28,8 @@ func categoryConf() map[any]any { func categoryDefaultArgs() map[string]string { return map[string]string{ - "{$before_widget}": `
`, - "{$after_widget}": `
`, + "{$before_widget}": ``, "{$name}": "cat", "{$class}": "postform", "{$selectId}": "cat", @@ -70,32 +70,43 @@ func parseAttr(attr map[any]any) string { return strings.Join(attrs, " ") } -func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, error) { +func Category(h *wp.Handle, id string, blockParser ParserBlock, args map[string]string) (func() string, error) { counter := number.Counters[int]() - var con any - err := json.Unmarshal([]byte(blockParser.Attrs), &con) + var err error + conf := reload.GetAnyValBys("block-category-conf", h, func(h *wp.Handle) map[any]any { + var con any + err = json.Unmarshal([]byte(blockParser.Attrs), &con) + if err != nil { + logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs) + return nil + } + var conf map[any]any + switch con.(type) { + case map[any]any: + conf = con.(map[any]any) + case map[string]any: + conf = maps.StrAnyToAnyAny(con.(map[string]any)) + } + conf = maps.FilterZeroMerge(categoryConf(), conf) + + if maps.GetAnyAnyValWithDefaults(conf, false, "showPostCounts") { + conf["count"] = int64(1) + } + + if maps.GetAnyAnyValWithDefaults(conf, false, "displayAsDropdown") { + conf["dropdown"] = int64(1) + } + if maps.GetAnyAnyValWithDefaults(conf, false, "showHierarchy") { + conf["hierarchical"] = int64(1) + } + return conf + }) + if err != nil { - logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs) return nil, err } - var conf map[any]any - switch con.(type) { - case map[any]any: - conf = con.(map[any]any) - case map[string]any: - conf = maps.StrAnyToAnyAny(con.(map[string]any)) - } - conf = maps.FilterZeroMerge(categoryConf(), conf) - - if maps.GetAnyAnyValWithDefaults(conf, false, "showPostCounts") { - conf["count"] = int64(1) - } - - if maps.GetAnyAnyValWithDefaults(conf, false, "displayAsDropdown") { - conf["dropdown"] = int64(1) - } - if maps.GetAnyAnyValWithDefaults(conf, false, "showHierarchy") { - conf["hierarchical"] = int64(1) + if conf == nil { + return nil, errors.New("解析block-category配置错误") } if maps.GetAnyAnyValWithDefaults(conf, false, "showEmpty") { @@ -104,7 +115,10 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") { h.C.Set("showOnlyTopLevel", true) } - args := maps.FilterZeroMerge(categoryDefaultArgs(), wp.GetComponentsArgs[map[string]string](h, blocks.Categories, nil)) + args = reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string { + return maps.FilterZeroMerge(categoryDefaultArgs(), args) + }) + return func() string { return category(h, id, counter, args, conf) }, nil @@ -126,7 +140,7 @@ func category(h *wp.Handle, id string, counter number.Counter[int], args map[str conf["className"] = strings.Join(classes, " ") out = categoryUl(h, categories, conf) } - before := fmt.Sprintf(args["{$before_widget}"], id, strings.Join(class, " ")) + before := fmt.Sprintf(args["{$before_widget}"], str.Join("block-", id), strings.Join(class, " ")) return str.Join(before, out, args["{$after_widget}"]) } diff --git a/internal/theme/wp/components/widgetareadata.go b/internal/theme/wp/components/widgetareadata.go index c666956..da88b1e 100644 --- a/internal/theme/wp/components/widgetareadata.go +++ b/internal/theme/wp/components/widgetareadata.go @@ -32,8 +32,6 @@ func sidebars(h *wp.Handle) []wp.Components[string] { beforeWidget, ok := args["{$before_widget}"] if !ok { beforeWidget = "" - } else { - delete(args, "{$before_widget}") } v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) { @@ -49,18 +47,23 @@ func sidebars(h *wp.Handle) []wp.Components[string] { wp.SetComponentsArgsForMap(h, name, "{$id}", id) } names := str.Join("widget-", name) - if beforeWidget != "" { + + 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) } if name == "block" { - fn := Block(id) + fn := Block(id, args) if fn == nil { return wp.Components[string]{}, false }