wp-go/app/theme/hook.go

31 lines
646 B
Go
Raw Permalink Normal View History

2023-01-18 15:04:12 +00:00
package theme
import (
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/theme/wp"
2024-01-22 12:29:17 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2023-05-04 13:44:17 +00:00
"github.com/fthvgb1/wp-go/safety"
2023-01-18 15:04:12 +00:00
)
2023-05-04 13:44:17 +00:00
var themeMap = safety.NewMap[string, func(*wp.Handle)]()
2023-01-18 15:04:12 +00:00
2023-05-04 13:44:17 +00:00
func AddThemeHookFunc(name string, fn func(handle *wp.Handle)) {
if _, ok := themeMap.Load(name); ok {
2023-01-18 15:04:12 +00:00
panic("exists same name theme")
}
2023-05-04 13:44:17 +00:00
themeMap.Store(name, fn)
2023-01-18 15:04:12 +00:00
}
2024-01-22 12:29:17 +00:00
func IsThemeHookFuncExist(name string) bool {
_, ok := themeMap.Load(name)
return ok
}
2023-05-04 13:44:17 +00:00
func Hook(themeName string, h *wp.Handle) {
fn, ok := themeMap.Load(themeName)
2023-01-18 15:04:12 +00:00
if ok && fn != nil {
2023-05-04 13:44:17 +00:00
fn(h)
2023-01-18 15:04:12 +00:00
return
}
2024-01-22 12:29:17 +00:00
panic(str.Join("theme ", themeName, " don't exist"))
2023-01-18 15:04:12 +00:00
}