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/bp-xprofile-functions.php

    r12768 r12869  
    160160        'textbox'        => 'BP_XProfile_Field_Type_Textbox',
    161161        'telephone'      => 'BP_XProfile_Field_Type_Telephone',
     162        'wp-biography'   => 'BP_XProfile_Field_Type_WordPress_Biography',
     163        'wp-textbox'     => 'BP_XProfile_Field_Type_WordPress_Textbox',
    162164    );
    163165
     
    312314
    313315/**
     316 * Get a profile Field Type object.
     317 *
     318 * @since 8.0.0
     319 *
     320 * @param int $field_id ID of the field.
     321 * @return BP_XProfile_Field_Type|null Field Type object if found, otherwise null.
     322 */
     323function bp_xprofile_get_field_type( $field_id ) {
     324    $field_type = null;
     325    $field      = xprofile_get_field( $field_id, null, false );
     326
     327    if ( $field instanceof BP_XProfile_Field ) {
     328        $field_type = $field->type_obj;
     329    }
     330
     331    return $field_type;
     332}
     333
     334/**
    314335 * Delete a profile field object.
    315336 *
     
    463484    }
    464485
    465     $field           = new BP_XProfile_ProfileData();
    466     $field->field_id = $field_id;
    467     $field->user_id  = $user_id;
    468 
    469     // Gets un/reserialized via xprofile_sanitize_data_value_before_save().
    470     $field->value    = maybe_serialize( $value );
    471 
    472     return $field->save();
     486    $field_args = compact( 'field_type_obj', 'field', 'user_id', 'value', 'is_required' );
     487
     488    /**
     489     * Return a WP_Error object or true to use your custom way of saving field values.
     490     *
     491     * @since 8.0.0
     492     *
     493     * @param boolean Whether to shortcircuit the $bp->profile->table_name_data table.
     494     * @param array $field_args {
     495     *     An array of arguments.
     496     *
     497     *     @type object            $field_type_obj Field type object.
     498     *     @type BP_XProfile_Field $field          Field object.
     499     *     @type integer           $user_id        The user ID.
     500     *     @type mixed             $value          Value passed to xprofile_set_field_data().
     501     *     @type boolean           $is_required    Whether or not the field is required.
     502     * }
     503     */
     504    $retval = apply_filters( 'bp_xprofile_set_field_data_pre_save', false, $field_args );
     505
     506    if ( is_wp_error( $retval ) ) {
     507        return false;
     508    }
     509
     510    if ( false === $retval ) {
     511        $field           = new BP_XProfile_ProfileData();
     512        $field->field_id = $field_id;
     513        $field->user_id  = $user_id;
     514
     515        // Gets un/reserialized via xprofile_sanitize_data_value_before_save().
     516        $field->value    = maybe_serialize( $value );
     517
     518        $retval = $field->save();
     519    }
     520
     521    return $retval;
    473522}
    474523
     
    13581407    );
    13591408}
     1409
     1410/**
     1411 * Returns the list of supporterd WordPress field meta keys.
     1412 *
     1413 * @since 8.0.0
     1414 *
     1415 * @return string[] List of supported WordPress user keys.
     1416 */
     1417function bp_xprofile_get_wp_user_keys() {
     1418    return array_merge(
     1419        array( 'first_name', 'last_name', 'user_url', 'description' ),
     1420        array_keys( wp_get_user_contact_methods() )
     1421    );
     1422}
Note: See TracChangeset for help on using the changeset viewer.