number.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. ** A base module for the following types of tags:
  4. ** [number] and [number*] # Number
  5. ** [range] and [range*] # Range
  6. **/
  7. /* form_tag handler */
  8. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_number', 10, 0 );
  9. function wpcf7_add_form_tag_number() {
  10. wpcf7_add_form_tag( array( 'number', 'number*', 'range', 'range*' ),
  11. 'wpcf7_number_form_tag_handler', array( 'name-attr' => true ) );
  12. }
  13. function wpcf7_number_form_tag_handler( $tag ) {
  14. if ( empty( $tag->name ) ) {
  15. return '';
  16. }
  17. $validation_error = wpcf7_get_validation_error( $tag->name );
  18. $class = wpcf7_form_controls_class( $tag->type );
  19. $class .= ' wpcf7-validates-as-number';
  20. if ( $validation_error ) {
  21. $class .= ' wpcf7-not-valid';
  22. }
  23. $atts = array();
  24. $atts['class'] = $tag->get_class_option( $class );
  25. $atts['id'] = $tag->get_id_option();
  26. $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  27. $atts['min'] = $tag->get_option( 'min', 'signed_int', true );
  28. $atts['max'] = $tag->get_option( 'max', 'signed_int', true );
  29. $atts['step'] = $tag->get_option( 'step', 'int', true );
  30. if ( $tag->has_option( 'readonly' ) ) {
  31. $atts['readonly'] = 'readonly';
  32. }
  33. if ( $tag->is_required() ) {
  34. $atts['aria-required'] = 'true';
  35. }
  36. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  37. $value = (string) reset( $tag->values );
  38. if ( $tag->has_option( 'placeholder' )
  39. or $tag->has_option( 'watermark' ) ) {
  40. $atts['placeholder'] = $value;
  41. $value = '';
  42. }
  43. $value = $tag->get_default_option( $value );
  44. $value = wpcf7_get_hangover( $tag->name, $value );
  45. $atts['value'] = $value;
  46. if ( wpcf7_support_html5() ) {
  47. $atts['type'] = $tag->basetype;
  48. } else {
  49. $atts['type'] = 'text';
  50. }
  51. $atts['name'] = $tag->name;
  52. $atts = wpcf7_format_atts( $atts );
  53. $html = sprintf(
  54. '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
  55. sanitize_html_class( $tag->name ), $atts, $validation_error );
  56. return $html;
  57. }
  58. /* Validation filter */
  59. add_filter( 'wpcf7_validate_number', 'wpcf7_number_validation_filter', 10, 2 );
  60. add_filter( 'wpcf7_validate_number*', 'wpcf7_number_validation_filter', 10, 2 );
  61. add_filter( 'wpcf7_validate_range', 'wpcf7_number_validation_filter', 10, 2 );
  62. add_filter( 'wpcf7_validate_range*', 'wpcf7_number_validation_filter', 10, 2 );
  63. function wpcf7_number_validation_filter( $result, $tag ) {
  64. $name = $tag->name;
  65. $value = isset( $_POST[$name] )
  66. ? trim( strtr( (string) $_POST[$name], "\n", " " ) )
  67. : '';
  68. $min = $tag->get_option( 'min', 'signed_int', true );
  69. $max = $tag->get_option( 'max', 'signed_int', true );
  70. if ( $tag->is_required() and '' == $value ) {
  71. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  72. } elseif ( '' != $value and ! wpcf7_is_number( $value ) ) {
  73. $result->invalidate( $tag, wpcf7_get_message( 'invalid_number' ) );
  74. } elseif ( '' != $value and '' != $min and (float) $value < (float) $min ) {
  75. $result->invalidate( $tag, wpcf7_get_message( 'number_too_small' ) );
  76. } elseif ( '' != $value and '' != $max and (float) $max < (float) $value ) {
  77. $result->invalidate( $tag, wpcf7_get_message( 'number_too_large' ) );
  78. }
  79. return $result;
  80. }
  81. /* Messages */
  82. add_filter( 'wpcf7_messages', 'wpcf7_number_messages', 10, 1 );
  83. function wpcf7_number_messages( $messages ) {
  84. return array_merge( $messages, array(
  85. 'invalid_number' => array(
  86. 'description' => __( "Number format that the sender entered is invalid", 'contact-form-7' ),
  87. 'default' => __( "The number format is invalid.", 'contact-form-7' )
  88. ),
  89. 'number_too_small' => array(
  90. 'description' => __( "Number is smaller than minimum limit", 'contact-form-7' ),
  91. 'default' => __( "The number is smaller than the minimum allowed.", 'contact-form-7' )
  92. ),
  93. 'number_too_large' => array(
  94. 'description' => __( "Number is larger than maximum limit", 'contact-form-7' ),
  95. 'default' => __( "The number is larger than the maximum allowed.", 'contact-form-7' )
  96. ),
  97. ) );
  98. }
  99. /* Tag generator */
  100. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_number', 18, 0 );
  101. function wpcf7_add_tag_generator_number() {
  102. $tag_generator = WPCF7_TagGenerator::get_instance();
  103. $tag_generator->add( 'number', __( 'number', 'contact-form-7' ),
  104. 'wpcf7_tag_generator_number' );
  105. }
  106. function wpcf7_tag_generator_number( $contact_form, $args = '' ) {
  107. $args = wp_parse_args( $args, array() );
  108. $type = 'number';
  109. $description = __( "Generate a form-tag for a field for numeric value input. For more details, see %s.", 'contact-form-7' );
  110. $desc_link = wpcf7_link( __( 'https://contactform7.com/number-fields/', 'contact-form-7' ), __( 'Number Fields', 'contact-form-7' ) );
  111. ?>
  112. <div class="control-box">
  113. <fieldset>
  114. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  115. <table class="form-table">
  116. <tbody>
  117. <tr>
  118. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  119. <td>
  120. <fieldset>
  121. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  122. <select name="tagtype">
  123. <option value="number" selected="selected"><?php echo esc_html( __( 'Spinbox', 'contact-form-7' ) ); ?></option>
  124. <option value="range"><?php echo esc_html( __( 'Slider', 'contact-form-7' ) ); ?></option>
  125. </select>
  126. <br />
  127. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  128. </fieldset>
  129. </td>
  130. </tr>
  131. <tr>
  132. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  133. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  134. </tr>
  135. <tr>
  136. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Default value', 'contact-form-7' ) ); ?></label></th>
  137. <td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br />
  138. <label><input type="checkbox" name="placeholder" class="option" /> <?php echo esc_html( __( 'Use this text as the placeholder of the field', 'contact-form-7' ) ); ?></label></td>
  139. </tr>
  140. <tr>
  141. <th scope="row"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></th>
  142. <td>
  143. <fieldset>
  144. <legend class="screen-reader-text"><?php echo esc_html( __( 'Range', 'contact-form-7' ) ); ?></legend>
  145. <label>
  146. <?php echo esc_html( __( 'Min', 'contact-form-7' ) ); ?>
  147. <input type="number" name="min" class="numeric option" />
  148. </label>
  149. &ndash;
  150. <label>
  151. <?php echo esc_html( __( 'Max', 'contact-form-7' ) ); ?>
  152. <input type="number" name="max" class="numeric option" />
  153. </label>
  154. </fieldset>
  155. </td>
  156. </tr>
  157. <tr>
  158. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  159. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  160. </tr>
  161. <tr>
  162. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  163. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  164. </tr>
  165. </tbody>
  166. </table>
  167. </fieldset>
  168. </div>
  169. <div class="insert-box">
  170. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  171. <div class="submitbox">
  172. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  173. </div>
  174. <br class="clear" />
  175. <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>
  176. </div>
  177. <?php
  178. }