wp-go/internal/dao/postmeta.go

86 lines
2.1 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package common
import (
"context"
"github.com/leeqvip/gophp"
"github/fthvgb1/wp-go/helper"
2023-01-13 06:04:32 +00:00
"github/fthvgb1/wp-go/internal/pkg/logs"
models2 "github/fthvgb1/wp-go/internal/pkg/models"
2023-01-12 12:42:16 +00:00
"github/fthvgb1/wp-go/model"
"strconv"
)
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)
2023-01-13 06:04:32 +00:00
rr, err := model.Find[models2.Postmeta](ctx, model.SqlBuilder{
2023-01-12 12:42:16 +00:00
{"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
}
2023-01-13 06:04:32 +00:00
func ToPostThumb(c context.Context, meta map[string]any, postId uint64) (r models2.PostThumbnail) {
2023-01-12 12:42:16 +00:00
if meta != nil {
m, ok := meta["_thumbnail_id"]
if ok {
id, err := strconv.ParseUint(m.(string), 10, 64)
if err == nil {
mx, err := GetPostMetaByPostIds(c, []uint64{id})
if err == nil && mx != nil {
mm, ok := mx[id]
if ok && mm != nil {
f, ok := mm["_wp_attached_file"]
if ok {
ff, ok := f.(string)
if ok && ff != "" {
r.Path = ff
}
}
tt, ok := helper.GetStrMapAnyVal[map[string]any]("_wp_attachment_metadata.sizes.post-thumbnail", mm)
if ok && tt != nil {
width, ok := tt["width"]
if ok {
w, ok := width.(int)
if ok {
r.Width = w
}
}
height, ok := tt["height"]
if ok {
h, ok := height.(int)
if ok {
r.Height = h
}
}
}
}
}
}
}
}
return
}