mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 22:26:46 +02:00
feat(shortcodes): simplify logic for tr
shortcode
This commit is contained in:
@@ -21,21 +21,25 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
use function parsers;
|
||||
use function registry;
|
||||
|
||||
// Shortcode: [tr]
|
||||
// Shortcode: tr
|
||||
// Usage: (tr:foo)
|
||||
parsers()->shortcodes()->addHandler('tr', static function (ShortcodeInterface $s) {
|
||||
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.i18n.enabled')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
|
||||
$varsDelimeter = ($s->getParameter('varsDelimeter') != null) ? parsers()->shortcodes()->parse($s->getParameter('varsDelimeter')) : ',';
|
||||
|
||||
if ($s->getParameter('find') != null) {
|
||||
if ($s->getBbCode() != null) {
|
||||
|
||||
// Get vars
|
||||
foreach($s->getParameters() as $key => $value) {
|
||||
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [];
|
||||
}
|
||||
|
||||
// Parse shortcodes for each var.
|
||||
$vars = array_map(fn($v) => parsers()->shortcodes()->parse(is_string($v) ? $v : ''), $vars);
|
||||
|
||||
return __($vars[0], collectionFromQueryString($vars[1] ?? '')->toArray(), $vars[2] ?? null);
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,6 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
test('[tr] shortcode', function () {
|
||||
expect(parsers()->shortcodes()->parse('(tr find:foo)'))->toBe('foo');
|
||||
test('tr shortcode', function () {
|
||||
expect(parsers()->shortcodes()->parse('(tr:foo)'))->toBe('foo');
|
||||
});
|
||||
|
Reference in New Issue
Block a user