Skip to content

Commit

Permalink
Accept Mosaico-style unsubscribe URLs (veda-consulting-company#143)
Browse files Browse the repository at this point in the history
Before
------
As observed in veda-consulting-company#143, the default `versafix-1` templates use tokens like
`[unsubscribe_link]` which do not match `{action.unsubscribeUrl}`. Thus,
when you try to submit the mailing, it fails because the token is missing.

After
-----
The `[unsubscribe_link]` is treated as an alias for
`{action.unsubscribeUrl}`. Thus, when you try to submit the mailing,
it accepts that token.

Comment
-------
There's another required token, `{domain.address}`. For the moment,
the patch waives that requirement because Mosaico UX handles that
differently. See comments inlined.
  • Loading branch information
totten committed Nov 28, 2017
1 parent 2330891 commit ed9ab3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CRM/Mosaico/MosaicoRequiredTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
use Civi\FlexMailer\Listener\RequiredTokens;

use CRM_Mosaico_ExtensionUtil as E;

/**
* Class CRM_Mosaico_MosaicoRequiredTokens
*
* The token format for Mosaico extends the traditional format -- all
* traditional tokens (eg "{action.unsubscribeUrl}") are supported, and
* a few additional aliases (eg "[unsubscribe_link]") are also
* supported.
*
* When validating required tokens, we should accept the aliases.
*/
class CRM_Mosaico_MosaicoRequiredTokens extends RequiredTokens {

public function __construct() {
parent::__construct(array('mosaico'), array());
}

public function getRequiredTokens() {
return Civi::service('civi_flexmailer_required_tokens')
->getRequiredTokens();
}

public function findMissingTokens($str) {
$content = array('body_unspecified' => $str);
_mosaico_civicrm_alterMailContent($content);
return parent::findMissingTokens($content['body_unspecified']);
}

}
2 changes: 2 additions & 0 deletions CRM/Mosaico/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function registerServices(ContainerBuilder $container) {
}
$container->setDefinition('mosaico_flexmail_composer', new Definition('CRM_Mosaico_MosaicoComposer'));
$container->setDefinition('mosaico_flexmail_url_filter', new Definition('CRM_Mosaico_UrlFilter'));
$container->setDefinition('mosaico_required_tokens', new Definition('CRM_Mosaico_MosaicoRequiredTokens'));

foreach (self::getListenerSpecs() as $listenerSpec) {
$container->findDefinition('dispatcher')->addMethodCall('addListenerService', $listenerSpec);
Expand All @@ -31,6 +32,7 @@ public static function registerServices(ContainerBuilder $container) {
protected static function getListenerSpecs() {
$listenerSpecs = array();

$listenerSpecs[] = array(\Civi\FlexMailer\Validator::EVENT_CHECK_SENDABLE, array('mosaico_required_tokens', 'onCheckSendable'), FM::WEIGHT_MAIN);
$listenerSpecs[] = array(FM::EVENT_COMPOSE, array('mosaico_flexmail_composer', 'onCompose'), FM::WEIGHT_MAIN);
$listenerSpecs[] = array(FM::EVENT_COMPOSE, array('mosaico_flexmail_url_filter', 'onCompose'), FM::WEIGHT_ALTER - 100);

Expand Down

0 comments on commit ed9ab3a

Please sign in to comment.