Compare commits

...

3 Commits

Author SHA1 Message Date
57a21010a8 优化 fix bug 2023-03-17 19:51:53 +08:00
14aad9e15b 优化代码 2023-03-17 18:26:08 +08:00
3f96c09d36 fix bug 优化 2023-03-17 13:35:22 +08:00
16 changed files with 193 additions and 196 deletions

View File

@ -1,15 +1,22 @@
package actions package actions
import ( import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func ThemeHook(scene int) func(*gin.Context) { func ThemeHook(scene int) func(*gin.Context) {
return func(ctx *gin.Context) { return func(c *gin.Context) {
s := scene
if scene == constraints.Home {
if _, ok := c.GetQuery("s"); ok {
s = constraints.Search
}
}
t := theme.GetCurrentTemplateName() t := theme.GetCurrentTemplateName()
h := wp.NewHandle(ctx, scene, t) h := wp.NewHandle(c, s, t)
h.Index = wp.NewIndexHandle(h) h.Index = wp.NewIndexHandle(h)
h.Detail = wp.NewDetailHandle(h) h.Detail = wp.NewDetailHandle(h)
theme.Hook(t, h) theme.Hook(t, h)

View File

@ -7,4 +7,6 @@ const (
Archive = "archives" Archive = "archives"
Categories = "categories" Categories = "categories"
Meta = "meta" Meta = "meta"
Widget = "widget"
) )

View File

@ -34,14 +34,13 @@ func Init(fs embed.FS) {
logs.ErrPrintln(err, "解析colorscheme失败") logs.ErrPrintln(err, "解析colorscheme失败")
} }
var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(dispatch)...) var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready)...)
func Hook(h *wp.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
} }
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) { func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
components.WidgetArea(h)
h.GetPassword() h.GetPassword()
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string) string { h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string) string {
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
@ -49,19 +48,9 @@ func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
switch h.Scene() { h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.Indexs, 100))
case constraints.Detail: h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.Details, 100))
detail(next, h.Detail) next(h)
default:
index(next, h.Index)
}
}
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
i.Indexs()
}
func detail(fn wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
d.Details()
} }

View File

