-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Misc improvements to outbound redirects
- Loading branch information
Showing
11 changed files
with
390 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Skybrud.Umbraco.Redirects/Factories/RedirectsModelsFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Skybrud.Essentials.Json.Extensions; | ||
using Skybrud.Umbraco.Redirects.Models; | ||
using Skybrud.Umbraco.Redirects.Models.Outbound; | ||
using Umbraco.Cms.Core.Models.PublishedContent; | ||
using Umbraco.Cms.Core.Web; | ||
using Umbraco.Extensions; | ||
|
||
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault | ||
|
||
namespace Skybrud.Umbraco.Redirects.Factories { | ||
|
||
/// <summary> | ||
/// Factory class for various models used within the redirects package. | ||
/// </summary> | ||
public class RedirectsModelsFactory { | ||
|
||
private readonly IUmbracoContextAccessor _umbracoContextAccessor; | ||
|
||
#region Properties | ||
|
||
/// <summary> | ||
/// Gets a reference to the current <see cref="IUmbracoContext"/>, if any. | ||
/// </summary> | ||
protected IUmbracoContext UmbracoContext => _umbracoContextAccessor.GetRequiredUmbracoContext(); | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
/// <summary> | ||
/// Initializes a new instance based on the specified <see cref="IUmbracoContextAccessor"/>. | ||
/// </summary> | ||
/// <param name="umbracoContextAccessor">An instance of <see cref="IUmbracoContextAccessor"/>.</param> | ||
public RedirectsModelsFactory(IUmbracoContextAccessor umbracoContextAccessor) { | ||
_umbracoContextAccessor = umbracoContextAccessor; | ||
} | ||
|
||
#endregion | ||
|
||
#region Member methods | ||
|
||
/// <summary> | ||
/// Creates and returns a new <see cref="OutboundRedirect"/> instance. | ||
/// </summary> | ||
/// <param name="json">A <see cref="JObject"/> instance representing the outbound redirect.</param> | ||
/// <returns>An instance of <see cref="OutboundRedirect"/>.</returns> | ||
public virtual IOutboundRedirect CreateOutboundRedirect(JObject json) { | ||
|
||
if (json == null) return null; | ||
|
||
// Gets the type of the redirect | ||
RedirectType type = json.GetBoolean("permanent") ? RedirectType.Permanent : RedirectType.Temporary; | ||
|
||
// Get whether query string forwarding should be enabled | ||
bool forward = json.GetBoolean("forward"); | ||
|
||
// Parse the destination | ||
RedirectDestination destination = json.GetObject("destination", RedirectDestination.Parse); | ||
if (destination is not { IsValid: true }) return null; | ||
|
||
// Look up the current URL for content and media | ||
switch (destination.Type) { | ||
|
||
case RedirectDestinationType.Content: | ||
IPublishedContent content = UmbracoContext?.Content?.GetById(destination.Key); | ||
if (content != null) destination.Url = content.Url(); | ||
break; | ||
|
||
case RedirectDestinationType.Media: | ||
IPublishedContent media = UmbracoContext?.Media?.GetById(destination.Key); | ||
if (media != null) destination.Url = media.Url(); | ||
break; | ||
|
||
} | ||
|
||
// Initialize and return a new outbound redirect | ||
return new OutboundRedirect(type, forward, destination, json); | ||
|
||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 24 additions & 4 deletions
28
src/Skybrud.Umbraco.Redirects/PropertyEditors/OutboundRedirectValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,53 @@ | ||
using Skybrud.Umbraco.Redirects.Models.Outbound; | ||
using Newtonsoft.Json.Linq; | ||
using Skybrud.Essentials.Json.Newtonsoft; | ||
using Skybrud.Umbraco.Redirects.Factories; | ||
using Skybrud.Umbraco.Redirects.Models.Outbound; | ||
using Umbraco.Cms.Core.Models.PublishedContent; | ||
using Umbraco.Cms.Core.PropertyEditors; | ||
using Umbraco.Extensions; | ||
|
||
namespace Skybrud.Umbraco.Redirects.PropertyEditors { | ||
|
||
internal class OutboundRedirectValueConverter : PropertyValueConverterBase { | ||
|
||
private readonly RedirectsModelsFactory _modelsFactory; | ||
|
||
#region Constructors | ||
|
||
public OutboundRedirectValueConverter(RedirectsModelsFactory modelsFactory) { | ||
_modelsFactory = modelsFactory; | ||
} | ||
|
||
#endregion | ||
|
||
#region Member methods | ||
|
||
public override bool IsConverter(IPublishedPropertyType propertyType) { | ||
return propertyType.EditorAlias == OutboundRedirectEditor.EditorAlias; | ||
} | ||
|
||
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) { | ||
return OutboundRedirect.Deserialize(source as string); | ||
return source is string json && json.DetectIsJson() ? JsonUtils.ParseJsonObject(json) : null; | ||
} | ||
|
||
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { | ||
return inter as OutboundRedirect; | ||
return _modelsFactory.CreateOutboundRedirect(inter as JObject); | ||
} | ||
|
||
public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { | ||
return null; | ||
} | ||
|
||
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.None; | ||
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) { | ||
return PropertyCacheLevel.None; | ||
} | ||
|
||
public override System.Type GetPropertyValueType(IPublishedPropertyType propertyType) { | ||
return typeof(IOutboundRedirect); | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.