responsiv / campaign-plugin

[PREMIUM] Send professional campaign messages to your subscribers.
http://octobercms.com/plugin/responsiv-campaign
5 stars 2 forks source link

[feature request] Make tags extendable (with solution) #44

Closed munxar closed 2 years ago

munxar commented 6 years ago

I want to extend the available tags in the message editor. With three little events it was possible to totally customize the tags. All modifications are done in Responsiv\Campaign\Models\Message

    public static function getAvailableTags()
    {
        $tags = [
            'first_name'      => "Subscriber's first name",
            'last_name'       => "Subscriber's last name",
            'email'           => "Subscriber's email address",
            'unsubscribe_url' => "Link to unsubscribe from emails",
            'browser_url'     => "Link to open web-based version"
        ];

        // extend tags that are visible in the message editor 
        Event::fire('responsiv.campaign.extendAvailableTags', [&$tags]);

        return $tags;
    }
    public function buildTagData($subscriber)
    {
        $data = [];

        $data['first_name'] = $subscriber->first_name;
        $data['last_name'] = $subscriber->last_name;
        $data['email'] = $subscriber->email;
        $data['unsubscribe_url'] = $this->getBrowserUrl($subscriber).'?unsubscribe=1';
        $data['browser_url'] = $this->getBrowserUrl($subscriber);
        $data['tracking_pixel'] = $this->getTrackingPixelImage($subscriber);
        $data['tracking_url'] = $this->getBrowserUrl($subscriber).'.png';

        // extend tag data send with the email
        Event::fire('responsiv.campaign.extendTagData', [&$data, $subscriber]);

        return $data;
    }
    public function renderForPreview()
    {
        $content = $this->content_html ?: $this->renderTemplate();
        $parser = new TextParser;

        $data = [];
        $data['first_name'] = 'Test';
        $data['last_name'] = 'Person';
        $data['email'] = 'test@email.tld';
        $data['unsubscribe_url'] = 'javascript:;';
        $data['browser_url'] = 'javascript:;';
        $data['tracking_pixel'] = '';
        $data['tracking_url'] = 'javascript:;';

        // extend preview data
        Event::fire('responsiv.campaign.extendPreviewData', [&$data]);

        $result = $parser->parseString($content, $data);

        // Inject base target
        $result = str_replace(
            '</head>',
            '<base target="_blank" />' . PHP_EOL . '</head>',
            $result
        );

        return $result;
    }

Now my plugins boot method can do things like that (assume that the subscriber model was extended with a salutation attribute):

    public function boot()
    {
        Event::listen('responsiv.campaign.extendAvailableTags', function(&$tags) {   
            // add a new attribute to the sidebar
            $tags['salutation'] = 'Salutation';
        });

        Event::listen('responsiv.campaign.extendTagData', function(&$data, $subscriber) {
            $data['salutation'] = $subscriber->salutation;
        });

        Event::listen('responsiv.campaign.extendPreviewData', function(&$data) {
            $data['salutation'] = 'Hello';
        });
   }

It would be helpful if such an extension would be added to the plugin.

damsfx commented 6 years ago

+1 Very useful to add a civility field Mr, Ms or Mrs for more personalized emails.

grahambrown11 commented 4 years ago

@daftspunk any chance this can be added? I was going to start applying a similar patch and fortunately came across @munxar post here and he's even given a solution

damsfx commented 4 years ago

My preference goes to the "register" method...
One place to rule them all.

I think it's more efficient than having to watch 3 different events

daftspunk commented 2 years ago

Removed all the off-topic conversations. This feature has been implemented in v1.3.1, see the updated readme for details.

Thanks!