/**
 * A simple function to set profile data for a specific field for a specific user.
 *
 * @package BuddyPress Core
 * @param int|string $field The ID of the field, or the $name of the field.
 * @param int|$user_id      The ID of the user
 * @param mixed $value      The value for the field you want to set for the user.
 * @param $is_required      Whether or not the field is required
 * @uses xprofile_get_field_id_from_name() Gets the ID for the field based on the name.
 * @return bool True on success, false on failure.
 */
function xprofile_set_field_data($field, $user_id, $value, $is_required = false)
{
    if (is_numeric($field)) {
        $field_id = $field;
    } else {
        $field_id = xprofile_get_field_id_from_name($field);
    }
    if (empty($field_id)) {
        return false;
    }
    $field = new BP_XProfile_Field($field_id);
    $field_type = BP_XProfile_Field::get_type($field_id);
    $field_type_obj = bp_xprofile_create_field_type($field_type);
    /**
     * Filter the raw submitted profile field value.
     *
     * Use this filter to modify the values submitted by users before
     * doing field-type-specific validation.
     *
     * @since BuddyPress (2.1.0)
     *
     * @param mixed $value Value passed to xprofile_set_field_data()
     * @param BP_XProfile_Field $field Field object.
     * @param BP_XProfile_Field_Type $field_type_obj Field type object.
     */
    $value = apply_filters('bp_xprofile_set_field_data_pre_validate', $value, $field, $field_type_obj);
    // Special-case support for integer 0 for the number field type
    if ($is_required && !is_integer($value) && $value !== '0' && (empty($value) || !is_array($value) && !strlen(trim($value)))) {
        return false;
    }
    /**
     * Certain types of fields (checkboxes, multiselects) may come through empty.
     * Save as empty array so this isn't overwritten by the default on next edit.
     *
     * Special-case support for integer 0 for the number field type
     */
    if (empty($value) && !is_integer($value) && $value !== '0' && $field_type_obj->accepts_null_value) {
        $value = array();
    }
    // If the value is empty, then delete any field data that exists, unless the field is of a type where null values are semantically meaningful
    if (empty($value) && !is_integer($value) && $value !== '0' && !$field_type_obj->accepts_null_value) {
        xprofile_delete_field_data($field_id, $user_id);
        return true;
    }
    // For certain fields, only certain parameters are acceptable, so add them to the whitelist.
    if ($field_type_obj->supports_options) {
        $field_type_obj->set_whitelist_values(wp_list_pluck($field->get_children(), 'name'));
    }
    // Check the value is in an accepted format for this form field.
    if (!$field_type_obj->is_valid($value)) {
        return false;
    }
    $field = new BP_XProfile_ProfileData();
    $field->field_id = $field_id;
    $field->user_id = $user_id;
    $field->value = maybe_serialize($value);
    return $field->save();
}
Example #2
0
/**
 * xprofile_set_field_data()
 *
 * A simple function to set profile data for a specific field for a specific user.
 *
 * @package BuddyPress Core
 * @param $field The ID of the field, or the $name of the field.
 * @param $user_id The ID of the user
 * @param $value The value for the field you want to set for the user.
 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
 * @uses xprofile_get_field_id_from_name() Gets the ID for the field based on the name.
 * @return true on success, false on failure.
 */
