Bxcft_Field_Type_NumberMinMax

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

Defined (1)

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

/classes/Bxcft_Field_Type_NumberMinMax.php  
  1. class Bxcft_Field_Type_NumberMinMax extends BP_XProfile_Field_Type 
  2. public function __construct() { 
  3. parent::__construct(); 
  4.  
  5. $this->name = __( 'Number within min/max values (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_number_minmax', $this ); 
  13.  
  14. public function admin_field_html (array $raw_properties = array ()) 
  15. global $field; 
  16.  
  17. $args = array( 
  18. 'type' => 'number' 
  19. ); 
  20.  
  21. $options = $field->get_children( true ); 
  22. if ($options) { 
  23. foreach ($options as $o) { 
  24. if (strpos($o->name, 'min_') !== false) { 
  25. $args['min'] = str_replace('min_', '', $o->name); 
  26. if (strpos($o->name, 'max_') !== false) { 
  27. $args['max'] = str_replace('max_', '', $o->name); 
  28.  
  29. $html = $this->get_edit_field_html_elements(array_merge($args, $raw_properties)); 
  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.  
  45. $args = array( 
  46. 'type' => 'number',  
  47. 'value' => bp_get_the_profile_field_edit_value(),  
  48. ); 
  49. $options = $field->get_children( true ); 
  50. if ($options) { 
  51. foreach ($options as $o) { 
  52. if (strpos($o->name, 'min_') !== false) { 
  53. $args['min'] = str_replace('min_', '', $o->name); 
  54. if (strpos($o->name, 'max_') !== false) { 
  55. $args['max'] = str_replace('max_', '', $o->name); 
  56.  
  57. $html = $this->get_edit_field_html_elements(array_merge($args, $raw_properties)); 
  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. $min = ''; 
  85. $max = ''; 
  86. if ( ! $options ) { 
  87. $options = array(); 
  88. $i = 1; 
  89. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  90. $is_default_option = true; 
  91.  
  92. $options[] = (object) array( 
  93. 'id' => -1,  
  94. 'is_default_option' => $is_default_option,  
  95. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  96. ); 
  97.  
  98. ++$i; 
  99.  
  100. if ( ! $options ) { 
  101. $options[] = (object) array( 
  102. 'id' => -1,  
  103. 'is_default_option' => false,  
  104. 'name' => '2',  
  105. ); 
  106. } else { 
  107. foreach ($options as $o) { 
  108. if (strpos($o->name, 'min_') !== false) { 
  109. $min = str_replace('min_', '', $o->name); 
  110. if (strpos($o->name, 'max_') !== false) { 
  111. $max = str_replace('max_', '', $o->name); 
  112. ?> 
  113. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  114. <h3><?php esc_html_e( 'Write min and max values. You can leave any field blank if you want.', 'bxcft' ); ?></h3> 
  115. <div class="inside"> 
  116. <p> 
  117. <label for="<?php echo esc_attr( "{$type}_option1" ); ?>"> 
  118. <?php esc_html_e('Minimum:', 'bxcft'); ?> 
  119. </label> 
  120. <input type="text" name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" 
  121. id="<?php echo esc_attr( "{$type}_option1" ); ?>" value="<?php echo $min; ?>" /> 
  122. <label for="<?php echo esc_attr( "{$type}_option2" ); ?>"> 
  123. <?php esc_html_e('Maximum:', 'bxcft'); ?> 
  124. </label> 
  125. <input type="text" name="<?php echo esc_attr( "{$type}_option[2]" ); ?>" 
  126. id="<?php echo esc_attr( "{$type}_option2" ); ?>" value="<?php echo $max; ?>" /> 
  127. </p> 
  128. </div> 
  129. </div> 
  130. <script> 
  131. var error_msg_number_minmax = '<?php esc_html_e("Min value cannot be bigger than max value.", "bxcft"); ?>',  
  132. error_msg_number_minmax_empty = '<?php esc_html_e("You have to fill at least one field.", "bxcft"); ?>'; 
  133. </script> 
  134. <?php 
  135.  
  136. public function is_valid( $values ) { 
  137. $this->validation_whitelist = null; 
  138. return parent::is_valid($values); 
  139.  
  140. /** 
  141. * Modify the appearance of value. Apply autolink if enabled. 
  142. * @param string $value Original value of field 
  143. * @param int $field_id Id of field 
  144. * @return string Value formatted 
  145. */ 
  146. public static function display_filter($field_value, $field_id = '') { 
  147.  
  148. $new_field_value = $field_value; 
  149.  
  150. if (!empty($field_value)) { 
  151. if (!empty($field_id)) { 
  152. $field = BP_XProfile_Field::get_instance($field_id); 
  153. if ($field) { 
  154. $do_autolink = apply_filters('bxcft_do_autolink',  
  155. $field->get_do_autolink()); 
  156. if ($do_autolink) { 
  157. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  158. $search_url = add_query_arg( array( 
  159. $query_arg => urlencode( $field_value ) 
  160. ), bp_get_members_directory_permalink() ); 
  161. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  162. '" rel="nofollow">' . $new_field_value . '</a>'; 
  163.  
  164. /** 
  165. * bxcft_number_minmax_display_filter 
  166. * Use this filter to modify the appearance of 'Number within 
  167. * min/max values' field value. 
  168. * @param $new_field_value Value of field 
  169. * @param $field_id Id of field. 
  170. * @return Filtered value of field. 
  171. */ 
  172. return apply_filters('bxcft_number_minmax_display_filter',  
  173. $new_field_value, $field_id);