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

62 lines
1.8 KiB
Go
Raw Normal View History

2023-03-12 12:41:10 +00:00
package components
import (
2023-03-17 10:26:08 +00:00
"fmt"
2023-03-12 12:41:10 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
2023-03-17 10:26:08 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
2023-03-12 12:41:10 +00:00
"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-17 10:26:08 +00:00
var widgetFn = map[string]func(*wp.Handle) string{
2023-03-17 11:51:53 +00:00
"search": widget.Search,
"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) {
2023-03-17 10:26:08 +00:00
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
beforeWidget, ok := args["{$before_widget}"]
if !ok {
beforeWidget = ""
} else {
delete(args, "{$before_widget}")
}
2023-03-12 12:41:10 +00:00
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], "-")
2023-03-17 10:26:08 +00:00
fn, ok := widgetFn[name]
2023-03-12 12:41:10 +00:00
if ok {
if id != "2" {
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
}
2023-03-17 10:26:08 +00:00
if beforeWidget != "" {
n := strings.ReplaceAll(name, "-", "_")
if name == "recent-posts" {
n = "recent_entries"
}
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
}
if len(args) > 0 {
for k, val := range args {
wp.SetComponentsArgsForMap(h, name, k, val)
}
}
2023-03-12 12:41:10 +00:00
return fn, true
}
return nil, false
})
2023-03-17 10:26:08 +00:00
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
2023-03-12 12:41:10 +00:00
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
}