列表显示缩略图和手写的摘要
This commit is contained in:
parent
da2a61a606
commit
458d5b70ba
|
@ -20,6 +20,9 @@ var recentPostsCaches *cache.SliceCache[wp.Posts]
|
||||||
var recentCommentsCaches *cache.SliceCache[wp.Comments]
|
var recentCommentsCaches *cache.SliceCache[wp.Comments]
|
||||||
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
||||||
var postsCache *cache.MapCache[uint64, wp.Posts]
|
var postsCache *cache.MapCache[uint64, wp.Posts]
|
||||||
|
|
||||||
|
var postMetaCache *cache.MapCache[uint64, map[string]any]
|
||||||
|
|
||||||
var monthPostsCache *cache.MapCache[string, []uint64]
|
var monthPostsCache *cache.MapCache[string, []uint64]
|
||||||
var postListIdsCache *cache.MapCache[string, PostIds]
|
var postListIdsCache *cache.MapCache[string, PostIds]
|
||||||
var searchPostIdsCache *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)
|
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)
|
categoryCaches = cache.NewSliceCache[wp.TermsMy](categories, c.CategoryCacheTime)
|
||||||
|
|
||||||
recentPostsCaches = cache.NewSliceCache[wp.Posts](recentPosts, c.RecentPostCacheTime)
|
recentPostsCaches = cache.NewSliceCache[wp.Posts](recentPosts, c.RecentPostCacheTime)
|
||||||
|
@ -66,7 +71,7 @@ func InitActionsCommonCache() {
|
||||||
func ClearCache() {
|
func ClearCache() {
|
||||||
searchPostIdsCache.ClearExpired()
|
searchPostIdsCache.ClearExpired()
|
||||||
postsCache.ClearExpired()
|
postsCache.ClearExpired()
|
||||||
postsCache.ClearExpired()
|
postMetaCache.ClearExpired()
|
||||||
postListIdsCache.ClearExpired()
|
postListIdsCache.ClearExpired()
|
||||||
monthPostsCache.ClearExpired()
|
monthPostsCache.ClearExpired()
|
||||||
postContextCache.ClearExpired()
|
postContextCache.ClearExpired()
|
||||||
|
@ -77,7 +82,7 @@ func ClearCache() {
|
||||||
func FlushCache() {
|
func FlushCache() {
|
||||||
searchPostIdsCache.Flush()
|
searchPostIdsCache.Flush()
|
||||||
postsCache.Flush()
|
postsCache.Flush()
|
||||||
postsCache.Flush()
|
postMetaCache.Flush()
|
||||||
postListIdsCache.Flush()
|
postListIdsCache.Flush()
|
||||||
monthPostsCache.Flush()
|
monthPostsCache.Flush()
|
||||||
postContextCache.Flush()
|
postContextCache.Flush()
|
||||||
|
|
120
actions/common/postmeta.go
Normal file
120
actions/common/postmeta.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -62,6 +62,7 @@ func getPostsByIds(ids ...any) (m map[uint64]wp.Posts, err error) {
|
||||||
}
|
}
|
||||||
postsMap[post.Id] = v
|
postsMap[post.Id] = v
|
||||||
}
|
}
|
||||||
|
meta, _ := getPostMetaByPostIds(ctx, id)
|
||||||
for k, pp := range postsMap {
|
for k, pp := range postsMap {
|
||||||
if len(pp.Categories) > 0 {
|
if len(pp.Categories) > 0 {
|
||||||
t := make([]string, 0, len(pp.Categories))
|
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(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
|
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
|
||||||
}
|
}
|
||||||
pp.CategoriesHtml = strings.Join(t, "、")
|
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 {
|
if len(pp.Tags) > 0 {
|
||||||
t := make([]string, 0, len(pp.Tags))
|
t := make([]string, 0, len(pp.Tags))
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -25,6 +25,7 @@ require (
|
||||||
github.com/gorilla/securecookie v1.1.1 // indirect
|
github.com/gorilla/securecookie v1.1.1 // indirect
|
||||||
github.com/gorilla/sessions v1.2.1 // indirect
|
github.com/gorilla/sessions v1.2.1 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // 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/leodido/go-urn v1.2.1 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
|
2
go.sum
2
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.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 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
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 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
|
||||||
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
|
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=
|
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
|
16
models/wp/wp_postmeta.go
Normal file
16
models/wp/wp_postmeta.go
Normal file
|
@ -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"
|
||||||
|
}
|
|
@ -34,6 +34,13 @@ type Posts struct {
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
CategoriesHtml string
|
CategoriesHtml string
|
||||||
TagsHtml string
|
TagsHtml string
|
||||||
|
Thumbnail PostThumbnail
|
||||||
|
}
|
||||||
|
|
||||||
|
type PostThumbnail struct {
|
||||||
|
Path string
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Posts) PrimaryKey() string {
|
func (w Posts) PrimaryKey() string {
|
||||||
|
|
|
@ -123,6 +123,11 @@ func Digest(p *Plugin[wp.Posts], c *gin.Context, post *wp.Posts, scene uint) {
|
||||||
if scene == Detail {
|
if scene == Detail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post.PostContent = DigestCache(c, post.Id, post.PostContent)
|
if post.PostExcerpt != "" {
|
||||||
|
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
|
||||||
|
} else {
|
||||||
|
post.PostContent = DigestCache(c, post.Id, post.PostContent)
|
||||||
|
|
||||||
|
}
|
||||||
p.Next()
|
p.Next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,13 +17,19 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
{{ range $k,$v:=.posts}}
|
{{ range $k,$v:=.posts}}
|
||||||
<article id="post-{{$v.Id}}"
|
<article id="post-{{$v.Id}}"
|
||||||
class="post-{{$v.Id}} post type-post status-publish format-standard hentry category">
|
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}}
|
||||||
|
<a class="post-thumbnail" href="/p/{{$v.Id}}" aria-hidden="true">
|
||||||
|
<img width="{{$v.Thumbnail.Width}}" height="{{$v.Thumbnail.Height}}" src="{{"siteurl"| getOption}}/wp-content/uploads/{{$v.Thumbnail.Path}}" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="{{$v.PostTitle}}" decoding="async">
|
||||||
|
</a>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
<header class="entry-header">
|
<header class="entry-header">
|
||||||
<h2 class="entry-title">
|
<h2 class="entry-title">
|
||||||
<a href="/p/{{$v.Id}}" rel="bookmark">{{$v.PostTitle}}</a>
|
<a href="/p/{{$v.Id}}" rel="bookmark">{{$v.PostTitle}}</a>
|
||||||
</h2>
|
</h2>
|
||||||
</header><!-- .entry-header -->
|
</header>
|
||||||
|
<!-- .entry-header -->
|
||||||
|
|
||||||
<div class="entry-content">
|
<div class="entry-content">
|
||||||
{{$v.PostContent|unescaped}}
|
{{$v.PostContent|unescaped}}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user