BP_XProfile_Field_Type_Radiobutton

Radio button xprofile field type.

Defined (1)

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

/bp-xprofile/classes/class-bp-xprofile-field-type-radiobutton.php  
  1. class BP_XProfile_Field_Type_Radiobutton extends BP_XProfile_Field_Type { 
  2.  
  3. /** 
  4. * Constructor for the radio button 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( 'Radio Buttons', '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_Radiobutton class. 
  19. * @since 2.0.0 
  20. * @param BP_XProfile_Field_Type_Radiobutton $this Current instance of 
  21. * the field type radio button. 
  22. */ 
  23. do_action( 'bp_xprofile_field_type_radiobutton', $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/input.radio.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. <fieldset class="radio"> 
  45.  
  46. <legend> 
  47. <?php bp_the_profile_field_name(); ?> 
  48. <?php bp_the_profile_field_required_label(); ?> 
  49. </legend> 
  50.  
  51. <?php 
  52.  
  53. /** This action is documented in bp-xprofile/bp-xprofile-classes */ 
  54. do_action( bp_get_the_profile_field_errors_action() ); ?> 
  55.  
  56. <?php bp_the_profile_field_options( array( 'user_id' => $user_id ) ); 
  57.  
  58. if ( ! bp_get_the_profile_field_is_required() ) : ?> 
  59.  
  60. <a class="clear-value" href="javascript:clear( '<?php echo esc_js( bp_get_the_profile_field_input_name() ); ?>' );"> 
  61. <?php esc_html_e( 'Clear', 'buddypress' ); ?> 
  62. </a> 
  63.  
  64. <?php endif; ?> 
  65.  
  66. </fieldset> 
  67.  
  68. <?php 
  69.  
  70. /** 
  71. * Output the edit field options HTML for this field type. 
  72. * BuddyPress considers a field's "options" to be, for example, the items in a selectbox. 
  73. * These are stored separately in the database, and their templating is handled separately. 
  74. * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because 
  75. * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility. 
  76. * Must be used inside the {@link bp_profile_fields()} template loop. 
  77. * @since 2.0.0 
  78. * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. 
  79. */ 
  80. public function edit_field_options_html( array $args = array() ) { 
  81. $option_value = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ); 
  82. $options = $this->field_obj->get_children(); 
  83.  
  84. $html = ''; 
  85.  
  86. for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) { 
  87.  
  88. // Check for updated posted values, but errors preventing them from 
  89. // being saved first time. 
  90. if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_value != $_POST['field_' . $this->field_obj->id] ) { 
  91. if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) { 
  92. $option_value = sanitize_text_field( $_POST['field_' . $this->field_obj->id] ); 
  93.  
  94. // Run the allowed option name through the before_save filter, so 
  95. // we'll be sure to get a match. 
  96. $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false ); 
  97. $selected = ''; 
  98.  
  99. if ( $option_value === $allowed_options || ( empty( $option_value ) && ! empty( $options[$k]->is_default_option ) ) ) { 
  100. $selected = ' checked="checked"'; 
  101.  
  102. $new_html = sprintf( '<label for="%3$s" class="option-label"><input %1$s type="radio" name="%2$s" id="%3$s" value="%4$s">%5$s</label>',  
  103. $selected,  
  104. esc_attr( bp_get_the_profile_field_input_name() ),  
  105. esc_attr( "option_{$options[$k]->id}" ),  
  106. esc_attr( stripslashes( $options[$k]->name ) ),  
  107. esc_html( stripslashes( $options[$k]->name ) ) 
  108. ); 
  109.  
  110. /** 
  111. * Filters the HTML output for an individual field options radio button. 
  112. * @since 1.1.0 
  113. * @param string $new_html Label and radio input field. 
  114. * @param object $value Current option being rendered for. 
  115. * @param int $id ID of the field object being rendered. 
  116. * @param string $selected Current selected value. 
  117. * @param string $k Current index in the foreach loop. 
  118. */ 
  119. $html .= apply_filters( 'bp_get_the_profile_field_options_radio', $new_html, $options[$k], $this->field_obj->id, $selected, $k ); 
  120.  
  121. printf( '<div id="%1$s" class="input-options radio-button-options">%2$s</div>',  
  122. esc_attr( 'field_' . $this->field_obj->id ),  
  123. $html 
  124. ); 
  125.  
  126. /** 
  127. * Output HTML for this field type on the wp-admin Profile Fields screen. 
  128. * Must be used inside the {@link bp_profile_fields()} template loop. 
  129. * @since 2.0.0 
  130. * @param array $raw_properties Optional key/value array of permitted attributes that you want to add. 
  131. */ 
  132. public function admin_field_html( array $raw_properties = array() ) { 
  133. bp_the_profile_field_options(); 
  134.  
  135. if ( bp_get_the_profile_field_is_required() ) { 
  136. return; 
  137. } ?> 
  138.  
  139. <a class="clear-value" href="javascript:clear( '<?php echo esc_js( bp_get_the_profile_field_input_name() ); ?>' );"> 
  140. <?php esc_html_e( 'Clear', 'buddypress' ); ?> 
  141. </a> 
  142.  
  143. <?php 
  144.  
  145. /** 
  146. * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens. 
  147. * Must be used inside the {@link bp_profile_fields()} template loop. 
  148. * @since 2.0.0 
  149. * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. 
  150. * @param string $control_type Optional. HTML input type used to render the current 
  151. * field's child options. 
  152. */ 
  153. public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { 
  154. parent::admin_new_field_html( $current_field, 'radio' );