function xprofile_set_field_data( $field, $user_id, $value, $is_required = false ) {
	if ( is_numeric( $field ) )
		$field_id = $field;
	else
		$field_id = xprofile_get_field_id_from_name( $field );

	if ( !$field_id )
		return false;

	if ( $is_required && ( empty( $value ) || !strlen( trim( $value ) ) ) )
		return false;

	/* If the value is empty, then delete any field data that exists */
	if ( empty( $value ) ) {
		xprofile_delete_field_data( $field_id, $user_id );
		return true;
	}

	$field = new BP_XProfile_Field( $field_id );

	/* Check the value is an acceptable value */
	if ( 'checkbox' == $field->type || 'radio' == $field->type || 'selectbox' == $field->type || 'multiselectbox' == $field->type ) {
		$options = $field->get_children();

		foreach( $options as $option )
			$possible_values[] = $option->name;

		if ( is_array( $value ) ) {
			foreach( $value as $i => $single ) {
				if ( !in_array( $single, (array)$possible_values ) )
					unset( $value[$i] );
			}

			if ( empty( $value ) )
				return false;

			/* Reset the keys by merging with an empty array */
			$value = array_merge( array(), $value );
		} else {
			if ( !in_array( $value, (array)$possible_values ) )
				return false;
		}
	}

	$field = new BP_XProfile_ProfileData();
	$field->field_id = $field_id;
	$field->user_id = $user_id;
	$field->value = maybe_serialize( $value );

	return $field->save();
}
/**
 * A simple function to set profile data for a specific field for a specific user.
 *
 * @package BuddyPress Core
 * @param $field The ID of the field, or the $name of the field.
 * @param $user_id The ID of the user
 * @param $value The value for the field you want to set for the user.
 * @global object $bp Global BuddyPress settings object
 * @uses xprofile_get_field_id_from_name() Gets the ID for the field based on the name.
 * @return true on success, false on failure.
 */
function xprofile_set_field_data($field, $user_id, $value, $is_required = false)
{
    if (is_numeric($field)) {
        $field_id = $field;
    } else {
        $field_id = xprofile_get_field_id_from_name($field);
    }
    if (empty($field_id)) {
        return false;
    }
    if ($is_required && (empty($value) || !is_array($value) && !strlen(trim($value)))) {
        return false;
    }
    $field = new BP_XProfile_Field($field_id);
    // If the value is empty, then delete any field data that exists, unless the field is of a
    // type where null values are semantically meaningful
    if (empty($value) && 'checkbox' != $field->type && 'multiselectbox' != $field->type) {
        xprofile_delete_field_data($field_id, $user_id);
        return true;
    }
    // Check the value is an acceptable value
    if ('checkbox' == $field->type || 'radio' == $field->type || 'selectbox' == $field->type || 'multiselectbox' == $field->type) {
        $options = $field->get_children();
        foreach ($options as $option) {
            $possible_values[] = $option->name;
        }
        if (is_array($value)) {
            foreach ($value as $i => $single) {
                if (!in_array($single, (array) $possible_values)) {
                    unset($value[$i]);
                }
            }
            // Reset the keys by merging with an empty array
            $value = array_merge(array(), $value);
        } else {
            if (!in_array($value, (array) $possible_values)) {
                return false;
            }
        }
    }
    $field = new BP_XProfile_ProfileData();
    $field->field_id = $field_id;
    $field->user_id = $user_id;
    $field->value = maybe_serialize($value);
    return $field->save();
}
 /**
  * Delete the fields on new user activation/profile update that do not conform to our condition
  * and yes, I am the boss here, don' ask me the logic :P
  * 
  * @param type $user_id
  */
 public function update_saved_fields($user_id)
 {
     //build all conditions array
     $this->build_conditions();
     //get the fields whose value triggers conditions
     $conditional_fields = $this->conditional_fields;
     //Now, There can be multiple conditional fields
     foreach ($conditional_fields as $conditional_field_id => $related_fields) {
         //for each field triggering the condition, get the field data for this field
         $conditional_field_id = (int) str_replace('field_', '', $conditional_field_id);
         $data = xprofile_get_field_data($conditional_field_id, $user_id, 'array');
         //find all the conditions which are based on the vale of this field
         foreach ($related_fields['conditions'] as $condition) {
             //check if condition is matched
             if ($this->is_match($data, $condition['value'], $condition['operator'])) {
                 //if visibility is set to hidden and condition matched, delete data for the field on which this condition is applied
                 if ($condition['visibility'] == 'hide') {
                     xprofile_delete_field_data($condition['field_id'], $user_id);
                 }
             } else {
                 //if there is no match and the visibility is set to show on condition, we still need to delete the data for the field on which this condition is applied
                 if ($condition['visibility'] == 'show') {
                     xprofile_delete_field_data($condition['field_id'], $user_id);
                 }
             }
         }
     }
 }