Skip to:
Content

BuddyPress.org

Changeset 7836


Ignore:
Timestamp:
02/10/2014 04:29:06 PM (7 years ago)
Author:
boonebgorges
Message:

Introduce xprofile_get_field_visibility_level()

This is a simple function for grabbing the visibility level attached to a given
field by a specific user, sensitive to admin-provided defaults and override
settings.

See #4636

Location:
trunk
Files:
2 edited

Legend:

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

    r7812 r7836  
    288288}
    289289
     290/**
     291 * Get the visibility level for a field.
     292 *
     293 * @since BuddyPress (2.0.0)
     294 *
     295 * @param int $field_id The ID of the xprofile field.
     296 * @param int $user_id The ID of the user to whom the data belongs.
     297 * @return string
     298 */
     299function xprofile_get_field_visibility_level( $field_id = 0, $user_id = 0 ) {
     300    $current_level = '';
     301
     302    if ( empty( $field_id ) || empty( $user_id ) ) {
     303        return $current_level;
     304    }
     305
     306    $current_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );
     307    $current_level  = isset( $current_levels[ $field_id ] ) ? $current_levels[ $field_id ] : '';
     308
     309    // Use the user's stored level, unless custom visibility is disabled
     310    $field = new BP_XProfile_Field( $field_id );
     311    if ( isset( $field->allow_custom_visibility ) && 'disabled' === $field->allow_custom_visibility ) {
     312        $current_level = $field->default_visibility;
     313    }
     314
     315    // If we're still empty, it means that overrides are permitted, but the
     316    // user has not provided a value. Use the default value.
     317    if ( empty( $current_level ) ) {
     318        $current_level = $field->default_visibility;
     319    }
     320
     321    return $current_level;
     322}
     323
    290324function xprofile_delete_field_data( $field, $user_id ) {
    291325    if ( is_numeric( $field ) )
  • trunk/tests/testcases/xprofile/functions.php

    r7812 r7836  
    9393        $this->assertFalse( wp_cache_get( 'fullname_field_id', 'bp_xprofile' ) );
    9494    }
     95
     96    /**
     97     * @group xprofile_get_field_visibility_level
     98     */
     99    public function test_bp_xprofile_get_field_visibility_level_missing_params() {
     100        $this->assertSame( '', xprofile_get_field_visibility_level( 0, 1 ) );
     101        $this->assertSame( '', xprofile_get_field_visibility_level( 1, 0 ) );
     102    }
     103
     104    /**
     105     * @group xprofile_get_field_visibility_level
     106     */
     107    public function test_bp_xprofile_get_field_visibility_level_user_set() {
     108        $u = $this->create_user();
     109        $g = $this->factory->xprofile_group->create();
     110        $f = $this->factory->xprofile_field->create( array(
     111            'field_group_id' => $g->id,
     112            'type' => 'textbox',
     113        ) );
     114
     115        bp_xprofile_update_meta( $f->id, 'field', 'default_visibility', 'adminsonly' );
     116        bp_xprofile_update_meta( $f->id, 'field', 'allow_custom_visibility', 'allowed' );
     117
     118        xprofile_set_field_visibility_level( $f->id, $u, 'loggedin' );
     119
     120        $this->assertSame( 'loggedin', xprofile_get_field_visibility_level( $f->id, $u ) );
     121    }
     122
     123    /**
     124     * @group xprofile_get_field_visibility_level
     125     */
     126    public function test_bp_xprofile_get_field_visibility_level_user_unset() {
     127        $u = $this->create_user();
     128        $g = $this->factory->xprofile_group->create();
     129        $f = $this->factory->xprofile_field->create( array(
     130            'field_group_id' => $g->id,
     131            'type' => 'textbox',
     132        ) );
     133
     134        bp_xprofile_update_meta( $f->id, 'field', 'default_visibility', 'adminsonly' );
     135        bp_xprofile_update_meta( $f->id, 'field', 'allow_custom_visibility', 'allowed' );
     136
     137        $this->assertSame( 'adminsonly', xprofile_get_field_visibility_level( $f->id, $u ) );
     138
     139    }
     140
     141    /**
     142     * @group xprofile_get_field_visibility_level
     143     */
     144    public function test_bp_xprofile_get_field_visibility_level_admin_override() {
     145        $u = $this->create_user();
     146        $g = $this->factory->xprofile_group->create();
     147        $f = $this->factory->xprofile_field->create( array(
     148            'field_group_id' => $g->id,
     149            'type' => 'textbox',
     150        ) );
     151
     152        bp_xprofile_update_meta( $f->id, 'field', 'default_visibility', 'adminsonly' );
     153        bp_xprofile_update_meta( $f->id, 'field', 'allow_custom_visibility', 'disabled' );
     154
     155        xprofile_set_field_visibility_level( $f->id, $u, 'loggedin' );
     156
     157        $this->assertSame( 'adminsonly', xprofile_get_field_visibility_level( $f->id, $u ) );
     158    }
    95159}
Note: See TracChangeset for help on using the changeset viewer.