wp-go/app/theme/twentyseventeen/twentyseventeen.go

238 lines
8.3 KiB
Go
Raw Normal View History

2023-01-17 15:18:31 +00:00
package twentyseventeen
import (
"context"
2023-01-25 18:26:36 +00:00
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/theme/wp/components"
"github.com/fthvgb1/wp-go/app/theme/wp/middleware"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-11-12 13:39:04 +00:00
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper"
2023-02-24 16:56:52 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/plugin/pagination"
"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)
commentPageEle = plugins.PaginationNav{
Currents: p.Current,
Prevs: p.Prev,
Nexts: p.Next,
Dotss: p.Dots,
Middles: p.Middle,
Steps: func() int {
return 2
},
Urlss: plugins.TwentyFifteenCommentPagination().Urls,
}
2023-01-18 15:11:26 +00:00
return p
}()
var commentPageEle pagination.Render
2023-03-01 05:17:12 +00:00
func Hook(h *wp.Handle) {
2023-05-03 15:57:49 +00:00
wp.Run(h, configs)
2023-02-24 11:34:19 +00:00
}
2023-04-07 14:59:07 +00:00
func configs(h *wp.Handle) {
2023-05-03 15:57:49 +00:00
wp.InitPipe(h)
middleware.CommonMiddleware(h)
2023-08-26 14:01:20 +00:00
h.AddActionFilter("bodyClass", calClass)
2023-04-30 13:17:33 +00:00
h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h)
2023-03-04 06:44:51 +00:00
pushScripts(h)
2023-06-15 10:00:57 +00:00
h.PushRender(constraints.AllStats, wp.NewHandleFn(calCustomHeader, 10.005, "calCustomHeader"))
wp.SetComponentsArgs(widgets.Widget, map[string]string{
2023-03-27 14:03:01 +00:00
"{$before_widget}": `<section id="%s" class="%s">`,
2023-03-17 10:26:08 +00:00
"{$after_widget}": `</section>`,
})
2023-04-21 17:12:39 +00:00
h.PushRender(constraints.AllStats,
2023-06-15 10:00:57 +00:00
wp.NewHandleFn(wp.PreTemplate, 70.005, "wp.PreTemplate"),
wp.NewHandleFn(errorsHandle, 80.005, "errorsHandle"),
2023-04-21 17:12:39 +00:00
)
2023-06-12 15:08:46 +00:00
videoHeader(h)
2024-01-14 14:07:16 +00:00
h.SetData("colophon", colophon)
setPaginationAndRender(h)
h.CommonComponents()
2023-05-09 05:44:20 +00:00
h.PushPostPlugin(postThumbnail)
wp.SetComponentsArgsForMap(widgets.Search, "{$form}", searchForm)
2023-06-15 10:00:57 +00:00
wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10.005, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10.005, "wp.DetailRender"))
h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100.005, "wp.Detail"), wp.NewHandleFn(postThumb, 90.005, "{theme}.postThumb"))
wp.PushIndexHandler(constraints.PipeData, h, wp.NewHandleFn(wp.Index, 100.005, "wp.Index"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90.005, "wp.PreCodeAndStats"))
}
2023-02-28 11:44:19 +00:00
func setPaginationAndRender(h *wp.Handle) {
h.PushHandler(constraints.PipeRender, constraints.Detail, wp.NewHandleFn(func(hh *wp.Handle) {
d := hh.GetDetailHandle()
d.CommentRender = commentFormat
d.CommentPageEle = commentPageEle
}, 150, "setPaginationAndRender"))
wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(func(hh *wp.Handle) {
i := hh.GetIndexHandle()
i.SetPageEle(paginate)
}, 150, "setPaginationAndRender"))
}
var searchForm = `<form role="search" method="get" class="search-form" action="/">
<label for="search-form-1">
<span class="screen-reader-text">{$label}</span>
</label>
<input type="search" id="search-form-1" class="search-field" placeholder="{$placeholder}…" value="{$value}" name="s">
<button type="submit" class="search-submit">
<svg class="icon icon-search" aria-hidden="true" role="img"> <use href="#icon-search" xlink:href="#icon-search"></use> </svg>
<span class="screen-reader-text">{$button}</span>
</button>
</form>`
2023-03-02 12:36:58 +00:00
func errorsHandle(h *wp.Handle) {
switch h.Stats {
2023-03-17 10:26:08 +00:00
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
2023-04-07 14:59:07 +00:00
logs.IfError(h.Err(), "报错:")
2023-03-17 10:26:08 +00:00
h.SetTempl("twentyseventeen/posts/error.gohtml")
2023-03-02 12:36:58 +00:00
}
}
2023-05-05 12:45:20 +00:00
func postThumb(h *wp.Handle) {
d := h.GetDetailHandle()
2023-02-24 11:34:19 +00:00
if d.Post.Thumbnail.Path != "" {
2024-01-21 13:29:36 +00:00
d.Post.Thumbnail = getPostThumbs(d.Post.Id, d.Post)
2023-02-24 11:34:19 +00:00
}
2023-01-17 15:18:31 +00:00
}
2024-01-21 13:29:36 +00:00
var getPostThumbs = reload.BuildMapFn[uint64]("post-thumbnail", postThumbs)
func postThumbs(post models.Posts) models.PostThumbnail {
img := wpconfig.Thumbnail(post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail")
img.Sizes = "100vw"
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
return img
}
2023-02-09 12:49:33 +00:00
var commentFormat = comment{}
type comment struct {
plugins.CommonCommentFormat
}
var commentLi = plugins.CommonLi()
var respondFn = plugins.Responds(respondStr)
func (c comment) FormatLi(_ context.Context, m models.Comments, depth, maxDepth, page int, isTls, isThreadComments bool, eo, parent string) string {
return plugins.FormatLi(commentLi, m, respondFn, depth, maxDepth, page, isTls, isThreadComments, eo, parent)
}
2024-01-14 14:07:16 +00:00
var colophon = `<footer id="colophon" class="site-footer">
<div class="wrap">
<div class="site-info">
<a href="https://github.com/fthvgb1/wp-go" class="imprint">自豪地采用 wp-go</a>
</div>
</div>
</footer>`
var respondStr = `<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>`
func postThumbnail(h *wp.Handle, posts *models.Posts) {
if posts.Thumbnail.Path != "" {
posts.Thumbnail.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
2023-02-28 15:38:23 +00:00
if h.Scene() == constraints.Detail {
posts.Thumbnail.Sizes = "100vw"
2023-01-25 18:26:36 +00:00
}
2023-02-09 08:08:18 +00:00
}
2023-01-25 18:26:36 +00:00
}
var header = reload.Vars(models.PostThumbnail{})
2023-06-12 15:08:46 +00:00
func calCustomHeader(h *wp.Handle) {
2023-04-21 17:12:39 +00:00
h.SetData("HeaderImage", getHeaderImage(h))
}
2023-03-01 05:17:12 +00:00
func getHeaderImage(h *wp.Handle) (r models.PostThumbnail) {
img := header.Load()
2023-02-24 16:56:52 +00:00
if img.Path != "" {
2023-03-01 15:34:05 +00:00
return img
2023-02-24 16:56:52 +00:00
}
2023-06-12 15:08:46 +00:00
image, rand := h.GetCustomHeaderImg()
2023-02-24 16:56:52 +00:00
if image.Path != "" {
r = image
r.Sizes = "100vw"
if !rand {
header.Store(r)
}
2023-02-24 16:56:52 +00:00
return
2023-01-17 15:18:31 +00:00
}
2023-02-28 15:38:23 +00:00
r.Path = helper.CutUrlHost(h.CommonThemeMods().ThemeSupport.CustomHeader.DefaultImage)
2023-02-24 16:56:52 +00:00
r.Width = 2000
r.Height = 1200
header.Store(r)
2023-01-17 15:18:31 +00:00
return
}
2023-01-25 18:26:36 +00:00
2023-05-03 15:57:49 +00:00
func calClass(h *wp.Handle, s string, _ ...any) string {
2023-04-15 05:22:21 +00:00
class := strings.Split(s, " ")
2023-02-28 15:38:23 +00:00
themeMods := h.CommonThemeMods()
u := wpconfig.GetThemeModsVal(ThemeName, "header_image", themeMods.ThemeSupport.CustomHeader.DefaultImage)
2023-02-23 10:21:51 +00:00
if u != "" && u != "remove-header" {
2023-02-28 15:38:23 +00:00
class = append(class, "has-header-image")
2023-02-09 12:49:33 +00:00
}
2023-02-28 15:38:23 +00:00
if len(themeMods.SidebarsWidgets.Data.Sidebar1) > 0 {
class = append(class, "has-sidebar")
2023-02-24 16:56:52 +00:00
}
2023-02-28 15:38:23 +00:00
if themeMods.HeaderTextcolor == "blank" {
class = append(class, "title-tagline-hidden")
2023-02-24 16:56:52 +00:00
}
2023-02-28 15:38:23 +00:00
class = append(class, "hfeed")
class = append(class, str.Join("colors-", wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light")))
if h.Scene() == constraints.Archive {
if "one-column" == wpconfig.GetThemeModsVal(ThemeName, "page_layout", "") {
class = append(class, "page-one-column")
2023-02-23 10:21:51 +00:00
} else {
2023-02-28 15:38:23 +00:00
class = append(class, "page-two-column")
}
2023-01-25 18:26:36 +00:00
}
2023-04-15 05:22:21 +00:00
return strings.Join(class, " ")
2023-01-25 18:26:36 +00:00
}
2023-06-12 15:08:46 +00:00
func videoHeader(h *wp.Handle) {
2023-08-26 14:01:20 +00:00
h.AddActionFilter("videoSetting", videoPlay)
2023-06-13 04:02:46 +00:00
wp.CustomVideo(h, constraints.Home)
2023-06-12 15:08:46 +00:00
}
func videoPlay(h *wp.Handle, _ string, a ...any) string {
if len(a) < 1 {
return ""
}
v, ok := a[0].(*wp.VideoSetting)
if !ok {
return ""
}
img := getHeaderImage(h)
v.Width = img.Width
v.Height = img.Height
v.PosterUrl = img.Path
v.L10n.Play = `<span class="screen-reader-text">播放背景视频</span><svg class="icon icon-play" aria-hidden="true" role="img"> <use href="#icon-play" xlink:href="#icon-play"></use> </svg>`
v.L10n.Pause = `<span class="screen-reader-text">暂停背景视频</span><svg class="icon icon-pause" aria-hidden="true" role="img"> <use href="#icon-pause" xlink:href="#icon-pause"></use> </svg>`
return ""
}