diff --git a/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php b/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php new file mode 100644 index 00000000..9cf129b7 --- /dev/null +++ b/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php @@ -0,0 +1,31 @@ +shortcodes()->addHandler('calc', static function (ShortcodeInterface $s) { + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.calc.enabled')) { + return ''; + } + + return (new StringCalc())->calculate(parsers()->shortcodes()->parse($s->getBBCode())); +}); diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 16fcf9e0..44da2489 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -631,7 +631,10 @@ parsers: field: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/FieldShortcode.php" - + calc: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php" + # CORS # # CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 60df05b2..6b1acb64 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -612,7 +612,10 @@ parsers: field: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/FieldShortcode.php" - + calc: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php" + # CORS # # CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. diff --git a/tests/src/flextype/core/Parsers/Shortcodes/CalcShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/CalcShortcodeTest.php new file mode 100644 index 00000000..ddb7d479 --- /dev/null +++ b/tests/src/flextype/core/Parsers/Shortcodes/CalcShortcodeTest.php @@ -0,0 +1,16 @@ +directory(PATH['project'] . '/entries')->ensureExists(0755, true); +}); + +afterEach(function () { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('calc shortcode', function () { + expect(entries()->create('foo', ['price' => '(calc:2+2)']))->toBeTrue(); + expect(entries()->fetch('foo')['price'])->toBe('4'); +}); \ No newline at end of file