diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 9157658..bb5409e 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -124,6 +124,9 @@ func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { return a.data } +// CategoriesTags categories or tags +// +// t is constraints.Tag or constraints.Category func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy { r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) logs.ErrPrintln(err, "get category err") diff --git a/internal/pkg/constraints/widgets/constraints.go b/internal/pkg/constraints/widgets/constraints.go index c978f70..37794b3 100644 --- a/internal/pkg/constraints/widgets/constraints.go +++ b/internal/pkg/constraints/widgets/constraints.go @@ -5,4 +5,5 @@ const ( RecentPostsArgs = "RecentPostsArgs" RecentCommentsArgs = "RecentCommentsArgs" ArchiveArgs = "ArchiveArgs" + Categories = "Categories" ) diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 5ab6ebb..af9f0e9 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -10,6 +10,7 @@ import ( "github.com/fthvgb1/wp-go/internal/plugins/wphandle" "github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp/components" + "github.com/fthvgb1/wp-go/internal/theme/wp/components/widget" "strings" ) @@ -33,7 +34,7 @@ func Init(fs embed.FS) { logs.ErrPrintln(err, "解析colorscheme失败") } -var pipe = wp.HandlePipe(wp.Render, dispatch) +var pipe = wp.HandlePipe(wp.Render, widget.IsCategory, dispatch) func Hook(h *wp.Handle) { pipe(h) diff --git a/internal/theme/wp/components/widget/category.go b/internal/theme/wp/components/widget/category.go index 4a6f69a..5068bec 100644 --- a/internal/theme/wp/components/widget/category.go +++ b/internal/theme/wp/components/widget/category.go @@ -12,6 +12,7 @@ import ( "github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/safety" + "net/http" "strings" ) @@ -25,18 +26,23 @@ var categoryConfig = func() safety.Var[map[any]any] { "title": "分类", }) categoryArgs.Store(map[string]string{ - "{$before_widget}": `", - "{$before_title}": `

`, - "{$after_title}": "

", - "{$before_sidebar}": "", - "{$after_sidebar}": "", - "{$nav}": "", - "{$navCloser}": "", - "{$title}": "", - "{$dropdown_id}": "archives-dropdown-2", - "{$dropdown_type}": "monthly", - "{$dropdown_label}": "选择月份", + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + "{$before_sidebar}": "", + "{$after_sidebar}": "", + "{$class}": "postform", + "{$show_option_none}": "选择分类", + "{$name}": "cat", + "{$id}": "cat", + "{$required}": "", + "{$nav}": "", + "{$navCloser}": "", + "{$title}": "", + "{$dropdown_id}": "archives-dropdown-2", + "{$dropdown_type}": "monthly", + "{$dropdown_label}": "选择月份", }) return v }() @@ -50,7 +56,7 @@ var categoryTemplate = `{$before_widget} ` func Category(h *wp.Handle) string { - args := wp.GetComponentsArgs(h, widgets.ArchiveArgs, categoryArgs.Load()) + args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs.Load()) args = maps.FilterZeroMerge(categoryArgs.Load(), args) conf := wpconfig.GetPHPArrayVal("widget_categories", categoryConfig.Load(), int64(2)) conf = maps.FilterZeroMerge(categoryConfig.Load(), conf) @@ -59,6 +65,7 @@ func Category(h *wp.Handle) string { dropdown := conf["dropdown"].(int64) categories := cache.CategoriesTags(h.C, constraints.Category) if dropdown == 1 { + t = strings.ReplaceAll(t, "{$html}", categoryDropdown(h, args, conf, categories)) } else { t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories)) @@ -87,3 +94,92 @@ func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categori s.WriteString("") return s.String() } + +var categoryDropdownJs = `/* 0 ) { + dropdown.parentNode.submit(); + } + } + dropdown.onchange = onCatChange; +})(); +/* ]]> */ +` + +func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string { + s := str.NewBuilder() + s.WriteString(`
+`) + s.Sprintf(` +`, args["{$id}"], args["{$title}"]) + if len(categories) > 0 { + s.Sprintf(` \n") + } + s.WriteString("
\n") + attr := "" + if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "script") { + attr = ` type="text/javascript"` + } + s.Sprintf(` +`, attr) + s.Sprintf(categoryDropdownJs, args["{$id}"]) + s.WriteString("\n") + return s.String() +} + +func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) { + if h.Scene() != constraints.Home { + next(h) + return + } + name, ok := parseDropdownCate(h) + if !ok { + next(h) + return + } + h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name)) +} + +func parseDropdownCate(h *wp.Handle) (cateName string, r bool) { + cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs.Load()) + name, ok := cate["{$name}"] + if !ok || name == "" { + return + } + cat := h.C.Query(name) + if cat == "" { + return + } + cates := slice.SimpleToMap(cache.CategoriesTags(h.C, constraints.Category), func(v models.TermsMy) uint64 { + return v.Terms.TermId + }) + cc, r := cates[str.ToInteger[uint64](cat, 0)] + if !r { + return + } + cateName = cc.Name + return +}