WordPressMVC

  1. Get Started
  2. Documentation
  3. Tutorials
  4. Blog
  1. Documentation
  2. Tutorials
v1.0.8 Login
  • Blog
  • Download
  • Get Started
  • The Basics
    • Ayuco
    • Main Class
    • Configuration
  • Hooks
    • Actions
    • Filters
    • Widgets
    • Shortcodes
  • MVC
    • Models
    • Post Models
    • Option Models
    • Term Models
    • User Models
    • Views
    • Controllers
  • Resources
    • Assets
    • Styles
    • Scripts
  • Advanced
    • Request
    • Response
    • Localization
    • Cache
    • Logger
    • Database
    • Testing
    • Deployment
    • Functions
    • Add-ons

Main Class

Last updated: February 12, 2021

The Main Class is the heart of the framework.

The Main Class is the bridge between Wordpress and the framework; WPMVC can not function without it. This class holds the definition of all the hooks and connectivity with Wordpress.

Main Class Location

In your project, Main class is located inside the app folder.

Adding Hooks

Most of WordPress’ hooks will be created using Ayuco (command line interpreter), which implements Scaffolding to facilitate development.

Hooks can be defined manually inside Main Class; hooks that affect Wordpress globally should be defined inside the init() method and those who only affect or are called during the admin dashboard should be defined inside the on_admin() method.

use WPMVC\Bridge;

class Main extends Bridge
{
    /**
     * Hooks that affect Wordpress globally are defined here. 
     */
    public function init()
    {
        $this->add_filter( 'the_content', 'PostController@the_content' );
    }
    
    /**
     * Hooks that only affect ADMIN-DASHBOARD are defined here. 
     */
    public function on_admin()
    {
        $this->add_action( 'save_post', 'PostController@save' );
    }
}

Hooks Declaration

Notice how hooks are defined by calling to Main Class methods (i.e. $this->add_action()) rather than Wordpress functions (i.e. add_action()); Main Class uses methods to enable MVC functionality.

The difference between using hooks with the Main Class rather than traditional Wordpress functions is that controllers and views are used as callbacks instead of functions, Main Class hooks support a priority and accepted arguments as parameters too.

These are the available methods for declaring hooks and functionality:

  • add_action: Used to add Wordpress action hooks.
  • add_filter: Used to add Wordpress filter hooks.
  • add_shortcode: Used to add Wordpress shortcodes.
  • add_widget: Used to register custom widgets into Wordpress.
  • add_model: Used to register automated models into Wordpress.
  • add_asset: Used to register assets (CSS or Javascript) into Wordpress.

Hook Callbacks

Defined hooks support controllers and views as callbacks.

Controllers

The syntax for controller callback consists of defining the controller's class name and its method, separated by the "@" symbol.

class Main extends Bridge
{
    public function on_admin()
    {
        $this->add_action( 'save_post', 'PostController@save' );
    }
}

In the example above, the method save() in controller "PostController" will be controlling the functionality for the hook "save_post".

Views

The syntax for view callback consists of defining the key "view" and a view, separated by the "@" symbol.

class Main extends Bridge
{
    public function init()
    {
        $this->add_shortcode( 'hello-world', 'view@shortcodes.hello-world' );
    }
}

In the example above, the view "shortcodes.hello-world" will display when the shortcode "hello-world" is used.

The hook and view parameters can be mapped by adding an array as 3rd parameter.

class Main extends Bridge
{
    public function init()
    {
        $this->add_action( 'woocommerce_thankyou', 'view@woocommerce.thankyou', ['product_id'] );
    }
}

In the example above, the view "woocommerce.thankyou" will display when the action "woocommerce_thankyou" is triggered, the mapping array is indicating that the view is expecting 1 parameter to be passed by and it should be mapped as view parameter $product_id.

Override Hook Parameters

Hook parameters can be overridden by defining alternative default values.

class Main extends Bridge
{
    public function init()
    {
        /**
         * Main class object '$this' is passed to the controller as
         * first method parameter.
         */
        $this->add_action( 'init', 'AppController@init', [$this] );
    }
}

In the example above, the Main Class object $this is being passed to the controller method init() as a method parameter. Multiple values can be added to the array of overridden parameters.

Global Variable

The framework defines a PHP global variable for the Main Class.

If the project is a theme, the variable will be named $theme.

If the project is a plugin, the variable will be named the same as the namespace defined but with all characters in lower case; this means that if the plugin's namespace was defined as "MyPlugin", the global variable would be $myplugin.

The global variable instance can be obtained using the get_bridge() global function.

// Current theme
$theme = get_bridge( 'theme' );
// By namespace
$myplugin = get_bridge( 'MyPlugin' );

Direct MVC Wildcards

Controller methods and views can be accessed directly from the Main Class by calling dynamic wildcard methods.

Wildcard pattern:

_{object}_{return}_{handler}

{object}Use c for controllers and v for views.
{return}Indicates if a return value is expected or not. Use void if no return is expected or return if otherwise.
{handler}View or controller handler.

Examples:

/**
 * In a "theme" project and using global variable $theme.
 * The following example will call to method "do_setup"
 * in controller "AppController".
 */
get_bridge( 'theme' )->{'_c_void_AppController@do_setup'}()


/**
 * In a "plugin" project and using global variable $myplugin.
 * The following example will call to method "get_config"
 * in controller "AppController" and set its returned value
 * to variable "$config".
 * 
 * Additianlly, "$param1" is sent as function parameter.
 */
$config = get_bridge( 'MyPlugin' )->{'_c_return_AppController@get_config'}($param1);

/**
 * In a "theme" project and using global variable $theme.
 * The following example will return view "hello-world" and
 * assign it to variable "$view".
 */
$view = get_bridge( 'theme' )->{'_v_return_view@hello-world'}();


/**
 * In a "plugin" project and using global variable $myplugin.
 * The following example will echo view "hello-world".
 */
get_bridge( 'MyPlugin' )->{'_v_void_view@hello-world'}();

Remove hooks

Action and filters hooks can be removed by calling the remove_action() and remove_filter() methods of a Main Class instance. These methods receive the 3 parameters (same as WordPress remove_action and remove_filter), with the priority parameter being optional.

Examples:

/**
 * Removes the handler "ConfigController@init" from the "init" action hook.
 */
get_bridge( 'MyPlugin' )->remove_action( 'init', 'ConfigController@init' );

/**
 * Removes the handler "ThemeController@body_class" from the "body_class" filter hook.
 */
get_bridge( 'theme' )->remove_filter( 'body_class', 'ThemeController@body_class' );

Use the global function get_bridge() to find an instantiated Main Class.

© 2023 10 Quality Studio . All rights reserved.
Search in: