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

60 lines
1.8 KiB
Go
Raw Normal View History

2023-03-12 12:41:10 +00:00
package components
import (
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/theme/wp/components/widget"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-03-12 12:41:10 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
2023-04-30 13:17:33 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"strings"
2023-03-12 12:41:10 +00:00
)
2023-03-28 13:45:20 +00:00
var widgetFn = map[string]widgetComponent{
2023-04-30 13:17:33 +00:00
"search": {fn: widget.Search, name: "widget.search"},
"recent-posts": {fn: widget.RecentPosts, name: "widget.recent-posts"},
"recent-comments": {fn: widget.RecentComments, name: "widget.recent-comments"},
"archives": {fn: widget.Archive, name: "widget.archives"},
"categories": {fn: widget.Category, name: "widget.categories"},
"meta": {fn: widget.Meta, name: "widget.meta", cached: true},
2023-03-28 13:45:20 +00:00
}
type widgetComponent struct {
2023-04-29 14:05:24 +00:00
fn func(h *wp.Handle, id string) string
cached bool
name string
2023-03-18 05:17:21 +00:00
}
2023-03-12 12:41:10 +00:00
func WidgetArea(h *wp.Handle) {
2023-04-30 13:17:33 +00:00
h.PushComponents(constraints.AllScene, 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-04-30 13:17:33 +00:00
fn, fnName := 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
2023-04-30 13:17:33 +00:00
component.Name = str.Join("block.", fnName)
2023-03-28 13:45:20 +00:00
} else {
component.Fn = widget.Fn(id, widgetComponents.fn)
2023-04-29 14:05:24 +00:00
component.Name = widgetComponents.name
component.Cached = widgetComponents.cached
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
})
}