From eeeee6e725ff870cae968f59602c9a7d20ab2766 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 14 Feb 2020 21:49:12 +0200 Subject: [PATCH 1/5] Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded --- CHANGELOG.md | 6 ++++++ classes/plugin/ShortcodeManager.php | 8 +++++--- shortcode-core.php | 17 ++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58201a8..5b674c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v4.2.2 +## mm/dd/2020 + +1. [](#improved) + * Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded + # v4.2.1 ## 02/14/2020 diff --git a/classes/plugin/ShortcodeManager.php b/classes/plugin/ShortcodeManager.php index 1e506b0..3bac6f1 100644 --- a/classes/plugin/ShortcodeManager.php +++ b/classes/plugin/ShortcodeManager.php @@ -198,13 +198,15 @@ public function registerShortcode($name, $directory = null) /** * register all files as shortcodes in a particular directory - * @param string $directory directory where the shortcodes are located + * @param string $directory directory where the shortcodes are located + * @param array $options Extra options */ - public function registerAllShortcodes($directory) + public function registerAllShortcodes($directory, array $options = []) { try { + $ignore = $options['ignore'] ?? []; foreach (new \DirectoryIterator($directory) as $file) { - if ($file->isDot()) { + if ($file->isDot() || \in_array($file->getBasename('.php'), $ignore, true)) { continue; } $this->registerShortcode($file->getFilename(), $directory); diff --git a/shortcode-core.php b/shortcode-core.php index de7ea16..8979049 100644 --- a/shortcode-core.php +++ b/shortcode-core.php @@ -103,6 +103,10 @@ public function onPageContentProcessed(Event $e) $this->processShortcodes($e['page'], 'processContent'); } + /** + * @param PageInterface $page + * @param string $type + */ protected function processShortcodes(PageInterface $page, $type = 'processContent') { $meta = []; $config = $this->mergeConfig($page); @@ -141,6 +145,10 @@ protected function processShortcodes(PageInterface $page, $type = 'processConten } } + /** + * @param PageInterface $page + * @return \Grav\Common\Data\Data + */ protected function getConfig(PageInterface $page) { $config = $this->mergeConfig($page); @@ -200,7 +208,7 @@ public function onPageContent(Event $event) */ public function onShortcodeHandlers() { - $this->shortcodes->registerAllShortcodes(__DIR__ . '/classes/shortcodes'); + $this->shortcodes->registerAllShortcodes(__DIR__ . '/classes/shortcodes', ['ignore' => ['Shortcode', 'ShortcodeObject']]); // Add custom shortcodes directory if provided $custom_shortcodes = $this->config->get('plugins.shortcode-core.custom_shortcodes'); @@ -214,12 +222,7 @@ public function onShortcodeHandlers() */ public function onTwigInitialized() { - $this->grav['twig']->twig()->addFilter( - new \Twig_SimpleFilter( - 'shortcodes', - [$this->shortcodes, 'processShortcodes'] - ) - ); + $this->grav['twig']->twig()->addFilter(new \Twig_SimpleFilter('shortcodes', [$this->shortcodes, 'processShortcodes'])); $this->grav['twig']->twig_vars['shortcode'] = new ShortcodeTwigVar(); } } From 16013bcb70f28b6aaeb227c30a5192f3d22ebf0c Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 28 Feb 2020 13:58:19 +0200 Subject: [PATCH 2/5] Fix shortcodes which do not override `init()` method, added deprecation notice instead --- CHANGELOG.md | 2 ++ classes/shortcodes/Shortcode.php | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b674c3..7f661f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ 1. [](#improved) * Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded +1. [](#bugfix) + * Fix shortcodes which do not override `init()` method, added deprecation notice instead # v4.2.1 ## 02/14/2020 diff --git a/classes/shortcodes/Shortcode.php b/classes/shortcodes/Shortcode.php index 69ff388..a3c9e30 100644 --- a/classes/shortcodes/Shortcode.php +++ b/classes/shortcodes/Shortcode.php @@ -36,7 +36,15 @@ public function __construct() /** * Initialize shortcode handler */ - abstract public function init(); + public function init() + { + user_error(__METHOD__ . '() method will be abstract in the future, please override it!', E_USER_DEPRECATED); + + // FIXME: This code had to be put back because of some plugins do not properly initialize themselves. + $this->shortcode->getHandlers()->add('u', static function(ShortcodeInterface $shortcode) { + return $shortcode->getContent(); + }); + } /** * Returns the name of the class if required From ca88077647a56e622944136945a9ce740f83319b Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 3 Mar 2020 10:51:25 +0200 Subject: [PATCH 3/5] Changelog update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f661f9..abd6e95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 1. [](#improved) * Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded 1. [](#bugfix) - * Fix shortcodes which do not override `init()` method, added deprecation notice instead + * Fix shortcodes which do not override `init()` method, added deprecation notice instead [#82](https://github.com/getgrav/grav-plugin-shortcode-core/issues/82) # v4.2.1 ## 02/14/2020 From 2e23f04e381a6857feb71f131111ff198aac78e3 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 4 Mar 2020 22:03:28 +0200 Subject: [PATCH 4/5] Fixed error message showing up when updating older versions (<4.2.0) of the plugin [#84] --- CHANGELOG.md | 1 + classes/Shortcode.php | 7 +++++++ classes/ShortcodeManager.php | 15 +++++++++++++++ classes/ShortcodeObject.php | 7 +++++++ 4 files changed, 30 insertions(+) create mode 100644 classes/Shortcode.php create mode 100644 classes/ShortcodeManager.php create mode 100644 classes/ShortcodeObject.php diff --git a/CHANGELOG.md b/CHANGELOG.md index abd6e95..fb1a8c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded 1. [](#bugfix) * Fix shortcodes which do not override `init()` method, added deprecation notice instead [#82](https://github.com/getgrav/grav-plugin-shortcode-core/issues/82) + * Fixed error message showing up when updating older versions (<4.2.0) of the plugin [#84](https://github.com/getgrav/grav-plugin-shortcode-core/issues/84) # v4.2.1 ## 02/14/2020 diff --git a/classes/Shortcode.php b/classes/Shortcode.php new file mode 100644 index 0000000..10cd16f --- /dev/null +++ b/classes/Shortcode.php @@ -0,0 +1,7 @@ + Date: Wed, 4 Mar 2020 16:31:22 -0700 Subject: [PATCH 5/5] prepare for release --- CHANGELOG.md | 2 +- blueprints.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb1a8c7..ae06306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v4.2.2 -## mm/dd/2020 +## 03/04/2020 1. [](#improved) * Added second `$options` parameter to `ShortcodeCore->registerAllShortcodes()`, key `ignore` can be used to ignore class names / files from being loaded diff --git a/blueprints.yaml b/blueprints.yaml index c7e5664..2231d25 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Shortcode Core -version: 4.2.1 +version: 4.2.2 description: "This plugin provides the core functionality for shortcode plugins" icon: code author: