Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/07/2015 02:30:31 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce cache support for xprofile fields.

xprofile_get_field() will now look in the cache before returning a
BP_XProfile_Field object (via the new BP_XProfile_Field::get_instance()),
and will populate the cache if necessary. It's strongly recommended to use
xprofile_get_field() when fetching individual existing fields, instead of
creating new BP_XProfile_Field objects directly.

BP_XProfile_Group::get() now leverages the field cache too. Instead of a
SELECT * query, it fetches a list of matching field IDs SELECT id, and
populates the full field objects from the cache, if available. The fields
property of BP_XProfile_Group objects is now an array of BP_XProfile_Field
objects, not stdClass. See #6358.

Props boonebgorges, r-a-y.
See #6638.

File:
1 edited

Legend:

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

    r10195 r10198  
    212212        $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id ) );
    213213
    214         if ( ! empty( $field ) ) {
    215             $this->id                = $field->id;
    216             $this->group_id          = $field->group_id;
    217             $this->parent_id         = $field->parent_id;
    218             $this->type              = $field->type;
    219             $this->name              = stripslashes( $field->name );
    220             $this->description       = stripslashes( $field->description );
    221             $this->is_required       = $field->is_required;
    222             $this->can_delete        = $field->can_delete;
    223             $this->field_order       = $field->field_order;
    224             $this->option_order      = $field->option_order;
    225             $this->order_by          = $field->order_by;
    226             $this->is_default_option = $field->is_default_option;
    227 
    228             // Create the field type and store a reference back to this object.
    229             $this->type_obj            = bp_xprofile_create_field_type( $field->type );
    230             $this->type_obj->field_obj = $this;
    231 
    232             if ( ! empty( $get_data ) && ! empty( $user_id ) ) {
    233                 $this->data = $this->get_field_data( $user_id );
    234             }
    235         }
     214        $this->fill_data( $field );
     215
     216        if ( ! empty( $get_data ) && ! empty( $user_id ) ) {
     217            $this->data = $this->get_field_data( $user_id );
     218        }
     219    }
     220
     221    /**
     222     * Retrieve a `BP_XProfile_Field` instance.
     223     *
     224     * @static
     225     *
     226     * @param int $field_id ID of the field.
     227     * @return BP_XProfile_Field|false Field object if found, otherwise false.
     228     */
     229    public static function get_instance( $field_id ) {
     230        global $wpdb;
     231
     232        $field_id = (int) $field_id;
     233        if ( ! $field_id ) {
     234            return false;
     235        }
     236
     237        $field = wp_cache_get( $field_id, 'bp_xprofile_fields' );
     238        if ( false === $field ) {
     239            $bp = buddypress();
     240
     241            $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ) );
     242
     243            wp_cache_add( $field->id, $field, 'bp_xprofile_fields' );
     244
     245            if ( ! $field ) {
     246                return false;
     247            }
     248        }
     249
     250        $_field = new BP_XProfile_Field();
     251        $_field->fill_data( $field );
     252
     253        return $_field;
     254    }
     255
     256    /**
     257     * Fill object vars based on data passed to the method.
     258     *
     259     * @since 2.4.0
     260     *
     261     * @param array|object $args Array or object representing the `BP_XProfile_Field` properties.
     262     *                           Generally, this is a row from the fields database table.
     263     */
     264    public function fill_data( $args ) {
     265        if ( is_object( $args ) ) {
     266            $args = (array) $args;
     267        }
     268
     269        foreach ( $args as $k => $v ) {
     270            if ( 'name' === $k || 'description' === $k ) {
     271                $v = stripslashes( $v );
     272            }
     273            $this->{$k} = $v;
     274        }
     275
     276        // Create the field type and store a reference back to this object.
     277        $this->type_obj            = bp_xprofile_create_field_type( $this->type );
     278        $this->type_obj->field_obj = $this;;
    236279    }
    237280
Note: See TracChangeset for help on using the changeset viewer.