Merge pull request #13 from fthvgb1/dev

修复默认页眉图片bug并优化代码
This commit is contained in:
2023-02-05 00:15:00 +08:00 committed by GitHub
commit 7997a9f512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 113 additions and 102 deletions

1
go.mod
View File

@ -13,6 +13,7 @@ require (
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )

5
go.sum
View File

@ -31,8 +31,8 @@ github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGF
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
@ -92,6 +92,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc=
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo=
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
@ -111,7 +113,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

View File

@ -4,19 +4,11 @@ package number
import ( import (
"fmt" "fmt"
"golang.org/x/exp/constraints"
"math/rand" "math/rand"
) )
type IntNumber interface { func Range[T constraints.Integer](start, end, step T) []T {
~int | ~int64 | ~int32 | ~int8 | ~int16 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type Number interface {
IntNumber | ~float64 | ~float32
}
func Range[T IntNumber](start, end, step T) []T {
if step == 0 { if step == 0 {
panic("step can't be 0") panic("step can't be 0")
} }
@ -36,12 +28,12 @@ func Range[T IntNumber](start, end, step T) []T {
} }
// Rand 都为闭区间 [start,end] // Rand 都为闭区间 [start,end]
func Rand[T IntNumber](start, end T) T { func Rand[T constraints.Integer](start, end T) T {
end++ end++
return T(rand.Int63n(int64(end-start))) + start return T(rand.Int63n(int64(end-start))) + start
} }
func Min[T Number](a ...T) T { func Min[T constraints.Integer | constraints.Float](a ...T) T {
min := a[0] min := a[0]
for _, t := range a { for _, t := range a {
if min > t { if min > t {
@ -51,7 +43,7 @@ func Min[T Number](a ...T) T {
return min return min
} }
func Max[T Number](a ...T) T { func Max[T constraints.Integer | constraints.Float](a ...T) T {
max := a[0] max := a[0]
for _, t := range a { for _, t := range a {
if max < t { if max < t {
@ -61,7 +53,7 @@ func Max[T Number](a ...T) T {
return max return max
} }
func Sum[T Number](a ...T) T { func Sum[T constraints.Integer | constraints.Float](a ...T) T {
s := T(0) s := T(0)
for _, t := range a { for _, t := range a {
s += t s += t
@ -69,28 +61,28 @@ func Sum[T Number](a ...T) T {
return s return s
} }
func Add[T Number](i, j T) T { func Add[T constraints.Integer | constraints.Float](i, j T) T {
return i + j return i + j
} }
func Sub[T Number](i, j T) T { func Sub[T constraints.Integer | constraints.Float](i, j T) T {
return i - j return i - j
} }
func ToString[T Number](n T) string { func ToString[T constraints.Integer | constraints.Float](n T) string {
return fmt.Sprintf("%v", n) return fmt.Sprintf("%v", n)
} }
func Abs[T Number](n T) T { func Abs[T constraints.Integer | constraints.Float](n T) T {
if n >= 0 { if n >= 0 {
return n return n
} }
return -n return -n
} }
func Mul[T Number](i, j T) T { func Mul[T constraints.Integer | constraints.Float](i, j T) T {
return i * j return i * j
} }
func Divide[T Number](i, j T) T { func Divide[T constraints.Integer | constraints.Float](i, j T) T {
return i / j return i / j
} }

View File

@ -23,7 +23,7 @@ func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme)) mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme))
if ok && mods != "" { if ok && mods != "" {
meta, er := plugins.UnPHPSerialize[plugins.HeaderImageMeta](mods) meta, er := plugins.UnPHPSerialize[plugins.HeaderImageMeta](mods)
if er != nil { if er != nil || meta.HeaderImage == "" {
err = er err = er
return return
} }

View File

@ -28,13 +28,15 @@
</h1> </h1>
<p class="site-description">{{"blogdescription"| getOption}}</p> <p class="site-description">{{"blogdescription"| getOption}}</p>
</div><!-- .site-branding-text --> </div><!-- .site-branding-text -->
{{if eq .scene 1}}
<a href="#content" class="menu-scroll-down">
<svg class="icon icon-arrow-right" aria-hidden="true" role="img">
<use href="#icon-arrow-right" xlink:href="#icon-arrow-right"></use>
</svg>
<span class="screen-reader-text">向下滚动到内容</span>
</a>
{{end}}
<a href="#content" class="menu-scroll-down">
<svg class="icon icon-arrow-right" aria-hidden="true" role="img">
<use href="#icon-arrow-right" xlink:href="#icon-arrow-right"></use>
</svg>
<span class="screen-reader-text">向下滚动到内容</span>
</a>
</div><!-- .wrap --> </div><!-- .wrap -->
</div><!-- .site-branding --> </div><!-- .site-branding -->

View File

@ -8,7 +8,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/plugin/pagination" "github.com/fthvgb1/wp-go/plugin/pagination"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strings" "strings"
@ -26,69 +25,68 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
func Hook(status int, c *gin.Context, h gin.H, scene, stats int) { type handle struct {
templ := "twentyseventeen/posts/index.gohtml" c *gin.Context
h["HeaderImage"] = getHeaderImage(c) ginH gin.H
scene int
status int
stats int
templ string
}
func Hook(status int, c *gin.Context, ginH gin.H, scene, stats int) {
h := handle{
c: c,
ginH: ginH,
scene: scene,
status: status,
stats: stats,
templ: "twentyseventeen/posts/index.gohtml",
}
ginH["HeaderImage"] = h.getHeaderImage(c)
if stats == plugins.Empty404 { if stats == plugins.Empty404 {
c.HTML(status, templ, h) c.HTML(status, h.templ, ginH)
return
} }
if _, ok := plugins.IndexSceneMap[scene]; ok { if scene == plugins.Detail {
posts := h["posts"].([]models.Posts) h.detail()
p, ok := h["pagination"] return
}
h.index()
}
func (h handle) index() {
posts := h.ginH["posts"].([]models.Posts)
p, ok := h.ginH["pagination"]
if ok {
pp, ok := p.(pagination.ParsePagination)
if ok { if ok {
pp, ok := p.(pagination.ParsePagination) h.ginH["pagination"] = pagination.Paginate(paginate, pp)
if ok {
h["pagination"] = pagination.Paginate(paginate, pp)
}
} }
d := 0
s := ""
if scene == plugins.Search {
if len(posts) > 0 {
d = 1
} else {
d = 0
}
} else if scene == plugins.Category {
cat := c.Param("category")
_, cate := slice.SearchFirst(cache.Categories(c), func(my models.TermsMy) bool {
return my.Name == cat
})
d = int(cate.Terms.TermId)
if cate.Slug[0] != '%' {
s = cate.Slug
}
} else if scene == plugins.Tag {
cat := c.Param("tag")
_, cate := slice.SearchFirst(cache.Tags(c), func(my models.TermsMy) bool {
return my.Name == cat
})
d = int(cate.Terms.TermId)
if cate.Slug[0] != '%' {
s = cate.Slug
}
}
h["bodyClass"] = bodyClass(scene, d, s)
h["posts"] = postThumbnail(posts, scene)
} else if scene == plugins.Detail {
post := h["post"].(models.Posts)
h["bodyClass"] = bodyClass(scene, int(post.Id))
//host, _ := wpconfig.Options.Load("siteurl")
host := ""
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
img.Width = img.OriginAttachmentData.Width
img.Height = img.OriginAttachmentData.Height
img.Sizes = "100vw"
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
post.Thumbnail = img
h["post"] = post
comments := h["comments"].([]models.Comments)
dep := h["maxDep"].(int)
h["comments"] = plugins.FormatComments(c, comment{}, comments, dep)
templ = "twentyseventeen/posts/detail.gohtml"
} }
c.HTML(status, templ, h) h.ginH["bodyClass"] = h.bodyClass()
return h.ginH["posts"] = h.postThumbnail(posts, h.scene)
h.c.HTML(h.status, h.templ, h.ginH)
}
func (h handle) detail() {
post := h.ginH["post"].(models.Posts)
h.ginH["bodyClass"] = h.bodyClass()
//host, _ := wpconfig.Options.Load("siteurl")
host := ""
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
img.Width = img.OriginAttachmentData.Width
img.Height = img.OriginAttachmentData.Height
img.Sizes = "100vw"
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
post.Thumbnail = img
h.ginH["post"] = post
comments := h.ginH["comments"].([]models.Comments)
dep := h.ginH["maxDep"].(int)
h.ginH["comments"] = plugins.FormatComments(h.c, comment{}, comments, dep)
h.templ = "twentyseventeen/posts/detail.gohtml"
h.c.HTML(h.status, h.templ, h.ginH)
} }
type comment struct { type comment struct {
@ -109,7 +107,7 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
} }
func postThumbnail(posts []models.Posts, scene int) []models.Posts { func (h handle) postThumbnail(posts []models.Posts, scene int) []models.Posts {
return slice.Map(posts, func(t models.Posts) models.Posts { return slice.Map(posts, func(t models.Posts) models.Posts {
if t.Thumbnail.Path != "" { if t.Thumbnail.Path != "" {
if slice.IsContained(scene, []int{plugins.Home, plugins.Archive, plugins.Search}) { if slice.IsContained(scene, []int{plugins.Home, plugins.Archive, plugins.Search}) {
@ -122,33 +120,50 @@ func postThumbnail(posts []models.Posts, scene int) []models.Posts {
}) })
} }
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) { func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg" r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
r.Width = 2000 r.Width = 2000
r.Height = 1200 r.Height = 1200
t, _ := wpconfig.Options.Load("template") hs, err := cache.GetHeaderImages(c, ThemeName)
hs, err := cache.GetHeaderImages(c, t)
if err != nil { if err != nil {
logs.ErrPrintln(err, "获取页眉背景图失败") logs.ErrPrintln(err, "获取页眉背景图失败")
} else if len(hs) > 0 && err == nil { } else if len(hs) > 0 && err == nil {
_, r = slice.Rand(hs) _, r = slice.Rand(hs)
} }
r.Sizes = "100vw" r.Sizes = "100vw"
return return
} }
func bodyClass(scene, d int, a ...any) string { func (h handle) bodyClass() string {
s := "" s := ""
if scene == plugins.Search { switch h.scene {
if d > 0 { case plugins.Search:
if len(h.ginH["posts"].([]models.Posts)) > 0 {
s = "search-results" s = "search-results"
} else { } else {
s = "search-no-results" s = "search-no-results"
} }
} else if scene == plugins.Category || scene == plugins.Tag { case plugins.Category:
s = fmt.Sprintf("category-%d %v", d, a[0]) cat := h.c.Param("category")
} else if scene == plugins.Detail { _, cate := slice.SearchFirst(cache.Categories(h.c), func(my models.TermsMy) bool {
s = fmt.Sprintf("postid-%d", d) return my.Name == cat
})
if cate.Slug[0] != '%' {
s = cate.Slug
}
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
case plugins.Tag:
cat := h.c.Param("tag")
_, cate := slice.SearchFirst(cache.Tags(h.c), func(my models.TermsMy) bool {
return my.Name == cat
})
if cate.Slug[0] != '%' {
s = cate.Slug
}
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
case plugins.Detail:
s = fmt.Sprintf("postid-%d", h.ginH["post"].(models.Posts).Id)
} }
return map[int]string{ return map[int]string{
plugins.Home: "home blog ", plugins.Home: "home blog ",
@ -157,5 +172,5 @@ func bodyClass(scene, d int, a ...any) string {
plugins.Tag: str.Join("archive category page-two-column ", s), plugins.Tag: str.Join("archive category page-two-column ", s),
plugins.Search: str.Join("search ", s), plugins.Search: str.Join("search ", s),
plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s), plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s),
}[scene] }[h.scene]
} }

View File

@ -3,7 +3,7 @@ package model
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/number" "golang.org/x/exp/constraints"
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
@ -87,7 +87,7 @@ func SimplePagination[T Model](ctx context.Context, where ParseWhere, fields, gr
return return
} }
func FindOneById[T Model, I number.IntNumber](ctx context.Context, id I) (T, error) { func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error) {
var r T var r T
sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey()) sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
err := globalBb.Get(ctx, &r, sq, id) err := globalBb.Get(ctx, &r, sq, id)