2023-03-12 12:41:10 +00:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-03-18 13:02:24 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
2023-03-12 12:41:10 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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-03-18 13:02:24 +00:00
|
|
|
sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars)
|
|
|
|
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:45:20 +00:00
|
|
|
func sidebars(*wp.Handle) []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)
|
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], "-")
|
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
|
|
|
})
|
|
|
|
}
|