Нема описа

class-number.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Number text field.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Field_Number extends WPForms_Field {
  8. /**
  9. * Primary class constructor.
  10. *
  11. * @since 1.0.0
  12. */
  13. public function init() {
  14. // Define field type information.
  15. $this->name = esc_html__( 'Numbers', 'wpforms-lite' );
  16. $this->type = 'number';
  17. $this->icon = 'fa-hashtag';
  18. $this->order = 130;
  19. }
  20. /**
  21. * Field options panel inside the builder.
  22. *
  23. * @since 1.0.0
  24. *
  25. * @param array $field Field data.
  26. */
  27. public function field_options( $field ) {
  28. /*
  29. * Basic field options.
  30. */
  31. // Options open markup.
  32. $args = array(
  33. 'markup' => 'open',
  34. );
  35. $this->field_option( 'basic-options', $field, $args );
  36. // Label.
  37. $this->field_option( 'label', $field );
  38. // Description.
  39. $this->field_option( 'description', $field );
  40. // Required toggle.
  41. $this->field_option( 'required', $field );
  42. // Options close markup.
  43. $args = array(
  44. 'markup' => 'close',
  45. );
  46. $this->field_option( 'basic-options', $field, $args );
  47. /*
  48. * Advanced field options.
  49. */
  50. // Options open markup.
  51. $args = [
  52. 'markup' => 'open',
  53. ];
  54. $this->field_option( 'advanced-options', $field, $args );
  55. // Size.
  56. $this->field_option( 'size', $field );
  57. // Placeholder.
  58. $this->field_option( 'placeholder', $field );
  59. // Default value.
  60. $this->field_option( 'default_value', $field );
  61. // Custom CSS classes.
  62. $this->field_option( 'css', $field );
  63. // Hide label.
  64. $this->field_option( 'label_hide', $field );
  65. // Options close markup.
  66. $args = [
  67. 'markup' => 'close',
  68. ];
  69. $this->field_option( 'advanced-options', $field, $args );
  70. }
  71. /**
  72. * Field preview inside the builder.
  73. *
  74. * @since 1.0.0
  75. *
  76. * @param array $field Field data.
  77. */
  78. public function field_preview( $field ) {
  79. // Define data.
  80. $placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : '';
  81. // Label.
  82. $this->field_preview_option( 'label', $field );
  83. // Primary input.
  84. echo '<input type="text" placeholder="' . esc_attr( $placeholder ) . '" class="primary-input" readonly>';
  85. // Description.
  86. $this->field_preview_option( 'description', $field );
  87. }
  88. /**
  89. * Field display on the form front-end.
  90. *
  91. * @since 1.0.0
  92. *
  93. * @param array $field Field data.
  94. * @param array $deprecated Deprecated, not used.
  95. * @param array $form_data Form data.
  96. */
  97. public function field_display( $field, $deprecated, $form_data ) {
  98. // Define data.
  99. $primary = $field['properties']['inputs']['primary'];
  100. // Primary field.
  101. printf(
  102. '<input type="number" pattern="\d*" %s %s>',
  103. wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ),
  104. esc_attr( $primary['required'] )
  105. );
  106. }
  107. /**
  108. * Validate field on form submit.
  109. *
  110. * @since 1.0.0
  111. *
  112. * @param int $field_id Field id.
  113. * @param string $field_submit Submitted value.
  114. * @param array $form_data Form data.
  115. */
  116. public function validate( $field_id, $field_submit, $form_data ) {
  117. $form_id = $form_data['id'];
  118. $value = $this->sanitize_value( $field_submit );
  119. // If field is marked as required, check for entry data.
  120. if (
  121. ! empty( $form_data['fields'][ $field_id ]['required'] ) &&
  122. empty( $value ) &&
  123. ! is_numeric( $value )
  124. ) {
  125. wpforms()->process->errors[ $form_id ][ $field_id ] = wpforms_get_required_label();
  126. }
  127. // Check if value is numeric.
  128. if ( ! empty( $value ) && ! is_numeric( $value ) ) {
  129. wpforms()->process->errors[ $form_id ][ $field_id ] = apply_filters( 'wpforms_valid_number_label', esc_html__( 'Please enter a valid number.', 'wpforms-lite' ) );
  130. }
  131. }
  132. /**
  133. * Format and sanitize field.
  134. *
  135. * @since 1.3.5
  136. *
  137. * @param int $field_id Field id.
  138. * @param string $field_submit Submitted value.
  139. * @param array $form_data Form data.
  140. */
  141. public function format( $field_id, $field_submit, $form_data ) {
  142. // Define data.
  143. $name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : '';
  144. // Set final field details.
  145. wpforms()->process->fields[ $field_id ] = array(
  146. 'name' => sanitize_text_field( $name ),
  147. 'value' => $this->sanitize_value( $field_submit ),
  148. 'id' => absint( $field_id ),
  149. 'type' => $this->type,
  150. );
  151. }
  152. /**
  153. * Sanitize the value.
  154. *
  155. * @since 1.5.7
  156. *
  157. * @param string $value The number field submitted value.
  158. *
  159. * @return float|int|string
  160. */
  161. private function sanitize_value( $value ) {
  162. if ( empty( $value ) && ! is_numeric( $value ) ) {
  163. return '';
  164. }
  165. // Some browsers allow other non-digit/decimal characters to be submitted
  166. // with the num input, which then trips the is_numeric validation below.
  167. // To get around this we remove all chars that are not expected.
  168. $signed_value = preg_replace( '/[^-0-9.]/', '', $value );
  169. $abs_value = str_replace( '-', '', $signed_value );
  170. return $signed_value < 0 ? '-' . $abs_value : $abs_value;
  171. }
  172. }
  173. new WPForms_Field_Number();