WPCF7_FormTag

The Contact Form 7 WPCF7 FormTag class.

Defined (1)

The class is defined in the following location(s).

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