Skip to content

Instantly share code, notes, and snippets.

@bi0xid
Last active September 23, 2022 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bi0xid/226cc4ec01530d891670 to your computer and use it in GitHub Desktop.
Save bi0xid/226cc4ec01530d891670 to your computer and use it in GitHub Desktop.
BuddyPress Datebox +18 (you have to be at least 18)
<?php
/**
* Datebox +18 xprofile field type.
*
* @since BuddyPress (2.0.1)
* @author bi0xid
*/
class BP_XProfile_Field_Type_Datebox18 extends BP_XProfile_Field_Type {
/**
* Constructor for the datebox +18 field type
*
* @since BuddyPress (2.0.0)
*/
public function __construct() {
parent::__construct();
$this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
$this->name = _x( 'Date Selector +18', '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"
do_action( 'bp_xprofile_field_type_datebox18', $this );
}
/**
* Output the edit field HTML for this field type.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @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.
* @since BuddyPress (2.0.0)
*/
public function edit_field_html( array $raw_properties = array() ) {
$user_id = bp_displayed_user_id();
// 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'] );
}
$day_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_day',
'name' => bp_get_the_profile_field_input_name() . '_day',
),
$raw_properties
) );
$month_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_month',
'name' => bp_get_the_profile_field_input_name() . '_month',
),
$raw_properties
) );
$year_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_year',
'name' => bp_get_the_profile_field_input_name() . '_year',
),
$raw_properties
) );
?>
<div class="datebox">
<label for="<?php bp_the_profile_field_input_name(); ?>_day"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
<?php do_action( bp_get_the_profile_field_errors_action() ); ?>
<select <?php echo $day_html; ?>>
<?php bp_the_profile_field_options( array( 'type' => 'day', 'user_id' => $user_id ) ); ?>
</select>
<select <?php echo $month_html; ?>>
<?php bp_the_profile_field_options( array( 'type' => 'month', 'user_id' => $user_id ) ); ?>
</select>
<select <?php echo $year_html; ?>>
<?php bp_the_profile_field_options( array( 'type' => 'year', 'user_id' => $user_id ) ); ?>
</select>
</div>
<?php
}
/**
* Output the edit field options HTML for this field type.
*
* 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 seperately.
*
* This templating is separate from {@link 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 {@link bp_profile_fields()} template loop.
*
* @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
* @since BuddyPress (2.0.0)
*/
public function edit_field_options_html( array $args = array() ) {
$options = $this->field_obj->get_children();
$date = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
$day = 0;
$month = 0;
$year = 0;
$html = '';
// 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'] ) ) {
$new_month = (int) $_POST['field_' . $this->field_obj->id . '_month'];
$month = ( $month != $new_month ) ? $new_month : $month;
}
if ( ! empty( $_POST['field_' . $this->field_obj->id . '_year'] ) ) {
$new_year = date( 'j', (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 */ __( '----', 'buddypress' ) );
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':
$eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
$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( '<option value="" %1$s>%2$s</option>', selected( $month, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
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 */ __( '----', 'buddypress' ) );
// You need to be +18 to use this
$mayoria_edad = date('Y') - 18;
for ( $i = $mayoria_edad; $i > 1901; $i-- ) {
$html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i );
}
break;
}
echo apply_filters( 'bp_get_the_profile_field_datebox18', $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.
*
* @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
* @since BuddyPress (2.0.0)
*/
public function admin_field_html( array $raw_properties = array() ) {
$day_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_day',
'name' => bp_get_the_profile_field_input_name() . '_day',
),
$raw_properties
) );
$month_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_month',
'name' => bp_get_the_profile_field_input_name() . '_month',
),
$raw_properties
) );
$year_html = $this->get_edit_field_html_elements( array_merge(
array(
'id' => bp_get_the_profile_field_input_name() . '_year',
'name' => bp_get_the_profile_field_input_name() . '_year',
),
$raw_properties
) );
?>
<select <?php echo $day_html; ?>>
<?php bp_the_profile_field_options( 'type=day' ); ?>
</select>
<select <?php echo $month_html; ?>>
<?php bp_the_profile_field_options( 'type=month' ); ?>
</select>
<select <?php echo $year_html; ?>>
<?php bp_the_profile_field_options( 'type=year' ); ?>
</select>
<?php
}
/**
* This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
* "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
*
* @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
* @param string $control_type Optional. HTML input type used to render the current field's child options.
* @since BuddyPress (2.0.0)
*/
public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment