Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Extending WordPress with C# Plugins

, 30 May 2012
This article describes how to extend WordPress with plugins written in C# and shows very first C# plugin for this system.
ContentWatchdog_src.zip
ContentWatchdog
ContentWatchDog(MVC3).suo
ContentWatchdog.csproj.vspscc
ContentWatchDog.suo
ContentWatchdog.vssscc
Properties
/*
 * 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

About the Author

Miloslav Beno
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
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 30 May 2012
Article Copyright 2012 by Miloslav Beno
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid