diff --git a/src/flextype/app/Support/Parsers/Shortcode.php b/src/flextype/app/Support/Parsers/Shortcode.php index 1c04930c..f390c97e 100644 --- a/src/flextype/app/Support/Parsers/Shortcode.php +++ b/src/flextype/app/Support/Parsers/Shortcode.php @@ -35,25 +35,61 @@ class Shortcode } /** - * Add shortcode handler + * Shortcode instance * - * @param string $name Shortcode name - * @param callable $handler Handler + * @access public */ - public function add(string $name, $handler) + public function getInstance() + { + return $this->shortcode; + } + + /** + * Add shortcode handler. + * + * @param string $name Shortcode + * @param callable $handler Handler + * + * @access public + */ + public function addHandler(string $name, $handler) { return $this->shortcode->addHandler($name, $handler); } /** - * Takes a SHORTCODE encoded string and converts it into a PHP variable. + * Add event handler. * - * @param string $input A string containing SHORTCODE + * @param string $name Event + * @param callable $handler Handler + * + * @access public + */ + public function addEventHandler($name, $handler) { + return $this->shortcode->addEventHandler($name, $handler); + } + + /** + * Parses text into shortcodes. + * + * @param string $input A text containing SHORTCODE + * + * @access public + */ + public function parse(string $input) + { + return $this->shortcode->parse($input); + } + + /** + * Processes text and replaces shortcodes. + * + * @param string $input A text containing SHORTCODE * @param bool $cache Cache result data or no. Default is true * - * @return mixed The SHORTCODE converted to a PHP value + * @access public */ - public function parse(string $input, bool $cache = true) : string + public function process(string $input, bool $cache = true) { if ($cache === true && $this->flextype['registry']->get('flextype.settings.cache.enabled') === true) { $key = $this->getCacheID($input); @@ -62,24 +98,25 @@ class Shortcode return $data_from_cache; } - $data = $this->_parse($input); + $data = $this->shortcode->process($input); $this->flextype['cache']->save($key, $data); return $data; } - return $this->_parse($input); - } - - /** - * @see parse() - */ - protected function _parse(string $input) : string - { return $this->shortcode->process($input); } - protected function getCacheID($input) + /** + * Get Cache ID for shortcode + * + * @param string $input Input + * + * @return string Cache ID + * + * @access public + */ + public function getCacheID(string $input) : string { return md5('shortcode' . $input); } diff --git a/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcode.php new file mode 100644 index 00000000..a4a98d40 --- /dev/null +++ b/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcode.php @@ -0,0 +1,19 @@ +registry->get('flextype.settings.shortcode.shortcodes.entries.enabled')) { + + // Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"] + $flextype['shortcode']->addHandler('entries_fetch', function (ShortcodeInterface $s) use ($flextype) { + return Arrays::get($flextype['entries']->fetch($s->getParameter('id')), $s->getParameter('field'), $s->getParameter('default')); + }); +} diff --git a/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcodeExtension.php b/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcodeExtension.php deleted file mode 100644 index 1afcccfc..00000000 --- a/src/flextype/app/Support/Parsers/Shortcodes/EntriesShortcodeExtension.php +++ /dev/null @@ -1,16 +0,0 @@ -add('entries_fetch', function (ShortcodeInterface $s) use ($flextype) { - return Arrays::get($flextype['entries']->fetch($s->getParameter('id')), $s->getParameter('field'), $s->getParameter('default')); -}); diff --git a/src/flextype/app/Support/Parsers/Shortcodes/RawShortcode.php b/src/flextype/app/Support/Parsers/Shortcodes/RawShortcode.php new file mode 100644 index 00000000..f1c69ba4 --- /dev/null +++ b/src/flextype/app/Support/Parsers/Shortcodes/RawShortcode.php @@ -0,0 +1,22 @@ +registry->get('flextype.settings.shortcode.shortcodes.raw.enabled')) { + + // Shortcode: [raw] + $flextype['shortcode']->addHandler('raw', function (ShortcodeInterface $s) use ($flextype) { + return $s->getContent(); + }); + + $flextype['shortcode']->addEventHandler(Events::FILTER_SHORTCODES, new FilterRawEventHandler(['raw'])); +} diff --git a/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcode.php b/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcode.php new file mode 100644 index 00000000..4b0e1bb7 --- /dev/null +++ b/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcode.php @@ -0,0 +1,18 @@ +registry->get('flextype.settings.shortcode.shortcodes.registry.enabled')) { + + // Shortcode: [registry_get name="item-name" default="default-value"] + $flextype['shortcode']->addHandler('registry_get', function (ShortcodeInterface $s) use ($flextype) { + return $flextype['registry']->get($s->getParameter('name'), $s->getParameter('default')); + }); +} diff --git a/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcodeExtension.php b/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcodeExtension.php deleted file mode 100644 index e5176cda..00000000 --- a/src/flextype/app/Support/Parsers/Shortcodes/RegistryShortcodeExtension.php +++ /dev/null @@ -1,15 +0,0 @@ -add('registry_get', function (ShortcodeInterface $s) use ($flextype) { - return $flextype['registry']->get($s->getParameter('name'), $s->getParameter('default')); -}); diff --git a/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcode.php b/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcode.php new file mode 100644 index 00000000..d5f998ad --- /dev/null +++ b/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcode.php @@ -0,0 +1,23 @@ +registry->get('flextype.settings.shortcode.shortcodes.url.enabled')) { + + // Shortcode: [url] + $flextype['shortcode']->addHandler('url', function () use ($flextype) { + if ($flextype['registry']->has('flextype.settings.url') && $flextype['registry']->get('flextype.settings.url') !== '') { + return $flextype['registry']->get('flextype.settings.url'); + } + + return Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl(); + }); +} diff --git a/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcodeExtension.php b/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcodeExtension.php deleted file mode 100644 index 0aaf62f5..00000000 --- a/src/flextype/app/Support/Parsers/Shortcodes/UrlShortcodeExtension.php +++ /dev/null @@ -1,20 +0,0 @@ -add('url', function () use ($flextype) { - if ($flextype['registry']->has('flextype.settings.url') && $flextype['registry']->get('flextype.settings.url') !== '') { - return $flextype['registry']->get('flextype.settings.url'); - } - - return Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl(); -}); diff --git a/src/flextype/bootstrap.php b/src/flextype/bootstrap.php index d29fbe0c..76a54de5 100755 --- a/src/flextype/bootstrap.php +++ b/src/flextype/bootstrap.php @@ -106,17 +106,17 @@ date_default_timezone_set($flextype['registry']->get('flextype.settings.timezone /** * Init shortocodes * - * Load Flextype Shortcodes extensions from directory /flextype/app/Support/Parsers/Shortcodes/ based on flextype.settings.shortcodes.extensions array + * Load Flextype Shortcodes from directory /flextype/app/Support/Parsers/Shortcodes/ based on flextype.settings.shortcode.shortcodes array */ -$shortcodes_extensions = $flextype['registry']->get('flextype.settings.shortcodes.extensions'); +$shortcodes = $flextype['registry']->get('flextype.settings.shortcode.shortcodes'); -foreach ($shortcodes_extensions as $shortcodes_extension) { - $shortcodes_extension_file_path = ROOT_DIR . '/src/flextype/app/Support/Parsers/Shortcodes/' . $shortcodes_extension . 'ShortcodeExtension.php'; - if (! file_exists($shortcodes_extension_file_path)) { +foreach ($shortcodes as $shortcode_name => $shortcode) { + $shortcode_file_path = ROOT_DIR . '/src/flextype/app/Support/Parsers/Shortcodes/' . str_replace("_", '', ucwords($shortcode_name, "_")) . 'Shortcode.php'; + if (! file_exists($shortcode_file_path)) { continue; } - include_once $shortcodes_extension_file_path; + include_once $shortcode_file_path; } /** diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index c661fff0..0906f7b3 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -81,7 +81,7 @@ entries: enabled: true id: enabled: true - + # Cache # # - enabled: Set to true to enable caching @@ -216,11 +216,19 @@ slugify: image: driver: gd -# Shortcodes +# Shortcode # -# - extensions: Flextype Shortcodes Extension to load. -shortcodes: - extensions: ['Entries', 'Registry', 'Url'] +# - shortcodes: Flextype Shortcodes to load. +shortcode: + shortcodes: + entries: + enabled: true + raw: + enabled: true + registry: + enabled: true + url: + enabled: true # CORS #