Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/27/2014 07:34:02 PM (11 years ago)
Author:
djpaul
Message:

xProfile: re-architecture profile field types and de-duplicate the templating and validation logic to make it easier for core and plugins to add new profile field types.

Until now, it's been pretty hard to add new types of profile field to BuddyPress. There are a couple of plugins that do a good job, but BuddyPress makes it much harder than it should be because, historically, we've hardcoded values and checks in templates throughout the project. For example, profile field templating was duplicated in the registration template, the member/profile/edit template, in parts of the wp-admin xProfile screens, and in the new wp-admin extended profile editor.

This change implements a new approach that creates a class for each profile field type; selectbox, textbox, textarea, and so on. They all share an abstract base class BP_XProfile_Field_Type which consolidates a lot of special behaviour that had been added to BuddyPress over the years (e.g. some fields accept null values, some accept multiple default values), and adds true field value validation. Unit tests are included.

We've also implemented a new "Numbers" field type with these changes. It behaves much the same as a regular textbox field does, but it only accepts numbers.

Fixes #5220 and #4694

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r8165 r8178  
    542542    public $allow_custom_visibility = 'allowed';
    543543
     544    /**
     545     * @since BuddyPress (2.0.0)
     546     * @var BP_XProfile_Field_Type Field type object used for validation
     547     */
     548    public $type_obj = null;
     549
    544550    public $data;
    545551    public $message = null;
     
    547553
    548554    public function __construct( $id = null, $user_id = null, $get_data = true ) {
    549         if ( !empty( $id ) )
     555        if ( !empty( $id ) ) {
    550556            $this->populate( $id, $user_id, $get_data );
     557
     558        // Initialise the type obj to prevent fatals when creating new profile fields
     559        } else {
     560            $this->type_obj            = bp_xprofile_create_field_type( 'textbox' );
     561            $this->type_obj->field_obj = $this;
     562        }
    551563    }
    552564
     
    574586            $this->is_default_option = $field->is_default_option;
    575587
     588            // Create the field type and store a reference back to this object.
     589            $this->type_obj            = bp_xprofile_create_field_type( $field->type );
     590            $this->type_obj->field_obj = $this;
     591
    576592            if ( $get_data && $user_id ) {
    577593                $this->data          = $this->get_field_data( $user_id );
     
    619635        $this->field_order = apply_filters( 'xprofile_field_field_order_before_save', $this->field_order, $this->id );
    620636        $this->can_delete  = apply_filters( 'xprofile_field_can_delete_before_save',  $this->can_delete,  $this->id );
     637        $this->type_obj    = bp_xprofile_create_field_type( $this->type );
    621638
    622639        do_action_ref_array( 'xprofile_field_before_save', array( $this ) );
     
    656673             * We need to add the options to the db, if it is.
    657674             */
    658             if ( 'radio' == $this->type || 'selectbox' == $this->type || 'checkbox' == $this->type || 'multiselectbox' == $this->type ) {
     675            if ( $this->type_obj->supports_options ) {
    659676
    660677                if ( !empty( $this->id ) ) {
     
    664681                }
    665682
    666                 if ( 'radio' == $this->type ) {
    667                     $post_option  = !empty( $_POST['radio_option']           ) ? $_POST['radio_option']           : '';
    668                     $post_default = !empty( $_POST['isDefault_radio_option'] ) ? $_POST['isDefault_radio_option'] : '';
    669 
    670                     $options    = apply_filters( 'xprofile_field_options_before_save', $post_option,  'radio' );
    671                     $defaults   = apply_filters( 'xprofile_field_default_before_save', $post_default, 'radio' );
    672 
    673                 } elseif ( 'selectbox' == $this->type ) {
    674                     $post_option  = !empty( $_POST['selectbox_option']           ) ? $_POST['selectbox_option']           : '';
    675                     $post_default = !empty( $_POST['isDefault_selectbox_option'] ) ? $_POST['isDefault_selectbox_option'] : '';
    676 
    677                     $options    = apply_filters( 'xprofile_field_options_before_save', $post_option, 'selectbox' );
    678                     $defaults   = apply_filters( 'xprofile_field_default_before_save', $post_default, 'selectbox' );
    679 
    680                 } elseif ( 'multiselectbox' == $this->type ) {
    681                     $post_option  = !empty( $_POST['multiselectbox_option']           ) ? $_POST['multiselectbox_option']           : '';
    682                     $post_default = !empty( $_POST['isDefault_multiselectbox_option'] ) ? $_POST['isDefault_multiselectbox_option'] : '';
    683 
    684                     $options    = apply_filters( 'xprofile_field_options_before_save', $post_option, 'multiselectbox' );
    685                     $defaults   = apply_filters( 'xprofile_field_default_before_save', $post_default, 'multiselectbox' );
    686 
    687                 } elseif ( 'checkbox' == $this->type ) {
    688                     $post_option  = !empty( $_POST['checkbox_option']           ) ? $_POST['checkbox_option']           : '';
    689                     $post_default = !empty( $_POST['isDefault_checkbox_option'] ) ? $_POST['isDefault_checkbox_option'] : '';
    690 
    691                     $options    = apply_filters( 'xprofile_field_options_before_save', $post_option, 'checkbox' );
    692                     $defaults   = apply_filters( 'xprofile_field_default_before_save', $post_default, 'checkbox' );
    693                 }
     683                // Allow plugins to filter the field's child options (i.e. the items in a selectbox).
     684                $post_option  = ! empty( $_POST["{$this->type}_option"] ) ? $_POST["{$this->type}_option"] : '';
     685                $post_default = ! empty( $_POST["isDefault_{$this->type}_option"] ) ? $_POST["isDefault_{$this->type}_option"] : '';
     686                $options      = apply_filters( 'xprofile_field_options_before_save', $post_option,  $this->type );
     687                $defaults     = apply_filters( 'xprofile_field_default_before_save', $post_default, $this->type );
    694688
    695689                $counter = 1;
     
    719713            do_action_ref_array( 'xprofile_field_after_save', array( $this ) );
    720714
     715            // Recreate type_obj in case someone changed $this->type via a filter
     716            $this->type_obj            = bp_xprofile_create_field_type( $this->type );
     717            $this->type_obj->field_obj = $this;
     718
    721719            return $field_id;
    722720        } else {
     
    825823    }
    826824
    827     /* ADMIN AREA HTML.
    828     * TODO: Get this out of here and replace with standard template loops
    829     */
    830 
    831     /* This function populates the items for radio buttons checkboxes and drop down boxes */
     825    /**
     826     * This function populates the items for radio buttons checkboxes and drop down boxes
     827     */
    832828    public function render_admin_form_children() {
    833         $input_types = array( 'checkbox', 'selectbox', 'multiselectbox', 'radio' );
    834 
    835         foreach ( $input_types as $type ) {
    836             $default_name = '';
    837 
    838             if ( ( 'multiselectbox' == $type ) || ( 'checkbox' == $type ) ) {
    839                 $default_input = 'checkbox';
    840             } else {
    841                 $default_input = 'radio';
    842             }
    843 
    844             $class = $this->type != $type ? 'display: none;' : '';
    845 
    846             if ( empty( $this->default_visibility ) ) {
    847                 $this->default_visibility = 'public';
    848             }
    849 
    850             ?>
    851 
    852             <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;">
    853                 <h3><?php _e( 'Please enter options for this Field:', 'buddypress' ); ?></h3>
    854                 <div class="inside">
    855                     <p>
    856                         <label for="sort_order_<?php echo esc_attr( $type ); ?>"><?php _e( 'Sort Order:', 'buddypress' ); ?></label>
    857                         <select name="sort_order_<?php echo esc_attr( $type ); ?>" id="sort_order_<?php echo esc_attr( $type ); ?>" >
    858                             <option value="custom" <?php selected( 'custom', $this->order_by ); ?>><?php _e( 'Custom',     'buddypress' ); ?></option>
    859                             <option value="asc"    <?php selected( 'asc',    $this->order_by ); ?>><?php _e( 'Ascending',  'buddypress' ); ?></option>
    860                             <option value="desc"   <?php selected( 'desc',   $this->order_by ); ?>><?php _e( 'Descending', 'buddypress' ); ?></option>
    861                         </select>
    862                     </p>
    863 
    864                     <?php if ( !$options = $this->get_children( true ) ) {
    865 
    866                         $i = 1;
    867                         while ( isset( $_POST[$type . '_option'][$i] ) ) {
    868                             (array) $options[] = (object) array(
    869                                 'id'                => -1,
    870                                 'name'              => $_POST[$type . '_option'][$i],
    871                                 'is_default_option' => ( ( 'multiselectbox' != $type ) && ( 'checkbox' != $type ) && ( $_POST["isDefault_{$type}_option"] == $i ) ) ? 1 : $_POST["isDefault_{$type}_option"][$i]
    872                             );
    873 
    874                             ++$i;
    875                         }
    876                     }
    877 
    878                     if ( !empty( $options ) ) {
    879                         for ( $i = 0, $count = count( $options ); $i < $count; ++$i ) {
    880                             $j = $i + 1;
    881 
    882                             if ( 'multiselectbox' == $type || 'checkbox' == $type )
    883                                 $default_name = '[' . $j . ']'; ?>
    884 
    885                             <p class="sortable">
    886                                 <span>&nbsp;&Xi;&nbsp;</span>
    887                                 <input type="text" name="<?php echo esc_attr( $type ); ?>_option[<?php echo esc_attr( $j ); ?>]" id="<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $j ); ?>" value="<?php echo stripslashes( esc_attr( $options[$i]->name ) ); ?>" />
    888                                 <input type="<?php echo $default_input; ?>" name="isDefault_<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $default_name ); ?>" <?php checked( (int) $options[$i]->is_default_option, true ); ?> value="<?php echo esc_attr( $j ); ?>" />
    889                                 <span><?php _e( 'Default Value', 'buddypress' ); ?></span>
    890                                 <a href="<?php echo esc_url( 'users.php?page=bp-profile-setup&amp;mode=delete_option&amp;option_id=' . $options[$i]->id ); ?>" class="ajax-option-delete" id="delete-<?php echo esc_attr( $options[$i]->id ); ?>">[x]</a>
    891                             </p>
    892 
    893                         <?php } /* end for */ ?>
    894 
    895                         <input type="hidden" name="<?php echo esc_attr( $type ); ?>_option_number" id="<?php echo esc_attr( $type ); ?>_option_number" value="<?php echo esc_attr( (int) $j + 1 ); ?>" />
    896 
    897                     <?php } else {
    898 
    899                         if ( 'multiselectbox' == $type || 'checkbox' == $type )
    900                             $default_name = '[1]'; ?>
    901 
    902                         <p class="sortable">
    903                             <span>&nbsp;&Xi;&nbsp;</span>
    904                             <input type="text" name="<?php echo esc_attr( $type ); ?>_option[1]" id="<?php echo esc_attr( $type ); ?>_option1" />
    905                             <input type="<?php echo esc_attr( $default_input ); ?>" name="isDefault_<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $default_name ); ?>" id="isDefault_<?php echo esc_attr( $type ); ?>_option" value="1" />
    906                             <span><?php _e( 'Default Value', 'buddypress' ); ?></span>
    907                             <input type="hidden" name="<?php echo esc_attr( $type ); ?>_option_number" id="<?php echo esc_attr( $type ); ?>_option_number" value="2" />
    908                         </p>
    909 
    910                     <?php } /* end if */ ?>
    911 
    912                     <div id="<?php echo esc_attr( $type ); ?>_more"></div>
    913                     <p><a href="javascript:add_option('<?php echo esc_attr( $type ); ?>')"><?php _e( 'Add Another Option', 'buddypress' ); ?></a></p>
    914                 </div>
    915             </div>
    916 
    917         <?php }
     829        foreach ( array_keys( bp_xprofile_get_field_types() ) as $field_type ) {
     830            $type_obj = bp_xprofile_create_field_type( $field_type );
     831            $type_obj->admin_new_field_html( $this );
     832        }
    918833    }
    919834
     
    1042957                                    <div class="inside">
    1043958                                        <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)" style="width: 30%">
    1044                                             <optgroup label="<?php esc_attr_e( 'Single Fields', 'buddypress' ); ?>">
    1045                                                 <option value="textbox"        <?php selected( $this->type, 'textbox'        ); ?>><?php _e( 'Text Box',             'buddypress' ); ?></option>
    1046                                                 <option value="textarea"       <?php selected( $this->type, 'textarea'       ); ?>><?php _e( 'Multi-line Text Area', 'buddypress' ); ?></option>
    1047                                                 <option value="datebox"        <?php selected( $this->type, 'datebox'        ); ?>><?php _e( 'Date Selector',        'buddypress' ); ?></option>
    1048                                             </optgroup>
    1049                                             <optgroup label="<?php esc_attr_e( 'Multi Fields', 'buddypress' ); ?>">
    1050                                                 <option value="radio"          <?php selected( $this->type, 'radio'          ); ?>><?php _e( 'Radio Buttons',        'buddypress' ); ?></option>
    1051                                                 <option value="selectbox"      <?php selected( $this->type, 'selectbox'      ); ?>><?php _e( 'Drop Down Select Box', 'buddypress' ); ?></option>
    1052                                                 <option value="multiselectbox" <?php selected( $this->type, 'multiselectbox' ); ?>><?php _e( 'Multi Select Box',     'buddypress' ); ?></option>
    1053                                                 <option value="checkbox"       <?php selected( $this->type, 'checkbox'       ); ?>><?php _e( 'Checkboxes',           'buddypress' ); ?></option>
    1054                                             </optgroup>
     959                                            <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
    1055960                                        </select>
    1056961
    1057                                         <?php do_action_ref_array( 'xprofile_field_additional_options', array( $this ) ); ?>
    1058 
    1059                                         <?php $this->render_admin_form_children(); ?>
    1060 
     962                                        <?php
     963                                        // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
     964                                        do_action( 'xprofile_field_additional_options', $this );
     965
     966                                        $this->render_admin_form_children();
     967                                        ?>
    1061968                                    </div>
    1062969                                </div>
     
    1087994            $message = __( 'Please make sure you fill out all required fields.', 'buddypress' );
    1088995            return false;
    1089         } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'radio' && empty( $_POST['radio_option'][1] ) ) {
    1090             $message = __( 'Radio button field types require at least one option. Please add options below.', 'buddypress' );
    1091             return false;
    1092         } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'selectbox' && empty( $_POST['selectbox_option'][1] ) ) {
    1093             $message = __( 'Select box field types require at least one option. Please add options below.', 'buddypress' );
    1094             return false;
    1095         } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'multiselectbox' && empty( $_POST['multiselectbox_option'][1] ) ) {
    1096             $message = __( 'Select box field types require at least one option. Please add options below.', 'buddypress' );
    1097             return false;
    1098         } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'checkbox' && empty( $_POST['checkbox_option'][1] ) ) {
    1099             $message = __( 'Checkbox field types require at least one option. Please add options below.', 'buddypress' );
    1100             return false;
    1101         } else {
    1102             return true;
    1103         }
     996
     997        } elseif ( empty( $_POST['field_file'] ) ) {
     998            $field_type  = bp_xprofile_create_field_type( $_POST['fieldtype'] );
     999            $option_name = "{$_POST['fieldtype']}_option";
     1000
     1001            if ( $field_type->supports_options && isset( $_POST[$option_name] ) && empty( $_POST[$option_name][1] ) ) {
     1002                $message = __( 'This field type require at least one option. Please add options below.', 'buddypress' );
     1003                return false;
     1004            }
     1005        }
     1006
     1007        return true;
    11041008    }
    11051009}
    1106 
    11071010
    11081011class BP_XProfile_ProfileData {
     
    11961099
    11971100        if ( $this->is_valid_field() ) {
    1198             if ( $this->exists() && !empty( $this->value ) && strlen( trim( $this->value ) ) ) {
     1101            if ( $this->exists() && strlen( trim( $this->value ) ) ) {
    11991102                $result   = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_data} SET value = %s, last_updated = %s WHERE user_id = %d AND field_id = %d", $this->value, $this->last_updated, $this->user_id, $this->field_id ) );
    12001103
     
    15451448    }
    15461449}
     1450
     1451/**
     1452 * Datebox xprofile field type.
     1453 *
     1454 * @since BuddyPress (2.0.0)
     1455 */
     1456class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type {
     1457
     1458    /**
     1459     * Constructor for the datebox field type
     1460     *
     1461     * @since BuddyPress (2.0.0)
     1462     */
     1463    public function __construct() {
     1464        parent::__construct();
     1465
     1466        $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
     1467        $this->name     = _x( 'Date Selector', 'xprofile field type', 'buddypress' );
     1468
     1469        $this->set_format( '/^\d{4}-\d{1,2}-\d{1,2} 00:00:00$/', 'replace' );  // "Y-m-d 00:00:00"
     1470        do_action( 'bp_xprofile_field_type_datebox', $this );
     1471    }
     1472
     1473    /**
     1474     * Output the edit field HTML for this field type.
     1475     *
     1476     * Must be used inside the {@link bp_profile_fields()} template loop.
     1477     *
     1478     * @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.
     1479     * @since BuddyPress (2.0.0)
     1480     */
     1481    public function edit_field_html( array $raw_properties = array() ) {
     1482        $user_id = bp_displayed_user_id();
     1483
     1484        // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}.
     1485        if ( isset( $raw_properties['user_id'] ) ) {
     1486            $user_id = (int) $raw_properties['user_id'];
     1487            unset( $raw_properties['user_id'] );
     1488        }
     1489
     1490        $day_html = $this->get_edit_field_html_elements( array_merge(
     1491            array(
     1492                'id'   => bp_get_the_profile_field_input_name() . '_day',
     1493                'name' => bp_get_the_profile_field_input_name() . '_day',
     1494            ),
     1495            $raw_properties
     1496        ) );
     1497
     1498        $month_html = $this->get_edit_field_html_elements( array_merge(
     1499            array(
     1500                'id'   => bp_get_the_profile_field_input_name() . '_month',
     1501                'name' => bp_get_the_profile_field_input_name() . '_month',
     1502            ),
     1503            $raw_properties
     1504        ) );
     1505
     1506        $year_html = $this->get_edit_field_html_elements( array_merge(
     1507            array(
     1508                'id'   => bp_get_the_profile_field_input_name() . '_year',
     1509                'name' => bp_get_the_profile_field_input_name() . '_year',
     1510            ),
     1511            $raw_properties
     1512        ) );
     1513    ?>
     1514        <div class="datebox">
     1515
     1516            <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>
     1517
     1518            <select <?php echo $day_html; ?>>
     1519                <?php bp_the_profile_field_options( array( 'type' => 'day', 'user_id' => $user_id ) ); ?>
     1520            </select>
     1521
     1522            <select <?php echo $month_html; ?>>
     1523                <?php bp_the_profile_field_options( array( 'type' => 'month', 'user_id' => $user_id ) ); ?>
     1524            </select>
     1525
     1526            <select <?php echo $year_html; ?>>
     1527                <?php bp_the_profile_field_options( array( 'type' => 'year', 'user_id' => $user_id ) ); ?>
     1528            </select>
     1529
     1530        </div>
     1531    <?php
     1532    }
     1533
     1534    /**
     1535     * Output the edit field options HTML for this field type.
     1536     *
     1537     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     1538     * These are stored separately in the database, and their templating is handled seperately.
     1539     *
     1540     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     1541     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     1542     *
     1543     * Must be used inside the {@link bp_profile_fields()} template loop.
     1544     *
     1545     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     1546     * @since BuddyPress (2.0.0)
     1547     */
     1548    public function edit_field_options_html( array $args = array() ) {
     1549        $options = $this->field_obj->get_children();
     1550        $date    = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
     1551
     1552        $day   = 0;
     1553        $month = 0;
     1554        $year  = 0;
     1555        $html  = '';
     1556
     1557        // Set day, month, year defaults
     1558        if ( ! empty( $date ) ) {
     1559
     1560            // If Unix timestamp
     1561            if ( is_numeric( $date ) ) {
     1562                $day   = date( 'j', $date );
     1563                $month = date( 'F', $date );
     1564                $year  = date( 'Y', $date );
     1565
     1566            // If MySQL timestamp
     1567            } else {
     1568                $day   = mysql2date( 'j', $date );
     1569                $month = mysql2date( 'F', $date, false ); // Not localized, so that selected() works below
     1570                $year  = mysql2date( 'Y', $date );
     1571            }
     1572        }
     1573
     1574        // Check for updated posted values, and errors preventing them from being saved first time.
     1575        if ( ! empty( $_POST['field_' . $this->field_obj->id . '_day'] ) ) {
     1576            $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day'];
     1577            $day     = ( $day != $new_day ) ? $new_day : $day;
     1578        }
     1579
     1580        if ( ! empty( $_POST['field_' . $this->field_obj->id . '_month'] ) ) {
     1581            $new_month = (int) $_POST['field_' . $this->field_obj->id . '_month'];
     1582            $month     = ( $month != $new_month ) ? $new_month : $month;
     1583        }
     1584
     1585        if ( ! empty( $_POST['field_' . $this->field_obj->id . '_year'] ) ) {
     1586            $new_year = date( 'j', (int) $_POST['field_' . $this->field_obj->id . '_year'] );
     1587            $year     = ( $year != $new_year ) ? $new_year : $year;
     1588        }
     1589
     1590        // $type will be passed by calling function when needed
     1591        switch ( $args['type'] ) {
     1592            case 'day':
     1593                $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $day, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
     1594
     1595                for ( $i = 1; $i < 32; ++$i ) {
     1596                    $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $day, $i, false ), (int) $i );
     1597                }
     1598            break;
     1599
     1600            case 'month':
     1601                $eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
     1602
     1603                $months = array(
     1604                    __( 'January', 'buddypress' ),
     1605                    __( 'February', 'buddypress' ),
     1606                    __( 'March', 'buddypress' ),
     1607                    __( 'April', 'buddypress' ),
     1608                    __( 'May', 'buddypress' ),
     1609                    __( 'June', 'buddypress' ),
     1610                    __( 'July', 'buddypress' ),
     1611                    __( 'August', 'buddypress' ),
     1612                    __( 'September', 'buddypress' ),
     1613                    __( 'October', 'buddypress' ),
     1614                    __( 'November', 'buddypress' ),
     1615                    __( 'December', 'buddypress' )
     1616                );
     1617
     1618                $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $month, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
     1619
     1620                for ( $i = 0; $i < 12; ++$i ) {
     1621                    $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $eng_months[$i] ), selected( $month, $eng_months[$i], false ), $months[$i] );
     1622                }
     1623            break;
     1624
     1625            case 'year':
     1626                $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $year, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
     1627
     1628                for ( $i = 2037; $i > 1901; $i-- ) {
     1629                    $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i );
     1630                }
     1631            break;
     1632        }
     1633
     1634        echo apply_filters( 'bp_get_the_profile_field_datebox', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date );
     1635    }
     1636
     1637    /**
     1638     * Output HTML for this field type on the wp-admin Profile Fields screen.
     1639     *
     1640     * Must be used inside the {@link bp_profile_fields()} template loop.
     1641     *
     1642     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     1643     * @since BuddyPress (2.0.0)
     1644     */
     1645    public function admin_field_html( array $raw_properties = array() ) {
     1646        $day_html = $this->get_edit_field_html_elements( array_merge(
     1647            array(
     1648                'id'   => bp_get_the_profile_field_input_name() . '_day',
     1649                'name' => bp_get_the_profile_field_input_name() . '_day',
     1650            ),
     1651            $raw_properties
     1652        ) );
     1653
     1654        $month_html = $this->get_edit_field_html_elements( array_merge(
     1655            array(
     1656                'id'   => bp_get_the_profile_field_input_name() . '_month',
     1657                'name' => bp_get_the_profile_field_input_name() . '_month',
     1658            ),
     1659            $raw_properties
     1660        ) );
     1661
     1662        $year_html = $this->get_edit_field_html_elements( array_merge(
     1663            array(
     1664                'id'   => bp_get_the_profile_field_input_name() . '_year',
     1665                'name' => bp_get_the_profile_field_input_name() . '_year',
     1666            ),
     1667            $raw_properties
     1668        ) );
     1669    ?>
     1670        <select <?php echo $day_html; ?>>
     1671            <?php bp_the_profile_field_options( 'type=day' ); ?>
     1672        </select>
     1673
     1674        <select <?php echo $month_html; ?>>
     1675            <?php bp_the_profile_field_options( 'type=month' ); ?>
     1676        </select>
     1677
     1678        <select <?php echo $year_html; ?>>
     1679            <?php bp_the_profile_field_options( 'type=year' ); ?>
     1680        </select>
     1681    <?php
     1682    }
     1683
     1684    /**
     1685     * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
     1686     * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
     1687     *
     1688     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     1689     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     1690     * @since BuddyPress (2.0.0)
     1691     */
     1692    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     1693}
     1694
     1695/**
     1696 * Checkbox xprofile field type.
     1697 *
     1698 * @since BuddyPress (2.0.0)
     1699 */
     1700class BP_XProfile_Field_Type_Checkbox extends BP_XProfile_Field_Type {
     1701
     1702    /**
     1703     * Constructor for the checkbox field type
     1704     *
     1705     * @since BuddyPress (2.0.0)
     1706     */
     1707    public function __construct() {
     1708        parent::__construct();
     1709
     1710        $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' );
     1711        $this->name     = _x( 'Checkboxes', 'xprofile field type', 'buddypress' );
     1712
     1713        $this->supports_multiple_defaults = true;
     1714        $this->accepts_null_value         = true;
     1715        $this->supports_options           = true;
     1716
     1717        $this->set_format( '/^.+$/', 'replace' );
     1718        do_action( 'bp_xprofile_field_type_checkbox', $this );
     1719    }
     1720
     1721    /**
     1722     * Output the edit field HTML for this field type.
     1723     *
     1724     * Must be used inside the {@link bp_profile_fields()} template loop.
     1725     *
     1726     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/input.checkbox.html permitted attributes} that you want to add.
     1727     * @since BuddyPress (2.0.0)
     1728     */
     1729    public function edit_field_html( array $raw_properties = array() ) {
     1730        $user_id = bp_displayed_user_id();
     1731
     1732        // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}.
     1733        if ( isset( $raw_properties['user_id'] ) ) {
     1734            $user_id = (int) $raw_properties['user_id'];
     1735            unset( $raw_properties['user_id'] );
     1736        }
     1737    ?>
     1738        <div class="checkbox">
     1739
     1740            <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     1741            <?php bp_the_profile_field_options( "user_id={$user_id}" ); ?>
     1742
     1743        </div>
     1744        <?php
     1745    }
     1746
     1747    /**
     1748     * Output the edit field options HTML for this field type.
     1749     *
     1750     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     1751     * These are stored separately in the database, and their templating is handled seperately.
     1752     *
     1753     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     1754     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     1755     *
     1756     * Must be used inside the {@link bp_profile_fields()} template loop.
     1757     *
     1758     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     1759     * @since BuddyPress (2.0.0)
     1760     */
     1761    public function edit_field_options_html( array $args = array() ) {
     1762        $options       = $this->field_obj->get_children();
     1763        $option_values = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
     1764        $option_values = (array) maybe_unserialize( $option_values );
     1765
     1766        $html = '';
     1767
     1768        // Check for updated posted values, but errors preventing them from being saved first time
     1769        if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_values != maybe_serialize( $_POST['field_' . $this->field_obj->id] ) ) {
     1770            if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
     1771                $option_values = array_map( 'sanitize_text_field', $_POST['field_' . $this->field_obj->id] );
     1772            }
     1773        }
     1774
     1775        for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
     1776            $selected = '';
     1777
     1778            // First, check to see whether the user's saved values match the option
     1779            for ( $j = 0, $count_values = count( $option_values ); $j < $count_values; ++$j ) {
     1780
     1781                // Run the allowed option name through the before_save filter, so we'll be sure to get a match
     1782                $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
     1783
     1784                if ( $option_values[$j] === $allowed_options || in_array( $allowed_options, $option_values ) ) {
     1785                    $selected = ' checked="checked"';
     1786                    break;
     1787                }
     1788            }
     1789
     1790            // If the user has not yet supplied a value for this field, check to see whether there is a default value available
     1791            if ( ! is_array( $option_values ) && empty( $option_values ) && empty( $selected ) && ! empty( $options[$k]->is_default_option ) ) {
     1792                $selected = ' checked="checked"';
     1793            }
     1794
     1795            $new_html = sprintf( '<label><input %1$s type="checkbox" name="%2$s" id="%3$s" value="%4$s">%5$s</label>',
     1796                $selected,
     1797                esc_attr( "field_{$this->field_obj->id}[]" ),
     1798                esc_attr( "field_{$options[$k]->id}_{$k}" ),
     1799                esc_attr( stripslashes( $options[$k]->name ) ),
     1800                esc_html( stripslashes( $options[$k]->name ) )
     1801            );
     1802            $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k );
     1803        }
     1804
     1805        echo $html;
     1806    }
     1807
     1808    /**
     1809     * Output HTML for this field type on the wp-admin Profile Fields screen.
     1810     *
     1811     * Must be used inside the {@link bp_profile_fields()} template loop.
     1812     *
     1813     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     1814     * @since BuddyPress (2.0.0)
     1815     */
     1816    public function admin_field_html( array $raw_properties = array() ) {
     1817        bp_the_profile_field_options();
     1818    }
     1819
     1820    /**
     1821     * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
     1822     *
     1823     * Must be used inside the {@link bp_profile_fields()} template loop.
     1824     *
     1825     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     1826     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     1827     * @since BuddyPress (2.0.0)
     1828     */
     1829    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
     1830        parent::admin_new_field_html( $current_field, 'checkbox' );
     1831    }
     1832}
     1833
     1834/**
     1835 * Radio button xprofile field type.
     1836 *
     1837 * @since BuddyPress (2.0.0)
     1838 */
     1839class BP_XProfile_Field_Type_Radiobutton extends BP_XProfile_Field_Type {
     1840
     1841    /**
     1842     * Constructor for the radio button field type
     1843     *
     1844     * @since BuddyPress (2.0.0)
     1845     */
     1846    public function __construct() {
     1847        parent::__construct();
     1848
     1849        $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' );
     1850        $this->name     = _x( 'Radio Buttons', 'xprofile field type', 'buddypress' );
     1851
     1852        $this->supports_options = true;
     1853
     1854        $this->set_format( '/^.+$/', 'replace' );
     1855        do_action( 'bp_xprofile_field_type_radiobutton', $this );
     1856    }
     1857
     1858    /**
     1859     * Output the edit field HTML for this field type.
     1860     *
     1861     * Must be used inside the {@link bp_profile_fields()} template loop.
     1862     *
     1863     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/input.radio.html permitted attributes} that you want to add.
     1864     * @since BuddyPress (2.0.0)
     1865     */
     1866    public function edit_field_html( array $raw_properties = array() ) {
     1867        $user_id = bp_displayed_user_id();
     1868
     1869        // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}.
     1870        if ( isset( $raw_properties['user_id'] ) ) {
     1871            $user_id = (int) $raw_properties['user_id'];
     1872            unset( $raw_properties['user_id'] );
     1873        }
     1874    ?>
     1875        <div class="radio">
     1876
     1877            <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     1878            <?php bp_the_profile_field_options( "user_id={$user_id}" );
     1879
     1880            if ( ! bp_get_the_profile_field_is_required() ) : ?>
     1881                <a class="clear-value" href="javascript:clear( '<?php echo esc_js( bp_get_the_profile_field_input_name() ); ?>' );"><?php esc_html_e( 'Clear', 'buddypress' ); ?></a>
     1882            <?php endif; ?>
     1883
     1884        </div>
     1885        <?php
     1886    }
     1887
     1888    /**
     1889     * Output the edit field options HTML for this field type.
     1890     *
     1891     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     1892     * These are stored separately in the database, and their templating is handled seperately.
     1893     *
     1894     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     1895     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     1896     *
     1897     * Must be used inside the {@link bp_profile_fields()} template loop.
     1898     *
     1899     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     1900     * @since BuddyPress (2.0.0)
     1901     */
     1902    public function edit_field_options_html( array $args = array() ) {
     1903        $option_value = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
     1904        $options      = $this->field_obj->get_children();
     1905
     1906        $html = sprintf( '<div id="%s">', esc_attr( 'field_' . $this->field_obj->id ) );
     1907
     1908        for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
     1909
     1910            // Check for updated posted values, but errors preventing them from being saved first time
     1911            if ( isset( $_POST['field_' . $this->field_obj->id] ) && $option_value != $_POST['field_' . $this->field_obj->id] ) {
     1912                if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
     1913                    $option_value = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );
     1914                }
     1915            }
     1916
     1917            // Run the allowed option name through the before_save filter, so we'll be sure to get a match
     1918            $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
     1919            $selected        = '';
     1920
     1921            if ( $option_value === $allowed_options || ( empty( $option_value ) && ! empty( $options[$k]->is_default_option ) ) ) {
     1922                $selected = ' checked="checked"';
     1923            }
     1924
     1925            $new_html = sprintf( '<label><input %1$s type="radio" name="%2$s" id="%3$s" value="%4$s">%5$s</label>',
     1926                $selected,
     1927                esc_attr( "field_{$this->field_obj->id}" ),
     1928                esc_attr( "option_{$options[$k]->id}" ),
     1929                esc_attr( stripslashes( $options[$k]->name ) ),
     1930                esc_html( stripslashes( $options[$k]->name ) )
     1931            );
     1932            $html .= apply_filters( 'bp_get_the_profile_field_options_radio', $new_html, $options[$k], $this->field_obj->id, $selected, $k );
     1933        }
     1934
     1935        echo $html . '</div>';
     1936    }
     1937
     1938    /**
     1939     * Output HTML for this field type on the wp-admin Profile Fields screen.
     1940     *
     1941     * Must be used inside the {@link bp_profile_fields()} template loop.
     1942     *
     1943     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     1944     * @since BuddyPress (2.0.0)
     1945     */
     1946    public function admin_field_html( array $raw_properties = array() ) {
     1947        bp_the_profile_field_options();
     1948
     1949        if ( ! bp_get_the_profile_field_is_required() ) : ?>
     1950            <a class="clear-value" href="javascript:clear( '<?php echo esc_js( bp_get_the_profile_field_input_name() ); ?>' );"><?php esc_html_e( 'Clear', 'buddypress' ); ?></a>
     1951        <?php endif; ?>
     1952    <?php
     1953    }
     1954
     1955    /**
     1956     * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
     1957     *
     1958     * Must be used inside the {@link bp_profile_fields()} template loop.
     1959     *
     1960     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     1961     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     1962     * @since BuddyPress (2.0.0)
     1963     */
     1964    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
     1965        parent::admin_new_field_html( $current_field, 'radio' );
     1966    }
     1967}
     1968
     1969/**
     1970 * Multi-selectbox xprofile field type.
     1971 *
     1972 * @since BuddyPress (2.0.0)
     1973 */
     1974class BP_XProfile_Field_Type_Multiselectbox extends BP_XProfile_Field_Type {
     1975
     1976    /**
     1977     * Constructor for the multi-selectbox field type
     1978     *
     1979     * @since BuddyPress (2.0.0)
     1980     */
     1981    public function __construct() {
     1982        parent::__construct();
     1983
     1984        $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' );
     1985        $this->name     = _x( 'Multi Select Box', 'xprofile field type', 'buddypress' );
     1986
     1987        $this->supports_multiple_defaults = true;
     1988        $this->accepts_null_value         = true;
     1989        $this->supports_options           = true;
     1990
     1991        $this->set_format( '/^.+$/', 'replace' );
     1992        do_action( 'bp_xprofile_field_type_multiselectbox', $this );
     1993    }
     1994
     1995    /**
     1996     * Output the edit field HTML for this field type.
     1997     *
     1998     * Must be used inside the {@link bp_profile_fields()} template loop.
     1999     *
     2000     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/select.html permitted attributes} that you want to add.
     2001     * @since BuddyPress (2.0.0)
     2002     */
     2003    public function edit_field_html( array $raw_properties = array() ) {
     2004        $user_id = bp_displayed_user_id();
     2005
     2006        // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}.
     2007        if ( isset( $raw_properties['user_id'] ) ) {
     2008            $user_id = (int) $raw_properties['user_id'];
     2009            unset( $raw_properties['user_id'] );
     2010        }
     2011
     2012        $html = $this->get_edit_field_html_elements( array_merge(
     2013            array(
     2014                'multiple' => 'multiple',
     2015                'id'       => bp_get_the_profile_field_input_name() . '[]',
     2016                'name'     => bp_get_the_profile_field_input_name() . '[]',
     2017            ),
     2018            $raw_properties
     2019        ) );
     2020    ?>
     2021        <label for="<?php bp_the_profile_field_input_name(); ?>[]"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     2022        <select <?php echo $html; ?>>
     2023            <?php bp_the_profile_field_options( "user_id={$user_id}" ); ?>
     2024        </select>
     2025
     2026        <?php if ( ! bp_get_the_profile_field_is_required() ) : ?>
     2027            <a class="clear-value" href="javascript:clear( '<?php echo esc_js( bp_get_the_profile_field_input_name() ); ?>[]' );"><?php esc_html_e( 'Clear', 'buddypress' ); ?></a>
     2028        <?php endif; ?>
     2029    <?php
     2030    }
     2031
     2032    /**
     2033     * Output the edit field options HTML for this field type.
     2034     *
     2035     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     2036     * These are stored separately in the database, and their templating is handled seperately.
     2037     *
     2038     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     2039     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     2040     *
     2041     * Must be used inside the {@link bp_profile_fields()} template loop.
     2042     *
     2043     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     2044     * @since BuddyPress (2.0.0)
     2045     */
     2046    public function edit_field_options_html( array $args = array() ) {
     2047        $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );
     2048
     2049        $options = $this->field_obj->get_children();
     2050        $html    = '';
     2051
     2052        if ( empty( $original_option_values ) && ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
     2053            $original_option_values = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );
     2054        }
     2055
     2056        $option_values = (array) $original_option_values;
     2057        for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
     2058            $selected = '';
     2059
     2060            // Check for updated posted values, but errors preventing them from being saved first time
     2061            foreach( $option_values as $i => $option_value ) {
     2062                if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id][$i] != $option_value ) {
     2063                    if ( ! empty( $_POST['field_' . $this->field_obj->id][$i] ) ) {
     2064                        $option_values[] = sanitize_text_field( $_POST['field_' . $this->field_obj->id][$i] );
     2065                    }
     2066                }
     2067            }
     2068
     2069            // Run the allowed option name through the before_save filter, so we'll be sure to get a match
     2070            $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
     2071
     2072            // First, check to see whether the user-entered value matches
     2073            if ( in_array( $allowed_options, $option_values ) ) {
     2074                $selected = ' selected="selected"';
     2075            }
     2076
     2077            // Then, if the user has not provided a value, check for defaults
     2078            if ( ! is_array( $original_option_values ) && empty( $option_values ) && ! empty( $options[$k]->is_default_option ) ) {
     2079                $selected = ' selected="selected"';
     2080            }
     2081
     2082            $html .= apply_filters( 'bp_get_the_profile_field_options_multiselect', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_html( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k], $this->field_obj->id, $selected, $k );
     2083        }
     2084
     2085        echo $html;
     2086    }
     2087
     2088    /**
     2089     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2090     *
     2091     * Must be used inside the {@link bp_profile_fields()} template loop.
     2092     *
     2093     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2094     * @since BuddyPress (2.0.0)
     2095     */
     2096    public function admin_field_html( array $raw_properties = array() ) {
     2097        $html = $this->get_edit_field_html_elements( array_merge(
     2098            array( 'multiple' => 'multiple' ),
     2099            $raw_properties
     2100        ) );
     2101    ?>
     2102        <select <?php echo $html; ?>>
     2103            <?php bp_the_profile_field_options(); ?>
     2104        </select>
     2105    <?php
     2106    }
     2107
     2108    /**
     2109     * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
     2110     *
     2111     * Must be used inside the {@link bp_profile_fields()} template loop.
     2112     *
     2113     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2114     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2115     * @since BuddyPress (2.0.0)
     2116     */
     2117    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
     2118        parent::admin_new_field_html( $current_field, 'checkbox' );
     2119    }
     2120}
     2121
     2122/**
     2123 * Selectbox xprofile field type.
     2124 *
     2125 * @since BuddyPress (2.0.0)
     2126 */
     2127class BP_XProfile_Field_Type_Selectbox extends BP_XProfile_Field_Type {
     2128
     2129    /**
     2130     * Constructor for the selectbox field type
     2131     *
     2132     * @since BuddyPress (2.0.0)
     2133     */
     2134    public function __construct() {
     2135        parent::__construct();
     2136
     2137        $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' );
     2138        $this->name     = _x( 'Drop Down Select Box', 'xprofile field type', 'buddypress' );
     2139
     2140        $this->supports_options = true;
     2141
     2142        $this->set_format( '/^.+$/', 'replace' );
     2143        do_action( 'bp_xprofile_field_type_selectbox', $this );
     2144    }
     2145
     2146    /**
     2147     * Output the edit field HTML for this field type.
     2148     *
     2149     * Must be used inside the {@link bp_profile_fields()} template loop.
     2150     *
     2151     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/select.html permitted attributes} that you want to add.
     2152     * @since BuddyPress (2.0.0)
     2153     */
     2154    public function edit_field_html( array $raw_properties = array() ) {
     2155        $user_id = bp_displayed_user_id();
     2156
     2157        // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}.
     2158        if ( isset( $raw_properties['user_id'] ) ) {
     2159            $user_id = (int) $raw_properties['user_id'];
     2160            unset( $raw_properties['user_id'] );
     2161        }
     2162
     2163        $html = $this->get_edit_field_html_elements( $raw_properties );
     2164    ?>
     2165        <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     2166        <select <?php echo $html; ?>>
     2167            <?php bp_the_profile_field_options( "user_id={$user_id}" ); ?>
     2168        </select>
     2169    <?php
     2170    }
     2171
     2172    /**
     2173     * Output the edit field options HTML for this field type.
     2174     *
     2175     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     2176     * These are stored separately in the database, and their templating is handled seperately.
     2177     *
     2178     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     2179     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     2180     *
     2181     * Must be used inside the {@link bp_profile_fields()} template loop.
     2182     *
     2183     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     2184     * @since BuddyPress (2.0.0)
     2185     */
     2186    public function edit_field_options_html( array $args = array() ) {
     2187        $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );
     2188
     2189        $options = $this->field_obj->get_children();
     2190        $html     = '<option value="">' . /* translators: no option picked in select box */ esc_html__( '----', 'buddypress' ) . '</option>';
     2191
     2192        if ( empty( $original_option_values ) && !empty( $_POST['field_' . $this->field_obj->id] ) ) {
     2193            $original_option_values = sanitize_text_field(  $_POST['field_' . $this->field_obj->id] );
     2194        }
     2195
     2196        $option_values = (array) $original_option_values;
     2197        for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
     2198            $selected = '';
     2199
     2200            // Check for updated posted values, but errors preventing them from being saved first time
     2201            foreach( $option_values as $i => $option_value ) {
     2202                if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id] != $option_value ) {
     2203                    if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
     2204                        $option_values[$i] = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );
     2205                    }
     2206                }
     2207            }
     2208
     2209            // Run the allowed option name through the before_save filter, so we'll be sure to get a match
     2210            $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );
     2211
     2212            // First, check to see whether the user-entered value matches
     2213            if ( in_array( $allowed_options, $option_values ) ) {
     2214                $selected = ' selected="selected"';
     2215            }
     2216
     2217            // Then, if the user has not provided a value, check for defaults
     2218            if ( ! is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option ) {
     2219                $selected = ' selected="selected"';
     2220            }
     2221
     2222            $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_html( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k], $this->field_obj->id, $selected, $k );
     2223        }
     2224
     2225        echo $html;
     2226    }
     2227
     2228    /**
     2229     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2230     *
     2231     * Must be used inside the {@link bp_profile_fields()} template loop.
     2232     *
     2233     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2234     * @since BuddyPress (2.0.0)
     2235     */
     2236    public function admin_field_html( array $raw_properties = array() ) {
     2237        $html = $this->get_edit_field_html_elements( $raw_properties );
     2238    ?>
     2239        <select <?php echo $html; ?>>
     2240            <?php bp_the_profile_field_options(); ?>
     2241        </select>
     2242    <?php
     2243    }
     2244
     2245    /**
     2246     * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
     2247     *
     2248     * Must be used inside the {@link bp_profile_fields()} template loop.
     2249     *
     2250     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2251     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2252     * @since BuddyPress (2.0.0)
     2253     */
     2254    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
     2255        parent::admin_new_field_html( $current_field, 'radio' );
     2256    }
     2257}
     2258
     2259/**
     2260 * Textarea xprofile field type.
     2261 *
     2262 * @since BuddyPress (2.0.0)
     2263 */
     2264class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {
     2265
     2266    /**
     2267     * Constructor for the textarea field type
     2268     *
     2269     * @since BuddyPress (2.0.0)
     2270     */
     2271    public function __construct() {
     2272        parent::__construct();
     2273
     2274        $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
     2275        $this->name     = _x( 'Multi-line Text Area', 'xprofile field type', 'buddypress' );
     2276
     2277        $this->set_format( '/^.*$/m', 'replace' );
     2278        do_action( 'bp_xprofile_field_type_textarea', $this );
     2279    }
     2280
     2281    /**
     2282     * Output the edit field HTML for this field type.
     2283     *
     2284     * Must be used inside the {@link bp_profile_fields()} template loop.
     2285     *
     2286     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/textarea.html permitted attributes} that you want to add.
     2287     * @since BuddyPress (2.0.0)
     2288     */
     2289    public function edit_field_html( array $raw_properties = array() ) {
     2290
     2291        // user_id is a special optional parameter that certain other fields types pass to {@link bp_the_profile_field_options()}.
     2292        if ( isset( $raw_properties['user_id'] ) ) {
     2293            unset( $raw_properties['user_id'] );
     2294        }
     2295
     2296        $html = $this->get_edit_field_html_elements( array_merge(
     2297            array(
     2298                'cols' => 40,
     2299                'rows' => 5,
     2300            ),
     2301            $raw_properties
     2302        ) );
     2303    ?>
     2304        <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     2305        <textarea <?php echo $html; ?>><?php bp_the_profile_field_edit_value(); ?></textarea>
     2306    <?php
     2307    }
     2308
     2309    /**
     2310     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2311     *
     2312     * Must be used inside the {@link bp_profile_fields()} template loop.
     2313     *
     2314     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2315     * @since BuddyPress (2.0.0)
     2316     */
     2317    public function admin_field_html( array $raw_properties = array() ) {
     2318        $html = $this->get_edit_field_html_elements( array_merge(
     2319            array(
     2320                'cols' => 40,
     2321                'rows' => 5,
     2322            ),
     2323            $raw_properties
     2324        ) );
     2325    ?>
     2326        <textarea <?php echo $html; ?>></textarea>
     2327    <?php
     2328    }
     2329
     2330    /**
     2331     * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
     2332     * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
     2333     *
     2334     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2335     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2336     * @since BuddyPress (2.0.0)
     2337     */
     2338    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     2339}
     2340
     2341/**
     2342 * Textbox xprofile field type.
     2343 *
     2344 * @since BuddyPress (2.0.0)
     2345 */
     2346class BP_XProfile_Field_Type_Textbox extends BP_XProfile_Field_Type {
     2347
     2348    /**
     2349     * Constructor for the textbox field type
     2350     *
     2351     * @since BuddyPress (2.0.0)
     2352     */
     2353    public function __construct() {
     2354        parent::__construct();
     2355
     2356        $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
     2357        $this->name     = _x( 'Text Box', 'xprofile field type', 'buddypress' );
     2358
     2359        $this->set_format( '/^.*$/', 'replace' );
     2360        do_action( 'bp_xprofile_field_type_textbox', $this );
     2361    }
     2362
     2363    /**
     2364     * Output the edit field HTML for this field type.
     2365     *
     2366     * Must be used inside the {@link bp_profile_fields()} template loop.
     2367     *
     2368     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/input.text.html permitted attributes} that you want to add.
     2369     * @since BuddyPress (2.0.0)
     2370     */
     2371    public function edit_field_html( array $raw_properties = array() ) {
     2372
     2373        // user_id is a special optional parameter that certain other fields types pass to {@link bp_the_profile_field_options()}.
     2374        if ( isset( $raw_properties['user_id'] ) ) {
     2375            unset( $raw_properties['user_id'] );
     2376        }
     2377
     2378        $html = $this->get_edit_field_html_elements( array_merge(
     2379            array(
     2380                'type'  => 'text',
     2381                'value' => bp_get_the_profile_field_edit_value(),
     2382            ),
     2383            $raw_properties
     2384        ) );
     2385    ?>
     2386        <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     2387        <input <?php echo $html; ?>>
     2388    <?php
     2389    }
     2390
     2391    /**
     2392     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2393     *
     2394     * Must be used inside the {@link bp_profile_fields()} template loop.
     2395     *
     2396     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2397     * @since BuddyPress (2.0.0)
     2398     */
     2399    public function admin_field_html( array $raw_properties = array() ) {
     2400        $html = $this->get_edit_field_html_elements( array_merge(
     2401            array( 'type' => 'text' ),
     2402            $raw_properties
     2403        ) );
     2404    ?>
     2405        <input <?php echo $html; ?>>
     2406    <?php
     2407    }
     2408
     2409    /**
     2410     * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
     2411     * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
     2412     *
     2413     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2414     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2415     * @since BuddyPress (2.0.0)
     2416     */
     2417    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     2418}
     2419
     2420/**
     2421 * Number xprofile field type.
     2422 *
     2423 * @since BuddyPress (2.0.0)
     2424 */
     2425class BP_XProfile_Field_Type_Number extends BP_XProfile_Field_Type {
     2426
     2427    /**
     2428     * Constructor for the number field type
     2429     *
     2430     * @since BuddyPress (2.0.0)
     2431     */
     2432    public function __construct() {
     2433        parent::__construct();
     2434
     2435        $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
     2436        $this->name     = _x( 'Number', 'xprofile field type', 'buddypress' );
     2437
     2438        $this->set_format( '/^\d+|-\d+$/', 'replace' );
     2439        do_action( 'bp_xprofile_field_type_number', $this );
     2440    }
     2441
     2442    /**
     2443     * Output the edit field HTML for this field type.
     2444     *
     2445     * Must be used inside the {@link bp_profile_fields()} template loop.
     2446     *
     2447     * @param array $raw_properties Optional key/value array of {@link http://dev.w3.org/html5/markup/input.number.html permitted attributes} that you want to add.
     2448     * @since BuddyPress (2.0.0)
     2449     */
     2450    public function edit_field_html( array $raw_properties = array() ) {
     2451
     2452        // user_id is a special optional parameter that certain other fields types pass to {@link bp_the_profile_field_options()}.
     2453        if ( isset( $raw_properties['user_id'] ) ) {
     2454            unset( $raw_properties['user_id'] );
     2455        }
     2456
     2457        $html = $this->get_edit_field_html_elements( array_merge(
     2458            array(
     2459                'type'  => 'number',
     2460                'value' =>  bp_get_the_profile_field_edit_value(),
     2461            ),
     2462            $raw_properties
     2463        ) );
     2464    ?>
     2465        <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
     2466        <input <?php echo $html; ?>>
     2467    <?php
     2468    }
     2469
     2470    /**
     2471     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2472     *
     2473     * Must be used inside the {@link bp_profile_fields()} template loop.
     2474     *
     2475     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2476     * @since BuddyPress (2.0.0)
     2477     */
     2478    public function admin_field_html( array $raw_properties = array() ) {
     2479        $html = $this->get_edit_field_html_elements( array_merge(
     2480            array( 'type' => 'number' ),
     2481            $raw_properties
     2482        ) );
     2483    ?>
     2484        <input <?php echo $html; ?>>
     2485    <?php
     2486    }
     2487
     2488    /**
     2489     * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields
     2490     * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out.
     2491     *
     2492     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2493     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2494     * @since BuddyPress (2.0.0)
     2495     */
     2496    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     2497}
     2498
     2499/**
     2500 * Represents a type of XProfile field and holds meta information about the type of value that it accepts.
     2501 *
     2502 * @since BuddyPress (2.0.0)
     2503 */
     2504abstract class BP_XProfile_Field_Type {
     2505
     2506    /**
     2507     * @since BuddyPress (2.0.0)
     2508     * @var array Field type validation regexes
     2509     */
     2510    protected $validation_regex = array();
     2511
     2512    /**
     2513     * @since BuddyPress (2.0.0)
     2514     * @var array Field type whitelisted values
     2515     */
     2516    protected $validation_whitelist = array();
     2517
     2518    /**
     2519     * @since BuddyPress (2.0.0)
     2520     * @var string The name of this field type
     2521     */
     2522    public $name = '';
     2523
     2524    /**
     2525     * The name of the category that this field type should be grouped with. Used on the [Users > Profile Fields] screen in wp-admin.
     2526     *
     2527     * @since BuddyPress (2.0.0)
     2528     * @var string
     2529     */
     2530    public $category = '';
     2531
     2532    /**
     2533     * @since BuddyPress (2.0.0)
     2534     * @var bool If this is set, allow BP to store null/empty values for this field type.
     2535     */
     2536    public $accepts_null_value = false;
     2537
     2538    /**
     2539     * If this is set, BP will set this field type's validation whitelist from the field's options (e.g checkbox, selectbox).
     2540     *
     2541     * @since BuddyPress (2.0.0)
     2542     * @var bool Does this field support options? e.g. selectbox, radio buttons, etc.
     2543     */
     2544    public $supports_options = false;
     2545
     2546    /**
     2547     * @since BuddyPress (2.0.0)
     2548     * @var bool Does this field type support multiple options being set as default values? e.g. multiselectbox, checkbox.
     2549     */
     2550    public $supports_multiple_defaults = false;
     2551
     2552    /**
     2553     * @since BuddyPress (2.0.0)
     2554     * @var BP_XProfile_Field If this object is created by instantiating a {@link BP_XProfile_Field}, this is a reference back to that object.
     2555     */
     2556    public $field_obj = null;
     2557
     2558    /**
     2559     * Constructor
     2560     *
     2561     * @since BuddyPress (2.0.0)
     2562     */
     2563    public function __construct() {
     2564        do_action( 'bp_xprofile_field_type', $this );
     2565    }
     2566
     2567    /**
     2568     * Set a regex that profile data will be asserted against.
     2569     *
     2570     * You can call this method multiple times to set multiple formats. When validation is performed,
     2571     * it's successful as long as the new value matches any one of the registered formats.
     2572     *
     2573     * @param string $format Regex string
     2574     * @param string $replace_format Optional; if 'replace', replaces the format instead of adding to it. Defaults to 'add'.
     2575     * @return BP_XProfile_Field_Type
     2576     * @since BuddyPress (2.0.0)
     2577     */
     2578    public function set_format( $format, $replace_format = 'add' ) {
     2579
     2580        $format = apply_filters( 'bp_xprofile_field_type_set_format', $format, $replace_format, $this );
     2581
     2582        if ( 'add' === $replace_format ) {
     2583            $this->validation_regex[] = $format;
     2584        } elseif ( 'replace' === $replace_format ) {
     2585            $this->validation_regex = array( $format );
     2586        }
     2587
     2588        return $this;
     2589    }
     2590
     2591    /**
     2592     * Add a value to this type's whitelist that that profile data will be asserted against.
     2593     *
     2594     * You can call this method multiple times to set multiple formats. When validation is performed,
     2595     * it's successful as long as the new value matches any one of the registered formats.
     2596     *
     2597     * @param string|array $values
     2598     * @return BP_XProfile_Field_Type
     2599     * @since BuddyPress (2.0.0)
     2600     */
     2601    public function set_whitelist_values( $values ) {
     2602        foreach ( (array) $values as $value ) {
     2603            $this->validation_whitelist[] = apply_filters( 'bp_xprofile_field_type_set_whitelist_values', $value, $values, $this );
     2604        }
     2605
     2606        return $this;
     2607    }
     2608
     2609    /**
     2610     * Check the given string against the registered formats for this field type.
     2611     *
     2612     * This method doesn't support chaining.
     2613     *
     2614     * @param string|array $values Value to check against the registered formats
     2615     * @return bool True if the value validates
     2616     * @since BuddyPress (2.0.0)
     2617     */
     2618    public function is_valid( $values ) {
     2619        $validated = false;
     2620
     2621        // Some types of field (e.g. multi-selectbox) may have multiple values to check
     2622        foreach ( (array) $values as $value ) {
     2623
     2624            // Validate the $value against the type's accepted format(s).
     2625            foreach ( $this->validation_regex as $format ) {
     2626                if ( 1 === preg_match( $format, $value ) ) {
     2627                    $validated = true;
     2628                    continue;
     2629
     2630                } else {
     2631                    $validated = false;
     2632                }
     2633            }
     2634        }
     2635
     2636        // Handle field types with accepts_null_value set if $values is an empty array
     2637        if ( ! $validated && is_array( $values ) && empty( $values ) && $this->accepts_null_value ) {
     2638            $validated = true;
     2639        }
     2640
     2641        // If there's a whitelist set, also check the $value.
     2642        if ( $validated && ! empty( $values ) && ! empty( $this->validation_whitelist ) ) {
     2643
     2644            foreach ( (array) $values as $value ) {
     2645                $validated = in_array( $value, $this->validation_whitelist, true );
     2646            }
     2647        }
     2648
     2649        return (bool) apply_filters( 'bp_xprofile_field_type_is_valid', $validated, $values, $this );
     2650    }
     2651
     2652    /**
     2653     * Output the edit field HTML for this field type.
     2654     *
     2655     * Must be used inside the {@link bp_profile_fields()} template loop.
     2656     *
     2657     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2658     * @since BuddyPress (2.0.0)
     2659     */
     2660    abstract public function edit_field_html( array $raw_properties = array() );
     2661
     2662    /**
     2663     * Output the edit field options HTML for this field type.
     2664     *
     2665     * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
     2666     * These are stored separately in the database, and their templating is handled separately.
     2667     * Populate this method in a child class if it's required. Otherwise, you can leave it out.
     2668     *
     2669     * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
     2670     * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
     2671     *
     2672     * Must be used inside the {@link bp_profile_fields()} template loop.
     2673     *
     2674     * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
     2675     * @since BuddyPress (2.0.0)
     2676     */
     2677    public function edit_field_options_html( array $args = array() ) {}
     2678
     2679    /**
     2680     * Output HTML for this field type on the wp-admin Profile Fields screen.
     2681     *
     2682     * Must be used inside the {@link bp_profile_fields()} template loop.
     2683     *
     2684     * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
     2685     * @since BuddyPress (2.0.0)
     2686     */
     2687    abstract public function admin_field_html( array $raw_properties = array() );
     2688
     2689    /**
     2690     * Output HTML for this field type's children options on the wp-admin Profile Fields "Add Field" and "Edit Field" screens.
     2691     *
     2692     * You don't need to implement this method for all field types. It's used in core by the
     2693     * selectbox, multi selectbox, checkbox, and radio button fields, to allow the admin to
     2694     * enter the child option values (e.g. the choices in a select box).
     2695     *
     2696     * Must be used inside the {@link bp_profile_fields()} template loop.
     2697     *
     2698     * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
     2699     * @param string $control_type Optional. HTML input type used to render the current field's child options.
     2700     * @since BuddyPress (2.0.0)
     2701     */
     2702    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
     2703        $type = array_search( get_class( $this ), bp_xprofile_get_field_types() );
     2704        if ( false === $type ) {
     2705            return;
     2706        }
     2707
     2708        $class            = $current_field->type != $type ? 'display: none;' : '';
     2709        $current_type_obj = bp_xprofile_create_field_type( $type );
     2710        ?>
     2711
     2712        <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;">
     2713            <h3><?php esc_html_e( 'Please enter options for this Field:', 'buddypress' ); ?></h3>
     2714            <div class="inside">
     2715                <p>
     2716                    <label for="sort_order_<?php echo esc_attr( $type ); ?>"><?php esc_html_e( 'Sort Order:', 'buddypress' ); ?></label>
     2717                    <select name="sort_order_<?php echo esc_attr( $type ); ?>" id="sort_order_<?php echo esc_attr( $type ); ?>" >
     2718                        <option value="custom" <?php selected( 'custom', $current_field->order_by ); ?>><?php esc_html_e( 'Custom',     'buddypress' ); ?></option>
     2719                        <option value="asc"    <?php selected( 'asc',    $current_field->order_by ); ?>><?php esc_html_e( 'Ascending',  'buddypress' ); ?></option>
     2720                        <option value="desc"   <?php selected( 'desc',   $current_field->order_by ); ?>><?php esc_html_e( 'Descending', 'buddypress' ); ?></option>
     2721                    </select>
     2722                </p>
     2723
     2724                <?php
     2725                $options = $current_field->get_children( true );
     2726
     2727                // If no children options exists for this field, check in $_POST for a submitted form (e.g. on the "new field" screen).
     2728                if ( ! $options ) {
     2729
     2730                    $options = array();
     2731                    $i       = 1;
     2732
     2733                    while ( isset( $_POST[$type . '_option'][$i] ) ) {
     2734
     2735                        // Multiselectbox and checkboxes support MULTIPLE default options; all other core types support only ONE.
     2736                        if ( $current_type_obj->supports_options && ! $current_type_obj->supports_multiple_defaults && isset( $_POST["isDefault_{$type}_option"][$i] ) && (int) $_POST["isDefault_{$type}_option"] === $i ) {
     2737                            $is_default_option = true;
     2738                        } elseif ( isset( $_POST["isDefault_{$type}_option"][$i] ) ) {
     2739                            $is_default_option = (bool) $_POST["isDefault_{$type}_option"][$i];
     2740                        } else {
     2741                            $is_default_option = false;
     2742                        }
     2743
     2744                        // Grab the values from $_POST to use as the form's options
     2745                        $options[] = (object) array(
     2746                            'id'                => -1,
     2747                            'is_default_option' => $is_default_option,
     2748                            'name'              => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),
     2749                        );
     2750
     2751                        ++$i;
     2752                    }
     2753
     2754                    // If there are still no children options set, this must be the "new field" screen, so add one new/empty option.
     2755                    if ( ! $options ) {
     2756                        $options[] = (object) array(
     2757                            'id'                => -1,
     2758                            'is_default_option' => false,
     2759                            'name'              => '',
     2760                        );
     2761                    }
     2762                }
     2763
     2764                // Render the markup for the children options
     2765                if ( ! empty( $options ) ) {
     2766                    $default_name = '';
     2767
     2768                    for ( $i = 0, $count = count( $options ); $i < $count; ++$i ) :
     2769                        $j = $i + 1;
     2770
     2771                        // Multiselectbox and checkboxes support MULTIPLE default options; all other core types support only ONE.
     2772                        if ( $current_type_obj->supports_options && $current_type_obj->supports_multiple_defaults ) {
     2773                            $default_name = '[' . $j . ']';
     2774                        }
     2775                        ?>
     2776
     2777                        <p class="sortable">
     2778                            <span>&nbsp;&Xi;&nbsp;</span>
     2779                            <input type="text" name="<?php echo esc_attr( "{$type}_option[{$j}]" ); ?>" id="<?php echo esc_attr( "{$type}_option{$j}" ); ?>" value="<?php echo esc_attr( $options[$i]->name ); ?>" />
     2780                            <input type="<?php echo esc_attr( $control_type ); ?>" name="<?php echo esc_attr( "isDefault_{$type}_option{$default_name}" ); ?>" <?php checked( $options[$i]->is_default_option, true ); ?> value="<?php echo esc_attr( $j ); ?>" />
     2781                            <span><?php _e( 'Default Value', 'buddypress' ); ?></span>
     2782                        </p>
     2783                    <?php endfor; ?>
     2784
     2785                    <input type="hidden" name="<?php echo esc_attr( "{$type}_option_number" ); ?>" id="<?php echo esc_attr( "{$type}_option_number" ); ?>" value="<?php echo esc_attr( $j + 1 ); ?>" />
     2786                <?php } ?>
     2787
     2788                <div id="<?php echo esc_attr( "{$type}_more" ); ?>"></div>
     2789                <p><a href="javascript:add_option('<?php echo esc_js( $type ); ?>')"><?php esc_html_e( 'Add Another Option', 'buddypress' ); ?></a></p>
     2790            </div>
     2791        </div>
     2792
     2793        <?php
     2794    }
     2795
     2796
     2797    /**
     2798     * Internal protected/private helper methods past this point.
     2799     */
     2800
     2801    /**
     2802     * Get a sanitised and escaped string of the edit field's HTML elements and attributes.
     2803     *
     2804     * Must be used inside the {@link bp_profile_fields()} template loop.
     2805     * This method was intended to be static but couldn't be because php.net/lsb/ requires PHP >= 5.3.
     2806     *
     2807     * @param array $properties Optional key/value array of attributes for this edit field.
     2808     * @return string
     2809     * @since BuddyPress (2.0.0)
     2810     */
     2811    protected function get_edit_field_html_elements( array $properties = array() ) {
     2812
     2813        $properties = array_merge( array(
     2814            'id'   => bp_get_the_profile_field_input_name(),
     2815            'name' => bp_get_the_profile_field_input_name(),
     2816        ), $properties );
     2817
     2818        if ( bp_get_the_profile_field_is_required() ) {
     2819            $properties['aria-required'] = 'true';
     2820        }
     2821
     2822        $html       = '';
     2823        $properties = (array) apply_filters( 'bp_xprofile_field_edit_html_elements', $properties, get_class( $this ) );
     2824
     2825        foreach ( $properties as $name => $value ) {
     2826            $html .= sprintf( '%s="%s" ', sanitize_key( $name ), esc_attr( $value ) );
     2827        }
     2828
     2829        return $html;
     2830    }
     2831}
Note: See TracChangeset for help on using the changeset viewer.