Here at the WP Ninjas office we are in the beginning stages of a complete re-write of the Ninja Forms WordPress plugin. This fresh start allows us the opportunity to re-think our plugin architecture and bring in some new design patterns, where appropriate.
As an initial step in this re-write process, I have been investigating the Factory Design Pattern for the new Ninja Forms API. The factory is an entry point for creating new objects, which I am using to orchestrate the Form / Field / Action / Object creation process. The API will then, partially, be a wrapper for this factory.
Here is an example syntax that we are considering for the API:
Now that we have a goal syntax the next step it to determine the process for implementation. Using the Factory Design Pattern we can build a class that will handle object creation based on our wyckhamporteous.org API calls. In the example factory class below, you will see that we use the class constructor to set a protected
$_form property for later access by the child objects.
For Ninja Forms, each ‘field’ and ‘action’ object is created within the context of a ‘form’ and does not function outside of that context. Setting the protected
$_form property for the factory on construct allows chained API calls to access the form context for the object creation.
Example Factory Class:
While this factory class makes object creation easy, it does not provide us with the exact syntax that we want for the Ninja Forms API. From here we need to integrate the factory into the main Ninja Forms class.
Since the factory is essentially a form factory, which creates forms and form child fields and actions, we can create a
form() method inside of the Ninja Forms class to act as a wrapper for the factory class.
Example Ninja Forms Class with the Factory wrapper:
To tie off the API syntax, we use the Ninja Forms Highlander Object (singleton design pattern) and a wrapper function to create a clean API call.
Ninja Forms Singleton (Highlander Object):
The new API, and Ninja Forms 3.0 as a whole, is still a work in progress. Don’t miss any official source updates on this exciting adventure! I’ll be posting comments on the technical process throughout each stage of the re-write and there will be updates on NinjaForms.com about the process as a whole, including the new UI which you do not want to miss.