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

Output the edit field options HTML for this field type.

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.

Parameters

$args

(Optional) The arguments passed to bp_the_profile_field_options().

Default value: array()

Source

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

	public function edit_field_options_html( array $args = array() ) {

		$date       = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
		$day        = 0;
		$month      = 0;
		$year       = 0;
		$html       = '';
		$eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );

		// Set day, month, year defaults.
		if ( ! empty( $date ) ) {

			// If Unix timestamp.
			if ( is_numeric( $date ) ) {
				$day   = date( 'j', $date );
				$month = date( 'F', $date );
				$year  = date( 'Y', $date );

			// If MySQL timestamp.
			} else {
				$day   = mysql2date( 'j', $date );
				$month = mysql2date( 'F', $date, false ); // Not localized, so that selected() works below.
				$year  = mysql2date( 'Y', $date );
			}
		}

		// Check for updated posted values, and errors preventing them from
		// being saved first time.
		if ( ! empty( $_POST['field_' . $this->field_obj->id . '_day'] ) ) {
			$new_day = (int) $_POST['field_' . $this->field_obj->id . '_day'];
			$day     = ( $day != $new_day ) ? $new_day : $day;
		}

		if ( ! empty( $_POST['field_' . $this->field_obj->id . '_month'] ) ) {
			if ( in_array( $_POST['field_' . $this->field_obj->id . '_month'], $eng_months ) ) {
				$new_month = $_POST['field_' . $this->field_obj->id . '_month'];
			} else {
				$new_month = $month;
			}

			$month = ( $month !== $new_month ) ? $new_month : $month;
		}

		if ( ! empty( $_POST['field_' . $this->field_obj->id . '_year'] ) ) {
			$new_year = (int) $_POST['field_' . $this->field_obj->id . '_year'];
			$year     = ( $year != $new_year ) ? $new_year : $year;
		}

		// $type will be passed by calling function when needed.
		switch ( $args['type'] ) {
			case 'day':
				$html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $day, 0, false ), /* translators: no option picked in select box */ __( 'Select Day', 'buddyboss' ) );

				for ( $i = 1; $i < 32; ++$i ) {
					$html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $day, $i, false ), (int) $i );
				}
			break;

			case 'month':
				$months = array(
					__( 'January',   'buddyboss' ),
					__( 'February',  'buddyboss' ),
					__( 'March',     'buddyboss' ),
					__( 'April',     'buddyboss' ),
					__( 'May',       'buddyboss' ),
					__( 'June',      'buddyboss' ),
					__( 'July',      'buddyboss' ),
					__( 'August',    'buddyboss' ),
					__( 'September', 'buddyboss' ),
					__( 'October',   'buddyboss' ),
					__( 'November',  'buddyboss' ),
					__( 'December',  'buddyboss' )
				);

				$html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $month, 0, false ), /* translators: no option picked in select box */ __( 'Select Month', 'buddyboss' ) );

				for ( $i = 0; $i < 12; ++$i ) {
					$html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $eng_months[$i] ), selected( $month, $eng_months[$i], false ), $months[$i] );
				}
			break;

			case 'year':
				$html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $year, 0, false ), /* translators: no option picked in select box */ __( 'Select Year', 'buddyboss' ) );

				$settings = $this->get_field_settings( $this->field_obj->id );

				if ( 'relative' === $settings['range_type'] ) {
					$start = date( 'Y' ) + $settings['range_relative_start'];
					$end   = date( 'Y' ) + $settings['range_relative_end'];
				} else {
					$start = $settings['range_absolute_start'];
					$end   = $settings['range_absolute_end'];
				}

				for ( $i = $end; $i >= $start; $i-- ) {
					$html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i );
				}
			break;
		}

		/**
		 * Filters the output for the profile field datebox.
		 *
		 * @since BuddyPress 1.1.0
		 *
		 * @param string $html  HTML output for the field.
		 * @param string $value Which date type is being rendered for.
		 * @param string $day   Date formatted for the current day.
		 * @param string $month Date formatted for the current month.
		 * @param string $year  Date formatted for the current year.
		 * @param int    $id    ID of the field object being rendered.
		 * @param string $date  Current date.
		 */
		echo apply_filters( 'bp_get_the_profile_field_datebox', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date );
	}

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.