wp-go/app/theme/wp/customlogo.go

52 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-03-01 05:17:12 +00:00
package wp
2023-02-16 12:48:08 +00:00
import (
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-02-16 12:48:08 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
2023-02-16 16:20:38 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2023-02-16 12:48:08 +00:00
)
func CalCustomLogo(h *Handle) (r string) {
2023-02-28 15:38:23 +00:00
id := uint64(h.themeMods.CustomLogo)
2023-02-16 12:48:08 +00:00
if id < 1 {
id = str.ToInteger[uint64](wpconfig.GetOption("site_logo"), 0)
2023-02-16 16:20:38 +00:00
if id < 1 {
return
}
2023-02-16 12:48:08 +00:00
}
logo, err := cache.GetPostById(h.C, id)
if err != nil || logo.AttachmentMetadata.File == "" {
return
}
siz := "full"
meta, _ := cache.GetPostMetaByPostId(h.C, id)
alt := maps.WithDefaultVal(meta, "_wp_attachment_image_alt", any(wpconfig.GetOption("blogname")))
2023-02-16 12:48:08 +00:00
desc := alt.(string)
imgx := map[string]string{
"class": "custom-logo",
"alt": desc,
"decoding": "async",
//"loading":"lazy",
}
img := wpconfig.Thumbnail(logo.AttachmentMetadata, siz, "", "")
imgx["srcset"] = img.Srcset
imgx["sizes"] = img.Sizes
imgx["src"] = img.Path
r = fmt.Sprintf("%s />", maps.Reduce(imgx, func(k string, v string, t string) string {
return fmt.Sprintf(`%s %s="%s"`, t, k, v)
}, fmt.Sprintf(`<img wight="%v" height="%v"`, img.Width, img.Height)))
r = fmt.Sprintf(`<a href="%s" class="custom-logo-link" rel="home"%s>%s</a>`, "/", ` aria-current="page"`, r)
return
}
2023-04-24 13:51:43 +00:00
func customLogo(h *Handle) func() string {
return func() string {
return reload.GetAnyValBys("customLogo", h, func(h *Handle) string {
return CalCustomLogo(h)
})
}
}