小优化
This commit is contained in:
parent
b1e824b27d
commit
1451b33af4
14
internal/pkg/cache/headerImages.go
vendored
14
internal/pkg/cache/headerImages.go
vendored
|
@ -22,20 +22,26 @@ func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
|
||||||
theme := a[1].(string)
|
theme := a[1].(string)
|
||||||
mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme))
|
mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme))
|
||||||
if ok && mods != "" {
|
if ok && mods != "" {
|
||||||
meta, er := plugins.UnPHPSerialize[plugins.HeaderImageMeta](mods)
|
meta, er := plugins.UnPHPSerialize[plugins.ThemeMods](mods)
|
||||||
if er != nil || meta.HeaderImage == "" {
|
if er != nil || meta.HeaderImage == "" {
|
||||||
err = er
|
err = er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if "random-uploaded-image" == meta.HeaderImage {
|
if "random-uploaded-image" == meta.HeaderImage {
|
||||||
headers, er := model.Find[models.Posts](ctx, model.SqlBuilder{
|
headers, er := model.Finds[models.Posts](ctx, model.Conditions(
|
||||||
|
model.Where(model.SqlBuilder{
|
||||||
{"post_type", "attachment"},
|
{"post_type", "attachment"},
|
||||||
{"post_status", "inherit"},
|
{"post_status", "inherit"},
|
||||||
{"meta_value", theme},
|
{"meta_value", theme},
|
||||||
{"meta_key", "_wp_attachment_is_custom_header"},
|
{"meta_key", "_wp_attachment_is_custom_header"},
|
||||||
}, "a.ID", "a.ID", nil, model.SqlBuilder{
|
}),
|
||||||
|
model.Fields("a.ID"),
|
||||||
|
model.Group("a.ID"),
|
||||||
|
model.Join(model.SqlBuilder{
|
||||||
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
|
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
|
||||||
}, nil, 0)
|
}),
|
||||||
|
))
|
||||||
|
|
||||||
if er != nil {
|
if er != nil {
|
||||||
err = er
|
err = er
|
||||||
return
|
return
|
||||||
|
|
|
@ -7,10 +7,18 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HeaderImageMeta struct {
|
type ThemeMods struct {
|
||||||
CustomCssPostId int `json:"custom_css_post_id,omitempty"`
|
CustomCssPostId int `json:"custom_css_post_id,omitempty"`
|
||||||
NavMenuLocations []string `json:"nav_menu_locations,omitempty"`
|
NavMenuLocations []string `json:"nav_menu_locations,omitempty"`
|
||||||
HeaderImage string `json:"header_image,omitempty"`
|
HeaderImage string `json:"header_image,omitempty"`
|
||||||
|
BackgroundImage string `json:"background_image"`
|
||||||
|
BackgroundSize string `json:"background_size"`
|
||||||
|
BackgroundRepeat string `json:"background_repeat"`
|
||||||
|
BackgroundColor string `json:"background_color"`
|
||||||
|
ColorScheme string `json:"color_scheme"`
|
||||||
|
SidebarTextcolor string `json:"sidebar_textcolor"`
|
||||||
|
HeaderBackgroundColor string `json:"header_background_color"`
|
||||||
|
HeaderTextcolor string `json:"header_textcolor"`
|
||||||
HeaderImagData ImageData `json:"header_image_data,omitempty"`
|
HeaderImagData ImageData `json:"header_image_data,omitempty"`
|
||||||
SidebarsWidgets Sidebars `json:"sidebars_widgets"`
|
SidebarsWidgets Sidebars `json:"sidebars_widgets"`
|
||||||
}
|
}
|
|
@ -1,9 +1,15 @@
|
||||||
package twentyfifteen
|
package twentyfifteen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,7 +43,8 @@ func Hook(h *common.Handle) {
|
||||||
|
|
||||||
func (i *indexHandle) Index() {
|
func (i *indexHandle) Index() {
|
||||||
i.Templ = "twentyfifteen/posts/index.gohtml"
|
i.Templ = "twentyfifteen/posts/index.gohtml"
|
||||||
|
img := getHeaderImage(i.C)
|
||||||
|
fmt.Println(img)
|
||||||
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
i.Stats = constraints.Error404
|
i.Stats = constraints.Error404
|
||||||
|
@ -66,3 +73,18 @@ func (d *detailHandle) Detail() {
|
||||||
d.RenderComment()
|
d.RenderComment()
|
||||||
d.C.HTML(d.Code, d.Templ, d.GinH)
|
d.C.HTML(d.Code, d.Templ, d.GinH)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
|
||||||
|
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
|
||||||
|
r.Width = 2000
|
||||||
|
r.Height = 1200
|
||||||
|
hs, err := cache.GetHeaderImages(c, ThemeName)
|
||||||
|
if err != nil {
|
||||||
|
logs.ErrPrintln(err, "获取页眉背景图失败")
|
||||||
|
} else if len(hs) > 0 && err == nil {
|
||||||
|
_, r = slice.Rand(hs)
|
||||||
|
|
||||||
|
}
|
||||||
|
r.Sizes = "100vw"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user