From 3c21187ee301c02d08096ea01eb921732af08c0a Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 16 Feb 2023 19:03:31 +0800 Subject: [PATCH] sitelogo --- internal/theme/common/detail.go | 1 + internal/theme/common/index.go | 1 + internal/theme/common/siteicon.go | 52 +++++++++++++++++++ .../theme/twentyfifteen/layout/head.gohtml | 5 ++ 4 files changed, 59 insertions(+) create mode 100644 internal/theme/common/siteicon.go diff --git a/internal/theme/common/detail.go b/internal/theme/common/detail.go index 4bc34e0..df8c48c 100644 --- a/internal/theme/common/detail.go +++ b/internal/theme/common/detail.go @@ -97,6 +97,7 @@ func (d *DetailHandle) Render() { if d.CommentRender == nil { d.CommentRender = plugins.CommentRender() } + d.SiteIcon() d.RenderComment() d.CalBodyClass() if d.Templ == "" { diff --git a/internal/theme/common/index.go b/internal/theme/common/index.go index 49a0475..a662810 100644 --- a/internal/theme/common/index.go +++ b/internal/theme/common/index.go @@ -112,6 +112,7 @@ func (i *IndexHandle) Render() { i.PageEle = plugins.TwentyFifteenPagination() } i.Pagination() + i.SiteIcon() i.CalBodyClass() if i.Templ == "" { i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) diff --git a/internal/theme/common/siteicon.go b/internal/theme/common/siteicon.go new file mode 100644 index 0000000..05b41d6 --- /dev/null +++ b/internal/theme/common/siteicon.go @@ -0,0 +1,52 @@ +package common + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "github.com/fthvgb1/wp-go/safety" + "strings" +) + +var icon = safety.NewVar("default") +var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} + +func (h *Handle) CalSiteIcon() (r string) { + id := str.ToInteger[uint64](wpconfig.Options.Value("site_icon"), 0) + if id < 1 { + return + } + icon, err := cache.GetPostById(h.C, id) + if err != nil || icon.AttachmentMetadata.File == "" { + return + } + m := strings.Join(strings.Split(icon.AttachmentMetadata.File, "/")[:2], "/") + size := slice.FilterAndMap(sizes, func(t string) (string, bool) { + s, ok := icon.AttachmentMetadata.Sizes[t] + if !ok { + return "", false + } + switch t { + case "site_icon-270": + return fmt.Sprintf(``, m, s.File), true + case "site_icon-180": + return fmt.Sprintf(`"`, m, s.File), true + default: + ss := strings.Replace(t, "site_icon-", "", 1) + return fmt.Sprintf(``, m, s.File, ss, ss), true + } + }) + r = strings.Join(size, "\n") + return +} + +func (h *Handle) SiteIcon() { + s := icon.Load() + if s == "default" { + s = h.CalSiteIcon() + icon.Store(s) + } + h.GinH["siteIcon"] = s +} diff --git a/internal/theme/twentyfifteen/layout/head.gohtml b/internal/theme/twentyfifteen/layout/head.gohtml index d243e25..9db72e0 100644 --- a/internal/theme/twentyfifteen/layout/head.gohtml +++ b/internal/theme/twentyfifteen/layout/head.gohtml @@ -64,4 +64,9 @@ {{if .customHeader}} {{.customHeader|unescaped}} {{end}} + + {{if .siteIcon}} + {{.siteIcon|unescaped}} + {{end}} + {{end}} \ No newline at end of file