2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-16 11:03:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-04 12:37:06 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/cache"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-02-16 11:03:31 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-08-26 14:01:20 +00:00
|
|
|
var iconSizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"}
|
2023-02-16 11:03:31 +00:00
|
|
|
|
2023-02-23 15:42:51 +00:00
|
|
|
func CalSiteIcon(h *Handle) (r string) {
|
2023-02-20 17:07:32 +00:00
|
|
|
id := str.ToInteger[uint64](wpconfig.GetOption("site_icon"), 0)
|
2023-02-16 11:03:31 +00:00
|
|
|
if id < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
icon, err := cache.GetPostById(h.C, id)
|
|
|
|
if err != nil || icon.AttachmentMetadata.File == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := strings.Join(strings.Split(icon.AttachmentMetadata.File, "/")[:2], "/")
|
2023-08-26 14:01:20 +00:00
|
|
|
size := slice.FilterAndMap(iconSizes, func(t string) (string, bool) {
|
2023-02-16 11:03:31 +00:00
|
|
|
s, ok := icon.AttachmentMetadata.Sizes[t]
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case "site_icon-270":
|
|
|
|
return fmt.Sprintf(`<meta name="msapplication-TileImage" content="/wp-content/uploads/%s/%s" />`, m, s.File), true
|
|
|
|
case "site_icon-180":
|
2023-02-19 05:42:07 +00:00
|
|
|
return fmt.Sprintf(`<link rel="apple-touch-icon" href="/wp-content/uploads/%s/%s" />`, m, s.File), true
|
2023-02-16 11:03:31 +00:00
|
|
|
default:
|
|
|
|
ss := strings.Replace(t, "site_icon-", "", 1)
|
|
|
|
return fmt.Sprintf(`<link rel="icon" href="/wp-content/uploads/%s/%s" sizes="%sx%s" />`, m, s.File, ss, ss), true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
r = strings.Join(size, "\n")
|
|
|
|
return
|
|
|
|
}
|