2023-01-17 15:18:31 +00:00
|
|
|
package twentyseventeen
|
|
|
|
|
|
|
|
import (
|
2023-01-25 18:26:36 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/plugin/pagination"
|
2023-01-17 15:18:31 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-18 15:02:59 +00:00
|
|
|
"strings"
|
2023-01-17 15:18:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const ThemeName = "twentyseventeen"
|
|
|
|
|
2023-01-18 15:11:26 +00:00
|
|
|
var paginate = func() plugins.PageEle {
|
|
|
|
p := plugins.TwentyFifteenPagination()
|
|
|
|
p.PrevEle = `<a class="prev page-numbers" href="%s"><svg class="icon icon-arrow-left" aria-hidden="true" role="img"> <use href="#icon-arrow-left" xlink:href="#icon-arrow-left"></use> </svg>
|
|
|
|
<span class="screen-reader-text">上一页</span></a>`
|
|
|
|
p.NextEle = strings.Replace(p.NextEle, "下一页", `<span class="screen-reader-text">下一页</span>
|
|
|
|
<svg class="icon icon-arrow-right" aria-hidden="true" role="img"> <use href="#icon-arrow-right" xlink:href="#icon-arrow-right"></use>
|
|
|
|
</svg>`, 1)
|
|
|
|
return p
|
|
|
|
}()
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
type handle struct {
|
|
|
|
c *gin.Context
|
|
|
|
ginH gin.H
|
|
|
|
scene int
|
|
|
|
status int
|
|
|
|
stats int
|
|
|
|
templ string
|
|
|
|
}
|
|
|
|
|
|
|
|
func Hook(status int, c *gin.Context, ginH gin.H, scene, stats int) {
|
|
|
|
h := handle{
|
|
|
|
c: c,
|
|
|
|
ginH: ginH,
|
|
|
|
scene: scene,
|
|
|
|
status: status,
|
|
|
|
stats: stats,
|
|
|
|
templ: "twentyseventeen/posts/index.gohtml",
|
|
|
|
}
|
|
|
|
ginH["HeaderImage"] = h.getHeaderImage(c)
|
2023-01-31 16:58:42 +00:00
|
|
|
if stats == plugins.Empty404 {
|
2023-02-04 16:07:10 +00:00
|
|
|
c.HTML(status, h.templ, ginH)
|
|
|
|
return
|
2023-01-31 16:58:42 +00:00
|
|
|
}
|
2023-02-04 16:07:10 +00:00
|
|
|
if scene == plugins.Detail {
|
|
|
|
h.detail()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.index()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h handle) index() {
|
|
|
|
posts := h.ginH["posts"].([]models.Posts)
|
|
|
|
p, ok := h.ginH["pagination"]
|
|
|
|
if ok {
|
|
|
|
pp, ok := p.(pagination.ParsePagination)
|
2023-01-18 15:02:59 +00:00
|
|
|
if ok {
|
2023-02-04 16:07:10 +00:00
|
|
|
h.ginH["pagination"] = pagination.Paginate(paginate, pp)
|
2023-01-18 15:02:59 +00:00
|
|
|
}
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
2023-02-04 16:07:10 +00:00
|
|
|
h.ginH["bodyClass"] = h.bodyClass()
|
|
|
|
h.ginH["posts"] = h.postThumbnail(posts, h.scene)
|
|
|
|
h.c.HTML(h.status, h.templ, h.ginH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h handle) detail() {
|
|
|
|
post := h.ginH["post"].(models.Posts)
|
|
|
|
h.ginH["bodyClass"] = h.bodyClass()
|
|
|
|
//host, _ := wpconfig.Options.Load("siteurl")
|
|
|
|
host := ""
|
|
|
|
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
|
|
|
|
img.Width = img.OriginAttachmentData.Width
|
|
|
|
img.Height = img.OriginAttachmentData.Height
|
|
|
|
img.Sizes = "100vw"
|
|
|
|
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
|
|
|
|
post.Thumbnail = img
|
|
|
|
h.ginH["post"] = post
|
|
|
|
comments := h.ginH["comments"].([]models.Comments)
|
|
|
|
dep := h.ginH["maxDep"].(int)
|
|
|
|
h.ginH["comments"] = plugins.FormatComments(h.c, comment{}, comments, dep)
|
|
|
|
h.templ = "twentyseventeen/posts/detail.gohtml"
|
|
|
|
h.c.HTML(h.status, h.templ, h.ginH)
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 16:07:42 +00:00
|
|
|
type comment struct {
|
|
|
|
plugins.CommonCommentFormat
|
|
|
|
}
|
|
|
|
|
2023-01-30 12:48:30 +00:00
|
|
|
func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string {
|
2023-01-26 16:07:42 +00:00
|
|
|
templ := plugins.CommonLi()
|
|
|
|
templ = strings.ReplaceAll(templ, `<a rel="nofollow" class="comment-reply-link"
|
|
|
|
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
|
|
|
|
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"
|
|
|
|
data-replyto="回复给{{CommentAuthor}}"
|
|
|
|
aria-label="回复给{{CommentAuthor}}">回复</a>`, `<a rel="nofollow" class="comment-reply-link"
|
|
|
|
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
|
|
|
|
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"
|
|
|
|
data-replyto="回复给{{CommentAuthor}}"
|
|
|
|
aria-label="回复给{{CommentAuthor}}"><svg class="icon icon-mail-reply" aria-hidden="true" role="img"> <use href="#icon-mail-reply" xlink:href="#icon-mail-reply"></use> </svg>回复</a>`)
|
2023-01-30 12:48:30 +00:00
|
|
|
return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent)
|
2023-01-26 16:07:42 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func (h handle) postThumbnail(posts []models.Posts, scene int) []models.Posts {
|
2023-01-25 18:26:36 +00:00
|
|
|
return slice.Map(posts, func(t models.Posts) models.Posts {
|
|
|
|
if t.Thumbnail.Path != "" {
|
|
|
|
if slice.IsContained(scene, []int{plugins.Home, plugins.Archive, plugins.Search}) {
|
|
|
|
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
|
|
|
|
} else {
|
|
|
|
t.Thumbnail.Sizes = "100vw"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
|
2023-01-17 15:18:31 +00:00
|
|
|
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
|
2023-01-25 18:26:36 +00:00
|
|
|
r.Width = 2000
|
|
|
|
r.Height = 1200
|
2023-02-04 16:07:10 +00:00
|
|
|
hs, err := cache.GetHeaderImages(c, ThemeName)
|
2023-01-31 16:58:42 +00:00
|
|
|
if err != nil {
|
|
|
|
logs.ErrPrintln(err, "获取页眉背景图失败")
|
|
|
|
} else if len(hs) > 0 && err == nil {
|
|
|
|
_, r = slice.Rand(hs)
|
2023-02-04 16:07:10 +00:00
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
2023-01-31 16:58:42 +00:00
|
|
|
r.Sizes = "100vw"
|
2023-01-17 15:18:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-25 18:26:36 +00:00
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
func (h handle) bodyClass() string {
|
2023-01-25 18:26:36 +00:00
|
|
|
s := ""
|
2023-02-04 16:07:10 +00:00
|
|
|
switch h.scene {
|
|
|
|
case plugins.Search:
|
2023-02-05 12:33:33 +00:00
|
|
|
s = "search-no-results"
|
2023-02-04 16:07:10 +00:00
|
|
|
if len(h.ginH["posts"].([]models.Posts)) > 0 {
|
2023-01-25 18:26:36 +00:00
|
|
|
s = "search-results"
|
|
|
|
}
|
2023-02-05 12:33:33 +00:00
|
|
|
case plugins.Category, plugins.Tag:
|
2023-02-04 16:07:10 +00:00
|
|
|
cat := h.c.Param("category")
|
2023-02-05 12:33:33 +00:00
|
|
|
if cat == "" {
|
|
|
|
cat = h.c.Param("tag")
|
2023-02-04 16:07:10 +00:00
|
|
|
}
|
2023-02-05 12:33:33 +00:00
|
|
|
_, cate := slice.SearchFirst(cache.CategoriesTags(h.c, h.scene), func(my models.TermsMy) bool {
|
2023-02-04 16:07:10 +00:00
|
|
|
return my.Name == cat
|
|
|
|
})
|
|
|
|
if cate.Slug[0] != '%' {
|
|
|
|
s = cate.Slug
|
|
|
|
}
|
|
|
|
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
|
|
|
|
case plugins.Detail:
|
|
|
|
s = fmt.Sprintf("postid-%d", h.ginH["post"].(models.Posts).Id)
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|
2023-02-05 10:29:55 +00:00
|
|
|
return str.Join(class[h.scene], s)
|
|
|
|
}
|
|
|
|
|
|
|
|
var class = map[int]string{
|
|
|
|
plugins.Home: "home blog ",
|
|
|
|
plugins.Archive: "archive date page-two-column",
|
|
|
|
plugins.Category: "archive category page-two-column",
|
|
|
|
plugins.Tag: "archive category page-two-column ",
|
|
|
|
plugins.Search: "search ",
|
|
|
|
plugins.Detail: "post-template-default single single-post single-format-standard ",
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|