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"
|
2023-03-16 16:45:04 +00:00
|
|
|
"strings"
|
2023-03-12 12:41:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var widgets = map[string]func(*wp.Handle) string{
|
2023-03-16 16:45:04 +00:00
|
|
|
"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)
|
2023-03-16 16:45:04 +00:00
|
|
|
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 {
|
2023-03-16 16:45:04 +00:00
|
|
|
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))
|
|
|
|
}
|