1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-10 23:24:04 +02:00

feat(core): Shortcode Parser Enhancements #454

This commit is contained in:
Awilum
2020-08-12 10:37:22 +03:00
parent cbae6952f3
commit 30f473bb92
10 changed files with 156 additions and 80 deletions

View File

@@ -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);
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Flextype\Component\Arrays\Arrays;
if ($flextype->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'));
});
}

View File

@@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Flextype\Component\Arrays\Arrays;
// Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"]
$flextype['shortcode']->add('entries_fetch', function (ShortcodeInterface $s) use ($flextype) {
return Arrays::get($flextype['entries']->fetch($s->getParameter('id')), $s->getParameter('field'), $s->getParameter('default'));
});

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Thunder\Shortcode\EventHandler\FilterRawEventHandler;
use Thunder\Shortcode\Events;
if ($flextype->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']));
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
if ($flextype->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'));
});
}

View File

@@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
// Shortcode: [registry_get name="item-name" default="default-value"]
$flextype['shortcode']->add('registry_get', function (ShortcodeInterface $s) use ($flextype) {
return $flextype['registry']->get($s->getParameter('name'), $s->getParameter('default'));
});

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Slim\Http\Environment;
use Slim\Http\Uri;
if ($flextype->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();
});
}

View File

@@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Slim\Http\Environment;
use Slim\Http\Uri;
// Shortcode: [url]
$flextype['shortcode']->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();
});

View File

@@ -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;
}
/**

View File

@@ -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
#