wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

Changes to handle custom content types and properties #288

Closed hakakou closed 1 year ago

hakakou commented 2 years ago

1) Added a CustomFields dictionary on Base class. This will contain members not found when serializing/deserializing. This way you can read or write custom properties, (like those added from Pods plug-in) even in standard post types. Example:

post.CustomFields["myData"] = x
var y = post.CustomFields["myData"].Value<string>()

2) Made public some methods in CRUDOperation<> class, to make it easy to add custom content types. For example:

    public class Product : Page
    {
        public string code { get; set; }
        public string product_name { get; set; }
    }

    public class Products : CRUDOperation<Product, PagesQueryBuilder>
    {
        private const string _methodPath = "product";

        public ProductsCRUD(HttpHelper HttpHelper, string defaultPath) : base(HttpHelper, defaultPath, _methodPath)
        {
        }

        public Task<IEnumerable<ProductEx>> GetPagesBySearch(string searchTerm, bool embed = false, bool useAuth = false)
        {
            return HttpHelper.GetRequest<IEnumerable<ProductEx>>($"{DefaultPath}{_methodPath}?search={searchTerm}", embed, useAuth);
        }

        public Task<bool> Delete(int ID, bool force = false)
        {
            return HttpHelper.DeleteRequest($"{DefaultPath}{_methodPath}/{ID}?force={force.ToString().ToLower()}");
        }
    }

To use such a class:

   var client = new WordPressClient("https://xxx");
   var clientProducts = new Products(client.HttpHelper, client.DefaultPath);
   var products = await clientProducts.GetAll()