wp-go/internal/theme/wp/components/widgetareadata.go

43 lines
1.3 KiB
Go
Raw Normal View History

2023-03-12 12:41:10 +00:00
package components
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"strings"
2023-03-12 12:41:10 +00:00
)
var widgets = map[string]func(*wp.Handle) string{
"search": widget.SearchForm,
"recent-posts": widget.RecentPosts,
"recent-comments": widget.RecentComments,
"archives": widget.Archive,
"categories": widget.Category,
"meta": widget.Meta,
2023-03-12 12:41:10 +00:00
}
func WidgetArea(h *wp.Handle) {
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) {
vv := t.(string)
ss := strings.Split(vv, "-")
id := ss[len(ss)-1]
name := strings.Join(ss[0:len(ss)-1], "-")
fn, ok := widgets[name]
2023-03-12 12:41:10 +00:00
if ok {
if id != "2" {
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
}
2023-03-12 12:41:10 +00:00
return fn, true
}
return nil, false
})
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(func(h *wp.Handle) {
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
}, 30))
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
}