优化widget参数
This commit is contained in:
parent
13520a0d43
commit
e10dc4e45e
|
@ -8,11 +8,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock, map[string]string) (func() string, error){
|
var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() string, error){
|
||||||
"core/categories": block.Category,
|
"core/categories": block.Category,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Block(id string, args map[string]string) func(*wp.Handle) string {
|
func Block(id string) func(*wp.Handle) string {
|
||||||
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
|
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
|
||||||
if content == "" {
|
if content == "" {
|
||||||
return nil
|
return nil
|
||||||
|
@ -23,7 +23,7 @@ func Block(id string, args map[string]string) func(*wp.Handle) string {
|
||||||
for _, parserBlock := range v.Output {
|
for _, parserBlock := range v.Output {
|
||||||
fn, ok := blockFn[parserBlock.Name]
|
fn, ok := blockFn[parserBlock.Name]
|
||||||
if ok {
|
if ok {
|
||||||
s, err := fn(h, id, parserBlock, args)
|
s, err := fn(h, id, parserBlock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
constraints2 "github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
constraints2 "github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"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"
|
||||||
|
@ -70,7 +71,7 @@ func parseAttr(attr map[any]any) string {
|
||||||
return strings.Join(attrs, " ")
|
return strings.Join(attrs, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Category(h *wp.Handle, id string, blockParser ParserBlock, args map[string]string) (func() string, error) {
|
func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, error) {
|
||||||
counter := number.Counters[int]()
|
counter := number.Counters[int]()
|
||||||
var err error
|
var err error
|
||||||
conf := reload.GetAnyValBys("block-category-conf", h, func(h *wp.Handle) map[any]any {
|
conf := reload.GetAnyValBys("block-category-conf", h, func(h *wp.Handle) map[any]any {
|
||||||
|
@ -115,7 +116,8 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock, args map[string]
|
||||||
if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") {
|
if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") {
|
||||||
h.C.Set("showOnlyTopLevel", true)
|
h.C.Set("showOnlyTopLevel", true)
|
||||||
}
|
}
|
||||||
args = reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string {
|
||||||
|
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
return maps.FilterZeroMerge(categoryDefaultArgs(), args)
|
return maps.FilterZeroMerge(categoryDefaultArgs(), args)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -24,10 +24,6 @@ var archiveTemplate = `{$before_widget}
|
||||||
|
|
||||||
func archiveArgs() map[string]string {
|
func archiveArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="archives-2" class="widget widget_archive">`,
|
|
||||||
"{$after_widget}": "</aside>",
|
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": "</h2>",
|
|
||||||
"{$before_sidebar}": "",
|
"{$before_sidebar}": "",
|
||||||
"{$after_sidebar}": "",
|
"{$after_sidebar}": "",
|
||||||
"{$nav}": "",
|
"{$nav}": "",
|
||||||
|
@ -47,24 +43,23 @@ func archivesConfig() map[any]any {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Archive(h *wp.Handle) string {
|
func Archive(h *wp.Handle, id string) string {
|
||||||
args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string {
|
|
||||||
archiveArgs := archiveArgs()
|
|
||||||
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs)
|
|
||||||
args = maps.FilterZeroMerge(archiveArgs, args)
|
|
||||||
return args
|
|
||||||
})
|
|
||||||
|
|
||||||
conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any {
|
conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any {
|
||||||
archivesConfig := archivesConfig()
|
archivesConfig := archivesConfig()
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
|
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
|
||||||
return conf
|
return conf
|
||||||
})
|
})
|
||||||
|
|
||||||
|
args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string {
|
||||||
|
archiveArgs := archiveArgs()
|
||||||
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs())
|
||||||
|
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs)
|
||||||
|
args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args)
|
||||||
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive"))
|
||||||
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 != "" {
|
return args
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
})
|
||||||
}
|
|
||||||
s := archiveTemplate
|
s := archiveTemplate
|
||||||
if int64(1) == conf["dropdown"].(int64) {
|
if int64(1) == conf["dropdown"].(int64) {
|
||||||
s = strings.ReplaceAll(s, "{$html}", archiveDropDown(h, conf, args, cache.Archives(h.C)))
|
s = strings.ReplaceAll(s, "{$html}", archiveDropDown(h, conf, args, cache.Archives(h.C)))
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package widget
|
package widget
|
||||||
|
|
||||||
func commonArgs() map[string]string {
|
func CommonArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="%s" class="widget widget_%s">`,
|
"{$before_widget}": `<aside id="%s" class="%s">`,
|
||||||
"{$after_widget}": "</aside>",
|
"{$after_widget}": "</aside>",
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
"{$before_title}": `<h2 class="widget-title">`,
|
||||||
"{$after_title}": "</h2>",
|
"{$after_title}": "</h2>",
|
||||||
|
|
|
@ -36,10 +36,6 @@ func categoryConfig() map[any]any {
|
||||||
|
|
||||||
func categoryArgs() map[string]string {
|
func categoryArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="categories-2" class="widget widget_categories">`,
|
|
||||||
"{$after_widget}": "</aside>",
|
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": "</h2>",
|
|
||||||
"{$before_sidebar}": "",
|
"{$before_sidebar}": "",
|
||||||
"{$after_sidebar}": "",
|
"{$after_sidebar}": "",
|
||||||
"{$class}": "postform",
|
"{$class}": "postform",
|
||||||
|
@ -53,28 +49,28 @@ func categoryArgs() map[string]string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Category(h *wp.Handle) string {
|
func Category(h *wp.Handle, id string) string {
|
||||||
args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string {
|
|
||||||
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
|
|
||||||
args = maps.FilterZeroMerge(categoryArgs(), args)
|
|
||||||
return args
|
|
||||||
})
|
|
||||||
conf := reload.GetAnyValBys("widget-category-conf", h, func(a *wp.Handle) map[any]any {
|
conf := reload.GetAnyValBys("widget-category-conf", h, func(a *wp.Handle) map[any]any {
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
|
conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig(), int64(2))
|
||||||
conf = maps.FilterZeroMerge(categoryConfig(), conf)
|
conf = maps.FilterZeroMerge(categoryConfig(), conf)
|
||||||
return conf
|
return conf
|
||||||
})
|
})
|
||||||
|
|
||||||
|
args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string {
|
||||||
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
|
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
|
||||||
|
args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args)
|
||||||
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("categories-", id), str.Join("widget widget_", "categories"))
|
||||||
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}"])
|
||||||
|
return args
|
||||||
|
})
|
||||||
|
|
||||||
t := categoryTemplate
|
t := categoryTemplate
|
||||||
dropdown := conf["dropdown"].(int64)
|
dropdown := conf["dropdown"].(int64)
|
||||||
if id, ok := args["{$id}"]; ok && id != "" {
|
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
|
||||||
}
|
|
||||||
categories := cache.CategoriesTags(h.C, constraints.Category)
|
categories := cache.CategoriesTags(h.C, constraints.Category)
|
||||||
if dropdown == 1 {
|
if dropdown == 1 {
|
||||||
t = strings.ReplaceAll(t, "{$html}", CategoryDropdown(h, args, conf, categories))
|
t = strings.ReplaceAll(t, "{$html}", CategoryDropdown(h, args, conf, categories))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
||||||
}
|
}
|
||||||
|
|
9
internal/theme/wp/components/widget/fn.go
Normal file
9
internal/theme/wp/components/widget/fn.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package widget
|
||||||
|
|
||||||
|
import "github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
|
|
||||||
|
func Fn(id string, fn func(*wp.Handle, string) string) func(h *wp.Handle) string {
|
||||||
|
return func(h *wp.Handle) string {
|
||||||
|
return fn(h, id)
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,27 +23,19 @@ var metaTemplate = `{$before_widget}
|
||||||
|
|
||||||
func metaArgs() map[string]string {
|
func metaArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="meta-2" class="widget widget_meta">`,
|
|
||||||
"{$after_widget}": `</aside>`,
|
|
||||||
"{$aria_label}": "",
|
"{$aria_label}": "",
|
||||||
"{$title}": "",
|
"{$title}": "",
|
||||||
"": "",
|
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": `</h2>`,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Meta(h *wp.Handle) string {
|
func Meta(h *wp.Handle, id string) string {
|
||||||
args := reload.GetAnyValBys("widget-meta-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("widget-meta-args", h, func(h *wp.Handle) map[string]string {
|
||||||
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
metaArgs := metaArgs()
|
metaArgs := metaArgs()
|
||||||
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
|
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
|
||||||
args = maps.FilterZeroMerge(metaArgs, args)
|
args = maps.FilterZeroMerge(metaArgs, CommonArgs(), commonArgs, args)
|
||||||
return args
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("meta-", id), str.Join("widget widget_", "meta"))
|
||||||
})
|
|
||||||
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 != "" {
|
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
|
||||||
}
|
|
||||||
if args["{$title}"] == "" {
|
if args["{$title}"] == "" {
|
||||||
args["{$title}"] = "其他操作"
|
args["{$title}"] = "其他操作"
|
||||||
}
|
}
|
||||||
|
@ -54,6 +46,9 @@ func Meta(h *wp.Handle) string {
|
||||||
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{$title}"])
|
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{$title}"])
|
||||||
args["{$navCloser}"] = "</nav>"
|
args["{$navCloser}"] = "</nav>"
|
||||||
}
|
}
|
||||||
|
return args
|
||||||
|
})
|
||||||
|
|
||||||
ss := str.NewBuilder()
|
ss := str.NewBuilder()
|
||||||
if str.ToInteger(wpconfig.GetOption("users_can_register"), 0) > 0 {
|
if str.ToInteger(wpconfig.GetOption("users_can_register"), 0) > 0 {
|
||||||
ss.Sprintf(`<li><a href="/wp-login.php?action=register">注册</li>`)
|
ss.Sprintf(`<li><a href="/wp-login.php?action=register">注册</li>`)
|
||||||
|
|
|
@ -16,10 +16,6 @@ import (
|
||||||
|
|
||||||
func recentCommentsArgs() map[string]string {
|
func recentCommentsArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`,
|
|
||||||
"{$after_widget}": "</aside>",
|
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": "</h2>",
|
|
||||||
"{$before_sidebar}": "",
|
"{$before_sidebar}": "",
|
||||||
"{$after_sidebar}": "",
|
"{$after_sidebar}": "",
|
||||||
"{$nav}": "",
|
"{$nav}": "",
|
||||||
|
@ -46,29 +42,27 @@ var recentCommentsTemplate = `{$before_widget}
|
||||||
{$after_widget}
|
{$after_widget}
|
||||||
`
|
`
|
||||||
|
|
||||||
func RecentComments(h *wp.Handle) string {
|
func RecentComments(h *wp.Handle, id string) string {
|
||||||
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
|
|
||||||
commentsArgs := recentCommentsArgs()
|
|
||||||
args := wp.GetComponentsArgs(h, widgets.RecentComments, commentsArgs)
|
|
||||||
args = maps.FilterZeroMerge(commentsArgs, args)
|
|
||||||
return args
|
|
||||||
})
|
|
||||||
|
|
||||||
conf := reload.GetAnyValBys("widget-recent-comment-conf", h, func(h *wp.Handle) map[any]any {
|
conf := reload.GetAnyValBys("widget-recent-comment-conf", h, func(h *wp.Handle) map[any]any {
|
||||||
commentConf := recentCommentConf()
|
commentConf := recentCommentConf()
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", commentConf, int64(2))
|
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", commentConf, int64(2))
|
||||||
conf = maps.FilterZeroMerge(commentConf, conf)
|
conf = maps.FilterZeroMerge(commentConf, conf)
|
||||||
return conf
|
return conf
|
||||||
})
|
})
|
||||||
|
args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string {
|
||||||
|
commentsArgs := recentCommentsArgs()
|
||||||
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
|
args := wp.GetComponentsArgs(h, widgets.RecentComments, commentsArgs)
|
||||||
|
args = maps.FilterZeroMerge(commentsArgs, CommonArgs(), commonArgs, args)
|
||||||
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("recent-comments-", id), str.Join("widget widget_", "recent_comments"))
|
||||||
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 != "" {
|
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
|
||||||
}
|
|
||||||
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
||||||
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
||||||
args["{$navCloser}"] = "</nav>"
|
args["{$navCloser}"] = "</nav>"
|
||||||
}
|
}
|
||||||
|
return args
|
||||||
|
})
|
||||||
|
|
||||||
comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string {
|
comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string {
|
||||||
return fmt.Sprintf(` <li>
|
return fmt.Sprintf(` <li>
|
||||||
<span class="comment-author-link">%s</span>发表在《
|
<span class="comment-author-link">%s</span>发表在《
|
||||||
|
|
|
@ -27,10 +27,6 @@ var recentPostsTemplate = `{$before_widget}
|
||||||
|
|
||||||
func recentPostsArgs() map[string]string {
|
func recentPostsArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$before_widget}": `<aside id="recent-posts-2" class="widget widget_recent_entries">`,
|
|
||||||
"{$after_widget}": "</aside>",
|
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": "</h2>",
|
|
||||||
"{$before_sidebar}": "",
|
"{$before_sidebar}": "",
|
||||||
"{$after_sidebar}": "",
|
"{$after_sidebar}": "",
|
||||||
"{$nav}": "",
|
"{$nav}": "",
|
||||||
|
@ -47,13 +43,7 @@ func recentConf() map[any]any {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RecentPosts(h *wp.Handle) string {
|
func RecentPosts(h *wp.Handle, id string) string {
|
||||||
args := reload.GetAnyValBys("widget-recent-posts-args", h, func(h *wp.Handle) map[string]string {
|
|
||||||
recent := recentPostsArgs()
|
|
||||||
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent)
|
|
||||||
args = maps.FilterZeroMerge(recent, args)
|
|
||||||
return args
|
|
||||||
})
|
|
||||||
conf := reload.GetAnyValBys("widget-recent-posts-conf", h, func(h *wp.Handle) map[any]any {
|
conf := reload.GetAnyValBys("widget-recent-posts-conf", h, func(h *wp.Handle) map[any]any {
|
||||||
recent := recentConf()
|
recent := recentConf()
|
||||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2))
|
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2))
|
||||||
|
@ -61,14 +51,20 @@ func RecentPosts(h *wp.Handle) string {
|
||||||
return conf
|
return conf
|
||||||
})
|
})
|
||||||
|
|
||||||
if id, ok := args["{$id}"]; ok && id != "" {
|
args := reload.GetAnyValBys("widget-recent-posts-args", h, func(h *wp.Handle) map[string]string {
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
recent := recentPostsArgs()
|
||||||
}
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
|
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent)
|
||||||
|
args = maps.FilterZeroMerge(recent, CommonArgs(), commonArgs, args)
|
||||||
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("recent-posts-", id), str.Join("widget widget_", "recent_entries"))
|
||||||
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 slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
||||||
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
||||||
args["{$navCloser}"] = "</nav>"
|
args["{$navCloser}"] = "</nav>"
|
||||||
}
|
}
|
||||||
|
return args
|
||||||
|
})
|
||||||
|
|
||||||
currentPostId := uint64(0)
|
currentPostId := uint64(0)
|
||||||
if h.Scene() == constraints.Detail {
|
if h.Scene() == constraints.Detail {
|
||||||
currentPostId = str.ToInteger(h.C.Param("id"), uint64(0))
|
currentPostId = str.ToInteger(h.C.Param("id"), uint64(0))
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package widget
|
package widget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper/html"
|
"github.com/fthvgb1/wp-go/helper/html"
|
||||||
"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"
|
||||||
|
@ -35,13 +36,8 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
|
||||||
|
|
||||||
func searchArgs() map[string]string {
|
func searchArgs() map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"{$id}": "2",
|
|
||||||
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
|
|
||||||
"{$after_widget}": `</aside>`,
|
|
||||||
"{$aria_label}": "",
|
"{$aria_label}": "",
|
||||||
"{$title}": "",
|
"{$title}": "",
|
||||||
"{$before_title}": `<h2 class="widget-title">`,
|
|
||||||
"{$after_title}": `</h2>`,
|
|
||||||
"{$form}": "",
|
"{$form}": "",
|
||||||
"{$button}": "搜索",
|
"{$button}": "搜索",
|
||||||
"{$placeholder}": "搜索…",
|
"{$placeholder}": "搜索…",
|
||||||
|
@ -49,35 +45,35 @@ func searchArgs() map[string]string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search(h *wp.Handle) string {
|
func Search(h *wp.Handle, id string) string {
|
||||||
|
form := html5SearchForm
|
||||||
args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string {
|
args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string {
|
||||||
search := searchArgs()
|
search := searchArgs()
|
||||||
|
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||||
args := wp.GetComponentsArgs(h, widgets.Search, search)
|
args := wp.GetComponentsArgs(h, widgets.Search, search)
|
||||||
args = maps.FilterZeroMerge(search, args)
|
args = maps.FilterZeroMerge(search, CommonArgs(), commonArgs, args)
|
||||||
return args
|
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("search-", id), str.Join("widget widget_", "search"))
|
||||||
})
|
|
||||||
if args["{$title}"] == "" {
|
if args["{$title}"] == "" {
|
||||||
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
|
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
|
||||||
}
|
}
|
||||||
if id, ok := args["{$id}"]; ok && id != "" {
|
|
||||||
args["{$before_widget}"] = strings.ReplaceAll(args["{$before_widget}"], "2", args["{$id}"])
|
|
||||||
}
|
|
||||||
if args["{$title}"] != "" {
|
if args["{$title}"] != "" {
|
||||||
args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"])
|
args["{$title}"] = str.Join(args["{$before_title}"], args["{$title}"], args["{$after_title}"])
|
||||||
}
|
}
|
||||||
args["{$value}"] = ""
|
|
||||||
if h.Scene() == constraints.Search {
|
|
||||||
args["{$value}"] = html.SpecialChars(h.Index.Param.Search)
|
|
||||||
}
|
|
||||||
form := html5SearchForm
|
|
||||||
if args["{$form}"] != "" {
|
if args["{$form}"] != "" {
|
||||||
form = args["{$form}"]
|
form = args["{$form}"]
|
||||||
delete(args, "{$form}")
|
delete(args, "{$form}")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
||||||
form = xmlSearchForm
|
form = xmlSearchForm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return args
|
||||||
|
})
|
||||||
s := strings.ReplaceAll(searchTemplate, "{$form}", form)
|
s := strings.ReplaceAll(searchTemplate, "{$form}", form)
|
||||||
|
args["{$value}"] = ""
|
||||||
|
if h.Scene() == constraints.Search {
|
||||||
|
args["{$value}"] = html.SpecialChars(h.Index.Param.Search)
|
||||||
|
}
|
||||||
return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args))
|
return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,27 @@
|
||||||
package components
|
package components
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"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 widgetFn = map[string]wp.Components[string]{
|
var widgetFn = map[string]widgetComponent{
|
||||||
"search": {Fn: widget.Search},
|
"search": {fn: widget.Search},
|
||||||
"recent-posts": {Fn: widget.RecentPosts},
|
"recent-posts": {fn: widget.RecentPosts},
|
||||||
"recent-comments": {Fn: widget.RecentComments},
|
"recent-comments": {fn: widget.RecentComments},
|
||||||
"archives": {Fn: widget.Archive},
|
"archives": {fn: widget.Archive},
|
||||||
"categories": {Fn: widget.Category},
|
"categories": {fn: widget.Category},
|
||||||
"meta": {Fn: widget.Meta, CacheKey: "widgetMeta"},
|
"meta": {fn: widget.Meta, cacheKey: "widgetMeta"},
|
||||||
|
}
|
||||||
|
|
||||||
|
type widgetComponent struct {
|
||||||
|
fn func(h *wp.Handle, id string) string
|
||||||
|
cacheKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func WidgetArea(h *wp.Handle) {
|
func WidgetArea(h *wp.Handle) {
|
||||||
|
@ -27,49 +29,29 @@ func WidgetArea(h *wp.Handle) {
|
||||||
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sidebars(h *wp.Handle) []wp.Components[string] {
|
func sidebars(*wp.Handle) []wp.Components[string] {
|
||||||
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
|
||||||
beforeWidget, ok := args["{$before_widget}"]
|
|
||||||
if !ok {
|
|
||||||
beforeWidget = ""
|
|
||||||
}
|
|
||||||
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
|
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
|
||||||
return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) {
|
return slice.FilterAndMap(v, func(t any) (wp.Components[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], "-")
|
||||||
components, ok := widgetFn[name]
|
widgetComponents, ok := widgetFn[name]
|
||||||
if name != "block" && !ok {
|
if name != "block" && !ok {
|
||||||
return components, false
|
|
||||||
}
|
|
||||||
if id != "2" {
|
|
||||||
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
|
||||||
}
|
|
||||||
names := str.Join("widget-", name)
|
|
||||||
|
|
||||||
if beforeWidget != "" && name != "block" {
|
|
||||||
n := strings.ReplaceAll(name, "-", "_")
|
|
||||||
if name == "recent-posts" {
|
|
||||||
n = "recent_entries"
|
|
||||||
}
|
|
||||||
n = str.Join("widget widget_", n)
|
|
||||||
delete(args, "{$before_widget}")
|
|
||||||
wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
|
||||||
} else {
|
|
||||||
args["{$before_widget}"] = beforeWidget
|
|
||||||
}
|
|
||||||
for k, val := range args {
|
|
||||||
wp.SetComponentsArgsForMap(h, names, k, val)
|
|
||||||
}
|
|
||||||
if name == "block" {
|
|
||||||
fn := Block(id, args)
|
|
||||||
if fn == nil {
|
|
||||||
return wp.Components[string]{}, false
|
return wp.Components[string]{}, false
|
||||||
}
|
}
|
||||||
components = wp.Components[string]{Fn: fn, Order: 10}
|
var component wp.Components[string]
|
||||||
|
if name == "block" {
|
||||||
|
fn := Block(id)
|
||||||
|
if fn == nil {
|
||||||
|
return component, false
|
||||||
}
|
}
|
||||||
components.Order = 10
|
component.Fn = fn
|
||||||
return components, true
|
} else {
|
||||||
|
component.Fn = widget.Fn(id, widgetComponents.fn)
|
||||||
|
component.CacheKey = widgetComponents.cacheKey
|
||||||
|
}
|
||||||
|
component.Order = 10
|
||||||
|
return component, true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user