HMVC framework for WordPress plugin development Download

WP Fuel API

WordPress Fuel Plugin API


  • License
    WordPress Fuel provides classes that makes it easy to work with WordPress plugin API and in an OOP way. Below is the WordPress Fuel abstract plugin class which has various method for different cases.



absMVC_Plugin Abstract class

Every WordPress Fuel plugin should inherit absMVC_Plugin class. Following is the abstract class and methods within the class.

abstract class absMVC_Plugin
{
    /**
    * Must be a word without any special characters
    * Unique Slug
    * @var (string)
    */
    protected $_plugin_slug = '';

    public function __construct($plugin_file, $mvc_folder = null){..}

    /**
    * Method register admin assets
    * JS, CSS assets etc
    */
    public function admin_assets(){}
    
   /**
    * Method register front assets
    * 'wp_enqueue_scripts' action invokes this method 
    * As name suggets, It is available in front-end only.
    * JS, CSS assets etc
    */
    public function front_assets(){}

    /** 
    * Global method to execute any code to initialize the plugin functionality.
    * Abstract method - This method must be implemented in a plugin class
    * @abstract
    */
    protected abstract function _init();

    /**
    * Method to execute global intilialize code.
    * Place any AJAX initialization code here
    */
    protected function _initAjax(){}
    
    /**
    * Initialize any back-end related code for this plugin
    */
    public function initAdmin(){}
    

    /**
    * Registers admin 
    */
    public function registerAdminMenu(){}
    
    /**
    * Initialize any front-end related code for this plugin
    * Example: Register shortcodes etc
    */
    public function initFront(){}
   
    /**
    * Place your plugin's activation logic here
    */
    protected function _activate(){}
    
    /**
    * Place your plugin's deactivation logic here
    */
    protected function _deactivate(){}
    
    /**
     * Method to add rewrite rules
     */
    protected function _init_rewrite_rules(){}

    /**
     * Registers a rewrite rule
     * @param type $regex
     * @param type $pagename
     * @return absMVC_Plugin 
     */
    protected function register_rewrite_rule($regex,$pagename)[]

    /**
     * Generic implementation to WordPress rewrite requests
     * Handles rewrite request
     * @uses clsFrontController
     */
    public function handleRewriteRequest(){..}
    
/**
     * Load config file as array from plugin folder directory structure.
     * @param (string) $config_file_name
     * @return (array) 
     */
    function LoadConfig($config_file_name){..}

    /**
     * Disptaches the requests and return the response from dispatcher (absDispatcher)
     * Helper method to dispatch request
     * @param string $core_url
     * @param array $invokeParams
     * @return clsHTTP_Response
     */
    function dispatchRequest($core_url,array $invokeParams = array()){..}
   
    
    /**
     * Wrapper method to add actions using WordPress. 
     * Returns 'absMVC_Plugin' for method chaining.
     * 
     * @param type $action
     * @param type $function
     * @param type $priority
     * @param type $accepted_args
     * @return absMVC_Plugin 
     */
    protected function add_action( $action, $function = '', $priority = 10, $accepted_args = 1 ){..}
    
     /**
     * Wrapper method to add actions using WordPress. 
     * Returns 'absMVC_Plugin' for method chaining.
     *
     * @param type $action
     * @param type $function
     * @param type $priority
     * @param type $accepted_args
     * @return absMVC_Plugin 
     */
    protected function add_ajax_action( $action, $function = '', $priority = 10, $accepted_args = 1 ){..}

    /**
     * Wrapper method to add filters using WordPress. 
     * Returns 'absMVC_Plugin' for method chaining.
     *
     * @param type $filter
     * @param type $function
     * @param type $priority
     * @param type $accepted_args
     * @return absMVC_Plugin 
     */
    protected function add_filter( $filter, $function, $priority = 10, $accepted_args = 1 ){..}
    
    /**
     * Wrapper method to add short code using WordPress. 
     * Instantiate absMVC_Shortcode class and registers short code
     * Returns 'absMVC_Plugin' for method chaining.
     *
     * @param type $shortcode
     * @param type $function
     * @return absMVC_Plugin 
     */
    protected function add_shortcode($shortcode,  absMVC_Shortcode $shortCodeHandlerClass , $method){..}
    

    /**
     * Prepares full wp-admin link keeping the base link intact.
     * 
     * @param string $core_url
     * @param array $qsData
     * @param type $isSecure
     * @return (string)
     */
    function getAdminUrl($core_url,array $qsData = array(), $isSecure = false){..}
    
