PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-includes/shortcodes.php

https://gitlab.com/darmawan.fatria/df-skp-2014
PHP | 557 lines | 253 code | 49 blank | 255 comment | 44 complexity | 2c0ed3f995695f095bcdef0eb3c388ca MD5 | raw file
  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. * $out = do_shortcode( $content );
  25. *
  26. * @link https://codex.wordpress.org/Shortcode_API
  27. *
  28. * @package WordPress
  29. * @subpackage Shortcodes
  30. * @since 2.5.0
  31. */
  32. /**
  33. * Container for storing shortcode tags and their hook to call for the shortcode
  34. *
  35. * @since 2.5.0
  36. *
  37. * @name $shortcode_tags
  38. * @var array
  39. * @global array $shortcode_tags
  40. */
  41. $shortcode_tags = array();
  42. /**
  43. * Add hook for shortcode tag.
  44. *
  45. * There can only be one hook for each shortcode. Which means that if another
  46. * plugin has a similar shortcode, it will override yours or yours will override
  47. * theirs depending on which order the plugins are included and/or ran.
  48. *
  49. * Simplest example of a shortcode tag using the API:
  50. *
  51. * // [footag foo="bar"]
  52. * function footag_func( $atts ) {
  53. * return "foo = {
  54. * $atts[foo]
  55. * }";
  56. * }
  57. * add_shortcode( 'footag', 'footag_func' );
  58. *
  59. * Example with nice attribute defaults:
  60. *
  61. * // [bartag foo="bar"]
  62. * function bartag_func( $atts ) {
  63. * $args = shortcode_atts( array(
  64. * 'foo' => 'no foo',
  65. * 'baz' => 'default baz',
  66. * ), $atts );
  67. *
  68. * return "foo = {$args['foo']}";
  69. * }
  70. * add_shortcode( 'bartag', 'bartag_func' );
  71. *
  72. * Example with enclosed content:
  73. *
  74. * // [baztag]content[/baztag]
  75. * function baztag_func( $atts, $content = '' ) {
  76. * return "content = $content";
  77. * }
  78. * add_shortcode( 'baztag', 'baztag_func' );
  79. *
  80. * @since 2.5.0
  81. *
  82. * @uses $shortcode_tags
  83. *
  84. * @param string $tag Shortcode tag to be searched in post content.
  85. * @param callable $func Hook to run when shortcode is found.
  86. */
  87. function add_shortcode($tag, $func) {
  88. global $shortcode_tags;
  89. if ( is_callable($func) )
  90. $shortcode_tags[$tag] = $func;
  91. }
  92. /**
  93. * Removes hook for shortcode.
  94. *
  95. * @since 2.5.0
  96. *
  97. * @uses $shortcode_tags
  98. *
  99. * @param string $tag Shortcode tag to remove hook for.
  100. */
  101. function remove_shortcode($tag) {
  102. global $shortcode_tags;
  103. unset($shortcode_tags[$tag]);
  104. }
  105. /**
  106. * Clear all shortcodes.
  107. *
  108. * This function is simple, it clears all of the shortcode tags by replacing the
  109. * shortcodes global by a empty array. This is actually a very efficient method
  110. * for removing all shortcodes.
  111. *
  112. * @since 2.5.0
  113. *
  114. * @uses $shortcode_tags
  115. */
  116. function remove_all_shortcodes() {
  117. global $shortcode_tags;
  118. $shortcode_tags = array();
  119. }
  120. /**
  121. * Whether a registered shortcode exists named $tag
  122. *
  123. * @since 3.6.0
  124. *
  125. * @global array $shortcode_tags List of shortcode tags and their callback hooks.
  126. *
  127. * @param string $tag Shortcode tag to check.
  128. * @return bool Whether the given shortcode exists.
  129. */
  130. function shortcode_exists( $tag ) {
  131. global $shortcode_tags;
  132. return array_key_exists( $tag, $shortcode_tags );
  133. }
  134. /**
  135. * Whether the passed content contains the specified shortcode
  136. *
  137. * @since 3.6.0
  138. *
  139. * @global array $shortcode_tags
  140. *
  141. * @param string $content Content to search for shortcodes.
  142. * @param string $tag Shortcode tag to check.
  143. * @return bool Whether the passed content contains the given shortcode.
  144. */
  145. function has_shortcode( $content, $tag ) {
  146. if ( false === strpos( $content, '[' ) ) {
  147. return false;
  148. }
  149. if ( shortcode_exists( $tag ) ) {
  150. preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
  151. if ( empty( $matches ) )
  152. return false;
  153. foreach ( $matches as $shortcode ) {
  154. if ( $tag === $shortcode[2] ) {
  155. return true;
  156. } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * Search content for shortcodes and filter shortcodes through their hooks.
  165. *
  166. * If there are no shortcode tags defined, then the content will be returned
  167. * without any filtering. This might cause issues when plugins are disabled but
  168. * the shortcode will still show up in the post or content.
  169. *
  170. * @since 2.5.0
  171. *
  172. * @global array $shortcode_tags List of shortcode tags and their callback hooks.
  173. *
  174. * @param string $content Content to search for shortcodes.
  175. * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
  176. * @return string Content with shortcodes filtered out.
  177. */
  178. function do_shortcode( $content, $ignore_html = false ) {
  179. global $shortcode_tags;
  180. if ( false === strpos( $content, '[' ) ) {
  181. return $content;
  182. }
  183. if (empty($shortcode_tags) || !is_array($shortcode_tags))
  184. return $content;
  185. $tagnames = array_keys($shortcode_tags);
  186. $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
  187. $pattern = "/\\[($tagregexp)/s";
  188. if ( 1 !== preg_match( $pattern, $content ) ) {
  189. // Avoids parsing HTML when there are no shortcodes or embeds anyway.
  190. return $content;
  191. }
  192. $content = do_shortcodes_in_html_tags( $content, $ignore_html );
  193. $pattern = get_shortcode_regex();
  194. $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
  195. // Always restore square braces so we don't break things like <!--[if IE ]>
  196. $content = unescape_invalid_shortcodes( $content );
  197. return $content;
  198. }
  199. /**
  200. * Retrieve the shortcode regular expression for searching.
  201. *
  202. * The regular expression combines the shortcode tags in the regular expression
  203. * in a regex class.
  204. *
  205. * The regular expression contains 6 different sub matches to help with parsing.
  206. *
  207. * 1 - An extra [ to allow for escaping shortcodes with double [[]]
  208. * 2 - The shortcode name
  209. * 3 - The shortcode argument list
  210. * 4 - The self closing /
  211. * 5 - The content of a shortcode when it wraps some content.
  212. * 6 - An extra ] to allow for escaping shortcodes with double [[]]
  213. *
  214. * @since 2.5.0
  215. *
  216. * @uses $shortcode_tags
  217. *
  218. * @return string The shortcode search regular expression
  219. */
  220. function get_shortcode_regex() {
  221. global $shortcode_tags;
  222. $tagnames = array_keys($shortcode_tags);
  223. $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
  224. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
  225. // Also, see shortcode_unautop() and shortcode.js.
  226. return
  227. '\\[' // Opening bracket
  228. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
  229. . "($tagregexp)" // 2: Shortcode name
  230. . '(?![\\w-])' // Not followed by word character or hyphen
  231. . '(' // 3: Unroll the loop: Inside the opening shortcode tag
  232. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  233. . '(?:'
  234. . '\\/(?!\\])' // A forward slash not followed by a closing bracket
  235. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  236. . ')*?'
  237. . ')'
  238. . '(?:'
  239. . '(\\/)' // 4: Self closing tag ...
  240. . '\\]' // ... and closing bracket
  241. . '|'
  242. . '\\]' // Closing bracket
  243. . '(?:'
  244. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  245. . '[^\\[]*+' // Not an opening bracket
  246. . '(?:'
  247. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  248. . '[^\\[]*+' // Not an opening bracket
  249. . ')*+'
  250. . ')'
  251. . '\\[\\/\\2\\]' // Closing shortcode tag
  252. . ')?'
  253. . ')'
  254. . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
  255. }
  256. /**
  257. * Regular Expression callable for do_shortcode() for calling shortcode hook.
  258. * @see get_shortcode_regex for details of the match array contents.
  259. *
  260. * @since 2.5.0
  261. * @access private
  262. * @uses $shortcode_tags
  263. *
  264. * @param array $m Regular expression match array
  265. * @return mixed False on failure.
  266. */
  267. function do_shortcode_tag( $m ) {
  268. global $shortcode_tags;
  269. // allow [[foo]] syntax for escaping a tag
  270. if ( $m[1] == '[' && $m[6] == ']' ) {
  271. return substr($m[0], 1, -1);
  272. }
  273. $tag = $m[2];
  274. $attr = shortcode_parse_atts( $m[3] );
  275. if ( isset( $m[5] ) ) {
  276. // enclosing tag - extra parameter
  277. return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
  278. } else {
  279. // self-closing tag
  280. return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null, $tag ) . $m[6];
  281. }
  282. }
  283. /**
  284. * Search only inside HTML elements for shortcodes and process them.
  285. *
  286. * Any [ or ] characters remaining inside elements will be HTML encoded
  287. * to prevent interference with shortcodes that are outside the elements.
  288. * Assumes $content processed by KSES already. Users with unfiltered_html
  289. * capability may get unexpected output if angle braces are nested in tags.
  290. *
  291. * @since 4.2.3
  292. *
  293. * @param string $content Content to search for shortcodes
  294. * @param bool $ignore_html When true, all square braces inside elements will be encoded.
  295. * @return string Content with shortcodes filtered out.
  296. */
  297. function do_shortcodes_in_html_tags( $content, $ignore_html ) {
  298. // Normalize entities in unfiltered HTML before adding placeholders.
  299. $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
  300. $content = strtr( $content, $trans );
  301. $trans = array( '[' => '&#91;', ']' => '&#93;' );
  302. $pattern = get_shortcode_regex();
  303. $textarr = wp_html_split( $content );
  304. foreach ( $textarr as &$element ) {
  305. if ( '' == $element || '<' !== $element[0] ) {
  306. continue;
  307. }
  308. $noopen = false === strpos( $element, '[' );
  309. $noclose = false === strpos( $element, ']' );
  310. if ( $noopen || $noclose ) {
  311. // This element does not contain shortcodes.
  312. if ( $noopen xor $noclose ) {
  313. // Need to encode stray [ or ] chars.
  314. $element = strtr( $element, $trans );
  315. }
  316. continue;
  317. }
  318. if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
  319. // Encode all [ and ] chars.
  320. $element = strtr( $element, $trans );
  321. continue;
  322. }
  323. $attributes = wp_kses_attr_parse( $element );
  324. if ( false === $attributes ) {
  325. // Some plugins are doing things like [name] <[email]>.
  326. if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) {
  327. $element = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $element );
  328. }
  329. // Looks like we found some crazy unfiltered HTML. Skipping it for sanity.
  330. $element = strtr( $element, $trans );
  331. continue;
  332. }
  333. // Get element name
  334. $front = array_shift( $attributes );
  335. $back = array_pop( $attributes );
  336. $matches = array();
  337. preg_match('%[a-zA-Z0-9]+%', $front, $matches);
  338. $elname = $matches[0];
  339. // Look for shortcodes in each attribute separately.
  340. foreach ( $attributes as &$attr ) {
  341. $open = strpos( $attr, '[' );
  342. $close = strpos( $attr, ']' );
  343. if ( false === $open || false === $close ) {
  344. continue; // Go to next attribute. Square braces will be escaped at end of loop.
  345. }
  346. $double = strpos( $attr, '"' );
  347. $single = strpos( $attr, "'" );
  348. if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
  349. // $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
  350. // In this specific situation we assume KSES did not run because the input
  351. // was written by an administrator, so we should avoid changing the output
  352. // and we do not need to run KSES here.
  353. $attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr );
  354. } else {
  355. // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'"
  356. // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
  357. $count = 0;
  358. $new_attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr, -1, $count );
  359. if ( $count > 0 ) {
  360. // Sanitize the shortcode output using KSES.
  361. $new_attr = wp_kses_one_attr( $new_attr, $elname );
  362. if ( '' !== $new_attr ) {
  363. // The shortcode is safe to use now.
  364. $attr = $new_attr;
  365. }
  366. }
  367. }
  368. }
  369. $element = $front . implode( '', $attributes ) . $back;
  370. // Now encode any remaining [ or ] chars.
  371. $element = strtr( $element, $trans );
  372. }
  373. $content = implode( '', $textarr );
  374. return $content;
  375. }
  376. /**
  377. * Remove placeholders added by do_shortcodes_in_html_tags().
  378. *
  379. * @since 4.2.3
  380. *
  381. * @param string $content Content to search for placeholders.
  382. * @return string Content with placeholders removed.
  383. */
  384. function unescape_invalid_shortcodes( $content ) {
  385. // Clean up entire string, avoids re-parsing HTML.
  386. $trans = array( '&#91;' => '[', '&#93;' => ']' );
  387. $content = strtr( $content, $trans );
  388. return $content;
  389. }
  390. /**
  391. * Retrieve all attributes from the shortcodes tag.
  392. *
  393. * The attributes list has the attribute name as the key and the value of the
  394. * attribute as the value in the key/value pair. This allows for easier
  395. * retrieval of the attributes, since all attributes have to be known.
  396. *
  397. * @since 2.5.0
  398. *
  399. * @param string $text
  400. * @return array List of attributes and their value.
  401. */
  402. function shortcode_parse_atts($text) {
  403. $atts = array();
  404. $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
  405. $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
  406. if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
  407. foreach ($match as $m) {
  408. if (!empty($m[1]))
  409. $atts[strtolower($m[1])] = stripcslashes($m[2]);
  410. elseif (!empty($m[3]))
  411. $atts[strtolower($m[3])] = stripcslashes($m[4]);
  412. elseif (!empty($m[5]))
  413. $atts[strtolower($m[5])] = stripcslashes($m[6]);
  414. elseif (isset($m[7]) && strlen($m[7]))
  415. $atts[] = stripcslashes($m[7]);
  416. elseif (isset($m[8]))
  417. $atts[] = stripcslashes($m[8]);
  418. }
  419. // Reject any unclosed HTML elements
  420. foreach( $atts as &$value ) {
  421. if ( false !== strpos( $value, '<' ) ) {
  422. if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
  423. $value = '';
  424. }
  425. }
  426. }
  427. } else {
  428. $atts = ltrim($text);
  429. }
  430. return $atts;
  431. }
  432. /**
  433. * Combine user attributes with known attributes and fill in defaults when needed.
  434. *
  435. * The pairs should be considered to be all of the attributes which are
  436. * supported by the caller and given as a list. The returned attributes will
  437. * only contain the attributes in the $pairs list.
  438. *
  439. * If the $atts list has unsupported attributes, then they will be ignored and
  440. * removed from the final returned list.
  441. *
  442. * @since 2.5.0
  443. *
  444. * @param array $pairs Entire list of supported attributes and their defaults.
  445. * @param array $atts User defined attributes in shortcode tag.
  446. * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
  447. * @return array Combined and filtered attribute list.
  448. */
  449. function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
  450. $atts = (array)$atts;
  451. $out = array();
  452. foreach($pairs as $name => $default) {
  453. if ( array_key_exists($name, $atts) )
  454. $out[$name] = $atts[$name];
  455. else
  456. $out[$name] = $default;
  457. }
  458. /**
  459. * Filter a shortcode's default attributes.
  460. *
  461. * If the third parameter of the shortcode_atts() function is present then this filter is available.
  462. * The third parameter, $shortcode, is the name of the shortcode.
  463. *
  464. * @since 3.6.0
  465. *
  466. * @param array $out The output array of shortcode attributes.
  467. * @param array $pairs The supported attributes and their defaults.
  468. * @param array $atts The user defined shortcode attributes.
  469. */
  470. if ( $shortcode )
  471. $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );
  472. return $out;
  473. }
  474. /**
  475. * Remove all shortcode tags from the given content.
  476. *
  477. * @since 2.5.0
  478. *
  479. * @uses $shortcode_tags
  480. *
  481. * @param string $content Content to remove shortcode tags.
  482. * @return string Content without shortcode tags.
  483. */
  484. function strip_shortcodes( $content ) {
  485. global $shortcode_tags;
  486. if ( false === strpos( $content, '[' ) ) {
  487. return $content;
  488. }
  489. if (empty($shortcode_tags) || !is_array($shortcode_tags))
  490. return $content;
  491. $content = do_shortcodes_in_html_tags( $content, true );
  492. $pattern = get_shortcode_regex();
  493. $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
  494. // Always restore square braces so we don't break things like <!--[if IE ]>
  495. $content = unescape_invalid_shortcodes( $content );
  496. return $content;
  497. }
  498. function strip_shortcode_tag( $m ) {
  499. // allow [[foo]] syntax for escaping a tag
  500. if ( $m[1] == '[' && $m[6] == ']' ) {
  501. return substr($m[0], 1, -1);
  502. }
  503. return $m[1] . $m[6];
  504. }