2023-03-27 04:59:29 +00:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/wp/components/block"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
|
|
"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
|
|
|
}
|