The file holding the implementation of a custom action should look like the following one.

<?php
// No direct access
defined('_JEXEC') or die;

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

class ConditionalTextSenderAction extends ConditionalTextActionAware
{
    /**
     * @inheritDoc
     *
     * @see ConditionalTextAction
     */
    public function apply(Mail $mail)
    {
        // get sender e-mail address
        $senderMail = $this->options->get('sendermail');

        // get sender name
        $senderName = $this->options->get('sendername');

        if ($senderMail) {
            // overwrite the sender with the provided one
            $mail->setSender($senderMail, $senderName);
        }

        // get reply-to e-mail address
        $replyto = $this->options->get('replyto');

        if ($replyto) {
            // overwrite the reply-to address with the provided one
            $mail->setReplyTo($replyto);
        }
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getID()
    {
        return 'sender';
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getName()
    {
        return 'Sender';
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getDescription()
    {
        return 'Used to alter the default sender and reply-to e-mail addresses.';
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getIcon()
    {
        return 'fas fa-paper-plane';
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getSummary()
    {
        $info = [];

        if ($sender = $this->options->get('sendermail')) {
            $info[] = sprintf(
                'Sender: %s (%s)',
                $sender,
                $this->options->get('sendername') ?: '/'
            );
        }

        if ($replyto = $this->options->get('replyto')) {
            $info[] = sprintf(
                'Reply-to: %s',
                $replyto,
            );
        }

        return implode('; ', $info);
    }

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getForm()
    {
        return [
            'sendermail' => [
                'type'  => 'email',
                'label' => 'Sender e-mail',
                'value' => $this->options->get('sendermail'),
            ],
            'sendername' => [
                'type'  => 'text',
                'label' => 'Sender name',
                'value' => $this->options->get('sendername'),
            ],
            'replyto' => [
                'type'  => 'email',
                'label' => 'Reply-to e-mail',
                'value' => $this->options->get('replyto'),
            ],
        ];
    }
}

While this is the code you should use to support the newly created action, assuming that the previous code was added to the actions/sender.php file of your own WordPress plugin.

// subscribe to the hook used to set up the conditional texts framework of VikRestaurants
public function onSetupConditionalTexts($factory)
{
    // register a new provider to support the sender action
    $factory->registerActionProvider('sender', function($options) {
        // autoload the file ./actions/sender.php
        require_once dirname(__FILE__) . '/actions/sender.php';
        // instantiate the class
        return new ConditionalTextSenderAction($options);
    });
}

If you properly followed all the steps and after activating the plugin on your website, the management of the conditional text should look like the screenshot below.

Conditional Text - Action Management

Bear in mind that the summary is displayed only after saving the conditional text. If you don't see the summary after saving just the action, this is fine.

Last Update: 2023-12-29 14:15
Helpful?