Skip to content

Commit b38fefd

Browse files
authored
Add support for 'subject' argument in safe-email shortcode. (#113)
* Add support for 'subject' argument in safe-email shortcode. * safe-email: Add 'subject' argument to nextgen editor attributes. * Update README for safe-email 'subject' option.
1 parent 39159f1 commit b38fefd

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ Do not process the shortcodes between these raw shortcode tags
241241
242242
#### Safe-Email
243243
244-
Encode an email address so that it's not so easily 'scrapable' by nefarious scripts. This one has a couple of options: `autolink` toggle to turn the email into a link, and an `icon` option that lets you pick a font-awesome icon to prefix the email. Both settings are optional.
244+
Encode an email address so that it's not so easily 'scrapable' by nefarious scripts. This one has a couple of options: `autolink` toggle to turn the email into a link, an `icon` option that lets you pick a font-awesome icon to prefix the email, and a `subject` option that let's you specify the subject line for the user's mail agent to prefill. All settings are optional.
245245
246246
```
247-
Safe-Email Address: [safe-email autolink="true" icon="envelope-o"]user@domain.com[/safe-email]
247+
Safe-Email Address: [safe-email autolink="true" icon="envelope-o" subject="Feedback"]user@domain.com[/safe-email]
248248
```
249249
250250
#### Section

classes/shortcodes/SafeEmailShortcode.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@ public function init()
1414
}
1515

1616
// Get shortcode content and parameters
17-
$str = $sc->getContent();
17+
$addr_str = $sc->getContent();
1818
$icon = $sc->getParameter('icon', false);
1919
$icon_base = "fa fa-";
2020
$autolink = $sc->getParameter('autolink', false);
21+
$subject = $sc->getParameter('subject', false);
2122

22-
// Encode email
23-
$email = '';
24-
$str_len = strlen($str);
25-
for ($i = 0; $i < $str_len; $i++) {
26-
$email .= '&#' . ord($str[$i]). ';';
23+
// Add subject, if any, to the link target.
24+
$link_str = $addr_str;
25+
if ($subject) {
26+
$subject = html_entity_decode($subject);
27+
$link_str .= '?subject=' . rawurlencode($subject);
2728
}
2829

30+
// Encode display text and link target
31+
$email_disp = static::encodeText($addr_str);
32+
$email_link = static::encodeText($link_str);
33+
2934
// Handle autolinking
3035
if ($autolink) {
31-
$output = '<a href="mailto:' . $email . '">' . $email . '</a>';
36+
$output = '<a href="mailto:' . $email_link . '">' . $email_disp . '</a>';
3237
} else {
33-
$output = $email;
38+
$output = $email_disp;
3439
}
3540

3641
// Handle icon option
@@ -48,4 +53,21 @@ public function init()
4853
return $output;
4954
});
5055
}
51-
}
56+
57+
/**
58+
* encodes text as numeric HTML entities
59+
* @param string $text the text to encode
60+
* @return string the encoded text
61+
*/
62+
private static function encodeText($text)
63+
{
64+
$encoded = '';
65+
$str_len = strlen($text);
66+
67+
for ($i = 0; $i < $str_len; $i++) {
68+
$encoded .= '&#' . ord($text[$i]). ';';
69+
}
70+
71+
return $encoded;
72+
}
73+
}

nextgen-editor/shortcodes/safe-email/safe-email.js

100755100644
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ window.nextgenEditor.addShortcode('safe-email', {
88
icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 9.25a4.924 4.924 0 011.666.291.25.25 0 00.334-.236V1.75a.158.158 0 00-.1-.147.16.16 0 00-.173.034L12.2 10.164a2.407 2.407 0 01-3.4 0L.271 1.637A.159.159 0 000 1.75v10.5A1.749 1.749 0 001.75 14h12.043a.25.25 0 00.249-.226A4.992 4.992 0 0119 9.25z"/><path d="M9.726 9.236a1.094 1.094 0 001.547 0L19.748.761A.437.437 0 0019.5.019 1.62 1.62 0 0019.249 0h-17.5A1.62 1.62 0 001.5.019a.437.437 0 00-.352.3.441.441 0 00.102.442zM22.5 15.5v-1.25a3.5 3.5 0 00-7 0v1.25A1.5 1.5 0 0014 17v5.5a1.5 1.5 0 001.5 1.5h7a1.5 1.5 0 001.5-1.5V17a1.5 1.5 0 00-1.5-1.5zM19 12.75a1.5 1.5 0 011.5 1.5v1.25h-3v-1.25a1.5 1.5 0 011.5-1.5zm1 7.5a1 1 0 11-1-1 1 1 0 011 1z"/></svg>',
99
},
1010
attributes: {
11+
subject: {
12+
type: String,
13+
title: 'Subject',
14+
widget: 'input-text',
15+
default: '',
16+
},
1117
icon: {
1218
type: String,
1319
title: 'Icon',

0 commit comments

Comments
 (0)