I wrote a small plugin to automatically encode emails in kirbytext and to provide a fieldMethod to apply the same filter to other fields, e.g. block-textfields.
This also works if a email gets wrapped in an A-Tag, because only the emails found are converted to encoded characters.
I imagine this is useful, in order that no rawtext email gets output on the website.
Maybe it’s also useful for someone here:
<?php
class Encode {
public static function email ($value) {
return Str::encode($value);
}
public static function emailsInText ($text) {
$text = preg_replace_callback('/[A-Z0-9\._%+-]+(@)[A-Z0-9\.-]+(\.)[a-z]{2,6}/i', function($match) {
return Encode::email($match[0]);
}, $text);
return $text;
}
}
Kirby::plugin('site/encode', [
'fieldMethods' => [
'encodeEmail' => function ($field) {
$field->value = Encode::email($field->value);
return $field;
},
'encodeEmails' => function ($field) {
$field->value = Encode::emailsInText($field->value);
return $field;
},
],
'hooks' => [
'kirbytext:after' => function ($text) { return Encode::emailsInText($text); }
],
]);