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

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