Continuing from Part 1, let’s layout our configuration store’s module, api, and internals. We are going to design this module in procedural, although you could design it in class wrapper (static functions within a class structure) or OOP. We’ll use a static variable to be our container. Resources Static Variables in PHP Static Variables – PHP Internals PHP Manual – Static Variable
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Loading the Configuration into the Store
Let’s test and figure out how to load a configuration file from the filesystem and get it into our configuration store. We’ll do this together step-by-step, systematically discovering how to load, how to extract the store key and configuration parameters, and thinking through our store. Resources In this episode, you’ll use: current() key() list()
Fix the Config Model
We have a structural error in our configuration models. Oh bummer. Did you notice it? In this episode, let’s fix all 3 files before we move onto the Configuration Store.
The Implementation Configurations
We’ve completed our meta box configuration model. Now, let’s take that model and load up our 2 custom implementations. You’ll want to store each configuration file into the config folder. If the plugin was doing more than a meta box, then you’d put the configs into config/meta-box folder. Resources intval sanitize_text_field
Configuration Model – Render Parameters
In the last episode, I challenged you to complete the render parameters yourself. In this episode, we’ll walk through how I solved it. We’ll fill out the custom fields in our Configuration Model. Plus, we’ll refactor our render function for our new generic, configuration business logic.
Laying Out the Architecture
Let’s layout the basic architecture of the configs, config store, and metadata. Work along with me in the video to get your plugin architecture setup and ready for the remaining parts of this lab.
Separating Implementation from Business Logic
As you are developing your Reusable Mindset, I want you to think about implementation and business logic. More specifically, think about how to separate a specific implementation from the business logic. This design pattern makes your code reusable. For example, in our project, we have 2 custom meta boxes. Yet, both are using the same code. Imagine pulling the implementation out of the code and abstracting it into a configuration file. Now imagine that our business logic is rebuilt to accept a configuration file and then it processes it. Why? In doing so, our business logic is built once. It […]
Improving the Render
Let’s improve the render function, as we have too much going on in that function. We’ll abstract away the task of fetching the metadata from the database for our custom fields’ values. Why? Our render function’s job is to render the view. It needs the values, but does not need to know how to do it. Rather, just gimme the values for the custom fields. We’ll also add a WordPress filter event to extend our meta data, allowing an external process to filter the values, do some processing, or just run before we render out to the browser.
Rethink the Save Functionality
Our current save function is not reusable. As we’ve done with the register and render functions, we need to remove the implementation and allow the saving process to accept a configuration and then process it. Let’s rethink our save function, split up the different parts, and prepare for how to refactor it.
Merging Configuration with the Defaults
If we run our code as it is right now, the meta boxes did not appear. Why? Because we only configured what was different in the Portfolio and Subtitle configuration models. We need to merge each of those with the default configuration parameters. Let’s do that in this episode. You’ll be using the PHP function array_replace_recursive to merge all levels within our multidimensional arrays. When you get done with this episode, both custom meta boxes will now render for a post. WooHoo!! Pat yourself on the back, as you did a lot of work to get to this point. Keep […]