优化
This commit is contained in:
parent
12a1fea5ed
commit
b6b92ede34
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/fthvgb1/wp-go
|
||||
|
||||
go 1.19
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.7.0
|
||||
|
@ -1,8 +1,11 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -58,3 +61,36 @@ func DefaultVal[T any](v, defaults T) T {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func IsZero[T comparable](t T) bool {
|
||||
var vv T
|
||||
return vv != t
|
||||
}
|
||||
func IsZeros(v any) bool {
|
||||
switch v.(type) {
|
||||
case int64, int, int8, int16, int32, uint64, uint, uint8, uint16, uint32:
|
||||
i := fmt.Sprintf("%d", v)
|
||||
return str.ToInt[int](i) == 0
|
||||
case float32, float64:
|
||||
f := fmt.Sprintf("%v", v)
|
||||
ff, _ := strconv.ParseFloat(f, 64)
|
||||
return ff == float64(0)
|
||||
case bool:
|
||||
return v.(bool) == false
|
||||
case string:
|
||||
s := v.(string)
|
||||
return s == ""
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ToBool[T comparable](t T) bool {
|
||||
v := any(t)
|
||||
switch v.(type) {
|
||||
case string:
|
||||
s := v.(string)
|
||||
return s != "" && s != "0"
|
||||
}
|
||||
var vv T
|
||||
return vv != t
|
||||
}
|
||||
|
@ -144,3 +144,76 @@ func TestDefaults(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToBool(t *testing.T) {
|
||||
{
|
||||
name := "bool"
|
||||
args := true
|
||||
want := true
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "int"
|
||||
args := 0
|
||||
want := false
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "int"
|
||||
args := 1
|
||||
want := true
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "string"
|
||||
args := "1"
|
||||
want := true
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "string"
|
||||
args := "0"
|
||||
want := false
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "string"
|
||||
args := ""
|
||||
want := false
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
{
|
||||
name := "float"
|
||||
args := 0.2
|
||||
want := true
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := ToBool(args); got != want {
|
||||
t.Errorf("ToBool() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package maps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/fthvgb1/wp-go/helper"
|
||||
)
|
||||
|
||||
func StrAnyMapToStruct[T any, M any](m M) (r T, err error) {
|
||||
@ -109,6 +110,24 @@ func Merge[K comparable, V any](m ...map[K]V) map[K]V {
|
||||
return mm
|
||||
}
|
||||
|
||||
func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V {
|
||||
if len(m) < 1 {
|
||||
panic("no map")
|
||||
} else if len(m) < 2 {
|
||||
return m[0]
|
||||
}
|
||||
mm := m[0]
|
||||
for _, m2 := range m[1:] {
|
||||
for k, v := range m2 {
|
||||
if helper.IsZeros(v) {
|
||||
continue
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
}
|
||||
return mm
|
||||
}
|
||||
|
||||
func WithDefaultVal[K comparable, V any](m map[K]V, k K, defaults V) V {
|
||||
vv, ok := m[k]
|
||||
if ok {
|
||||
|
@ -9,29 +9,35 @@ import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var archivesConfig = map[any]any{
|
||||
"count": int64(0),
|
||||
"dropdown": int64(0),
|
||||
"title": "归档",
|
||||
}
|
||||
var archivesConfig = func() safety.Var[map[any]any] {
|
||||
v := safety.Var[map[any]any]{}
|
||||
v.Store(map[any]any{
|
||||
"count": int64(0),
|
||||
"dropdown": int64(0),
|
||||
"title": "归档",
|
||||
})
|
||||
archiveArgs.Store(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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
"{$dropdown_id}": "archives-dropdown-2",
|
||||
"{$dropdown_type}": "monthly",
|
||||
"{$dropdown_label}": "选择月份",
|
||||
})
|
||||
return v
|
||||
}()
|
||||
|
||||
var archiveArgs = 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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
"{$dropdown_id}": "archives-dropdown-2",
|
||||
"{$dropdown_type}": "monthly",
|
||||
"{$dropdown_label}": "选择月份",
|
||||
}
|
||||
var archiveArgs = safety.Var[map[string]string]{}
|
||||
|
||||
var archiveTemplate = `{$before_widget}
|
||||
{$title}
|
||||
@ -42,9 +48,9 @@ var archiveTemplate = `{$before_widget}
|
||||
`
|
||||
|
||||
func Archive(h *Handle) string {
|
||||
args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs)
|
||||
args = maps.Merge(archiveArgs, args)
|
||||
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
|
||||
args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs.Load())
|
||||
args = maps.FilterZeroMerge(archiveArgs.Load(), args)
|
||||
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2))
|
||||
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
|
||||
s := archiveTemplate
|
||||
if int64(1) == conf["dropdown"].(int64) {
|
||||
|
@ -9,26 +9,32 @@ import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var recentCommentsArgs = 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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
"{$recent_comments_id}": "recentcomments",
|
||||
}
|
||||
var recentCommentsArgs = func() safety.Var[map[string]string] {
|
||||
v := safety.Var[map[string]string]{}
|
||||
v.Store(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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
"{$recent_comments_id}": "recentcomments",
|
||||
})
|
||||
recentCommentConf.Store(map[any]any{
|
||||
"number": int64(5),
|
||||
"title": "近期评论",
|
||||
})
|
||||
return v
|
||||
}()
|
||||
|
||||
var recentCommentConf = map[any]any{
|
||||
"number": int64(5),
|
||||
"title": "近期评论",
|
||||
}
|
||||
var recentCommentConf = safety.Var[map[any]any]{}
|
||||
|
||||
var recentCommentsTemplate = `{$before_widget}
|
||||
{$nav}
|
||||
@ -41,9 +47,10 @@ var recentCommentsTemplate = `{$before_widget}
|
||||
`
|
||||
|
||||
func RecentComments(h *Handle) string {
|
||||
args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs)
|
||||
args = maps.Merge(recentCommentsArgs, args)
|
||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-comments", recentCommentConf, int64(2))
|
||||
args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs.Load())
|
||||
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args)
|
||||
conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2))
|
||||
conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf)
|
||||
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
|
||||
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
||||
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
||||
|
@ -10,20 +10,11 @@ import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var recentPostsArgs = 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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
}
|
||||
var recentPostsArgs = safety.Var[map[string]string]{}
|
||||
|
||||
var recentPostsTemplate = `{$before_widget}
|
||||
{$nav}
|
||||
@ -35,16 +26,33 @@ var recentPostsTemplate = `{$before_widget}
|
||||
{$after_widget}
|
||||
`
|
||||
|
||||
var recentConf = map[any]any{
|
||||
"number": int64(5),
|
||||
"show_date": false,
|
||||
"title": "近期文章",
|
||||
}
|
||||
var recentConf = func() safety.Var[map[any]any] {
|
||||
|
||||
recentPostsArgs.Store(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}": "",
|
||||
"{$after_sidebar}": "",
|
||||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$title}": "",
|
||||
})
|
||||
v := safety.Var[map[any]any]{}
|
||||
v.Store(map[any]any{
|
||||
"number": int64(5),
|
||||
"show_date": false,
|
||||
"title": "近期文章",
|
||||
})
|
||||
return v
|
||||
}()
|
||||
|
||||
func RecentPosts(h *Handle) string {
|
||||
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs)
|
||||
args = maps.Merge(recentPostsArgs, args)
|
||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
|
||||
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs.Load())
|
||||
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args)
|
||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf.Load(), int64(2))
|
||||
conf = maps.FilterZeroMerge(recentConf.Load(), conf)
|
||||
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
|
||||
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
|
||||
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -16,17 +17,21 @@ var searchTemplate = `{$before_widget}
|
||||
{$form}
|
||||
{$after_widget}`
|
||||
|
||||
var searchArgs = map[string]string{
|
||||
"{$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>`,
|
||||
"{$button}": "搜索",
|
||||
"{$placeholder}": "搜索…",
|
||||
"{$label}": "搜索:",
|
||||
}
|
||||
var searchArgs = func() safety.Var[map[string]string] {
|
||||
v := safety.Var[map[string]string]{}
|
||||
v.Store(map[string]string{
|
||||
"{$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>`,
|
||||
"{$button}": "搜索",
|
||||
"{$placeholder}": "搜索…",
|
||||
"{$label}": "搜索:",
|
||||
})
|
||||
return v
|
||||
}()
|
||||
|
||||
var html5SearchForm = `<form role="search" {$aria_label} method="get" class="search-form" action="/">
|
||||
<label>
|
||||
@ -44,8 +49,8 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
|
||||
</form>`
|
||||
|
||||
func SearchForm(h *Handle) string {
|
||||
args := GetComponentsArgs(h, components.SearchFormArgs, searchArgs)
|
||||
args = maps.Merge(searchArgs, args)
|
||||
args := GetComponentsArgs(h, components.SearchFormArgs, searchArgs.Load())
|
||||
args = maps.FilterZeroMerge(searchArgs.Load(), args)
|
||||
if args["{$title}"] == "" {
|
||||
args["{$title}"] = wpconfig.GetPHPArrayVal("widget_search", "", int64(2), "title")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user