2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-14 11:47:47 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-15 16:57:50 +00:00
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/constraints"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-02-26 05:55:05 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-02-14 11:47:47 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-04-16 08:37:27 +00:00
|
|
|
"strconv"
|
2023-02-14 11:47:47 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-04-24 13:51:43 +00:00
|
|
|
func bodyClass(h *Handle) func() string {
|
|
|
|
return func() string {
|
|
|
|
return h.BodyClass()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 05:22:21 +00:00
|
|
|
func (h *Handle) BodyClass() string {
|
|
|
|
var class []string
|
2023-02-14 11:47:47 +00:00
|
|
|
if constraints.Ok != h.Stats {
|
2023-02-27 14:58:35 +00:00
|
|
|
class = append(class, "error404")
|
2023-02-14 11:47:47 +00:00
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
switch h.scene {
|
2023-02-26 14:20:20 +00:00
|
|
|
case constraints.Home:
|
|
|
|
class = append(class, "home", "blog")
|
|
|
|
|
|
|
|
case constraints.Archive:
|
|
|
|
class = append(class, "archive", "date")
|
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
case constraints.Search:
|
2023-02-26 05:55:05 +00:00
|
|
|
s := "search-no-results"
|
|
|
|
if len(h.Index.Posts) > 0 {
|
2023-02-14 11:47:47 +00:00
|
|
|
s = "search-results"
|
|
|
|
}
|
2023-02-26 14:20:20 +00:00
|
|
|
class = append(class, "search", s)
|
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
case constraints.Category, constraints.Tag:
|
2023-02-26 14:20:20 +00:00
|
|
|
class = append(class, "archive", "category")
|
2023-02-26 05:55:05 +00:00
|
|
|
cat := h.Index.Param.Category
|
2023-03-16 16:45:04 +00:00
|
|
|
if cat == "" {
|
|
|
|
break
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
_, cate := slice.SearchFirst(cache.CategoriesTags(h.C, h.scene), func(my models.TermsMy) bool {
|
2023-02-14 11:47:47 +00:00
|
|
|
return my.Name == cat
|
|
|
|
})
|
|
|
|
if cate.Slug[0] != '%' {
|
2023-02-26 05:55:05 +00:00
|
|
|
class = append(class, str.Join("category-", cate.Slug))
|
|
|
|
}
|
|
|
|
class = append(class, str.Join("category-", number.ToString(cate.Terms.TermId)))
|
|
|
|
|
|
|
|
case constraints.Author:
|
2023-02-26 14:20:20 +00:00
|
|
|
class = append(class, "archive", "author")
|
2023-02-26 05:55:05 +00:00
|
|
|
author := h.Index.Param.Author
|
|
|
|
user, _ := cache.GetUserByName(h.C, author)
|
|
|
|
class = append(class, str.Join("author-", number.ToString(user.Id)))
|
|
|
|
if user.UserLogin[0] != '%' {
|
|
|
|
class = append(class, str.Join("author-", user.UserLogin))
|
2023-02-14 11:47:47 +00:00
|
|
|
}
|
2023-02-26 05:55:05 +00:00
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
case constraints.Detail:
|
2023-02-26 14:20:20 +00:00
|
|
|
class = append(class, "post-template-default", "single", "single-post")
|
2023-02-26 05:55:05 +00:00
|
|
|
class = append(class, str.Join("postid-", number.ToString(h.Detail.Post.Id)))
|
2023-02-28 15:38:23 +00:00
|
|
|
if len(h.themeMods.ThemeSupport.PostFormats) > 0 {
|
2023-02-26 05:55:05 +00:00
|
|
|
class = append(class, "single-format-standard")
|
2023-02-14 11:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
if wpconfig.IsCustomBackground(h.theme) {
|
2023-02-18 15:35:39 +00:00
|
|
|
class = append(class, "custom-background")
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
if h.themeMods.CustomLogo > 0 || str.ToInteger(wpconfig.GetOption("site_logo"), 0) > 0 {
|
2023-02-18 15:35:39 +00:00
|
|
|
class = append(class, "wp-custom-logo")
|
|
|
|
}
|
2023-02-28 15:38:23 +00:00
|
|
|
if h.themeMods.ThemeSupport.ResponsiveEmbeds {
|
2023-02-18 15:35:39 +00:00
|
|
|
class = append(class, "wp-embed-responsive")
|
2023-02-14 11:47:47 +00:00
|
|
|
}
|
2023-04-15 05:22:21 +00:00
|
|
|
return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " "))
|
2023-02-14 11:47:47 +00:00
|
|
|
}
|
2023-04-24 13:51:43 +00:00
|
|
|
|
|
|
|
func postClass(h *Handle) func(posts models.Posts) string {
|
|
|
|
return func(posts models.Posts) string {
|
|
|
|
return h.PostClass(posts)
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 16:57:50 +00:00
|
|
|
func (h *Handle) PostClass(posts models.Posts) string {
|
|
|
|
var class []string
|
|
|
|
class = append(class, fmt.Sprintf("post-%d", posts.Id), posts.PostType,
|
|
|
|
str.Join("type-", posts.PostType), str.Join("status-", posts.PostStatus),
|
|
|
|
"hentry", "format-standard")
|
|
|
|
if h.CommonThemeMods().ThemeSupport.PostThumbnails && posts.Thumbnail.Path != "" {
|
|
|
|
class = append(class, "has-post-thumbnail")
|
|
|
|
}
|
|
|
|
|
|
|
|
if posts.PostPassword != "" {
|
2023-05-03 15:57:49 +00:00
|
|
|
if h.GetPassword() != posts.PostPassword {
|
2023-04-15 16:57:50 +00:00
|
|
|
class = append(class, "post-password-required")
|
|
|
|
} else {
|
|
|
|
class = append(class, "post-password-projected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.scene == constraints.Home && h.IsStick(posts.Id) {
|
|
|
|
class = append(class, "sticky")
|
|
|
|
}
|
|
|
|
for _, id := range posts.TermIds {
|
|
|
|
term, ok := wpconfig.GetTermMy(id)
|
|
|
|
if !ok || term.Slug == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-16 08:37:27 +00:00
|
|
|
class = append(class, TermClass(term))
|
2023-04-15 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.ComponentFilterFnHook("postClass", strings.Join(class, " "))
|
|
|
|
}
|
2023-04-16 08:37:27 +00:00
|
|
|
|
|
|
|
func TermClass(term models.TermsMy) string {
|
|
|
|
termClass := term.Slug
|
|
|
|
if strings.Contains(term.Slug, "%") {
|
|
|
|
termClass = strconv.FormatUint(term.TermTaxonomy.TermId, 10)
|
|
|
|
}
|
|
|
|
switch term.Taxonomy {
|
|
|
|
case "post_tag":
|
|
|
|
return str.Join("tag-", termClass)
|
|
|
|
case "post_format":
|
|
|
|
return fmt.Sprintf("format-%s", strings.ReplaceAll(term.Slug, "post-format-", ""))
|
|
|
|
}
|
2023-04-17 09:24:42 +00:00
|
|
|
return str.Join(term.Taxonomy, "-", termClass)
|
2023-04-16 08:37:27 +00:00
|
|
|
}
|