This commit is contained in:
xing 2023-03-27 22:03:01 +08:00
parent a1f36da904
commit bebe6bac81
4 changed files with 52 additions and 35 deletions

View File

@ -61,7 +61,7 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
pushScripts(h) pushScripts(h)
h.SetData("HeaderImage", getHeaderImage(h)) h.SetData("HeaderImage", getHeaderImage(h))
h.SetComponentsArgs(widgets.Widget, map[string]string{ h.SetComponentsArgs(widgets.Widget, map[string]string{
"{$before_widget}": `<section id="%s" class="widget widget_%s">`, "{$before_widget}": `<section id="%s" class="%s">`,
"{$after_widget}": `</section>`, "{$after_widget}": `</section>`,
}) })
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)

View File

@ -8,11 +8,11 @@ import (
"strings" "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, "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") content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
if content == "" { if content == "" {
return nil return nil
@ -23,7 +23,7 @@ func Block(id string) func(*wp.Handle) string {
for _, parserBlock := range v.Output { for _, parserBlock := range v.Output {
fn, ok := blockFn[parserBlock.Name] fn, ok := blockFn[parserBlock.Name]
if ok { if ok {
s, err := fn(h, id, parserBlock) s, err := fn(h, id, parserBlock, args)
if err != nil { if err != nil {
continue continue
} }

View File

@ -2,6 +2,7 @@ package block
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/number" "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/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
constraints2 "github.com/fthvgb1/wp-go/internal/pkg/constraints" 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/logs"
"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"
@ -28,8 +28,8 @@ func categoryConf() map[any]any {
func categoryDefaultArgs() map[string]string { func categoryDefaultArgs() map[string]string {
return map[string]string{ return map[string]string{
"{$before_widget}": `<section id="block-%v" class="%s">`, "{$before_widget}": `<aside id="%s" class="%s">`,
"{$after_widget}": `</section>`, "{$after_widget}": `</aside>`,
"{$name}": "cat", "{$name}": "cat",
"{$class}": "postform", "{$class}": "postform",
"{$selectId}": "cat", "{$selectId}": "cat",
@ -70,32 +70,43 @@ func parseAttr(attr map[any]any) string {
return strings.Join(attrs, " ") 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]() counter := number.Counters[int]()
var con any var err error
err := json.Unmarshal([]byte(blockParser.Attrs), &con) 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 { if err != nil {
logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs)
return nil, err return nil, err
} }
var conf map[any]any if conf == nil {
switch con.(type) { return nil, errors.New("解析block-category配置错误")
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 maps.GetAnyAnyValWithDefaults(conf, false, "showEmpty") { 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") { if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") {
h.C.Set("showOnlyTopLevel", true) 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 func() string {
return category(h, id, counter, args, conf) return category(h, id, counter, args, conf)
}, nil }, nil
@ -126,7 +140,7 @@ func category(h *wp.Handle, id string, counter number.Counter[int], args map[str
conf["className"] = strings.Join(classes, " ") conf["className"] = strings.Join(classes, " ")
out = categoryUl(h, categories, conf) 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}"]) return str.Join(before, out, args["{$after_widget}"])
} }

View File

@ -32,8 +32,6 @@ func sidebars(h *wp.Handle) []wp.Components[string] {
beforeWidget, ok := args["{$before_widget}"] beforeWidget, ok := args["{$before_widget}"]
if !ok { if !ok {
beforeWidget = "" beforeWidget = ""
} else {
delete(args, "{$before_widget}")
} }
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) { 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) wp.SetComponentsArgsForMap(h, name, "{$id}", id)
} }
names := str.Join("widget-", name) names := str.Join("widget-", name)
if beforeWidget != "" {
if beforeWidget != "" && name != "block" {
n := strings.ReplaceAll(name, "-", "_") n := strings.ReplaceAll(name, "-", "_")
if name == "recent-posts" { if name == "recent-posts" {
n = "recent_entries" n = "recent_entries"
} }
n = str.Join("widget widget_", n)
delete(args, "{$before_widget}")
wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n)) wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
} else {
args["{$before_widget}"] = beforeWidget
} }
for k, val := range args { for k, val := range args {
wp.SetComponentsArgsForMap(h, names, k, val) wp.SetComponentsArgsForMap(h, names, k, val)
} }
if name == "block" { if name == "block" {
fn := Block(id) fn := Block(id, args)
if fn == nil { if fn == nil {
return wp.Components[string]{}, false return wp.Components[string]{}, false
} }