接入后台规档设置

This commit is contained in:
xing 2023-03-06 20:53:51 +08:00
parent 4f1a2f717e
commit 240a41f1eb
10 changed files with 228 additions and 28 deletions

View File

@ -55,3 +55,54 @@ func GetStrMapAnyValWithAny(key string, v map[string]any) (r any, o bool) {
}
return
}
func GetAnyAnyMapVal[T any](m map[any]any, k ...any) (r T, o bool) {
if len(k) > 1 {
val, ok := m[k[0]]
if ok {
vx, ok := val.(map[any]any)
if ok {
r, o = GetAnyAnyMapVal[T](vx, k[1:]...)
}
}
} else {
x, ok := m[k[0]]
if ok {
vv, ok := x.(T)
if ok {
o = true
r = vv
}
}
}
return
}
func GetAnyAnyMapWithAny(v map[any]any, k ...any) (r any, o bool) {
if len(k) > 1 {
val, ok := v[k[0]]
if ok {
vx, ok := val.(map[any]any)
if ok {
r, o = GetAnyAnyMapWithAny(vx, k[1:]...)
}
}
} else {
x, ok := v[k[0]]
if ok {
o = true
r = x
}
}
return
}
func GetAnyAnyValWithDefaults[T any](m map[any]any, defaults T, key ...any) (r T) {
r = defaults
v, ok := GetAnyAnyMapVal[T](m, key...)
if !ok {
return
}
r = v
return
}

View File

@ -123,3 +123,96 @@ func TestGetStrMapAnyValWithAny(t *testing.T) {
})
}
}
func TestGetAnyAnyMapVal(t *testing.T) {
m := map[any]any{
1: "1",
"1": 2,
"xxx": map[any]any{
"sss": []int{1, 2, 3},
5: "5",
},
}
t.Run("t1", func(t *testing.T) {
wantR := []int{1, 2, 3}
gotR, gotO := GetAnyAnyMapVal[[]int](m, "xxx", "sss")
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
if gotO != true {
t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true)
}
})
t.Run("t2", func(t *testing.T) {
wantR := "5"
gotR, gotO := GetAnyAnyMapVal[string](m, "xxx", 5)
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
if gotO != true {
t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true)
}
})
}
func TestGetAnyAnyMapWithAny(t *testing.T) {
m := map[any]any{
1: "1",
"1": 2,
"xxx": map[any]any{
"sss": []int{1, 2, 3},
5: "5",
},
}
t.Run("t1", func(t *testing.T) {
wantR := any([]int{1, 2, 3})
gotR, gotO := GetAnyAnyMapWithAny(m, "xxx", "sss")
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
if gotO != true {
t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true)
}
})
t.Run("t2", func(t *testing.T) {
wantR := any("5")
gotR, gotO := GetAnyAnyMapWithAny(m, "xxx", 5)
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
if gotO != true {
t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true)
}
})
}
func TestGetAnyAnyValWithDefaults(t *testing.T) {
m := map[any]any{
1: "1",
"1": 2,
"xxx": map[any]any{
"sss": []int{1, 2, 3},
5: "5",
},
}
t.Run("t1", func(t *testing.T) {
wantR := []int{1, 2, 3}
gotR := GetAnyAnyValWithDefaults[[]int](m, nil, "xxx", "sss")
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
})
t.Run("t2", func(t *testing.T) {
wantR := "xxx"
gotR := GetAnyAnyValWithDefaults[string](m, "xxx", "xxx", 55)
if !reflect.DeepEqual(gotR, wantR) {
t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR)
}
})
}

View File

