Grunion_Contact_Form_Field

Class for the contact-field shortcode.

Defined (1)

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

/modules/contact-form/grunion-contact-form.php  
  1. class Grunion_Contact_Form_Field extends Crunion_Contact_Form_Shortcode { 
  2. public $shortcode_name = 'contact-field'; 
  3.  
  4. /** 
  5. * @var Grunion_Contact_Form parent form 
  6. */ 
  7. public $form; 
  8.  
  9. /** 
  10. * @var string default or POSTed value 
  11. */ 
  12. public $value; 
  13.  
  14. /** 
  15. * @var bool Is the input invalid? 
  16. */ 
  17. public $error = false; 
  18.  
  19. /** 
  20. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts() 
  21. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise. 
  22. * @param Grunion_Contact_Form $form The parent form 
  23. */ 
  24. function __construct( $attributes, $content = null, $form = null ) { 
  25. $attributes = shortcode_atts( array( 
  26. 'label' => null,  
  27. 'type' => 'text',  
  28. 'required' => false,  
  29. 'options' => array(),  
  30. 'id' => null,  
  31. 'default' => null,  
  32. 'placeholder' => null,  
  33. ), $attributes, 'contact-field' ); 
  34.  
  35. // special default for subject field 
  36. if ( 'subject' == $attributes['type'] && is_null( $attributes['default'] ) && !is_null( $form ) ) { 
  37. $attributes['default'] = $form->get_attribute( 'subject' ); 
  38.  
  39. // allow required=1 or required=true 
  40. if ( '1' == $attributes['required'] || 'true' == strtolower( $attributes['required'] ) ) 
  41. $attributes['required'] = true; 
  42. else 
  43. $attributes['required'] = false; 
  44.  
  45. // parse out comma-separated options list (for selects, radios, and checkbox-multiples) 
  46. if ( !empty( $attributes['options'] ) && is_string( $attributes['options'] ) ) { 
  47. $attributes['options'] = array_map( 'trim', explode( ', ', $attributes['options'] ) ); 
  48.  
  49. if ( $form ) { 
  50. // make a unique field ID based on the label, with an incrementing number if needed to avoid clashes 
  51. $form_id = $form->get_attribute( 'id' ); 
  52. $id = isset( $attributes['id'] ) ? $attributes['id'] : false; 
  53.  
  54. $unescaped_label = $this->unesc_attr( $attributes['label'] ); 
  55. $unescaped_label = str_replace( '%', '-', $unescaped_label ); // jQuery doesn't like % in IDs? 
  56. $unescaped_label = preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $unescaped_label ); 
  57.  
  58. if ( empty( $id ) ) { 
  59. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label ); 
  60. $i = 0; 
  61. $max_tries = 99; 
  62. while ( isset( $form->fields[$id] ) ) { 
  63. $i++; 
  64. $id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label . '-' . $i ); 
  65.  
  66. if ( $i > $max_tries ) { 
  67. break; 
  68.  
  69. $attributes['id'] = $id; 
  70.  
  71. parent::__construct( $attributes, $content ); 
  72.  
  73. // Store parent form 
  74. $this->form = $form; 
  75.  
  76. /** 
  77. * This field's input is invalid. Flag as invalid and add an error to the parent form 
  78. * @param string $message The error message to display on the form. 
  79. */ 
  80. function add_error( $message ) { 
  81. $this->is_error = true; 
  82.  
  83. if ( !is_wp_error( $this->form->errors ) ) { 
  84. $this->form->errors = new WP_Error; 
  85.  
  86. $this->form->errors->add( $this->get_attribute( 'id' ), $message ); 
  87.  
  88. /** 
  89. * Is the field input invalid? 
  90. * @see $error 
  91. * @return bool 
  92. */ 
  93. function is_error() { 
  94. return $this->error; 
  95.  
  96. /** 
  97. * Validates the form input 
  98. */ 
  99. function validate() { 
  100. // If it's not required, there's nothing to validate 
  101. if ( !$this->get_attribute( 'required' ) ) { 
  102. return; 
  103.  
  104. $field_id = $this->get_attribute( 'id' ); 
  105. $field_type = $this->get_attribute( 'type' ); 
  106. $field_label = $this->get_attribute( 'label' ); 
  107.  
  108. $field_value = isset( $_POST[$field_id] ) ? stripslashes( $_POST[$field_id] ) : ''; 
  109.  
  110. switch ( $field_type ) { 
  111. case 'email' : 
  112. // Make sure the email address is valid 
  113. if ( !is_email( $field_value ) ) { 
  114. $this->add_error( sprintf( __( '%s requires a valid email address', 'jetpack' ), $field_label ) ); 
  115. break; 
  116. default : 
  117. // Just check for presence of any text 
  118. if ( !strlen( trim( $field_value ) ) ) { 
  119. $this->add_error( sprintf( __( '%s is required', 'jetpack' ), $field_label ) ); 
  120.  
  121. /** 
  122. * Outputs the HTML for this form field 
  123. * @return string HTML 
  124. */ 
  125. function render() { 
  126. global $current_user, $user_identity; 
  127.  
  128. $r = ''; 
  129.  
  130. $field_id = $this->get_attribute( 'id' ); 
  131. $field_type = $this->get_attribute( 'type' ); 
  132. $field_label = $this->get_attribute( 'label' ); 
  133. $field_required = $this->get_attribute( 'required' ); 
  134. $placeholder = $this->get_attribute( 'placeholder' ); 
  135. $field_placeholder = ( ! empty( $placeholder ) ) ? "placeholder='" . esc_attr( $placeholder ) . "'" : ''; 
  136.  
  137. if ( isset( $_POST[ $field_id ] ) ) { 
  138. if ( is_array( $_POST[ $field_id ] ) ) { 
  139. $this->value = array_map( 'stripslashes', $_POST[ $field_id ] ); 
  140. } else { 
  141. $this->value = stripslashes( (string) $_POST[ $field_id ] ); 
  142. } elseif ( isset( $_GET[ $field_id ] ) ) { 
  143. $this->value = stripslashes( (string) $_GET[ $field_id ] ); 
  144. } elseif ( 
  145. is_user_logged_in() && 
  146. ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || 
  147. /** 
  148. * Allow third-party tools to prefill the contact form with the user's details when they're logged in. 
  149. * @module contact-form 
  150. * @since 3.2.0 
  151. * @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false. 
  152. */ 
  153. true === apply_filters( 'jetpack_auto_fill_logged_in_user', false ) 
  154. ) { 
  155. // Special defaults for logged-in users 
  156. switch ( $this->get_attribute( 'type' ) ) { 
  157. case 'email' : 
  158. $this->value = $current_user->data->user_email; 
  159. break; 
  160. case 'name' : 
  161. $this->value = $user_identity; 
  162. break; 
  163. case 'url' : 
  164. $this->value = $current_user->data->user_url; 
  165. break; 
  166. default : 
  167. $this->value = $this->get_attribute( 'default' ); 
  168. } else { 
  169. $this->value = $this->get_attribute( 'default' ); 
  170.  
  171. $field_value = Grunion_Contact_Form_Plugin::strip_tags( $this->value ); 
  172. $field_label = Grunion_Contact_Form_Plugin::strip_tags( $field_label ); 
  173.  
  174. switch ( $field_type ) { 
  175. case 'email' : 
  176. $r .= "\n<div>\n"; 
  177. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label email" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  178. $r .= "\t\t<input type='email' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='email' " . $field_placeholder . " " . ( $field_required ? "required aria-required='true'" : "" ) . "/>\n"; 
  179. $r .= "\t</div>\n"; 
  180. break; 
  181. case 'telephone' : 
  182. $r .= "\n<div>\n"; 
  183. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label telephone" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  184. $r .= "\t\t<input type='tel' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='telephone' " . $field_placeholder . "/>\n"; 
  185. case 'textarea' : 
  186. $r .= "\n<div>\n"; 
  187. $r .= "\t\t<label for='contact-form-comment-" . esc_attr( $field_id ) . "' class='grunion-field-label textarea" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  188. $r .= "\t\t<textarea name='" . esc_attr( $field_id ) . "' id='contact-form-comment-" . esc_attr( $field_id ) . "' rows='20' " . $field_placeholder . " " . ( $field_required ? "required aria-required='true'" : "" ) . ">" . esc_textarea( $field_value ) . "</textarea>\n"; 
  189. $r .= "\t</div>\n"; 
  190. break; 
  191. case 'radio' : 
  192. $r .= "\t<div><label class='grunion-field-label" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  193. foreach ( $this->get_attribute( 'options' ) as $option ) { 
  194. $option = Grunion_Contact_Form_Plugin::strip_tags( $option ); 
  195. $r .= "\t\t<label class='grunion-radio-label radio" . ( $this->is_error() ? ' form-error' : '' ) . "'>"; 
  196. $r .= "<input type='radio' name='" . esc_attr( $field_id ) . "' value='" . esc_attr( $option ) . "' class='radio' " . checked( $option, $field_value, false ) . " " . ( $field_required ? "required aria-required='true'" : "" ) . "/> "; 
  197. $r .= esc_html( $option ) . "</label>\n"; 
  198. $r .= "\t\t<div class='clear-form'></div>\n"; 
  199. $r .= "\t\t</div>\n"; 
  200. break; 
  201. case 'checkbox' : 
  202. $r .= "\t<div>\n"; 
  203. $r .= "\t\t<label class='grunion-field-label checkbox" . ( $this->is_error() ? ' form-error' : '' ) . "'>\n"; 
  204. $r .= "\t\t<input type='checkbox' name='" . esc_attr( $field_id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' class='checkbox' " . checked( (bool) $field_value, true, false ) . " " . ( $field_required ? "required aria-required='true'" : "" ) . "/> \n"; 
  205. $r .= "\t\t" . esc_html( $field_label ) . ( $field_required ? '<span>'. __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  206. $r .= "\t\t<div class='clear-form'></div>\n"; 
  207. $r .= "\t</div>\n"; 
  208. break; 
  209. case 'checkbox-multiple' : 
  210. $r .= "\t<div><label class='grunion-field-label" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  211. foreach ( $this->get_attribute( 'options' ) as $option ) { 
  212. $option = Grunion_Contact_Form_Plugin::strip_tags( $option ); 
  213. $r .= "\t\t<label class='grunion-checkbox-multiple-label checkbox-multiple" . ( $this->is_error() ? ' form-error' : '' ) . "'>"; 
  214. $r .= "<input type='checkbox' name='" . esc_attr( $field_id ) . "[]' value='" . esc_attr( $option ) . "' class='checkbox-multiple' " . checked( in_array( $option, (array) $field_value ), true, false ) . " /> "; 
  215. $r .= esc_html( $option ) . "</label>\n"; 
  216. $r .= "\t\t<div class='clear-form'></div>\n"; 
  217. $r .= "\t\t</div>\n"; 
  218. break; 
  219. case 'select' : 
  220. $r .= "\n<div>\n"; 
  221. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label select" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>'. __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  222. $r .= "\t<select name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' class='select' " . ( $field_required ? "required aria-required='true'" : "" ) . ">\n"; 
  223. foreach ( $this->get_attribute( 'options' ) as $option ) { 
  224. $option = Grunion_Contact_Form_Plugin::strip_tags( $option ); 
  225. $r .= "\t\t<option" . selected( $option, $field_value, false ) . ">" . esc_html( $option ) . "</option>\n"; 
  226. $r .= "\t</select>\n"; 
  227. $r .= "\t</div>\n"; 
  228. break; 
  229. case 'date' : 
  230. $r .= "\n<div>\n"; 
  231. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label " . esc_attr( $field_type ) . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  232. $r .= "\t\t<input type='date' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='" . esc_attr( $field_type ) . "' " . ( $field_required ? "required aria-required='true'" : "" ) . "/>\n"; 
  233. $r .= "\t</div>\n"; 
  234.  
  235. wp_enqueue_script( 'grunion-frontend', plugins_url( 'js/grunion-frontend.js', __FILE__ ), array( 'jquery', 'jquery-ui-datepicker' ) ); 
  236. break; 
  237. default : // text field 
  238. // note that any unknown types will produce a text input, so we can use arbitrary type names to handle 
  239. // input fields like name, email, url that require special validation or handling at POST 
  240. $r .= "\n<div>\n"; 
  241. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label " . esc_attr( $field_type ) . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n"; 
  242. $r .= "\t\t<input type='text' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='" . esc_attr( $field_type ) . "' " . $field_placeholder . " " . ( $field_required ? "required aria-required='true'" : "" ) . "/>\n"; 
  243. $r .= "\t</div>\n"; 
  244.  
  245. /** 
  246. * Filter the HTML of the Contact Form. 
  247. * @module contact-form 
  248. * @since 2.6.0 
  249. * @param string $r Contact Form HTML output. 
  250. * @param string $field_label Field label. 
  251. * @param int|null $id Post ID. 
  252. */ 
  253. return apply_filters( 'grunion_contact_form_field_html', $r, $field_label, ( in_the_loop() ? get_the_ID() : null ) );