wp-go/app/theme/wp/components/block.go
2023-11-13 22:53:20 +08:00

43 lines
1.1 KiB
Go

package components
import (
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/theme/wp/components/block"
"github.com/fthvgb1/wp-go/app/wpconfig"
str "github.com/fthvgb1/wp-go/helper/strings"
"strings"
)
var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() string, error){
"core/categories": block.Category,
}
func Block(id string) (func(*wp.Handle) string, string) {
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
if content == "" {
return nil, ""
}
var name string
v := block.ParseBlock(content)
if len(v.Output) > 0 {
name = v.Output[0].Name
}
return func(h *wp.Handle) string {
var out []string
for _, parserBlock := range v.Output {
fn, ok := blockFn[parserBlock.Name]
if ok {
s, err := fn(h, id, parserBlock)
if err != nil {
logs.Error(err, str.Join("parse block", parserBlock.Name, " fail "), parserBlock)
continue
}
out = append(out, s())
}
}
return strings.Join(out, "\n")
}, name
}