wp-go/app/theme/wp/listpostplugins.go

102 lines
2.7 KiB
Go
Raw Normal View History

2023-03-01 05:17:12 +00:00
package wp
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-11-12 13:39:04 +00:00
"github.com/fthvgb1/wp-go/cache/reload"
2023-02-22 14:11:25 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
)
type PostsPlugin func(*Handle, *models.Posts)
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)
}
}, initial)
}
2023-05-09 05:44:20 +00:00
var pluginFns = reload.Vars(map[string]func(PostsPlugin, *Handle, *models.Posts){
"passwordProject": PasswordProject,
"digest": Digest,
2023-05-09 05:44:20 +00:00
})
func (h *Handle) PushPostsPlugin(name string, fn func(PostsPlugin, *Handle, *models.Posts)) {
m := pluginFns.Load()
m[name] = fn
}
// PasswordProject 标题和内容密码保护
func PasswordProject(next PostsPlugin, h *Handle, post *models.Posts) {
r := post
if post.PostPassword != "" {
wpposts.PasswordProjectTitle(r)
2023-05-03 15:57:49 +00:00
if h.GetPassword() != post.PostPassword {
wpposts.PasswdProjectContent(r)
return
}
}
next(h, r)
}
// Digest 生成摘要
func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
if post.PostExcerpt != "" {
plugins.PostExcerpt(post)
} else {
plugins.Digest(h.C, post, config.GetConfig().DigestWordCount)
}
next(h, post)
}
2023-02-22 14:11:25 +00:00
2023-05-09 05:44:20 +00:00
var ordinaryPlugin = reload.Vars([]PostsPlugin{})
func (h *Handle) PushPostPlugin(plugin ...PostsPlugin) {
p := ordinaryPlugin.Load()
p = append(p, plugin...)
ordinaryPlugin.Store(p)
}
func PostPlugin(calls ...PostsPlugin) PostsPlugin {
return func(h *Handle, posts *models.Posts) {
for _, call := range calls {
call(h, posts)
}
}
}
2023-05-09 05:44:20 +00:00
func UsePostsPlugins() PostsPlugin {
m := pluginFns.Load()
2023-05-10 06:28:21 +00:00
pluginss := slice.FilterAndMap(config.GetConfig().ListPagePlugins, func(t string) (func(PostsPlugin, *Handle, *models.Posts), bool) {
f, ok := m[t]
return f, ok
})
2023-05-10 05:18:47 +00:00
slice.Unshift(&pluginss, PasswordProject)
2023-05-09 05:44:20 +00:00
return PostsPlugins(PostPlugin(ordinaryPlugin.Load()...), pluginss...)
}
func ListPostPlugins() map[string]func(PostsPlugin, *Handle, *models.Posts) {
2023-05-09 05:44:20 +00:00
return maps.Copy(pluginFns.Load())
2023-02-22 14:11:25 +00:00
}
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
}
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
})
}