diff --git a/helper/func.go b/helper/func.go index 302ca66..ff53435 100644 --- a/helper/func.go +++ b/helper/func.go @@ -8,6 +8,13 @@ func ToAny[T any](v T) any { return v } +func Or[T any](is bool, left, right T) T { + if is { + return left + } + return right +} + func StructColumnToSlice[T any, M any](arr []M, field string) (r []T) { for i := 0; i < len(arr); i++ { v := reflect.ValueOf(arr[i]).FieldByName(field).Interface() diff --git a/internal/actions/detail.go b/internal/actions/detail.go index d06b68f..54e9085 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -21,7 +21,7 @@ func Detail(c *gin.Context) { var err error recent := cache.RecentPosts(c, 5) archive := cache.Archives(c) - categoryItems := cache.Categories(c) + categoryItems := cache.CategoriesTags(c, plugins.Category) recentComments := cache.RecentComments(c, 5) var ginH = gin.H{ "title": wpconfig.Options.Value("blogname"), diff --git a/internal/actions/index.go b/internal/actions/index.go index ec19906..1336a86 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -141,7 +141,7 @@ func (h *indexHandle) parseParams() (err error) { category = h.c.Param("tag") if category != "" { h.scene = plugins.Tag - allNames := cache.AllTagsNames(h.c) + allNames := cache.AllCategoryTagsNames(h.c, plugins.Tag) if _, ok := allNames[category]; !ok { return errors.New(str.Join("not exists tag ", category)) } @@ -150,7 +150,7 @@ func (h *indexHandle) parseParams() (err error) { } } else { h.scene = plugins.Category - allNames := cache.AllCategoryNames(h.c) + allNames := cache.AllCategoryTagsNames(h.c, plugins.Category) if _, ok := allNames[category]; !ok { return errors.New(str.Join("not exists category ", category)) } @@ -237,7 +237,7 @@ func Index(c *gin.Context) { var err error archive := cache.Archives(c) recent := cache.RecentPosts(c, 5) - categoryItems := cache.Categories(c) + categoryItems := cache.CategoriesTags(c, plugins.Category) recentComments := cache.RecentComments(c, 5) ginH := gin.H{ "err": err, diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 8c2e992..710f4b5 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -3,11 +3,13 @@ package cache import ( "context" "github.com/fthvgb1/wp-go/cache" + "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/dao" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/plugins" "sync" "time" ) @@ -155,39 +157,21 @@ func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { return c.data } -func Categories(ctx context.Context) []models.TermsMy { +func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy { r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) logs.ErrPrintln(err, "get category err") - r = slice.Filter(r, func(my models.TermsMy) bool { - return my.Taxonomy == "category" - }) + if len(t) > 0 { + return slice.Filter(r, func(my models.TermsMy) bool { + return helper.Or(t[0] == plugins.Tag, "post_tag", "category") == my.Taxonomy + }) + } return r } - -func Tags(ctx context.Context) []models.TermsMy { - r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) - logs.ErrPrintln(err, "get category err") - r = slice.Filter(r, func(my models.TermsMy) bool { - return my.Taxonomy == "post_tag" - }) - return r -} -func AllTagsNames(ctx context.Context) map[string]struct{} { +func AllCategoryTagsNames(ctx context.Context, c int) map[string]struct{} { r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) logs.ErrPrintln(err, "get category err") return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) { - if t.Taxonomy == "post_tag" { - return t.Name, struct{}{}, true - } - return "", struct{}{}, false - }) -} - -func AllCategoryNames(ctx context.Context) map[string]struct{} { - r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) - logs.ErrPrintln(err, "get category err") - return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) { - if t.Taxonomy == "category" { + if helper.Or(c == plugins.Tag, "post_tag", "category") == t.Taxonomy { return t.Name, struct{}{}, true } return "", struct{}{}, false diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 430dea9..7f3df32 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -139,23 +139,16 @@ func (h handle) bodyClass() string { s := "" switch h.scene { case plugins.Search: + s = "search-no-results" if len(h.ginH["posts"].([]models.Posts)) > 0 { s = "search-results" - } else { - s = "search-no-results" } - case plugins.Category: + case plugins.Category, plugins.Tag: cat := h.c.Param("category") - _, cate := slice.SearchFirst(cache.Categories(h.c), func(my models.TermsMy) bool { - return my.Name == cat - }) - if cate.Slug[0] != '%' { - s = cate.Slug + if cat == "" { + cat = h.c.Param("tag") } - s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s) - case plugins.Tag: - cat := h.c.Param("tag") - _, cate := slice.SearchFirst(cache.Tags(h.c), func(my models.TermsMy) bool { + _, cate := slice.SearchFirst(cache.CategoriesTags(h.c, h.scene), func(my models.TermsMy) bool { return my.Name == cat }) if cate.Slug[0] != '%' {