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

https://gitlab.com/VTTE/sitios-vtte · PHP · 368 lines · 280 code · 86 blank · 2 comment · 46 complexity · 690d2444f2ae6ac87309270cefd6f763 MD5 · raw file

  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. $feature = array_filter( (array) $feature );
  63. if ( isset( $this->tag_types[$tag]['features'] ) ) {
  64. return (bool) array_intersect(
  65. array_keys( array_filter( $this->tag_types[$tag]['features'] ) ),
  66. $feature );
  67. }
  68. return false;
  69. }
  70. public function collect_tag_types( $feature = null, $invert = false ) {
  71. $tag_types = array_keys( $this->tag_types );
  72. if ( empty( $feature ) ) {
  73. return $tag_types;
  74. }
  75. $output = array();
  76. foreach ( $tag_types as $tag ) {
  77. if ( ! $invert && $this->tag_type_supports( $tag, $feature )
  78. || $invert && ! $this->tag_type_supports( $tag, $feature ) ) {
  79. $output[] = $tag;
  80. }
  81. }
  82. return $output;
  83. }
  84. private function sanitize_tag_type( $tag ) {
  85. $tag = preg_replace( '/[^a-zA-Z0-9_*]+/', '_', $tag );
  86. $tag = rtrim( $tag, '_' );
  87. $tag = strtolower( $tag );
  88. return $tag;
  89. }
  90. public function remove( $tag ) {
  91. unset( $this->tag_types[$tag] );
  92. }
  93. public function normalize( $content ) {
  94. if ( empty( $this->tag_types ) ) {
  95. return $content;
  96. }
  97. $content = preg_replace_callback(
  98. '/' . $this->tag_regex() . '/s',
  99. array( $this, 'normalize_callback' ),
  100. $content );
  101. return $content;
  102. }
  103. private function normalize_callback( $m ) {
  104. // allow [[foo]] syntax for escaping a tag
  105. if ( $m[1] == '['
  106. and $m[6] == ']' ) {
  107. return $m[0];
  108. }
  109. $tag = $m[2];
  110. $attr = trim( preg_replace( '/[\r\n\t ]+/', ' ', $m[3] ) );
  111. $attr = strtr( $attr, array( '<' => '&lt;', '>' => '&gt;' ) );
  112. $content = trim( $m[5] );
  113. $content = str_replace( "\n", '<WPPreserveNewline />', $content );
  114. $result = $m[1] . '[' . $tag
  115. . ( $attr ? ' ' . $attr : '' )
  116. . ( $m[4] ? ' ' . $m[4] : '' )
  117. . ']'
  118. . ( $content ? $content . '[/' . $tag . ']' : '' )
  119. . $m[6];
  120. return $result;
  121. }
  122. public function replace_all( $content ) {
  123. return $this->scan( $content, true );
  124. }
  125. public function scan( $content, $replace = false ) {
  126. $this->scanned_tags = array();
  127. if ( empty( $this->tag_types ) ) {
  128. if ( $replace ) {
  129. return $content;
  130. } else {
  131. return $this->scanned_tags;
  132. }
  133. }
  134. if ( $replace ) {
  135. $content = preg_replace_callback(
  136. '/' . $this->tag_regex() . '/s',
  137. array( $this, 'replace_callback' ),
  138. $content );
  139. return $content;
  140. } else {
  141. preg_replace_callback(
  142. '/' . $this->tag_regex() . '/s',
  143. array( $this, 'scan_callback' ),
  144. $content );
  145. return $this->scanned_tags;
  146. }
  147. }
  148. public function filter( $input, $cond ) {
  149. if ( is_array( $input ) ) {
  150. $tags = $input;
  151. } elseif ( is_string( $input ) ) {
  152. $tags = $this->scan( $input );
  153. } else {
  154. $tags = $this->scanned_tags;
  155. }
  156. if ( empty( $tags ) ) {
  157. return array();
  158. }
  159. $cond = wp_parse_args( $cond, array(
  160. 'type' => array(),
  161. 'name' => array(),
  162. 'feature' => '',
  163. ) );
  164. $type = array_filter( (array) $cond['type'] );
  165. $name = array_filter( (array) $cond['name'] );
  166. $feature = is_string( $cond['feature'] ) ? trim( $cond['feature'] ) : '';
  167. if ( '!' == substr( $feature, 0, 1 ) ) {
  168. $feature_negative = true;
  169. $feature = trim( substr( $feature, 1 ) );
  170. } else {
  171. $feature_negative = false;
  172. }
  173. $output = array();
  174. foreach ( $tags as $tag ) {
  175. $tag = new WPCF7_FormTag( $tag );
  176. if ( $type and ! in_array( $tag->type, $type, true ) ) {
  177. continue;
  178. }
  179. if ( $name and ! in_array( $tag->name, $name, true ) ) {
  180. continue;
  181. }
  182. if ( $feature ) {
  183. if ( ! $this->tag_type_supports( $tag->type, $feature )
  184. and ! $feature_negative ) {
  185. continue;
  186. } elseif ( $this->tag_type_supports( $tag->type, $feature )
  187. and $feature_negative ) {
  188. continue;
  189. }
  190. }
  191. $output[] = $tag;
  192. }
  193. return $output;
  194. }
  195. private function tag_regex() {
  196. $tagnames = array_keys( $this->tag_types );
  197. $tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) );
  198. return '(\[?)'
  199. . '\[(' . $tagregexp . ')(?:[\r\n\t ](.*?))?(?:[\r\n\t ](\/))?\]'
  200. . '(?:([^[]*?)\[\/\2\])?'
  201. . '(\]?)';
  202. }
  203. private function replace_callback( $m ) {
  204. return $this->scan_callback( $m, true );
  205. }
  206. private function scan_callback( $m, $replace = false ) {
  207. // allow [[foo]] syntax for escaping a tag
  208. if ( $m[1] == '['
  209. and $m[6] == ']' ) {
  210. return substr( $m[0], 1, -1 );
  211. }
  212. $tag = $m[2];
  213. $attr = $this->parse_atts( $m[3] );
  214. $scanned_tag = array(
  215. 'type' => $tag,
  216. 'basetype' => trim( $tag, '*' ),
  217. 'name' => '',
  218. 'options' => array(),
  219. 'raw_values' => array(),
  220. 'values' => array(),
  221. 'pipes' => null,
  222. 'labels' => array(),
  223. 'attr' => '',
  224. 'content' => '',
  225. );
  226. if ( is_array( $attr ) ) {
  227. if ( is_array( $attr['options'] ) ) {
  228. if ( $this->tag_type_supports( $tag, 'name-attr' )
  229. and ! empty( $attr['options'] ) ) {
  230. $scanned_tag['name'] = array_shift( $attr['options'] );
  231. if ( ! wpcf7_is_name( $scanned_tag['name'] ) ) {
  232. return $m[0]; // Invalid name is used. Ignore this tag.
  233. }
  234. }
  235. $scanned_tag['options'] = (array) $attr['options'];
  236. }
  237. $scanned_tag['raw_values'] = (array) $attr['values'];
  238. if ( WPCF7_USE_PIPE ) {
  239. $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] );
  240. $scanned_tag['values'] = $pipes->collect_befores();
  241. $scanned_tag['pipes'] = $pipes;
  242. } else {
  243. $scanned_tag['values'] = $scanned_tag['raw_values'];
  244. }
  245. $scanned_tag['labels'] = $scanned_tag['values'];
  246. } else {
  247. $scanned_tag['attr'] = $attr;
  248. }
  249. $scanned_tag['values'] = array_map( 'trim', $scanned_tag['values'] );
  250. $scanned_tag['labels'] = array_map( 'trim', $scanned_tag['labels'] );
  251. $content = trim( $m[5] );
  252. $content = preg_replace( "/<br[\r\n\t ]*\/?>$/m", '', $content );
  253. $scanned_tag['content'] = $content;
  254. $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag, $replace );
  255. $scanned_tag = new WPCF7_FormTag( $scanned_tag );
  256. $this->scanned_tags[] = $scanned_tag;
  257. if ( $replace ) {
  258. $func = $this->tag_types[$tag]['function'];
  259. return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6];
  260. } else {
  261. return $m[0];
  262. }
  263. }
  264. private function parse_atts( $text ) {
  265. $atts = array( 'options' => array(), 'values' => array() );
  266. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text );
  267. $text = stripcslashes( trim( $text ) );
  268. $pattern = '%^([-+*=0-9a-zA-Z:.!?#$&@_/|\%\r\n\t ]*?)((?:[\r\n\t ]*"[^"]*"|[\r\n\t ]*\'[^\']*\')*)$%';
  269. if ( preg_match( $pattern, $text, $match ) ) {
  270. if ( ! empty( $match[1] ) ) {
  271. $atts['options'] = preg_split( '/[\r\n\t ]+/', trim( $match[1] ) );
  272. }
  273. if ( ! empty( $match[2] ) ) {
  274. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values );
  275. $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] );
  276. }
  277. } else {
  278. $atts = $text;
  279. }
  280. return $atts;
  281. }
  282. }