1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 13:46:42 +02:00

feat(shortcodes): add when and unless shortcodes

This commit is contained in:
Awilum
2022-05-31 21:38:14 +03:00
parent 9f676ebd43
commit fbdcc8af3c
6 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Parsers\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use function parsers;
use function registry;
// Shortcode: unless
// Usage: (unless:'(var:score) < (var:level1)') Show something... (/when)
parsers()->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()) : '';
});

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Parsers\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use function parsers;
use function registry;
// Shortcode: when
// Usage: (when:'(var:score) < (var:level1)') Show something... (/when)
parsers()->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()) : '';
});

View File

@@ -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"

View File

@@ -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"

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
test('unless shortcode', function () {
expect(parsers()->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);
});

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
test('when shortcode', function () {
expect(parsers()->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);
});