English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  API  >  Start Framework

do_action_ref_array( 'vikrestaurants_start_api', API &$api )

Fires while starting the API framework.


Description

Trigger hook to let the plugins alter the application framework. It is possible to use this hook to include third-party applications.

In order to extend the list of supported plugins/applications, you need to attach your classes to the $api object, which can be done through the code below.

$api->registerEventProvider('test_plugin', function(string $event, array $options) {
    require_once dirname(__FILE__) . '/plugins/TestPluginEvent.php';
    return new TestPluginEvent($event, $options);
});

The registered events must inherit the E4J\VikRestaurants\API\Event class, otherwise they will be ignored.


Parameters

&$api

(API)  The framework API instance. This class is part of the E4J\VikRestaurants\API namespace.


Example

The example below explains how to support a custom API event.

/**
 * Trigger event to let the plugins alter the API framework.
 * It is possible to use this event to include third-party applications.
 *
 * @param  E4J\VikRestaurants\API\API  &$api
 *
 * @since  1.3
 */
add_action('vikrestaurants_start_api', function(&$api) {
    // register "test_plugin" API event
    $api->registerEventProvider('test_plugin', function(string $event, array $options) {
        // anonymous classes are supported too, but still need to inherit the Event class
        return new class ($event, $options) extends E4J\VikRestaurants\API\Event {
            /**
             * @inheritDoc
             */
            public function getTitle()
            {
                return __('Test Plugin', 'myplugin');
            }

            /**
             * @inheritDoc
             */
            public function getShortDescription()
            {
                return __('Testing the implementation of a custom event for the VikRestaurants API framework.', 'myplugin');
            }

            /**
             * @inheritDoc
             */
            protected function execute(array $args, Response $response)
            {
                /**
                 * @todo do stuff here
                 */
            }
        };
    });
});

Changelog

Version Description
1.3 Introduced.
Last Update: 2023-12-20
Helpful?
100% of people found this helpful.
See Also: