wp-go/app/theme/twentyfifteen/custombackground.go

66 lines
2.3 KiB
Go
Raw Permalink Normal View History

package twentyfifteen
2023-02-15 16:32:02 +00:00
import (
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/theme/wp"
2023-02-15 16:32:02 +00:00
"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
)
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-03-01 05:17:12 +00:00
func CalCustomBackGround(h *wp.Handle) (r string) {
2023-02-28 15:38:23 +00:00
themeMods := h.CommonThemeMods()
2023-03-14 05:50:13 +00:00
if themeMods.BackgroundImage == "" && (themeMods.BackgroundColor == "" || themeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor) {
2023-02-15 16:32:02 +00:00
return
}
2023-02-16 03:53:24 +00:00
s := str.NewBuilder()
2023-02-28 15:38:23 +00:00
if themeMods.BackgroundImage != "" {
s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(themeMods.BackgroundImage))
2023-02-15 16:32:02 +00:00
}
2023-02-28 15:38:23 +00:00
backgroundPositionX := helper.Defaults(themeMods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX)
2023-02-15 16:32:02 +00:00
backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left")
2023-02-28 15:38:23 +00:00
backgroundPositionY := helper.Defaults(themeMods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY)
2023-02-15 16:32:02 +00:00
backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top")
positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY)
2023-02-28 15:38:23 +00:00
siz := helper.DefaultVal(themeMods.BackgroundSize, themesupport.CustomBackground.DefaultSize)
2023-02-15 16:32:02 +00:00
siz = maps.WithDefaultVal(size, siz, "auto")
siz = fmt.Sprintf(" background-size: %s;", siz)
2023-02-28 15:38:23 +00:00
repeats := helper.Defaults(themeMods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat)
2023-02-15 16:32:02 +00:00
repeats = maps.WithDefaultVal(repeat, repeats, "repeat")
repeats = fmt.Sprintf(" background-repeat: %s;", repeats)
2023-02-28 15:38:23 +00:00
attachment := helper.Defaults(themeMods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment)
2023-02-15 16:32:02 +00:00
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
}