select.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. ** A base module for [select] and [select*]
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select', 10, 0 );
  7. function wpcf7_add_form_tag_select() {
  8. wpcf7_add_form_tag( array( 'select', 'select*' ),
  9. 'wpcf7_select_form_tag_handler',
  10. array(
  11. 'name-attr' => true,
  12. 'selectable-values' => true,
  13. )
  14. );
  15. }
  16. function wpcf7_select_form_tag_handler( $tag ) {
  17. if ( empty( $tag->name ) ) {
  18. return '';
  19. }
  20. $validation_error = wpcf7_get_validation_error( $tag->name );
  21. $class = wpcf7_form_controls_class( $tag->type );
  22. if ( $validation_error ) {
  23. $class .= ' wpcf7-not-valid';
  24. }
  25. $atts = array();
  26. $atts['class'] = $tag->get_class_option( $class );
  27. $atts['id'] = $tag->get_id_option();
  28. $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  29. if ( $tag->is_required() ) {
  30. $atts['aria-required'] = 'true';
  31. }
  32. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  33. $multiple = $tag->has_option( 'multiple' );
  34. $include_blank = $tag->has_option( 'include_blank' );
  35. $first_as_label = $tag->has_option( 'first_as_label' );
  36. if ( $tag->has_option( 'size' ) ) {
  37. $size = $tag->get_option( 'size', 'int', true );
  38. if ( $size ) {
  39. $atts['size'] = $size;
  40. } elseif ( $multiple ) {
  41. $atts['size'] = 4;
  42. } else {
  43. $atts['size'] = 1;
  44. }
  45. }
  46. if ( $data = (array) $tag->get_data_option() ) {
  47. $tag->values = array_merge( $tag->values, array_values( $data ) );
  48. $tag->labels = array_merge( $tag->labels, array_values( $data ) );
  49. }
  50. $values = $tag->values;
  51. $labels = $tag->labels;
  52. $default_choice = $tag->get_default_option( null, array(
  53. 'multiple' => $multiple,
  54. 'shifted' => $include_blank,
  55. ) );
  56. if ( $include_blank
  57. or empty( $values ) ) {
  58. array_unshift( $labels, '---' );
  59. array_unshift( $values, '' );
  60. } elseif ( $first_as_label ) {
  61. $values[0] = '';
  62. }
  63. $html = '';
  64. $hangover = wpcf7_get_hangover( $tag->name );
  65. foreach ( $values as $key => $value ) {
  66. if ( $hangover ) {
  67. $selected = in_array( $value, (array) $hangover, true );
  68. } else {
  69. $selected = in_array( $value, (array) $default_choice, true );
  70. }
  71. $item_atts = array(
  72. 'value' => $value,
  73. 'selected' => $selected ? 'selected' : '',
  74. );
  75. $item_atts = wpcf7_format_atts( $item_atts );
  76. $label = isset( $labels[$key] ) ? $labels[$key] : $value;
  77. $html .= sprintf( '<option %1$s>%2$s</option>',
  78. $item_atts, esc_html( $label ) );
  79. }
  80. if ( $multiple ) {
  81. $atts['multiple'] = 'multiple';
  82. }
  83. $atts['name'] = $tag->name . ( $multiple ? '[]' : '' );
  84. $atts = wpcf7_format_atts( $atts );
  85. $html = sprintf(
  86. '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
  87. sanitize_html_class( $tag->name ), $atts, $html, $validation_error );
  88. return $html;
  89. }
  90. /* Validation filter */
  91. add_filter( 'wpcf7_validate_select', 'wpcf7_select_validation_filter', 10, 2 );
  92. add_filter( 'wpcf7_validate_select*', 'wpcf7_select_validation_filter', 10, 2 );
  93. function wpcf7_select_validation_filter( $result, $tag ) {
  94. $name = $tag->name;
  95. if ( isset( $_POST[$name] )
  96. and is_array( $_POST[$name] ) ) {
  97. foreach ( $_POST[$name] as $key => $value ) {
  98. if ( '' === $value ) {
  99. unset( $_POST[$name][$key] );
  100. }
  101. }
  102. }
  103. $empty = ! isset( $_POST[$name] ) || empty( $_POST[$name] ) && '0' !== $_POST[$name];
  104. if ( $tag->is_required() and $empty ) {
  105. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  106. }
  107. return $result;
  108. }
  109. /* Tag generator */
  110. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_menu', 25, 0 );
  111. function wpcf7_add_tag_generator_menu() {
  112. $tag_generator = WPCF7_TagGenerator::get_instance();
  113. $tag_generator->add( 'menu', __( 'drop-down menu', 'contact-form-7' ),
  114. 'wpcf7_tag_generator_menu' );
  115. }
  116. function wpcf7_tag_generator_menu( $contact_form, $args = '' ) {
  117. $args = wp_parse_args( $args, array() );
  118. $description = __( "Generate a form-tag for a drop-down menu. For more details, see %s.", 'contact-form-7' );
  119. $desc_link = wpcf7_link( __( 'https://contactform7.com/checkboxes-radio-buttons-and-menus/', 'contact-form-7' ), __( 'Checkboxes, Radio Buttons and Menus', 'contact-form-7' ) );
  120. ?>
  121. <div class="control-box">
  122. <fieldset>
  123. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  124. <table class="form-table">
  125. <tbody>
  126. <tr>
  127. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  128. <td>
  129. <fieldset>
  130. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  131. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  132. </fieldset>
  133. </td>
  134. </tr>
  135. <tr>
  136. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  137. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  138. </tr>
  139. <tr>
  140. <th scope="row"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></th>
  141. <td>
  142. <fieldset>
  143. <legend class="screen-reader-text"><?php echo esc_html( __( 'Options', 'contact-form-7' ) ); ?></legend>
  144. <textarea name="values" class="values" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>"></textarea>
  145. <label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><span class="description"><?php echo esc_html( __( "One option per line.", 'contact-form-7' ) ); ?></span></label><br />
  146. <label><input type="checkbox" name="multiple" class="option" /> <?php echo esc_html( __( 'Allow multiple selections', 'contact-form-7' ) ); ?></label><br />
  147. <label><input type="checkbox" name="include_blank" class="option" /> <?php echo esc_html( __( 'Insert a blank item as the first option', 'contact-form-7' ) ); ?></label>
  148. </fieldset>
  149. </td>
  150. </tr>
  151. <tr>
  152. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  153. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  154. </tr>
  155. <tr>
  156. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  157. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  158. </tr>
  159. </tbody>
  160. </table>
  161. </fieldset>
  162. </div>
  163. <div class="insert-box">
  164. <input type="text" name="select" class="tag code" readonly="readonly" onfocus="this.select()" />
  165. <div class="submitbox">
  166. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  167. </div>
  168. <br class="clear" />
  169. <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To use the value input through this field in a mail field, you need to insert the corresponding mail-tag (%s) into the field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p>
  170. </div>
  171. <?php
  172. }