Beispiel #1
0
 /**
  * @group bp_xprofile_update_meta_cache
  * @group bp_has_profile
  */
 public function test_bp_has_profile_meta_cache_update_meta_cache_false()
 {
     $u = $this->factory->user->create();
     $g = $this->factory->xprofile_group->create();
     $f = $this->factory->xprofile_field->create(array('field_group_id' => $g));
     $d = new BP_XProfile_ProfileData($f, $u);
     $d->user_id = $u;
     $d->field_id = $f;
     $d->value = 'foo';
     $d->last_updated = bp_core_current_time();
     $d->save();
     bp_xprofile_add_meta($g, 'group', 'group_foo', 'group_bar');
     bp_xprofile_add_meta($f, 'field', 'field_foo', 'field_bar');
     bp_xprofile_add_meta($d->id, 'data', 'data_foo', 'data_bar');
     // prime cache
     bp_has_profile(array('user_id' => $u, 'profile_group_id' => $g, 'update_meta_cache' => false));
     $this->assertFalse(wp_cache_get($g, 'xprofile_group_meta'));
     $this->assertFalse(wp_cache_get($f, 'xprofile_field_meta'));
     $this->assertFalse(wp_cache_get($d->id, 'xprofile_data_meta'));
 }
/**
 * 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();
}
/**
 * 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();
}
function xprofile_extract_signup_meta($user_id, $meta)
{
    // Extract signup meta fields to fill out profile
    $field_ids = $meta['xprofile_field_ids'];
    $field_ids = explode(',', $field_ids);
    // Loop through each bit of profile data and save it to profile.
    for ($i = 0; $i < count($field_ids); $i++) {
        if (empty($field_ids[$i])) {
            continue;
        }
        $field_value = $meta["field_{$field_ids[$i]}"];
        $field = new BP_XProfile_ProfileData();
        $field->user_id = $user_id;
        $field->value = $field_value;
        $field->field_id = $field_ids[$i];
        $field->last_updated = time();
        $field->save();
    }
    update_usermeta($user_id, 'last_activity', time());
}
Beispiel #5
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_name The name of the field to set data for.
 * @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_name, $user_id, $value)
{
    global $bp;
    if (!($field_id = xprofile_get_field_id_from_name($field_name))) {
        return false;
    }
    $field = new BP_XProfile_ProfileData();
    $field->field_id = $field_id;
    $field->user_id = $user_id;
    $field->value = $value;
    return $field->save();
}
 /**
  * @group get_all_for_user
  */
 public function test_get_all_for_user_uncached()
 {
     $u = $this->factory->user->create();
     $g1 = $this->factory->xprofile_group->create();
     $g2 = $this->factory->xprofile_group->create();
     $f1 = $this->factory->xprofile_field->create(array('type' => 'textbox', 'field_group_id' => $g1));
     $f2 = $this->factory->xprofile_field->create(array('type' => 'radio', 'field_group_id' => $g2));
     $time = bp_core_current_time();
     // Get the fullname field - hackish
     $f0_id = xprofile_get_field_id_from_name(bp_xprofile_fullname_field_name());
     $f0 = new BP_XProfile_Field($f0_id);
     $g0 = new BP_XProfile_Group($f0->group_id);
     $d0 = new BP_XProfile_ProfileData($f0->id, $u);
     $d1 = new BP_XProfile_ProfileData();
     $d1->user_id = $u;
     $d1->field_id = $f1;
     $d1->value = 'foo';
     $d1->last_updated = $time;
     $d1->save();
     $d2 = new BP_XProfile_ProfileData();
     $d2->user_id = $u;
     $d2->field_id = $f2;
     $d2->value = 'bar';
     $d2->last_updated = $time;
     $d2->save();
     // Ensure it's deleted from cache
     wp_cache_delete("{$u}:{$f1}", 'bp_xprofile_data');
     wp_cache_delete("{$u}:{$f2}", 'bp_xprofile_data');
     $u_obj = new WP_User($u);
     $g1_obj = new BP_XProfile_Group($g1);
     $g2_obj = new BP_XProfile_Group($g2);
     $f1_obj = new BP_XProfile_Field($f1);
     $f2_obj = new BP_XProfile_Field($f2);
     $expected = array('user_login' => $u_obj->user_login, 'user_nicename' => $u_obj->user_nicename, 'user_email' => $u_obj->user_email, $f0->name => array('field_group_id' => $g0->id, 'field_group_name' => $g0->name, 'field_id' => $f0->id, 'field_type' => $f0->type, 'field_data' => $d0->value), $f1_obj->name => array('field_group_id' => $g1, 'field_group_name' => $g1_obj->name, 'field_id' => $f1, 'field_type' => $f1_obj->type, 'field_data' => $d1->value), $f2_obj->name => array('field_group_id' => $g2, 'field_group_name' => $g2_obj->name, 'field_id' => $f2, 'field_type' => $f2_obj->type, 'field_data' => $d2->value));
     $this->assertEquals($expected, BP_XProfile_ProfileData::get_all_for_user($u));
 }
Beispiel #7
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();
}