diff --git a/actions/common/common.go b/actions/common/common.go index 4dc60e9..043f424 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -20,6 +20,9 @@ var recentPostsCaches *cache.SliceCache[wp.Posts] var recentCommentsCaches *cache.SliceCache[wp.Comments] var postCommentCaches *cache.MapCache[uint64, []uint64] var postsCache *cache.MapCache[uint64, wp.Posts] + +var postMetaCache *cache.MapCache[uint64, map[string]any] + var monthPostsCache *cache.MapCache[string, []uint64] var postListIdsCache *cache.MapCache[string, PostIds] var searchPostIdsCache *cache.MapCache[string, PostIds] @@ -46,6 +49,8 @@ func InitActionsCommonCache() { postsCache = cache.NewMapCacheByBatchFn[uint64, wp.Posts](getPostsByIds, c.PostDataCacheTime) + postMetaCache = cache.NewMapCacheByBatchFn[uint64, map[string]any](getPostMetaByPostIds, c.PostDataCacheTime) + categoryCaches = cache.NewSliceCache[wp.TermsMy](categories, c.CategoryCacheTime) recentPostsCaches = cache.NewSliceCache[wp.Posts](recentPosts, c.RecentPostCacheTime) @@ -66,7 +71,7 @@ func InitActionsCommonCache() { func ClearCache() { searchPostIdsCache.ClearExpired() postsCache.ClearExpired() - postsCache.ClearExpired() + postMetaCache.ClearExpired() postListIdsCache.ClearExpired() monthPostsCache.ClearExpired() postContextCache.ClearExpired() @@ -77,7 +82,7 @@ func ClearCache() { func FlushCache() { searchPostIdsCache.Flush() postsCache.Flush() - postsCache.Flush() + postMetaCache.Flush() postListIdsCache.Flush() monthPostsCache.Flush() postContextCache.Flush() diff --git a/actions/common/postmeta.go b/actions/common/postmeta.go new file mode 100644 index 0000000..17cce39 --- /dev/null +++ b/actions/common/postmeta.go @@ -0,0 +1,120 @@ +package common + +import ( + "context" + "github.com/leeqvip/gophp" + "github/fthvgb1/wp-go/helper" + "github/fthvgb1/wp-go/logs" + "github/fthvgb1/wp-go/models" + "github/fthvgb1/wp-go/models/wp" + "strconv" + "strings" + "time" +) + +func GetPostMetaByPostIds(ctx context.Context, ids []uint64) (r []map[string]any, err error) { + r, err = postMetaCache.GetCacheBatch(ctx, ids, time.Second, ctx, ids) + return +} +func GetPostMetaByPostId(ctx context.Context, id uint64) (r map[string]any, err error) { + r, err = postMetaCache.GetCache(ctx, id, time.Second, ctx, id) + return +} + +func getMetaVal(key string, v any) (r any) { + vv, ok := v.(map[string]any) + if !ok { + return + } + k := strings.Split(key, ".") + if len(k) > 1 { + val, ok := vv[k[0]] + if ok { + vx, ok := val.(map[string]any) + if ok { + r = getMetaVal(strings.Join(k[1:], "."), vx) + } + } + } else { + x, ok := vv[k[0]] + if ok { + r = x + } + } + return +} + +func ToPostThumbnail(c context.Context, postId uint64) (r wp.PostThumbnail) { + meta, err := GetPostMetaByPostId(c, postId) + if err == nil { + m, ok := meta["_thumbnail_id"] + if ok { + id, err := strconv.ParseUint(m.(string), 10, 64) + if err == nil { + mm, err := GetPostMetaByPostId(c, id) + if err == nil { + f, ok := mm["_wp_attached_file"] + if ok { + ff, ok := f.(string) + if ok && ff != "" { + r.Path = ff + } + } + tt := getMetaVal("_wp_attachment_metadata.sizes.post-thumbnail", mm) + if tt != nil { + sss, ok := tt.(map[string]any) + if ok { + width, ok := sss["width"] + if ok { + w, ok := width.(int) + if ok { + r.Width = w + } + } + height, ok := sss["height"] + if ok { + h, ok := height.(int) + if ok { + r.Height = h + } + } + } + } + } + } + } + } + return +} + +func getPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) { + r = make(map[uint64]map[string]any) + ctx := args[0].(context.Context) + ids := args[1].([]uint64) + rr, err := models.Find[wp.Postmeta](ctx, models.SqlBuilder{ + {"post_id", "in", ""}, + }, "*", "", nil, nil, nil, 0, helper.SliceMap(ids, helper.ToAny[uint64])) + if err != nil { + return + } + for _, postmeta := range rr { + if _, ok := r[postmeta.PostId]; !ok { + r[postmeta.PostId] = make(map[string]any) + } + if postmeta.MetaKey == "_wp_attachment_metadata" { + meta, err := gophp.Unserialize([]byte(postmeta.MetaValue)) + if err != nil { + logs.ErrPrintln(err, "反序列化postmeta失败", postmeta.MetaValue) + continue + } + metaVal, ok := meta.(map[string]any) + if ok { + r[postmeta.PostId][postmeta.MetaKey] = metaVal + } + } else { + r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue + } + + } + return +} diff --git a/actions/common/posts.go b/actions/common/posts.go index ab2d95d..eb2cc38 100644 --- a/actions/common/posts.go +++ b/actions/common/posts.go @@ -62,6 +62,7 @@ func getPostsByIds(ids ...any) (m map[uint64]wp.Posts, err error) { } postsMap[post.Id] = v } + meta, _ := getPostMetaByPostIds(ctx, id) for k, pp := range postsMap { if len(pp.Categories) > 0 { t := make([]string, 0, len(pp.Categories)) @@ -69,6 +70,13 @@ func getPostsByIds(ids ...any) (m map[uint64]wp.Posts, err error) { t = append(t, fmt.Sprintf(`%s`, cat, cat)) } pp.CategoriesHtml = strings.Join(t, "、") + _, ok := meta[pp.Id] + if ok { + thumb := ToPostThumbnail(ctx, pp.Id) + if thumb.Path != "" { + pp.Thumbnail = thumb + } + } } if len(pp.Tags) > 0 { t := make([]string, 0, len(pp.Tags)) diff --git a/go.mod b/go.mod index 9b7598b..a19ca2f 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/sessions v1.2.1 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/leeqvip/gophp v1.0.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 4e6ce5c..1e07ec5 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leeqvip/gophp v1.0.0 h1:lzWOECV3vkXjzrwoOVYyrfcAfOH+9BqIZLDIwxffy8o= +github.com/leeqvip/gophp v1.0.0/go.mod h1:DRoO5E9Sk+t4/3LGPCH4QZ/arcASXk9VsqdeTXLgYC4= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= diff --git a/models/wp/wp_postmeta.go b/models/wp/wp_postmeta.go new file mode 100644 index 0000000..81fca8d --- /dev/null +++ b/models/wp/wp_postmeta.go @@ -0,0 +1,16 @@ +package wp + +type Postmeta struct { + MetaId uint64 `db:"meta_id" json:"meta_id" form:"meta_id"` + PostId uint64 `db:"post_id" json:"post_id" form:"post_id"` + MetaKey string `db:"meta_key" json:"meta_key" form:"meta_key"` + MetaValue string `db:"meta_value" json:"meta_value" form:"meta_value"` +} + +func (p Postmeta) PrimaryKey() string { + return "meta_id" +} + +func (p Postmeta) Table() string { + return "wp_postmeta" +} diff --git a/models/wp/wp_posts.go b/models/wp/wp_posts.go index d1eb6c1..33adf75 100644 --- a/models/wp/wp_posts.go +++ b/models/wp/wp_posts.go @@ -34,6 +34,13 @@ type Posts struct { Tags []string `json:"tags"` CategoriesHtml string TagsHtml string + Thumbnail PostThumbnail +} + +type PostThumbnail struct { + Path string + Width int + Height int } func (w Posts) PrimaryKey() string { diff --git a/plugins/digest.go b/plugins/digest.go index 83ef8da..bf5efa8 100644 --- a/plugins/digest.go +++ b/plugins/digest.go @@ -123,6 +123,11 @@ func Digest(p *Plugin[wp.Posts], c *gin.Context, post *wp.Posts, scene uint) { if scene == Detail { return } - post.PostContent = DigestCache(c, post.Id, post.PostContent) + if post.PostExcerpt != "" { + post.PostContent = strings.Replace(post.PostExcerpt, "\n", "
", -1) + } else { + post.PostContent = DigestCache(c, post.Id, post.PostContent) + + } p.Next() } diff --git a/templates/twentyfifteen/posts/index.gohtml b/templates/twentyfifteen/posts/index.gohtml index 3bf7ea1..fe2061e 100644 --- a/templates/twentyfifteen/posts/index.gohtml +++ b/templates/twentyfifteen/posts/index.gohtml @@ -17,13 +17,19 @@ {{end}} {{ range $k,$v:=.posts}}
+ class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category"> + {{if $v.Thumbnail.Path}} + + {{end}}

{{$v.PostTitle}}

-
+ +
{{$v.PostContent|unescaped}}