wp-go/internal/theme/twentyseventeen/twentyseventeen.go

170 lines
5.4 KiB
Go
Raw Normal View History

2023-01-17 15:18:31 +00:00
package twentyseventeen
import (
2023-01-25 18:26:36 +00:00
"fmt"
2023-01-17 15:18:31 +00:00
"github.com/elliotchance/phpserialize"
2023-01-21 11:31:23 +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"
"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/internal/wpconfig"
"github.com/fthvgb1/wp-go/plugin/pagination"
2023-01-17 15:18:31 +00:00
"github.com/gin-gonic/gin"
"strings"
2023-01-17 15:18:31 +00:00
)
const ThemeName = "twentyseventeen"
type HeaderImageMeta struct {
CustomCssPostId int `json:"custom_css_post_id,omitempty"`
NavMenuLocations []string `json:"nav_menu_locations,omitempty"`
HeaderImage string `json:"header_image,omitempty"`
HeaderImagData ImageData `json:"header_image_data,omitempty"`
}
type ImageData struct {
AttachmentId int64 `json:"attachment_id,omitempty"`
Url string `json:"url,omitempty"`
ThumbnailUrl string `json:"thumbnail_url,omitempty"`
Height int64 `json:"height,omitempty"`
Width int64 `json:"width,omitempty"`
}
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
}()
func Hook(status int, c *gin.Context, h gin.H, scene, stats int) {
templ := "twentyseventeen/posts/index.gohtml"
2023-01-17 15:18:31 +00:00
if _, ok := plugins.IndexSceneMap[scene]; ok {
h["HeaderImage"] = getHeaderImage(c)
2023-01-25 18:26:36 +00:00
posts := h["posts"].([]models.Posts)
p, ok := h["pagination"]
if ok {
pp, ok := p.(pagination.ParsePagination)
if ok {
2023-01-18 15:11:26 +00:00
h["pagination"] = pagination.Paginate(paginate, pp)
}
}
2023-01-25 18:26:36 +00:00
d := 0
s := ""
if scene == plugins.Search {
if len(posts) > 0 {
d = 1
} else {
d = 0
}
} else if scene == plugins.Category {
cate := slice.Filter(cache.Categories(c), func(my models.TermsMy) bool {
return my.Name == c.Param("category")
})[0]
d = int(cate.Terms.TermId)
if cate.Slug[0] != '%' {
s = cate.Slug
}
}
h["bodyClass"] = bodyClass(scene, d, s)
h["posts"] = postThumbnail(posts, scene)
} else if scene == plugins.Detail {
2023-01-23 15:42:37 +00:00
h["HeaderImage"] = getHeaderImage(c)
2023-01-25 18:26:36 +00:00
post := h["post"].(models.Posts)
h["bodyClass"] = bodyClass(scene, int(post.Id))
host, _ := wpconfig.Options.Load("siteurl")
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["post"] = post
templ = "twentyseventeen/posts/detail.gohtml"
2023-01-17 15:18:31 +00:00
}
c.HTML(status, templ, h)
2023-01-17 15:18:31 +00:00
return
}
2023-01-25 18:26:36 +00:00
func postThumbnail(posts []models.Posts, scene int) []models.Posts {
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-01-17 15:18:31 +00:00
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
meta, err := getHeaderMarkup()
if err != nil {
logs.ErrPrintln(err, "解析主题背景图设置错误")
return
}
if meta.HeaderImagData.AttachmentId > 0 {
m, err := cache.GetPostById(c, uint64(meta.HeaderImagData.AttachmentId))
if err != nil {
logs.ErrPrintln(err, "获取主题背景图信息错误")
return
}
2023-01-25 18:26:36 +00:00
host, _ := wpconfig.Options.Load("siteurl")
m.Thumbnail = plugins.Thumbnail(m.AttachmentMetadata, "thumbnail", host, "thumbnail", "post-thumbnail", "twentyseventeen-thumbnail-avatar")
2023-01-17 15:18:31 +00:00
if m.Thumbnail.Path != "" {
r = m.Thumbnail
2023-01-25 18:26:36 +00:00
if len(m.AttachmentMetadata.Sizes) > 0 {
r.Srcset = str.Join(r.Path, " 2000vw, ", r.Srcset)
}
2023-01-17 15:18:31 +00:00
}
}
2023-01-25 18:26:36 +00:00
r.Width = 2000
r.Height = 1200
r.Sizes = "100vw"
2023-01-17 15:18:31 +00:00
return
}
func getHeaderMarkup() (r HeaderImageMeta, err error) {
mods, ok := wpconfig.Options.Load("theme_mods_twentyseventeen")
var rr map[any]any
if ok {
err = phpserialize.Unmarshal([]byte(mods), &rr)
if err == nil {
2023-01-21 11:31:23 +00:00
rx := maps.AnyAnyToStrAny(rr)
r, err = maps.StrAnyMapToStruct[HeaderImageMeta](rx)
2023-01-17 15:18:31 +00:00
}
}
return
}
2023-01-25 18:26:36 +00:00
func bodyClass(scene, d int, a ...any) string {
s := ""
if scene == plugins.Search {
if d > 0 {
s = "search-results"
} else {
s = "search-no-results"
}
} else if scene == plugins.Category {
s = fmt.Sprintf("category-%d %v", d, a[0])
} else if scene == plugins.Detail {
s = fmt.Sprintf("postid-%d", d)
}
return map[int]string{
plugins.Home: "home blog ",
plugins.Archive: "archive date page-two-column",
plugins.Category: str.Join("archive category page-two-column ", s),
plugins.Search: str.Join("search ", s),
plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s),
}[scene]
}