diff --git a/src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php b/src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php new file mode 100644 index 00000000..145b230e --- /dev/null +++ b/src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php @@ -0,0 +1,33 @@ +shortcodes()->addHandler('unless', static function (ShortcodeInterface $s) { + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.unless.enabled')) { + return ''; + } + + return expression()->evaluate(parsers()->shortcodes()->parse((($s->getBbCode() != null) ? $s->getBbCode() : ''))) === false ? parsers()->shortcodes()->parse($s->getContent()) : ''; +}); \ No newline at end of file diff --git a/src/flextype/core/Parsers/Shortcodes/WhenShortcode.php b/src/flextype/core/Parsers/Shortcodes/WhenShortcode.php new file mode 100644 index 00000000..d35219d8 --- /dev/null +++ b/src/flextype/core/Parsers/Shortcodes/WhenShortcode.php @@ -0,0 +1,33 @@ +shortcodes()->addHandler('when', static function (ShortcodeInterface $s) { + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.when.enabled')) { + return ''; + } + + return expression()->evaluate(parsers()->shortcodes()->parse((($s->getBbCode() != null) ? $s->getBbCode() : ''))) === true ? parsers()->shortcodes()->parse($s->getContent()) : ''; +}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index f895c819..585bea4f 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -661,6 +661,12 @@ parsers: if: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/IfShortcode.php" + when: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/WhenShortcode.php" + unless: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php" uuid: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/UuidShortcode.php" diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 9c92c2c2..fbbe6693 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -645,6 +645,12 @@ parsers: if: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/IfShortcode.php" + when: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/WhenShortcode.php" + unless: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php" uuid: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/UuidShortcode.php" diff --git a/tests/src/flextype/core/Parsers/Shortcodes/UnlessShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/UnlessShortcodeTest.php new file mode 100644 index 00000000..f7c3e677 --- /dev/null +++ b/tests/src/flextype/core/Parsers/Shortcodes/UnlessShortcodeTest.php @@ -0,0 +1,15 @@ +shortcodes()->parse("(unless:'2 > 1')yes(/unless)"))->toBe(""); + expect(parsers()->shortcodes()->parse("(unless:'2>1')yes(/unless)"))->toBe(""); + expect(parsers()->shortcodes()->parse("(unless:'2<1')yes(/unless)"))->toBe("yes"); +}); + +test('unless shortcode disabled', function () { + registry()->set('flextype.settings.parsers.shortcodes.shortcodes.unless.enabled', false); + expect(parsers()->shortcodes()->parse("(unless:'2 > 1')yes(/unless)"))->toBe(''); + registry()->set('flextype.settings.parsers.shortcodes.shortcodes.unless.enabled', true); + }); \ No newline at end of file diff --git a/tests/src/flextype/core/Parsers/Shortcodes/WhenShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/WhenShortcodeTest.php new file mode 100644 index 00000000..87739be8 --- /dev/null +++ b/tests/src/flextype/core/Parsers/Shortcodes/WhenShortcodeTest.php @@ -0,0 +1,15 @@ +shortcodes()->parse("(when:'2 > 1')yes(/when)"))->toBe("yes"); + expect(parsers()->shortcodes()->parse("(when:'2>1')yes(/when)"))->toBe("yes"); + expect(parsers()->shortcodes()->parse("(when:'2<1')yes(/when)"))->toBe(""); +}); + +test('when shortcode disabled', function () { + registry()->set('flextype.settings.parsers.shortcodes.shortcodes.when.enabled', false); + expect(parsers()->shortcodes()->parse("(when:'2 > 1')yes(/when)"))->toBe(''); + registry()->set('flextype.settings.parsers.shortcodes.shortcodes.when.enabled', true); + }); \ No newline at end of file