@ -43,34 +43,29 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready, dispatch)...) var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready)...)
func Hook(h *wp.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
} }
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
components.WidgetArea(h)
h.GetPassword() h.GetPassword()
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
errHandle := wp.NewHandleFn(errorsHandle, 100) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(index, 100))
h.PushHandleFn(constraints.Error404, errHandle) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(detail, 100))
h.PushHandleFn(constraints.ParamError, errHandle) h.PushGroupHandleFn(constraints.AllStats, 99, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle)
h.PushHandleFn(constraints.InternalErr, errHandle)
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
pushScripts(h) pushScripts(h)
h.SetData("HeaderImage", getHeaderImage(h)) h.SetData("HeaderImage", getHeaderImage(h))
h.SetData("scene", h.Scene()) h.SetComponentsArgs(widgets.Widget, map[string]string{
for _, s := range []string{widgets.Meta, widgets.Categories, widgets.Archive, widgets.Search, widgets.RecentComments, widgets.RecentPosts} { "{$before_widget}": `<section id="%s" class="widget widget_%s">`,
ss := strings.ReplaceAll(s, "-", "_") "{$after_widget}": `</section>`,
if s == widgets.RecentPosts { })
ss = "recent_entries"
}
wp.SetComponentsArgsForMap(h, s, "{$before_widget}", fmt.Sprintf(`<section id="%s-2" class="widget widget_%s">`, s, ss))
wp.SetComponentsArgsForMap(h, s, "{$after_widget}", "</section>")
}
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
next(h) next(h)
} }
@ -85,15 +80,6 @@ var searchForm = `<form role="search" method="get" class="search-form" action="/
</button> </button>
</form>` </form>`
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
switch h.Scene() {
case constraints.Detail:
detail(next, h.Detail)
default:
index(next, h.Index)
}
}
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] { var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{ return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
"twentyseventeen_postThumbnail": postThumbnail, "twentyseventeen_postThumbnail": postThumbnail,
@ -101,24 +87,29 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
}() }()
func errorsHandle(h *wp.Handle) { func errorsHandle(h *wp.Handle) {
h.SetTempl("twentyseventeen/posts/error.gohtml")
switch h.Stats { switch h.Stats {
case constraints.Error404, constraints.InternalErr: case constraints.Error404, constraints.InternalErr, constraints.ParamError:
logs.ErrPrintln(h.Err(), "报错:") logs.ErrPrintln(h.Err(), "报错:")
h.SetTempl("twentyseventeen/posts/error.gohtml")
} }
} }
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) { func index(h *wp.Handle) {
if h.Scene() == constraints.Detail {
return
}
i := h.Index
err := i.BuildIndexData(wp.NewIndexParams(i.C)) err := i.BuildIndexData(wp.NewIndexParams(i.C))
if err != nil { if err != nil {
i.SetErr(err) i.SetErr(err)
} }
h.SetData("scene", h.Scene())
i.SetPageEle(paginate) i.SetPageEle(paginate)
i.SetPostsPlugins(listPostsPlugins) i.SetPostsPlugins(listPostsPlugins)
next(i.Handle)
} }
func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) { func detail(h *wp.Handle) {
d := h.Detail
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.SetErr(err) d.SetErr(err)
@ -129,10 +120,7 @@ func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset) img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
d.Post.Thumbnail = img d.Post.Thumbnail = img
} }
d.CommentRender = commentFormat d.CommentRender = commentFormat
next(d.Handle)
} }
var commentFormat = comment{} var commentFormat = comment{}

View File

@ -11,7 +11,7 @@ import (
"strings" "strings"
) )
func (h *Handle) CalBodyClass() { func CalBodyClass(h *Handle) {
h.ginH["bodyClass"] = h.BodyClass(h.class...) h.ginH["bodyClass"] = h.BodyClass(h.class...)
} }

View File

@ -10,18 +10,19 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var archivesConfig = func() safety.Var[map[any]any] { var archiveTemplate = `{$before_widget}
v := safety.Var[map[any]any]{} {$title}
v.Store(map[any]any{ {$nav}
"count": int64(0), {$html}
"dropdown": int64(0), {$navCloser}
"title": "归档", {$after_widget}
}) `
archiveArgs.Store(map[string]string{
func archiveArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`, "{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -34,24 +35,23 @@ var archivesConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2", "{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly", "{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份", "{$dropdown_label}": "选择月份",
}) }
return v }
}()
var archiveArgs = safety.Var[map[string]string]{} func archivesConfig() map[any]any {
return map[any]any{
var archiveTemplate = `{$before_widget} "count": int64(0),
{$title} "dropdown": int64(0),
{$nav} "title": "归档",
{$html} }
{$navCloser} }
{$after_widget}
`
func Archive(h *wp.Handle) string { func Archive(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs.Load()) archiveArgs := archiveArgs()
args = maps.FilterZeroMerge(archiveArgs.Load(), args) archivesConfig := archivesConfig()
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2)) args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs)
args = maps.FilterZeroMerge(archiveArgs, args)
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"]) args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])

View File

