2023-02-15 16:32:02 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-02-16 03:53:24 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-02-15 16:32:02 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
|
|
|
)
|
|
|
|
|
|
|
|
var postx = map[string]string{
|
|
|
|
"left": "left",
|
|
|
|
"right": "right",
|
|
|
|
"center": "center",
|
|
|
|
}
|
|
|
|
var posty = map[string]string{
|
|
|
|
"top": "top",
|
|
|
|
"bottom": "bottom",
|
|
|
|
"center": "center",
|
|
|
|
}
|
|
|
|
var size = map[string]string{
|
|
|
|
"auto": "auto",
|
|
|
|
"contain": "contain",
|
|
|
|
"cover": "cover",
|
|
|
|
}
|
|
|
|
var repeat = map[string]string{
|
|
|
|
"repeat-x": "repeat-x",
|
|
|
|
"repeat-y": "repeat-y",
|
|
|
|
"repeat": "repeat",
|
|
|
|
"no-repeat": "no-repeat",
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:34:36 +00:00
|
|
|
var backgroud = safety.NewVar("default")
|
2023-02-15 16:32:02 +00:00
|
|
|
|
|
|
|
func (h *Handle) CustomBackGround() {
|
|
|
|
b := backgroud.Load()
|
2023-02-15 16:34:36 +00:00
|
|
|
if b == "default" {
|
2023-02-15 16:32:02 +00:00
|
|
|
b = h.CalCustomBackGround()
|
|
|
|
backgroud.Store(b)
|
|
|
|
}
|
|
|
|
h.GinH["customBackground"] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) CalCustomBackGround() (r string) {
|
|
|
|
mods, err := wpconfig.GetThemeMods(h.Theme)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mods.BackgroundImage == "" && mods.BackgroundColor == mods.ThemeSupport.CustomBackground.DefaultColor {
|
|
|
|
return
|
|
|
|
}
|
2023-02-16 03:53:24 +00:00
|
|
|
s := str.NewBuilder()
|
2023-02-15 16:32:02 +00:00
|
|
|
if mods.BackgroundImage != "" {
|
2023-02-16 03:53:24 +00:00
|
|
|
s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage))
|
2023-02-15 16:32:02 +00:00
|
|
|
}
|
|
|
|
backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, mods.ThemeSupport.CustomBackground.DefaultPositionX)
|
|
|
|
backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left")
|
|
|
|
|
|
|
|
backgroundPositionY := helper.Defaults(mods.BackgroundPositionY, mods.ThemeSupport.CustomBackground.DefaultPositionY)
|
|
|
|
backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top")
|
|
|
|
positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY)
|
|
|
|
|
|
|
|
siz := helper.DefaultVal(mods.BackgroundSize, mods.ThemeSupport.CustomBackground.DefaultSize)
|
|
|
|
siz = maps.WithDefaultVal(size, siz, "auto")
|
|
|
|
siz = fmt.Sprintf(" background-size: %s;", siz)
|
|
|
|
|
|
|
|
repeats := helper.Defaults(mods.BackgroundRepeat, mods.ThemeSupport.CustomBackground.DefaultRepeat)
|
|
|
|
repeats = maps.WithDefaultVal(repeat, repeats, "repeat")
|
|
|
|
repeats = fmt.Sprintf(" background-repeat: %s;", repeats)
|
|
|
|
|
|
|
|
attachment := helper.Defaults(mods.BackgroundAttachment, mods.ThemeSupport.CustomBackground.DefaultAttachment)
|
|
|
|
attachment = helper.Or(attachment == "fixed", "fixed", "scroll")
|
|
|
|
attachment = fmt.Sprintf(" background-attachment: %s;", attachment)
|
2023-02-16 03:53:24 +00:00
|
|
|
s.WriteString(positon, siz, repeats, attachment)
|
2023-02-15 16:32:02 +00:00
|
|
|
r = fmt.Sprintf(`<style id="custom-background-css">
|
|
|
|
body.custom-background {%s}
|
|
|
|
</style>`, s.String())
|
|
|
|
return
|
|
|
|
}
|