English (United Kingdom)

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

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

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', 'myplugin');
    }

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

    /**
     * @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)', 'myplugin'),
                $sender,
                $this->options->get('sendername') ?: '/'
            );
        }

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

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

    /**
     * @inheritDoc
     * 
     * @see ConditionalTextManageable
     */
    public function getForm()
    {
        return [
            'sendermail' => [
                'type'  => 'email',
                'label' => __('Sender e-mail', 'myplugin'),
                'value' => $this->options->get('sendermail'),
            ],
            'sendername' => [
                'type'  => 'text',
                'label' => __('Sender name', 'myplugin'),
                'value' => $this->options->get('sendername'),
            ],
            'replyto' => [
                'type'  => 'email',
                'label' => __('Reply-to e-mail', 'myplugin'),
                '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
add_action('vikrestaurants_setup_conditional_texts', function($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-21
Helpful?