Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Status Code  >  Caricare le Regole

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.
Ultimo aggiornamento: 2023-12-15
Utile?

Questo sito web utilizza i cookie

Questo sito web utilizza cookie e tecnologie simili. Alcune di queste tecnologie sono necessarie per il corretto funzionamento del sito web (essenziali). Altre tecnologie sono utilizzate per valutare il comportamento degli utenti (analisi), per integrare media esterni o per scopi pubblicitari. Se l'utente accetta, queste tecnologie vengono attivate. I dettagli sono riportati nella Privacy Policy. Da questa stessa pagina è inoltre possibile aggiornare/revocare il consenso. Se non si fornisce il consenso, verranno attivati solo i cookie con funzioni essenziali.

Sul nostro sito web utilizziamo tecnologie provenienti dagli Stati Uniti. Pertanto, se acconsentite, acconsentite anche al trasferimento dei vostri dati negli Stati Uniti. Desideriamo sottolineare che gli Stati Uniti non hanno un livello di protezione dei dati paragonabile a quello dell'Unione Europea e che qualsiasi azienda statunitense può essere obbligata dalle autorità statali a consegnare i vostri dati senza che voi abbiate alcuno strumento efficace di protezione legale contro questo.