    /**
     * Get the collection of shortcodes for this plugin
     * @return (array) 
     */  
    public function getShortCodes()
    {
        $this->_short_codes;
    }

    /**
     * Plugin Slug
     * @return (string) 
     */
    function getSlug()
    {
        return $this->_plugin_slug;
    }
    
    /**
     * Plugin MVC Directiry Path
     * @return (string) 
     */
    function getMVCPath()
    {
        return $this->_mvc_path;
    }

    /**
     * Plugin Directory
     * @return (string) 
     */
    function getPluginPath()
    {
        return $this->_plugin_path;
    }
    
    /**
     * Plugin URL
     * @return (string) 
     */
    function getPluginUrl()
    {
        return $this->_plugin_url;
    }
    
    /**
     * Plugin File
     * @return (string) 
     */
    function getPluginFile()
    {
        return $this->_plugin_file;
    }
    
    /**
     * Prepares Admin Menu Slug. 
     * Plugin slug will be automatically prepended
     * @param type $slug
     * @return string 
     * @outputs: [plugin_slug]_$slug
     */
    protected function _getAdminPageSlug($page_slug){..}

     /**
     * Generic implementation to execute admin MVC workflow.
     * Handles Admin Request
     * @uses clsFrontController
     */
    public function handleAdminMenu(){..}

     /**
     * Generic implementation to execute AJAX requests.
     * Handles AJAX Request
     * @uses clsFrontController
     */
    public function handleAjaxRequest(){..}

     /**
     * Generic implementation to WordPress rewrite requests
     * Handles rewrite request
     * @uses clsFrontController
     */
    public function handleRewriteRequest(){..}
}

Important Methods & Examples


protected abstract function _init()

As you can see this is an abstract method and must be implemented.

Example
protected function _init()
{
    //configure the core plugin functionality here
}


function admin_assets()

Here you can register back-end (wp-admin) assets. Available in the admin only.

Example
function admin_assets()
{
    wp_enqueue_style('plugin-css-admin', plugins_url( '/assets/css/admin.css', __FILE__ ),false,'1.0','all');
}


function front_assets()

Here you can register front-end assets. This method will only be executed in the front-end.

Example
function front_assets()
{
    wp_enqueue_script('plugin-js', plugins_url( '/assets/js/plugin.js', __FILE__ ),false,'1.0','all');
    wp_enqueue_script('jquery-min', plugins_url( '/assets/js/jquery.min.js', __FILE__ ),false,'1.8','all');
    wp_enqueue_style('social-events', plugins_url( '/assets/css/plugin.css', __FILE__ ),false,'1.0','all');
}


protected function _getAdminPageSlug($page_slug)

Prepares Admin menu Slug. Plugin slug will be automatically suffixed.

Example

Assume that the plugin slug (protected $_plugin_slug) is 'helloworldmvc'

$custom_page_link = $this->_getAdminPageSlug('custom_page'); /*returns helloworldmvc_custom_page */
$plugin_settings_link = $this->_getAdminPageSlug('settings'); /*return helloworldmvc_settings */

add_submenu_page(__FILE__, 'Settings', 'Settings', 'administrator', 
        $plugin_settings_link, array($this , 'handleAdminMenu')
); 


function registerAdminMenu()

If you want back-end menu for you plugin's custom functionality. Use this plugin to register admin menus.

Example
function registerAdminMenu()
{
    add_menu_page('My WP Fuel Plugin','My WP Fuel Plugin' ,'administrator', __FILE__); 

    add_submenu_page(__FILE__, 'Custom Page', 'Custom Page', 'administrator', 
        $this->_getAdminPageSlug('custom_page'), array($this , 'handleAdminMenu')
    ); 
}
  • registerAdminMenu() method is used to generate admin menu for the plugin. 'handleAdminMenu' is passed as a callback method to process the menu request. 'handleAdminMenu' is a generic method to process all the admin menu requests for a WP Fuel plugin. It uses 'page' query parameter to dispatch the request to particular admin controller.


function LoadConfig($config_file_name)

Load a particular config file from the plugin config folder.

Example
function initAdmin()
{
    /*
    * You only need to input file token and suffix config. and postfix .php will be formatted automatically 
    * loads wp-content/plugins/yourplugin/mvc/config/config.admin.php
    */
   $admin_config = $this->LoadConfig('admin'); 
   
   //do stuff
}


