Merge pull request #6302 from jonom/shortcode-parse-extension-points

API Added extension points to ShortcodeParser
This commit is contained in:
Daniel Hensby 2016-11-09 11:23:16 +00:00 committed by GitHub
commit cfbfe1427c

View File

@ -21,6 +21,13 @@ class ShortcodeParser extends Object {
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
/**
* Registered shortcodes. Items follow this structure:
* [shortcode_name] => Array(
* [0] => class_containing_handler
* [1] => name_of_shortcode_handler_method
* )
*/
protected $shortcodes = array(); protected $shortcodes = array();
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
@ -96,6 +103,15 @@ class ShortcodeParser extends Object {
if($this->registered($shortcode)) unset($this->shortcodes[$shortcode]); if($this->registered($shortcode)) unset($this->shortcodes[$shortcode]);
} }
/**
* Get an array containing information about registered shortcodes
*
* @return array
*/
public function getRegisteredShortcodes() {
return $this->shortcodes;
}
/** /**
* Remove all registered shortcodes. * Remove all registered shortcodes.
*/ */
@ -540,15 +556,21 @@ class ShortcodeParser extends Object {
* @return string * @return string
*/ */
public function parse($content) { public function parse($content) {
$this->extend('onBeforeParse', $content);
$continue = true;
// If no shortcodes defined, don't try and parse any // If no shortcodes defined, don't try and parse any
if(!$this->shortcodes) return $content; if(!$this->shortcodes) $continue = false;
// If no content, don't try and parse it // If no content, don't try and parse it
if (!trim($content)) return $content; else if (!trim($content)) $continue = false;
// If no shortcode tag, don't try and parse it // If no shortcode tag, don't try and parse it
if (strpos($content, '[') === false) return $content; else if (strpos($content, '[') === false) $continue = false;
if ($continue) {
// First we operate in text mode, replacing any shortcodes with marker elements so that later we can // First we operate in text mode, replacing any shortcodes with marker elements so that later we can
// use a proper DOM // use a proper DOM
list($content, $tags) = $this->replaceElementTagsWithMarkers($content); list($content, $tags) = $this->replaceElementTagsWithMarkers($content);
@ -561,10 +583,12 @@ class ShortcodeParser extends Object {
user_error('Couldn\'t decode HTML when processing short codes', E_USER_ERRROR); user_error('Couldn\'t decode HTML when processing short codes', E_USER_ERRROR);
} }
else { else {
return $content; $continue = false;
}
} }
} }
if ($continue) {
// First, replace any shortcodes that are in attributes // First, replace any shortcodes that are in attributes
$this->replaceAttributeTagsWithContent($htmlvalue); $this->replaceAttributeTagsWithContent($htmlvalue);
@ -613,6 +637,9 @@ class ShortcodeParser extends Object {
}, },
$content $content
); );
}
$this->extend('onAfterParse', $content);
return $content; return $content;
} }