wp-go/app/theme/wp/stickyposts.go

49 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-03-29 13:58:39 +00:00
package wp
import (
"fmt"
"github.com/elliotchance/phpserialize"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-11-12 13:39:04 +00:00
"github.com/fthvgb1/wp-go/cache/reload"
2023-03-29 13:58:39 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
)
var GetStickPosts = reload.BuildValFnWithConfirm("stickPostsSlice", ParseStickPosts)
func ParseStickPosts(h *Handle) (r []models.Posts, ok bool) {
v := wpconfig.GetOption("sticky_posts")
if v == "" {
return
}
array, err := phpserialize.UnmarshalIndexedArray([]byte(v))
if err != nil {
logs.Error(err, "解析option sticky_posts错误", v)
2023-03-29 13:58:39 +00:00
return
}
r = slice.FilterAndMap(array, func(t any) (models.Posts, bool) {
id := str.ToInt[uint64](fmt.Sprintf("%v", t))
post, err := cache.GetPostById(h.C, id)
post.IsSticky = true
return post, err == nil
2023-03-29 13:58:39 +00:00
})
ok = true
return
2023-03-29 13:58:39 +00:00
}
var GetStickMapPosts = reload.BuildValFn("stickPostsMap", StickMapPosts)
func StickMapPosts(h *Handle) map[uint64]models.Posts {
return slice.SimpleToMap(GetStickPosts(h), func(v models.Posts) uint64 {
return v.Id
2023-03-29 13:58:39 +00:00
})
}
func (h *Handle) IsStick(id uint64) bool {
_, ok := GetStickMapPosts(h)[id]
return ok
2023-03-29 13:58:39 +00:00
}