PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/pankajmohale/chef2go
PHP | 355 lines | 268 code | 85 blank | 2 comment | 48 complexity | 8768ff488b8db29eeef5ab8119971f06 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] == '[' && $m[6] == ']' ) {
  106. return $m[0];
  107. }
  108. $tag = $m[2];
  109. $attr = trim( preg_replace( '/[\r\n\t ]+/', ' ', $m[3] ) );
  110. $attr = strtr( $attr, array( '<' => '&lt;', '>' => '&gt;' ) );
  111. $content = trim( $m[5] );
  112. $content = str_replace( "\n", '<WPPreserveNewline />', $content );
  113. $result = $m[1] . '[' . $tag
  114. . ( $attr ? ' ' . $attr : '' )
  115. . ( $m[4] ? ' ' . $m[4] : '' )
  116. . ']'
  117. . ( $content ? $content . '[/' . $tag . ']' : '' )
  118. . $m[6];
  119. return $result;
  120. }
  121. public function replace_all( $content ) {
  122. return $this->scan( $content, true );
  123. }
  124. public function scan( $content, $replace = false ) {
  125. $this->scanned_tags = array();
  126. if ( empty( $this->tag_types ) ) {
  127. if ( $replace ) {
  128. return $content;
  129. } else {
  130. return $this->scanned_tags;
  131. }
  132. }
  133. if ( $replace ) {
  134. $content = preg_replace_callback(
  135. '/' . $this->tag_regex() . '/s',
  136. array( $this, 'replace_callback' ),
  137. $content );
  138. return $content;
  139. } else {
  140. preg_replace_callback(
  141. '/' . $this->tag_regex() . '/s',
  142. array( $this, 'scan_callback' ),
  143. $content );
  144. return $this->scanned_tags;
  145. }
  146. }
  147. public function filter( $input, $cond ) {
  148. if ( is_array( $input ) ) {
  149. $tags = $input;
  150. } elseif ( is_string( $input ) ) {
  151. $tags = $this->scan( $input );
  152. } else {
  153. $tags = $this->scanned_tags;
  154. }
  155. if ( empty( $tags ) ) {
  156. return array();
  157. }
  158. $cond = wp_parse_args( $cond, array(
  159. 'type' => array(),
  160. 'name' => array(),
  161. 'feature' => '',
  162. ) );
  163. $cond['type'] = array_filter( (array) $cond['type'] );
  164. $cond['name'] = array_filter( (array) $cond['name'] );
  165. $cond['feature'] = is_string( $cond['feature'] )
  166. ? trim( $cond['feature'] ) : '';
  167. $output = array();
  168. foreach ( $tags as $tag ) {
  169. $tag = new WPCF7_FormTag( $tag );
  170. if ( $cond['type'] && ! in_array( $tag->type, $cond['type'], true ) ) {
  171. continue;
  172. }
  173. if ( $cond['name'] && ! in_array( $tag->name, $cond['name'], true ) ) {
  174. continue;
  175. }
  176. if ( $cond['feature']
  177. && ! $this->tag_type_supports( $tag->type, $cond['feature'] ) ) {
  178. continue;
  179. }
  180. $output[] = $tag;
  181. }
  182. return $output;
  183. }
  184. private function tag_regex() {
  185. $tagnames = array_keys( $this->tag_types );
  186. $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
  187. return '(\[?)'
  188. . '\[(' . $tagregexp . ')(?:[\r\n\t ](.*?))?(?:[\r\n\t ](\/))?\]'
  189. . '(?:([^[]*?)\[\/\2\])?'
  190. . '(\]?)';
  191. }
  192. private function replace_callback( $m ) {
  193. return $this->scan_callback( $m, true );
  194. }
  195. private function scan_callback( $m, $replace = false ) {
  196. // allow [[foo]] syntax for escaping a tag
  197. if ( $m[1] == '[' && $m[6] == ']' ) {
  198. return substr( $m[0], 1, -1 );
  199. }
  200. $tag = $m[2];
  201. $attr = $this->parse_atts( $m[3] );
  202. $scanned_tag = array(
  203. 'type' => $tag,
  204. 'basetype' => trim( $tag, '*' ),
  205. 'name' => '',
  206. 'options' => array(),
  207. 'raw_values' => array(),
  208. 'values' => array(),
  209. 'pipes' => null,
  210. 'labels' => array(),
  211. 'attr' => '',
  212. 'content' => '',
  213. );
  214. if ( is_array( $attr ) ) {
  215. if ( is_array( $attr['options'] ) ) {
  216. if ( $this->tag_type_supports( $tag, 'name-attr' )
  217. && ! empty( $attr['options'] ) ) {
  218. $scanned_tag['name'] = array_shift( $attr['options'] );
  219. if ( ! wpcf7_is_name( $scanned_tag['name'] ) ) {
  220. return $m[0]; // Invalid name is used. Ignore this tag.
  221. }
  222. }
  223. $scanned_tag['options'] = (array) $attr['options'];
  224. }
  225. $scanned_tag['raw_values'] = (array) $attr['values'];
  226. if ( WPCF7_USE_PIPE ) {
  227. $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] );
  228. $scanned_tag['values'] = $pipes->collect_befores();
  229. $scanned_tag['pipes'] = $pipes;
  230. } else {
  231. $scanned_tag['values'] = $scanned_tag['raw_values'];
  232. }
  233. $scanned_tag['labels'] = $scanned_tag['values'];
  234. } else {
  235. $scanned_tag['attr'] = $attr;
  236. }
  237. $scanned_tag['values'] = array_map( 'trim', $scanned_tag['values'] );
  238. $scanned_tag['labels'] = array_map( 'trim', $scanned_tag['labels'] );
  239. $content = trim( $m[5] );
  240. $content = preg_replace( "/<br[\r\n\t ]*\/?>$/m", '', $content );
  241. $scanned_tag['content'] = $content;
  242. $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag, $replace );
  243. $scanned_tag = new WPCF7_FormTag( $scanned_tag );
  244. $this->scanned_tags[] = $scanned_tag;
  245. if ( $replace ) {
  246. $func = $this->tag_types[$tag]['function'];
  247. return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6];
  248. } else {
  249. return $m[0];
  250. }
  251. }
  252. private function parse_atts( $text ) {
  253. $atts = array( 'options' => array(), 'values' => array() );
  254. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text );
  255. $text = stripcslashes( trim( $text ) );
  256. $pattern = '%^([-+*=0-9a-zA-Z:.!?#$&@_/|\%\r\n\t ]*?)((?:[\r\n\t ]*"[^"]*"|[\r\n\t ]*\'[^\']*\')*)$%';
  257. if ( preg_match( $pattern, $text, $match ) ) {
  258. if ( ! empty( $match[1] ) ) {
  259. $atts['options'] = preg_split( '/[\r\n\t ]+/', trim( $match[1] ) );
  260. }
  261. if ( ! empty( $match[2] ) ) {
  262. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values );
  263. $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] );
  264. }
  265. } else {
  266. $atts = $text;
  267. }
  268. return $atts;
  269. }
  270. }