-
Notifications
You must be signed in to change notification settings - Fork 0
/
thememods.go
186 lines (166 loc) · 5.9 KB
/
thememods.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package wpconfig
import (
"embed"
"fmt"
"github.com/fthvgb1/wp-go/app/phphelper"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/safety"
"path/filepath"
"strings"
)
var templateFs embed.FS
func SetTemplateFs(fs embed.FS) {
templateFs = fs
}
// ThemeMods 只有部分公共的参数,其它的参数调用 GetThemeModsVal 函数获取
type ThemeMods struct {
CustomCssPostId int `json:"custom_css_post_id,omitempty"`
NavMenuLocations map[string]int `json:"nav_menu_locations,omitempty"`
CustomLogo int `json:"custom_logo,omitempty"`
HeaderImage string `json:"header_image,omitempty"`
BackgroundImage string `json:"background_image,omitempty"`
BackgroundSize string `json:"background_size,omitempty"`
BackgroundRepeat string `json:"background_repeat,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
BackgroundPreset string `json:"background_preset"`
BackgroundPositionX string `json:"background_position_x,omitempty"`
BackgroundPositionY string `json:"background_position_y"`
BackgroundAttachment string `json:"background_attachment"`
ColorScheme string `json:"color_scheme"`
SidebarTextcolor string `json:"sidebar_textcolor,omitempty"`
HeaderBackgroundColor string `json:"header_background_color,omitempty"`
HeaderTextcolor string `json:"header_textcolor,omitempty"`
HeaderVideo int `json:"header_video,omitempty"`
ExternalHeaderVideo string `json:"external_header_video,omitempty"`
HeaderImagData ImageData `json:"header_image_data,omitempty"`
SidebarsWidgets Sidebars `json:"sidebars_widgets,omitempty"`
ThemeSupport ThemeSupport
}
type Sidebars struct {
Time int `json:"time,omitempty"`
Data SidebarsData `json:"data,omitempty"`
}
type ColorScheme struct {
Label string `json:"label,omitempty"`
Colors []string `json:"colors,omitempty"`
}
type SidebarsData struct {
WpInactiveWidgets []string `json:"wp_inactive_widgets,omitempty"`
Sidebar1 []string `json:"sidebar-1,omitempty"`
Sidebar2 []string `json:"sidebar-2,omitempty"`
Sidebar3 []string `json:"sidebar-3,omitempty"`
}
type ImageData struct {
AttachmentId int64 `json:"attachment_id,omitempty"`
Url string `json:"url,omitempty"`
ThumbnailUrl string `json:"thumbnail_url,omitempty"`
Height int64 `json:"height,omitempty"`
Width int64 `json:"width,omitempty"`
}
func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) {
up := strings.Split(metadata.File, "/")
if metadata.File != "" && Type == "full" {
mimeType := metadata.Sizes["thumbnail"].MimeType
metadata.Sizes = maps.Copy(metadata.Sizes)
metadata.Sizes["full"] = models.MetaDataFileSize{
File: filepath.Base(metadata.File),
Width: metadata.Width,
Height: metadata.Height,
MimeType: mimeType,
FileSize: metadata.FileSize,
}
}
if siz, ok := metadata.Sizes[Type]; ok {
r.Path = fmt.Sprintf("%s/wp-content/uploads/%s", host, strings.ReplaceAll(metadata.File, filepath.Base(metadata.File), siz.File))
r.Width = metadata.Sizes[Type].Width
r.Height = metadata.Sizes[Type].Height
r.Srcset = strings.Join(maps.FilterToSlice[string](metadata.Sizes, func(s string, size models.MetaDataFileSize) (r string, ok bool) {
up[len(up)-1] = size.File
for _, s2 := range except {
if s == s2 {
return
}
}
r = fmt.Sprintf("%s/wp-content/uploads/%s %dw", host, strings.Join(up, "/"), size.Width)
ok = true
return
}), ", ")
r.Sizes = fmt.Sprintf("(max-width: %dpx) 100vw, %dpx", r.Width, r.Width)
if r.Width >= 740 && r.Width < 767 {
r.Sizes = "(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px"
} else if r.Width >= 767 {
r.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
}
r.OriginAttachmentData = metadata
}
return
}
var themeModes = func() *safety.Map[string, ThemeMods] {
m := safety.NewMap[string, ThemeMods]()
themeModsRaw = safety.NewMap[string, map[string]any]()
reload.Append(func() {
m.Flush()
themeModsRaw.Flush()
}, "theme-modes")
return m
}()
var themeModsRaw *safety.Map[string, map[string]any]
var themeSupport = map[string]ThemeSupport{}
func SetThemeSupport(theme string, support ThemeSupport) {
themeSupport[theme] = support
}
func GetThemeModsVal[T any](theme, k string, defaults T) (r T) {
m, ok := themeModsRaw.Load(theme)
if !ok {
r = defaults
return
}
r = maps.GetStrAnyValWithDefaults(m, k, defaults)
return
}
func GetThemeMods(theme string) (r ThemeMods, err error) {
r, ok := themeModes.Load(theme)
if ok {
return
}
mods := GetOption(fmt.Sprintf("theme_mods_%s", theme))
if mods == "" {
return
}
m, err := phphelper.UnPHPSerializeToStrAnyMap(mods)
if err != nil {
return
}
themeModsRaw.Store(theme, m)
//这里在的err可以不用处理,因为php的默认值和有设置过的类型可能不一样,直接按有设置的类型处理就行
r, err = maps.StrAnyMapToStruct[ThemeMods](m)
if err != nil {
logs.Error(err, "解析thememods错误(可忽略)")
err = nil
}
r.setThemeSupport(theme)
themeModes.Store(theme, r)
return
}
func IsCustomBackground(theme string) bool {
mods, err := GetThemeMods(theme)
if err != nil {
return false
}
if mods.BackgroundColor != "" && mods.BackgroundColor != "default-color" || mods.BackgroundImage != "" && mods.BackgroundImage != "default-image" {
return true
}
return false
}
func (m *ThemeMods) setThemeSupport(themeName string) {
var v ThemeSupport
vv, ok := themeSupport[themeName]
if ok {
m.ThemeSupport = vv
} else {
m.ThemeSupport = v
}
}