diff --git a/helper/maps/map.go b/helper/maps/map.go
index 25dcd25..a73099b 100644
--- a/helper/maps/map.go
+++ b/helper/maps/map.go
@@ -110,11 +110,14 @@ func Copy[K comparable, V any](m map[K]V) map[K]V {
func Merge[K comparable, V any](m ...map[K]V) map[K]V {
if len(m) < 1 {
- panic("no map")
+ return nil
} else if len(m) < 2 {
return m[0]
}
mm := m[0]
+ if mm == nil {
+ mm = make(map[K]V)
+ }
for _, m2 := range m[1:] {
for k, v := range m2 {
mm[k] = v
@@ -123,6 +126,31 @@ func Merge[K comparable, V any](m ...map[K]V) map[K]V {
return mm
}
+func MergeBy[K comparable, V any](fn func(k K, v1, v2 V) (V, bool), m ...map[K]V) map[K]V {
+ if len(m) < 1 {
+ return nil
+ } else if len(m) < 2 {
+ return m[0]
+ }
+ mm := m[0]
+ if mm == nil {
+ mm = make(map[K]V)
+ }
+ for _, m2 := range m[1:] {
+ for k, v := range m2 {
+ vv, ok := mm[k]
+ if ok {
+ vvv, ok := fn(k, vv, v)
+ if ok {
+ v = vvv
+ }
+ }
+ mm[k] = v
+ }
+ }
+ return mm
+}
+
func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V {
if len(m) < 1 {
panic("no map")
@@ -130,6 +158,9 @@ func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V {
return m[0]
}
mm := m[0]
+ if mm == nil {
+ mm = make(map[K]V)
+ }
for _, m2 := range m[1:] {
for k, v := range m2 {
if helper.IsZeros(v) {
diff --git a/internal/plugins/wphandle/enlightjs/enlighterjs.go b/internal/plugins/wphandle/enlightjs/enlighterjs.go
index 435732d..5781de7 100644
--- a/internal/plugins/wphandle/enlightjs/enlighterjs.go
+++ b/internal/plugins/wphandle/enlightjs/enlighterjs.go
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/internal/phphelper"
+ "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
@@ -37,9 +38,9 @@ type Selectors struct {
}
func EnlighterJS(h *wp.Handle) {
- h.PushGroupHeadScript("enlighterjs-css", 20, ``)
+ h.PushGroupHeadScript(constraints.AllScene, "enlighterjs-css", 20, ``)
- h.PushCacheGroupFooterScript("enlighterJs", 10, func(h *wp.Handle) string {
+ h.PushCacheGroupFooterScript(constraints.AllScene, "enlighterJs", 10, func(h *wp.Handle) string {
op := wpconfig.GetOption("enlighter-options")
opp, err := phphelper.UnPHPSerializeToStrAnyMap(op)
if err != nil {
diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go
index 4373f53..e096f0f 100644
--- a/internal/theme/twentyfifteen/twentyfifteen.go
+++ b/internal/theme/twentyfifteen/twentyfifteen.go
@@ -63,7 +63,8 @@ func configs(h *wp.Handle) {
})
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
wphandle.UsePlugins(h)
- h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
+ h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
+ h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
h.CommonComponents()
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
components.WidgetArea(h)
diff --git a/internal/theme/twentyseventeen/script.go b/internal/theme/twentyseventeen/script.go
index ce3ce79..175bc41 100644
--- a/internal/theme/twentyseventeen/script.go
+++ b/internal/theme/twentyseventeen/script.go
@@ -2,19 +2,20 @@ package twentyseventeen
import (
"fmt"
+ "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func pushScripts(h *wp.Handle) {
- h.PushCacheGroupHeadScript("{theme}.head", 30, func(h *wp.Handle) string {
+ h.PushCacheGroupHeadScript(constraints.AllScene, "{theme}.head", 30, func(h *wp.Handle) string {
head := headScript
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
head = fmt.Sprintf("%s\n%s", headScript, ` `)
}
return head
})
- h.PushGroupFooterScript("{theme}.footer", 20, footerScript)
+ h.PushGroupFooterScript(constraints.AllScene, "{theme}.footer", 20, footerScript)
}
diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go
index 0292123..e5e32bf 100644
--- a/internal/theme/twentyseventeen/twentyseventeen.go
+++ b/internal/theme/twentyseventeen/twentyseventeen.go
@@ -55,7 +55,7 @@ func configs(h *wp.Handle) {
conf := config.GetConfig()
wphandle.UsePlugins(h)
h.PushComponentFilterFn("bodyClass", calClass)
- h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
+ h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h)
pushScripts(h)
h.PushRender(constraints.AllStats, wp.NewHandleFn(headerImage, 10, "headerImage"))
diff --git a/internal/theme/wp/components.go b/internal/theme/wp/components.go
index 3cf9f0b..afad26c 100644
--- a/internal/theme/wp/components.go
+++ b/internal/theme/wp/components.go
@@ -27,7 +27,13 @@ func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Compo
}
func CalComponents(h *Handle) {
- for k, components := range h.components {
+ componentss := reload.GetAnyValMapBy("scene-components", str.Join("allScene-", h.scene), h, func(h *Handle) map[string][]Components[string] {
+ return maps.MergeBy(func(k string, v1, v2 []Components[string]) ([]Components[string], bool) {
+ vv := append(v1, v2...)
+ return vv, vv != nil
+ }, nil, h.components[h.scene], h.components[constraints.AllScene])
+ })
+ for k, components := range componentss {
key := str.Join("calComponents-", k)
key = h.ComponentFilterFnHook("calComponents", key, k)
ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] {
@@ -72,58 +78,63 @@ func CalComponents(h *Handle) {
}
}
-func (h *Handle) PushComponents(name string, components ...Components[string]) {
- h.components[name] = append(h.components[name], components...)
+func (h *Handle) PushComponents(scene, componentType string, components ...Components[string]) {
+ c, ok := h.components[scene]
+ if !ok {
+ c = make(map[string][]Components[string])
+ h.components[scene] = c
+ }
+ c[componentType] = append(c[componentType], components...)
}
-func (h *Handle) PushGroupComponentStr(componentType, name string, order int, strs ...string) {
- var calls []Components[string]
+func (h *Handle) PushGroupComponentStr(scene, componentType, name string, order int, strs ...string) {
+ var components []Components[string]
for _, val := range strs {
- calls = append(calls, Components[string]{
+ components = append(components, Components[string]{
Val: val,
Order: order,
Name: name,
})
}
- h.components[componentType] = append(h.components[componentType], calls...)
+ h.PushComponents(scene, componentType, components...)
}
-func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
- h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
+func (h *Handle) PushCacheGroupHeadScript(scene, name string, order int, fns ...func(*Handle) string) {
+ h.PushGroupCacheComponentFn(scene, constraints.HeadScript, name, order, fns...)
}
-func (h *Handle) PushFooterScript(components ...Components[string]) {
- h.PushComponents(constraints.FooterScript, components...)
+func (h *Handle) PushFooterScript(scene string, components ...Components[string]) {
+ h.PushComponents(scene, constraints.FooterScript, components...)
}
-func (h *Handle) PushGroupFooterScript(name string, order int, strs ...string) {
- h.PushGroupComponentStr(constraints.FooterScript, name, order, strs...)
+func (h *Handle) PushGroupFooterScript(scene, name string, order int, strs ...string) {
+ h.PushGroupComponentStr(scene, constraints.FooterScript, name, order, strs...)
}
-func (h *Handle) PushCacheGroupFooterScript(name string, order int, fns ...func(*Handle) string) {
- h.PushGroupCacheComponentFn(constraints.FooterScript, name, order, fns...)
+func (h *Handle) PushCacheGroupFooterScript(scene, name string, order int, fns ...func(*Handle) string) {
+ h.PushGroupCacheComponentFn(scene, constraints.FooterScript, name, order, fns...)
}
-func (h *Handle) PushGroupCacheComponentFn(componentType, name string, order int, fns ...func(*Handle) string) {
- h.PushComponents(componentType, h.NewComponent(name, true, order, func(h *Handle) string {
+func (h *Handle) PushGroupCacheComponentFn(scene, componentType, name string, order int, fns ...func(*Handle) string) {
+ h.PushComponents(scene, componentType, NewComponent(name, "", true, order, func(h *Handle) string {
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
return t(h)
}), "\n")
}))
}
-func (h *Handle) NewComponent(name string, cached bool, order int, fn func(handle *Handle) string) Components[string] {
- return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order}
+func NewComponent(name, val string, cached bool, order int, fn func(handle *Handle) string) Components[string] {
+ return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order, Val: val}
}
-func (h *Handle) AddCacheComponent(componentType, name string, order int, fn func(*Handle) string) {
- h.components[componentType] = append(h.components[componentType], h.NewComponent(name, true, order, fn))
+func (h *Handle) AddCacheComponent(scene, componentType, name string, order int, fn func(*Handle) string) {
+ h.PushComponents(scene, componentType, NewComponent(name, "", true, order, fn))
}
-func (h *Handle) PushHeadScript(components ...Components[string]) {
- h.PushComponents(constraints.HeadScript, components...)
+func (h *Handle) PushHeadScript(scene string, components ...Components[string]) {
+ h.PushComponents(scene, constraints.HeadScript, components...)
}
-func (h *Handle) PushGroupHeadScript(name string, order int, str ...string) {
- h.PushGroupComponentStr(constraints.HeadScript, name, order, str...)
+func (h *Handle) PushGroupHeadScript(scene, name string, order int, str ...string) {
+ h.PushGroupComponentStr(scene, constraints.HeadScript, name, order, str...)
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
diff --git a/internal/theme/wp/components/block.go b/internal/theme/wp/components/block.go
index 389e412..675b6f9 100644
--- a/internal/theme/wp/components/block.go
+++ b/internal/theme/wp/components/block.go
@@ -12,12 +12,16 @@ var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() str
"core/categories": block.Category,
}
-func Block(id string) func(*wp.Handle) string {
+func Block(id string) (func(*wp.Handle) string, string) {
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
if content == "" {
- return nil
+ return nil, ""
}
+ var name string
v := block.ParseBlock(content)
+ if len(v.Output) > 0 {
+ name = v.Output[0].Name
+ }
return func(h *wp.Handle) string {
var out []string
for _, parserBlock := range v.Output {
@@ -28,8 +32,9 @@ func Block(id string) func(*wp.Handle) string {
continue
}
out = append(out, s())
+
}
}
return strings.Join(out, "\n")
- }
+ }, name
}
diff --git a/internal/theme/wp/components/widgetareadata.go b/internal/theme/wp/components/widgetareadata.go
index 9ec97fa..3aeff74 100644
--- a/internal/theme/wp/components/widgetareadata.go
+++ b/internal/theme/wp/components/widgetareadata.go
@@ -2,6 +2,7 @@ package components
import (
"github.com/fthvgb1/wp-go/helper/slice"
+ str "github.com/fthvgb1/wp-go/helper/strings"
"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"
@@ -10,12 +11,12 @@ import (
)
var widgetFn = map[string]widgetComponent{
- "search": {fn: widget.Search, name: "search"},
- "recent-posts": {fn: widget.RecentPosts, name: "recent-posts"},
- "recent-comments": {fn: widget.RecentComments, name: "recent-comments"},
- "archives": {fn: widget.Archive, name: "archives"},
- "categories": {fn: widget.Category, name: "categories"},
- "meta": {fn: widget.Meta, name: "meta", cached: true},
+ "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},
}
type widgetComponent struct {
@@ -25,7 +26,7 @@ type widgetComponent struct {
}
func WidgetArea(h *wp.Handle) {
- h.PushComponents(constraints.SidebarsWidgets, sidebars()...)
+ h.PushComponents(constraints.AllScene, constraints.SidebarsWidgets, sidebars()...)
}
func sidebars() []wp.Components[string] {
@@ -41,11 +42,12 @@ func sidebars() []wp.Components[string] {
}
var component wp.Components[string]
if name == "block" {
- fn := Block(id)
+ fn, fnName := Block(id)
if fn == nil {
return component, false
}
component.Fn = fn
+ component.Name = str.Join("block.", fnName)
} else {
component.Fn = widget.Fn(id, widgetComponents.fn)
component.Name = widgetComponents.name
diff --git a/internal/theme/wp/detail.go b/internal/theme/wp/detail.go
index 3cd1512..d8807c4 100644
--- a/internal/theme/wp/detail.go
+++ b/internal/theme/wp/detail.go
@@ -114,7 +114,7 @@ func Details(h *Handle) {
}
func ReplyCommentJs(h *Handle) {
- h.PushFooterScript(h.NewComponent("comment-reply.js", false, 10, func(h *Handle) string {
+ h.PushFooterScript(constraints.Detail, NewComponent("comment-reply.js", "", false, 10, func(h *Handle) string {
reply := ""
if h.Detail.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
reply = ``
diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go
index d3086d5..ac5fa23 100644
--- a/internal/theme/wp/wp.go
+++ b/internal/theme/wp/wp.go
@@ -25,7 +25,7 @@ type Handle struct {
Code int
Stats string
templ string
- components map[string][]Components[string]
+ components map[string]map[string][]Components[string]
componentHook map[string][]func(Components[string]) (Components[string], bool)
themeMods wpconfig.ThemeMods
handlers map[string]map[string][]HandleCall
@@ -38,7 +38,7 @@ type Handle struct {
template *template.Template
}
-func (h *Handle) Components() map[string][]Components[string] {
+func (h *Handle) Components() map[string]map[string][]Components[string] {
return h.components
}
@@ -84,7 +84,7 @@ type HandleCall struct {
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
var inited = false
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
- h.components = make(map[string][]Components[string])
+ h.components = make(map[string]map[string][]Components[string])
h.componentsArgs = make(map[string]any)
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
h.handlers = make(map[string]map[string][]HandleCall)
@@ -193,7 +193,7 @@ func PreCodeAndStats(h *Handle) {
}
func (h *Handle) CommonComponents() {
- h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
+ h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10, "wp.CalComponents"))
h.PushRender(constraints.AllStats, NewHandleFn(RenderTemplate, 0, "wp.RenderTemplate"))
}