package plugins import ( "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/gin-gonic/gin" "net/url" "strconv" "strings" ) type CommentHandler struct { *gin.Context comments []*Comments maxDepth int depth int isTls bool i CommentHtml } type Comments struct { models.Comments Children []*Comments } type CommentHtml interface { Sort(i, j *Comments) bool FormatLi(c *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string } func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, maxDepth int) string { tree := treeComments(comments) u := c.Request.Header.Get("Referer") var isTls bool if u != "" { uu, _ := url.Parse(u) if uu.Scheme == "https" { isTls = true } } h := CommentHandler{ Context: c, comments: tree, maxDepth: maxDepth, depth: 1, isTls: isTls, i: i, } return h.formatComment(h.comments, true) } func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html string) { s := str.NewBuilder() if d.depth > d.maxDepth { comments = d.findComments(comments) } slice.SortSelf(comments, d.i.Sort) 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.isTls, eo, parent)) if fl { d.depth++ s.WriteString(`
{{CommentContent}}