/includes/form-tag.php

  1. <?php 
  2.  
  3. class WPCF7_FormTag { 
  4.  
  5. public $type; 
  6. public $basetype; 
  7. public $name = ''; 
  8. public $options = array(); 
  9. public $raw_values = array(); 
  10. public $values = array(); 
  11. public $pipes; 
  12. public $labels = array(); 
  13. public $attr = ''; 
  14. public $content = ''; 
  15.  
  16. public function __construct( $tag ) { 
  17. foreach ( $tag as $key => $value ) { 
  18. if ( property_exists( __CLASS__, $key ) ) { 
  19. $this->{$key} = $value; 
  20.  
  21. public function is_required() { 
  22. return ( '*' == substr( $this->type, -1 ) ); 
  23.  
  24. public function has_option( $opt ) { 
  25. $pattern = sprintf( '/^%s(:.+)?$/i', preg_quote( $opt, '/' ) ); 
  26. return (bool) preg_grep( $pattern, $this->options ); 
  27.  
  28. public function get_option( $opt, $pattern = '', $single = false ) { 
  29. $preset_patterns = array( 
  30. 'date' => '([0-9]{4}-[0-9]{2}-[0-9]{2}|today(.*))',  
  31. 'int' => '[0-9]+',  
  32. 'signed_int' => '-?[0-9]+',  
  33. 'class' => '[-0-9a-zA-Z_]+',  
  34. 'id' => '[-0-9a-zA-Z_]+',  
  35. ); 
  36.  
  37. if ( isset( $preset_patterns[$pattern] ) ) { 
  38. $pattern = $preset_patterns[$pattern]; 
  39.  
  40. if ( '' == $pattern ) { 
  41. $pattern = '.+'; 
  42.  
  43. $pattern = sprintf( '/^%s:%s$/i', preg_quote( $opt, '/' ), $pattern ); 
  44.  
  45. if ( $single ) { 
  46. $matches = $this->get_first_match_option( $pattern ); 
  47.  
  48. if ( ! $matches ) { 
  49. return false; 
  50.  
  51. return substr( $matches[0], strlen( $opt ) + 1 ); 
  52. } else { 
  53. $matches_a = $this->get_all_match_options( $pattern ); 
  54.  
  55. if ( ! $matches_a ) { 
  56. return false; 
  57.  
  58. $results = array(); 
  59.  
  60. foreach ( $matches_a as $matches ) { 
  61. $results[] = substr( $matches[0], strlen( $opt ) + 1 ); 
  62.  
  63. return $results; 
  64.  
  65. public function get_id_option() { 
  66. return $this->get_option( 'id', 'id', true ); 
  67.  
  68. public function get_class_option( $default = '' ) { 
  69. if ( is_string( $default ) ) { 
  70. $default = explode( ' ', $default ); 
  71.  
  72. $options = array_merge( 
  73. (array) $default,  
  74. (array) $this->get_option( 'class', 'class' ) ); 
  75.  
  76. $options = array_filter( array_unique( $options ) ); 
  77.  
  78. return implode( ' ', $options ); 
  79.  
  80. public function get_size_option( $default = '' ) { 
  81. $option = $this->get_option( 'size', 'int', true ); 
  82.  
  83. if ( $option ) { 
  84. return $option; 
  85.  
  86. $matches_a = $this->get_all_match_options( '%^([0-9]*)/[0-9]*$%' ); 
  87.  
  88. foreach ( (array) $matches_a as $matches ) { 
  89. if ( isset( $matches[1] ) && '' !== $matches[1] ) { 
  90. return $matches[1]; 
  91.  
  92. return $default; 
  93.  
  94. public function get_maxlength_option( $default = '' ) { 
  95. $option = $this->get_option( 'maxlength', 'int', true ); 
  96.  
  97. if ( $option ) { 
  98. return $option; 
  99.  
  100. $matches_a = $this->get_all_match_options( 
  101. '%^(?:[0-9]*x?[0-9]*)?/([0-9]+)$%' ); 
  102.  
  103. foreach ( (array) $matches_a as $matches ) { 
  104. if ( isset( $matches[1] ) && '' !== $matches[1] ) { 
  105. return $matches[1]; 
  106.  
  107. return $default; 
  108.  
  109. public function get_minlength_option( $default = '' ) { 
  110. $option = $this->get_option( 'minlength', 'int', true ); 
  111.  
  112. if ( $option ) { 
  113. return $option; 
  114. } else { 
  115. return $default; 
  116.  
  117. public function get_cols_option( $default = '' ) { 
  118. $option = $this->get_option( 'cols', 'int', true ); 
  119.  
  120. if ( $option ) { 
  121. return $option; 
  122.  
  123. $matches_a = $this->get_all_match_options( 
  124. '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); 
  125.  
  126. foreach ( (array) $matches_a as $matches ) { 
  127. if ( isset( $matches[1] ) && '' !== $matches[1] ) { 
  128. return $matches[1]; 
  129.  
  130. return $default; 
  131.  
  132. public function get_rows_option( $default = '' ) { 
  133. $option = $this->get_option( 'rows', 'int', true ); 
  134.  
  135. if ( $option ) { 
  136. return $option; 
  137.  
  138. $matches_a = $this->get_all_match_options( 
  139. '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); 
  140.  
  141. foreach ( (array) $matches_a as $matches ) { 
  142. if ( isset( $matches[2] ) && '' !== $matches[2] ) { 
  143. return $matches[2]; 
  144.  
  145. return $default; 
  146.  
  147. public function get_date_option( $opt ) { 
  148. $option = $this->get_option( $opt, 'date', true ); 
  149.  
  150. if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $option ) ) { 
  151. return $option; 
  152.  
  153. if ( preg_match( '/^today(?:([+-][0-9]+)([a-z]*))?/', $option, $matches ) ) { 
  154. $number = isset( $matches[1] ) ? (int) $matches[1] : 0; 
  155. $unit = isset( $matches[2] ) ? $matches[2] : ''; 
  156.  
  157. if ( ! preg_match( '/^(day|month|year|week)s?$/', $unit ) ) { 
  158. $unit = 'days'; 
  159.  
  160. $date = gmdate( 'Y-m-d',  
  161. strtotime( sprintf( 'today %1$s %2$s', $number, $unit ) ) ); 
  162. return $date; 
  163.  
  164. return false; 
  165.  
  166. public function get_default_option( $default = '', $args = '' ) { 
  167. $args = wp_parse_args( $args, array( 
  168. 'multiple' => false ) ); 
  169.  
  170. $options = (array) $this->get_option( 'default' ); 
  171. $values = array(); 
  172.  
  173. if ( empty( $options ) ) { 
  174. return $args['multiple'] ? $values : $default; 
  175.  
  176. foreach ( $options as $opt ) { 
  177. $opt = sanitize_key( $opt ); 
  178.  
  179. if ( 'user_' == substr( $opt, 0, 5 ) && is_user_logged_in() ) { 
  180. $primary_props = array( 'user_login', 'user_email', 'user_url' ); 
  181. $opt = in_array( $opt, $primary_props ) ? $opt : substr( $opt, 5 ); 
  182.  
  183. $user = wp_get_current_user(); 
  184. $user_prop = $user->get( $opt ); 
  185.  
  186. if ( ! empty( $user_prop ) ) { 
  187. if ( $args['multiple'] ) { 
  188. $values[] = $user_prop; 
  189. } else { 
  190. return $user_prop; 
  191.  
  192. } elseif ( 'post_meta' == $opt && in_the_loop() ) { 
  193. if ( $args['multiple'] ) { 
  194. $values = array_merge( $values,  
  195. get_post_meta( get_the_ID(), $this->name ) ); 
  196. } else { 
  197. $val = (string) get_post_meta( get_the_ID(), $this->name, true ); 
  198.  
  199. if ( strlen( $val ) ) { 
  200. return $val; 
  201.  
  202. } elseif ( 'get' == $opt && isset( $_GET[$this->name] ) ) { 
  203. $vals = (array) $_GET[$this->name]; 
  204. $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); 
  205.  
  206. if ( $args['multiple'] ) { 
  207. $values = array_merge( $values, $vals ); 
  208. } else { 
  209. $val = isset( $vals[0] ) ? (string) $vals[0] : ''; 
  210.  
  211. if ( strlen( $val ) ) { 
  212. return $val; 
  213.  
  214. } elseif ( 'post' == $opt && isset( $_POST[$this->name] ) ) { 
  215. $vals = (array) $_POST[$this->name]; 
  216. $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); 
  217.  
  218. if ( $args['multiple'] ) { 
  219. $values = array_merge( $values, $vals ); 
  220. } else { 
  221. $val = isset( $vals[0] ) ? (string) $vals[0] : ''; 
  222.  
  223. if ( strlen( $val ) ) { 
  224. return $val; 
  225.  
  226. } elseif ( 'shortcode_attr' == $opt ) { 
  227. if ( $contact_form = WPCF7_ContactForm::get_current() ) { 
  228. $val = $contact_form->shortcode_attr( $this->name ); 
  229.  
  230. if ( strlen( $val ) ) { 
  231. if ( $args['multiple'] ) { 
  232. $values[] = $val; 
  233. } else { 
  234. return $val; 
  235.  
  236. if ( $args['multiple'] ) { 
  237. $values = array_unique( $values ); 
  238. return $values; 
  239. } else { 
  240. return $default; 
  241.  
  242. public function get_data_option( $args = '' ) { 
  243. $options = (array) $this->get_option( 'data' ); 
  244.  
  245. return apply_filters( 'wpcf7_form_tag_data_option', null, $options, $args ); 
  246.  
  247. public function get_first_match_option( $pattern ) { 
  248. foreach( (array) $this->options as $option ) { 
  249. if ( preg_match( $pattern, $option, $matches ) ) { 
  250. return $matches; 
  251.  
  252. return false; 
  253.  
  254. public function get_all_match_options( $pattern ) { 
  255. $result = array(); 
  256.  
  257. foreach( (array) $this->options as $option ) { 
  258. if ( preg_match( $pattern, $option, $matches ) ) { 
  259. $result[] = $matches; 
  260.  
  261. return $result; 
.