BP_XProfile_Field_Type_Multiselectbox

Multi-selectbox xprofile field type.

Defined (1)

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

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