1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 13:16:45 +02:00

feat(shortcodes): add eval shortcode

This commit is contained in:
Awilum
2022-05-27 16:34:00 +03:00
parent 0d72580bbc
commit 5fe6c9a13e
4 changed files with 62 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
<?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 function registry;
use function expression;
// Shortcode: eval
// Usage: (eval:2+2)
// (eval)2+2(/eval)
// (eval)registry.get('flextype.manifest.version')(/eval)
parsers()->shortcodes()->addHandler('eval', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.eval.enabled')) {
return '';
}
if ($s->getContent() != null) {
return expression()->evaluate(parsers()->shortcodes()->parse($s->getContent()));
}
if ($s->getBbCode() != null) {
return expression()->evaluate(parsers()->shortcodes()->parse($s->getBBCode()));
}
});

View File

@@ -624,7 +624,10 @@ parsers:
calc:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php"
eval:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/EvalShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -608,6 +608,9 @@ parsers:
calc:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php"
eval:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/EvalShortcode.php"
# CORS
#

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->ensureExists(0755, true);
});
afterEach(function () {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('eval shortcode', function () {
expect(entries()->create('foo', ['price' => '(eval:2+2) (eval)2+2(/eval)']))->toBeTrue();
expect(entries()->fetch('foo')['price'])->toBe('4 4');
});