wp-go/app/theme/wp/components/block.go

43 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-03-27 04:59:29 +00:00
package components
import (
2023-11-13 14:53:20 +00:00
"github.com/fthvgb1/wp-go/app/pkg/logs"
2023-05-04 12:37:06 +00:00
"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"
2023-03-27 04:59:29 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"strings"
)
2023-03-28 13:45:20 +00:00
var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() string, error){
2023-03-27 04:59:29 +00:00
"core/categories": block.Category,
}
2023-04-30 13:17:33 +00:00
func Block(id string) (func(*wp.Handle) string, string) {
2023-03-27 04:59:29 +00:00
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
if content == "" {
2023-04-30 13:17:33 +00:00
return nil, ""
2023-03-27 04:59:29 +00:00
}
2023-04-30 13:17:33 +00:00
var name string
2023-03-27 04:59:29 +00:00
v := block.ParseBlock(content)
2023-04-30 13:17:33 +00:00
if len(v.Output) > 0 {
name = v.Output[0].Name
}
2023-03-27 04:59:29 +00:00
return func(h *wp.Handle) string {
var out []string
for _, parserBlock := range v.Output {
fn, ok := blockFn[parserBlock.Name]
if ok {
2023-03-28 13:45:20 +00:00
s, err := fn(h, id, parserBlock)
2023-03-27 04:59:29 +00:00
if err != nil {
2023-11-13 14:53:20 +00:00
logs.Error(err, str.Join("parse block", parserBlock.Name, " fail "), parserBlock)
2023-03-27 04:59:29 +00:00
continue
}
out = append(out, s())
2023-04-30 13:17:33 +00:00
2023-03-27 04:59:29 +00:00
}
}
return strings.Join(out, "\n")
2023-04-30 13:17:33 +00:00
}, name
2023-03-27 04:59:29 +00:00
}