PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/includes/form-tags-manager.php

https://bitbucket.org/RickCalder/mcc
PHP | 333 lines | 257 code | 74 blank | 2 comment | 52 complexity | 5a6aa47bcbeba47ad3b2fbebe4a900f2 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0, BSD-3-Clause
  1. <?php
  2. function wpcf7_add_form_tag( $tag, $func, $features = '' ) {
  3. $manager = WPCF7_FormTagsManager::get_instance();
  4. return $manager->add( $tag, $func, $features );
  5. }
  6. function wpcf7_remove_form_tag( $tag ) {
  7. $manager = WPCF7_FormTagsManager::get_instance();
  8. return $manager->remove( $tag );
  9. }
  10. function wpcf7_replace_all_form_tags( $content ) {
  11. $manager = WPCF7_FormTagsManager::get_instance();
  12. return $manager->replace_all( $content );
  13. }
  14. function wpcf7_scan_form_tags( $cond = null ) {
  15. $contact_form = WPCF7_ContactForm::get_current();
  16. if ( $contact_form ) {
  17. return $contact_form->scan_form_tags( $cond );
  18. }
  19. return array();
  20. }
  21. function wpcf7_form_tag_supports( $tag, $feature ) {
  22. $manager = WPCF7_FormTagsManager::get_instance();
  23. return $manager->tag_type_supports( $tag, $feature );
  24. }
  25. class WPCF7_FormTagsManager {
  26. private static $instance;
  27. private $tag_types = array();
  28. private $scanned_tags = null; // Tags scanned at the last time of scan()
  29. private function __construct() {}
  30. public static function get_instance() {
  31. if ( empty( self::$instance ) ) {
  32. self::$instance = new self;
  33. }
  34. return self::$instance;
  35. }
  36. public function get_scanned_tags() {
  37. return $this->scanned_tags;
  38. }
  39. public function add( $tag, $func, $features = '' ) {
  40. if ( ! is_callable( $func ) ) {
  41. return;
  42. }
  43. if ( true === $features ) { // for back-compat
  44. $features = array( 'name-attr' => true );
  45. }
  46. $features = wp_parse_args( $features, array() );
  47. $tags = array_filter( array_unique( (array) $tag ) );
  48. foreach ( $tags as $tag ) {
  49. $tag = $this->sanitize_tag_type( $tag );
  50. if ( ! $this->tag_type_exists( $tag ) ) {
  51. $this->tag_types[$tag] = array(
  52. 'function' => $func,
  53. 'features' => $features,
  54. );
  55. }
  56. }
  57. }
  58. public function tag_type_exists( $tag ) {
  59. return isset( $this->tag_types[$tag] );
  60. }
  61. public function tag_type_supports( $tag, $feature ) {
  62. if ( isset( $this->tag_types[$tag]['features'] ) ) {
  63. return ! empty( $this->tag_types[$tag]['features'][$feature] );
  64. }
  65. return false;
  66. }
  67. private function sanitize_tag_type( $tag ) {
  68. $tag = preg_replace( '/[^a-zA-Z0-9_*]+/', '_', $tag );
  69. $tag = rtrim( $tag, '_' );
  70. $tag = strtolower( $tag );
  71. return $tag;
  72. }
  73. public function remove( $tag ) {
  74. unset( $this->tag_types[$tag] );
  75. }
  76. public function normalize( $content ) {
  77. if ( empty( $this->tag_types ) ) {
  78. return $content;
  79. }
  80. $content = preg_replace_callback(
  81. '/' . $this->tag_regex() . '/s',
  82. array( $this, 'normalize_callback' ),
  83. $content );
  84. return $content;
  85. }
  86. private function normalize_callback( $m ) {
  87. // allow [[foo]] syntax for escaping a tag
  88. if ( $m[1] == '[' && $m[6] == ']' ) {
  89. return $m[0];
  90. }
  91. $tag = $m[2];
  92. $attr = trim( preg_replace( '/[\r\n\t ]+/', ' ', $m[3] ) );
  93. $attr = strtr( $attr, array( '<' => '&lt;', '>' => '&gt;' ) );
  94. $content = trim( $m[5] );
  95. $content = str_replace( "\n", '<WPPreserveNewline />', $content );
  96. $result = $m[1] . '[' . $tag
  97. . ( $attr ? ' ' . $attr : '' )
  98. . ( $m[4] ? ' ' . $m[4] : '' )
  99. . ']'
  100. . ( $content ? $content . '[/' . $tag . ']' : '' )
  101. . $m[6];
  102. return $result;
  103. }
  104. public function replace_all( $content ) {
  105. return $this->scan( $content, true );
  106. }
  107. public function scan( $content, $replace = false ) {
  108. $this->scanned_tags = array();
  109. if ( empty( $this->tag_types ) ) {
  110. if ( $replace ) {
  111. return $content;
  112. } else {
  113. return $this->scanned_tags;
  114. }
  115. }
  116. if ( $replace ) {
  117. $content = preg_replace_callback(
  118. '/' . $this->tag_regex() . '/s',
  119. array( $this, 'replace_callback' ),
  120. $content );
  121. return $content;
  122. } else {
  123. preg_replace_callback(
  124. '/' . $this->tag_regex() . '/s',
  125. array( $this, 'scan_callback' ),
  126. $content );
  127. return $this->scanned_tags;
  128. }
  129. }
  130. public function filter( $content, $cond ) {
  131. if ( is_array( $content ) ) {
  132. $tags = $content;
  133. } elseif ( is_string( $content ) ) {
  134. $tags = $this->scan( $content );
  135. } else {
  136. $tags = $this->scanned_tags;
  137. }
  138. if ( empty( $tags ) ) {
  139. return array();
  140. }
  141. if ( ! is_array( $cond ) || empty( $cond ) ) {
  142. return $tags;
  143. }
  144. for ( $i = 0, $size = count( $tags ); $i < $size; $i++ ) {
  145. if ( isset( $cond['type'] ) ) {
  146. if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
  147. if ( $tags[$i]['type'] != $cond['type'] ) {
  148. unset( $tags[$i] );
  149. continue;
  150. }
  151. } elseif ( is_array( $cond['type'] ) ) {
  152. if ( ! in_array( $tags[$i]['type'], $cond['type'] ) ) {
  153. unset( $tags[$i] );
  154. continue;
  155. }
  156. }
  157. }
  158. if ( isset( $cond['name'] ) ) {
  159. if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
  160. if ( $tags[$i]['name'] != $cond['name'] ) {
  161. unset ( $tags[$i] );
  162. continue;
  163. }
  164. } elseif ( is_array( $cond['name'] ) ) {
  165. if ( ! in_array( $tags[$i]['name'], $cond['name'] ) ) {
  166. unset( $tags[$i] );
  167. continue;
  168. }
  169. }
  170. }
  171. }
  172. return array_values( $tags );
  173. }
  174. private function tag_regex() {
  175. $tagnames = array_keys( $this->tag_types );
  176. $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
  177. return '(\[?)'
  178. . '\[(' . $tagregexp . ')(?:[\r\n\t ](.*?))?(?:[\r\n\t ](\/))?\]'
  179. . '(?:([^[]*?)\[\/\2\])?'
  180. . '(\]?)';
  181. }
  182. private function replace_callback( $m ) {
  183. return $this->scan_callback( $m, true );
  184. }
  185. private function scan_callback( $m, $replace = false ) {
  186. // allow [[foo]] syntax for escaping a tag
  187. if ( $m[1] == '[' && $m[6] == ']' ) {
  188. return substr( $m[0], 1, -1 );
  189. }
  190. $tag = $m[2];
  191. $attr = $this->parse_atts( $m[3] );
  192. $scanned_tag = array(
  193. 'type' => $tag,
  194. 'basetype' => trim( $tag, '*' ),
  195. 'name' => '',
  196. 'options' => array(),
  197. 'raw_values' => array(),
  198. 'values' => array(),
  199. 'pipes' => null,
  200. 'labels' => array(),
  201. 'attr' => '',
  202. 'content' => '',
  203. );
  204. if ( is_array( $attr ) ) {
  205. if ( is_array( $attr['options'] ) ) {
  206. if ( $this->tag_type_supports( $tag, 'name-attr' )
  207. && ! empty( $attr['options'] ) ) {
  208. $scanned_tag['name'] = array_shift( $attr['options'] );
  209. if ( ! wpcf7_is_name( $scanned_tag['name'] ) ) {
  210. return $m[0]; // Invalid name is used. Ignore this tag.
  211. }
  212. }
  213. $scanned_tag['options'] = (array) $attr['options'];
  214. }
  215. $scanned_tag['raw_values'] = (array) $attr['values'];
  216. if ( WPCF7_USE_PIPE ) {
  217. $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] );
  218. $scanned_tag['values'] = $pipes->collect_befores();
  219. $scanned_tag['pipes'] = $pipes;
  220. } else {
  221. $scanned_tag['values'] = $scanned_tag['raw_values'];
  222. }
  223. $scanned_tag['labels'] = $scanned_tag['values'];
  224. } else {
  225. $scanned_tag['attr'] = $attr;
  226. }
  227. $scanned_tag['values'] = array_map( 'trim', $scanned_tag['values'] );
  228. $scanned_tag['labels'] = array_map( 'trim', $scanned_tag['labels'] );
  229. $content = trim( $m[5] );
  230. $content = preg_replace( "/<br[\r\n\t ]*\/?>$/m", '', $content );
  231. $scanned_tag['content'] = $content;
  232. $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag, $replace );
  233. $this->scanned_tags[] = $scanned_tag;
  234. if ( $replace ) {
  235. $func = $this->tag_types[$tag]['function'];
  236. return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6];
  237. } else {
  238. return $m[0];
  239. }
  240. }
  241. private function parse_atts( $text ) {
  242. $atts = array( 'options' => array(), 'values' => array() );
  243. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text );
  244. $text = stripcslashes( trim( $text ) );
  245. $pattern = '%^([-+*=0-9a-zA-Z:.!?#$&@_/|\%\r\n\t ]*?)((?:[\r\n\t ]*"[^"]*"|[\r\n\t ]*\'[^\']*\')*)$%';
  246. if ( preg_match( $pattern, $text, $match ) ) {
  247. if ( ! empty( $match[1] ) ) {
  248. $atts['options'] = preg_split( '/[\r\n\t ]+/', trim( $match[1] ) );
  249. }
  250. if ( ! empty( $match[2] ) ) {
  251. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values );
  252. $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] );
  253. }
  254. } else {
  255. $atts = $text;
  256. }
  257. return $atts;
  258. }
  259. }