Skip to:
Content

BuddyPress.org

Changeset 11316


Ignore:
Timestamp:
12/21/2016 02:42:05 AM (8 years ago)
Author:
boonebgorges
Message:

XProfile: More consistent cache behavior when fetching user data.

  • Inside of a profile group loop (BP_XProfile_Group::get()), don't fetch user data when pulling up BP_XProfile_Field objects. In the absence of finer-grained information about users, fetching a field object grabs the data associated with the logged-in user. But in many cases, the logged-in user is irrelevant to the fields being looped over, so there's no benefit to pulling up this data. (When necessary - fetch_data - the data is queried separately, later in the get() method.)
  • When caching database misses for a data query (because the specifed user doesn't have anything filled in for the given field), store the field_id and user_id properties on the cached object. This ensures that values are properly associated with their fields when being displayed.

These changes resolve an issue where cached data for the logged-in user
can be shown erroneously on another user's profile, when the other user
doesn't have a value for a given field.

Props m_uysl, r-a-y.
See #6091. Fixes #7401.

Location:
trunk/src/bp-xprofile
Files:
4 edited

Legend:

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

    r11295 r11316  
    283283 *
    284284 * @since 1.1.0
    285  *
    286  * @param int|object $field ID of the field or object representing field data.
     285 * @since 2.8.0 Added `$user_id` and `$get_data` parameters.
     286 *
     287 * @param int|object $field    ID of the field or object representing field data.
     288 * @param int        $user_id  Optional. ID of the user associated with the field.
     289 *                             Ignored if `$get_data` is false. If `$get_data` is
     290 *                             true, but no `$user_id` is provided, defaults to
     291 *                             logged-in user ID.
     292 * @param bool       $get_data Whether to fetch data for the specified `$user_id`.
    287293 * @return BP_XProfile_Field|null Field object if found, otherwise null.
    288294 */
    289 function xprofile_get_field( $field ) {
     295function xprofile_get_field( $field, $user_id = null, $get_data = true ) {
    290296    if ( $field instanceof BP_XProfile_Field ) {
    291297        $_field = $field;
     
    294300        $_field->fill_data( $field );
    295301    } else {
    296         $_field = BP_XProfile_Field::get_instance( $field );
     302        $_field = BP_XProfile_Field::get_instance( $field, $user_id, $get_data );
    297303    }
    298304
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r11169 r11316  
    228228     *
    229229     * @since 2.4.0
     230     * @since 2.8.0 Added `$user_id` and `$get_data` parameters.
    230231     *
    231232     * @static
    232233     *
    233      * @param int $field_id ID of the field.
     234     * @param int  $field_id ID of the field.
     235     * @param int  $user_id  Optional. ID of the user associated with the field.
     236     *                       Ignored if `$get_data` is false. If `$get_data` is
     237     *                       true, but no `$user_id` is provided, defaults to
     238     *                       logged-in user ID.
     239     * @param bool $get_data Whether to fetch data for the specified `$user_id`.
    234240     * @return BP_XProfile_Field|false Field object if found, otherwise false.
    235241     */
    236     public static function get_instance( $field_id ) {
     242    public static function get_instance( $field_id, $user_id = null, $get_data = true ) {
    237243        global $wpdb;
    238244
     
    242248        }
    243249
    244         return new self( $field_id );
     250        return new self( $field_id, $user_id, $get_data );
    245251    }
    246252
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r11169 r11316  
    397397        $fields = array();
    398398        foreach ( $field_ids as $field_id ) {
    399             $fields[] = xprofile_get_field( $field_id );
     399            $fields[] = xprofile_get_field( $field_id, null, false );
    400400        }
    401401
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-profiledata.php

    r11030 r11316  
    324324                    $d               = new stdClass;
    325325                    $d->id           = '';
    326                     $d->user_id      = '';
     326                    $d->user_id      = $user_id;
    327327                    $d->field_id     = $field_id;
    328328                    $d->value        = '';
     
    490490                    $d->id           = '';
    491491                    $d->user_id      = $id;
    492                     $d->field_id     = '';
     492                    $d->field_id     = $field_id;
    493493                    $d->value        = '';
    494494                    $d->last_updated = '';
Note: See TracChangeset for help on using the changeset viewer.