2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-11 15:51:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/config"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/app/plugins/wpposts"
|
2023-02-22 14:11:25 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-02-11 15:51:07 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
)
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
type PostsPlugin func(*Handle, *models.Posts)
|
2023-02-11 15:51:07 +00:00
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *models.Posts)) PostsPlugin {
|
|
|
|
return slice.ReverseReduce(calls, func(t func(PostsPlugin, *Handle, *models.Posts), r PostsPlugin) PostsPlugin {
|
|
|
|
return func(handle *Handle, posts *models.Posts) {
|
|
|
|
t(r, handle, posts)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-04-09 13:51:12 +00:00
|
|
|
}, initial)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
var pluginFns = map[string]func(PostsPlugin, *Handle, *models.Posts){
|
2023-02-11 15:51:07 +00:00
|
|
|
"passwordProject": PasswordProject,
|
|
|
|
"digest": Digest,
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordProject 标题和内容密码保护
|
2023-04-09 13:51:12 +00:00
|
|
|
func PasswordProject(next PostsPlugin, h *Handle, post *models.Posts) {
|
|
|
|
r := post
|
2023-02-11 15:51:07 +00:00
|
|
|
if post.PostPassword != "" {
|
2023-04-09 13:51:12 +00:00
|
|
|
wpposts.PasswordProjectTitle(r)
|
2023-05-03 15:57:49 +00:00
|
|
|
if h.GetPassword() != post.PostPassword {
|
2023-04-09 13:51:12 +00:00
|
|
|
wpposts.PasswdProjectContent(r)
|
2023-02-11 15:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 13:51:12 +00:00
|
|
|
next(h, r)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
// Digest 生成摘要
|
|
|
|
func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
|
2023-02-11 15:51:07 +00:00
|
|
|
if post.PostExcerpt != "" {
|
2023-04-09 13:51:12 +00:00
|
|
|
plugins.PostExcerpt(post)
|
2023-02-11 15:51:07 +00:00
|
|
|
} else {
|
2023-04-09 13:51:12 +00:00
|
|
|
plugins.Digest(h.C, post, config.GetConfig().DigestWordCount)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-04-09 13:51:12 +00:00
|
|
|
next(h, post)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-02-22 14:11:25 +00:00
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func PostPlugin(calls ...func(h *Handle, posts *models.Posts)) PostsPlugin {
|
|
|
|
return func(h *Handle, posts *models.Posts) {
|
|
|
|
for _, call := range calls {
|
|
|
|
call(h, posts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListPostPlugins() map[string]func(PostsPlugin, *Handle, *models.Posts) {
|
2023-02-22 14:11:25 +00:00
|
|
|
return maps.Copy(pluginFns)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectTitle(t models.Posts) models.Posts {
|
|
|
|
if t.PostPassword != "" {
|
2023-03-02 15:49:28 +00:00
|
|
|
wpposts.PasswordProjectTitle(&t)
|
2023-02-22 14:11:25 +00:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2023-04-09 13:51:12 +00:00
|
|
|
func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, *models.Posts)) []func(PostsPlugin, *Handle, *models.Posts) {
|
|
|
|
return slice.FilterAndMap(name, func(t string) (func(PostsPlugin, *Handle, *models.Posts), bool) {
|
2023-02-22 14:11:25 +00:00
|
|
|
v, ok := m[t]
|
|
|
|
if ok {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
})
|
|
|
|
}
|