2023-01-17 15:18:31 +00:00
|
|
|
package twentyseventeen
|
|
|
|
|
|
|
|
import (
|
2023-01-25 18:26:36 +00:00
|
|
|
"fmt"
|
2023-02-09 15:15:10 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-01-25 18:26:36 +00:00
|
|
|
"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"
|
2023-02-10 13:23:30 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
2023-02-08 15:49:48 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/theme/common"
|
2023-02-14 11:47:47 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
2023-01-17 15:18:31 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-11 15:51:07 +00:00
|
|
|
"net/http"
|
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 {
|
2023-02-11 15:51:07 +00:00
|
|
|
*common.Handle
|
2023-02-04 16:07:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
func newHandle(h *common.Handle) *handle {
|
|
|
|
return &handle{Handle: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexHandle struct {
|
|
|
|
*common.IndexHandle
|
|
|
|
h *handle
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIndexHandle(iHandle *common.IndexHandle) *indexHandle {
|
|
|
|
return &indexHandle{IndexHandle: iHandle, h: newHandle(iHandle.Handle)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type detailHandle struct {
|
|
|
|
*common.DetailHandle
|
|
|
|
h *handle
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDetailHandle(dHandle *common.DetailHandle) *detailHandle {
|
|
|
|
return &detailHandle{DetailHandle: dHandle, h: newHandle(dHandle.Handle)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Hook(h *common.Handle) {
|
2023-02-10 16:32:46 +00:00
|
|
|
h.WidgetAreaData()
|
2023-02-11 15:51:07 +00:00
|
|
|
h.GetPassword()
|
|
|
|
h.GinH["HeaderImage"] = getHeaderImage(h.C)
|
2023-02-10 13:23:30 +00:00
|
|
|
if h.Scene == constraints.Detail {
|
2023-02-11 15:51:07 +00:00
|
|
|
newDetailHandle(common.NewDetailHandle(h)).Detail()
|
2023-02-04 16:07:10 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
newIndexHandle(common.NewIndexHandle(h)).Index()
|
2023-02-08 15:49:48 +00:00
|
|
|
}
|
2023-02-04 16:07:10 +00:00
|
|
|
|
2023-02-12 11:19:53 +00:00
|
|
|
var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {
|
|
|
|
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{
|
2023-02-09 15:15:10 +00:00
|
|
|
"twentyseventeen_postThumbnail": postThumbnail,
|
|
|
|
})
|
2023-02-09 08:08:18 +00:00
|
|
|
}()
|
2023-02-04 16:07:10 +00:00
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
func (i *indexHandle) Index() {
|
|
|
|
i.Templ = "twentyseventeen/posts/index.gohtml"
|
2023-02-11 15:58:40 +00:00
|
|
|
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
2023-02-10 16:32:46 +00:00
|
|
|
if err != nil {
|
2023-02-11 15:58:40 +00:00
|
|
|
i.Stats = constraints.Error404
|
|
|
|
i.Code = http.StatusNotFound
|
|
|
|
i.GinH["bodyClass"] = i.h.bodyClass()
|
2023-02-11 15:51:07 +00:00
|
|
|
i.C.HTML(i.Code, i.Templ, i.GinH)
|
2023-02-10 16:32:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
i.PageEle = paginate
|
|
|
|
i.ExecListPagePlugin(pluginFns)
|
|
|
|
i.Pagination()
|
2023-02-11 15:58:40 +00:00
|
|
|
i.GinH["bodyClass"] = i.h.bodyClass()
|
2023-02-11 15:51:07 +00:00
|
|
|
i.C.HTML(i.Code, i.Templ, i.GinH)
|
2023-02-04 16:07:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
func (d *detailHandle) Detail() {
|
|
|
|
err := d.BuildDetailData()
|
|
|
|
if err != nil {
|
|
|
|
d.Code = http.StatusNotFound
|
2023-02-11 15:58:40 +00:00
|
|
|
d.Stats = constraints.Error404
|
|
|
|
d.GinH["bodyClass"] = d.h.bodyClass()
|
2023-02-11 15:51:07 +00:00
|
|
|
d.C.HTML(d.Code, d.Templ, d.GinH)
|
2023-02-09 12:49:33 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-11 15:58:40 +00:00
|
|
|
d.GinH["bodyClass"] = d.h.bodyClass()
|
2023-02-14 11:47:47 +00:00
|
|
|
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail")
|
2023-02-04 16:07:10 +00:00
|
|
|
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)
|
2023-02-11 15:51:07 +00:00
|
|
|
d.Post.Thumbnail = img
|
|
|
|
d.GinH["post"] = d.Post
|
|
|
|
d.CommentRender = commentFormat
|
|
|
|
d.RenderComment()
|
|
|
|
d.PasswordProject()
|
|
|
|
d.Templ = "twentyseventeen/posts/detail.gohtml"
|
|
|
|
d.C.HTML(d.Code, d.Templ, d.GinH)
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 12:49:33 +00:00
|
|
|
var commentFormat = comment{}
|
|
|
|
|
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-10 16:32:46 +00:00
|
|
|
func postThumbnail(next common.Fn[models.Posts], h *common.Handle, t models.Posts) models.Posts {
|
2023-02-09 08:08:18 +00:00
|
|
|
if t.Thumbnail.Path != "" {
|
2023-02-09 09:12:33 +00:00
|
|
|
t.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
|
2023-02-10 13:23:30 +00:00
|
|
|
if h.Scene == constraints.Detail {
|
2023-02-09 08:08:18 +00:00
|
|
|
t.Thumbnail.Sizes = "100vw"
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|
2023-02-09 08:08:18 +00:00
|
|
|
}
|
|
|
|
return next(t)
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:51:07 +00:00
|
|
|
func 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-11 15:51:07 +00:00
|
|
|
func (i *handle) bodyClass() string {
|
2023-01-25 18:26:36 +00:00
|
|
|
s := ""
|
2023-02-11 15:51:07 +00:00
|
|
|
if constraints.Ok != i.Stats {
|
2023-02-09 12:49:33 +00:00
|
|
|
return "error404"
|
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
switch i.Scene {
|
2023-02-10 13:23:30 +00:00
|
|
|
case constraints.Search:
|
2023-02-05 12:33:33 +00:00
|
|
|
s = "search-no-results"
|
2023-02-11 15:51:07 +00:00
|
|
|
if len(i.GinH["posts"].([]models.Posts)) > 0 {
|
2023-01-25 18:26:36 +00:00
|
|
|
s = "search-results"
|
|
|
|
}
|
2023-02-10 13:23:30 +00:00
|
|
|
case constraints.Category, constraints.Tag:
|
2023-02-11 15:51:07 +00:00
|
|
|
cat := i.C.Param("category")
|
2023-02-05 12:33:33 +00:00
|
|
|
if cat == "" {
|
2023-02-11 15:51:07 +00:00
|
|
|
cat = i.C.Param("tag")
|
2023-02-04 16:07:10 +00:00
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
_, cate := slice.SearchFirst(cache.CategoriesTags(i.C, i.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)
|
2023-02-10 13:23:30 +00:00
|
|
|
case constraints.Detail:
|
2023-02-11 15:51:07 +00:00
|
|
|
s = fmt.Sprintf("postid-%d", i.GinH["post"].(models.Posts).Id)
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|
2023-02-11 15:51:07 +00:00
|
|
|
return str.Join(class[i.Scene], s)
|
2023-02-05 10:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var class = map[int]string{
|
2023-02-10 13:23:30 +00:00
|
|
|
constraints.Home: "home blog ",
|
|
|
|
constraints.Archive: "archive date page-two-column",
|
|
|
|
constraints.Category: "archive category page-two-column",
|
|
|
|
constraints.Tag: "archive category page-two-column ",
|
|
|
|
constraints.Search: "search ",
|
|
|
|
constraints.Detail: "post-template-default single single-post single-format-standard ",
|
2023-01-25 18:26:36 +00:00
|
|
|
}
|
2023-02-15 16:32:02 +00:00
|
|
|
|
|
|
|
func ThemeSupport() map[string]struct{} {
|
|
|
|
return map[string]struct{}{
|
|
|
|
"custom-header": {},
|
|
|
|
"wp-custom-logo": {},
|
|
|
|
"responsive-embeds": {},
|
|
|
|
"post-formats": {},
|
|
|
|
}
|
|
|
|
}
|