This commit is contained in:
xing 2023-03-12 20:28:57 +08:00
parent 12a1fea5ed
commit b6b92ede34
8 changed files with 228 additions and 74 deletions

2
go.mod
View File

@ -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

View File

@ -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
}

View File

@ -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)
}
})
}
}

View File

@ -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 {

View File

@ -9,29 +9,35 @@ 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] {
"count": int64(0), v := safety.Var[map[any]any]{}
"dropdown": int64(0), v.Store(map[any]any{
"title": "归档", "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{ var archiveArgs = safety.Var[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 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) {

View File

@ -9,26 +9,32 @@ 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] {
"{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`, v := safety.Var[map[string]string]{}
"{$after_widget}": "</aside>", v.Store(map[string]string{
"{$before_title}": `<h2 class="widget-title">`, "{$before_widget}": `<aside id="recent-comments-2" class="widget widget_recent_comments">`,
"{$after_title}": "</h2>", "{$after_widget}": "</aside>",
"{$before_sidebar}": "", "{$before_title}": `<h2 class="widget-title">`,
"{$after_sidebar}": "", "{$after_title}": "</h2>",
"{$nav}": "", "{$before_sidebar}": "",
"{$navCloser}": "", "{$after_sidebar}": "",
"{$title}": "", "{$nav}": "",
"{$recent_comments_id}": "recentcomments", "{$navCloser}": "",
} "{$title}": "",
"{$recent_comments_id}": "recentcomments",
})
recentCommentConf.Store(map[any]any{
"number": int64(5),
"title": "近期评论",
})
return v
}()
var recentCommentConf = map[any]any{ var recentCommentConf = safety.Var[map[any]any]{}
"number": int64(5),
"title": "近期评论",
}
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"])

View File

@ -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] {
"number": int64(5),
"show_date": false, recentPostsArgs.Store(map[string]string{
"title": "近期文章", "{$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 { 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"])

View File

@ -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,17 +17,21 @@ var searchTemplate = `{$before_widget}
{$form} {$form}
{$after_widget}` {$after_widget}`
var searchArgs = map[string]string{ var searchArgs = func() safety.Var[map[string]string] {
"{$before_widget}": `<aside id="search-2" class="widget widget_search">`, v := safety.Var[map[string]string]{}
"{$after_widget}": `</aside>`, v.Store(map[string]string{
"{$aria_label}": "", "{$before_widget}": `<aside id="search-2" class="widget widget_search">`,
"{$title}": "", "{$after_widget}": `</aside>`,
"{$before_title}": `<h2 class="widget-title">`, "{$aria_label}": "",
"{$after_title}": `</h2>`, "{$title}": "",
"{$button}": "搜索", "{$before_title}": `<h2 class="widget-title">`,
"{$placeholder}": "搜索&hellip;", "{$after_title}": `</h2>`,
"{$label}": "搜索:", "{$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>
@ -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")
} }