BP_XProfile_Field_Type_Checkbox::edit_field_options_html( array $args = array() )

Output the edit field options HTML for this field type.


Description Description

BuddyPress considers a field’s "options" to be, for example, the items in a selectbox. These are stored separately in the database, and their templating is handled separately.

This templating is separate from BP_XProfile_Field_Type::edit_field_html() because it’s also used in the wp-admin screens when creating new fields, and for backwards compatibility.

Must be used inside the bp_profile_fields() template loop.


Top ↑

Parameters Parameters

$args

(array) (Optional) The arguments passed to bp_the_profile_field_options().

Default value: array()


Source Source

File: bp-xprofile/classes/class-bp-xprofile-field-type-checkbox.php

	public function edit_field_options_html( array $args = array() ) {
		$options       = $this->field_obj->get_children();
		$option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );

		/*
		 * Determine whether to pre-select the default option.
		 *
		 * If there's no saved value, take the following into account:
		 * If the user has never saved a value for this field,
		 * $option_values will be an empty string, and we should pre-select the default option.
		 * If the user has specifically chosen none of the options,
		 * $option_values will be an empty array, and we should respect that value.
		 */
		$select_default_option = false;
		if ( empty( $option_values ) && ! is_array( $option_values ) ) {
			$select_default_option = true;
		}

		$option_values = ( $option_values ) ? (array) $option_values : array();

		$html = '';

		// Check for updated posted values, but errors preventing them from
		// being saved first time.
		if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_values != maybe_serialize( $_POST['field_' . $this->field_obj->id] ) ) {
			if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
				$option_values = array_map( 'sanitize_text_field', $_POST['field_' . $this->field_obj->id] );
			}
		}

		for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
			$selected = '';

			// First, check to see whether the user's saved values match the option.
			for ( $j = 0, $count_values = count( $option_values ); $j < $count_values; ++$j ) {

				// Run the allowed option name through the before_save filter,
				// so we'll be sure to get a match.
				$allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );

				if ( $option_values[$j] === $allowed_options || in_array( $allowed_options, $option_values ) ) {
					$selected = ' checked="checked"';
					break;
				}
			}

			// If the user has not yet supplied a value for this field, check to
			// see whether there is a default value available.
			if ( empty( $selected ) && $select_default_option && ! empty( $options[$k]->is_default_option ) ) {
				$selected = ' checked="checked"';
			}

			$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>',
				$selected,
				esc_attr( bp_get_the_profile_field_input_name() . '[]' ),
				esc_attr( "field_{$options[$k]->id}_{$k}" ),
				esc_attr( stripslashes( $options[$k]->name ) ),
				esc_html( stripslashes( $options[$k]->name ) )
			);

			/**
			 * Filters the HTML output for an individual field options checkbox.
			 *
			 * @since 1.1.0
			 *
			 * @param string $new_html Label and checkbox input field.
			 * @param object $value    Current option being rendered for.
			 * @param int    $id       ID of the field object being rendered.
			 * @param string $selected Current selected value.
			 * @param string $k        Current index in the foreach loop.
			 */
			$html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k );
		}

		printf( '<div id="%1$s" class="input-options checkbox-options">%2$s</div>',
			esc_attr( 'field_' . $this->field_obj->id ),
			$html
		);
	}


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.