重写body class机制

This commit is contained in:
xing 2023-04-15 13:22:21 +08:00
parent 125764711d
commit b53819f733
3 changed files with 8 additions and 12 deletions

View File

@ -51,7 +51,7 @@ func Hook(h *wp.Handle) {
func configs(h *wp.Handle) { func configs(h *wp.Handle) {
conf := config.GetConfig() conf := config.GetConfig()
wphandle.RegisterPlugins(h, conf.Plugins...) wphandle.RegisterPlugins(h, conf.Plugins...)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushComponentFilterFn("bodyClass", calClass)
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h) components.WidgetArea(h)
pushScripts(h) pushScripts(h)
@ -185,10 +185,10 @@ func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
return return
} }
func calClass(h *wp.Handle) { func calClass(h *wp.Handle, s string, a ...any) string {
class := strings.Split(s, " ")
themeMods := h.CommonThemeMods() themeMods := h.CommonThemeMods()
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage) u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
var class []string
if u != "" && u != "remove-header" { if u != "" && u != "remove-header" {
class = append(class, "has-header-image") class = append(class, "has-header-image")
} }
@ -207,5 +207,5 @@ func calClass(h *wp.Handle) {
class = append(class, "page-two-column") class = append(class, "page-two-column")
} }
} }
h.PushClass(class...) return strings.Join(class, " ")
} }

View File

@ -12,10 +12,11 @@ import (
) )
func CalBodyClass(h *Handle) { func CalBodyClass(h *Handle) {
h.ginH["bodyClass"] = h.BodyClass(h.bodyClass...) h.ginH["bodyClass"] = h.BodyClass()
} }
func (h *Handle) BodyClass(class ...string) string { func (h *Handle) BodyClass() string {
var class []string
if constraints.Ok != h.Stats { if constraints.Ok != h.Stats {
class = append(class, "error404") class = append(class, "error404")
} }
@ -72,5 +73,5 @@ func (h *Handle) BodyClass(class ...string) string {
if h.themeMods.ThemeSupport.ResponsiveEmbeds { if h.themeMods.ThemeSupport.ResponsiveEmbeds {
class = append(class, "wp-embed-responsive") class = append(class, "wp-embed-responsive")
} }
return strings.Join(class, " ") return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " "))
} }

View File

@ -27,7 +27,6 @@ type Handle struct {
Code int Code int
Stats int Stats int
templ string templ string
bodyClass []string
components map[string][]Components[string] components map[string][]Components[string]
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall handleFns map[int][]HandleCall
@ -149,10 +148,6 @@ func (h *Handle) SetData(k string, v any) {
h.ginH[k] = v h.ginH[k] = v
} }
func (h *Handle) PushClass(class ...string) {
h.bodyClass = append(h.bodyClass, class...)
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T { func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
v, ok := h.componentsArgs[k] v, ok := h.componentsArgs[k]
if ok { if ok {