private string BuildLinkWithFilters(WPEnums.ContentTypes contentType, Dictionary <WPEnums.Filters, string> filterParams, string endPoint) { //how specific parameters are constructed can be found in the "Apply" extensions for WPEnums.Filters if (filterParams != null && filterParams.Count > 0) { int i = 0; foreach (var param in filterParams) { var paramValue = param.Value; //Menus are a completely different API; therefore, the following standard for is set for WP REST API V2 plugin if (contentType != WPEnums.ContentTypes.MENU && contentType != WPEnums.ContentTypes.MENUCOLLECTION) { endPoint = i == 0 ? endPoint + "?" : endPoint + "&"; } //If the menu id is not an integer, but rather a slug value else if (contentType == WPEnums.ContentTypes.MENU && param.Key == WPEnums.Filters.slug) { paramValue = param.Value.Replace(WPEnums.Filters.slug.Apply(null), null); if (string.IsNullOrEmpty(paramValue)) { var menuId = GetDefaultMenu(); paramValue = @"/" + menuId; } } //this is the generic implementation for endPoint = endPoint + paramValue; i++; } } return(endPoint); }
private string BuildCacheKey(WPEnums.ContentTypes contentType, string endPoint) { //this area can be used for anything needed to modify the cache key string cacheKey = endPoint; return(cacheKey); }
/// <summary> /// Mock service method /// </summary> /// <param name="path"></param> /// <param name="contentType"></param> /// <returns></returns> public string MockWebCall(string path, WPEnums.ContentTypes contentType) { string response = null; switch (contentType) { case WPEnums.ContentTypes.CATEGORY: response = MockCategoryResponse(); break; case WPEnums.ContentTypes.PAGE: response = MockPageResponse(); break; case WPEnums.ContentTypes.POST: response = MockPostResponse(); break; case WPEnums.ContentTypes.POSTCATEGORY: response = MockPostCategoryResponse(); break; //Requires WP REST API V2 MENUS case WPEnums.ContentTypes.MENU: response = MockMenuResponse(path); break; case WPEnums.ContentTypes.MENUCOLLECTION: response = MockMenuCollectionResponse(path); break; } return(response); }
/// <summary> /// Map properties from the Json response to models. Reusable function for all WP ContentTypes /// </summary> /// <param name="jsonObj"></param> /// <param name="contentType"></param> /// <param name="filterParams"></param> /// <returns></returns> public static object MapWordPressProperties(dynamic jsonObj, WPEnums.ContentTypes contentType, Dictionary <WPEnums.Filters, string> filterParams) { var jsonString = ReplaceNativeJsonProperties(jsonObj); if (jsonString != null) { switch (contentType) { //WP API V2 Plugin Required case WPEnums.ContentTypes.PAGE: return(MapWPPage(jsonString)); break; case WPEnums.ContentTypes.POST: return(MapWPPost(jsonString)); break; case WPEnums.ContentTypes.CATEGORY: return(MapWPCategory(jsonString)); break; case WPEnums.ContentTypes.POSTCATEGORY: return(MapWPPostCategory(jsonString, filterParams)); break; case WPEnums.ContentTypes.TAG: return(MapWPTags(jsonString)); break; ////WP API V2 MENU Plugin Required case WPEnums.ContentTypes.MENU: return(MapWPMenu(jsonString)); break; case WPEnums.ContentTypes.MENUCOLLECTION: return(MapWPMenuCollection(jsonString)); break; default: return(new WPPostPageModel()); break; } } return(new WPPostPageModel()); }
private string GetEndPointPath(WPEnums.ContentTypes contentType) { var rootSitePath = WPEndPoint; string apiPath = null; switch (contentType) { //WP API V2 Plugin Required case WPEnums.ContentTypes.PAGE: apiPath = @"/wp-json/wp/v2/pages"; break; case WPEnums.ContentTypes.POST: apiPath = @"/wp-json/wp/v2/posts"; break; case WPEnums.ContentTypes.POSTCATEGORY: apiPath = @"/wp-json/wp/v2/posts"; break; case WPEnums.ContentTypes.CATEGORY: apiPath = @"/wp-json/wp/v2/categories"; break; case WPEnums.ContentTypes.TAG: apiPath = @"/wp-json/wp/v2/tags"; break; case WPEnums.ContentTypes.AUTHOR: apiPath = @"/wp-json/wp/v2/author"; break; //WP API V2 MENU Plugin Required case WPEnums.ContentTypes.MENU: apiPath = @"/wp-json/wp-api-menus/v2/menus"; break; case WPEnums.ContentTypes.MENUCOLLECTION: apiPath = @"/wp-json/wp-api-menus/v2/menus"; break; case WPEnums.ContentTypes.MENULOCATION: apiPath = @"/wp-json/wp-api-menus/v2/menu-locations"; break; default: return(null); } return(rootSitePath + apiPath); }
/// <summary> /// Used to acquire and map various WordPress content items /// </summary> /// <param name="contentType"></param> /// <param name="filterParams"></param> /// <param name="genericGet"></param> /// <returns></returns> public object GetWPContent(WPEnums.ContentTypes contentType, Dictionary <WPEnums.Filters, string> filterParams) { //retrive the end point for calling WP var endPoint = GetEndPointPath(contentType); //append filters to the WP REST endpoing endPoint = BuildLinkWithFilters(contentType, filterParams, endPoint); //create the cachekey for caching the mapped response var cacheKey = BuildCacheKey(contentType, endPoint); //Call for WP content, Map the response, and Cache the mapped response var mappedWPContent = CacheAndMapWpContent(contentType, filterParams, endPoint, cacheKey); return(mappedWPContent); }
private object MapWPContent(WPEnums.ContentTypes contentType, Dictionary <WPEnums.Filters, string> filterParams, string wpContent) { object model = null; if (wpContent != null) { try { //Structured Class Model Mapper model = WPModelMapper.MapWordPressProperties(wpContent, contentType, filterParams); } catch (Exception ex) { //whatever you want to put here } } return(model); }
/// <summary> /// Sets the cache expiration value based on the given content type /// </summary> /// <param name="contentType"></param> /// <returns></returns> private DateTimeOffset SetCacheExpirationValue(WPEnums.ContentTypes contentType) { int cacheExpirationValue = 30; try { //Set the cache duration for whichever duration you want for any given content type cacheExpirationValue = Convert.ToInt32(ConfigurationManager.AppSettings["CacheExpireMins-" + contentType.ToString()]); if (cacheExpirationValue <= 0) { cacheExpirationValue = 30; } } catch (Exception ex) { //whatever you want to put for your error handling cacheExpirationValue = 30; } return(DateTimeOffset.UtcNow.AddMinutes(cacheExpirationValue)); }
private string CallForWpContent(string endPoint, WPEnums.ContentTypes contentType) { var post = new JSONRequest(); string response = null; try { if (MockRequest == "true") { response = post.MockWebCall(endPoint, contentType); } else { response = post.WebCall(endPoint); } } catch (Exception ex) { //whatever you want to do here } return(response); }
private object CacheAndMapWpContent(WPEnums.ContentTypes contentType, Dictionary <WPEnums.Filters, string> filterParams, string endPoint, string cacheKey) { //object declaration object mappedWPContent = null; //first, check memory cache for content if (!_memoryCache.Contains(cacheKey)) { //call for WP content var wpContent = CallForWpContent(endPoint, contentType); //due to native links, if content is assumed to be a PAGE and returns nothing, then make the call for as a POST if (string.IsNullOrEmpty(wpContent) || wpContent == "[]" && contentType == WPEnums.ContentTypes.PAGE) { contentType = WPEnums.ContentTypes.POST; //send the request back through the pipeline as a POST mappedWPContent = GetWPContent(contentType, filterParams); } else { //Map the returned content mappedWPContent = MapWPContent(contentType, filterParams, wpContent); } //grab the cache expiration for the specified content type var expiration = SetCacheExpirationValue(contentType); if (!wpContent.Contains("System.Net.WebException") && mappedWPContent != null) { //cache the mapped content model object _memoryCache.Add(cacheKey, mappedWPContent, expiration); } } return(_memoryCache.Get(cacheKey, null)); }
/// <summary> /// Used to safely obtain the contentViewModel title of the specified contentType. Be sure to note whether the "MENU" contentType is the TopNav or Footer menu. /// </summary> /// <param name="contentViewModel"></param> /// <param name="contentType"></param> /// <param name="footerMenu"></param> /// <returns string="title"></returns> public static string GetSafeTitle(this WPContentViewModel contentViewModel, WPEnums.ContentTypes contentType, bool footerMenu = false) { //declarations string title = null; try { if (contentViewModel != null) { //determine content type and return title switch (contentType) { case WPEnums.ContentTypes.PAGE: title = contentViewModel.PageModel.GetSafeTitle(); break; case WPEnums.ContentTypes.POST: title = contentViewModel.PostModel.GetSafeTitle(); break; case WPEnums.ContentTypes.POSTCATEGORY: if (contentViewModel.PostCategories != null) { title = contentViewModel.PostCategories.CategoryName; } break; case WPEnums.ContentTypes.MENU: if (footerMenu) { if (contentViewModel.FooterNavMenuModel != null) { title = contentViewModel.FooterNavMenuModel.name; } } else { if (contentViewModel.TopNavMenuModel != null) { title = contentViewModel.TopNavMenuModel.name; } } break; case WPEnums.ContentTypes.POSTMEDIA: if (contentViewModel.PostModel.ContainsFeaturedMedia()) { var featuredMedia = contentViewModel.PostModel.GetFeaturedMedia(); if (featuredMedia?.title != null) { title = featuredMedia.title.rendered; } } break; } } } catch (Exception ex) { title = ""; } return(title); }