-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
79 lines (72 loc) · 1.59 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package wpconfig
import (
"context"
"github.com/fthvgb1/wp-go/app/phphelper"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var options safety.Map[string, string]
var phpArr safety.Map[string, map[any]any]
var ctx context.Context
func InitOptions() error {
options.Flush()
phpArr.Flush()
if ctx == nil {
ctx = context.Background()
}
ops, err := model.FindToStringMap[models.Options](ctx, model.Conditions(
model.Where(model.SqlBuilder{{"autoload", "yes"}}),
model.Fields("option_name k, option_value v"),
))
if err != nil {
return err
}
for _, option := range ops {
options.Store(option["k"], option["v"])
}
return nil
}
func GetOption(k string) string {
v, ok := options.Load(k)
if ok {
return v
}
vv, err := model.GetField[models.Options](ctx, "option_value",
model.Conditions(
model.Where(
model.SqlBuilder{{"option_name", k}},
),
),
)
options.Store(k, vv)
if err != nil {
return ""
}
return vv
}
func GetLang() string {
s, ok := options.Load("WPLANG")
if !ok {
s = "zh-CN"
}
return strings.Replace(s, "_", "-", 1)
}
func GetPHPArrayVal[T any](optionName string, defaults T, key ...any) T {
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...)
}