Process

WordPress Fuel Workflow


The WordPress Fuel plugin workflow is implemented by several components. The workflow of WordPress Fuel plugin is simple.First the WordPress plugin is loaded then plugin class gets instantiated. Request is received by WordPress Fuel Front Controller which executes the request by calling the WordPress Fuel Router class that maps the router parameters (controller, action etc) into the request object. Once the routing parameters are set, WordPress Fuel then dispatches the request using Dispatcher to the action controller and runs the method in that controller. After the controller has finished, control returns to the front controller. Front controller returns the response and thus the process ends.

This is how the WordPress Fuel plugin execution takes place:

  1. WordPress core is loaded.
  2. WordPress Fuel plugin 'wp-content/plugins/yourplugin/index.php' is included.
  3. The plugin bootstrap file 'mvc/init.php' is included. Once we are in plugin's bootstrap file:
    • The WordPress Fuel core is loaded, if it wasn't loaded previously by any other plugin. This makes sure that the WordPress Fuel engine is initialized only once per HTTP request.
  4. Plugin class is instantiated
  5. URI is prepared by WordPress Fuel Core.
  6. WordPress Fuel Router takes the URI
  7. WordPress Fuel Router maps it into a request object.
  8. WordPress Fuel Request is dispatched to an appropriate controller set by Router. Dispatching process consists of instantiating the controller class and calling the action method in that controller.
  9. Controller executes the action and sets the clsHTTP_Response object.
  10. Control then returns to the Front controller.
  11. WordPress Fuel Front controller returns the HTTP Response.

WordPress Fuel Plugin - Skeleton Code

<?php
//wp-content/plugins/hello-world-mvc/index.php
/* 
Plugin Name: Hello World HMVC
Description: Sample Hello World Plugin to demonstrate WP Fuel features
Version: 1.0 
*/

//loads WP Fuel Engine
require_once "mvc/init.php";
        
class HelloWorld_Plugin extends absMVC_Plugin
{
    /**
    * Unique Slug for every plugin
    * Must be one word without any special characters
    * Important: It makes sure that the admin menu URI stays Unique
    */
    protected $_plugin_slug = 'helloworld';
    
    public function registerAdminMenu()
    {
        $parent = 'hello-world-mvc/init.php';
        $role = "administrator";
        add_menu_page('Hello World', 'Hello World',$role, $parent); 
            
        add_submenu_page($parent, 'Home Controller', 'Home Controller',$role, 
              $this->_getAdminPageSlug('home') , //automatically prepend slug
              array($this , 'handleAdminMenu')
        ); 
        
        add_submenu_page($parent, 'Item Controller', 'Item Controller',$role, 
              $this->_getAdminPageSlug('item') , //automatically prepend slug
              array($this , 'handleAdminMenu')
        ); 
    }
    
    /**
     * Handles Admin Request
     * @uses clsFrontController
     */
    public function handleAdminMenu()
    {
        $core_url = CoreHelper::assembleAdminMenuUri();
        
        //Example Admin URI: /wp-admin/admin.php?page=helloworld_home
        //$core_url Output: helloworld_home
        
        //Example Admin URI: /wp-admin/admin.php?page=helloworld_item
        //$core_url Output: helloworld_item
        //where; helloworld is plugin slug and item is controller
        
        $this->getFrontController()->setCoreUri($core_url)
            ->execute()
            ->getOutput();  
    }
}
new HelloWorld_Plugin(__FILE__);
?>

Below is the method from WordPress Front Controller class which uses several components to execute the request:

public function execute()
{
    $this->_request = $this->getRequest();

    //class the router
    $router = $this->getRouter($this->_request);

    //Router maps the conroller, action and other parameters
    $router->map();

    //Dispatcher dispatches the request
    $this->getDispatcher()->setParams($this->getParams())
            ->dispatch($this->_request);

    return $this; //returns FrontController object
}