|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Options; |
| 3 | +using Umbraco.Cms.Core.Configuration.Models; |
| 4 | +using Umbraco.Cms.Core.Models; |
| 5 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 6 | +using Umbraco.Cms.Core.Notifications; |
| 7 | +using Umbraco.Cms.Core.PublishedCache; |
| 8 | +using Umbraco.Cms.Core.Routing; |
| 9 | +using Umbraco.Cms.Core.Services; |
| 10 | +using Umbraco.Cms.Core.Services.Navigation; |
| 11 | +using Umbraco.Cms.Core.Web; |
| 12 | +using Umbraco.Extensions; |
| 13 | + |
| 14 | +namespace Umbraco.Cms.Core.Events; |
| 15 | + |
| 16 | +public class AddUnroutableContentWarningsWhenPublishingNotificationHandler : INotificationAsyncHandler<ContentPublishedNotification> |
| 17 | +{ |
| 18 | + private readonly IPublishedRouter _publishedRouter; |
| 19 | + private readonly IUmbracoContextAccessor _umbracoContextAccessor; |
| 20 | + private readonly ILanguageService _languageService; |
| 21 | + private readonly ILocalizedTextService _localizedTextService; |
| 22 | + private readonly IContentService _contentService; |
| 23 | + private readonly IVariationContextAccessor _variationContextAccessor; |
| 24 | + private readonly ILoggerFactory _loggerFactory; |
| 25 | + private readonly UriUtility _uriUtility; |
| 26 | + private readonly IPublishedUrlProvider _publishedUrlProvider; |
| 27 | + private readonly IPublishedContentCache _publishedContentCache; |
| 28 | + private readonly IDocumentNavigationQueryService _navigationQueryService; |
| 29 | + private readonly IEventMessagesFactory _eventMessagesFactory; |
| 30 | + private readonly ContentSettings _contentSettings; |
| 31 | + |
| 32 | + public AddUnroutableContentWarningsWhenPublishingNotificationHandler( |
| 33 | + IPublishedRouter publishedRouter, |
| 34 | + IUmbracoContextAccessor umbracoContextAccessor, |
| 35 | + ILanguageService languageService, |
| 36 | + ILocalizedTextService localizedTextService, |
| 37 | + IContentService contentService, |
| 38 | + IVariationContextAccessor variationContextAccessor, |
| 39 | + ILoggerFactory loggerFactory, |
| 40 | + UriUtility uriUtility, |
| 41 | + IPublishedUrlProvider publishedUrlProvider, |
| 42 | + IPublishedContentCache publishedContentCache, |
| 43 | + IDocumentNavigationQueryService navigationQueryService, |
| 44 | + IEventMessagesFactory eventMessagesFactory, |
| 45 | + IOptions<ContentSettings> contentSettings) |
| 46 | + { |
| 47 | + _publishedRouter = publishedRouter; |
| 48 | + _umbracoContextAccessor = umbracoContextAccessor; |
| 49 | + _languageService = languageService; |
| 50 | + _localizedTextService = localizedTextService; |
| 51 | + _contentService = contentService; |
| 52 | + _variationContextAccessor = variationContextAccessor; |
| 53 | + _loggerFactory = loggerFactory; |
| 54 | + _uriUtility = uriUtility; |
| 55 | + _publishedUrlProvider = publishedUrlProvider; |
| 56 | + _publishedContentCache = publishedContentCache; |
| 57 | + _navigationQueryService = navigationQueryService; |
| 58 | + _eventMessagesFactory = eventMessagesFactory; |
| 59 | + _contentSettings = contentSettings.Value; |
| 60 | + } |
| 61 | + |
| 62 | + public async Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken) |
| 63 | + { |
| 64 | + if (_contentSettings.ShowUnroutableContentWarnings is false) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + foreach (IContent content in notification.PublishedEntities) |
| 70 | + { |
| 71 | + string[]? successfulCultures; |
| 72 | + if (content.ContentType.VariesByCulture() is false) |
| 73 | + { |
| 74 | + // successfulCultures will be null here - change it to a wildcard and utilize this below |
| 75 | + successfulCultures = ["*"]; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + successfulCultures = content.PublishedCultures.ToArray(); |
| 80 | + } |
| 81 | + |
| 82 | + if (successfulCultures?.Any() is not true) |
| 83 | + { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (!_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbracoContext)) |
| 88 | + { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + UrlInfo[] urls = (await content.GetContentUrlsAsync( |
| 93 | + _publishedRouter, |
| 94 | + umbracoContext, |
| 95 | + _languageService, |
| 96 | + _localizedTextService, |
| 97 | + _contentService, |
| 98 | + _variationContextAccessor, |
| 99 | + _loggerFactory.CreateLogger<IContent>(), |
| 100 | + _uriUtility, |
| 101 | + _publishedUrlProvider, |
| 102 | + _publishedContentCache, |
| 103 | + _navigationQueryService)).ToArray(); |
| 104 | + |
| 105 | + |
| 106 | + EventMessages eventMessages = _eventMessagesFactory.Get(); |
| 107 | + foreach (var culture in successfulCultures) |
| 108 | + { |
| 109 | + if (urls.Where(u => u.Culture == culture || culture == "*").All(u => u.IsUrl is false)) |
| 110 | + { |
| 111 | + eventMessages.Add(new EventMessage("Content published", "The document does not have a URL, possibly due to a naming collision with another document. More details can be found under Info.", EventMessageType.Warning)); |
| 112 | + |
| 113 | + // only add one warning here, even though there might actually be more |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments