From 4e5d9c167776fee99b8149ca59339233227743ec Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 7 Aug 2021 11:14:01 +0300 Subject: [PATCH] feat(shortcodes): update logic for setting `enabled`, affected all shortcodes #564 --- src/flextype/Parsers/Shortcodes.php | 7 +++++-- .../Parsers/Shortcodes/ContentShortcode.php | 14 +++++++++----- .../Parsers/Shortcodes/MediaShortcode.php | 14 +++++++++----- .../Parsers/Shortcodes/RawShortcode.php | 11 +++++------ .../Parsers/Shortcodes/RegistryShortcode.php | 13 ++++++++----- .../Parsers/Shortcodes/UrlShortcode.php | 19 +++++++++++-------- 6 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/flextype/Parsers/Shortcodes.php b/src/flextype/Parsers/Shortcodes.php index 05158fa1..6a42150b 100644 --- a/src/flextype/Parsers/Shortcodes.php +++ b/src/flextype/Parsers/Shortcodes.php @@ -87,12 +87,15 @@ final class Shortcodes } foreach ($shortcodes as $shortcode) { - if (! isset($shortcode['path'])) { + if (! isset($shortcode['enabled'])) { + continue; + } + + if (! $shortcode['enabled']) { continue; } if (! file_exists(ROOT_DIR . $shortcode['path'])) { - continue; } diff --git a/src/flextype/Parsers/Shortcodes/ContentShortcode.php b/src/flextype/Parsers/Shortcodes/ContentShortcode.php index 43dc724f..5c1fa571 100644 --- a/src/flextype/Parsers/Shortcodes/ContentShortcode.php +++ b/src/flextype/Parsers/Shortcodes/ContentShortcode.php @@ -12,8 +12,12 @@ namespace Flextype\Parsers\Shortcodes; use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [content_fetch id="content-id" field="field-name" default="default-value"] -if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.content.enabled')) { - parsers()->shortcodes()->addHandler('content_fetch', static function (ShortcodeInterface $s) { - return arrays(content()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); - }); -} +parsers()->shortcodes()->addHandler('content_fetch', static function (ShortcodeInterface $s) { + + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.content.enabled')) { + return ''; + } + + return arrays(content()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); +}); + diff --git a/src/flextype/Parsers/Shortcodes/MediaShortcode.php b/src/flextype/Parsers/Shortcodes/MediaShortcode.php index 26bbde31..199a6eaf 100644 --- a/src/flextype/Parsers/Shortcodes/MediaShortcode.php +++ b/src/flextype/Parsers/Shortcodes/MediaShortcode.php @@ -12,8 +12,12 @@ namespace Flextype\Parsers\Shortcodes; use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [media_files_fetch id="media-id" field="field-name" default="default-value"] -if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.media.enabled')) { - parsers()->shortcodes()->addHandler('media_files_fetch', static function (ShortcodeInterface $s) { - return arrays(flextype('media')->files()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); - }); -} +parsers()->shortcodes()->addHandler('media_files_fetch', static function (ShortcodeInterface $s) { + + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.media.enabled')) { + return ''; + } + + return arrays(flextype('media')->files()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); +}); + diff --git a/src/flextype/Parsers/Shortcodes/RawShortcode.php b/src/flextype/Parsers/Shortcodes/RawShortcode.php index 47c114ec..804f2294 100644 --- a/src/flextype/Parsers/Shortcodes/RawShortcode.php +++ b/src/flextype/Parsers/Shortcodes/RawShortcode.php @@ -14,10 +14,9 @@ use Thunder\Shortcode\Events; use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [raw] -if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.raw.enabled')) { - parsers()->shortcodes()->addHandler('raw', static function (ShortcodeInterface $s) { - return $s->getContent(); - }); +parsers()->shortcodes()->addHandler('raw', static function (ShortcodeInterface $s) { + return $s->getContent(); +}); + +parsers()->shortcodes()->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['raw'])); - parsers()->shortcodes()->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['raw'])); -} diff --git a/src/flextype/Parsers/Shortcodes/RegistryShortcode.php b/src/flextype/Parsers/Shortcodes/RegistryShortcode.php index 88d66861..08647414 100644 --- a/src/flextype/Parsers/Shortcodes/RegistryShortcode.php +++ b/src/flextype/Parsers/Shortcodes/RegistryShortcode.php @@ -12,8 +12,11 @@ namespace Flextype\Parsers\Shortcodes; use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [registry_get name="item-name" default="default-value"] -if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.registry.enabled')) { - parsers()->shortcodes()->addHandler('registry_get', static function (ShortcodeInterface $s) { - return registry()->get($s->getParameter('name'), $s->getParameter('default')); - }); -} +parsers()->shortcodes()->addHandler('registry_get', static function (ShortcodeInterface $s) { + + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.registry.enabled')) { + return ''; + } + + return registry()->get($s->getParameter('name'), $s->getParameter('default')); +}); \ No newline at end of file diff --git a/src/flextype/Parsers/Shortcodes/UrlShortcode.php b/src/flextype/Parsers/Shortcodes/UrlShortcode.php index f8341c3d..4bb4bf5f 100644 --- a/src/flextype/Parsers/Shortcodes/UrlShortcode.php +++ b/src/flextype/Parsers/Shortcodes/UrlShortcode.php @@ -13,12 +13,15 @@ use Slim\Http\Environment; use Slim\Http\Uri; // Shortcode: [url] -if (registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) { - parsers()->shortcodes()->addHandler('url', static function () { - if (registry()->has('flextype.settings.url') && registry()->get('flextype.settings.url') !== '') { - return registry()->get('flextype.settings.url'); - } +parsers()->shortcodes()->addHandler('url', static function () { + + if (!registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) { + return ''; + } - return app()->getBasePath(); - }); -} + if (registry()->has('flextype.settings.url') && registry()->get('flextype.settings.url') !== '') { + return registry()->get('flextype.settings.url'); + } + + return app()->getBasePath(); +});