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-27 03:37:24 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
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"
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = ""
|
|
|
|
}
|
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-27 03:37:24 +00:00
|
|
|
if name != "block" && !ok {
|
2023-03-18 14:26:01 +00:00
|
|
|
return components, false
|
|
|
|
}
|
|
|
|
if id != "2" {
|
|
|
|
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
|
|
|
}
|
2023-03-27 03:37:24 +00:00
|
|
|
names := str.Join("widget-", name)
|
2023-03-27 14:03:01 +00:00
|
|
|
|
|
|
|
if beforeWidget != "" && name != "block" {
|
2023-03-18 14:26:01 +00:00
|
|
|
n := strings.ReplaceAll(name, "-", "_")
|
|
|
|
if name == "recent-posts" {
|
|
|
|
n = "recent_entries"
|
2023-03-17 10:26:08 +00:00
|
|
|
}
|
2023-03-27 14:03:01 +00:00
|
|
|
n = str.Join("widget widget_", n)
|
|
|
|
delete(args, "{$before_widget}")
|
2023-03-27 03:37:24 +00:00
|
|
|
wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
2023-03-27 14:03:01 +00:00
|
|
|
} else {
|
|
|
|
args["{$before_widget}"] = beforeWidget
|
2023-03-18 14:26:01 +00:00
|
|
|
}
|
|
|
|
for k, val := range args {
|
2023-03-27 03:37:24 +00:00
|
|
|
wp.SetComponentsArgsForMap(h, names, k, val)
|
|
|
|
}
|
|
|
|
if name == "block" {
|
2023-03-27 14:03:01 +00:00
|
|
|
fn := Block(id, args)
|
2023-03-27 03:37:24 +00:00
|
|
|
if fn == nil {
|
|
|
|
return wp.Components[string]{}, false
|
|
|
|
}
|
|
|
|
components = wp.Components[string]{Fn: fn, Order: 10}
|
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
|
|
|
})
|
|
|
|
}
|