Compare commits

...

2 Commits

Author SHA1 Message Date
00c16c03a4 小优化 2023-03-18 22:26:01 +08:00
ec4dfad86a 优化 fix bug 2023-03-18 21:02:24 +08:00
4 changed files with 49 additions and 49 deletions

View File

@ -55,7 +55,7 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(index, 100)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(index, 100))
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(detail, 100)) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(detail, 100))
h.PushGroupHandleFn(constraints.AllStats, 99, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle) h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle)
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20)) h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
pushScripts(h) pushScripts(h)

View File

@ -3,6 +3,7 @@ package components
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"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/pkg/constraints/widgets"
@ -12,7 +13,7 @@ import (
"strings" "strings"
) )
var widgetFn = map[string]wp.Components{ var widgetFn = map[string]wp.Components[string]{
"search": {Fn: widget.Search, CacheKey: "widgetSearch"}, "search": {Fn: widget.Search, CacheKey: "widgetSearch"},
"recent-posts": {Fn: widget.RecentPosts}, "recent-posts": {Fn: widget.RecentPosts},
"recent-comments": {Fn: widget.RecentComments}, "recent-comments": {Fn: widget.RecentComments},
@ -21,12 +22,13 @@ var widgetFn = map[string]wp.Components{
"meta": {Fn: widget.Meta, CacheKey: "widgetMeta"}, "meta": {Fn: widget.Meta, CacheKey: "widgetMeta"},
} }
type Widget struct { func WidgetArea(h *wp.Handle) {
Fn func(*wp.Handle) string sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars)
CacheKey string h.PushComponents(constraints.SidebarsWidgets, sidebar...)
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
} }
func WidgetArea(h *wp.Handle) { func sidebars(h *wp.Handle) []wp.Components[string] {
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
beforeWidget, ok := args["{$before_widget}"] beforeWidget, ok := args["{$before_widget}"]
if !ok { if !ok {
@ -35,31 +37,29 @@ func WidgetArea(h *wp.Handle) {
delete(args, "{$before_widget}") delete(args, "{$before_widget}")
} }
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
sidebar := slice.FilterAndMap(v, func(t any) (wp.Components, 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] components, ok := widgetFn[name]
if ok { if !ok {
if id != "2" { return components, false
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
}
if beforeWidget != "" {
n := strings.ReplaceAll(name, "-", "_")
if name == "recent-posts" {
n = "recent_entries"
}
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
}
for k, val := range args {
wp.SetComponentsArgsForMap(h, name, k, val)
}
components.Order = 10
return components, true
} }
return components, false if id != "2" {
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
}
if beforeWidget != "" {
n := strings.ReplaceAll(name, "-", "_")
if name == "recent-posts" {
n = "recent_entries"
}
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
}
for k, val := range args {
wp.SetComponentsArgsForMap(h, name, k, val)
}
components.Order = 10
return components, true
}) })
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
} }

View File

@ -27,7 +27,7 @@ type Handle struct {
Stats int Stats int
templ string templ string
class []string class []string
components map[string][]Components components map[string][]Components[string]
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall handleFns map[int][]HandleCall
err error err error
@ -39,9 +39,9 @@ type Handle struct {
type HandlePlugins map[string]HandleFn[*Handle] type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行 // Components Order 为执行顺序,降序执行
type Components struct { type Components[T any] struct {
Str string Val T
Fn func(*Handle) string Fn func(*Handle) T
Order int Order int
CacheKey string CacheKey string
} }
@ -178,15 +178,15 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
scene: scene, scene: scene,
Stats: constraints.Ok, Stats: constraints.Ok,
themeMods: mods, themeMods: mods,
components: make(map[string][]Components), components: make(map[string][]Components[string]),
handleFns: make(map[int][]HandleCall), handleFns: make(map[int][]HandleCall),
componentsArgs: make(map[string]any), componentsArgs: make(map[string]any),
componentFilterFn: make(map[string][]func(*Handle, string) string), componentFilterFn: make(map[string][]func(*Handle, string) string),
} }
} }
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components { func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components[string] {
return Components{Fn: fn, CacheKey: name, Order: order} return Components[string]{Fn: fn, CacheKey: name, Order: order}
} }
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
@ -205,7 +205,7 @@ func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
h.ginH[name] = reload.GetAnyValBys(name, h, fn) h.ginH[name] = reload.GetAnyValBys(name, h, fn)
} }
func (h *Handle) PushHeadScript(fn ...Components) { func (h *Handle) PushHeadScript(fn ...Components[string]) {
h.PushComponents(constraints.HeadScript, fn...) h.PushComponents(constraints.HeadScript, fn...)
} }
func (h *Handle) PushGroupHeadScript(order int, str ...string) { func (h *Handle) PushGroupHeadScript(order int, str ...string) {
@ -215,7 +215,7 @@ func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Ha
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...) h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
} }
func (h *Handle) PushFooterScript(fn ...Components) { func (h *Handle) PushFooterScript(fn ...Components[string]) {
h.PushComponents(constraints.FooterScript, fn...) h.PushComponents(constraints.FooterScript, fn...)
} }
@ -299,24 +299,24 @@ func (h *Handle) CommonComponents() {
}, 0)) }, 0))
} }
func (h *Handle) PushComponents(name string, components ...Components) { func (h *Handle) PushComponents(name string, components ...Components[string]) {
h.components[name] = append(h.components[name], components...) h.components[name] = append(h.components[name], components...)
} }
func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) { func (h *Handle) PushGroupComponentStrs(name string, order int, str ...string) {
var calls []Components var calls []Components[string]
for _, fn := range fns { for _, fn := range str {
calls = append(calls, Components{ calls = append(calls, Components[string]{
Str: fn, Val: fn,
Order: order, Order: order,
}) })
} }
h.components[name] = append(h.components[name], calls...) h.components[name] = append(h.components[name], calls...)
} }
func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) { func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) {
var calls []Components var calls []Components[string]
for _, fn := range fns { for _, fn := range fns {
calls = append(calls, Components{ calls = append(calls, Components[string]{
Fn: fn, Fn: fn,
Order: order, Order: order,
}) })
@ -326,13 +326,13 @@ func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Hand
func CalComponents(h *Handle) { func CalComponents(h *Handle) {
for k, ss := range h.components { for k, ss := range h.components {
slice.Sort(ss, func(i, j Components) bool { slice.Sort(ss, func(i, j Components[string]) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
var s []string var s []string
for _, component := range ss { for _, component := range ss {
if component.Str != "" { if component.Val != "" {
s = append(s, component.Str) s = append(s, component.Val)
continue continue
} }
if component.Fn != nil { if component.Fn != nil {

View File

@ -57,7 +57,7 @@ func Raw(str string, limit int, u string) string {
} }
} }
if end >= limit || i >= total-1 { if end >= limit || i >= total {
break break
} }
if ru[i] == l { if ru[i] == l {
@ -71,8 +71,8 @@ func Raw(str string, limit int, u string) string {
end++ end++
} }
} }
if i > total-1 { if i > total {
i = total - 1 i = total
} }
content = string(ru[:i]) content = string(ru[:i])