Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/22/2021 07:45:24 PM (6 months ago)
Author:
imath
Message:

Prepare the xProfile component to handle the WordPress field types

  • Add a cache group to cache usermeta IDs by user IDs to avoid requesting information more than necessary when it hasn't been updated.
  • Edit the Profile field sanitization filter bp_xprofile_escape_field_data so that WordPress fields are escaped the way WordPress does.
  • Add the two new Field types wp-biography & wp-textbox to the available ones. See bp_xprofile_get_field_types().
  • Introduce the bp_xprofile_get_field_type() function to get the fied type of a field thanks to its ID.
  • Introduce a new filter bp_xprofile_set_field_data_pre_save. This filter returns false by default. You can use it to avoid using the Profile data DB table to save a field value by returning a WP_Error object or true. This filter is used by the WordPress field types to avoid duplicating the corresponding usermeta values into the Profile data DB table.
  • Introduce the bp_xprofile_get_wp_user_keys() function to inform about the supported WordPress fields (first_name, last_name, user_url, description and potential WP contact methods).
  • Edit the BP_XProfile_ProfileData methods to fetch field data so that they now try to fetch data within the usermeta table if the xProfile field has a WordPress field type.
  • Deprecate the BP_XProfile_ProfileData::get_value_byfieldname() as it's no more used into BP Core code.
  • Edit the function used to output the fields into the WP Admin/Extended profile screen so that WordPress fields are ignored as they can be updated from the WP Admin regular profile.

Props DJPaul, Offereins, needle, netweb, vapvarun

See #7162

File:
1 edited

Legend:

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

    r12781 r12869  
    317317     *
    318318     * @since 2.0.0
    319      *
    320      * @param int   $user_id   ID of user whose data is being queried.
    321      * @param array $field_ids Array of field IDs to query for.
     319     * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
     320     *              Adds a new parameter `$field_type_objects` to pass the list of field type objects.
     321     *
     322     * @param int   $user_id            ID of user whose data is being queried.
     323     * @param array $field_ids          Array of field IDs to query for.
     324     * @param array $field_type_objects Array of field type objects keyed by the queried filed IDs.
    322325     * @return array
    323326     */
    324     public static function get_data_for_user( $user_id, $field_ids ) {
     327    public static function get_data_for_user( $user_id, $field_ids, $field_type_objects = array() ) {
    325328        global $wpdb;
    326329
     
    340343                $d               = new stdClass;
    341344                $d->id           = $ud->id;
     345                $d->table_name   = $bp->profile->table_name_data;
    342346                $d->user_id      = $ud->user_id;
    343347                $d->field_id     = $ud->field_id;
     
    360364                // to avoid future cache misses.
    361365                } else {
    362                     $d               = new stdClass;
    363                     $d->id           = '';
     366                    $d = new stdClass;
     367
     368                    // Check WordPress if it's a WordPress field.
     369                    if ( isset( $field_type_objects[ $field_id ]->wp_user_key ) ) {
     370                        $meta          = $field_type_objects[ $field_id ]->get_field_value( $user_id, $field_id );
     371                        $d->id         = $meta['id'];
     372                        $d->value      = $meta['value'];
     373                        $d->table_name = $meta['table_name'];
     374
     375                    } else {
     376                        $d->id    = '';
     377                        $d->value = '';
     378                    }
     379
     380                    $d->table_name   = '';
    364381                    $d->user_id      = $user_id;
    365382                    $d->field_id     = $field_id;
    366                     $d->value        = '';
    367383                    $d->last_updated = '';
    368384
     
    397413     *
    398414     * @since 1.2.0
     415     * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
    399416     *
    400417     * @param int $user_id ID of the user.
     
    431448                        'field_id'         => $field->id,
    432449                        'field_type'       => $field->type,
    433                         'field_data'       => $field->data->value,
    434450                    );
     451
     452                    if ( is_null( $field->data ) ) {
     453                        if ( 1 === $field->id ) {
     454                            $profile_data[ $field->name ]['field_data'] = $user->display_name;
     455                        } elseif ( isset( $field->type_obj ) && $field->type_obj instanceof BP_XProfile_Field_Type && isset( $field->type_obj->wp_user_key ) ) {
     456                            $meta = $field->type_obj->get_field_value( $user->ID, $field->id );
     457
     458                            if ( isset( $meta['value'] ) ) {
     459                                $profile_data[ $field->name ]['field_data'] = $meta['value'];
     460                            }
     461                        } else {
     462                            $profile_data[ $field->name ]['field_data'] = false;
     463                        }
     464                    } else {
     465                        $profile_data[ $field->name ]['field_data'] = $field->data->value;
     466                    }
    435467                }
    436468            }
     
    476508     *
    477509     * @since 1.0.0
     510     * @since 8.0.0 Checks if a null field data is an xProfile WP Field.
    478511     *
    479512     * @param int            $field_id ID of the field.
     
    525558                // avoid cache misses and PHP notices.
    526559                } else {
    527                     $d = new stdClass;
    528                     $d->id           = '';
     560                    $d          = new stdClass;
     561                    $field_type = bp_xprofile_get_field_type( $field_id );
     562
     563                    // Check WordPress if it's a WordPress field.
     564                    if ( isset( $field_type->wp_user_key ) ) {
     565                        $meta          = $field_type->get_field_value( $user_id, $field_id );
     566                        $d->id         = $meta['id'];
     567                        $d->value      = $meta['value'];
     568                        $d->table_name = $meta['table_name'];
     569
     570                    } else {
     571                        $d->id    = '';
     572                        $d->value = '';
     573                    }
     574
     575                    $d->table_name   = '';
    529576                    $d->user_id      = $id;
    530577                    $d->field_id     = $field_id;
    531                     $d->value        = '';
    532578                    $d->last_updated = '';
    533579                }
     
    571617     *
    572618     * @since 1.0.0
     619     * @deprecated 8.0.0 This function is not used anymore.
    573620     *
    574621     * @param array|string $fields  Field(s) to get.
     
    577624     */
    578625    public static function get_value_byfieldname( $fields, $user_id = null ) {
     626        _deprecated_function( __FUNCTION__, '8.0.0' );
    579627        global $wpdb;
    580628
Note: See TracChangeset for help on using the changeset viewer.