Skip to content

Commit

Permalink
Merge pull request #215 from totten/2.x-token-validation
Browse files Browse the repository at this point in the history
Accept Mosaico-style unsubscribe URLs during validation (#143)
  • Loading branch information
totten authored Nov 28, 2017
2 parents 2330891 + 3f3256b commit 48da157
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
47 changes: 47 additions & 0 deletions CRM/Mosaico/MosaicoRequiredTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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() {
$requiredTokens = Civi::service('civi_flexmailer_required_tokens')
->getRequiredTokens();

// Mosaico's templates handle the mailing-address/contact-info
// differently than the CiviMail templates -- one of the standard
// blocks provides a section where it prompts you to fill in this info.
//
// Arguably, this makes the `{domain.address}` requirement redundant.
// Arguably, the `{domain.address}` approach is better.
//
// For the moment, it's convenient to go along with the Mosaico-way.
// But it's quite patch-welcome to change (eg inject `{domain.address}`
// to the default layout, and then enforce the requirement).
unset($requiredTokens['domain.address']);

return $requiredTokens;
}

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

}
5 changes: 5 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,10 @@ public static function registerServices(ContainerBuilder $container) {
protected static function getListenerSpecs() {
$listenerSpecs = array();

if (class_exists('\Civi\FlexMailer\Validator')) {
// TODO Simplify by removing conditional. Wait until at least Feb 2018.
$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
18 changes: 18 additions & 0 deletions mosaico.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ function mosaico_civicrm_check(&$messages) {
\Psr\Log\LogLevel::CRITICAL
);
}
else {
$RECOMMENDED_FLEXMAILER = '0.2-alpha5';
$fmInfo = CRM_Extension_System::singleton()->getMapper()->keyToInfo('org.civicrm.flexmailer');
if (version_compare($fmInfo->version, $RECOMMENDED_FLEXMAILER, '<')) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_flexmailer_ver',
ts('The extension %1 expects %2 version <code>%3</code> or newer. Found version <code>%4</code>.', array(
1 => 'Mosaico',
2 => 'FlexMailer',
3 => $RECOMMENDED_FLEXMAILER,
4 => $fmInfo->version,
)),
ts('Outdated dependency'),
\Psr\Log\LogLevel::WARNING
);
}
}

if (!empty($mConfig['BASE_URL'])) {
// detect incorrect image upload url. (Note: Since v4.4.4, CRM_Utils_Check_Security has installed index.html placeholder.)
$handle = curl_init($mConfig['BASE_URL'] . '/index.html');
Expand Down

0 comments on commit 48da157

Please sign in to comment.