Italian (Italy)

It is possible to implement a new action for the conditional texts by creating an apposite PHP file, containing a new class that implements the methods defined by the E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextAction interface.

The class can be named as you prefer, there are no restrictions applied. It is good practice to always include Action as class suffix.

<?php
// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Mail\Mail;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextAction;

class CustomAction implements ConditionalTextAction
{
    /**
     * @inheritDoc
     */
    public function preflight(Mail $mail)
    {
        // do some stuff before applying the action
    }

    /**
     * @inheritDoc
     */
    public function apply(Mail $mail)
    {
        // apply the action
    }
}

The implementation of the interface as previously described let you interact with the action only via code. This because the selection of the actions from the management page of a conditional text takes only those actions that implement the E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextManageable interface.

The framework of VikRestaurants already provides a few abstractions to speed up this kind of implementation.

Here's how the extended class should look like.

<?php
// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Mail\Mail;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextActionAware;

class CustomAction extends ConditionalTextActionAware
{
    /**
     * @inheritDoc
     * 
     * @see ConditionalTextAction
     */
    public function preflight(Mail $mail)
    {
        // do some stuff before applying the action
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextAction
     */
    public function apply(Mail $mail)
    {
        // apply the action
    }

    /**
     * Returns the identifier of the conditional text action.
     * 
     * @return  string
     * 
     * @see ConditionalTextManageable
     */
    public function getID()
    {
        return 'custom';
    }

    /**
     * Returns a readable name for the conditional text action.
     * 
     * @return  string
     * 
     * @see ConditionalTextManageable
     */
    public function getName()
    {
        return __('Custom Action', 'myplugin');
    }

    /**
     * Returns an extended description for the conditional text action.
     * 
     * @return  string
     * 
     * @see ConditionalTextManageable
     */
    public function getDescription()
    {
        return __('This is how this action works', 'myplugin');
    }

    /**
     * Returns an icon (FontAwesome) for the conditional text action.
     * 
     * @see ConditionalTextManageable
     */
    public function getIcon()
    {
        return 'fas fa-plug';
    }

    /**
     * Renders a summary text according to the parameters of the conditional text action.
     * 
     * @return  string
     * 
     * @see ConditionalTextManageable
     */
    public function getSummary()
    {
        return __('Here you can summarize the parameters configured for this action.', 'myplugin');
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getForm()
    {
        return [];
    }
}
Further details about the implementable methods are specified from the related articles under this handbook.

Where should I place that file?

You can place that file wherever you want and name it as you wish. Just remember to automatically load it during the registration of the action provider, as described by the vikrestaurants_setup_conditional_texts hook.

Let's take the following plugin structure.

  • myplugin/
    • myplugin.php
    • actions/
      • custom_action_one.php
      • custom_action_two.php

According to the structure of these files, both the actions can be supported by using the following code, which should be placed within the main myplugin.php file.

// subscribe to the hook used to set up the conditional texts framework of VikRestaurants
add_action('vikrestaurants_setup_conditional_texts', function($factory) {
    // register a action provider to create the first action
    $factory->registerActionProvider('custom_action_one', function($options) {
        // autoload the file ./actions/custom_action_one.php, which must declare a class named as:
        // FirstCustomConditionalTextAction
        require_once dirname(__FILE__) . '/actions/custom_action_one.php';
        // instantiate the class
        return new FirstCustomConditionalTextAction($options);
    });

    // register a action provider to create the second action
    $factory->registerActionProvider('custom_action_two', function($options) {
        // autoload the file ./actions/custom_action_two.php, which must declare a class named as:
        // SecondCustomConditionalTextAction
        require_once dirname(__FILE__) . '/actions/custom_action_two.php';
        // instantiate the class
        return new SecondCustomConditionalTextAction($options);
    });
});
Ultimo aggiornamento: 2023-12-21
Utile?