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