Skip to:
Content

BuddyPress.org

Changeset 12869


Ignore:
Timestamp:
03/22/2021 07:45:24 PM (6 years 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

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

Legend:

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

    r12529 r12869  
    303303// List actions to clear super cached pages on, if super cache is installed.
    304304add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
     305
     306/**
     307 * Resets the User Metadata ids cache.
     308 *
     309 * @since 8.0.0
     310 *
     311 * @param integer $user_id The user ID.
     312 */
     313function bp_xprofile_reset_user_mid_cache( $user_id ) {
     314        wp_cache_delete( $user_id, 'bp_user_mid' );
     315}
     316add_action( 'profile_update', 'bp_xprofile_reset_user_mid_cache', 10, 1 );
  • trunk/src/bp-xprofile/bp-xprofile-filters.php

    r12806 r12869  
    353353 */
    354354function bp_xprofile_escape_field_data( $value, $field_type, $field_id ) {
     355        // Sanitization for these types is directly done into their `display_filter()` method.
     356        if ( 'wp-biography' === $field_type || 'wp-textbox' === $field_type ) {
     357                return $value;
     358        }
     359
    355360        if ( bp_xprofile_is_richtext_enabled_for_field( $field_id ) ) {
    356361                // The xprofile_filter_kses() expects a BP_XProfile_ProfileData object.
  • 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}
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-component.php

    r12694 r12869  
    411411                        'bp_xprofile_fields',
    412412                        'bp_xprofile_groups',
    413                         'xprofile_meta'
     413                        'xprofile_meta',
     414                        'bp_user_mid',
    414415                ) );
    415416
  • 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
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-user-admin.php

    r12554 r12869  
    273273                $r = bp_parse_args( $args['args'], array(
    274274                        'profile_group_id' => 0,
    275                         'user_id'          => $user->ID
     275                        'user_id'          => $user->ID,
     276                        'hide_field_types' => array( 'wp-textbox', 'wp-biography' ),
    276277                ), 'bp_xprofile_user_admin_profile_loop_args' );
    277278
  • trunk/src/bp-xprofile/screens/edit.php

    r11928 r12869  
    5050                $posted_field_ids = wp_parse_id_list( $_POST['field_ids'] );
    5151                $is_required      = array();
     52
     53                $bp_displayed_user = bp_get_displayed_user();
     54                $bp_displayed_user->updated_keys = array();
    5255
    5356                // Loop through the posted fields formatting any datebox values then validate the field.
    … …  
    127130                        do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
    128131
     132                        // Some WP User keys have been updated: let's update the WP fiels all together.
     133                        if ( $bp_displayed_user->updated_keys ) {
     134                                $user_id = wp_update_user(
     135                                        array_merge(
     136                                                array(
     137                                                        'ID' => bp_displayed_user_id(),
     138                                                ),
     139                                                $bp_displayed_user->updated_keys
     140                                        )
     141                                );
     142
     143                                $bp_displayed_user->updated_keys = array();
     144
     145                                if ( is_wp_error( $user_id ) ) {
     146                                        $errors = true;
     147                                }
     148                        }
     149
    129150                        // Set the feedback messages.
    130151                        if ( !empty( $errors ) ) {
Note: See TracChangeset for help on using the changeset viewer.