@ -0,0 +1,10 @@
package widget
func commonArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="%s" class="widget widget_%s">`,
"{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": "</h2>",
}
}

View File

@ -12,21 +12,29 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"net/http" "net/http"
"strings" "strings"
) )
var categoryArgs safety.Var[map[string]string] var categoryTemplate = `{$before_widget}
var categoryConfig = func() safety.Var[map[any]any] { {$title}
v := safety.Var[map[any]any]{} {$nav}
v.Store(map[any]any{ {$html}
{$navCloser}
{$after_widget}
`
func categoryConfig() map[any]any {
return map[any]any{
"count": int64(0), "count": int64(0),
"dropdown": int64(0), "dropdown": int64(0),
"hierarchical": int64(0), "hierarchical": int64(0),
"title": "分类", "title": "分类",
}) }
categoryArgs.Store(map[string]string{ }
func categoryArgs() map[string]string {
return map[string]string{
"{$before_widget}": `<aside id="categories-2" class="widget widget_categories">`, "{$before_widget}": `<aside id="categories-2" class="widget widget_categories">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -44,23 +52,14 @@ var categoryConfig = func() safety.Var[map[any]any] {
"{$dropdown_id}": "archives-dropdown-2", "{$dropdown_id}": "archives-dropdown-2",
"{$dropdown_type}": "monthly", "{$dropdown_type}": "monthly",
"{$dropdown_label}": "选择月份", "{$dropdown_label}": "选择月份",
}) }
return v }
}()
var categoryTemplate = `{$before_widget}
{$title}
{$nav}
{$html}
{$navCloser}
{$after_widget}
`
func Category(h *wp.Handle) string { func Category(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs.Load()) args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
args = maps.FilterZeroMerge(categoryArgs.Load(), args) args = maps.FilterZeroMerge(categoryArgs(), args)
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2)) conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
conf = maps.FilterZeroMerge(categoryConfig.Load(), conf) conf = maps.FilterZeroMerge(categoryConfig(), conf)
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
t := categoryTemplate t := categoryTemplate
dropdown := conf["dropdown"].(int64) dropdown := conf["dropdown"].(int64)
@ -240,7 +239,7 @@ func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
} }
func parseDropdownCate(h *wp.Handle) (cateName string, r bool) { func parseDropdownCate(h *wp.Handle) (cateName string, r bool) {
cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs.Load()) cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs())
name, ok := cate["{$name}"] name, ok := cate["{$name}"]
if !ok || name == "" { if !ok || name == "" {
return return

View File

@ -8,7 +8,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
@ -21,9 +20,8 @@ var metaTemplate = `{$before_widget}
{$navCloser} {$navCloser}
{$after_widget}` {$after_widget}`
var metaArgs = func() safety.Var[map[string]string] { func metaArgs() map[string]string {
v := safety.Var[map[string]string]{} return map[string]string{
v.Store(map[string]string{
"{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`, "{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`,
"{$after_widget}": `</aside>`, "{$after_widget}": `</aside>`,
"{$aria_label}": "", "{$aria_label}": "",
@ -31,13 +29,13 @@ var metaArgs = func() safety.Var[map[string]string] {
"": "", "": "",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`, "{$after_title}": `</h2>`,
}) }
return v }
}()
func Meta(h *wp.Handle) string { func Meta(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs.Load()) metaArgs := metaArgs()
args = maps.FilterZeroMerge(metaArgs.Load(), args) args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
args = maps.FilterZeroMerge(metaArgs, args)
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_meta", "其它操作", int64(2), "title") args["{$title}"] = wpconfig.GetPHPArrayVal("widget_meta", "其它操作", int64(2), "title")
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"]) args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])

View File

@ -10,13 +10,11 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var recentCommentsArgs = func() safety.Var[map[string]string] { func recentCommentsArgs() map[string]string {
v := safety.Var[map[string]string]{} return map[string]string{
v.Store(map[string]string{
"{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`, "{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -27,15 +25,15 @@ var recentCommentsArgs = func() safety.Var[map[string]string] {
"{$navCloser}": "", "{$navCloser}": "",
"{$title}": "", "{$title}": "",
"{$recent_comments_id}": "recentcomments", "{$recent_comments_id}": "recentcomments",
}) }
recentCommentConf.Store(map[any]any{ }
func recentCommentConf() map[any]any {
return map[any]any{
"number": int64(5), "number": int64(5),
"title": "近期评论", "title": "近期评论",
}) }
return v }
}()
var recentCommentConf = safety.Var[map[any]any]{}
var recentCommentsTemplate = `{$before_widget} var recentCommentsTemplate = `{$before_widget}
{$nav} {$nav}
@ -48,10 +46,12 @@ var recentCommentsTemplate = `{$before_widget}
` `
func RecentComments(h *wp.Handle) string { func RecentComments(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs.Load()) recentCommentsArgs := recentCommentsArgs()
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args) recentCommentConf := recentCommentConf()
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2)) args := wp.GetComponentsArgs(h, widgets.RecentComments, recentCommentsArgs)
conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf) args = maps.FilterZeroMerge(recentCommentsArgs, args)
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf, int64(2))
conf = maps.FilterZeroMerge(recentCommentConf, conf)
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"]) args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])

View File

@ -11,12 +11,9 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
var recentPostsArgs = safety.Var[map[string]string]{}
var recentPostsTemplate = `{$before_widget} var recentPostsTemplate = `{$before_widget}
{$nav} {$nav}
{$title} {$title}
@ -27,9 +24,8 @@ var recentPostsTemplate = `{$before_widget}
{$after_widget} {$after_widget}
` `
var recentConf = func() safety.Var[map[any]any] { func recentPostsArgs() map[string]string {
return map[string]string{
recentPostsArgs.Store(map[string]string{
"{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`, "{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`,
"{$after_widget}": "</aside>", "{$after_widget}": "</aside>",
"{$before_title}": `<h2 class="widget-title">`, "{$before_title}": `<h2 class="widget-title">`,
@ -39,21 +35,24 @@ var recentConf = func() safety.Var[map[any]any] {
"{$nav}": "", "{$nav}": "",
"{$navCloser}": "", "{$navCloser}": "",
"{$title}": "", "{$title}": "",
}) }
v := safety.Var[map[any]any]{} }
v.Store(map[any]any{
func recentConf() map[any]any {
return map[any]any{
"number": int64(5), "number": int64(5),
"show_date": false, "show_date": false,
"title": "近期文章", "title": "近期文章",
}) }
return v }
}()
func RecentPosts(h *wp.Handle) string { func RecentPosts(h *wp.Handle) string {
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs.Load()) recentPostsArgs := recentPostsArgs()
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args) recentConf := recentConf()
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf.Load(), int64(2)) args := wp.GetComponentsArgs(h, widgets.RecentPosts, recentPostsArgs)
conf = maps.FilterZeroMerge(recentConf.Load(), conf) args = maps.FilterZeroMerge(recentPostsArgs, args)
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
conf = maps.FilterZeroMerge(recentConf, conf)
if id, ok := args["{$id}"]; ok && id != "" { if id, ok := args["{$id}"]; ok && id != "" {
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"]) args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
} }

View File

@ -9,7 +9,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
"strings" "strings"
) )
@ -18,24 +17,6 @@ var searchTemplate = `{$before_widget}
{$form} {$form}
{$after_widget}` {$after_widget}`
var searchArgs = func() safety.Var[map[string]string] {
v := safety.Var[map[string]string]{}
v.Store(map[string]string{
"{$id}": "2",
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
"{$after_widget}": `</aside>`,
"{$aria_label}": "",
"{$title}": "",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`,
"{$form}": "",
"{$button}": "搜索",
"{$placeholder}": "搜索&hellip;",
"{$label}": "搜索:",
})
return v
}()
var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/"> var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/">
<label> <label>
<span class="screen-reader-text">{$label}</span> <span class="screen-reader-text">{$label}</span>
@ -51,9 +32,26 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
</div> </div>
</form>` </form>`
func SearchForm(h *wp.Handle) string { func searchArgs() map[string]string {
args := wp.GetComponentsArgs(h, widgets.Search, searchArgs.Load()) return map[string]string{
args = maps.FilterZeroMerge(searchArgs.Load(), args) "{$id}": "2",
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
"{$after_widget}": `</aside>`,
"{$aria_label}": "",
"{$title}": "",
"{$before_title}": `<h2 class="widget-title">`,
"{$after_title}": `</h2>`,
"{$form}": "",
"{$button}": "搜索",
"{$placeholder}": "搜索&hellip;",
"{$label}": "搜索:",
}
}
func Search(h *wp.Handle) string {
searchArgs := searchArgs()
args := wp.GetComponentsArgs(h, widgets.Search, searchArgs)
args = maps.FilterZeroMerge(searchArgs, args)
if args["{$title}"] == "" { if args["{$title}"] == "" {
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title") args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
} }

View File

@ -1,17 +1,19 @@
package components package components
import ( import (
"fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget" "github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"strings" "strings"
) )
var widgets = map[string]func(*wp.Handle) string{ var widgetFn = map[string]func(*wp.Handle) string{
"search": widget.SearchForm, "search": widget.Search,
"recent-posts": widget.RecentPosts, "recent-posts": widget.RecentPosts,
"recent-comments": widget.RecentComments, "recent-comments": widget.RecentComments,
"archives": widget.Archive, "archives": widget.Archive,
@ -20,23 +22,40 @@ var widgets = map[string]func(*wp.Handle) string{
} }
func WidgetArea(h *wp.Handle) { func WidgetArea(h *wp.Handle) {
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
beforeWidget, ok := args["{$before_widget}"]
if !ok {
beforeWidget = ""
} else {
delete(args, "{$before_widget}")
}
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) { sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) {
vv := t.(string) vv := t.(string)
ss := strings.Split(vv, "-") ss := strings.Split(vv, "-")
id := ss[len(ss)-1] id := ss[len(ss)-1]
name := strings.Join(ss[0:len(ss)-1], "-") name := strings.Join(ss[0:len(ss)-1], "-")
fn, ok := widgets[name] fn, ok := widgetFn[name]
if ok { if ok {
if id != "2" { if id != "2" {
wp.SetComponentsArgsForMap(h, name, "{$id}", id) wp.SetComponentsArgsForMap(h, name, "{$id}", id)
} }
if beforeWidget != "" {
n := strings.ReplaceAll(name, "-", "_")
if name == "recent-posts" {
n = "recent_entries"
}
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
}
if len(args) > 0 {
for k, val := range args {
wp.SetComponentsArgsForMap(h, name, k, val)
}
}
return fn, true return fn, true
} }
return nil, false return nil, false
}) })
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(func(h *wp.Handle) { h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
}, 30))
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category)) h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
} }

View File

@ -101,8 +101,6 @@ func (d *DetailHandle) ContextPost() {
} }
func (d *DetailHandle) Render() { func (d *DetailHandle) Render() {
PreCodeAndStats(d.Handle)
PreTemplate(d.Handle)
reply := "" reply := ""
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" { if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>` reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
@ -118,7 +116,8 @@ func (d *DetailHandle) Render() {
d.Handle.Render() d.Handle.Render()
} }
func (d *DetailHandle) Details() { func Details(h *Handle) {
_ = d.BuildDetailData() _ = h.Detail.BuildDetailData()
d.Render() PreCodeAndStats(h)
PreTemplate(h)
} }

View File

@ -47,11 +47,7 @@ func NewIndexHandle(handle *Handle) *IndexHandle {
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
i.Param = parm i.Param = parm
switch i.scene { switch i.scene {
case constraints.Home, constraints.Search: case constraints.Search:
s := i.C.Query("s")
if s != "" && strings.Replace(s, " ", "", -1) != "" {
i.scene = constraints.Search
}
i.Param.ParseSearch() i.Param.ParseSearch()
case constraints.Category: case constraints.Category:
err = i.Param.ParseCategory() err = i.Param.ParseCategory()
@ -148,8 +144,6 @@ func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
} }
func (i *IndexHandle) Render() { func (i *IndexHandle) Render() {
PreCodeAndStats(i.Handle)
PreTemplate(i.Handle)
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) { i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
i.ExecPostsPlugin() i.ExecPostsPlugin()
i.Pagination() i.Pagination()
@ -159,7 +153,12 @@ func (i *IndexHandle) Render() {
i.Handle.Render() i.Handle.Render()
} }
func (i *IndexHandle) Indexs() { func Indexs(h *Handle) {
if h.Scene() == constraints.Detail {
return
}
i := h.Index
_ = i.BuildIndexData(NewIndexParams(i.C)) _ = i.BuildIndexData(NewIndexParams(i.C))
i.Render() PreCodeAndStats(h)
PreTemplate(h)
} }

View File

@ -1,7 +1,6 @@
package wp package wp
import ( import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
@ -229,10 +228,6 @@ func (h *Handle) PushGroupFooterScript(order int, fns ...string) {
h.PushGroupComponentStrs(constraints.FooterScript, order, fns...) h.PushGroupComponentStrs(constraints.FooterScript, order, fns...)
} }
func (h *Handle) componentKey(name string) string {
return fmt.Sprintf("theme_%d_%s", h.scene, name)
}
func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) { func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.FooterScript, key, order, fns...) h.PushGroupCacheComponentFn(constraints.FooterScript, key, order, fns...)
} }
@ -303,17 +298,14 @@ func (h *Handle) Render() {
func (h *Handle) CommonComponents() { func (h *Handle) CommonComponents() {
h.AddCacheComponent("customLogo", CalCustomLogo) h.AddCacheComponent("customLogo", CalCustomLogo)
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss) h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushGroupHandleFn(constraints.AllStats, 10, CalMultipleComponents, CalBodyClass)
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) { h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.CalMultipleComponents()
h.CalBodyClass()
}, 10), NewHandleFn(func(h *Handle) {
h.C.HTML(h.Code, h.templ, h.ginH) h.C.HTML(h.Code, h.templ, h.ginH)
}, 0)) }, 0))
} }
func (h *Handle) PushComponents(name string, components ...Components) { func (h *Handle) PushComponents(name string, components ...Components) {
k := h.componentKey(name) h.components[name] = append(h.components[name], components...)
h.components[k] = append(h.components[k], components...)
} }
func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) { func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) {
@ -324,8 +316,7 @@ func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) {
Order: order, Order: order,
}) })
} }
k := h.componentKey(name) h.components[name] = append(h.components[name], calls...)
h.components[k] = append(h.components[k], calls...)
} }
func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) { func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) {
var calls []Components var calls []Components
@ -335,11 +326,10 @@ func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Hand
Order: order, Order: order,
}) })
} }
k := h.componentKey(name) h.components[name] = append(h.components[name], calls...)
h.components[k] = append(h.components[k], calls...)
} }
func (h *Handle) CalMultipleComponents() { func CalMultipleComponents(h *Handle) {
for k, ss := range h.components { for k, ss := range h.components {
slice.Sort(ss, func(i, j Components) bool { slice.Sort(ss, func(i, j Components) bool {
return i.Order > j.Order return i.Order > j.Order