From a635b9a8eaa4d17c3cacc4e9b1866ae051322894 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 16 Feb 2023 20:48:08 +0800 Subject: [PATCH] customlogo --- internal/theme/common/bodyclass.go | 20 ++++--- internal/theme/common/customlogo.go | 54 +++++++++++++++++++ internal/theme/common/detail.go | 1 + internal/theme/common/index.go | 1 + internal/theme/common/reload.go | 1 + .../theme/twentyfifteen/layout/base.gohtml | 3 ++ internal/wpconfig/thememods.go | 25 +++++---- 7 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 internal/theme/common/customlogo.go diff --git a/internal/theme/common/bodyclass.go b/internal/theme/common/bodyclass.go index 06fb5ad..be2f386 100644 --- a/internal/theme/common/bodyclass.go +++ b/internal/theme/common/bodyclass.go @@ -71,14 +71,18 @@ func (h *Handle) bodyClass(class ...string) string { } } class = append(class, s) - if h.IsSupport("custom-background") && wpconfig.IsCustomBackground(h.Theme) { - class = append(class, "custom-background") - } - if h.IsSupport("custom-logo") && wpconfig.IsCustomLogo(h.Theme) { - class = append(class, "wp-custom-logo") - } - if h.IsSupport("responsive-embeds") { - class = append(class, "wp-embed-responsive") + mods, err := wpconfig.GetThemeMods(h.Theme) + if err == nil { + if wpconfig.IsCustomBackground(h.Theme) { + class = append(class, "custom-background") + } + if mods.CustomLogo > 0 { + class = append(class, "wp-custom-logo") + } + if mods.ThemeSupport.ResponsiveEmbeds { + class = append(class, "wp-embed-responsive") + } } + return str.Join(commonClass[h.Scene], strings.Join(class, " ")) } diff --git a/internal/theme/common/customlogo.go b/internal/theme/common/customlogo.go new file mode 100644 index 0000000..b2f6831 --- /dev/null +++ b/internal/theme/common/customlogo.go @@ -0,0 +1,54 @@ +package common + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/internal/pkg/cache" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "github.com/fthvgb1/wp-go/safety" +) + +var logo = safety.NewVar("default") + +func (h *Handle) CalCustomLogo() (r string) { + mods, err := wpconfig.GetThemeMods(h.Theme) + if err != nil { + return + } + id := uint64(mods.CustomLogo) + if id < 1 { + return + } + 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.Options.Value("blogname"))) + 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(`%s`, "/", ` aria-current="page"`, r) + return +} + +func (h *Handle) CustomLogo() { + s := logo.Load() + if s == "default" { + s = h.CalCustomLogo() + logo.Store(s) + } + h.GinH["customLogo"] = s +} diff --git a/internal/theme/common/detail.go b/internal/theme/common/detail.go index df8c48c..c5801ca 100644 --- a/internal/theme/common/detail.go +++ b/internal/theme/common/detail.go @@ -98,6 +98,7 @@ func (d *DetailHandle) Render() { d.CommentRender = plugins.CommentRender() } d.SiteIcon() + d.CustomLogo() d.RenderComment() d.CalBodyClass() if d.Templ == "" { diff --git a/internal/theme/common/index.go b/internal/theme/common/index.go index a662810..2ebf47f 100644 --- a/internal/theme/common/index.go +++ b/internal/theme/common/index.go @@ -113,6 +113,7 @@ func (i *IndexHandle) Render() { } i.Pagination() i.SiteIcon() + i.CustomLogo() i.CalBodyClass() if i.Templ == "" { i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) diff --git a/internal/theme/common/reload.go b/internal/theme/common/reload.go index 93eb97a..ce7d291 100644 --- a/internal/theme/common/reload.go +++ b/internal/theme/common/reload.go @@ -2,4 +2,5 @@ package common func Reload() { backgroud.Store("default") + icon.Store("default") } diff --git a/internal/theme/twentyfifteen/layout/base.gohtml b/internal/theme/twentyfifteen/layout/base.gohtml index 8e2fba5..3e9531e 100644 --- a/internal/theme/twentyfifteen/layout/base.gohtml +++ b/internal/theme/twentyfifteen/layout/base.gohtml @@ -16,6 +16,9 @@