wp-go/internal/theme/common/customlogo.go

43 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-02-16 12:48:08 +00:00
package common
import (
"fmt"
"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
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func (h *Handle) CalCustomLogo() (r string) {
2023-02-18 15:35:39 +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
}