diff --git a/internal/actions/login.go b/internal/actions/login.go index f076c1c..343184d 100644 --- a/internal/actions/login.go +++ b/internal/actions/login.go @@ -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 diff --git a/internal/actions/themehook.go b/internal/actions/themehook.go index c1d3a7d..6b2edd9 100644 --- a/internal/actions/themehook.go +++ b/internal/actions/themehook.go @@ -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)) } } diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 4fabea3..1be0851 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -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() diff --git a/internal/phpass/phpass.go b/internal/phphelper/phpass.go similarity index 99% rename from internal/phpass/phpass.go rename to internal/phphelper/phpass.go index 4b412cd..887ca55 100644 --- a/internal/phpass/phpass.go +++ b/internal/phphelper/phpass.go @@ -1,4 +1,4 @@ -package phpass +package phphelper import ( "crypto/md5" diff --git a/internal/plugins/phpserialize.go b/internal/phphelper/unserialize.go similarity index 94% rename from internal/plugins/phpserialize.go rename to internal/phphelper/unserialize.go index c1cd45c..dc35bbf 100644 --- a/internal/plugins/phpserialize.go +++ b/internal/phphelper/unserialize.go @@ -1,4 +1,4 @@ -package plugins +package phphelper import ( "github.com/elliotchance/phpserialize" diff --git a/internal/pkg/cache/headerImages.go b/internal/pkg/cache/headerImages.go index fd8ff00..19fc8ba 100644 --- a/internal/pkg/cache/headerImages.go +++ b/internal/pkg/cache/headerImages.go @@ -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 != "" { diff --git a/internal/pkg/dao/postmeta.go b/internal/pkg/dao/postmeta.go index dba8891..195139d 100644 --- a/internal/pkg/dao/postmeta.go +++ b/internal/pkg/dao/postmeta.go @@ -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") } } } diff --git a/internal/pkg/dao/posts.go b/internal/pkg/dao/posts.go index a767ed1..310eabe 100644 --- a/internal/pkg/dao/posts.go +++ b/internal/pkg/dao/posts.go @@ -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 } diff --git a/internal/theme/common/bodyclass.go b/internal/theme/common/bodyclass.go new file mode 100644 index 0000000..06fb5ad --- /dev/null +++ b/internal/theme/common/bodyclass.go @@ -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, " ")) +} diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 73f332b..c5eaf0d 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -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, diff --git a/internal/theme/common/indexparams.go b/internal/theme/common/indexparams.go index bb10aa1..24d972b 100644 --- a/internal/theme/common/indexparams.go +++ b/internal/theme/common/indexparams.go @@ -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 } diff --git a/internal/theme/theme.go b/internal/theme/theme.go index ea6236b..8f7cb1d 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -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 { diff --git a/internal/theme/twentyfifteen/layout/base.gohtml b/internal/theme/twentyfifteen/layout/base.gohtml index 91ccebe..8e2fba5 100644 --- a/internal/theme/twentyfifteen/layout/base.gohtml +++ b/internal/theme/twentyfifteen/layout/base.gohtml @@ -6,7 +6,7 @@ {{block "head" .}} {{end}} - + {{template "svg"}}