-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
SeoMetaPartHandler.cs
202 lines (172 loc) · 7.38 KB
/
SeoMetaPartHandler.cs
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
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Handlers;
using OrchardCore.DisplayManagement;
using OrchardCore.Media;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Seo.Models;
using OrchardCore.Settings;
namespace OrchardCore.Seo.Drivers
{
public class SeoMetaPartHandler : ContentPartHandler<SeoMetaPart>
{
private readonly IMediaFileStore _mediaFileStore;
private readonly ISiteService _siteService;
private readonly IActionContextAccessor _actionContextAccessor;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly IContentManager _contentManager;
public SeoMetaPartHandler(
IMediaFileStore mediaFileStore,
ISiteService siteService,
IActionContextAccessor actionContextAccessor,
IHttpContextAccessor httpContextAccessor,
IUrlHelperFactory urlHelperFactory,
IContentManager contentManager
)
{
_mediaFileStore = mediaFileStore;
_siteService = siteService;
_actionContextAccessor = actionContextAccessor;
_httpContextAccessor = httpContextAccessor;
_urlHelperFactory = urlHelperFactory;
_contentManager = contentManager;
}
public override Task GetContentItemAspectAsync(ContentItemAspectContext context, SeoMetaPart part)
{
return context.ForAsync<SeoAspect>(async aspect =>
{
aspect.Render = part.Render;
if (!string.IsNullOrEmpty(part.PageTitle))
{
aspect.PageTitle = part.PageTitle;
}
if (!string.IsNullOrEmpty(part.MetaDescription))
{
aspect.MetaDescription = part.MetaDescription;
}
if (!string.IsNullOrEmpty(part.MetaKeywords))
{
aspect.MetaKeywords = part.MetaKeywords;
}
if (!string.IsNullOrEmpty(part.MetaRobots))
{
aspect.MetaRobots = part.MetaRobots;
}
aspect.CustomMetaTags = part.CustomMetaTags;
var siteSettings = await _siteService.GetSiteSettingsAsync();
var actionContext = _actionContextAccessor.ActionContext;
actionContext ??= await GetActionContextAsync(_httpContextAccessor.HttpContext);
var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext);
if (!string.IsNullOrEmpty(part.Canonical))
{
aspect.Canonical = part.Canonical;
}
else
{
var contentItemMetadata = await _contentManager.PopulateAspectAsync<ContentItemMetadata>(part.ContentItem);
var relativeUrl = urlHelper.RouteUrl(contentItemMetadata.DisplayRouteValues);
aspect.Canonical = urlHelper.ToAbsoluteUrl(relativeUrl);
}
// OpenGraph
if (part.OpenGraphImage?.Paths?.Length > 0)
{
aspect.OpenGraphImage = urlHelper.ToAbsoluteUrl(_mediaFileStore.MapPathToPublicUrl(part.OpenGraphImage.Paths[0]));
}
else if (part.DefaultSocialImage?.Paths?.Length > 0)
{
aspect.OpenGraphImage = urlHelper.ToAbsoluteUrl(_mediaFileStore.MapPathToPublicUrl(part.DefaultSocialImage.Paths[0]));
}
if (part.OpenGraphImage?.MediaTexts?.Length > 0)
{
aspect.OpenGraphImageAlt = part.OpenGraphImage.MediaTexts[0];
}
else if (part.DefaultSocialImage?.MediaTexts?.Length > 0)
{
aspect.OpenGraphImageAlt = part.DefaultSocialImage.MediaTexts[0];
}
if (!string.IsNullOrEmpty(part.OpenGraphTitle))
{
aspect.OpenGraphTitle = part.OpenGraphTitle;
}
else
{
aspect.OpenGraphTitle = part.PageTitle;
}
if (!string.IsNullOrEmpty(part.OpenGraphDescription))
{
aspect.OpenGraphDescription = part.OpenGraphDescription;
}
else
{
aspect.OpenGraphDescription = part.MetaDescription;
}
// Twitter
if (part.TwitterImage?.Paths?.Length > 0)
{
aspect.TwitterImage = urlHelper.ToAbsoluteUrl(_mediaFileStore.MapPathToPublicUrl(part.TwitterImage.Paths[0]));
}
else if (part.DefaultSocialImage?.Paths?.Length > 0)
{
aspect.TwitterImage = urlHelper.ToAbsoluteUrl(_mediaFileStore.MapPathToPublicUrl(part.DefaultSocialImage.Paths[0]));
}
if (part.TwitterImage?.MediaTexts?.Length > 0)
{
aspect.TwitterImageAlt = part.TwitterImage.MediaTexts[0];
}
else if (part.DefaultSocialImage?.MediaTexts?.Length > 0)
{
aspect.TwitterImageAlt = part.DefaultSocialImage.MediaTexts[0];
}
if (!string.IsNullOrEmpty(part.TwitterTitle))
{
aspect.TwitterTitle = part.TwitterTitle;
}
else
{
aspect.TwitterTitle = part.PageTitle;
}
if (!string.IsNullOrEmpty(part.TwitterDescription))
{
aspect.TwitterDescription = part.TwitterDescription;
}
else
{
aspect.TwitterDescription = part.MetaDescription;
}
if (!string.IsNullOrEmpty(part.TwitterCard))
{
aspect.TwitterCard = part.TwitterCard;
}
if (!string.IsNullOrEmpty(part.TwitterCreator))
{
aspect.TwitterCreator = part.TwitterCreator;
}
if (!string.IsNullOrEmpty(part.TwitterSite))
{
aspect.TwitterSite = part.TwitterSite;
}
aspect.GoogleSchema = part.GoogleSchema;
});
}
internal static async Task<ActionContext> GetActionContextAsync(HttpContext httpContext)
{
var routeData = new RouteData();
routeData.Routers.Add(new RouteCollection());
var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
var filters = httpContext.RequestServices.GetServices<IAsyncViewActionFilter>();
foreach (var filter in filters)
{
await filter.OnActionExecutionAsync(actionContext);
}
return actionContext;
}
}
}