Skip to:
Content

BuddyPress.org

Changeset 7812


Ignore:
Timestamp:
02/06/2014 07:38:35 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce bp_xprofile_fullname_field_id() and use in query building throughout BP

There are many places throughout BuddyPress where it's necessary to query for
user fullnames. When XProfile is active, part of this process involves
determining the ID of the fullname field. Historically, this has been done by
querying for the ID of a field corresponding to the 'name' bp_xprofile_fullname_field_name().
Creating a standalone function for this allows us to centralize persistent
caching for the field ID.

As a side note, the fact that we do this query at all is a little farcical.
The fullname field is essentially necessarily tied to field #1 (due to some
hardcoding in the xprofile schema definition as well as bp-xprofile-admin.php).
We rely on the Name setting only for historical reasons, which probably didn't
even make all that much sense at the time. At some point, a serious review
should be done, and references to the fullname field id standardized (either
all hardcoded, or all not hardcoded).

See #5362

Location:
trunk
Files:
6 edited

Legend:

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

    r7800 r7812  
    302302                // the xprofile table
    303303                } else {
    304                     $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) );
    305 
    306304                    $this->uid_name = 'user_id';
    307305                    $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$bp->profile->table_name_data} u";
    308                     $sql['where'][] = "u.field_id = {$fullname_field_id}";
     306                    $sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() );
    309307                    $sql['orderby'] = "ORDER BY u.value";
    310308                    $sql['order']   = "ASC";
  • trunk/bp-xprofile/bp-xprofile-cache.php

    r7791 r7812  
    5959add_action( 'xprofile_data_after_delete', 'xprofile_clear_profiledata_object_cache' );
    6060
     61/**
     62 * Clear fullname_field_id cache when bp-xprofile-fullname-field-name is updated.
     63 *
     64 * Note for future developers: Dating from an early version of BuddyPress where
     65 * the fullname field (field #1) did not have a title that was editable in the
     66 * normal Profile Fields admin interface, we have the bp-xprofile-fullname-field-name
     67 * option. In many places throughout BuddyPress, the ID of the fullname field
     68 * is queried using this setting. However, this is no longer strictly necessary,
     69 * because we essentially hardcode (in the xprofile admin save routine, as well
     70 * as the xprofile schema definition) that the fullname field will be 1. The
     71 * presence of the non-hardcoded versions (and thus this bit of cache
     72 * invalidation) is thus for backward compatibility only.
     73 *
     74 * @since BuddyPress (2.0.0)
     75 */
     76function xprofile_clear_fullname_field_id_cache() {
     77    wp_cache_delete( 'fullname_field_id', 'bp_xprofile' );
     78}
     79add_action( 'update_option_bp-xprofile-fullname-field-name', 'xprofile_clear_fullname_field_id_cache' );
     80
    6181// List actions to clear super cached pages on, if super cache is installed
    6282add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r7808 r7812  
    14921492            $user_id = bp_displayed_user_id();
    14931493
    1494         $field_name = bp_xprofile_fullname_field_name();
    1495         $data       = xprofile_get_field_data( $field_name, $user_id );
     1494        $data = xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );
    14961495
    14971496        return $data[$field_name];
  • trunk/bp-xprofile/bp-xprofile-filters.php

    r7810 r7812  
    252252    }
    253253
    254     $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) );
    255     $user_id_names     = BP_XProfile_ProfileData::get_value_byid( $fullname_field_id, $user_query->user_ids );
     254    $user_id_names = BP_XProfile_ProfileData::get_value_byid( bp_xprofile_fullname_field_id(), $user_query->user_ids );
    256255
    257256    // Loop through names and override each user's fullname
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7730 r7812  
    438438        return false;
    439439
    440     $fullname = xprofile_get_field_data( bp_xprofile_fullname_field_name(), $user_id );
     440    $fullname = xprofile_get_field_data( bp_xprofile_fullname_field_id(), $user_id );
    441441    $space    = strpos( $fullname, ' ' );
    442442
     
    474474        return;
    475475
    476     xprofile_set_field_data( bp_xprofile_fullname_field_name(), $user->ID, $user->display_name );
     476    xprofile_set_field_data( bp_xprofile_fullname_field_id(), $user->ID, $user->display_name );
    477477}
    478478add_action( 'user_profile_update_errors', 'xprofile_sync_bp_profile', 10, 3 );
     
    626626
    627627/**
     628 * Return the field ID for the Full Name xprofile field.
     629 *
     630 * @since BuddyPress (2.0.0)
     631 *
     632 * @return int Field ID.
     633 */
     634function bp_xprofile_fullname_field_id() {
     635    $id = wp_cache_get( 'fullname_field_id', 'bp_xprofile' );
     636
     637    if ( false === $id ) {
     638        global $wpdb;
     639
     640        $bp = buddypress();
     641        $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) );
     642
     643        wp_cache_set( 'fullname_field_id', $id, 'bp_xprofile' );
     644    }
     645
     646    return absint( $id );
     647}
     648
     649/**
    628650 * Return the field name for the Full Name xprofile field
    629651 *
  • trunk/tests/testcases/xprofile/functions.php

    r7469 r7812  
    8181        $this->assertEquals( $meta_value, bp_xprofile_get_meta( $f->id, 'field', 'linebreak_field' ) );
    8282    }
     83
     84    /**
     85     * @group bp_xprofile_fullname_field_id
     86     */
     87    public function test_bp_xprofile_fullname_field_id_invalidation() {
     88        // Prime the cache
     89        $id = bp_xprofile_fullname_field_id();
     90
     91        bp_update_option( 'bp-xprofile-fullname-field-name', 'foo' );
     92
     93        $this->assertFalse( wp_cache_get( 'fullname_field_id', 'bp_xprofile' ) );
     94    }
    8395}
Note: See TracChangeset for help on using the changeset viewer.