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

41 lines
962 B
Go
Raw Normal View History

2023-03-27 04:59:29 +00:00
package components
import (
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 {
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
}