FireStudio – Core Concepts – Features and Services

At the core, FireStudio is a framework that makes it extremely easy to rapidly create new features and functionality for WordPress. It provides a simple Object Oriented Programming architecture which help you keep your code organized, clean, and easy to test. FireStudio also comes bundled with a number of debugging tools, making it possible to inspect and debug the environment you are developing in. Lastly, FireStudio comes bundled with a simplified interface for working with data and the database. Saving, updating, retrieving, and deleting data just got so much simpler.

There are two main concepts you need to be familiar with in FireStudio, Features and Services. A Feature, is a class that provides the custom functionality you intend to integrate into WordPress. A Service is a class that provides shared functionality to be used by features.

The main difference between features and services is that a feature implements the custom functionality (also known as business logic) you are developing. A service represents shared functionality that might be used between different services.

Other than features and services, there are some other, more advanced concepts, we will cover as well. The FireDi (The Injector), FireBug (The Debug Panel), and FireSql (The Data Interface).

Feature

Features are meant to contain the business logic (also known as custom functionality) you would like to implement within WordPress. Whether you are trying to add new admin pages, or you are trying to build out a new custom block for Gutenberg, you will implement these as a Feature within FireStudio. Creating a feature will start with the boilerplate code:

namespace My\Custom;

use \UA1Labs\Fire\Studio\Feature;

class Logic extends Feature
{
    public function __construct()
    {
    }
}

Once you have the boilerplate in place, you can have to load the feature with FireStudio:

function add_fire_studio_features(\UA1Labs\Fire\Studio $fireStudio)
{
    $fireStudio->loadFeature(\My\Custom\Logic::class);
}
add_action('firstudio_loaded', 'add_fire_studio_features');

Once a feature has been loaded into FireStudio, you can start implementing custom functionality.

In WordPress, all functionality is added through using Action/Filter Hooks. FireStudio has made it easier than ever to register Feature class methods to actions using the @action decorator. Using the boilerplate class you created above, we can register actions like so:

namespace My\Custom;

use \UA1Labs\Fire\Studio\Feature;

class Logic extends Feature
{
    public function __construct()
    {
    }

    /**
     * @action init
     * @priority 0
     */
    public function myCustomAction()
    {
        // action logic here
    }
}

Notice in the myCustomAction() method above, you will see a doc block that includes an @action and @priority decorator with a value. In the background, FireStudio will register the myCustomAction() method with the init hook at a priority of 0. It would be the equivalent of:

$myCustomLogic = new \My\Custom\Logic();
add_action('init', [$myCustomLogic, 'myCustomAction'], 0);

Which means at the point where WordPress executes the init action hook, our method myCustomAction() will be executed.

Service

Services are meant to contain shared logic that Features might need. An example of this might be something like needing to check to determine if the logged in user is an admin user. A service does not need any special boilerplate to create it. Also, it does not need to be registered with FireStudio like a Feature does.

Services are integrated into Features through the constructor method of the Feature class. Let’s say that we want to use the \UA1Labs\Fire\Studio\Service\WpHelperService that comes bundled with FireStudio, within the Feature we are building. All you need to do is add a type hint into the constructor method of your feature. Like so:

use \UA1Labs\Fire\Studio\Feature;

class MyAwesomeFeature extends Feature
{
    /**
     * @var \UA1Labs\Fire\Studio\Service\WpHelperService
     */
    private $wpHelper;

    public function __construct(WpHelperService $wpHelper)
    {
        $this->wpHelper = $wpHelper;
    }
}

When FireStudio loads this feature into the FireStudio run time environment, it will automatically find and inject the Service class you type hinted on the constructor.

Leave a comment

Leave a Reply