-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathseo_setting.go
324 lines (281 loc) · 8.73 KB
/
seo_setting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package seo
import (
"bytes"
"database/sql/driver"
"encoding/json"
"html/template"
"log"
"net/url"
"time"
"github.com/qor/admin"
"github.com/qor/media/media_library"
"github.com/qor/qor"
"github.com/qor/qor/resource"
)
// QorSEOSettingInterface support customize Seo model
type QorSEOSettingInterface interface {
GetName() string
SetName(string)
GetSEOSetting() Setting
GetGlobalSetting() map[string]string
SetGlobalSetting(map[string]string)
GetSEOType() string
SetSEOType(string)
GetIsGlobalSEO() bool
SetIsGlobalSEO(bool)
GetTitle() string
GetDescription() string
GetKeywords() string
SetCollection(*Collection)
GetOpenGraphTitle() string
GetOpenGraphDescription() string
GetOpenGraphURL() string
GetOpenGraphType() string
GetOpenGraphImageURL() string
GetOpenGraphImageFromMediaLibrary() media_library.MediaBox
GetOpenGraphMetadata() []OpenGraphMetadata
}
// QorSEOSetting default seo model
type QorSEOSetting struct {
Name string `gorm:"primary_key"`
Setting Setting
IsGlobalSEO bool
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `gorm:"index"`
collection *Collection
}
// Setting defined meta's attributes
type Setting struct {
Title string `gorm:"size:4294967295"`
Description string
Keywords string
Type string
OpenGraphTitle string
OpenGraphDescription string
OpenGraphURL string
OpenGraphType string
OpenGraphImageURL string
OpenGraphImageFromMediaLibrary media_library.MediaBox
OpenGraphMetadata []OpenGraphMetadata
EnabledCustomize bool
GlobalSetting map[string]string
}
// OpenGraphMetadata open graph meta data
type OpenGraphMetadata struct {
Property string
Content string
}
// GetSEOSetting get seo setting
func (s QorSEOSetting) GetSEOSetting() Setting {
return s.Setting
}
// GetName get QorSeoSetting's name
func (s QorSEOSetting) GetName() string {
return s.Name
}
// SetName set QorSeoSetting's name
func (s *QorSEOSetting) SetName(name string) {
s.Name = name
}
// GetSEOType get QorSeoSetting's type
func (s QorSEOSetting) GetSEOType() string {
return s.Setting.Type
}
// SetSEOType set QorSeoSetting's type
func (s *QorSEOSetting) SetSEOType(t string) {
s.Setting.Type = t
}
// GetIsGlobalSEO get QorSEOSetting's isGlobal
func (s QorSEOSetting) GetIsGlobalSEO() bool {
return s.IsGlobalSEO
}
// SetIsGlobalSEO set QorSeoSetting's isGlobal
func (s *QorSEOSetting) SetIsGlobalSEO(isGlobal bool) {
s.IsGlobalSEO = isGlobal
}
// GetGlobalSetting get QorSeoSetting's globalSetting
func (s QorSEOSetting) GetGlobalSetting() map[string]string {
return s.Setting.GlobalSetting
}
// SetGlobalSetting set QorSeoSetting's globalSetting
func (s *QorSEOSetting) SetGlobalSetting(globalSetting map[string]string) {
s.Setting.GlobalSetting = globalSetting
}
func (s QorSEOSetting) GetOpenGraphTitle() string {
return s.Setting.OpenGraphTitle
}
func (s QorSEOSetting) GetOpenGraphDescription() string {
return s.Setting.OpenGraphDescription
}
func (s QorSEOSetting) GetOpenGraphURL() string {
return s.Setting.OpenGraphURL
}
func (s QorSEOSetting) GetOpenGraphType() string {
return s.Setting.OpenGraphType
}
func (s QorSEOSetting) GetOpenGraphImageURL() string {
return s.Setting.OpenGraphImageURL
}
func (s QorSEOSetting) GetOpenGraphImageFromMediaLibrary() media_library.MediaBox {
return s.Setting.OpenGraphImageFromMediaLibrary
}
func (s QorSEOSetting) GetOpenGraphMetadata() []OpenGraphMetadata {
return s.Setting.OpenGraphMetadata
}
// GetTitle get Setting's title
func (s QorSEOSetting) GetTitle() string {
return s.Setting.Title
}
// GetDescription get Setting's description
func (s QorSEOSetting) GetDescription() string {
return s.Setting.Description
}
// GetKeywords get Setting's keywords
func (s QorSEOSetting) GetKeywords() string {
return s.Setting.Keywords
}
// SetCollection set Setting's collection
func (s *QorSEOSetting) SetCollection(collection *Collection) {
s.collection = collection
}
// GetSEO get Setting's SEO configure
func (s QorSEOSetting) GetSEO() *SEO {
return s.collection.GetSEO(s.Name)
}
// Scan scan value from database into struct
func (setting *Setting) Scan(value interface{}) error {
if bytes, ok := value.([]byte); ok {
json.Unmarshal(bytes, setting)
} else if str, ok := value.(string); ok {
json.Unmarshal([]byte(str), setting)
} else if strs, ok := value.([]string); ok {
for _, str := range strs {
json.Unmarshal([]byte(str), setting)
}
}
return nil
}
// Value get value from struct, and save into database
func (setting Setting) Value() (driver.Value, error) {
result, err := json.Marshal(setting)
return string(result), err
}
// FormattedHTML return formated seo setting as HTML
func (setting Setting) FormattedHTML(context *qor.Context) template.HTML {
toAbsoluteURL := func(str string) string {
if u, err := url.Parse(str); err == nil {
if u.IsAbs() {
return str
}
if u.Host == "" && context.Request != nil {
u.Host = context.Request.Host
}
if u.Scheme == "" {
if context.Request != nil && context.Request.URL.Scheme != "" {
u.Scheme = context.Request.URL.Scheme
} else {
u.Scheme = "http"
}
}
return u.String()
}
return ""
}
openGraphData := map[string]string{}
if setting.OpenGraphURL != "" {
openGraphData["og:url"] = toAbsoluteURL(setting.OpenGraphURL)
}
if setting.OpenGraphType != "" {
openGraphData["og:type"] = setting.OpenGraphType
}
if len(setting.OpenGraphImageFromMediaLibrary.Files) > 0 {
openGraphData["og:image"] = toAbsoluteURL(setting.OpenGraphImageFromMediaLibrary.URL())
} else if setting.OpenGraphImageURL != "" {
openGraphData["og:image"] = toAbsoluteURL(setting.OpenGraphImageURL)
}
for _, metavalue := range setting.OpenGraphMetadata {
openGraphData[metavalue.Property] = metavalue.Content
}
if _, ok := openGraphData["og:title"]; !ok {
title := setting.Title
if setting.OpenGraphTitle != "" {
title = setting.OpenGraphTitle
}
openGraphData["og:title"] = title
}
if _, ok := openGraphData["og:description"]; !ok {
desc := setting.Description
if setting.OpenGraphDescription != "" {
desc = setting.OpenGraphDescription
}
openGraphData["og:description"] = desc
}
var buf bytes.Buffer
err := seoTmpl.Execute(&buf, map[string]interface{}{
"title": setting.Title,
"description": setting.Description,
"keywords": setting.Keywords,
"ogs": openGraphData,
})
if err != nil {
var requestURL string
if context != nil && context.Request != nil && context.Request.URL != nil {
requestURL = context.Request.URL.String()
}
log.Printf("Error: exec seo tmpl has err (%v) in %s", err, requestURL)
return ""
}
return template.HTML(buf.String())
}
var seoTmpl = template.Must(
template.New("seo_tmpl").Parse(`<title>{{.title}}</title>
<meta name="description" content="{{.description}}">
<meta name="keywords" content="{{.keywords}}">
{{range $key, $val := .ogs -}}
{{if ne $val "" -}}
<meta property="{{$key}}" name="{{$key}}" content="{{$val}}">
{{end -}}
{{end -}}`),
)
// ConfigureQorMetaBeforeInitialize configure SEO setting for qor admin
func (setting Setting) ConfigureQorMetaBeforeInitialize(meta resource.Metaor) {
if meta, ok := meta.(*admin.Meta); ok {
meta.Type = "seo"
if res, ok := meta.GetBaseResource().(*admin.Resource); ok {
res.UseTheme("seo_meta")
}
}
}
// ConfigureQorResource configure resource for seo setting
func (setting Setting) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
res.Meta(&admin.Meta{Name: "Title", Label: "HTML Title"})
res.Meta(&admin.Meta{Name: "Description", Label: "Meta Description"})
res.Meta(&admin.Meta{Name: "Keywords", Label: "Meta Keywords"})
res.Meta(&admin.Meta{Name: "Type", Type: "hidden"})
res.Meta(&admin.Meta{Name: "EnabledCustomize", Type: "hidden"})
res.Meta(&admin.Meta{Name: "OpenGraphImageFromMediaLibrary", Label: "Open Graph Image", Config: &media_library.MediaBoxConfig{
Max: 1,
AllowType: media_library.ALLOW_TYPE_IMAGE,
}})
metadataResource := res.Meta(&admin.Meta{Name: "OpenGraphMetadata"}).Resource
metadataResource.NewAttrs(&admin.Section{Rows: [][]string{{"Property", "Content"}}})
metadataResource.EditAttrs(&admin.Section{Rows: [][]string{{"Property", "Content"}}})
res.EditAttrs(
&admin.Section{
Title: "Basic",
Rows: [][]string{{"Title"}, {"Description"}, {"Keywords"}},
},
&admin.Section{
Title: "Open Graph Information",
Rows: [][]string{
{"OpenGraphTitle", "OpenGraphDescription"},
{"OpenGraphURL", "OpenGraphType"},
{"OpenGraphImageURL", "OpenGraphImageFromMediaLibrary"}, {"OpenGraphMetadata"},
},
},
"Type", "EnabledCustomize",
)
}
}