package plugins import ( "context" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-gonic/gin" "strconv" "strings" ) type CommentHandler struct { *gin.Context comments []*Comments maxDepth int depth int isTls bool i CommentHtml isThreadComments bool } type Comments struct { models.Comments Children []*Comments } type CommentHtml interface { FormatLi(c context.Context, m models.Comments, depth, maxDepth, page int, isTls, isThreadComments bool, eo, parent string) string FloorOrder(wpOrder string, i, j models.PostComments) bool } func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, maxDepth int) string { tree := treeComments(comments) var isTls bool if c.Request.TLS != nil { isTls = true } else { isTls = "https" == strings.ToLower(c.Request.Header.Get("X-Forwarded-Proto")) } h := CommentHandler{ Context: c, comments: tree, maxDepth: maxDepth, depth: 1, isTls: isTls, i: i, } return h.formatComment(h.comments) } func (d CommentHandler) formatComment(comments []*Comments) (html string) { s := str.NewBuilder() if d.depth >= d.maxDepth { comments = d.findComments(comments) } order := wpconfig.GetOption("comment_order") slice.Sort(comments, func(i, j *Comments) bool { if order == "asc" { return i.CommentDate.Sub(j.CommentDate) < 0 } return i.CommentDate.Sub(j.CommentDate) > 0 }) for i, comment := range comments { eo := "even" if (i+1)%2 == 0 { eo = "odd" } parent := "" fl := false if len(comment.Children) > 0 && d.depth < d.maxDepth+1 { parent = "parent" fl = true } s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, d.maxDepth, 1, d.isTls, d.isThreadComments, eo, parent)) if fl { d.depth++ s.WriteString(`
{{CommentContent}}