Skip to content

Commit 5488d14

Browse files
committed
Shortcode 1.1.0
1 parent 52ff9c6 commit 5488d14

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# v1.0.0, 2015-10-17
2+
* Initial release

LICENSE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22-

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Shortcode
22
The Shortcode class for creating shortcodes.
3+
4+
## License
5+
See [LICENSE](https://github.com/force-components/Shortcode/blob/master/LICENSE)

Shortcode.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Force Components.
5+
*
6+
* (c) Romanenko Sergey / Awilum <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
class Shortcode
13+
{
14+
/**
15+
* Shortcode tags array
16+
*
17+
* @var shortcode_tags
18+
*/
19+
protected static $shortcode_tags = array();
20+
21+
/**
22+
* Protected constructor since this is a static class.
23+
*
24+
* @access protected
25+
*/
26+
protected function __construct()
27+
{
28+
// Nothing here
29+
}
30+
31+
/**
32+
* Add new shortcode
33+
*
34+
* <code>
35+
* function returnSiteUrl() {
36+
* return Option::get('siteurl');
37+
* }
38+
*
39+
* // Add shortcode {siteurl}
40+
* Shortcode::add('siteurl', 'returnSiteUrl');
41+
* </code>
42+
*
43+
* @param string $shortcode Shortcode tag to be searched in content.
44+
* @param string $callback_function The callback function to replace the shortcode with.
45+
*/
46+
public static function add($shortcode, $callback_function)
47+
{
48+
// Redefine vars
49+
$shortcode = (string) $shortcode;
50+
51+
// Add new shortcode
52+
if (is_callable($callback_function)) {
53+
Shortcode::$shortcode_tags[$shortcode] = $callback_function;
54+
}
55+
}
56+
57+
/**
58+
* Remove a specific registered shortcode.
59+
*
60+
* <code>
61+
* Shortcode::delete('shortcode_name');
62+
* </code>
63+
*
64+
* @param string $shortcode Shortcode tag.
65+
*/
66+
public static function delete($shortcode)
67+
{
68+
// Redefine vars
69+
$shortcode = (string) $shortcode;
70+
71+
// Delete shortcode
72+
if (Shortcode::exists($shortcode)) {
73+
unset(Shortcode::$shortcode_tags[$shortcode]);
74+
}
75+
}
76+
77+
/**
78+
* Remove all registered shortcodes.
79+
*
80+
* <code>
81+
* Shortcode::clear();
82+
* </code>
83+
*
84+
*/
85+
public static function clear()
86+
{
87+
Shortcode::$shortcode_tags = array();
88+
}
89+
90+
/**
91+
* Check if a shortcode has been registered.
92+
*
93+
* <code>
94+
* if (Shortcode::exists('shortcode_name')) {
95+
* // do something...
96+
* }
97+
* </code>
98+
*
99+
* @param string $shortcode Shortcode tag.
100+
*/
101+
public static function exists($shortcode)
102+
{
103+
// Redefine vars
104+
$shortcode = (string) $shortcode;
105+
106+
// Check shortcode
107+
return array_key_exists($shortcode, Shortcode::$shortcode_tags);
108+
}
109+
110+
/**
111+
* Parse a string, and replace any registered shortcodes within it with the result of the mapped callback.
112+
*
113+
* <code>
114+
* $content = Shortcode::parse($content);
115+
* </code>
116+
*
117+
* @param string $content Content
118+
* @return string
119+
*/
120+
public static function parse($content)
121+
{
122+
if (! Shortcode::$shortcode_tags) {
123+
return $content;
124+
}
125+
126+
$shortcodes = implode('|', array_map('preg_quote', array_keys(Shortcode::$shortcode_tags)));
127+
$pattern = "/(.?)\{([$shortcodes]+)(.*?)(\/)?\}(?(4)|(?:(.+?)\{\/\s*\\2\s*\}))?(.?)/s";
128+
129+
return preg_replace_callback($pattern, 'Shortcode::_handle', $content);
130+
}
131+
132+
/**
133+
* _handle()
134+
*/
135+
protected static function _handle($matches)
136+
{
137+
$prefix = $matches[1];
138+
$suffix = $matches[6];
139+
$shortcode = $matches[2];
140+
141+
// Allow for escaping shortcodes by enclosing them in {{shortcode}}
142+
if ($prefix == '{' && $suffix == '}') {
143+
return substr($matches[0], 1, -1);
144+
}
145+
146+
$attributes = array(); // Parse attributes into into this array.
147+
148+
if (preg_match_all('/(\w+) *= *(?:([\'"])(.*?)\\2|([^ "\'>]+))/', $matches[3], $match, PREG_SET_ORDER)) {
149+
foreach ($match as $attribute) {
150+
if (! empty($attribute[4])) {
151+
$attributes[strtolower($attribute[1])] = $attribute[4];
152+
} elseif (! empty($attribute[3])) {
153+
$attributes[strtolower($attribute[1])] = $attribute[3];
154+
}
155+
}
156+
}
157+
158+
// Check if this shortcode realy exists then call user function else return empty string
159+
return (isset(Shortcode::$shortcode_tags[$shortcode])) ? $prefix . call_user_func(Shortcode::$shortcode_tags[$shortcode], $attributes, $matches[5], $shortcode) . $suffix : '';
160+
}
161+
}

0 commit comments

Comments
 (0)