category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
$this->name = _x( 'Date Selector', 'xprofile field type', 'buddypress' );
$this->set_format( '/^\d{4}-\d{1,2}-\d{1,2} 00:00:00$/', 'replace' ); // "Y-m-d 00:00:00"
/**
* Fires inside __construct() method for BP_XProfile_Field_Type_Datebox class.
*
* @since 2.0.0
*
* @param BP_XProfile_Field_Type_Datebox $this Current instance of
* the field type datebox.
*/
do_action( 'bp_xprofile_field_type_datebox', $this );
}
/**
* Output the edit field HTML for this field type.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @since 2.0.0
*
* @param array $raw_properties Optional key/value array of
* {@link http://dev.w3.org/html5/markup/input.html permitted attributes}
* that you want to add.
*/
public function edit_field_html( array $raw_properties = array() ) {
// User_id is a special optional parameter that we pass to.
// {@link bp_the_profile_field_options()}.
if ( isset( $raw_properties['user_id'] ) ) {
$user_id = (int) $raw_properties['user_id'];
unset( $raw_properties['user_id'] );
} else {
$user_id = bp_displayed_user_id();
}
$day_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_day',
'name' => bp_get_the_profile_field_input_name() . '_day'
) );
$month_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_month',
'name' => bp_get_the_profile_field_input_name() . '_month'
) );
$year_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_year',
'name' => bp_get_the_profile_field_input_name() . '_year'
) ); ?>
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( '', selected( $day, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
for ( $i = 1; $i < 32; ++$i ) {
$html .= sprintf( '', (int) $i, selected( $day, $i, false ), (int) $i );
}
break;
case 'month':
$months = array(
__( 'January', 'buddypress' ),
__( 'February', 'buddypress' ),
__( 'March', 'buddypress' ),
__( 'April', 'buddypress' ),
__( 'May', 'buddypress' ),
__( 'June', 'buddypress' ),
__( 'July', 'buddypress' ),
__( 'August', 'buddypress' ),
__( 'September', 'buddypress' ),
__( 'October', 'buddypress' ),
__( 'November', 'buddypress' ),
__( 'December', 'buddypress' )
);
$html = sprintf( '', selected( $month, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
for ( $i = 0; $i < 12; ++$i ) {
$html .= sprintf( '', esc_attr( $eng_months[$i] ), selected( $month, $eng_months[$i], false ), $months[$i] );
}
break;
case 'year':
$html = sprintf( '', selected( $year, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
for ( $i = 2037; $i > 1901; $i-- ) {
$html .= sprintf( '', (int) $i, selected( $year, $i, false ), (int) $i );
}
break;
}
/**
* Filters the output for the profile field datebox.
*
* @since 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 );
}
/**
* Output HTML for this field type on the wp-admin Profile Fields screen.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @since 2.0.0
*
* @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
*/
public function admin_field_html( array $raw_properties = array() ) {
$day_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_day',
'name' => bp_get_the_profile_field_input_name() . '_day'
) );
$month_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_month',
'name' => bp_get_the_profile_field_input_name() . '_month'
) );
$year_r = bp_parse_args( $raw_properties, array(
'id' => bp_get_the_profile_field_input_name() . '_year',
'name' => bp_get_the_profile_field_input_name() . '_year'
) ); ?>