/wp-content/plugins/contact-form-7/includes/shortcodes.php

https://github.com/sharpmachine/wakeupmedia.com · PHP · 195 lines · 143 code · 48 blank · 4 comment · 27 complexity · 43ea9b100ca6a2b74ee4452f6879be3d MD5 · raw file

  1. <?php
  2. class WPCF7_ShortcodeManager {
  3. var $shortcode_tags = array();
  4. // Taggs scanned at the last time of do_shortcode()
  5. var $scanned_tags = null;
  6. // Executing shortcodes (true) or just scanning (false)
  7. var $exec = true;
  8. function add_shortcode( $tag, $func, $has_name = false ) {
  9. if ( is_callable( $func ) )
  10. $this->shortcode_tags[$tag] = array(
  11. 'function' => $func,
  12. 'has_name' => (boolean) $has_name );
  13. }
  14. function remove_shortcode( $tag ) {
  15. unset( $this->shortcode_tags[$tag] );
  16. }
  17. function normalize_shortcode( $content ) {
  18. if ( empty( $this->shortcode_tags ) || ! is_array( $this->shortcode_tags ) )
  19. return $content;
  20. $pattern = $this->get_shortcode_regex();
  21. return preg_replace_callback( '/' . $pattern . '/s',
  22. array( &$this, 'normalize_space_cb' ), $content );
  23. }
  24. function normalize_space_cb( $m ) {
  25. // allow [[foo]] syntax for escaping a tag
  26. if ( $m[1] == '[' && $m[6] == ']' )
  27. return $m[0];
  28. $tag = $m[2];
  29. $attr = trim( preg_replace( '/[\r\n\t ]+/', ' ', $m[3] ) );
  30. $content = trim( $m[5] );
  31. $content = str_replace( "\n", '<WPPreserveNewline />', $content );
  32. $result = $m[1] . '[' . $tag
  33. . ( $attr ? ' ' . $attr : '' )
  34. . ( $m[4] ? ' ' . $m[4] : '' )
  35. . ']'
  36. . ( $content ? $content . '[/' . $tag . ']' : '' )
  37. . $m[6];
  38. return $result;
  39. }
  40. function do_shortcode( $content, $exec = true ) {
  41. $this->exec = (bool) $exec;
  42. $this->scanned_tags = array();
  43. if ( empty( $this->shortcode_tags ) || ! is_array( $this->shortcode_tags ) )
  44. return $content;
  45. $pattern = $this->get_shortcode_regex();
  46. return preg_replace_callback( '/' . $pattern . '/s',
  47. array( &$this, 'do_shortcode_tag' ), $content );
  48. }
  49. function scan_shortcode( $content ) {
  50. $this->do_shortcode( $content, false );
  51. return $this->scanned_tags;
  52. }
  53. function get_shortcode_regex() {
  54. $tagnames = array_keys( $this->shortcode_tags );
  55. $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
  56. return '(\[?)'
  57. . '\[(' . $tagregexp . ')(?:[\r\n\t ](.*?))?(?:[\r\n\t ](\/))?\]'
  58. . '(?:([^[]*?)\[\/\2\])?'
  59. . '(\]?)';
  60. }
  61. function do_shortcode_tag( $m ) {
  62. // allow [[foo]] syntax for escaping a tag
  63. if ( $m[1] == '[' && $m[6] == ']' ) {
  64. return substr( $m[0], 1, -1 );
  65. }
  66. $tag = $m[2];
  67. $attr = $this->shortcode_parse_atts( $m[3] );
  68. $scanned_tag = array(
  69. 'type' => $tag,
  70. 'name' => '',
  71. 'options' => array(),
  72. 'raw_values' => array(),
  73. 'values' => array(),
  74. 'pipes' => null,
  75. 'labels' => array(),
  76. 'attr' => '',
  77. 'content' => '' );
  78. if ( is_array( $attr ) ) {
  79. if ( is_array( $attr['options'] ) ) {
  80. if ( $this->shortcode_tags[$tag]['has_name'] && ! empty( $attr['options'] ) ) {
  81. $scanned_tag['name'] = array_shift( $attr['options'] );
  82. if ( ! wpcf7_is_name( $scanned_tag['name'] ) )
  83. return $m[0]; // Invalid name is used. Ignore this tag.
  84. }
  85. $scanned_tag['options'] = (array) $attr['options'];
  86. }
  87. $scanned_tag['raw_values'] = (array) $attr['values'];
  88. if ( WPCF7_USE_PIPE ) {
  89. $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] );
  90. $scanned_tag['values'] = $pipes->collect_befores();
  91. $scanned_tag['pipes'] = $pipes;
  92. } else {
  93. $scanned_tag['values'] = $scanned_tag['raw_values'];
  94. }
  95. $scanned_tag['labels'] = $scanned_tag['values'];
  96. } else {
  97. $scanned_tag['attr'] = $attr;
  98. }
  99. $content = trim( $m[5] );
  100. $content = preg_replace( "/<br[\r\n\t ]*\/?>$/m", '', $content );
  101. $scanned_tag['content'] = $content;
  102. $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag, $this->exec );
  103. $this->scanned_tags[] = $scanned_tag;
  104. if ( $this->exec ) {
  105. $func = $this->shortcode_tags[$tag]['function'];
  106. return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6];
  107. } else {
  108. return $m[0];
  109. }
  110. }
  111. function shortcode_parse_atts( $text ) {
  112. $atts = array( 'options' => array(), 'values' => array() );
  113. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text );
  114. $text = stripcslashes( trim( $text ) );
  115. $pattern = '%^([-+*=0-9a-zA-Z:.!?#$&@_/|\%\r\n\t ]*?)((?:[\r\n\t ]*"[^"]*"|[\r\n\t ]*\'[^\']*\')*)$%';
  116. if ( preg_match( $pattern, $text, $match ) ) {
  117. if ( ! empty( $match[1] ) ) {
  118. $atts['options'] = preg_split( '/[\r\n\t ]+/', trim( $match[1] ) );
  119. }
  120. if ( ! empty( $match[2] ) ) {
  121. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values );
  122. $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] );
  123. }
  124. } else {
  125. $atts = $text;
  126. }
  127. return $atts;
  128. }
  129. }
  130. $wpcf7_shortcode_manager = new WPCF7_ShortcodeManager();
  131. function wpcf7_add_shortcode( $tag, $func, $has_name = false ) {
  132. global $wpcf7_shortcode_manager;
  133. return $wpcf7_shortcode_manager->add_shortcode( $tag, $func, $has_name );
  134. }
  135. function wpcf7_remove_shortcode( $tag ) {
  136. global $wpcf7_shortcode_manager;
  137. return $wpcf7_shortcode_manager->remove_shortcode( $tag );
  138. }
  139. function wpcf7_do_shortcode( $content ) {
  140. global $wpcf7_shortcode_manager;
  141. return $wpcf7_shortcode_manager->do_shortcode( $content );
  142. }
  143. function wpcf7_get_shortcode_regex() {
  144. global $wpcf7_shortcode_manager;
  145. return $wpcf7_shortcode_manager->get_shortcode_regex();
  146. }
  147. ?>