优化
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
|
module github.com/fthvgb1/wp-go
|
||||||
|
|
||||||
go 1.19
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dlclark/regexp2 v1.7.0
|
github.com/dlclark/regexp2 v1.7.0
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -58,3 +61,36 @@ func DefaultVal[T any](v, defaults T) T {
|
|||||||
}
|
}
|
||||||
return v
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/fthvgb1/wp-go/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StrAnyMapToStruct[T any, M any](m M) (r T, err error) {
|
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
|
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 {
|
func WithDefaultVal[K comparable, V any](m map[K]V, k K, defaults V) V {
|
||||||
vv, ok := m[k]
|
vv, ok := m[k]
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -9,16 +9,18 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var archivesConfig = map[any]any{
|
var archivesConfig = func() safety.Var[map[any]any] {
|
||||||
|
v := safety.Var[map[any]any]{}
|
||||||
|
v.Store(map[any]any{
|
||||||
"count": int64(0),
|
"count": int64(0),
|
||||||
"dropdown": int64(0),
|
"dropdown": int64(0),
|
||||||
"title": "归档",
|
"title": "归档",
|
||||||
}
|
})
|
||||||
|
archiveArgs.Store(map[string]string{
|
||||||
var archiveArgs = 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">`,
|
||||||
@ -31,7 +33,11 @@ var archiveArgs = map[string]string{
|
|||||||
"{$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]{}
|
||||||
|
|
||||||
var archiveTemplate = `{$before_widget}
|
var archiveTemplate = `{$before_widget}
|
||||||
{$title}
|
{$title}
|
||||||
@ -42,9 +48,9 @@ var archiveTemplate = `{$before_widget}
|
|||||||
`
|
`
|
||||||
|
|
||||||
func Archive(h *Handle) string {
|
func Archive(h *Handle) string {
|
||||||
args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs)
|
args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs.Load())
|
||||||
args = maps.Merge(archiveArgs, args)
|
args = maps.FilterZeroMerge(archiveArgs.Load(), args)
|
||||||
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2))
|
conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), 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}"])
|
||||||
s := archiveTemplate
|
s := archiveTemplate
|
||||||
if int64(1) == conf["dropdown"].(int64) {
|
if int64(1) == conf["dropdown"].(int64) {
|
||||||
|
@ -9,10 +9,13 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var recentCommentsArgs = map[string]string{
|
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">`,
|
"{$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">`,
|
||||||
@ -23,12 +26,15 @@ var recentCommentsArgs = map[string]string{
|
|||||||
"{$navCloser}": "",
|
"{$navCloser}": "",
|
||||||
"{$title}": "",
|
"{$title}": "",
|
||||||
"{$recent_comments_id}": "recentcomments",
|
"{$recent_comments_id}": "recentcomments",
|
||||||
}
|
})
|
||||||
|
recentCommentConf.Store(map[any]any{
|
||||||
var recentCommentConf = 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}
|
||||||
@ -41,9 +47,10 @@ var recentCommentsTemplate = `{$before_widget}
|
|||||||
`
|
`
|
||||||
|
|
||||||
func RecentComments(h *Handle) string {
|
func RecentComments(h *Handle) string {
|
||||||
args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs)
|
args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs.Load())
|
||||||
args = maps.Merge(recentCommentsArgs, args)
|
args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args)
|
||||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-comments", recentCommentConf, int64(2))
|
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}"])
|
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"])
|
||||||
|
@ -10,20 +10,11 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var recentPostsArgs = map[string]string{
|
var recentPostsArgs = safety.Var[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 recentPostsTemplate = `{$before_widget}
|
var recentPostsTemplate = `{$before_widget}
|
||||||
{$nav}
|
{$nav}
|
||||||
@ -35,16 +26,33 @@ var recentPostsTemplate = `{$before_widget}
|
|||||||
{$after_widget}
|
{$after_widget}
|
||||||
`
|
`
|
||||||
|
|
||||||
var recentConf = map[any]any{
|
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),
|
"number": int64(5),
|
||||||
"show_date": false,
|
"show_date": false,
|
||||||
"title": "近期文章",
|
"title": "近期文章",
|
||||||
}
|
})
|
||||||
|
return v
|
||||||
|
}()
|
||||||
|
|
||||||
func RecentPosts(h *Handle) string {
|
func RecentPosts(h *Handle) string {
|
||||||
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs)
|
args := GetComponentsArgs(h, components.RecentPostsArgs, recentPostsArgs.Load())
|
||||||
args = maps.Merge(recentPostsArgs, args)
|
args = maps.FilterZeroMerge(recentPostsArgs.Load(), args)
|
||||||
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
|
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}"])
|
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"])
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/components"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
|
"github.com/fthvgb1/wp-go/safety"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,7 +17,9 @@ var searchTemplate = `{$before_widget}
|
|||||||
{$form}
|
{$form}
|
||||||
{$after_widget}`
|
{$after_widget}`
|
||||||
|
|
||||||
var searchArgs = map[string]string{
|
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">`,
|
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
|
||||||
"{$after_widget}": `</aside>`,
|
"{$after_widget}": `</aside>`,
|
||||||
"{$aria_label}": "",
|
"{$aria_label}": "",
|
||||||
@ -26,7 +29,9 @@ var searchArgs = map[string]string{
|
|||||||
"{$button}": "搜索",
|
"{$button}": "搜索",
|
||||||
"{$placeholder}": "搜索…",
|
"{$placeholder}": "搜索…",
|
||||||
"{$label}": "搜索:",
|
"{$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>
|
||||||
@ -44,8 +49,8 @@ var xmlSearchForm = `<form role="search" {$aria_label} method="get" id="searchfo
|
|||||||
</form>`
|
</form>`
|
||||||
|
|
||||||
func SearchForm(h *Handle) string {
|
func SearchForm(h *Handle) string {
|
||||||
args := GetComponentsArgs(h, components.SearchFormArgs, searchArgs)
|
args := GetComponentsArgs(h, components.SearchFormArgs, searchArgs.Load())
|
||||||
args = maps.Merge(searchArgs, args)
|
args = maps.FilterZeroMerge(searchArgs.Load(), 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")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user