2023-02-14 11:47:47 +00:00
|
|
|
|
package wpconfig
|
2023-01-25 18:26:36 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2023-02-15 16:32:02 +00:00
|
|
|
|
"embed"
|
|
|
|
|
"encoding/json"
|
2023-01-25 18:26:36 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-02-17 15:36:54 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
2023-02-14 11:47:47 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/phphelper"
|
2023-02-20 17:07:32 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
2023-01-25 18:26:36 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
2023-02-14 11:47:47 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-02-15 16:32:02 +00:00
|
|
|
|
"path/filepath"
|
2023-01-25 18:26:36 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-15 16:32:02 +00:00
|
|
|
|
var templateFs embed.FS
|
|
|
|
|
|
|
|
|
|
func SetTemplateFs(fs embed.FS) {
|
|
|
|
|
templateFs = fs
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 17:07:32 +00:00
|
|
|
|
// ThemeMods 只有部分公共的参数,其它的参数调用 GetThemeModsVal 函数获取
|
2023-02-18 15:35:39 +00:00
|
|
|
|
type ThemeMods struct {
|
2023-02-19 14:15:19 +00:00
|
|
|
|
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"`
|
2023-02-20 17:07:32 +00:00
|
|
|
|
HeaderVideo int `json:"header_video,omitempty"`
|
|
|
|
|
ExternalHeaderVideo string `json:"external_header_video,omitempty"`
|
2023-02-19 14:15:19 +00:00
|
|
|
|
HeaderImagData ImageData `json:"header_image_data,omitempty"`
|
|
|
|
|
SidebarsWidgets Sidebars `json:"sidebars_widgets,omitempty"`
|
2023-02-15 16:32:02 +00:00
|
|
|
|
ThemeSupport ThemeSupport
|
2023-01-31 16:58:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Sidebars struct {
|
|
|
|
|
Time int `json:"time,omitempty"`
|
2023-02-15 16:32:02 +00:00
|
|
|
|
Data SidebarsData `json:"data,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ColorScheme struct {
|
|
|
|
|
Label string `json:"label,omitempty"`
|
|
|
|
|
Colors []string `json:"colors,omitempty"`
|
2023-01-31 16:58:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 18:26:36 +00:00
|
|
|
|
func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) {
|
2023-02-16 12:48:08 +00:00
|
|
|
|
up := strings.Split(metadata.File, "/")
|
2023-02-24 11:34:19 +00:00
|
|
|
|
if metadata.File != "" && Type == "full" {
|
2023-02-16 12:48:08 +00:00
|
|
|
|
metadata.Sizes["full"] = models.MetaDataFileSize{
|
|
|
|
|
File: filepath.Base(metadata.File),
|
|
|
|
|
Width: metadata.Width,
|
|
|
|
|
Height: metadata.Height,
|
|
|
|
|
MimeType: metadata.Sizes["thumbnail"].MimeType,
|
|
|
|
|
FileSize: metadata.FileSize,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-25 18:26:36 +00:00
|
|
|
|
if _, ok := metadata.Sizes[Type]; ok {
|
|
|
|
|
r.Path = fmt.Sprintf("%s/wp-content/uploads/%s", host, metadata.File)
|
|
|
|
|
r.Width = metadata.Sizes[Type].Width
|
|
|
|
|
r.Height = metadata.Sizes[Type].Height
|
2023-02-16 12:48:08 +00:00
|
|
|
|
|
2023-01-25 18:26:36 +00:00
|
|
|
|
r.Srcset = strings.Join(maps.FilterToSlice[string](metadata.Sizes, func(s string, size models.MetaDataFileSize) (r string, ok bool) {
|
2023-02-16 12:48:08 +00:00
|
|
|
|
up[len(up)-1] = size.File
|
2023-01-25 18:26:36 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-02-14 11:47:47 +00:00
|
|
|
|
|
2023-02-18 15:35:39 +00:00
|
|
|
|
var themeModes = func() *safety.Map[string, ThemeMods] {
|
|
|
|
|
m := safety.NewMap[string, ThemeMods]()
|
2023-02-20 17:07:32 +00:00
|
|
|
|
themeModsRaw = safety.NewMap[string, map[string]any]()
|
2023-02-17 15:36:54 +00:00
|
|
|
|
reload.Push(func() {
|
|
|
|
|
m.Flush()
|
2023-02-20 17:07:32 +00:00
|
|
|
|
themeModsRaw.Flush()
|
2023-02-17 15:36:54 +00:00
|
|
|
|
})
|
2023-02-20 17:07:32 +00:00
|
|
|
|
|
2023-02-17 15:36:54 +00:00
|
|
|
|
return m
|
|
|
|
|
}()
|
2023-02-14 11:47:47 +00:00
|
|
|
|
|
2023-02-20 17:07:32 +00:00
|
|
|
|
var themeModsRaw *safety.Map[string, map[string]any]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 15:35:39 +00:00
|
|
|
|
func GetThemeMods(theme string) (r ThemeMods, err error) {
|
2023-02-14 11:47:47 +00:00
|
|
|
|
r, ok := themeModes.Load(theme)
|
|
|
|
|
if ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-20 17:07:32 +00:00
|
|
|
|
mods := GetOption(fmt.Sprintf("theme_mods_%s", theme))
|
|
|
|
|
if mods == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-06 12:53:51 +00:00
|
|
|
|
m, err := phphelper.UnPHPSerializeToStrAnyMap(mods)
|
2023-02-20 17:07:32 +00:00
|
|
|
|
if err != nil {
|
2023-02-14 11:47:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-20 17:07:32 +00:00
|
|
|
|
themeModsRaw.Store(theme, m)
|
2023-02-19 14:15:19 +00:00
|
|
|
|
//这里在的err可以不用处理,因为php的默认值和有设置过的类型可能不一样,直接按有设置的类型处理就行
|
2023-02-20 17:07:32 +00:00
|
|
|
|
r, err = maps.StrAnyMapToStruct[ThemeMods](m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.ErrPrintln(err, "解析thememods错误")
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
2023-02-16 06:36:43 +00:00
|
|
|
|
r.setThemeSupport(theme)
|
2023-02-14 11:47:47 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-02-15 16:32:02 +00:00
|
|
|
|
|
2023-02-18 15:35:39 +00:00
|
|
|
|
func (m *ThemeMods) setThemeSupport(themeName string) {
|
2023-02-15 16:32:02 +00:00
|
|
|
|
bytes, err := templateFs.ReadFile(filepath.Join(themeName, "themesupport.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var themeSupport ThemeSupport
|
|
|
|
|
err = json.Unmarshal(bytes, &themeSupport)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.ThemeSupport = themeSupport
|
|
|
|
|
}
|