Skip to content

Commit

Permalink
Merge branch 'release/4.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Mar 4, 2020
2 parents b835d7d + 26e53e5 commit 10a555d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# v4.2.2
## 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
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

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
7 changes: 7 additions & 0 deletions classes/Shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// This file is for older Grav versions (needed during plugin update).

if (!class_exists(\Grav\Plugin\Shortcodes\Shortcode::class, false)) {
require_once __DIR__ . '/shortcodes/Shortcode.php';
}
15 changes: 15 additions & 0 deletions classes/ShortcodeManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// This file is for older Grav versions (needed during plugin update).

@trigger_error(
'\\Grav\\Plugin\\ShortcodeManager class is deprecated, use \\Grav\\Plugin\\ShortcodeCore\\ShortcodeManager instead',
E_USER_DEPRECATED
);

if (!class_exists(\Grav\Plugin\ShortcodeCore\ShortcodeManager::class, false)) {
require_once __DIR__ . '/plugin/ShortcodeManager.php';
}

// Create alias for the deprecated class.
class_alias(\Grav\Plugin\ShortcodeCore\ShortcodeManager::class, \Grav\Plugin\ShortcodeManager::class);
7 changes: 7 additions & 0 deletions classes/ShortcodeObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// This file is for older Grav versions (needed during plugin update).

if (!class_exists(\Grav\Plugin\ShortcodeCore\ShortcodeObject::class, false)) {
require_once __DIR__ . '/shortcodes/ShortcodeObject.php';
}
8 changes: 5 additions & 3 deletions classes/plugin/ShortcodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion classes/shortcodes/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions shortcode-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -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();
}
}

0 comments on commit 10a555d

Please sign in to comment.