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

56 lines
1.5 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/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
)
2023-03-28 13:45:20 +00:00
var widgetFn = map[string]widgetComponent{
"search": {fn: widget.Search},
"recent-posts": {fn: widget.RecentPosts},
"recent-comments": {fn: widget.RecentComments},
"archives": {fn: widget.Archive},
"categories": {fn: widget.Category},
"meta": {fn: widget.Meta, cacheKey: "widgetMeta"},
}
type widgetComponent struct {
fn func(h *wp.Handle, id string) string
cacheKey string
2023-03-18 05:17:21 +00:00
}
2023-03-12 12:41:10 +00:00
func WidgetArea(h *wp.Handle) {
2023-04-19 07:50:22 +00:00
h.PushComponents(constraints.SidebarsWidgets, sidebars()...)
2023-03-18 13:02:24 +00:00
}
2023-04-19 07:50:22 +00:00
func sidebars() []wp.Components[string] {
2023-03-12 12:41:10 +00:00
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
2023-03-18 13:02:24 +00:00
return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) {
2023-03-12 12:41:10 +00:00
vv := t.(string)
ss := strings.Split(vv, "-")
id := ss[len(ss)-1]
name := strings.Join(ss[0:len(ss)-1], "-")
2023-03-28 13:45:20 +00:00
widgetComponents, ok := widgetFn[name]
2023-03-27 03:37:24 +00:00
if name != "block" && !ok {
2023-03-28 13:45:20 +00:00
return wp.Components[string]{}, false
2023-03-27 03:37:24 +00:00
}
2023-03-28 13:45:20 +00:00
var component wp.Components[string]
2023-03-27 03:37:24 +00:00
if name == "block" {
2023-03-28 13:45:20 +00:00
fn := Block(id)
2023-03-27 03:37:24 +00:00
if fn == nil {
2023-03-28 13:45:20 +00:00
return component, false
2023-03-27 03:37:24 +00:00
}
2023-03-28 13:45:20 +00:00
component.Fn = fn
} else {
component.Fn = widget.Fn(id, widgetComponents.fn)
component.CacheKey = widgetComponents.cacheKey
2023-03-12 12:41:10 +00:00
}
2023-03-28 13:45:20 +00:00
component.Order = 10
return component, true
2023-03-12 12:41:10 +00:00
})
}