From cc09668fd7c0471e060b5231991e42e7fccdfdf4 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 10 Mar 2023 19:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E8=BF=91=E8=AF=84=E8=AE=BA=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/wp/recentcomments.go | 61 +++++++++++++++++++++++++++++ internal/theme/wp/recentposts.go | 5 +-- 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 internal/theme/wp/recentcomments.go diff --git a/internal/theme/wp/recentcomments.go b/internal/theme/wp/recentcomments.go new file mode 100644 index 0000000..e8dc013 --- /dev/null +++ b/internal/theme/wp/recentcomments.go @@ -0,0 +1,61 @@ +package wp + +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/pkg/cache" + "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/theme/wp/components" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "strings" +) + +var recentCommentsArgs = map[string]string{ + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + "{$before_sidebar}": "", + "{$after_sidebar}": "", + "{$nav}": "", + "{$navCloser}": "", + "{$title}": "", + "{$recent_comments_id}": "recentcomments", +} + +var recentCommentConf = map[any]any{ + "number": int64(5), + "title": "近期评论", +} + +var recentCommentsTemplate = `{$before_widget} +{$nav} +{$title} + +{$navCloser} +{$after_widget} +` + +func RecentComments(h *Handle) string { + args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs) + args = maps.Merge(recentCommentsArgs, args) + conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-comments", recentCommentConf, int64(2)) + args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) + if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { + args["{$nav}"] = fmt.Sprintf(`" + } + comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string { + return fmt.Sprintf(`
  • +%s发表在《 + %s + 》 +
  • `, t.CommentAuthor, t.CommentId, t.CommentPostId, t.PostTitle) + }) + s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n")) + return str.Replace(s, args) +} diff --git a/internal/theme/wp/recentposts.go b/internal/theme/wp/recentposts.go index 4274938..7fb1f75 100644 --- a/internal/theme/wp/recentposts.go +++ b/internal/theme/wp/recentposts.go @@ -3,7 +3,6 @@ package wp import ( "fmt" "github.com/fthvgb1/wp-go/helper/maps" - "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" @@ -66,9 +65,9 @@ func RecentPosts(h *Handle) string { ariaCurrent = ` aria-current="page"` } return fmt.Sprintf(`
  • - %s + %s %s -
  • `, str.Join("/p/", number.ToString(t.Id)), ariaCurrent, t.PostTitle, date) + `, t.Id, ariaCurrent, t.PostTitle, date) }) s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n")) return str.Replace(s, args)