Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/21/2015 02:00:38 PM (11 years ago)
Author:
boonebgorges
Message:

Allow xProfile fields to be restricted to users belonging to one or more member types.

A new metabox on the profile field edit panel allows the administrator to
select the member types to which the field is applicable. Admins can also
choose to have a field apply to users who do not belong to any member type.
Information about a field's member type associations is displayed on the
Users > Profile Fields page, alongside each field name.

During registration, the only fields that are displayed are those that are
unrestricted - that is, those available to all users, regardless of member type
(or lack thereof).

This changeset introduces a number of new methods on BP_XProfile_Field that
developers should use to manipulate member type information:
get_member_types(), set_member_types(), and the static
get_fields_for_member_type(). In addition to member types that have been
explicitly registered, 'null' is a pseudo-type representing users who do not
belong to a member type.

This changeset introduces a blacklist of illegal member type names. By default,
the blacklist includes 'any', 'null', and '_none'. Use the
'bp_member_type_illegal_names' filter to add names to the blacklist.

Props Offereins, boonebgorges, tanner m, imath.
See #5192.

File:
1 edited

Legend:

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

    r10009 r10022  
    233233         *      @type int   $user_id           Required if you want to load a specific user's data.
    234234         *                                     Default: displayed user's ID.
     235         *      @type array|string $member_type Limit fields by those restricted to a given member type, or array of
     236         *                                      member types. If `$user_id` is provided, the value of `$member_type`
     237         *                                      will be overridden by the member types of the provided user. The
     238         *                                      special value of 'any' will return only those fields that are
     239         *                                      unrestricted by member type - i.e., those applicable to any type.
    235240         *      @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    236241         *      @type bool  $hide_empty_fields True to hide fields where the user has not provided data. Default: false.
    … …  
    252257                        'profile_group_id'       => false,
    253258                        'user_id'                => bp_displayed_user_id(),
     259                        'member_type'            => false,
    254260                        'hide_empty_groups'      => false,
    255261                        'hide_empty_fields'      => false,
    … …  
    319325                $exclude_fields_cs  = implode( ',', $exclude_fields_cs );
    320326
    321                 // Setup IN query for field IDs
     327                // Set up NOT IN query for excluded field IDs.
    322328                if ( ! empty( $exclude_fields_cs ) ) {
    323329                        $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
    … …  
    326332                }
    327333
     334                // Set up IN query for included field IDs.
     335                $include_field_ids = array();
     336
     337                // Member-type restrictions.
     338                if ( bp_get_member_types() ) {
     339                        if ( $r['user_id'] || false !== $r['member_type'] ) {
     340                                $member_types = $r['member_type'];
     341                                if ( $r['user_id'] ) {
     342                                        $member_types = bp_get_member_type( $r['user_id'], false );
     343                                        if ( empty( $member_types ) ) {
     344                                                $member_types = array( 'null' );
     345                                        }
     346                                }
     347
     348                                $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
     349                                $include_field_ids += array_keys( $member_types_fields );
     350                        }
     351                }
     352
     353                $in_sql = '';
     354                if ( ! empty( $include_field_ids ) ) {
     355                        $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
     356                        $in_sql = " AND id IN ({$include_field_ids_cs}) ";
     357                }
     358
    328359                // Fetch the fields
    329                 $fields    = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" );
     360                $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
     361
    330362                $field_ids = wp_list_pluck( $fields, 'id' );
    331363
Note: See TracChangeset for help on using the changeset viewer.