2022-11-17 08:29:39 +00:00
|
|
|
package wpconfig
|
2022-11-15 03:11:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/phphelper"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
2023-03-06 12:53:51 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/model"
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-01-19 06:12:59 +00:00
|
|
|
"strings"
|
2022-11-15 03:11:08 +00:00
|
|
|
)
|
|
|
|
|
2023-02-20 17:07:32 +00:00
|
|
|
var options safety.Map[string, string]
|
2023-03-06 12:53:51 +00:00
|
|
|
var phpArr safety.Map[string, map[any]any]
|
2023-02-20 17:07:32 +00:00
|
|
|
|
|
|
|
var ctx context.Context
|
2022-11-15 03:11:08 +00:00
|
|
|
|
|
|
|
func InitOptions() error {
|
2023-02-20 17:07:32 +00:00
|
|
|
options.Flush()
|
2023-03-06 12:53:51 +00:00
|
|
|
phpArr.Flush()
|
2023-02-20 17:07:32 +00:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
ops, err := model.FindToStringMap[models.Options](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{{"autoload", "yes"}}),
|
2023-02-21 11:49:13 +00:00
|
|
|
model.Fields("option_name k, option_value v"),
|
2023-02-21 10:25:52 +00:00
|
|
|
))
|
2022-11-15 03:11:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-20 17:07:32 +00:00
|
|
|
for _, option := range ops {
|
2023-02-21 11:49:13 +00:00
|
|
|
options.Store(option["k"], option["v"])
|
2022-11-15 03:11:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-19 06:12:59 +00:00
|
|
|
|
2023-02-20 17:07:32 +00:00
|
|
|
func GetOption(k string) string {
|
|
|
|
v, ok := options.Load(k)
|
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
2023-02-21 10:25:52 +00:00
|
|
|
vv, err := model.GetField[models.Options](ctx, "option_value", model.Conditions(model.Where(model.SqlBuilder{{"option_name", k}})))
|
2023-02-20 17:07:32 +00:00
|
|
|
options.Store(k, vv)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return vv
|
|
|
|
}
|
|
|
|
|
2023-01-19 06:12:59 +00:00
|
|
|
func GetLang() string {
|
2023-02-20 17:07:32 +00:00
|
|
|
s, ok := options.Load("WPLANG")
|
2023-01-19 06:12:59 +00:00
|
|
|
if !ok {
|
|
|
|
s = "zh-CN"
|
|
|
|
}
|
|
|
|
return strings.Replace(s, "_", "-", 1)
|
|
|
|
}
|
2023-03-06 12:53:51 +00:00
|
|
|
|
2023-03-06 15:43:58 +00:00
|
|
|
func GetPHPArrayVal[T any](optionName string, defaults T, key ...any) T {
|
2023-03-06 12:53:51 +00:00
|
|
|
op, ok := phpArr.Load(optionName)
|
|
|
|
if ok {
|
|
|
|
return maps.GetAnyAnyValWithDefaults(op, defaults, key...)
|
|
|
|
}
|
|
|
|
v := GetOption(optionName)
|
|
|
|
if v == "" {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
arr, err := phphelper.UnPHPSerializeToAnyAnyMap(v)
|
|
|
|
if err != nil {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
phpArr.Store(optionName, arr)
|
|
|
|
return maps.GetAnyAnyValWithDefaults(arr, defaults, key...)
|
|
|
|
}
|