BP_XProfile_Field_Type_Checkbox

Checkbox xprofile field type.

Defined (1)

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

/bp-xprofile/classes/class-bp-xprofile-field-type-checkbox.php  
  1. class BP_XProfile_Field_Type_Checkbox extends BP_XProfile_Field_Type { 
  2.  
  3. /** 
  4. * Constructor for the checkbox 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( 'Checkboxes', '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_Checkbox class. 
  21. * @since 2.0.0 
  22. * @param BP_XProfile_Field_Type_Checkbox $this Current instance of 
  23. * the field type checkbox. 
  24. */ 
  25. do_action( 'bp_xprofile_field_type_checkbox', $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/input.checkbox.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.  
  46. <fieldset class="checkbox"> 
  47. <legend> 
  48. <?php bp_the_profile_field_name(); ?> 
  49. <?php bp_the_profile_field_required_label(); ?> 
  50. </legend> 
  51.  
  52. <?php 
  53.  
  54. /** This action is documented in bp-xprofile/bp-xprofile-classes */ 
  55. do_action( bp_get_the_profile_field_errors_action() ); ?> 
  56.  
  57. <?php bp_the_profile_field_options( array( 
  58. 'user_id' => $user_id 
  59. ) ); ?> 
  60.  
  61. </fieldset> 
  62.  
  63. <?php 
  64.  
  65. /** 
  66. * Output the edit field options HTML for this field type. 
  67. * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. 
  68. * These are stored separately in the database, and their templating is handled separately. 
  69. * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because 
  70. * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. 
  71. * Must be used inside the {@link bp_profile_fields()} template loop. 
  72. * @since 2.0.0 
  73. * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. 
  74. */ 
  75. public function edit_field_options_html( array $args = array() ) { 
  76. $options = $this->field_obj->get_children(); 
  77. $option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) ); 
  78.  
  79. /** 
  80. * Determine whether to pre-select the default option. 
  81. * If there's no saved value, take the following into account: 
  82. * If the user has never saved a value for this field,  
  83. * $option_values will be an empty string, and we should pre-select the default option. 
  84. * If the user has specifically chosen none of the options,  
  85. * $option_values will be an empty array, and we should respect that value. 
  86. */ 
  87. $select_default_option = false; 
  88. if ( empty( $option_values ) && ! is_array( $option_values ) ) { 
  89. $select_default_option = true; 
  90.  
  91. $option_values = ( $option_values ) ? (array) $option_values : array(); 
  92.  
  93. $html = ''; 
  94.  
  95. // Check for updated posted values, but errors preventing them from 
  96. // being saved first time. 
  97. if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_values != maybe_serialize( $_POST['field_' . $this->field_obj->id] ) ) { 
  98. if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) { 
  99. $option_values = array_map( 'sanitize_text_field', $_POST['field_' . $this->field_obj->id] ); 
  100.  
  101. for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) { 
  102. $selected = ''; 
  103.  
  104. // First, check to see whether the user's saved values match the option. 
  105. for ( $j = 0, $count_values = count( $option_values ); $j < $count_values; ++$j ) { 
  106.  
  107. // Run the allowed option name through the before_save filter,  
  108. // so we'll be sure to get a match. 
  109. $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false ); 
  110.  
  111. if ( $option_values[$j] === $allowed_options || in_array( $allowed_options, $option_values ) ) { 
  112. $selected = ' checked="checked"'; 
  113. break; 
  114.  
  115. // If the user has not yet supplied a value for this field, check to 
  116. // see whether there is a default value available. 
  117. if ( empty( $selected ) && $select_default_option && ! empty( $options[$k]->is_default_option ) ) { 
  118. $selected = ' checked="checked"'; 
  119.  
  120. $new_html = sprintf( '<label for="%3$s" class="option-label"><input %1$s type="checkbox" name="%2$s" id="%3$s" value="%4$s">%5$s</label>',  
  121. $selected,  
  122. esc_attr( bp_get_the_profile_field_input_name() . '[]' ),  
  123. esc_attr( "field_{$options[$k]->id}_{$k}" ),  
  124. esc_attr( stripslashes( $options[$k]->name ) ),  
  125. esc_html( stripslashes( $options[$k]->name ) ) 
  126. ); 
  127.  
  128. /** 
  129. * Filters the HTML output for an individual field options checkbox. 
  130. * @since 1.1.0 
  131. * @param string $new_html Label and checkbox input field. 
  132. * @param object $value Current option being rendered for. 
  133. * @param int $id ID of the field object being rendered. 
  134. * @param string $selected Current selected value. 
  135. * @param string $k Current index in the foreach loop. 
  136. */ 
  137. $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k ); 
  138.  
  139. printf( '<div id="%1$s" class="input-options checkbox-options">%2$s</div>',  
  140. esc_attr( 'field_' . $this->field_obj->id ),  
  141. $html 
  142. ); 
  143.  
  144. /** 
  145. * Output HTML for this field type on the wp-admin Profile Fields screen. 
  146. * Must be used inside the {@link bp_profile_fields()} template loop. 
  147. * @since 2.0.0 
  148. * @param array $raw_properties Optional key/value array of permitted attributes that you want to add. 
  149. */ 
  150. public function admin_field_html( array $raw_properties = array() ) { 
  151. bp_the_profile_field_options(); 
  152.  
  153. /** 
  154. * Output HTML for this field type's children options on the wp-admin Profile Fields "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' );