package widget import ( "fmt" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/wpconfig" "strings" ) var archiveTemplate = `{$before_widget} {$title} {$nav} {$html} {$navCloser} {$after_widget} ` func archiveArgs() map[string]string { return map[string]string{ "{$before_sidebar}": "", "{$after_sidebar}": "", "{$nav}": "", "{$navCloser}": "", "{$title}": "", "{$dropdown_id}": "archives-dropdown-2", "{$dropdown_type}": "monthly", "{$dropdown_label}": "选择月份", } } func archivesConfig() map[any]any { return map[any]any{ "count": int64(0), "dropdown": int64(0), "title": "归档", } } func Archive(h *wp.Handle, id string) string { conf := reload.GetAnyValBys("widget-archive-conf", h, func(h *wp.Handle) map[any]any { archivesConfig := archivesConfig() conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2)) return conf }) args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string { archiveArgs := archiveArgs() commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs()) args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs) args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args) args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive")) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) return args }) s := archiveTemplate if int64(1) == conf["dropdown"].(int64) { s = strings.ReplaceAll(s, "{$html}", archiveDropDown(h, conf, args, cache.Archives(h.C))) } else { s = strings.ReplaceAll(s, "{$html}", archiveUl(h, conf, args, cache.Archives(h.C))) } return h.ComponentFilterFnHook(widgets.Archive, str.Replace(s, args)) } var dropdownScript = ` ` func archiveDropDown(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string { option := str.NewBuilder() option.Sprintf(``, args["{$dropdown_label}"]) month := strings.TrimLeft(h.Index.Param.Month, "0") showCount := conf["count"].(int64) for _, archive := range archives { sel := "" if h.Index.Param.Year == archive.Year && month == archive.Month { sel = "selected" } count := "" if showCount == int64(1) { count = fmt.Sprintf("(%v)", archive.Posts) } option.Sprintf(` `, sel, archive.Year, archive.Month, archive.Year, archive.Month, count) } s := str.NewBuilder() s.Sprintf(` %s `, args["{$dropdown_id}"], args["{$title}"], args["{$dropdown_id}"], option.String(), dropdownScript) return s.String() } func archiveUl(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string { if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { args["{$nav}"] = fmt.Sprintf(`" } s := str.NewBuilder() s.WriteString(`