Allows defining custom link tag

This commit is contained in:
Vlad
2017-07-08 11:56:51 -04:00
parent 2514d87a00
commit cfc5f5a88d
2 changed files with 11 additions and 3 deletions

View File

@@ -5,13 +5,14 @@ class Helpers {
const DIVIDER = '***MailPoet***'; const DIVIDER = '***MailPoet***';
const LINK_TAG = 'link'; const LINK_TAG = 'link';
static function replaceLinkTags($source, $link = false, $attributes = array()) { static function replaceLinkTags($source, $link = false, $attributes = array(), $link_tag = false) {
if(!$link) return $source; if(!$link) return $source;
$link_tag = ($link_tag) ? $link_tag : self::LINK_TAG;
$attributes = array_map(function($key) use ($attributes) { $attributes = array_map(function($key) use ($attributes) {
return sprintf('%s="%s"', $key, $attributes[$key]); return sprintf('%s="%s"', $key, $attributes[$key]);
}, array_keys($attributes)); }, array_keys($attributes));
$source = str_replace( $source = str_replace(
'[' . self::LINK_TAG . ']', '[' . $link_tag . ']',
sprintf( sprintf(
'<a %s href="%s">', '<a %s href="%s">',
join(' ', $attributes), join(' ', $attributes),
@@ -19,7 +20,7 @@ class Helpers {
), ),
$source $source
); );
$source = str_replace('[/' . self::LINK_TAG . ']', '</a>', $source); $source = str_replace('[/' . $link_tag . ']', '</a>', $source);
return preg_replace('/\s+/', ' ', $source); return preg_replace('/\s+/', ' ', $source);
} }

View File

@@ -20,4 +20,11 @@ class HelpersTest extends MailPoetTest {
expect(Helpers::replaceLinkTags($source, $link, $attributes)) expect(Helpers::replaceLinkTags($source, $link, $attributes))
->equals('<a class="test class" target="_blank" href="' . $link . '">example link</a>'); ->equals('<a class="test class" target="_blank" href="' . $link . '">example link</a>');
} }
function testItAcceptsCustomLinkTag() {
$source = '[custom_link_tag]example link[/custom_link_tag]';
$link = 'http://example.com';
expect(Helpers::replaceLinkTags($source, $link, array(), 'custom_link_tag'))
->equals('<a href="' . $link . '">example link</a>');
}
} }