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"
|
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/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"
|
2023-03-16 16:45:04 +00:00
|
|
|
"strings"
|
2023-03-12 12:41:10 +00:00
|
|
|
)
|
|
|
|
|
2023-03-18 13:02:24 +00:00
|
|
|
var widgetFn = map[string]wp.Components[string]{
|
2023-03-19 12:53:27 +00:00
|
|
|
"search": {Fn: widget.Search},
|
2023-03-18 05:17:21 +00:00
|
|
|
"recent-posts": {Fn: widget.RecentPosts},
|
|
|
|
"recent-comments": {Fn: widget.RecentComments},
|
|
|
|
"archives": {Fn: widget.Archive},
|
|
|
|
"categories": {Fn: widget.Category},
|
|
|
|
"meta": {Fn: widget.Meta, CacheKey: "widgetMeta"},
|
|
|
|
}
|
|
|
|
|
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...)
|
|
|
|
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sidebars(h *wp.Handle) []wp.Components[string] {
|
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")
|
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-18 05:17:21 +00:00
|
|
|
components, ok := widgetFn[name]
|
2023-03-18 14:26:01 +00:00
|
|
|
if !ok {
|
|
|
|
return components, false
|
|
|
|
}
|
|
|
|
if id != "2" {
|
|
|
|
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
|
|
|
}
|
|
|
|
if beforeWidget != "" {
|
|
|
|
n := strings.ReplaceAll(name, "-", "_")
|
|
|
|
if name == "recent-posts" {
|
|
|
|
n = "recent_entries"
|
2023-03-17 10:26:08 +00:00
|
|
|
}
|
2023-03-18 14:26:01 +00:00
|
|
|
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
|
|
|
}
|
|
|
|
for k, val := range args {
|
|
|
|
wp.SetComponentsArgsForMap(h, name, k, val)
|
2023-03-12 12:41:10 +00:00
|
|
|
}
|
2023-03-18 14:26:01 +00:00
|
|
|
components.Order = 10
|
|
|
|
return components, true
|
2023-03-12 12:41:10 +00:00
|
|
|
})
|
|
|
|
}
|