PageRenderTime 54ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/shortcodes.php

https://bitbucket.org/aqge/deptandashboard
PHP | 336 lines | 117 code | 26 blank | 193 comment | 19 complexity | e53c70bf3db15abea4bc15c8bd52a363 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress API for creating bbcode like tags or what WordPress calls
  4. * "shortcodes." The tag and attribute parsing or regular expression code is
  5. * based on the Textpattern tag parser.
  6. *
  7. * A few examples are below:
  8. *
  9. * [shortcode /]
  10. * [shortcode foo="bar" baz="bing" /]
  11. * [shortcode foo="bar"]content[/shortcode]
  12. *
  13. * Shortcode tags support attributes and enclosed content, but does not entirely
  14. * support inline shortcodes in other shortcodes. You will have to call the
  15. * shortcode parser in your function to account for that.
  16. *
  17. * {@internal
  18. * Please be aware that the above note was made during the beta of WordPress 2.6
  19. * and in the future may not be accurate. Please update the note when it is no
  20. * longer the case.}}
  21. *
  22. * To apply shortcode tags to content:
  23. *
  24. * <code>
  25. * $out = do_shortcode($content);
  26. * </code>
  27. *
  28. * @link http://codex.wordpress.org/Shortcode_API
  29. *
  30. * @package WordPress
  31. * @subpackage Shortcodes
  32. * @since 2.5
  33. */
  34. /**
  35. * Container for storing shortcode tags and their hook to call for the shortcode
  36. *
  37. * @since 2.5
  38. * @name $shortcode_tags
  39. * @var array
  40. * @global array $shortcode_tags
  41. */
  42. $shortcode_tags = array();
  43. /**
  44. * Add hook for shortcode tag.
  45. *
  46. * There can only be one hook for each shortcode. Which means that if another
  47. * plugin has a similar shortcode, it will override yours or yours will override
  48. * theirs depending on which order the plugins are included and/or ran.
  49. *
  50. * Simplest example of a shortcode tag using the API:
  51. *
  52. * <code>
  53. * // [footag foo="bar"]
  54. * function footag_func($atts) {
  55. * return "foo = {$atts[foo]}";
  56. * }
  57. * add_shortcode('footag', 'footag_func');
  58. * </code>
  59. *
  60. * Example with nice attribute defaults:
  61. *
  62. * <code>
  63. * // [bartag foo="bar"]
  64. * function bartag_func($atts) {
  65. * extract(shortcode_atts(array(
  66. * 'foo' => 'no foo',
  67. * 'baz' => 'default baz',
  68. * ), $atts));
  69. *
  70. * return "foo = {$foo}";
  71. * }
  72. * add_shortcode('bartag', 'bartag_func');
  73. * </code>
  74. *
  75. * Example with enclosed content:
  76. *
  77. * <code>
  78. * // [baztag]content[/baztag]
  79. * function baztag_func($atts, $content='') {
  80. * return "content = $content";
  81. * }
  82. * add_shortcode('baztag', 'baztag_func');
  83. * </code>
  84. *
  85. * @since 2.5
  86. * @uses $shortcode_tags
  87. *
  88. * @param string $tag Shortcode tag to be searched in post content.
  89. * @param callable $func Hook to run when shortcode is found.
  90. */
  91. function add_shortcode($tag, $func) {
  92. global $shortcode_tags;
  93. if ( is_callable($func) )
  94. $shortcode_tags[$tag] = $func;
  95. }
  96. /**
  97. * Removes hook for shortcode.
  98. *
  99. * @since 2.5
  100. * @uses $shortcode_tags
  101. *
  102. * @param string $tag shortcode tag to remove hook for.
  103. */
  104. function remove_shortcode($tag) {
  105. global $shortcode_tags;
  106. unset($shortcode_tags[$tag]);
  107. }
  108. /**
  109. * Clear all shortcodes.
  110. *
  111. * This function is simple, it clears all of the shortcode tags by replacing the
  112. * shortcodes global by a empty array. This is actually a very efficient method
  113. * for removing all shortcodes.
  114. *
  115. * @since 2.5
  116. * @uses $shortcode_tags
  117. */
  118. function remove_all_shortcodes() {
  119. global $shortcode_tags;
  120. $shortcode_tags = array();
  121. }
  122. /**
  123. * Search content for shortcodes and filter shortcodes through their hooks.
  124. *
  125. * If there are no shortcode tags defined, then the content will be returned
  126. * without any filtering. This might cause issues when plugins are disabled but
  127. * the shortcode will still show up in the post or content.
  128. *
  129. * @since 2.5
  130. * @uses $shortcode_tags
  131. * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
  132. *
  133. * @param string $content Content to search for shortcodes
  134. * @return string Content with shortcodes filtered out.
  135. */
  136. function do_shortcode($content) {
  137. global $shortcode_tags;
  138. if (empty($shortcode_tags) || !is_array($shortcode_tags))
  139. return $content;
  140. $pattern = get_shortcode_regex();
  141. return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
  142. }
  143. /**
  144. * Retrieve the shortcode regular expression for searching.
  145. *
  146. * The regular expression combines the shortcode tags in the regular expression
  147. * in a regex class.
  148. *
  149. * The regular expression contains 6 different sub matches to help with parsing.
  150. *
  151. * 1 - An extra [ to allow for escaping shortcodes with double [[]]
  152. * 2 - The shortcode name
  153. * 3 - The shortcode argument list
  154. * 4 - The self closing /
  155. * 5 - The content of a shortcode when it wraps some content.
  156. * 6 - An extra ] to allow for escaping shortcodes with double [[]]
  157. *
  158. * @since 2.5
  159. * @uses $shortcode_tags
  160. *
  161. * @return string The shortcode search regular expression
  162. */
  163. function get_shortcode_regex() {
  164. global $shortcode_tags;
  165. $tagnames = array_keys($shortcode_tags);
  166. $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
  167. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
  168. return
  169. '\\[' // Opening bracket
  170. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
  171. . "($tagregexp)" // 2: Shortcode name
  172. . '\\b' // Word boundary
  173. . '(' // 3: Unroll the loop: Inside the opening shortcode tag
  174. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  175. . '(?:'
  176. . '\\/(?!\\])' // A forward slash not followed by a closing bracket
  177. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  178. . ')*?'
  179. . ')'
  180. . '(?:'
  181. . '(\\/)' // 4: Self closing tag ...
  182. . '\\]' // ... and closing bracket
  183. . '|'
  184. . '\\]' // Closing bracket
  185. . '(?:'
  186. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  187. . '[^\\[]*+' // Not an opening bracket
  188. . '(?:'
  189. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  190. . '[^\\[]*+' // Not an opening bracket
  191. . ')*+'
  192. . ')'
  193. . '\\[\\/\\2\\]' // Closing shortcode tag
  194. . ')?'
  195. . ')'
  196. . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
  197. }
  198. /**
  199. * Regular Expression callable for do_shortcode() for calling shortcode hook.
  200. * @see get_shortcode_regex for details of the match array contents.
  201. *
  202. * @since 2.5
  203. * @access private
  204. * @uses $shortcode_tags
  205. *
  206. * @param array $m Regular expression match array
  207. * @return mixed False on failure.
  208. */
  209. function do_shortcode_tag( $m ) {
  210. global $shortcode_tags;
  211. // allow [[foo]] syntax for escaping a tag
  212. if ( $m[1] == '[' && $m[6] == ']' ) {
  213. return substr($m[0], 1, -1);
  214. }
  215. $tag = $m[2];
  216. $attr = shortcode_parse_atts( $m[3] );
  217. if ( isset( $m[5] ) ) {
  218. // enclosing tag - extra parameter
  219. return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
  220. } else {
  221. // self-closing tag
  222. return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, NULL, $tag ) . $m[6];
  223. }
  224. }
  225. /**
  226. * Retrieve all attributes from the shortcodes tag.
  227. *
  228. * The attributes list has the attribute name as the key and the value of the
  229. * attribute as the value in the key/value pair. This allows for easier
  230. * retrieval of the attributes, since all attributes have to be known.
  231. *
  232. * @since 2.5
  233. *
  234. * @param string $text
  235. * @return array List of attributes and their value.
  236. */
  237. function shortcode_parse_atts($text) {
  238. $atts = array();
  239. $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
  240. $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
  241. if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
  242. foreach ($match as $m) {
  243. if (!empty($m[1]))
  244. $atts[strtolower($m[1])] = stripcslashes($m[2]);
  245. elseif (!empty($m[3]))
  246. $atts[strtolower($m[3])] = stripcslashes($m[4]);
  247. elseif (!empty($m[5]))
  248. $atts[strtolower($m[5])] = stripcslashes($m[6]);
  249. elseif (isset($m[7]) and strlen($m[7]))
  250. $atts[] = stripcslashes($m[7]);
  251. elseif (isset($m[8]))
  252. $atts[] = stripcslashes($m[8]);
  253. }
  254. } else {
  255. $atts = ltrim($text);
  256. }
  257. return $atts;
  258. }
  259. /**
  260. * Combine user attributes with known attributes and fill in defaults when needed.
  261. *
  262. * The pairs should be considered to be all of the attributes which are
  263. * supported by the caller and given as a list. The returned attributes will
  264. * only contain the attributes in the $pairs list.
  265. *
  266. * If the $atts list has unsupported attributes, then they will be ignored and
  267. * removed from the final returned list.
  268. *
  269. * @since 2.5
  270. *
  271. * @param array $pairs Entire list of supported attributes and their defaults.
  272. * @param array $atts User defined attributes in shortcode tag.
  273. * @return array Combined and filtered attribute list.
  274. */
  275. function shortcode_atts($pairs, $atts) {
  276. $atts = (array)$atts;
  277. $out = array();
  278. foreach($pairs as $name => $default) {
  279. if ( array_key_exists($name, $atts) )
  280. $out[$name] = $atts[$name];
  281. else
  282. $out[$name] = $default;
  283. }
  284. return $out;
  285. }
  286. /**
  287. * Remove all shortcode tags from the given content.
  288. *
  289. * @since 2.5
  290. * @uses $shortcode_tags
  291. *
  292. * @param string $content Content to remove shortcode tags.
  293. * @return string Content without shortcode tags.
  294. */
  295. function strip_shortcodes( $content ) {
  296. global $shortcode_tags;
  297. if (empty($shortcode_tags) || !is_array($shortcode_tags))
  298. return $content;
  299. $pattern = get_shortcode_regex();
  300. return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
  301. }
  302. function strip_shortcode_tag( $m ) {
  303. // allow [[foo]] syntax for escaping a tag
  304. if ( $m[1] == '[' && $m[6] == ']' ) {
  305. return substr($m[0], 1, -1);
  306. }
  307. return $m[1] . $m[6];
  308. }
  309. add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
  310. ?>