完善
This commit is contained in:
parent
a1f36da904
commit
bebe6bac81
|
@ -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}": `<section id="%s" class="widget widget_%s">`,
|
||||
"{$before_widget}": `<section id="%s" class="%s">`,
|
||||
"{$after_widget}": `</section>`,
|
||||
})
|
||||
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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}": `<section id="block-%v" class="%s">`,
|
||||
"{$after_widget}": `</section>`,
|
||||
"{$before_widget}": `<aside id="%s" class="%s">`,
|
||||
"{$after_widget}": `</aside>`,
|
||||
"{$name}": "cat",
|
||||
"{$class}": "postform",
|
||||
"{$selectId}": "cat",
|
||||
|
@ -70,13 +70,15 @@ 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 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)
|
||||
err = json.Unmarshal([]byte(blockParser.Attrs), &con)
|
||||
if err != nil {
|
||||
logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs)
|
||||
return nil, err
|
||||
return nil
|
||||
}
|
||||
var conf map[any]any
|
||||
switch con.(type) {
|
||||
|
@ -97,6 +99,15 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string,
|
|||
if maps.GetAnyAnyValWithDefaults(conf, false, "showHierarchy") {
|
||||
conf["hierarchical"] = int64(1)
|
||||
}
|
||||
return conf
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if conf == nil {
|
||||
return nil, errors.New("解析block-category配置错误")
|
||||
}
|
||||
|
||||
if maps.GetAnyAnyValWithDefaults(conf, false, "showEmpty") {
|
||||
h.C.Set("showEmpty", true)
|
||||
|
@ -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}"])
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user