diff --git a/internal/theme/common/bodyclass.go b/internal/theme/common/bodyclass.go index acadccd..6678f4d 100644 --- a/internal/theme/common/bodyclass.go +++ b/internal/theme/common/bodyclass.go @@ -29,7 +29,6 @@ func (h *Handle) bodyClass(class ...string) string { if constraints.Ok != h.Stats { return "error404" } - mods, err := wpconfig.GetThemeMods(h.Theme) switch h.Scene { case constraints.Search: s = "search-no-results" @@ -50,22 +49,20 @@ func (h *Handle) bodyClass(class ...string) string { s = fmt.Sprintf("category-%v category-%v", s, cate.Terms.TermId) case constraints.Detail: s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id) - if len(mods.ThemeSupport.PostFormats) > 0 { + if len(h.ThemeMods.ThemeSupport.PostFormats) > 0 { s = str.Join(s, " single-format-standard") } } class = append(class, s) - if err == nil { - if wpconfig.IsCustomBackground(h.Theme) { - class = append(class, "custom-background") - } - if mods.CustomLogo > 0 { - class = append(class, "wp-custom-logo") - } - if mods.ThemeSupport.ResponsiveEmbeds { - class = append(class, "wp-embed-responsive") - } + if wpconfig.IsCustomBackground(h.Theme) { + class = append(class, "custom-background") + } + if h.ThemeMods.CustomLogo > 0 { + class = append(class, "wp-custom-logo") + } + if h.ThemeMods.ThemeSupport.ResponsiveEmbeds { + class = append(class, "wp-embed-responsive") } return str.Join(commonClass[h.Scene], strings.Join(class, " ")) diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index c5eaf0d..1fb2e50 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -11,33 +11,38 @@ import ( "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/gin-contrib/sessions" "github.com/gin-gonic/gin" "net/http" ) type Handle struct { - C *gin.Context - Theme string - Session sessions.Session - GinH gin.H - Password string - Scene int - Code int - Stats int - Templ string - Class []string + C *gin.Context + Theme string + Session sessions.Session + GinH gin.H + Password string + Scene int + Code int + Stats int + Templ string + Class []string + ThemeMods wpconfig.ThemeMods } func NewHandle(c *gin.Context, scene int, theme string) *Handle { + mods, err := wpconfig.GetThemeMods(theme) + logs.ErrPrintln(err, "获取mods失败") return &Handle{ - C: c, - Theme: theme, - Session: sessions.Default(c), - GinH: gin.H{}, - Scene: scene, - Code: http.StatusOK, - Stats: constraints.Ok, + C: c, + Theme: theme, + Session: sessions.Default(c), + GinH: gin.H{}, + Scene: scene, + Code: http.StatusOK, + Stats: constraints.Ok, + ThemeMods: mods, } } diff --git a/internal/theme/common/customcss.go b/internal/theme/common/customcss.go index f2aea02..b886609 100644 --- a/internal/theme/common/customcss.go +++ b/internal/theme/common/customcss.go @@ -6,17 +6,15 @@ import ( "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/constraints" - "github.com/fthvgb1/wp-go/internal/wpconfig" ) var css = reload.Vars(constraints.Defaults) func (h *Handle) CalCustomCss() (r string) { - mods, err := wpconfig.GetThemeMods(h.Theme) - if err != nil || mods.CustomCssPostId < 1 { + if h.ThemeMods.CustomCssPostId < 1 { return } - post, err := cache.GetPostById(h.C, uint64(mods.CustomCssPostId)) + post, err := cache.GetPostById(h.C, uint64(h.ThemeMods.CustomCssPostId)) if err != nil || post.Id < 1 { return } diff --git a/internal/theme/common/customheader.go b/internal/theme/common/customheader.go index 62ea0a4..e5f7e80 100644 --- a/internal/theme/common/customheader.go +++ b/internal/theme/common/customheader.go @@ -5,15 +5,10 @@ import ( "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/wpconfig" ) func (h *Handle) DisplayHeaderText() bool { - mods, err := wpconfig.GetThemeMods(h.Theme) - if err != nil { - return false - } - return mods.ThemeSupport.CustomHeader.HeaderText && "blank" != mods.HeaderTextcolor + return h.ThemeMods.ThemeSupport.CustomHeader.HeaderText && "blank" != h.ThemeMods.HeaderTextcolor } func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) { diff --git a/internal/theme/common/customlogo.go b/internal/theme/common/customlogo.go index cd5f754..c1a2282 100644 --- a/internal/theme/common/customlogo.go +++ b/internal/theme/common/customlogo.go @@ -13,11 +13,7 @@ import ( var logo = reload.Vars(constraints.Defaults) func (h *Handle) CalCustomLogo() (r string) { - mods, err := wpconfig.GetThemeMods(h.Theme) - if err != nil { - return - } - id := uint64(mods.CustomLogo) + id := uint64(h.ThemeMods.CustomLogo) if id < 1 { id = str.ToInteger[uint64](wpconfig.Options.Value("site_logo"), 0) if id < 1 { diff --git a/internal/theme/common/template.gohtml b/internal/theme/common/template.gohtml index 41439c2..d5c621e 100644 --- a/internal/theme/common/template.gohtml +++ b/internal/theme/common/template.gohtml @@ -9,6 +9,9 @@ {{if .customCss}} {{.customCss|unescaped}} {{end}} + {{if .externHead}} + {{.externHead|unescaped}} + {{end}} {{end}} {{define "common/customLogo"}} diff --git a/internal/theme/theme.go b/internal/theme/theme.go index 7e82af4..44344ac 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -9,6 +9,7 @@ import ( func InitTheme() { addThemeHookFunc(twentyfifteen.ThemeName, twentyfifteen.Hook) + twentyfifteen.Init(TemplateFs) addThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook) } diff --git a/internal/theme/twentyfifteen/colorschemecss.go b/internal/theme/twentyfifteen/colorschemecss.go new file mode 100644 index 0000000..be1f9aa --- /dev/null +++ b/internal/theme/twentyfifteen/colorschemecss.go @@ -0,0 +1,617 @@ +package twentyfifteen + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/internal/cmd/reload" + "github.com/fthvgb1/wp-go/internal/pkg/constraints" + "strconv" + "strings" +) + +var colorCss = reload.Vars("default") + +func (h *handle) colorSchemeCss() { + color := colorCss.Load() + if color == constraints.Defaults { + s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool { + return s != "" + }) + color = fmt.Sprintf(`\n`, "twentyfifteen-style", "", strings.Join(s, "\n")) + colorCss.Store(color) + } + h.IndexHandle.GinH["colorScheme"] = color +} +func (h *handle) calColorSchemeCss() (r string) { + color := h.getColorScheme() + if "default" == h.IndexHandle.ThemeMods.ColorScheme || len(color) < 1 { + return + } + textColorRgb := slice.ToAnySlice(Hex2RgbUint8(color[3])) + sidebarTextColorRgb := Hex2RgbUint8(color[4]) + sidebarTextColorRgbs := slice.ToAnySlice(sidebarTextColorRgb) + colors := map[string]string{ + "background_color": color[0], + "header_background_color": color[1], + "box_background_color": color[2], + "textcolor": color[3], + "secondary_textcolor": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", textColorRgb...), + "border_color": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", textColorRgb...), + "border_focus_color": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", textColorRgb...), + "sidebar_textcolor": color[4], + "sidebar_border_color": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", sidebarTextColorRgbs...), + "sidebar_border_focus_color": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", sidebarTextColorRgbs...), + "secondary_sidebar_textcolor": fmt.Sprintf("rgba(%d, %d, %d, 0.7)", sidebarTextColorRgbs...), + "meta_box_background_color": color[5], + } + r = cssTemplate + for k, v := range colors { + r = strings.ReplaceAll(r, fmt.Sprintf(`{$colors['%s']}`, k), v) + } + return +} + +func (h *handle) calSidebarTextColorCss() (r string) { + colors := h.getColorScheme() + if h.IndexHandle.ThemeMods.SidebarTextcolor == colors[4] { + return + } + linkColorRgb := Hex2RgbUint8(h.IndexHandle.ThemeMods.SidebarTextcolor) + color := slice.ToAnySlice(linkColorRgb) + textColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.7)`, color...) + borderColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.1)`, color...) + borderFocusColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.3)`, color...) + r = fmt.Sprintf(sidebarTextColorTemplate, h.IndexHandle.ThemeMods.SidebarTextcolor, textColor, borderColor, borderFocusColor) + return +} + +func (h *handle) calHeaderBackgroundColorCss() (r string) { + colors := h.getColorScheme() + if h.IndexHandle.ThemeMods.HeaderBackgroundColor == colors[1] { + return + } + r = fmt.Sprintf(headerBackgroundColorCssTemplate, h.IndexHandle.ThemeMods.HeaderBackgroundColor, h.IndexHandle.ThemeMods.HeaderBackgroundColor) + return +} + +func (h *handle) getColorScheme() (r []string) { + x, ok := colorscheme[h.IndexHandle.ThemeMods.ColorScheme] + if ok { + r = x.Colors + } + return +} + +type ColorScheme struct { + Label string `json:"label,omitempty"` + Colors []string `json:"colors,omitempty"` +} + +func Hex2RgbUint8(color string) []uint8 { + var r []uint8 + color = strings.TrimLeft(color, "#") + fn := func(s string) uint8 { + n, _ := strconv.ParseInt(s, 16, 0) + return uint8(n) + } + switch len(color) { + case 3: + r = []uint8{color[0], color[1], color[2]} + case 6: + r = []uint8{fn(color[:2]), fn(color[2:4]), fn(color[4:])} + } + return r +} + +var cssTemplate = ` +/* Color Scheme */ + + /* Background Color */ + body { + background-color: {$colors['background_color']}; + } + + /* Sidebar Background Color */ + body:before, + .site-header { + background-color: {$colors['header_background_color']}; + } + + /* Box Background Color */ + .post-navigation, + .pagination, + .secondary, + .site-footer, + .hentry, + .page-header, + .page-content, + .comments-area, + .widecolumn { + background-color: {$colors['box_background_color']}; + } + + /* Box Background Color */ + button, + input[type="button"], + input[type="reset"], + input[type="submit"], + .pagination .prev, + .pagination .next, + .widget_calendar tbody a, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus, + .page-links a, + .page-links a:hover, + .page-links a:focus, + .sticky-post { + color: {$colors['box_background_color']}; + } + + /* Main Text Color */ + button, + input[type="button"], + input[type="reset"], + input[type="submit"], + .pagination .prev, + .pagination .next, + .widget_calendar tbody a, + .page-links a, + .sticky-post { + background-color: {$colors['textcolor']}; + } + + /* Main Text Color */ + body, + blockquote cite, + blockquote small, + a, + .dropdown-toggle:after, + .image-navigation a:hover, + .image-navigation a:focus, + .comment-navigation a:hover, + .comment-navigation a:focus, + .widget-title, + .entry-footer a:hover, + .entry-footer a:focus, + .comment-metadata a:hover, + .comment-metadata a:focus, + .pingback .edit-link a:hover, + .pingback .edit-link a:focus, + .comment-list .reply a:hover, + .comment-list .reply a:focus, + .site-info a:hover, + .site-info a:focus { + color: {$colors['textcolor']}; + } + + /* Main Text Color */ + .entry-content a, + .entry-summary a, + .page-content a, + .comment-content a, + .pingback .comment-body > a, + .author-description a, + .taxonomy-description a, + .textwidget a, + .entry-footer a:hover, + .comment-metadata a:hover, + .pingback .edit-link a:hover, + .comment-list .reply a:hover, + .site-info a:hover { + border-color: {$colors['textcolor']}; + } + + /* Secondary Text Color */ + button:hover, + button:focus, + input[type="button"]:hover, + input[type="button"]:focus, + input[type="reset"]:hover, + input[type="reset"]:focus, + input[type="submit"]:hover, + input[type="submit"]:focus, + .pagination .prev:hover, + .pagination .prev:focus, + .pagination .next:hover, + .pagination .next:focus, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus, + .page-links a:hover, + .page-links a:focus { + background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + background-color: {$colors['secondary_textcolor']}; + } + + /* Secondary Text Color */ + blockquote, + a:hover, + a:focus, + .main-navigation .menu-item-description, + .post-navigation .meta-nav, + .post-navigation a:hover .post-title, + .post-navigation a:focus .post-title, + .image-navigation, + .image-navigation a, + .comment-navigation, + .comment-navigation a, + .widget, + .author-heading, + .entry-footer, + .entry-footer a, + .taxonomy-description, + .page-links > .page-links-title, + .entry-caption, + .comment-author, + .comment-metadata, + .comment-metadata a, + .pingback .edit-link, + .pingback .edit-link a, + .post-password-form label, + .comment-form label, + .comment-notes, + .comment-awaiting-moderation, + .logged-in-as, + .form-allowed-tags, + .no-comments, + .site-info, + .site-info a, + .wp-caption-text, + .gallery-caption, + .comment-list .reply a, + .widecolumn label, + .widecolumn .mu_register label { + color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + color: {$colors['secondary_textcolor']}; + } + + /* Secondary Text Color */ + blockquote, + .logged-in-as a:hover, + .comment-author a:hover { + border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + border-color: {$colors['secondary_textcolor']}; + } + + /* Border Color */ + hr, + .dropdown-toggle:hover, + .dropdown-toggle:focus { + background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + background-color: {$colors['border_color']}; + } + + /* Border Color */ + pre, + abbr[title], + table, + th, + td, + input, + textarea, + .main-navigation ul, + .main-navigation li, + .post-navigation, + .post-navigation div + div, + .pagination, + .comment-navigation, + .widget li, + .widget_categories .children, + .widget_nav_menu .sub-menu, + .widget_pages .children, + .site-header, + .site-footer, + .hentry + .hentry, + .author-info, + .entry-content .page-links a, + .page-links > span, + .page-header, + .comments-area, + .comment-list + .comment-respond, + .comment-list article, + .comment-list .pingback, + .comment-list .trackback, + .comment-list .reply a, + .no-comments { + border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + border-color: {$colors['border_color']}; + } + + /* Border Focus Color */ + a:focus, + button:focus, + input:focus { + outline-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + outline-color: {$colors['border_focus_color']}; + } + + input:focus, + textarea:focus { + border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */ + border-color: {$colors['border_focus_color']}; + } + + /* Sidebar Link Color */ + .secondary-toggle:before { + color: {$colors['sidebar_textcolor']}; + } + + .site-title a, + .site-description { + color: {$colors['sidebar_textcolor']}; + } + + /* Sidebar Text Color */ + .site-title a:hover, + .site-title a:focus { + color: {$colors['secondary_sidebar_textcolor']}; + } + + /* Sidebar Border Color */ + .secondary-toggle { + border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */ + border-color: {$colors['sidebar_border_color']}; + } + + /* Sidebar Border Focus Color */ + .secondary-toggle:hover, + .secondary-toggle:focus { + border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */ + border-color: {$colors['sidebar_border_focus_color']}; + } + + .site-title a { + outline-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */ + outline-color: {$colors['sidebar_border_focus_color']}; + } + + /* Meta Background Color */ + .entry-footer { + background-color: {$colors['meta_box_background_color']}; + } + + @media screen and (min-width: 38.75em) { + /* Main Text Color */ + .page-header { + border-color: {$colors['textcolor']}; + } + } + + @media screen and (min-width: 59.6875em) { + /* Make sure its transparent on desktop */ + .site-header, + .secondary { + background-color: transparent; + } + + /* Sidebar Background Color */ + .widget button, + .widget input[type="button"], + .widget input[type="reset"], + .widget input[type="submit"], + .widget_calendar tbody a, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus { + color: {$colors['header_background_color']}; + } + + /* Sidebar Link Color */ + .secondary a, + .dropdown-toggle:after, + .widget-title, + .widget blockquote cite, + .widget blockquote small { + color: {$colors['sidebar_textcolor']}; + } + + .widget button, + .widget input[type="button"], + .widget input[type="reset"], + .widget input[type="submit"], + .widget_calendar tbody a { + background-color: {$colors['sidebar_textcolor']}; + } + + .textwidget a { + border-color: {$colors['sidebar_textcolor']}; + } + + /* Sidebar Text Color */ + .secondary a:hover, + .secondary a:focus, + .main-navigation .menu-item-description, + .widget, + .widget blockquote, + .widget .wp-caption-text, + .widget .gallery-caption { + color: {$colors['secondary_sidebar_textcolor']}; + } + + .widget button:hover, + .widget button:focus, + .widget input[type="button"]:hover, + .widget input[type="button"]:focus, + .widget input[type="reset"]:hover, + .widget input[type="reset"]:focus, + .widget input[type="submit"]:hover, + .widget input[type="submit"]:focus, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus { + background-color: {$colors['secondary_sidebar_textcolor']}; + } + + .widget blockquote { + border-color: {$colors['secondary_sidebar_textcolor']}; + } + + /* Sidebar Border Color */ + .main-navigation ul, + .main-navigation li, + .widget input, + .widget textarea, + .widget table, + .widget th, + .widget td, + .widget pre, + .widget li, + .widget_categories .children, + .widget_nav_menu .sub-menu, + .widget_pages .children, + .widget abbr[title] { + border-color: {$colors['sidebar_border_color']}; + } + + .dropdown-toggle:hover, + .dropdown-toggle:focus, + .widget hr { + background-color: {$colors['sidebar_border_color']}; + } + + .widget input:focus, + .widget textarea:focus { + border-color: {$colors['sidebar_border_focus_color']}; + } + + .sidebar a:focus, + .dropdown-toggle:focus { + outline-color: {$colors['sidebar_border_focus_color']}; + } + } +` + +var headerBackgroundColorCssTemplate = ` +/* Custom Header Background Color */ + body:before, + .site-header { + background-color: %s; + } + + @media screen and (min-width: 59.6875em) { + .site-header, + .secondary { + background-color: transparent; + } + + .widget button, + .widget input[type="button"], + .widget input[type="reset"], + .widget input[type="submit"], + .widget_calendar tbody a, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus { + color: %s; + } + } +` + +var sidebarTextColorTemplate = ` +/* Custom Sidebar Text Color */ + .site-title a, + .site-description, + .secondary-toggle:before { + color: %[1]v; + } + + .site-title a:hover, + .site-title a:focus { + color: %[1]v; /* Fallback for IE7 and IE8 */ + color: %[2]v; + } + + .secondary-toggle { + border-color: %[1]v; /* Fallback for IE7 and IE8 */ + border-color: %[3]v; + } + + .secondary-toggle:hover, + .secondary-toggle:focus { + border-color: %[1]v; /* Fallback for IE7 and IE8 */ + border-color: %[4]v; + } + + .site-title a { + outline-color: %[1]v; /* Fallback for IE7 and IE8 */ + outline-color: %[4]v; + } + + @media screen and (min-width: 59.6875em) { + .secondary a, + .dropdown-toggle:after, + .widget-title, + .widget blockquote cite, + .widget blockquote small { + color: %[1]v; + } + + .widget button, + .widget input[type="button"], + .widget input[type="reset"], + .widget input[type="submit"], + .widget_calendar tbody a { + background-color: %[1]v; + } + + .textwidget a { + border-color: %[1]v; + } + + .secondary a:hover, + .secondary a:focus, + .main-navigation .menu-item-description, + .widget, + .widget blockquote, + .widget .wp-caption-text, + .widget .gallery-caption { + color: %[2]v; + } + + .widget button:hover, + .widget button:focus, + .widget input[type="button"]:hover, + .widget input[type="button"]:focus, + .widget input[type="reset"]:hover, + .widget input[type="reset"]:focus, + .widget input[type="submit"]:hover, + .widget input[type="submit"]:focus, + .widget_calendar tbody a:hover, + .widget_calendar tbody a:focus { + background-color: %[2]v; + } + + .widget blockquote { + border-color: %[2]v; + } + + .main-navigation ul, + .main-navigation li, + .secondary-toggle, + .widget input, + .widget textarea, + .widget table, + .widget th, + .widget td, + .widget pre, + .widget li, + .widget_categories .children, + .widget_nav_menu .sub-menu, + .widget_pages .children, + .widget abbr[title] { + border-color: %[3]v; + } + + .dropdown-toggle:hover, + .dropdown-toggle:focus, + .widget hr { + background-color: %[3]v; + } + + .widget input:focus, + .widget textarea:focus { + border-color: %[4]v; + } + + .sidebar a:focus, + .dropdown-toggle:focus { + outline-color: %[4]v; + } + } +` diff --git a/internal/theme/twentyfifteen/custombackground.go b/internal/theme/twentyfifteen/custombackground.go index df4619b..b64b455 100644 --- a/internal/theme/twentyfifteen/custombackground.go +++ b/internal/theme/twentyfifteen/custombackground.go @@ -7,7 +7,6 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/constraints" - "github.com/fthvgb1/wp-go/internal/wpconfig" ) var postx = map[string]string{ @@ -32,45 +31,41 @@ var repeat = map[string]string{ "no-repeat": "no-repeat", } -var backgroud = reload.Vars(constraints.Defaults) +var background = reload.Vars(constraints.Defaults) func (h *handle) CustomBackGround() { - b := backgroud.Load() + b := background.Load() if b == constraints.Defaults { b = h.CalCustomBackGround() - backgroud.Store(b) + background.Store(b) } h.IndexHandle.GinH["customBackground"] = b } func (h *handle) CalCustomBackGround() (r string) { - mods, err := wpconfig.GetThemeMods(h.IndexHandle.Theme) - if err != nil { - return - } - if mods.BackgroundImage == "" && mods.BackgroundColor == themesupport.CustomBackground.DefaultColor { + if h.IndexHandle.ThemeMods.BackgroundImage == "" && h.IndexHandle.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor { return } s := str.NewBuilder() - if mods.BackgroundImage != "" { - s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(mods.BackgroundImage)) + if h.IndexHandle.ThemeMods.BackgroundImage != "" { + s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(h.IndexHandle.ThemeMods.BackgroundImage)) } - backgroundPositionX := helper.Defaults(mods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX) + backgroundPositionX := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX) backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left") - backgroundPositionY := helper.Defaults(mods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY) + backgroundPositionY := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY) backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top") positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY) - siz := helper.DefaultVal(mods.BackgroundSize, themesupport.CustomBackground.DefaultSize) + siz := helper.DefaultVal(h.IndexHandle.ThemeMods.BackgroundSize, themesupport.CustomBackground.DefaultSize) siz = maps.WithDefaultVal(size, siz, "auto") siz = fmt.Sprintf(" background-size: %s;", siz) - repeats := helper.Defaults(mods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat) + repeats := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat) repeats = maps.WithDefaultVal(repeat, repeats, "repeat") repeats = fmt.Sprintf(" background-repeat: %s;", repeats) - attachment := helper.Defaults(mods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment) + attachment := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment) attachment = helper.Or(attachment == "fixed", "fixed", "scroll") attachment = fmt.Sprintf(" background-attachment: %s;", attachment) s.WriteString(positon, siz, repeats, attachment) diff --git a/internal/theme/twentyfifteen/layout/head.gohtml b/internal/theme/twentyfifteen/layout/head.gohtml index 141c764..2f529bc 100644 --- a/internal/theme/twentyfifteen/layout/head.gohtml +++ b/internal/theme/twentyfifteen/layout/head.gohtml @@ -64,4 +64,8 @@ {{.customBackground|unescaped}} {{end}} + {{if .colorScheme}} + {{.colorScheme|unescaped}} + {{end}} + {{end}} \ No newline at end of file diff --git a/internal/theme/twentyfifteen/themesupport.go b/internal/theme/twentyfifteen/themesupport.go index 5a010bc..b3a488a 100644 --- a/internal/theme/twentyfifteen/themesupport.go +++ b/internal/theme/twentyfifteen/themesupport.go @@ -30,4 +30,5 @@ type EditorGradientPresets struct { Gradient string `json:"gradient"` } -var themesupport = themeSupport{} +var themesupport themeSupport +var colorscheme map[string]ColorScheme diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 9c37947..afacb64 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -1,12 +1,33 @@ package twentyfifteen import ( + "embed" + "encoding/json" "github.com/fthvgb1/wp-go/internal/pkg/constraints" + "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/theme/common" ) const ThemeName = "twentyfifteen" +func Init(fs embed.FS) { + b, err := fs.ReadFile("twentyfifteen/themesupport.json") + if err != nil { + return + } + err = json.Unmarshal(b, &themesupport) + logs.ErrPrintln(err, "解析themesupport失败") + bytes, err := fs.ReadFile("twentyfifteen/colorscheme.json") + if err != nil { + return + } + err = json.Unmarshal(bytes, &colorscheme) + if err != nil { + return + } + logs.ErrPrintln(err, "解析colorscheme失败") +} + type handle struct { *common.IndexHandle *common.DetailHandle @@ -37,12 +58,14 @@ func Hook(h *common.Handle) { func (h *handle) Index() { h.CustomHeader() + h.colorSchemeCss() h.CustomBackGround() h.Indexs() } func (h *handle) Detail() { h.CustomHeader() + h.colorSchemeCss() h.CustomBackGround() h.Details() } diff --git a/internal/wpconfig/thememods.go b/internal/wpconfig/thememods.go index f665ff3..a422611 100644 --- a/internal/wpconfig/thememods.go +++ b/internal/wpconfig/thememods.go @@ -19,20 +19,20 @@ func SetTemplateFs(fs embed.FS) { templateFs = fs } -type ThemeMod struct { - CustomCssPostId int `json:"custom_css_post_id,omitempty"` - NavMenuLocations []string `json:"nav_menu_locations,omitempty"` - CustomLogo int `json:"custom_logo,omitempty"` - HeaderImage string `json:"header_image,omitempty"` - BackgroundImage string `json:"background_image,omitempty"` - BackgroundSize string `json:"background_size,omitempty"` - BackgroundRepeat string `json:"background_repeat,omitempty"` - BackgroundColor string `json:"background_color,omitempty"` - BackgroundPreset string `json:"background_preset"` - BackgroundPositionX string `json:"background_position_x,omitempty"` - BackgroundPositionY string `json:"background_position_y"` - BackgroundAttachment string `json:"background_attachment"` - ColorScheme map[string]ColorScheme +type ThemeMods struct { + CustomCssPostId int `json:"custom_css_post_id,omitempty"` + NavMenuLocations []string `json:"nav_menu_locations,omitempty"` + CustomLogo int `json:"custom_logo,omitempty"` + HeaderImage string `json:"header_image,omitempty"` + BackgroundImage string `json:"background_image,omitempty"` + BackgroundSize string `json:"background_size,omitempty"` + BackgroundRepeat string `json:"background_repeat,omitempty"` + BackgroundColor string `json:"background_color,omitempty"` + BackgroundPreset string `json:"background_preset"` + BackgroundPositionX string `json:"background_position_x,omitempty"` + BackgroundPositionY string `json:"background_position_y"` + BackgroundAttachment string `json:"background_attachment"` + ColorScheme string `json:"color_scheme"` SidebarTextcolor string `json:"sidebar_textcolor,omitempty"` HeaderBackgroundColor string `json:"header_background_color,omitempty"` HeaderTextcolor string `json:"header_textcolor,omitempty"` @@ -104,29 +104,27 @@ func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except . return } -var themeModes = func() *safety.Map[string, ThemeMod] { - m := safety.NewMap[string, ThemeMod]() +var themeModes = func() *safety.Map[string, ThemeMods] { + m := safety.NewMap[string, ThemeMods]() reload.Push(func() { m.Flush() }) return m }() -func GetThemeMods(theme string) (r ThemeMod, err error) { +func GetThemeMods(theme string) (r ThemeMods, err error) { r, ok := themeModes.Load(theme) if ok { return } - mods, ok := Options.Load(fmt.Sprintf("theme_mods_%s", theme)) if !ok || mods == "" { return } - r, err = phphelper.UnPHPSerialize[ThemeMod](mods) + r, err = phphelper.UnPHPSerialize[ThemeMods](mods) if err != nil { return } - r.setThemeColorScheme(theme) r.setThemeSupport(theme) themeModes.Store(theme, r) return @@ -144,19 +142,7 @@ func IsCustomBackground(theme string) bool { return false } -func (m *ThemeMod) setThemeColorScheme(themeName string) { - bytes, err := templateFs.ReadFile(filepath.Join(themeName, "colorscheme.json")) - if err != nil { - return - } - var scheme map[string]ColorScheme - err = json.Unmarshal(bytes, &scheme) - if err != nil { - return - } - m.ColorScheme = scheme -} -func (m *ThemeMod) setThemeSupport(themeName string) { +func (m *ThemeMods) setThemeSupport(themeName string) { bytes, err := templateFs.ReadFile(filepath.Join(themeName, "themesupport.json")) if err != nil { return