Click here to Skip to main content
15,867,939 members
Articles / Programming Languages / Razor

Extending WordPress with C# Plugins

Rate me:
Please Sign up or sign in to vote.
4.98/5 (21 votes)
30 May 2012Apache16 min read 168.4K   1.5K   57  
This article describes how to extend WordPress with plugins written in C# and shows very first C# plugin for this system.
/*
 * Content Watchdog Wordpress C# Plugin
 * 
 * Author: 
 *  Miloslav Beno (miloslav@devsense.com)
 * 
 * Copyright (c) 2012 DEVSENSE s.r.o
 */

using System;
using System.Globalization;
using System.Text;
using System.Dynamic;
using System.Reflection;

namespace Devsense.WordPress.Plugins.ContentWatchdog
{

    /// <summary>
    /// Provides a base implementation of a template.
    /// </summary>
    public class PhpRazorTemplateBase
    {
        public PhpRazorTemplateBase Layout { get; set; }

        protected PHP.Core.ScriptContext context = PHP.Core.ScriptContext.CurrentContext;

        public virtual void Execute()
        {
        }

        public void WriteLiteral(string textToAppend)
        {
            if (String.IsNullOrEmpty(textToAppend))
            {
                return;
            }
            PHP.Core.ScriptContext.Echo(textToAppend, context);
        }

        public void Write(object value)
        {
            if ((value == null))
            {
                return;
            }

            WriteLiteral(Convert.ToString(value, CultureInfo.InvariantCulture));
        }


    }


    /// <summary>
    /// Provides a base implementation of a template with a model.
    /// </summary>
    public class PhpRazorTemplateBase<T> : PhpRazorTemplateBase
    {
        #region Fields
        private object model;
        #endregion

        #region Constructor
        /// <summary>
        /// Initialises a new instance of <see cref="TemplateBase{T}"/>
        /// </summary>
        protected PhpRazorTemplateBase()
        {
            var type = GetType();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets whether this template has a dynamic model.
        /// </summary>
        protected bool HasDynamicModel { get; private set; }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        public virtual T Model
        {
            get { return (T)model; }
            set
            {
                if (HasDynamicModel)
                    model = new RazorDynamicObject { Model = value };
                else
                    model = value;
            }
        }
        #endregion

        #region Types
        /// <summary>
        /// Defines a dynamic object.
        /// </summary>
        class RazorDynamicObject : DynamicObject
        {
            #region Properties
            /// <summary>
            /// Gets or sets the model.
            /// </summary>
            public object Model { get; set; }
            #endregion

            #region Methods
            /// <summary>
            /// Gets the value of the specified member.
            /// </summary>
            /// <param name="binder">The current binder.</param>
            /// <param name="result">The member result.</param>
            /// <returns>True.</returns>
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                result = Model
                    .GetType()
                    .InvokeMember(
                        binder.Name,
                        BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                        null,
                        Model,
                        null);

                return (result != null);
            }
            #endregion
        }
        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer DEVSENSE s.r.o
Czech Republic Czech Republic
Miloslav is software developer of open-source PHP compiler & runtime for .NET/Mono called Phalanger and PHP Tools for Visual Studio. He's graduated at Faculty of Mathematics and Physics at Charles University in Prague. Beside of compilers and dynamic languages he is also interested in semantic web technologies. Available on twitter @miloslavbeno

Comments and Discussions