Replacing shortcode implementation
This commit is contained in:
@ -6,7 +6,6 @@ use Html;
|
|||||||
use Event;
|
use Event;
|
||||||
use System\Classes\PluginBase;
|
use System\Classes\PluginBase;
|
||||||
use Illuminate\Foundation\AliasLoader;
|
use Illuminate\Foundation\AliasLoader;
|
||||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
|
||||||
use Sensory5\Shortcode\Models\Settings;
|
use Sensory5\Shortcode\Models\Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,7 +21,7 @@ The above registration would enable the following shortcode to be used:
|
|||||||
|
|
||||||
### Use the shortcode within a page or blog post:
|
### Use the shortcode within a page or blog post:
|
||||||
|
|
||||||
The usual shortcode syntax is supported via the [Thunderer\Shortcode](https://github.com/thunderer/Shortcode) project.
|
The usual shortcode syntax is supported:
|
||||||
|
|
||||||
[code]
|
[code]
|
||||||
[code argument="value"]
|
[code argument="value"]
|
||||||
@ -29,6 +29,8 @@ The usual shortcode syntax is supported via the [Thunderer\Shortcode](https://gi
|
|||||||
[code]content[/code]
|
[code]content[/code]
|
||||||
[code argument="value"]content[/code]
|
[code argument="value"]content[/code]
|
||||||
|
|
||||||
|
For nested shortcodes, you must call the `parse` method within the function.
|
||||||
|
|
||||||
### Enable shortcodes on all pages
|
### Enable shortcodes on all pages
|
||||||
|
|
||||||
To enable shortcodes on all page rendering, go to **Shortcode Settings** in the admin settings panel and check "Enable Shortcodes on all page rendering".
|
To enable shortcodes on all page rendering, go to **Shortcode Settings** in the admin settings panel and check "Enable Shortcodes on all page rendering".
|
||||||
|
87
classes/ProcessedShortcode.php
Normal file
87
classes/ProcessedShortcode.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php namespace Sensory5\Shortcode\Classes;
|
||||||
|
|
||||||
|
class ProcessedShortcode implements ShortcodeInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
private $attributes;
|
||||||
|
private $content;
|
||||||
|
private $name;
|
||||||
|
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create from shortcode data
|
||||||
|
*/
|
||||||
|
public static function create($attributes, $content, $tagName)
|
||||||
|
{
|
||||||
|
|
||||||
|
$self = new self();
|
||||||
|
$self->attributes = $attributes;
|
||||||
|
$self->content = $content;
|
||||||
|
$self->name = $tagName;
|
||||||
|
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns new instance of given shortcode with changed content
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function withContent($content)
|
||||||
|
{
|
||||||
|
$self = clone $this;
|
||||||
|
return $self->content = $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns shortcode name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns associative array(name => value) of shortcode parameters
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParameters()
|
||||||
|
{
|
||||||
|
return $this->attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns parameter value using its name, will return null for parameter
|
||||||
|
* without value
|
||||||
|
*
|
||||||
|
* @param string $name Parameter name
|
||||||
|
* @param null $default Value returned if there is no parameter with given name
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getParameter($name, $default = null)
|
||||||
|
{
|
||||||
|
return array_get($this->attributes, $name, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns shortcode content (data between opening and closing tag). Null
|
||||||
|
* means that shortcode had no content (was self closing), do not confuse
|
||||||
|
* that with empty string (hint: use strict comparison operator ===).
|
||||||
|
*
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getContent()
|
||||||
|
{
|
||||||
|
return $this->content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,153 +1,291 @@
|
|||||||
<?php namespace Sensory5\Shortcode\Classes;
|
<?php namespace Sensory5\Shortcode\Classes;
|
||||||
|
|
||||||
|
use Sensory5\Shortcode\Classes\ProcessedShortcode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortcodes
|
* This is a port of WordPress' brilliant shortcode feature
|
||||||
|
* for use outside of WordPress. The code has remained largely unchanged
|
||||||
*
|
*
|
||||||
* Original idea from Pingpong Labs (https://github.com/pingpong-labs/shortcode)
|
* Original from: https://github.com/Badcow/Shortcodes
|
||||||
*
|
*
|
||||||
* Changed to fit closer to the Thunderer\Shortcode syntax.
|
* Class Shortcode
|
||||||
|
*
|
||||||
|
* @package Shortcode
|
||||||
*/
|
*/
|
||||||
|
class Shortcode
|
||||||
use Countable;
|
|
||||||
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
|
|
||||||
use Thunder\Shortcode\Parser\RegexParser;
|
|
||||||
use Thunder\Shortcode\Processor\Processor;
|
|
||||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
|
||||||
|
|
||||||
class Shortcode implements Countable
|
|
||||||
{
|
{
|
||||||
/** @var HandlerContainer */
|
/**
|
||||||
private $handlers;
|
* The regex for attributes.
|
||||||
|
*
|
||||||
|
* This regex covers the following attribute situations:
|
||||||
|
* - key = "value"
|
||||||
|
* - key = 'value'
|
||||||
|
* - key = value
|
||||||
|
* - "value"
|
||||||
|
* - value
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $attrPattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constructor.
|
* Indexed array of tags: shortcode callbacks
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
private $shortcodes = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $tag
|
||||||
|
* @param callable $function
|
||||||
|
* @throws \ErrorException
|
||||||
|
*/
|
||||||
|
public function add($tag, $function)
|
||||||
{
|
{
|
||||||
$this->handlers = new HandlerContainer();
|
if (!is_callable($function)) {
|
||||||
|
throw new \ErrorException("Function must be callable");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->shortcodes[$tag] = $function;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the names for all registered shortcodes.
|
* @param string $tag
|
||||||
*
|
* @return Shortcode
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public function getNames()
|
public function remove($tag)
|
||||||
{
|
{
|
||||||
return $this->handlers->getNames();
|
if (array_key_exists($tag, $this->shortcodes)) {
|
||||||
}
|
unset($this->shortcodes[$tag]);
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a new shortcode to the handler container.
|
|
||||||
*
|
|
||||||
* @param string $name
|
|
||||||
* @param mixed $callback
|
|
||||||
*/
|
|
||||||
public function add($name, $callback)
|
|
||||||
{
|
|
||||||
$this->handlers->add($name, $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the specified shortcode name from the handler.
|
|
||||||
*
|
|
||||||
* @param string $name
|
|
||||||
*/
|
|
||||||
public function remove($name)
|
|
||||||
{
|
|
||||||
if ($this->exists($name)) {
|
|
||||||
$this->handlers->remove($name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all registered shortcodes
|
* @return array
|
||||||
*
|
|
||||||
* @return self
|
|
||||||
*/
|
*/
|
||||||
public function destroyAll()
|
public function getShortcodes()
|
||||||
{
|
{
|
||||||
$this->handlers = new HandlerContainer();
|
return $this->shortcodes;
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strip any shortcodes from the content.
|
* @param $shortcode
|
||||||
*
|
|
||||||
* @param string $content
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function strip($content)
|
|
||||||
{
|
|
||||||
$handlers = new HandlerContainer();
|
|
||||||
$handlers->setDefault(function(ShortcodeInterface $s) { return $s->getContent(); });
|
|
||||||
$processor = new Processor(new RegexParser(), $handlers);
|
|
||||||
|
|
||||||
return $processor->process($content);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get count from all shortcodes.
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function count()
|
|
||||||
{
|
|
||||||
return count($this->handlers->getNames());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true is the given name exist in shortcodes array.
|
|
||||||
*
|
|
||||||
* @param string $name
|
|
||||||
*
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function exists($name)
|
public function has($shortcode)
|
||||||
{
|
{
|
||||||
return $this->handlers->has($name);
|
return array_key_exists($shortcode, $this->shortcodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return true is the given content contains the named shortcode.
|
* Tests whether content has a particular shortcode
|
||||||
*
|
|
||||||
* @param string $content
|
|
||||||
* @param string $name
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function contains($content, $name)
|
|
||||||
{
|
|
||||||
$hasShortcode = false;
|
|
||||||
|
|
||||||
$handlers = new HandlerContainer();
|
|
||||||
$handlers->setDefault(function(ShortcodeInterface $s) use($name, &$hasShortcode) {
|
|
||||||
if($s->getName() === $name) {
|
|
||||||
$hasShortcode = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
$processor = new Processor(new RegexParser(), $handlers);
|
|
||||||
$processor->process($content);
|
|
||||||
|
|
||||||
return $hasShortcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse content and replace parts of it using registered handlers
|
|
||||||
*
|
*
|
||||||
* @param $content
|
* @param $content
|
||||||
|
* @param $tag
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function contentContains($content, $tag)
|
||||||
|
{
|
||||||
|
if (!$this->has($tag)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
preg_match_all($this->shortcodeRegex(), $content, $matches, PREG_SET_ORDER);
|
||||||
|
|
||||||
|
if (empty($matches)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($matches as $shortcode) {
|
||||||
|
if ($tag === $shortcode[2]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of tag names that have been added
|
||||||
*
|
*
|
||||||
* @return string
|
* @return array
|
||||||
|
*/
|
||||||
|
public function names()
|
||||||
|
{
|
||||||
|
|
||||||
|
return array_keys($this->shortcodes);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search content for shortcodes and filter shortcodes through their hooks.
|
||||||
|
*
|
||||||
|
* If there are no shortcode tags defined, then the content will be returned
|
||||||
|
* without any filtering. This might cause issues when plugins are disabled but
|
||||||
|
* the shortcode will still show up in the post or content.
|
||||||
|
*
|
||||||
|
* @param string $content Content to search for shortcodes
|
||||||
|
* @return string Content with shortcodes filtered out.
|
||||||
*/
|
*/
|
||||||
public function parse($content)
|
public function parse($content)
|
||||||
{
|
{
|
||||||
$processor = new Processor(new RegexParser(), $this->handlers);
|
if (empty($this->shortcodes)) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
return $processor->process($content);
|
return preg_replace_callback($this->shortcodeRegex(), array($this, 'processTag'), $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all shortcode tags from the given content.
|
||||||
|
*
|
||||||
|
* @uses $shortcode_tags
|
||||||
|
*
|
||||||
|
* @param string $content Content to remove shortcode tags.
|
||||||
|
* @return string Content without shortcode tags.
|
||||||
|
*/
|
||||||
|
public function strip($content)
|
||||||
|
{
|
||||||
|
if (empty($this->shortcodes)) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
return preg_replace_callback($this->shortcodeRegex(), array($this, 'stripShortcodeTag'), $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regular Expression callable for do_shortcode() for calling shortcode hook.
|
||||||
|
*
|
||||||
|
* @see get_shortcode_regex for details of the match array contents.
|
||||||
|
*
|
||||||
|
* @param array $tag Regular expression match array
|
||||||
|
* @return mixed False on failure.
|
||||||
|
*/
|
||||||
|
private function processTag(array $tag)
|
||||||
|
{
|
||||||
|
// allow [[foo]] syntax for escaping a tag
|
||||||
|
if ($tag[1] == '[' && $tag[6] == ']') {
|
||||||
|
return substr($tag[0], 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tagName = $tag[2];
|
||||||
|
$attr = $this->parseAttributes($tag[3]);
|
||||||
|
|
||||||
|
$processed = ProcessedShortcode::create($attr, isset($tag[5]) ? $tag[5] : null, $tagName);
|
||||||
|
|
||||||
|
return $tag[1] . call_user_func($this->shortcodes[$tagName], $processed) . $tag[6];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all attributes from the shortcodes tag.
|
||||||
|
*
|
||||||
|
* The attributes list has the attribute name as the key and the value of the
|
||||||
|
* attribute as the value in the key/value pair. This allows for easier
|
||||||
|
* retrieval of the attributes, since all attributes have to be known.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param string $text
|
||||||
|
* @return array List of attributes and their value.
|
||||||
|
*/
|
||||||
|
private function parseAttributes($text)
|
||||||
|
{
|
||||||
|
$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
|
||||||
|
|
||||||
|
if (!preg_match_all($this->attrPattern, $text, $matches, PREG_SET_ORDER)) {
|
||||||
|
return array(ltrim($text));
|
||||||
|
}
|
||||||
|
|
||||||
|
$attr = array();
|
||||||
|
|
||||||
|
foreach ($matches as $match) {
|
||||||
|
if (!empty($match[1])) {
|
||||||
|
$attr[strtolower($match[1])] = stripcslashes($match[2]);
|
||||||
|
} elseif (!empty($match[3])) {
|
||||||
|
$attr[strtolower($match[3])] = stripcslashes($match[4]);
|
||||||
|
} elseif (!empty($match[5])) {
|
||||||
|
$attr[strtolower($match[5])] = stripcslashes($match[6]);
|
||||||
|
} elseif (isset($match[7]) && strlen($match[7])) {
|
||||||
|
$attr[] = stripcslashes($match[7]);
|
||||||
|
} elseif (isset($match[8])) {
|
||||||
|
$attr[] = stripcslashes($match[8]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strips a tag leaving escaped tags
|
||||||
|
*
|
||||||
|
* @param $tag
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function stripShortcodeTag($tag)
|
||||||
|
{
|
||||||
|
if ($tag[1] == '[' && $tag[6] == ']') {
|
||||||
|
return substr($tag[0], 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tag[1] . $tag[6];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the shortcode regular expression for searching.
|
||||||
|
*
|
||||||
|
* The regular expression combines the shortcode tags in the regular expression
|
||||||
|
* in a regex class.
|
||||||
|
*
|
||||||
|
* The regular expression contains 6 different sub matches to help with parsing.
|
||||||
|
*
|
||||||
|
* 1 - An extra [ to allow for escaping shortcodes with double [[]]
|
||||||
|
* 2 - The shortcode name
|
||||||
|
* 3 - The shortcode argument list
|
||||||
|
* 4 - The self closing /
|
||||||
|
* 5 - The content of a shortcode when it wraps some content.
|
||||||
|
* 6 - An extra ] to allow for escaping shortcodes with double [[]]
|
||||||
|
*
|
||||||
|
* @return string The shortcode search regular expression
|
||||||
|
*/
|
||||||
|
private function shortcodeRegex()
|
||||||
|
{
|
||||||
|
$tagRegex = join('|', array_map('preg_quote', array_keys($this->shortcodes)));
|
||||||
|
|
||||||
|
return
|
||||||
|
'/'
|
||||||
|
. '\\[' // Opening bracket
|
||||||
|
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
|
||||||
|
. "($tagRegex)" // 2: Shortcode name
|
||||||
|
. '(?![\\w-])' // Not followed by word character or hyphen
|
||||||
|
. '(' // 3: Unroll the loop: Inside the opening shortcode tag
|
||||||
|
. '[^\\]\\/]*' // Not a closing bracket or forward slash
|
||||||
|
. '(?:'
|
||||||
|
. '\\/(?!\\])' // A forward slash not followed by a closing bracket
|
||||||
|
. '[^\\]\\/]*' // Not a closing bracket or forward slash
|
||||||
|
. ')*?'
|
||||||
|
. ')'
|
||||||
|
. '(?:'
|
||||||
|
. '(\\/)' // 4: Self closing tag ...
|
||||||
|
. '\\]' // ... and closing bracket
|
||||||
|
. '|'
|
||||||
|
. '\\]' // Closing bracket
|
||||||
|
. '(?:'
|
||||||
|
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
|
||||||
|
. '[^\\[]*+' // Not an opening bracket
|
||||||
|
. '(?:'
|
||||||
|
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
|
||||||
|
. '[^\\[]*+' // Not an opening bracket
|
||||||
|
. ')*+'
|
||||||
|
. ')'
|
||||||
|
. '\\[\\/\\2\\]' // Closing shortcode tag
|
||||||
|
. ')?'
|
||||||
|
. ')'
|
||||||
|
. '(\\]?)' // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
|
||||||
|
. '/s';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
51
classes/ShortcodeInterface.php
Normal file
51
classes/ShortcodeInterface.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php namespace Sensory5\Shortcode\Classes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
|
||||||
|
*/
|
||||||
|
interface ShortcodeInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns new instance of given shortcode with changed content
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function withContent($content);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns shortcode name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns associative array(name => value) of shortcode parameters
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParameters();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns parameter value using its name, will return null for parameter
|
||||||
|
* without value
|
||||||
|
*
|
||||||
|
* @param string $name Parameter name
|
||||||
|
* @param null $default Value returned if there is no parameter with given name
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getParameter($name, $default = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns shortcode content (data between opening and closing tag). Null
|
||||||
|
* means that shortcode had no content (was self closing), do not confuse
|
||||||
|
* that with empty string (hint: use strict comparison operator ===).
|
||||||
|
*
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getContent();
|
||||||
|
|
||||||
|
}
|
@ -15,8 +15,7 @@
|
|||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.4.0",
|
"php": ">=5.4.0",
|
||||||
"composer/installers": "~1.0",
|
"composer/installers": "~1.0"
|
||||||
"thunderer/shortcode": "dev-master"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
60
composer.lock
generated
60
composer.lock
generated
@ -4,8 +4,8 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"hash": "1af5d57fe99292fd0a02e59af2fe9fd5",
|
"hash": "76dd76ccda7c2f94fab2f61858c44195",
|
||||||
"content-hash": "fb0e5eeb8a149a654e9b5bea92cd66e7",
|
"content-hash": "d098e1add52c1f1376b78a8d954f5408",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "composer/installers",
|
"name": "composer/installers",
|
||||||
@ -104,66 +104,12 @@
|
|||||||
"zikula"
|
"zikula"
|
||||||
],
|
],
|
||||||
"time": "2015-10-29 23:28:48"
|
"time": "2015-10-29 23:28:48"
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "thunderer/shortcode",
|
|
||||||
"version": "dev-master",
|
|
||||||
"source": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/thunderer/Shortcode.git",
|
|
||||||
"reference": "745110c4ef490ba84a910abf7d3a2f26830df541"
|
|
||||||
},
|
|
||||||
"dist": {
|
|
||||||
"type": "zip",
|
|
||||||
"url": "https://api.github.com/repos/thunderer/Shortcode/zipball/745110c4ef490ba84a910abf7d3a2f26830df541",
|
|
||||||
"reference": "745110c4ef490ba84a910abf7d3a2f26830df541",
|
|
||||||
"shasum": ""
|
|
||||||
},
|
|
||||||
"require": {
|
|
||||||
"php": ">=5.3"
|
|
||||||
},
|
|
||||||
"require-dev": {
|
|
||||||
"phpunit/phpunit": "~4.1"
|
|
||||||
},
|
|
||||||
"suggest": {
|
|
||||||
"ext-dom": "if you want to use XML serializer",
|
|
||||||
"ext-json": "if you want to use JSON serializer",
|
|
||||||
"symfony/yaml": "if you want to use YAML serializer"
|
|
||||||
},
|
|
||||||
"type": "library",
|
|
||||||
"autoload": {
|
|
||||||
"psr-4": {
|
|
||||||
"Thunder\\Shortcode\\": "src/",
|
|
||||||
"Thunder\\Shortcode\\Tests\\": "tests/"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"notification-url": "https://packagist.org/downloads/",
|
|
||||||
"license": [
|
|
||||||
"MIT"
|
|
||||||
],
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Tomasz Kowalczyk",
|
|
||||||
"email": "tomasz@kowalczyk.cc"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "Advanced shortcode (BBCode) parser and engine for PHP",
|
|
||||||
"keywords": [
|
|
||||||
"bbcode",
|
|
||||||
"engine",
|
|
||||||
"library",
|
|
||||||
"parser",
|
|
||||||
"shortcode"
|
|
||||||
],
|
|
||||||
"time": "2015-11-12 13:00:02"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [],
|
"packages-dev": [],
|
||||||
"aliases": [],
|
"aliases": [],
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"stability-flags": {
|
"stability-flags": [],
|
||||||
"thunderer/shortcode": 20
|
|
||||||
},
|
|
||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
"platform": {
|
"platform": {
|
||||||
|
23
phpunit.xml
Normal file
23
phpunit.xml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
bootstrap="../../../tests/bootstrap.php"
|
||||||
|
colors="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false"
|
||||||
|
syntaxCheck="false"
|
||||||
|
>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="PolicyManager Unit Test Suite">
|
||||||
|
<directory>./tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<php>
|
||||||
|
<env name="APP_ENV" value="testing" />
|
||||||
|
<env name="CACHE_DRIVER" value="array" />
|
||||||
|
<env name="SESSION_DRIVER" value="array" />
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
@ -1,12 +1,11 @@
|
|||||||
<?php namespace Sensory5\Shortcode;
|
<?php namespace Sensory5\Shortcode;
|
||||||
|
|
||||||
|
use Lang;
|
||||||
|
use PluginTestCase;
|
||||||
use Sensory5\Shortcode\Classes\Shortcode;
|
use Sensory5\Shortcode\Classes\Shortcode;
|
||||||
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
|
use Sensory5\Shortcode\Classes\ShortcodeInterface;
|
||||||
use Thunder\Shortcode\Parser\RegexParser;
|
|
||||||
use Thunder\Shortcode\Processor\Processor;
|
|
||||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
|
||||||
|
|
||||||
class ShortcodeTest extends \PHPUnit_Framework_TestCase
|
class ShortcodeTest extends PluginTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideTexts
|
* @dataProvider provideTexts
|
||||||
@ -21,71 +20,95 @@ class ShortcodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
return [
|
return [
|
||||||
['[name]', 'name'],
|
['[name]', 'name'],
|
||||||
['[content]', ''],
|
['[content]', ''],
|
||||||
['[content]thunder[/content]', 'thunder'],
|
['[content]sensory5[/content]', 'sensory5'],
|
||||||
['[content][name][/content]', 'name'],
|
['[nested][name][/nested]', 'name'],
|
||||||
['[nc][name][/nc]', 'nc: name'],
|
['[content]no1[/content], [content]no2[/content], [nested]no3, [content]no4[/content][/nested]', 'no1, no2, no3, no4'],
|
||||||
|
/* ['[nc][name][/nc]', 'nc: name'], */
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCount()
|
public function testCount()
|
||||||
{
|
{
|
||||||
$this->assertSame(3, $this->getShortcode()->count());
|
$this->assertSame(6, count($this->getShortcode()->getShortCodes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAll()
|
public function testNames()
|
||||||
{
|
{
|
||||||
$this->assertSame(['name', 'content', 'nc'], $this->getShortcode()->all());
|
$this->assertSame(['name', 'content', 'nc', 'nested', 'params', 'param'], $this->getShortcode()->names());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnregister()
|
public function testRemove()
|
||||||
{
|
{
|
||||||
$this->assertSame('[name]', $this->getShortcode()->unregister('name')->parse('[name]'));
|
$this->assertSame('[name]', $this->getShortcode()->remove('name')->parse('[name]'));
|
||||||
}
|
|
||||||
|
|
||||||
public function testDestroy()
|
|
||||||
{
|
|
||||||
$this->assertSame('[name]', $this->getShortcode()->destroy()->parse('[name]'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStrip()
|
public function testStrip()
|
||||||
{
|
{
|
||||||
$this->assertSame('', $this->getShortcode()->strip('[name]'));
|
$this->assertSame('', $this->getShortcode()->strip('[name]'));
|
||||||
$this->assertSame('x y', $this->getShortcode()->strip('x [name]y'));
|
$this->assertSame('x y', $this->getShortcode()->strip('x [name]y'));
|
||||||
$this->assertSame('x a a y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
|
// $this->assertSame('x a a y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
|
||||||
|
$this->assertSame('x y', $this->getShortcode()->strip('x [name] a [content /] a [/name] y'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExists()
|
public function testHas()
|
||||||
{
|
{
|
||||||
$shortcode = $this->getShortcode();
|
$shortcode = $this->getShortcode();
|
||||||
|
|
||||||
$this->assertTrue($shortcode->exists('name'));
|
$this->assertTrue($shortcode->has('name'));
|
||||||
$this->assertTrue($shortcode->exists('content'));
|
$this->assertTrue($shortcode->has('content'));
|
||||||
$this->assertTrue($shortcode->exists('nc'));
|
$this->assertTrue($shortcode->has('nc'));
|
||||||
$this->assertFalse($shortcode->exists('invalid'));
|
$this->assertFalse($shortcode->has('invalid'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testContains()
|
public function testContains()
|
||||||
{
|
{
|
||||||
$shortcode = $this->getShortcode();
|
$shortcode = $this->getShortcode();
|
||||||
|
|
||||||
$this->assertTrue($shortcode->contains('[name]', 'name'));
|
$this->assertTrue($shortcode->contentContains('[name]', 'name'));
|
||||||
$this->assertFalse($shortcode->contains('[x]', 'name'));
|
$this->assertFalse($shortcode->contentContains('[x]', 'name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetParameters()
|
||||||
|
{
|
||||||
|
$shortcode = $this->getShortcode();
|
||||||
|
|
||||||
|
$this->assertSame('param1,one,param2,two,', $shortcode->parse('[params param1=one param2=two]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testgetParameter()
|
||||||
|
{
|
||||||
|
$shortcode = $this->getShortcode();
|
||||||
|
|
||||||
|
$this->assertSame('numero uno', $shortcode->parse('[param param1="numero uno"]'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getShortcode()
|
private function getShortcode()
|
||||||
{
|
{
|
||||||
$shortcode = new Shortcode();
|
$shortcode = new Shortcode();
|
||||||
|
|
||||||
$shortcode->register('name', function(ShortcodeInterface $s) {
|
$shortcode->add('name', function(ShortcodeInterface $s) {
|
||||||
return $s->getName();
|
return $s->getName();
|
||||||
});
|
});
|
||||||
$shortcode->register('content', function(ShortcodeInterface $s) {
|
$shortcode->add('content', function(ShortcodeInterface $s) {
|
||||||
return $s->getContent();
|
return $s->getContent();
|
||||||
});
|
});
|
||||||
$shortcode->register('nc', function(ShortcodeInterface $s) {
|
$shortcode->add('nc', function(ShortcodeInterface $s) {
|
||||||
return $s->getName().': '.$s->getContent();
|
return $s->getName().': '.$s->getContent();
|
||||||
});
|
});
|
||||||
|
$shortcode->add('nested', function(ShortcodeInterface $s) use ($shortcode) {
|
||||||
|
return $shortcode->parse($s->getContent());
|
||||||
|
});
|
||||||
|
$shortcode->add('params', function(ShortcodeInterface $s) use ($shortcode) {
|
||||||
|
$params = '';
|
||||||
|
foreach($s->getParameters() as $key => $value) {
|
||||||
|
$params .= $key.','.$value.',';
|
||||||
|
}
|
||||||
|
return $params;
|
||||||
|
});
|
||||||
|
$shortcode->add('param', function(ShortcodeInterface $s) use ($shortcode) {
|
||||||
|
return $s->getParameter('param1');
|
||||||
|
});
|
||||||
return $shortcode;
|
return $shortcode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1,2 @@
|
|||||||
1.0.1: First version of Shortcodes
|
1.0.1: First version of Shortcodes
|
||||||
|
1.1.0: Replacing Shortcode implementation with WordPress clone
|
||||||
|
Reference in New Issue
Block a user