计算body class
This commit is contained in:
parent
1451b33af4
commit
f96f364a69
|
@ -3,7 +3,7 @@ package actions
|
|||
import (
|
||||
"fmt"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/phpass"
|
||||
"github.com/fthvgb1/wp-go/internal/phphelper"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -28,7 +28,7 @@ func Login(c *gin.Context) {
|
|||
c.Error(err)
|
||||
return
|
||||
}
|
||||
pass, err := phpass.NewPasswordHash(8, true).HashPassword(password)
|
||||
pass, err := phphelper.NewPasswordHash(8, true).HashPassword(password)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
func ThemeHook(scene int) func(*gin.Context) {
|
||||
return func(ctx *gin.Context) {
|
||||
theme.Hook(theme.GetTemplateName(), common.NewHandle(ctx, scene))
|
||||
t := theme.GetTemplateName()
|
||||
theme.Hook(t, common.NewHandle(ctx, scene, t))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ func init() {
|
|||
}
|
||||
cache.InitActionsCommonCache()
|
||||
plugins.InitDigestCache()
|
||||
theme.InitThemeAndTemplateFuncMap()
|
||||
theme.InitTheme()
|
||||
go cronClearCache()
|
||||
}
|
||||
|
||||
|
@ -105,6 +105,7 @@ func reload() {
|
|||
err = wpconfig.InitOptions()
|
||||
logs.ErrPrintln(err, "获取网站设置WpOption失败")
|
||||
err = wpconfig.InitTerms()
|
||||
wpconfig.FlushModes()
|
||||
logs.ErrPrintln(err, "获取WpTerms表失败")
|
||||
if middleWareReloadFn != nil {
|
||||
middleWareReloadFn()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package phpass
|
||||
package phphelper
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
|
@ -1,4 +1,4 @@
|
|||
package plugins
|
||||
package phphelper
|
||||
|
||||
import (
|
||||
"github.com/elliotchance/phpserialize"
|
12
internal/pkg/cache/headerImages.go
vendored
12
internal/pkg/cache/headerImages.go
vendored
|
@ -6,7 +6,6 @@ import (
|
|||
"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/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"time"
|
||||
|
@ -20,13 +19,8 @@ func GetHeaderImages(ctx context.Context, theme string) (r []models.PostThumbnai
|
|||
func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
|
||||
ctx := a[0].(context.Context)
|
||||
theme := a[1].(string)
|
||||
mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme))
|
||||
if ok && mods != "" {
|
||||
meta, er := plugins.UnPHPSerialize[plugins.ThemeMods](mods)
|
||||
if er != nil || meta.HeaderImage == "" {
|
||||
err = er
|
||||
return
|
||||
}
|
||||
meta, err := wpconfig.GetThemeMods(theme)
|
||||
if err != nil && meta.HeaderImage != "" {
|
||||
if "random-uploaded-image" == meta.HeaderImage {
|
||||
headers, er := model.Finds[models.Posts](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
|
@ -72,7 +66,7 @@ func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
|
|||
}
|
||||
|
||||
func thumb(m models.Posts, theme string) models.PostThumbnail {
|
||||
m.Thumbnail = plugins.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
|
||||
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
|
||||
m.Thumbnail.Width = m.AttachmentMetadata.Width
|
||||
m.Thumbnail.Height = m.AttachmentMetadata.Height
|
||||
if m.Thumbnail.Path != "" {
|
||||
|
|
|
@ -3,9 +3,10 @@ package dao
|
|||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/phphelper"
|
||||
"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/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -27,7 +28,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error)
|
|||
}
|
||||
r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue
|
||||
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
||||
metadata, err := plugins.UnPHPSerialize[models.WpAttachmentMetadata](postmeta.MetaValue)
|
||||
metadata, err := phphelper.UnPHPSerialize[models.WpAttachmentMetadata](postmeta.MetaValue)
|
||||
if err != nil {
|
||||
logs.ErrPrintln(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue)
|
||||
continue
|
||||
|
@ -60,7 +61,7 @@ func ToPostThumb(c context.Context, meta map[string]any, host string) (r models.
|
|||
if ok {
|
||||
metadata, ok := x.(models.WpAttachmentMetadata)
|
||||
if ok {
|
||||
r = plugins.Thumbnail(metadata, "post-thumbnail", host, "thumbnail")
|
||||
r = wpconfig.Thumbnail(metadata, "post-thumbnail", host, "thumbnail")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
@ -70,7 +70,7 @@ func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) {
|
|||
pp.Thumbnail = thumb
|
||||
}
|
||||
} else if pp.PostType == "attachment" && pp.AttachmentMetadata.File != "" {
|
||||
thumb := plugins.Thumbnail(pp.AttachmentMetadata, "thumbnail", host, "thumbnail", "post-thumbnail")
|
||||
thumb := wpconfig.Thumbnail(pp.AttachmentMetadata, "thumbnail", host, "thumbnail", "post-thumbnail")
|
||||
if thumb.Path != "" {
|
||||
pp.Thumbnail = thumb
|
||||
}
|
||||
|
|
84
internal/theme/common/bodyclass.go
Normal file
84
internal/theme/common/bodyclass.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var commonClass = map[int]string{
|
||||
constraints.Home: "home blog",
|
||||
constraints.Archive: "archive date",
|
||||
constraints.Category: "archive category ",
|
||||
constraints.Tag: "archive category ",
|
||||
constraints.Search: "search",
|
||||
constraints.Detail: "post-template-default single single-post ",
|
||||
}
|
||||
|
||||
type Support map[string]struct{}
|
||||
|
||||
var themeSupport = map[string]Support{}
|
||||
|
||||
func AddThemeSupport(theme string, support Support) {
|
||||
themeSupport[theme] = support
|
||||
}
|
||||
|
||||
func (h *Handle) IsSupport(name string) bool {
|
||||
m, ok := themeSupport[h.Theme]
|
||||
if ok {
|
||||
return maps.IsExists(m, name)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (h *Handle) CalBodyClass() {
|
||||
h.GinH["bodyClass"] = h.bodyClass(h.Class...)
|
||||
}
|
||||
|
||||
func (h *Handle) bodyClass(class ...string) string {
|
||||
s := ""
|
||||
if constraints.Ok != h.Stats {
|
||||
return "error404"
|
||||
}
|
||||
switch h.Scene {
|
||||
case constraints.Search:
|
||||
s = "search-no-results"
|
||||
if len(h.GinH["posts"].([]models.Posts)) > 0 {
|
||||
s = "search-results"
|
||||
}
|
||||
case constraints.Category, constraints.Tag:
|
||||
cat := h.C.Param("category")
|
||||
if cat == "" {
|
||||
cat = h.C.Param("tag")
|
||||
}
|
||||
_, cate := slice.SearchFirst(cache.CategoriesTags(h.C, h.Scene), func(my models.TermsMy) bool {
|
||||
return my.Name == cat
|
||||
})
|
||||
if cate.Slug[0] != '%' {
|
||||
s = cate.Slug
|
||||
}
|
||||
s = fmt.Sprintf("category-%v category-%v", s, cate.Terms.TermId)
|
||||
case constraints.Detail:
|
||||
s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id)
|
||||
if h.IsSupport("post-formats") {
|
||||
s = str.Join(s, " single-format-standard")
|
||||
}
|
||||
}
|
||||
class = append(class, s)
|
||||
if h.IsSupport("custom-background") && wpconfig.IsCustomBackground(h.Theme) {
|
||||
class = append(class, "custom-background")
|
||||
}
|
||||
if h.IsSupport("custom-logo") && wpconfig.IsCustomLogo(h.Theme) {
|
||||
class = append(class, "wp-custom-logo")
|
||||
}
|
||||
if h.IsSupport("responsive-embeds") {
|
||||
class = append(class, "wp-embed-responsive")
|
||||
}
|
||||
return str.Join(commonClass[h.Scene], strings.Join(class, " "))
|
||||
}
|
|
@ -18,6 +18,7 @@ import (
|
|||
|
||||
type Handle struct {
|
||||
C *gin.Context
|
||||
Theme string
|
||||
Session sessions.Session
|
||||
GinH gin.H
|
||||
Password string
|
||||
|
@ -25,11 +26,13 @@ type Handle struct {
|
|||
Code int
|
||||
Stats int
|
||||
Templ string
|
||||
Class []string
|
||||
}
|
||||
|
||||
func NewHandle(c *gin.Context, scene int) *Handle {
|
||||
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||
return &Handle{
|
||||
C: c,
|
||||
Theme: theme,
|
||||
Session: sessions.Default(c),
|
||||
GinH: gin.H{},
|
||||
Scene: scene,
|
||||
|
|
|
@ -80,7 +80,7 @@ func (i *IndexParams) setTitleLR(l, r string) {
|
|||
}
|
||||
|
||||
func (i *IndexParams) getTitle() string {
|
||||
i.Title = fmt.Sprintf("%s-%s", i.TitleL, i.TitleR)
|
||||
i.Title = fmt.Sprintf("%s - %s", i.TitleL, i.TitleR)
|
||||
return i.Title
|
||||
}
|
||||
|
||||
|
|
|
@ -2,14 +2,16 @@ package theme
|
|||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/twentyfifteen"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/twentyseventeen"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
)
|
||||
|
||||
func InitThemeAndTemplateFuncMap() {
|
||||
func InitTheme() {
|
||||
addThemeHookFunc(twentyfifteen.ThemeName, twentyfifteen.Hook)
|
||||
addThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook)
|
||||
common.AddThemeSupport(twentyfifteen.ThemeName, twentyfifteen.ThemeSupport())
|
||||
}
|
||||
|
||||
func GetTemplateName() string {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
{{block "head" .}}
|
||||
{{end}}
|
||||
</head>
|
||||
<body>
|
||||
<body class="{{.bodyClass}}">
|
||||
{{template "svg"}}
|
||||
<div id="page" class="hfeed site">
|
||||
<a class="skip-link screen-reader-text" href="#content">
|
||||
|
|
|
@ -55,6 +55,7 @@ func (i *indexHandle) Index() {
|
|||
i.ExecPostsPlugin()
|
||||
i.PageEle = plugins.TwentyFifteenPagination()
|
||||
i.Pagination()
|
||||
i.CalBodyClass()
|
||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||
}
|
||||
|
||||
|
@ -71,6 +72,7 @@ func (d *detailHandle) Detail() {
|
|||
d.PasswordProject()
|
||||
d.CommentRender = plugins.CommentRender()
|
||||
d.RenderComment()
|
||||
d.CalBodyClass()
|
||||
d.C.HTML(d.Code, d.Templ, d.GinH)
|
||||
}
|
||||
|
||||
|
@ -88,3 +90,12 @@ func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
|
|||
r.Sizes = "100vw"
|
||||
return
|
||||
}
|
||||
|
||||
func ThemeSupport() map[string]struct{} {
|
||||
return map[string]struct{}{
|
||||
"custom-background": {},
|
||||
"wp-custom-logo": {},
|
||||
"responsive-embeds": {},
|
||||
"post-formats": {},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -98,7 +99,7 @@ func (d *detailHandle) Detail() {
|
|||
return
|
||||
}
|
||||
d.GinH["bodyClass"] = d.h.bodyClass()
|
||||
img := plugins.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail")
|
||||
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail")
|
||||
img.Width = img.OriginAttachmentData.Width
|
||||
img.Height = img.OriginAttachmentData.Height
|
||||
img.Sizes = "100vw"
|
||||
|
|
|
@ -1,15 +1,18 @@
|
|||
package plugins
|
||||
package wpconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/internal/phphelper"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ThemeMods struct {
|
||||
CustomCssPostId int `json:"custom_css_post_id,omitempty"`
|
||||
NavMenuLocations []string `json:"nav_menu_locations,omitempty"`
|
||||
CustomLogo int `json:"custom_logo"`
|
||||
HeaderImage string `json:"header_image,omitempty"`
|
||||
BackgroundImage string `json:"background_image"`
|
||||
BackgroundSize string `json:"background_size"`
|
||||
|
@ -70,3 +73,50 @@ func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except .
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
var themeModes = safety.Map[string, ThemeMods]{}
|
||||
|
||||
func FlushModes() {
|
||||
themeModes.Flush()
|
||||
}
|
||||
|
||||
func GetThemeMods(theme string) (r ThemeMods, err error) {
|
||||
r, ok := themeModes.Load(theme)
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
|
||||
mods, ok := Options.Load(fmt.Sprintf("theme_mods_%s", theme))
|
||||
if !ok || mods == "" {
|
||||
return
|
||||
}
|
||||
r, err = phphelper.UnPHPSerialize[ThemeMods](mods)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
themeModes.Store(theme, r)
|
||||
return
|
||||
}
|
||||
|
||||
func IsCustomBackground(theme string) bool {
|
||||
mods, err := GetThemeMods(theme)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if mods.BackgroundColor != "" && mods.BackgroundColor != "default-color" || mods.BackgroundImage != "" && mods.BackgroundImage != "default-image" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func IsCustomLogo(theme string) bool {
|
||||
mods, err := GetThemeMods(theme)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if mods.CustomLogo > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue
Block a user