English (United Kingdom)

do_action( 'vikrestaurants_load_reservation_codes_rules', array &$config )

Fires while loading all the supported rules of the reservation codes.


Description

This hook can be used to support custom rules for the reservation codes.

It is enough to include the directory containing the new rules. Only the files that inherits the E4J\VikRestaurants\ReservationCodes\CodeRule class will be taken.

In addition, the loaded classes must have a name built as [FILE_NAME] + CodeRule and should be part of the E4J\VikRestaurants\ReservationCodes\Rules namespace.

In case the class name is not correct or it doesn't extend the previously mentioned class, the file will be ignored.


Parameters

&$config

(array)  It is possible to inject here the configuration for a specific rule. The parameters have to be assigned by using the rule file name.


Example

The example below adds support for all the rules contained in a specific folder of a third-party plugin. Such as:

/wp-content/plugins/vikwp/rules/

All the PHP files contained within the rules folder of the VikWP plugin will be loaded.

/** 
 * This hook can be used to support custom reservation codes rules.
 * It is enough to include the directory containing the new rules
 * Only the files that inherits the ResCodesRule class will be taken.
 *
 * @param  array  &$config  It is possible to inject the configuration for
 *                          a specific rule. The parameters have to be assigned
 *                          by using the rule file name.
 */
add_action('vikrestaurants_load_reservation_codes_rules', function(&$config) {
    // fetch rules folder path
    $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rules';

    // include rules folder
    E4J\VikRestaurants\ReservationCodes\CodesHandler::addIncludePath($folder);

    // recover rule configuration from database
    $params = json_decode(get_option('myplugin_rule_test_config', '{}'));

    // register plugin configuration for being used
    // while trying to apply the rule
    $config['test'] = $params;
});

The class (shell) of the rule will be built as follows. The path of the file will be equal to this one.

/wp-content/plugins/vikwp/rules/test.php
namespace E4J\VikRestaurants\ReservationCodes\Rules;

class TestCodeRule extends \E4J\VikRestaurants\ReservationCodes\CodeRule
{
    /**
     * Returns a code readable name.
     *
     * @return  string
     */
    public function getName()
    {
        return 'VikWP - Test';
    }

    /**
     * Returns the description of the reservation code.
     *
     * @return  string
     */
    public function getDescription()
    {
        return 'Rule for test purposes';
    }

    /**
     * Checks whether the specified group is supported
     * by the rule. Children classes can override this
     * method to drop the support for a specific group.
     *
     * @param   string   $group  The group to check.
     *
     * @return  boolean  True if supported, false otherwise.
     */
    public function isSupported($group)
    {
        return true;
    }

    /**
     * Executes the rule.
     *
     * @param   mixed  $record  The record to dispatch.
     *
     * @return  void
     */
    public function execute($record)
    {
        /**
         * @todo apply the rule
         */
    }
}

Changelog

Version Description
1.3 The hook has been changed from a filter into an action as the plugin didn't need a return value anymore.
1.0 Introduced.
Last Update: 2023-12-15
Helpful?