@ -16,7 +16,7 @@ func UnPHPSerializeToStruct[T any](s string) (r T, err error) {
return
}
func UnPHPSerializeToAnyMap(s string) (map[string]any, error) {
func UnPHPSerializeToStrAnyMap(s string) (map[string]any, error) {
m := map[string]any{}
var r map[any]any
err := phpserialize.Unmarshal([]byte(s), &r)
@ -27,3 +27,11 @@ func UnPHPSerializeToAnyMap(s string) (map[string]any, error) {
m = maps.AnyAnyToStrAny(r)
return m, err
}
func UnPHPSerializeToAnyAnyMap(s string) (map[any]any, error) {
var r map[any]any
err := phpserialize.Unmarshal([]byte(s), &r)
if err != nil {
return nil, err
}
return r, err
}

View File

@ -22,7 +22,8 @@
</nav>
</aside>
<aside id="recent-comments-2" class="widget widget_recent_comments"><h2 class="widget-title">近期评论</h2>
<aside id="recent-comments-2" class="widget widget_recent_comments">
<h2 class="widget-title">近期评论</h2>
<nav aria-label="近期评论">
<ul id="recentcomments">
{{ range $i,$v := .recentComments}}
@ -33,18 +34,9 @@
</ul>
</nav>
</aside>
<aside id="archives-2" class="widget widget_archive"><h2 class="widget-title">归档</h2>
<nav aria-label="归档">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月</a>
</li>
{{end}}
</ul>
</nav>
</aside>
<aside id="categories-2" class="widget widget_categories"><h2 class="widget-title">分类</h2>
{{template "common/archives" .}}
<aside id="categories-2" class="widget widget_categories">
<h2 class="widget-title">分类</h2>
<nav aria-label="分类">
<ul>
{{range $k,$v := .categories}}

View File

@ -37,18 +37,7 @@
</ul>
</nav>
</section>
<section id="archives-2" class="widget widget_archive">
<h2 class="widget-title">归档</h2>
<nav aria-label="归档">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月</a>
</li>
{{end}}
</ul>
</nav>
</section>
{{template "common/archives" .}}
<section id="categories-2" class="widget widget_categories">
<h2 class="widget-title">分类</h2>
<nav aria-label="分类">

View File

@ -12,6 +12,7 @@ import (
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/plugin/pagination"
"strings"
)
type IndexHandle struct {
@ -87,7 +88,8 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
case constraints.Archive:
i.ginH["archiveYear"] = i.Param.Year
i.ginH["archiveMonth"] = strings.TrimLeft(i.Param.Month, "0")
posts, totalRaw, err = cache.GetMonthPostIds(i.C, i.Param.Year, i.Param.Month, i.Param.Page, i.Param.PageSize, i.Param.Order)
}

View File

@ -23,3 +23,42 @@
{{.customLogo|unescaped}}
{{end}}
{{end}}
{{define "common/archives"}}
<section id="archives-2" class="widget widget_archive">
<h2 class="widget-title">{{.archiveTitle}}</h2>
{{ if eq .archiveDropdown 1}}
<label class="screen-reader-text" for="archives-dropdown-2">{{.archiveTitle}}</label>
<select id="archives-dropdown-2" name="archive-dropdown">
<option value="">选择月份</option>
{{range $k,$v := .archives}}
<option {{if and (eq $.archiveYear $v.Year) (eq $.archiveMonth $v.Month) }}selected{{end}} value="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}"> {{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}} </option>
{{end}}
</select>
<script>
/* <![CDATA[ */
(function() {
const dropdown = document.getElementById("archives-dropdown-2");
function onSelectChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
document.location.href = this.options[ this.selectedIndex ].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
{{else}}
<nav aria-label="{{.archiveTitle}}">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}}</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
</section>
{{end}}

View File

@ -4,9 +4,14 @@ import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func (h *Handle) WidgetAreaData() {
h.ginH["showArchiveCount"] = wpconfig.GetPHPArrayValWithDefaults[int64]("widget_archives", 0, int64(2), "count")
h.ginH["archiveDropdown"] = wpconfig.GetPHPArrayValWithDefaults[int64]("widget_archives", 0, int64(2), "dropdown")
h.ginH["archiveTitle"] = wpconfig.GetPHPArrayValWithDefaults[string]("widget_archives", "归档", int64(2), "title")
h.ginH["archives"] = cache.Archives(h.C)
h.ginH["recentPosts"] = slice.Map(cache.RecentPosts(h.C, 5), ProjectTitle)
h.ginH["categories"] = cache.CategoriesTags(h.C, constraints.Category)

View File

@ -2,6 +2,8 @@ package wpconfig
import (
"context"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/internal/phphelper"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/safety"
@ -9,11 +11,13 @@ import (
)
var options safety.Map[string, string]
var phpArr safety.Map[string, map[any]any]
var ctx context.Context
func InitOptions() error {
options.Flush()
phpArr.Flush()
if ctx == nil {
ctx = context.Background()
}
@ -50,3 +54,20 @@ func GetLang() string {
}
return strings.Replace(s, "_", "-", 1)
}
func GetPHPArrayValWithDefaults[T any](optionName string, defaults T, key ...any) T {
op, ok := phpArr.Load(optionName)
if ok {
return maps.GetAnyAnyValWithDefaults(op, defaults, key...)
}
v := GetOption(optionName)
if v == "" {
return defaults
}
arr, err := phphelper.UnPHPSerializeToAnyAnyMap(v)
if err != nil {
return defaults
}
phpArr.Store(optionName, arr)
return maps.GetAnyAnyValWithDefaults(arr, defaults, key...)
}

View File

@ -140,7 +140,7 @@ func GetThemeMods(theme string) (r ThemeMods, err error) {
if mods == "" {
return
}
m, err := phphelper.UnPHPSerializeToAnyMap(mods)
m, err := phphelper.UnPHPSerializeToStrAnyMap(mods)
if err != nil {
return
}