Bxcft_Field_Type_DecimalNumber

The BuddyPress Xprofile Custom Fields Type Bxcft Field Type DecimalNumber class.

Defined (1)

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

/classes/Bxcft_Field_Type_DecimalNumber.php  
  1. class Bxcft_Field_Type_DecimalNumber extends BP_XProfile_Field_Type 
  2. public function __construct() { 
  3. parent::__construct(); 
  4.  
  5. $this->name = __( 'Decimal number (HTML5 field)', 'bxcft' ); 
  6.  
  7. $this->accepts_null_value = true; 
  8. $this->supports_options = true; 
  9.  
  10. $this->set_format( '/^\d+\.?\d*$/', 'replace' ); 
  11.  
  12. do_action( 'bp_xprofile_field_type_decimal_number', $this ); 
  13.  
  14. public function admin_field_html (array $raw_properties = array ()) 
  15. global $field; 
  16.  
  17. $options = $field->get_children( true ); 
  18. if (count($options) > 0) { 
  19. $step = (1 / (pow(10, (int)$options[0]->name))); 
  20. } else { 
  21. $step = 0.01; 
  22.  
  23. $html = $this->get_edit_field_html_elements( array_merge( 
  24. array( 
  25. 'type' => 'number',  
  26. 'step' => $step,  
  27. ),  
  28. $raw_properties 
  29. ) ); 
  30. ?> 
  31. <input <?php echo $html; ?> /> 
  32. <?php 
  33.  
  34. public function edit_field_html (array $raw_properties = array ()) 
  35. if ( isset( $raw_properties['user_id'] ) ) { 
  36. unset( $raw_properties['user_id'] ); 
  37.  
  38. // HTML5 required attribute. 
  39. if ( bp_get_the_profile_field_is_required() ) { 
  40. $raw_properties['required'] = 'required'; 
  41.  
  42. $field = new BP_XProfile_Field(bp_get_the_profile_field_id()); 
  43.  
  44. $options = $field->get_children( true ); 
  45. if (count($options) > 0) { 
  46. $step = (1 / (pow(10, (int)$options[0]->name))); 
  47. } else { 
  48. $step = 0.01; 
  49.  
  50. $html = $this->get_edit_field_html_elements( array_merge( 
  51. array( 
  52. 'type' => 'number',  
  53. 'step' => $step,  
  54. 'value' => bp_get_the_profile_field_edit_value(),  
  55. ),  
  56. $raw_properties 
  57. ) ); 
  58.  
  59. $label = sprintf( 
  60. '<label for="%s">%s%s</label>',  
  61. bp_get_the_profile_field_input_name(),  
  62. bp_get_the_profile_field_name(),  
  63. (bp_get_the_profile_field_is_required()) ? 
  64. ' ' . esc_html__( '(required)', 'buddypress' ) : '' 
  65. ); 
  66. // Label. 
  67. echo apply_filters('bxcft_field_label', $label, bp_get_the_profile_field_id(), bp_get_the_profile_field_type(), bp_get_the_profile_field_input_name(), bp_get_the_profile_field_name(), bp_get_the_profile_field_is_required()); 
  68. // Errors. 
  69. do_action( bp_get_the_profile_field_errors_action() ); 
  70. // Input. 
  71. ?> 
  72. <input <?php echo $html; ?> /> 
  73. <?php 
  74.  
  75. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  76. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  77. if ( false === $type ) { 
  78. return; 
  79.  
  80. $class = $current_field->type != $type ? 'display: none;' : ''; 
  81. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  82.  
  83. $options = $current_field->get_children( true ); 
  84. if ( ! $options ) { 
  85. $options = array(); 
  86. $i = 1; 
  87. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  88. $is_default_option = true; 
  89.  
  90. $options[] = (object) array( 
  91. 'id' => -1,  
  92. 'is_default_option' => $is_default_option,  
  93. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  94. ); 
  95.  
  96. ++$i; 
  97.  
  98. if ( ! $options ) { 
  99. $options[] = (object) array( 
  100. 'id' => -1,  
  101. 'is_default_option' => false,  
  102. 'name' => '2',  
  103. ); 
  104. ?> 
  105. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  106. <h3><?php esc_html_e( 'Select max number of decimals:', 'bxcft' ); ?></h3> 
  107. <div class="inside"> 
  108. <p> 
  109. <select name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" id="<?php echo esc_attr( "{$type}_option1" ); ?>"> 
  110. <?php for ($j=1;$j<=6;$j++): ?> 
  111. <option value="<?php echo $j; ?>"<?php if ($j === (int)$options[0]->name): ?> selected="selected"<?php endif; ?>><?php echo $j; ?></option> 
  112. <?php endfor; ?> 
  113. </select> 
  114. </p> 
  115. </div> 
  116. </div> 
  117. <?php 
  118.  
  119. public function is_valid( $values ) { 
  120. $this->validation_whitelist = null; 
  121. return parent::is_valid($values); 
  122.  
  123. /** 
  124. * Modify the appearance of value. Apply autolink if enabled. 
  125. * @param string $value Original value of field 
  126. * @param int $field_id Id of field 
  127. * @return string Value formatted 
  128. */ 
  129. public static function display_filter($field_value, $field_id = '') { 
  130.  
  131. $new_field_value = $field_value; 
  132.  
  133. if (!empty($field_value)) { 
  134. if (!empty($field_id)) { 
  135. $field = BP_XProfile_Field::get_instance($field_id); 
  136. if ($field) { 
  137. $do_autolink = apply_filters('bxcft_do_autolink',  
  138. $field->get_do_autolink()); 
  139. if ($do_autolink) { 
  140. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  141. $search_url = add_query_arg( array( 
  142. $query_arg => urlencode( $field_value ) 
  143. ), bp_get_members_directory_permalink() ); 
  144. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  145. '" rel="nofollow">' . $new_field_value . '</a>'; 
  146.  
  147. /** 
  148. * bxcft_decimal_number_display_filter 
  149. * Use this filter to modify the appearance of Decimal Number 
  150. * field value. 
  151. * @param $new_field_value Value of field 
  152. * @param $field_id Id of field. 
  153. * @return Filtered value of field. 
  154. */ 
  155. return apply_filters('bxcft_decimal_number_display_filter',  
  156. $new_field_value, $field_id);