function dispatchRequest($core_url,array $invokeParams = array())

Disptaches the requests and return the response from the dispatcher (absDispatcher)

Example
$item = $this->dispatchRequest('portfolio/get_item',array('item_id' => 1));

Controller Example
class pluginslug_home_controller extends absController
{
    function index()
    {
        /* Get the list of recent items */
        $recent_items = $this->_getCurrentPlugin()->dispatchRequest('portfolio/get_recent_items', array('category_id' => 1, 'limit' => 10));
        $this->ViewData('recent_items',$recent_items) //push recent items to the view
            ->View('home/index');
    }
}


View Example
//views/item/details
<div id="div_like_button">
    echo $plugin->dispatchRequest('portfolio/get_stats', array('item_id' => 1));
</div>
  • $plugin is the absMVC_Plugin object available globally in every view file.
  • Usage: This is direct rendering of item statistics on the item details page. If you wish, you can also make an AJAX call to reduce page load time.



function getAdminUrl($core_url,array $qsData = array(), $isSecure = false)

Prepares full wp-admin link keeping the base link intact.

Example
$item = $this->dispatchRequest('portfolio/get_item',array('item_id' => 1));

Manage Contacts - Example
//views/admin/contacts/list
<?PHP if(count($contacts)): ?>
<table class="widefat">
    <thead>
        <tr>
            <th><?PHP echo _e('Name'); ?></th>   
            <th><?PHP echo _e('Email'); ?></th>   
            <th><?PHP echo _e('Action'); ?></th>   
        </tr>
    </thead>
    <tbody>
        <?PHP foreach($contacts as $k => $record): ?>
       <tr>
         <td><?PHP echo $record['name']; ?></td>
         <td><?PHP echo $record['email']; ?></td>
         <td><a href="<?PHP echo $plugin->getAdminUrl('admin_contacts/edit',array('contact_id' => $record['contact_id'])); ?>"><?PHP echo _e('Edit'); ?></a> 
             | <a class="delete-action" href="<?PHP echo $plugin->getAdminUrl('admin_contacts/delete',array('contact_id' => $record['contact_id'])); ?>"><?PHP echo _e('Delete'); ?></a></td>
       </tr>
       <?PHP endforeach; ?>
    </tbody>
</table>
<?PHP else: ?>
    <p><?PHP echo _e('No contacts found'); ?></p>
<?PHP endif; ?>

  • Line 16: $plugin->getAdminUrl('admin_contacts/edit',array('contact_id' => $record['contact_id']));
  • Prepares admin URL to edit the record.
  • Returns: http://yoursite.com/wp-admin/admin.php?page=helloworldmvc_admin_contacts&method=edit&contact_id=[NUMBER]

Standalone Examples

/*Returns http://yoursite.com/wp-admin/admin.php?page=helloworldmvc_admin_contacts&method=view&contact_id=1*/
$url = $plugin->getAdminUrl('admin_contacts/edit',array('contact_id' => 1));
//URL to edit a particular contact. 'action_edit' will be automatically called.

/*Returns http://yoursite.com/wp-admin/admin.php?page=helloworldmvc_admin_contacts&method=view&slug=skill&&contact_id=1&skill_id=2*/
$url = $plugin->getAdminUrl('admin_contacts/edit/skill',array('contact_id' => 1, 'skill_id' => 2));
//Ability to display skill edit form. Use Slug as an interceptive action within a controller.

Controller Example
class helloworldmvc_admin_contacts_controller extends absController
{
    function before()
    {
        $slug = $this->_request->getParam('slug');
        $action = 'action_'.$slug;
        if(method_exists($this, $action))
        {
            $this->setInterceptiveAction('action_'.$slug);
        }
    }

    function action_edit()
    {
       $contact_id = (int)$this->_request->getParam('contact_id');

       $oContact = new modContact();
       $skill_data = $oContact->Data($contact_id);
       $this->ViewData('skill_row',$skill_data)
            ->View('admin/contacts/edit_form');
    }

    function action_skill()
    {
        
       $skill_id = (int)$this->_request->getParam('skill_id');

       $oSkills = new modContact_Skills();
       $skill_data = $oSkills->Data($skill_id);
       $this->ViewData('skill_row',$skill_data)
            ->View('admin/contacts/skill_form');
    }

}
    If interceptive action is set within a particular controller it will be executed. As you can see we first verified whether method exists and then set the interceptive action.