Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/15/2012 08:33:09 PM (14 years ago)
Author:
boonebgorges
Message:

First pass at per-field visibility/privacy for XProfile:

  • Allows admins to set default levels for specific fields
  • Allows users to set visibility/privacy on a field-by-field basis
  • Modifies the profile templates to show markup for editing visibility status

See #3695

File:
1 edited

Legend:

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

    r5750 r5789  
    115115
    116116        $defaults = array(
    117             'profile_group_id'  => false,
    118             'user_id'           => bp_displayed_user_id(),
    119             'hide_empty_groups' => false,
    120             'hide_empty_fields' => false,
    121             'fetch_fields'      => false,
    122             'fetch_field_data'  => false,
    123             'exclude_groups'    => false,
    124             'exclude_fields'    => false
     117            'profile_group_id'    => false,
     118            'user_id'             => bp_displayed_user_id(),
     119            'hide_empty_groups'   => false,
     120            'hide_empty_fields'   => false,
     121            'fetch_fields'        => false,
     122            'fetch_field_data'    => false,
     123            'fetch_privacy_level' => false,
     124            'exclude_groups'      => false,
     125            'exclude_fields'      => false
    125126        );
    126127
     
    152153        if ( empty( $group_ids ) )
    153154            return $groups;
    154 
    155         $exclude_fields_sql = '';
    156         if ( !empty( $exclude_fields ) )
    157             $exclude_fields_sql = $wpdb->prepare( "AND id NOT IN ({$exclude_fields})" );
     155       
     156        // Support arrays and comma-separated strings
     157        if ( !empty( $exclude_fields ) && !is_array( $exclude_fields ) ) {
     158            $exclude_fields = explode( ',', $exclude_fields );
     159        }
     160       
     161        // Sanitization - ensure that each field_id passed is an integer
     162        $exclude_fields_cs = array();
     163        foreach( (array)$exclude_fields as $exclude_field ) {
     164            $efint = (int)$exclude_field;
     165            if ( !empty( $efint ) ) {
     166                $exclude_fields_cs[] = $efint;
     167            }
     168        }
     169       
     170        // Privacy - Handled here so as not to be overridden by sloppy use of the
     171        // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user()
     172        $exclude_fields_cs = array_merge( $exclude_fields_cs, bp_xprofile_get_hidden_fields_for_user( $user_id ) );
     173       
     174        $exclude_fields_cs = implode( ',', $exclude_fields_cs );
     175       
     176        if ( !empty( $exclude_fields_cs ) ) {
     177            $exclude_fields_sql = $wpdb->prepare( "AND id NOT IN ({$exclude_fields_cs})" );
     178        } else {
     179            $exclude_fields_sql = '';
     180        }
    158181
    159182        // Fetch the fields
     
    173196
    174197            if ( !empty( $field_ids ) )
    175                 $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) );
     198                $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) );
    176199
    177200            // Remove data-less fields, if necessary
     
    211234
    212235                        // Assign correct data value to the field
    213                         if ( $field->id == $data->field_id )
     236                        if ( $field->id == $data->field_id ) {
    214237                            $fields[$field_key]->data->value = $data->value;
     238                            $fields[$field_key]->data->id = $data->id;
     239                        }
    215240                    }
     241                }
     242                       
     243                if ( $fetch_privacy_level ) {
     244                    $fields = self::fetch_privacy_level( $user_id, $fields );
    216245                }
    217246            }
     
    264293        return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) );
    265294    }
    266 
     295   
     296    /**
     297     * Fetch the field privacy level for the returned fielddata
     298     */
     299    function fetch_privacy_level( $user_id, $fields ) {
     300        global $wpdb, $bp;
     301       
     302        // Get the user's privacy level preferences
     303        $privacy_levels = get_user_meta( $user_id, 'bp_xprofile_privacy_levels', true );
     304       
     305        foreach( (array)$fields as $key => $field ) {
     306            // Look to see if the user has set the privacy for this field
     307            if ( isset( $privacy_levels[$field->id] ) ) {
     308                $field_privacy = $privacy_levels[$field->id];
     309            } else {
     310                // If not, bring up the admin-set defaults
     311                if ( !isset( $default_privacy_levels ) ) {
     312                    $default_privacy_levels = self::fetch_default_privacy_levels();
     313                }
     314               
     315                // If no admin-set default is saved, fall back on a global default
     316                $field_privacy = !empty( $default_privacy_levels[$field->id] ) ? $default_privacy_levels[$field->id] : apply_filters( 'bp_xprofile_default_privacy_level', 'public' );
     317            }
     318           
     319            $fields[$key]->privacy_level = $field_privacy;
     320        }
     321       
     322        return $fields;
     323    }
     324   
     325    /**
     326     * Fetch the admin-set default privacy levels for all fields
     327     */
     328    function fetch_default_privacy_levels() {
     329        global $wpdb, $bp;
     330       
     331        $levels = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND meta_key = 'default_privacy'" ) );
     332       
     333        // Arrange so that the field id is the key and the privacy level the value
     334        $default_privacy_levels = array();
     335        foreach( $levels as $level ) {
     336            $default_privacy_levels[$level->object_id] = $level->meta_value;
     337        }
     338       
     339        return $default_privacy_levels;
     340    }
     341   
    267342    /* ADMIN AREA HTML.
    268343    * TODO: Get this out of here and replace with standard loops
     
    344419    var $order_by;
    345420    var $is_default_option;
     421    var $default_privacy;
    346422
    347423    var $data;
     
    381457                $this->data          = $this->get_field_data( $user_id );
    382458            }
     459           
     460            $this->default_privacy = bp_xprofile_get_meta( $id, 'field', 'default_privacy' );
     461           
     462            if ( empty( $this->default_privacy ) ) {
     463                $this->default_privacy = 'public';
     464            }
    383465        }
    384466    }
     
    639721            if ( $this->type != $type ) {
    640722                $class = 'display: none;';
     723            }
     724           
     725            if ( empty( $this->default_privacy ) ) {
     726                $this->default_privacy = 'public';
    641727            }
    642728
     
    788874                    <?php } ?>
    789875
     876                    <?php /* The fullname field cannot be hidden */ ?>
     877                    <?php if ( 1 != $this->id ) : ?>
     878
     879                        <div id="titlediv">
     880                            <h3><label for="default-privacy"><?php _e( "Default Privacy Level", 'buddypress' ); ?></label></h3>
     881                            <div id="titlewrap">
     882                                <ul>
     883                                <?php foreach( bp_xprofile_get_privacy_levels() as $level ) : ?>
     884                                    <li><input type="radio" name="default-privacy" value="<?php echo esc_attr( $level['id'] ) ?>" <?php checked( $this->default_privacy, $level['id'] ) ?>> <?php echo esc_html( $level['label'] ) ?></li>
     885                                <?php endforeach ?>
     886                                </ul>
     887                            </div>
     888                        </div>
     889                   
     890                    <?php endif ?>
     891
    790892                    <p class="submit">
    791893                        <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
     
    9681070        return $profile_data;
    9691071    }
     1072   
     1073    /**
     1074     * Get the user's field data id by the id of the xprofile field
     1075     *
     1076     * @param int $field_id
     1077     * @param int $user_id
     1078     * @return int $fielddata_id
     1079     */
     1080    function get_fielddataid_byid( $field_id, $user_id ) {
     1081        global $wpdb, $bp;
     1082       
     1083        if ( empty( $field_id ) || empty( $user_id ) ) {
     1084            $fielddata_id = 0;
     1085        } else {
     1086            $fielddata_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ) );
     1087        }
     1088       
     1089        return $fielddata_id;
     1090    }
    9701091
    9711092    function get_value_byid( $field_id, $user_ids = null ) {
Note: See TracChangeset for help on using the changeset viewer.