2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-16 06:36:43 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-29 15:31:16 +00:00
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/cmd/reload"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-02-16 06:36:43 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-03-29 15:31:16 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"github.com/fthvgb1/wp-go/model"
|
2023-02-16 06:36:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *Handle) DisplayHeaderText() bool {
|
2023-02-28 15:38:23 +00:00
|
|
|
return h.themeMods.ThemeSupport.CustomHeader.HeaderText && "blank" != h.themeMods.HeaderTextcolor
|
2023-02-16 06:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
|
2023-03-29 15:31:16 +00:00
|
|
|
var err error
|
2023-04-04 15:02:54 +00:00
|
|
|
img := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail {
|
2023-03-29 15:31:16 +00:00
|
|
|
hs, er := h.GetHeaderImages(h.theme)
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return hs
|
|
|
|
})
|
2023-02-16 06:36:43 +00:00
|
|
|
if err != nil {
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.Error(err, "获取页眉背景图失败")
|
2023-02-16 06:36:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-04 15:02:54 +00:00
|
|
|
hs := slice.Copy(img)
|
2023-03-29 15:31:16 +00:00
|
|
|
|
2023-02-16 06:36:43 +00:00
|
|
|
if len(hs) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(hs) > 1 {
|
|
|
|
isRand = true
|
|
|
|
}
|
|
|
|
r, _ = slice.RandPop(&hs)
|
|
|
|
return
|
|
|
|
}
|
2023-03-29 15:31:16 +00:00
|
|
|
|
|
|
|
func (h *Handle) GetHeaderImages(theme string) (r []models.PostThumbnail, err error) {
|
|
|
|
meta, err := wpconfig.GetThemeMods(theme)
|
|
|
|
if err != nil || meta.HeaderImage == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if "random-uploaded-image" != meta.HeaderImage {
|
|
|
|
m, er := cache.GetPostById(h.C, 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](h.C, 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 := cache.GetPostsByIds(h.C, 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func thumb(m models.Posts, theme string) models.PostThumbnail {
|
2023-04-19 07:27:58 +00:00
|
|
|
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "full", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
|
2023-03-29 15:31:16 +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
|
|
|
|
}
|