WPCF7_Shortcode

The Contact Form 7 WPCF7 Shortcode class.

Defined (1)

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

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