/classes/Bxcft_Field_Type_Slider.php

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