From 6308cc1dae8b18b54b402617519b72e56b987172 Mon Sep 17 00:00:00 2001 From: patrickdemooij9 Date: Thu, 27 Apr 2023 13:42:11 +0200 Subject: [PATCH] Set default for seo settings (#183) * Set default for seo settings * Don't use tree yet --- .../Composers/SeoToolkitComposer.cs | 24 ++++------- .../Models/Config/GlobalAppSettingsModel.cs | 8 ++++ .../Models/Config/GlobalConfig.cs | 8 ++++ .../SeoSettingsRepository.cs | 8 +++- .../SettingsService/GlobalConfigService.cs | 26 ++++++++++++ .../Config/GlobalAppSettingsModel.cs | 7 ---- .../Startup/SeoToolkitComposer.cs | 2 +- .../Controllers/MetaFieldsController.cs | 6 +-- .../Controllers/MetaFieldsTreeController.cs | 42 +++++++++++++++++++ .../Providers/DefaultMetaTagsProvider.cs | 6 +-- .../ManifestLoader.cs | 3 +- .../MetaFields/settings.controller.js | 8 ++++ .../backoffice/MetaFields/settings.html | 3 ++ 13 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalAppSettingsModel.cs create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalConfig.cs create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Services/SettingsService/GlobalConfigService.cs delete mode 100644 src/SeoToolkit.Umbraco.Core/Config/GlobalAppSettingsModel.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsTreeController.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.controller.js create mode 100644 src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.html diff --git a/src/SeoToolkit.Umbraco.Common.Core/Composers/SeoToolkitComposer.cs b/src/SeoToolkit.Umbraco.Common.Core/Composers/SeoToolkitComposer.cs index 804981c3..d1fef688 100644 --- a/src/SeoToolkit.Umbraco.Common.Core/Composers/SeoToolkitComposer.cs +++ b/src/SeoToolkit.Umbraco.Common.Core/Composers/SeoToolkitComposer.cs @@ -8,6 +8,8 @@ using SeoToolkit.Umbraco.Common.Core.Repositories.SeoSettingsRepository; using SeoToolkit.Umbraco.Common.Core.Sections; using SeoToolkit.Umbraco.Common.Core.Services.SeoSettingsService; +using SeoToolkit.Umbraco.Common.Core.Services.SettingsService; +using SeoToolkit.Umbraco.Common.Core.Models.Config; namespace SeoToolkit.Umbraco.Common.Core.Composers { @@ -15,6 +17,10 @@ public class SeoToolkitComposer : IComposer { public void Compose(IUmbracoBuilder builder) { + var section = builder.Config.GetSection("SeoToolkit:Global"); + builder.Services.Configure(section); + builder.Services.AddSingleton(typeof(ISettingsService), typeof(GlobalConfigService)); + builder.Sections().Append(); builder.Dashboards().Add(); @@ -26,23 +32,7 @@ public void Compose(IUmbracoBuilder builder) builder.Services.AddUnique(); builder.Services.AddUnique(); - - /*builder.Services.AddTransient(sp => - { - var languageFiles = new List(); - var webhostEnvironment = sp.GetRequiredService(); - - var seoToolkitFolder = webhostEnvironment.ContentRootFileProvider.GetDirectoryContents("/App_Plugins/SeoToolkit/"); - foreach (var dir in seoToolkitFolder.Where(it => it.IsDirectory)) - { - foreach (var langDir in new DirectoryInfo(dir.PhysicalPath).EnumerateDirectories().Where(d => d.Exists && d.Name.InvariantEquals("lang"))) - { - languageFiles.AddRange(langDir.EnumerateFiles("*.xml").Select(langFile => new LocalizedTextServiceSupplementaryFileSource(langFile, false))); - } - } - languageFiles.Add(new LocalizedTextServiceSupplementaryFileSource(new FileInfo(webhostEnvironment.ContentRootFileProvider.GetFileInfo("/App_Plugins/SeoToolkit/lang/en-us.xml").PhysicalPath), false)); - return (IEnumerable)languageFiles; - });*/ + } } } diff --git a/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalAppSettingsModel.cs b/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalAppSettingsModel.cs new file mode 100644 index 00000000..db8da1ef --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalAppSettingsModel.cs @@ -0,0 +1,8 @@ +namespace SeoToolkit.Umbraco.Common.Core.Models.Config +{ + public class GlobalAppSettingsModel + { + public bool AutomaticSitemapsInRobotsTxt { get; set; } = true; + public bool EnableSeoSettingsByDefault { get; set; } = false; // TODO: Change this in a major version to true; + } +} diff --git a/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalConfig.cs b/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalConfig.cs new file mode 100644 index 00000000..ffd06808 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Models/Config/GlobalConfig.cs @@ -0,0 +1,8 @@ +namespace SeoToolkit.Umbraco.Common.Core.Models.Config +{ + public class GlobalConfig + { + public bool AutomaticSitemapsInRobotsTxt { get; set; } + public bool EnableSeoSettingsByDefault { get; set; } + } +} diff --git a/src/SeoToolkit.Umbraco.Common.Core/Repositories/SeoSettingsRepository/SeoSettingsRepository.cs b/src/SeoToolkit.Umbraco.Common.Core/Repositories/SeoSettingsRepository/SeoSettingsRepository.cs index 80a01c5d..63a68e6f 100644 --- a/src/SeoToolkit.Umbraco.Common.Core/Repositories/SeoSettingsRepository/SeoSettingsRepository.cs +++ b/src/SeoToolkit.Umbraco.Common.Core/Repositories/SeoSettingsRepository/SeoSettingsRepository.cs @@ -1,16 +1,20 @@ using Umbraco.Extensions; using SeoToolkit.Umbraco.Common.Core.Models.Database; using Umbraco.Cms.Infrastructure.Scoping; +using SeoToolkit.Umbraco.Common.Core.Services.SettingsService; +using SeoToolkit.Umbraco.Common.Core.Models.Config; namespace SeoToolkit.Umbraco.Common.Core.Repositories.SeoSettingsRepository { public class SeoSettingsRepository : ISeoSettingsRepository { private readonly IScopeProvider _scopeProvider; + private readonly ISettingsService _settingsService; - public SeoSettingsRepository(IScopeProvider scopeProvider) + public SeoSettingsRepository(IScopeProvider scopeProvider, ISettingsService settingsService) { _scopeProvider = scopeProvider; + _settingsService = settingsService; } public bool IsEnabled(int contentTypeId) @@ -24,7 +28,7 @@ public bool IsEnabled(int contentTypeId) //Default is disabled. if (entity is null) - return false; + return _settingsService.GetSettings().EnableSeoSettingsByDefault; return entity.Enabled; } } diff --git a/src/SeoToolkit.Umbraco.Common.Core/Services/SettingsService/GlobalConfigService.cs b/src/SeoToolkit.Umbraco.Common.Core/Services/SettingsService/GlobalConfigService.cs new file mode 100644 index 00000000..c1393d97 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Services/SettingsService/GlobalConfigService.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Options; +using SeoToolkit.Umbraco.Common.Core.Models.Config; +using System; + +namespace SeoToolkit.Umbraco.Common.Core.Services.SettingsService +{ + public class GlobalConfigService : DefaultAppSettingsService + { + private readonly IOptionsMonitor _config; + + public GlobalConfigService(IOptionsMonitor config) + { + _config = config; + } + + public override GlobalConfig GetSettings() + { + var settings = _config.CurrentValue; + return new GlobalConfig + { + AutomaticSitemapsInRobotsTxt = settings.AutomaticSitemapsInRobotsTxt, + EnableSeoSettingsByDefault = settings.EnableSeoSettingsByDefault + }; + } + } +} diff --git a/src/SeoToolkit.Umbraco.Core/Config/GlobalAppSettingsModel.cs b/src/SeoToolkit.Umbraco.Core/Config/GlobalAppSettingsModel.cs deleted file mode 100644 index b7b2584f..00000000 --- a/src/SeoToolkit.Umbraco.Core/Config/GlobalAppSettingsModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SeoToolkit.Umbraco.Core.Config -{ - public class GlobalAppSettingsModel - { - public bool AutomaticSitemapsInRobotsTxt { get; set; } = true; - } -} diff --git a/src/SeoToolkit.Umbraco.Core/Startup/SeoToolkitComposer.cs b/src/SeoToolkit.Umbraco.Core/Startup/SeoToolkitComposer.cs index f6d7eae3..b4c12188 100644 --- a/src/SeoToolkit.Umbraco.Core/Startup/SeoToolkitComposer.cs +++ b/src/SeoToolkit.Umbraco.Core/Startup/SeoToolkitComposer.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using SeoToolkit.Umbraco.Core.Config; +using SeoToolkit.Umbraco.Common.Core.Models.Config; using SeoToolkit.Umbraco.Core.Connectors; using SeoToolkit.Umbraco.RobotsTxt.Core.Interfaces; using Umbraco.Cms.Core.Composing; diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsController.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsController.cs index cbcc6960..5024572d 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsController.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsController.cs @@ -101,16 +101,14 @@ public IActionResult Get(int nodeId, string culture) [HttpPost] public IActionResult Save(MetaFieldsSettingsPostViewModel postModel) - { - var settings = _documentTypeSettingsService.Get(postModel.ContentTypeId); - + { if (!_seoSettingsService.IsEnabled(postModel.ContentTypeId)) return BadRequest("SEO settings are turned off for this node!"); EnsureLanguage(postModel.Culture); var isDirty = false; var values = new Dictionary(); - foreach (var (seoField, _) in settings.Fields) + foreach (var seoField in _fieldCollection) { if (!postModel.UserValues.ContainsKey(seoField.Alias)) { diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsTreeController.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsTreeController.cs new file mode 100644 index 00000000..d710a854 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Controllers/MetaFieldsTreeController.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Trees; +using Umbraco.Cms.Web.BackOffice.Trees; +using Umbraco.Cms.Web.Common.Attributes; + +namespace SeoToolkit.Umbraco.MetaFields.Core.Controllers +{ + /*[Tree("SeoToolkit", "MetaFields", TreeTitle = "MetaFields", TreeGroup = "SeoToolkit", SortOrder = 5)] + [PluginController("SeoToolkit")] + public class MetaFieldsTreeController : TreeController + { + public MetaFieldsTreeController(ILocalizedTextService localizedTextService, UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, IEventAggregator eventAggregator) : base(localizedTextService, umbracoApiControllerTypeCollection, eventAggregator) + { + } + + protected override ActionResult CreateRootNode(FormCollection queryStrings) + { + var root = base.CreateRootNode(queryStrings); + + root.Value.Icon = "icon-trafic"; + root.Value.HasChildren = false; + root.Value.RoutePath = $"{SectionAlias}/{TreeAlias}/settings"; + root.Value.MenuUrl = null; + + return root.Value; + } + + protected override ActionResult GetMenuForNode(string id, FormCollection queryStrings) + { + return null; + } + + protected override ActionResult GetTreeNodes(string id, FormCollection queryStrings) + { + return null; + } + }*/ +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Providers/DefaultMetaTagsProvider.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Providers/DefaultMetaTagsProvider.cs index 254e6d05..a0bcba4a 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Providers/DefaultMetaTagsProvider.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Providers/DefaultMetaTagsProvider.cs @@ -60,8 +60,6 @@ public MetaTagsModel Get(IPublishedContent content, bool includeUserValues) _eventAggregator.Publish(new BeforeMetaTagsNotification(content.ContentType.Alias, metaTags)); var settings = _documentTypeSettingsService.Get(content.ContentType.Id); - if (settings is null) - return null; if (_seoSettingsService.IsEnabled(content.ContentType.Id) != true) return null; var userValues = includeUserValues ? _seoValueService.GetUserValues(content.Id) : null; @@ -79,7 +77,7 @@ public MetaTagsModel Get(IPublishedContent content, bool includeUserValues) intermediateObject = result; } - if (intermediateObject is null) + if (intermediateObject is null && settings != null) { var documentTypeValue = settings.Get(it.Alias); if (documentTypeValue != null && documentTypeValue.UseInheritedValue) @@ -88,7 +86,7 @@ public MetaTagsModel Get(IPublishedContent content, bool includeUserValues) while (inheritance != null) { var inheritedSettings = _documentTypeSettingsService.Get(inheritance.Id); - documentTypeValue = inheritedSettings.Get(it.Alias); + documentTypeValue = inheritedSettings?.Get(it.Alias); if (documentTypeValue != null && documentTypeValue.UseInheritedValue) inheritance = inheritedSettings.Inheritance; else diff --git a/src/SeoToolkit.Umbraco.MetaFields/ManifestLoader.cs b/src/SeoToolkit.Umbraco.MetaFields/ManifestLoader.cs index cb28b87a..9c5bb46a 100644 --- a/src/SeoToolkit.Umbraco.MetaFields/ManifestLoader.cs +++ b/src/SeoToolkit.Umbraco.MetaFields/ManifestLoader.cs @@ -32,7 +32,8 @@ public void Filter(List manifests) "/App_Plugins/SeoToolkit/MetaFields/Interface/SeoFieldEditors/PropertyEditor/noSelectCheckboxList.controller.js", "/App_Plugins/SeoToolkit/MetaFields/Interface/Components/ItemGroupPicker/itemGroupPicker.controller.js", "/App_Plugins/SeoToolkit/MetaFields/Interface/SeoFieldEditors/seoFieldEditor.directive.js", - "/App_Plugins/SeoToolkit/MetaFields/Interface/Previewers/previewer.directive.js" + "/App_Plugins/SeoToolkit/MetaFields/Interface/Previewers/previewer.directive.js", + "/App_Plugins/SeoToolkit/backoffice/MetaFields/settings.controller.js" }, Stylesheets = new[] { diff --git a/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.controller.js b/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.controller.js new file mode 100644 index 00000000..4c659b31 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.controller.js @@ -0,0 +1,8 @@ +(function () { + + function globalSettingsController() { + var vm = this; + } + + angular.module("umbraco").controller("SeoToolkit.MetaFields.GlobalSettingsController", globalSettingsController); +})(); \ No newline at end of file diff --git a/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.html b/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.html new file mode 100644 index 00000000..cefb8dc5 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields/wwwroot/backoffice/MetaFields/settings.html @@ -0,0 +1,3 @@ +
+ Hello +
\ No newline at end of file