Bxcft_Field_Type_Slider

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

Defined (1)

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

/classes/Bxcft_Field_Type_Slider.php  
  1. class Bxcft_Field_Type_Slider extends BP_XProfile_Field_Type 
  2. public function __construct() { 
  3. parent::__construct(); 
  4.  
  5. $this->name = __( 'Range input (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_slider', $this ); 
  13.  
  14. public function admin_field_html (array $raw_properties = array ()) 
  15. global $field; 
  16.  
  17. $args = array( 
  18. 'type' => 'range',  
  19. 'class' => 'bxcft-slider' 
  20. ); 
  21.  
  22. $options = $field->get_children( true ); 
  23. if ($options) { 
  24. foreach ($options as $o) { 
  25. if (strpos($o->name, 'min_') !== false) { 
  26. $args['min'] = str_replace('min_', '', $o->name); 
  27. if (strpos($o->name, 'max_') !== false) { 
  28. $args['max'] = str_replace('max_', '', $o->name); 
  29.  
  30. $html = $this->get_edit_field_html_elements(array_merge($args, $raw_properties)); 
  31. ?> 
  32. <input <?php echo $html; ?> /> 
  33. <?php 
  34.  
  35. public function edit_field_html (array $raw_properties = array ()) 
  36. if ( isset( $raw_properties['user_id'] ) ) { 
  37. unset( $raw_properties['user_id'] ); 
  38.  
  39. // HTML5 required attribute. 
  40. if ( bp_get_the_profile_field_is_required() ) { 
  41. $raw_properties['required'] = 'required'; 
  42.  
  43. $field = new BP_XProfile_Field(bp_get_the_profile_field_id()); 
  44.  
  45.  
  46. $args = array( 
  47. 'type' => 'range',  
  48. 'class' => 'bxcft-slider',  
  49. 'value' => bp_get_the_profile_field_edit_value(),  
  50. ); 
  51. $options = $field->get_children( true ); 
  52. if ($options) { 
  53. foreach ($options as $o) { 
  54. if (strpos($o->name, 'min_') !== false) { 
  55. $args['min'] = str_replace('min_', '', $o->name); 
  56. if (strpos($o->name, 'max_') !== false) { 
  57. $args['max'] = str_replace('max_', '', $o->name); 
  58.  
  59. $html = $this->get_edit_field_html_elements(array_merge($args, $raw_properties)); 
  60.  
  61. $label = sprintf( 
  62. '<label for="%s">%s%s</label>',  
  63. bp_get_the_profile_field_input_name(),  
  64. bp_get_the_profile_field_name(),  
  65. (bp_get_the_profile_field_is_required()) ? 
  66. ' ' . esc_html__( '(required)', 'buddypress' ) : '' 
  67. ); 
  68. // Label. 
  69. 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()); 
  70. // Errors. 
  71. do_action( bp_get_the_profile_field_errors_action() ); 
  72. // Input. 
  73. ?> 
  74. <input <?php echo $html; ?> /><span id="output-field_<?php echo $field->id; ?>"></span> 
  75. <?php 
  76.  
  77. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  78. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  79. if ( false === $type ) { 
  80. return; 
  81.  
  82. $class = $current_field->type != $type ? 'display: none;' : ''; 
  83. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  84.  
  85. $options = $current_field->get_children( true ); 
  86. $min = ''; 
  87. $max = ''; 
  88. if ( ! $options ) { 
  89. $options = array(); 
  90. $i = 1; 
  91. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  92. $is_default_option = true; 
  93.  
  94. $options[] = (object) array( 
  95. 'id' => -1,  
  96. 'is_default_option' => $is_default_option,  
  97. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  98. ); 
  99.  
  100. ++$i; 
  101.  
  102. if ( ! $options ) { 
  103. $options[] = (object) array( 
  104. 'id' => -1,  
  105. 'is_default_option' => false,  
  106. 'name' => '2',  
  107. ); 
  108. } else { 
  109. foreach ($options as $o) { 
  110. if (strpos($o->name, 'min_') !== false) { 
  111. $min = str_replace('min_', '', $o->name); 
  112. if (strpos($o->name, 'max_') !== false) { 
  113. $max = str_replace('max_', '', $o->name); 
  114. ?> 
  115. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  116. <h3><?php esc_html_e( 'Write min and max values.', 'bxcft' ); ?></h3> 
  117. <div class="inside"> 
  118. <p> 
  119. <label for="<?php echo esc_attr( "{$type}_option1" ); ?>"> 
  120. <?php esc_html_e('Minimum:', 'bxcft'); ?> 
  121. </label> 
  122. <input type="text" name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" 
  123. id="<?php echo esc_attr( "{$type}_option1" ); ?>" value="<?php echo $min; ?>" /> 
  124. <label for="<?php echo esc_attr( "{$type}_option2" ); ?>"> 
  125. <?php esc_html_e('Maximum:', 'bxcft'); ?> 
  126. </label> 
  127. <input type="text" name="<?php echo esc_attr( "{$type}_option[2]" ); ?>" 
  128. id="<?php echo esc_attr( "{$type}_option2" ); ?>" value="<?php echo $max; ?>" /> 
  129. </p> 
  130. </div> 
  131. </div> 
  132. <script> 
  133. var error_msg_slider = '<?php esc_html_e("Min value cannot be bigger than max value.", "bxcft"); ?>',  
  134. error_msg_slider_empty = '<?php esc_html_e("You have to fill the two fields.", "bxcft"); ?>'; 
  135. </script> 
  136. <?php 
  137.  
  138. public function is_valid( $values ) { 
  139. $this->validation_whitelist = null; 
  140. return parent::is_valid($values); 
  141.  
  142. /** 
  143. * Modify the appearance of value. Apply autolink if enabled. 
  144. * @param string $value Original value of field 
  145. * @param int $field_id Id of field 
  146. * @return string Value formatted 
  147. */ 
  148. public static function display_filter($field_value, $field_id = '') { 
  149.  
  150. $new_field_value = $field_value; 
  151.  
  152. if (!empty($field_value)) { 
  153. if (!empty($field_id)) { 
  154. $field = BP_XProfile_Field::get_instance($field_id); 
  155. if ($field) { 
  156. $do_autolink = apply_filters('bxcft_do_autolink',  
  157. $field->get_do_autolink()); 
  158. if ($do_autolink) { 
  159. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  160. $search_url = add_query_arg( array( 
  161. $query_arg => urlencode( $field_value ) 
  162. ), bp_get_members_directory_permalink() ); 
  163. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  164. '" rel="nofollow">' . $new_field_value . '</a>'; 
  165.  
  166. /** 
  167. * bxcft_slider_display_filter 
  168. * Use this filter to modify the appearance of 'Slider' 
  169. * field value. 
  170. * @param $new_field_value Value of field 
  171. * @param $field_id Id of field. 
  172. * @return Filtered value of field. 
  173. */ 
  174. return apply_filters('bxcft_slider_display_filter',  
  175. $new_field_value, $field_id);