wp-go/app/theme/wp/customheader.go

233 lines
6.7 KiB
Go
Raw Normal View History

2023-03-01 05:17:12 +00:00
package wp
import (
2023-06-12 15:08:46 +00:00
"encoding/json"
2023-03-29 15:31:16 +00:00
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig"
"github.com/fthvgb1/wp-go/helper/slice"
2023-03-29 15:31:16 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/model"
2023-06-12 15:08:46 +00:00
"regexp"
)
func (h *Handle) DisplayHeaderText() bool {
2023-02-28 15:38:23 +00:00
return h.themeMods.ThemeSupport.CustomHeader.HeaderText && "blank" != h.themeMods.HeaderTextcolor
}
2023-06-12 15:08:46 +00:00
func (h *Handle) GetCustomHeaderImg() (r models.PostThumbnail, isRand bool) {
2023-03-29 15:31:16 +00:00
var err error
img := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail {
2023-03-29 15:31:16 +00:00
hs, er := h.GetHeaderImages(h.theme)
if er != nil {
err = er
return nil
}
return hs
})
if err != nil {
2023-04-07 14:59:07 +00:00
logs.Error(err, "获取页眉背景图失败")
return
}
hs := slice.Copy(img)
2023-03-29 15:31:16 +00:00
if len(hs) < 1 {
return
}
if len(hs) > 1 {
isRand = true
}
r, _ = slice.RandPop(&hs)
return
}
2023-03-29 15:31:16 +00:00
2023-06-12 15:08:46 +00:00
type VideoPlay struct {
Pause string `json:"pause,omitempty"`
Play string `json:"play,omitempty"`
PauseSpeak string `json:"pauseSpeak,omitempty"`
PlaySpeak string `json:"playSpeak,omitempty"`
}
type VideoSetting struct {
MimeType string `json:"mimeType,omitempty"`
PosterUrl string `json:"posterUrl,omitempty"`
VideoUrl string `json:"videoUrl,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
MinWidth int `json:"minWidth,omitempty"`
MinHeight int `json:"minHeight,omitempty"`
L10n VideoPlay `json:"l10n"`
}
var videoReg = regexp.MustCompile(`^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)`)
func GetVideoSetting(h *Handle, u string) (string, error) {
img, _ := h.GetCustomHeaderImg()
v := VideoSetting{
MimeType: GetMimeType(u),
PosterUrl: img.Path,
VideoUrl: u,
Width: img.Width,
Height: img.Height,
MinWidth: 900,
MinHeight: 500,
L10n: VideoPlay{
Pause: "暂停",
Play: "播放",
PauseSpeak: "视频已暂停",
PlaySpeak: "视频正在播放",
},
}
if is := videoReg.FindString(u); is != "" {
v.MimeType = "video/x-youtube"
}
_ = h.ComponentFilterFnHook("videoSetting", "", &v)
s, err := json.Marshal(v)
if err != nil {
return "", err
}
setting := fmt.Sprintf(`var %s = %s`, "_wpCustomHeaderSettings", string(s))
script := str.Join(`<script id="wp-custom-header-js-extra">`, setting, "</script>\n")
return script, nil
}
2023-06-13 04:02:46 +00:00
func CustomVideo(h *Handle, scene ...string) (ok bool) {
2023-06-12 15:08:46 +00:00
mod, err := wpconfig.GetThemeMods(h.theme)
if err != nil {
logs.Error(err, "getThemeMods fail", h.theme)
return
}
if !mod.ThemeSupport.CustomHeader.Video || (mod.HeaderVideo < 1 && mod.ExternalHeaderVideo == "") {
return
}
u := ""
if mod.HeaderVideo > 0 {
post, err := cache.GetPostById(h.C, uint64(mod.HeaderVideo))
if err != nil {
logs.Error(err, "get headerVideo fail", mod.HeaderVideo)
return
}
u = post.Metas["_wp_attached_file"].(string)
u = str.Join("/wp-content/uploads/", u)
} else {
u = mod.ExternalHeaderVideo
}
hs, err := GetVideoSetting(h, u)
if err != nil {
logs.Error(err, "get headerVideo fail", mod.HeaderVideo)
return
}
scripts := []string{
"/wp-includes/js/dist/vendor/wp-polyfill-inert.min.js",
"/wp-includes/js/dist/vendor/regenerator-runtime.min.js",
"/wp-includes/js/dist/vendor/wp-polyfill.min.js",
"/wp-includes/js/dist/dom-ready.min.js",
"/wp-includes/js/dist/hooks.min.js",
"/wp-includes/js/dist/i18n.min.js",
"/wp-includes/js/dist/a11y.min.js",
2023-06-12 16:17:15 +00:00
"/wp-includes/js/wp-custom-header.min.js",
2023-06-12 15:08:46 +00:00
}
scripts = slice.Map(scripts, func(t string) string {
return fmt.Sprintf(`<script src="%s" id="wp-%s-js"></script>
`, t, str.Replaces(t, [][]string{
{"/wp-includes/js/dist/vendor/"},
{"/wp-includes/js/dist/"},
{"/wp-includes/js/"},
{".min.js"},
{".js"},
{"wp-", ""},
}))
})
2023-06-13 04:02:46 +00:00
2023-06-12 15:08:46 +00:00
var tr = `<script id="wp-i18n-js-after">
wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } );
</script>
<script id='wp-a11y-js-translations'>
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "default", {"translation-revision-date":"2023-04-23 22:48:55+0000","generator":"GlotPress/4.0.0-alpha.4","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","plural-forms":"nplurals=1; plural=0;","lang":"zh_CN"},"Notifications":["u901au77e5"]}},"comment":{"reference":"wp-includes/js/dist/a11y.js"}} );
</script>
<script src='/wp-includes/js/dist/a11y.min.js?ver=ecce20f002eda4c19664' id='wp-a11y-js'></script>
`
2023-06-13 04:02:46 +00:00
c := []Components[string]{
2023-06-15 10:00:57 +00:00
NewComponent("wp-a11y-js-translations", tr, true, 10.0065, nil),
NewComponent("VideoSetting", hs, true, 10.0064, nil),
NewComponent("header-script", scripts[len(scripts)-1], true, 10.0063, nil),
2023-06-13 04:02:46 +00:00
}
for _, s := range scene {
2023-06-15 10:00:57 +00:00
h.PushGroupFooterScript(s, "wp-custom-header", 10.0066, scripts[0:len(scripts)-2]...)
2023-06-13 04:02:46 +00:00
h.PushFooterScript(s, c...)
}
2023-06-12 15:08:46 +00:00
ok = true
return
}
2023-03-29 15:31:16 +00:00
func (h *Handle) GetHeaderImages(theme string) (r []models.PostThumbnail, err error) {
meta, err := wpconfig.GetThemeMods(theme)
if err != nil || meta.HeaderImage == "" {
return
}
if "random-uploaded-image" != meta.HeaderImage {
m, er := cache.GetPostById(h.C, uint64(meta.HeaderImagData.AttachmentId))
if er != nil {
err = er
return
}
m.Thumbnail = thumb(m, theme)
r = []models.PostThumbnail{m.Thumbnail}
return
}
headers, er := model.Finds[models.Posts](h.C, model.Conditions(
model.Where(model.SqlBuilder{
{"post_type", "attachment"},
{"post_status", "inherit"},
{"meta_value", theme},
{"meta_key", "_wp_attachment_is_custom_header"},
}),
model.Fields("a.ID"),
model.Group("a.ID"),
model.Join(model.SqlBuilder{
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
}),
))
if er != nil {
err = er
return
}
if len(headers) > 0 {
posts, er := cache.GetPostsByIds(h.C, slice.Map(headers, func(t models.Posts) uint64 {
return t.Id
}))
if er != nil {
err = er
return
}
r = slice.Map(posts, func(m models.Posts) models.PostThumbnail {
return thumb(m, theme)
})
}
return
}
func thumb(m models.Posts, theme string) models.PostThumbnail {
2023-04-19 07:27:58 +00:00
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "full", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
2023-03-29 15:31:16 +00:00
m.Thumbnail.Width = m.AttachmentMetadata.Width
m.Thumbnail.Height = m.AttachmentMetadata.Height
if m.Thumbnail.Path != "" {
if len(m.AttachmentMetadata.Sizes) > 0 {
m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset)
}
}
return m.Thumbnail
}