Skip to:
Content

BuddyPress.org

Ticket #4636: 4636.patch

File 4636.patch, 4.1 KB (added by ericlewis, 12 years ago)
  • bp-xprofile/bp-xprofile-functions.php

     
    199199 * @param $value The value for the field you want to set for the user.
    200200 * @global BuddyPress $bp The one true BuddyPress instance
    201201 * @uses xprofile_get_field_id_from_name() Gets the ID for the field based on the name.
    202  * @return true on success, false on failure.
     202 * @return true on success, WP_Error on failure.
    203203 */
    204204function xprofile_set_field_data( $field, $user_id, $value, $is_required = false ) {
    205205
     
    209209                $field_id = xprofile_get_field_id_from_name( $field );
    210210
    211211        if ( empty( $field_id ) )
    212                 return false;
     212                return new WP_Error( 'empty_field_id', __( 'Could not find profile field ID given field name', 'buddypress' ) );
    213213
    214214        if ( $is_required && ( empty( $value ) || !is_array( $value ) && !strlen( trim( $value ) ) ) )
    215                 return false;
     215                return new WP_Error( 'empty_required_field', __( 'Required field must have a value', 'buddypress' ) );
    216216
    217217        $field = new BP_XProfile_Field( $field_id );
    218218
     
    243243                        $value = array_merge( array(), $value );
    244244                } else {
    245245                        if ( !in_array( $value, $possible_values ) ) {
    246                                 return false;
     246                                return new WP_Error( 'value_not_in_possible_values', __( 'Field value entered is not in possible values', 'buddypress' ) );
    247247                        }
    248248                }
    249249        }
    250250
    251         $field           = new BP_XProfile_ProfileData();
    252         $field->field_id = $field_id;
    253         $field->user_id  = $user_id;
    254         $field->value    = maybe_serialize( $value );
     251        $field = new BP_XProfile_ProfileData( $field_id, $user_id );
     252        if ( $field->exists() && $value == $field->value )
     253                return new WP_Error( 'same_value', __( 'Field value entered is the same as the current value', 'buddypress' ) );
     254        $field->value = maybe_serialize( $value );
    255255
    256256        return $field->save();
    257257}
  • bp-xprofile/bp-xprofile-classes.php

     
    10301030                return apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool)$retval, $this ) );
    10311031        }
    10321032
     1033        /**
     1034         * save()
     1035         *
     1036         * Save the profile field data to the database.
     1037         *
     1038         * @global object $wpdb
     1039         * @global object $bp
     1040         * @return true on success, WP_Error on failure
     1041         */
    10331042        function save() {
    10341043                global $wpdb, $bp;
    10351044
     
    10541063                        }
    10551064
    10561065                        if ( false === $result )
    1057                                 return false;
     1066                                return new WP_Error( 'db_error', __( 'Could not update or insert profile data into the database', 'buddypress' ), $wpdb->last_error );
    10581067
    10591068                        do_action_ref_array( 'xprofile_data_after_save', array( $this ) );
    10601069
     1070                        if ( bp_is_active( 'activity' ) ) {
     1071                                $field = new BP_XProfile_Field( $this->id, $this->user_id, false );
     1072                                bp_activity_add( array( 'action' => sprintf( __( '%s edited %s on their profile', 'buddypress' ), bp_core_get_userlink( $this->user_id ), $field->name ),
     1073                                        'component' => 'xprofile', 'primary_link' => bp_loggedin_user_domain() . BP_XPROFILE_SLUG, 'type' => 'profile_updated', 'item_id' => $this->id ) );
     1074                        }
     1075
    10611076                        return true;
    10621077                }
    10631078
    1064                 return false;
     1079                return new WP_Error( 'invalid_field', __( 'The field ID is invalid', 'buddypress' ) );
    10651080        }
    10661081
    10671082        function delete() {
  • bp-xprofile/bp-xprofile-screens.php

     
    111111                                        $value = $_POST['field_' . $field_id];
    112112                                }
    113113
    114                                 if ( !xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[$field_id] ) ) {
     114                                $saved = xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[$field_id] );
     115                                if ( is_wp_error( $saved ) && $saved->get_error_code() != 'same_value' ) {
    115116                                        $errors = true;
    116                                 } else {
     117                                } else if ( is_bool( $saved ) && $saved ) {
    117118                                        do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
    118119                                }
    119120