wp-go/internal/pkg/cache/headerImages.go

82 lines
2.1 KiB
Go
Raw Normal View History

2023-01-31 16:58:42 +00:00
package cache
import (
"context"
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
"time"
)
func GetHeaderImages(ctx context.Context, theme string) (r []models.PostThumbnail, err error) {
r, err = headerImagesCache.GetCache(ctx, theme, time.Second, ctx, theme)
return
}
func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
ctx := a[0].(context.Context)
theme := a[1].(string)
2023-02-14 11:47:47 +00:00
meta, err := wpconfig.GetThemeMods(theme)
if err != nil || meta.HeaderImage == "" {
return
}
if "random-uploaded-image" != meta.HeaderImage {
2023-01-31 16:58:42 +00:00
m, er := GetPostById(ctx, uint64(meta.HeaderImagData.AttachmentId))
if er != nil {
err = er
return
}
m.Thumbnail = thumb(m, theme)
r = []models.PostThumbnail{m.Thumbnail}
return
}
headers, er := model.Finds[models.Posts](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"post_type", "attachment"},
{"post_status", "inherit"},
{"meta_value", theme},
{"meta_key", "_wp_attachment_is_custom_header"},
}),
model.Fields("a.ID"),
model.Group("a.ID"),
model.Join(model.SqlBuilder{
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
}),
))
if er != nil {
err = er
return
}
if len(headers) > 0 {
posts, er := GetPostsByIds(ctx, slice.Map(headers, func(t models.Posts) uint64 {
return t.Id
}))
if er != nil {
err = er
return
}
r = slice.Map(posts, func(m models.Posts) models.PostThumbnail {
return thumb(m, theme)
})
2023-01-31 16:58:42 +00:00
}
return
2023-01-31 16:58:42 +00:00
}
func thumb(m models.Posts, theme string) models.PostThumbnail {
2023-02-14 11:47:47 +00:00
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
2023-01-31 16:58:42 +00:00
m.Thumbnail.Width = m.AttachmentMetadata.Width
m.Thumbnail.Height = m.AttachmentMetadata.Height
if m.Thumbnail.Path != "" {
if len(m.AttachmentMetadata.Sizes) > 0 {
m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset)
}
}
return m.Thumbnail
}