BP_XProfile_Field_Type_Selectbox

Selectbox xprofile field type.

Defined (1)

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

/bp-xprofile/classes/class-bp-xprofile-field-type-selectbox.php  
  1. class BP_XProfile_Field_Type_Selectbox extends BP_XProfile_Field_Type { 
  2.  
  3. /** 
  4. * Constructor for the selectbox field type. 
  5. * @since 2.0.0 
  6. */ 
  7. public function __construct() { 
  8. parent::__construct(); 
  9.  
  10. $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' ); 
  11. $this->name = _x( 'Drop Down Select Box', 'xprofile field type', 'buddypress' ); 
  12.  
  13. $this->supports_options = true; 
  14.  
  15. $this->set_format( '/^.+$/', 'replace' ); 
  16.  
  17. /** 
  18. * Fires inside __construct() method for BP_XProfile_Field_Type_Selectbox class. 
  19. * @since 2.0.0 
  20. * @param BP_XProfile_Field_Type_Selectbox $this Current instance of 
  21. * the field type select box. 
  22. */ 
  23. do_action( 'bp_xprofile_field_type_selectbox', $this ); 
  24.  
  25. /** 
  26. * Output the edit field HTML for this field type. 
  27. * Must be used inside the {@link bp_profile_fields()} template loop. 
  28. * @since 2.0.0 
  29. * @param array $raw_properties Optional key/value array of 
  30. * {@link http://dev.w3.org/html5/markup/select.html permitted attributes} 
  31. * that you want to add. 
  32. */ 
  33. public function edit_field_html( array $raw_properties = array() ) { 
  34.  
  35. // User_id is a special optional parameter that we pass to 
  36. // {@link bp_the_profile_field_options()}. 
  37. if ( isset( $raw_properties['user_id'] ) ) { 
  38. $user_id = (int) $raw_properties['user_id']; 
  39. unset( $raw_properties['user_id'] ); 
  40. } else { 
  41. $user_id = bp_displayed_user_id(); 
  42. } ?> 
  43.  
  44. <label for="<?php bp_the_profile_field_input_name(); ?>"> 
  45. <?php bp_the_profile_field_name(); ?> 
  46. <?php bp_the_profile_field_required_label(); ?> 
  47. </label> 
  48.  
  49. <?php 
  50.  
  51. /** This action is documented in bp-xprofile/bp-xprofile-classes */ 
  52. do_action( bp_get_the_profile_field_errors_action() ); ?> 
  53.  
  54. <select <?php echo $this->get_edit_field_html_elements( $raw_properties ); ?>> 
  55. <?php bp_the_profile_field_options( array( 'user_id' => $user_id ) ); ?> 
  56. </select> 
  57.  
  58. <?php 
  59.  
  60. /** 
  61. * Output the edit field options HTML for this field type. 
  62. * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. 
  63. * These are stored separately in the database, and their templating is handled separately. 
  64. * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because 
  65. * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. 
  66. * Must be used inside the {@link bp_profile_fields()} template loop. 
  67. * @since 2.0.0 
  68. * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. 
  69. */ 
  70. public function edit_field_options_html( array $args = array() ) { 
  71. $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) ); 
  72.  
  73. $options = $this->field_obj->get_children(); 
  74. $html = '<option value="">' . /** translators: no option picked in select box */ esc_html__( '----', 'buddypress' ) . '</option>'; 
  75.  
  76. if ( empty( $original_option_values ) && !empty( $_POST['field_' . $this->field_obj->id] ) ) { 
  77. $original_option_values = sanitize_text_field( $_POST['field_' . $this->field_obj->id] ); 
  78.  
  79. $option_values = ( $original_option_values ) ? (array) $original_option_values : array(); 
  80. for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) { 
  81. $selected = ''; 
  82.  
  83. // Check for updated posted values, but errors preventing them from 
  84. // being saved first time. 
  85. foreach( $option_values as $i => $option_value ) { 
  86. if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id] != $option_value ) { 
  87. if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) { 
  88. $option_values[$i] = sanitize_text_field( $_POST['field_' . $this->field_obj->id] ); 
  89.  
  90. // Run the allowed option name through the before_save filter, so 
  91. // we'll be sure to get a match. 
  92. $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false ); 
  93.  
  94. // First, check to see whether the user-entered value matches. 
  95. if ( in_array( $allowed_options, $option_values ) ) { 
  96. $selected = ' selected="selected"'; 
  97.  
  98. // Then, if the user has not provided a value, check for defaults. 
  99. if ( ! is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option ) { 
  100. $selected = ' selected="selected"'; 
  101.  
  102. /** 
  103. * Filters the HTML output for options in a select input. 
  104. * @since 1.1.0 
  105. * @param string $value Option tag for current value being rendered. 
  106. * @param object $value Current option being rendered for. 
  107. * @param int $id ID of the field object being rendered. 
  108. * @param string $selected Current selected value. 
  109. * @param string $k Current index in the foreach loop. 
  110. */ 
  111. $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_html( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k], $this->field_obj->id, $selected, $k ); 
  112.  
  113. echo $html; 
  114.  
  115. /** 
  116. * Output HTML for this field type on the wp-admin Profile Fields screen. 
  117. * Must be used inside the {@link bp_profile_fields()} template loop. 
  118. * @since 2.0.0 
  119. * @param array $raw_properties Optional key/value array of permitted attributes that you want to add. 
  120. */ 
  121. public function admin_field_html( array $raw_properties = array() ) { 
  122. ?> 
  123.  
  124. <label for="<?php bp_the_profile_field_input_name(); ?>" class="screen-reader-text"><?php 
  125. /** translators: accessibility text */ 
  126. esc_html_e( 'Select', 'buddypress' ); 
  127. ?></label> 
  128. <select <?php echo $this->get_edit_field_html_elements( $raw_properties ); ?>> 
  129. <?php bp_the_profile_field_options(); ?> 
  130. </select> 
  131.  
  132. <?php 
  133.  
  134. /** 
  135. * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens. 
  136. * Must be used inside the {@link bp_profile_fields()} template loop. 
  137. * @since 2.0.0 
  138. * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. 
  139. * @param string $control_type Optional. HTML input type used to render the current 
  140. * field's child options. 
  141. */ 
  142. public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { 
  143. parent::admin_new_field_html( $current_field, 'radio' );