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:
33
src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php
Normal file
33
src/flextype/core/Parsers/Shortcodes/UnlessShortcode.php
Normal 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()) : '';
|
||||
});
|
33
src/flextype/core/Parsers/Shortcodes/WhenShortcode.php
Normal file
33
src/flextype/core/Parsers/Shortcodes/WhenShortcode.php
Normal 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()) : '';
|
||||
});
|
@@ -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"
|
||||
|
6
tests/fixtures/settings/settings.yaml
vendored
6
tests/fixtures/settings/settings.yaml
vendored
@@ -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"
|
||||
|
@@ -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);
|
||||
});
|
@@ -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);
|
||||
});
|
Reference in New Issue
Block a user