/** * Return the field ID for the Full Name xprofile field. * * @since BuddyPress (2.0.0) * * @return int Field ID. */ function bp_xprofile_fullname_field_id() { $id = wp_cache_get('fullname_field_id', 'bp_xprofile'); if (false === $id) { global $wpdb; $bp = buddypress(); $id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name())); wp_cache_set('fullname_field_id', $id, 'bp_xprofile'); } return absint($id); }
/** * Syncs the standard built in NXTClass profile data to XProfile. * * @since 1.2.4 * @package BuddyPress Core */ function xprofile_sync_bp_profile(&$errors, $update, &$user) { global $bp; if (!empty($bp->site_options['bp-disable-profile-sync']) && (int) $bp->site_options['bp-disable-profile-sync'] || !$update || $errors->get_error_codes()) { return; } xprofile_set_field_data(bp_xprofile_fullname_field_name(), $user->ID, $user->display_name); }
function sort_by_name($user_ids) { global $nxtdb, $bp; if (!bp_is_active('xprofile')) { return false; } return $nxtdb->get_results($nxtdb->prepare("SELECT user_id FROM {$bp->profile->table_name_data} pd, {$bp->profile->table_name_fields} pf WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} ) ORDER BY pd.value ASC", bp_xprofile_fullname_field_name())); }
/** * Saves Buddypress profile data. * * @uses WP_CRM_Core::wp_crm_save_user_data() * @param array $data. Request (POST,GET) * @author peshkov@UD */ static function bp_save_profile_data($data) { global $bp; if (empty($data['bp']) || empty($data['user_id'])) { return; } //* Set necessary variables */ $user_id = $data['user_id']; $user_data = $data['wp_crm']['user_data']; $data = $data['bp']; $errors = false; $posted_field_ids = array(); $is_required = array(); //* Set xprofile full name from display_name */ $display_name = WP_CRM_F::get_first_value($user_data['display_name']); if (!empty($display_name)) { $fullname_field_name = bp_xprofile_fullname_field_name(); $fullname_field_id = xprofile_get_field_id_from_name($fullname_field_name); $data["field_{$fullname_field_id}"] = $display_name; } //* Get all posted field ids */ foreach ($data as $name => $value) { $field_id = str_replace(array('field_', '_day', '_month', '_year'), '', $name); array_push($posted_field_ids, $field_id); } $posted_field_ids = array_unique($posted_field_ids); //* Validate the field */ foreach ($posted_field_ids as $field_id) { if (!isset($data['field_' . $field_id])) { if (!empty($data['field_' . $field_id . '_day']) && !empty($data['field_' . $field_id . '_month']) && !empty($data['field_' . $field_id . '_year'])) { /* Concatenate the values */ $date_value = $data['field_' . $field_id . '_day'] . ' ' . $data['field_' . $field_id . '_month'] . ' ' . $data['field_' . $field_id . '_year']; /* Turn the concatenated value into a timestamp */ $data['field_' . $field_id] = date('Y-m-d H:i:s', strtotime($date_value)); } } $is_required[$field_id] = xprofile_check_is_required_field($field_id); if ($is_required[$field_id] && empty($data['field_' . $field_id])) { $errors = true; } } //** There are errors */ if ($errors) { WP_CRM_F::add_message(__('Please make sure you fill in all required Buddypress fields in this profile field group before saving.', ud_get_wp_crm()->domain), 'bad'); //** No errors */ } else { //** Now we've checked for required fields, lets save the values. */ foreach ($posted_field_ids as $field_id) { //** Certain types of fields (checkboxes, multiselects) may come through empty. */ //** Save them as an empty array so that they don't get overwritten by the default on the next edit. */ if (empty($data['field_' . $field_id])) { $value = array(); } else { $value = $data['field_' . $field_id]; } if (!xprofile_set_field_data($field_id, $user_id, $value, $is_required[$field_id])) { $errors = true; } else { do_action('xprofile_profile_field_data_updated', $field_id, $value); } } //** Set the feedback message if we have error */ if ($errors) { WP_CRM_F::add_message(__('There was a problem updating some of Buddypress profile information, please try again.', ud_get_wp_crm()->domain), 'bad'); } } }
/** * Fetch extra user information, such as friend count and last profile update message. * * Accepts multiple user IDs to fetch data for. * * @global wpdb $wpdb WordPress database object. * * @param array $paged_users An array of stdClass containing the users. * @param string $user_ids The user ids to select information about. * @param string $type The type of fields we wish to get. * @return mixed False on error, otherwise associative array of results. */ public static function get_user_extras(&$paged_users, &$user_ids, $type = false) { global $wpdb; $bp = buddypress(); if (empty($user_ids)) { return $paged_users; } // Sanitize user IDs $user_ids = implode(',', wp_parse_id_list($user_ids)); // Fetch the user's full name if (bp_is_active('xprofile') && 'alphabetical' != $type) { $names = $wpdb->get_results($wpdb->prepare("SELECT pd.user_id as id, pd.value as fullname FROM {$bp->profile->table_name_fields} pf, {$bp->profile->table_name_data} pd WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} )", bp_xprofile_fullname_field_name())); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $names as $name) { if ($name->id == $paged_users[$i]->id) { $paged_users[$i]->fullname = $name->fullname; } } } } // Fetch the user's total friend count if ('popular' != $type) { $friend_count = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as total_friend_count FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('total_friend_count'))); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $friend_count as $fcount) { if ($fcount->id == $paged_users[$i]->id) { $paged_users[$i]->total_friend_count = (int) $fcount->total_friend_count; } } } } // Fetch whether or not the user is a friend if (bp_is_active('friends')) { $friend_status = $wpdb->get_results($wpdb->prepare("SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id())); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $friend_status as $status) { if ($status->initiator_user_id == $paged_users[$i]->id || $status->friend_user_id == $paged_users[$i]->id) { $paged_users[$i]->is_friend = $status->is_confirmed; } } } } if ('active' != $type) { $user_activity = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('last_activity'))); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $user_activity as $activity) { if ($activity->id == $paged_users[$i]->id) { $paged_users[$i]->last_activity = $activity->last_activity; } } } } // Fetch the user's last_activity if ('active' != $type) { $user_activity = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('last_activity'))); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $user_activity as $activity) { if ($activity->id == $paged_users[$i]->id) { $paged_users[$i]->last_activity = $activity->last_activity; } } } } // Fetch the user's latest update $user_update = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as latest_update FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('bp_latest_update'))); for ($i = 0, $count = count($paged_users); $i < $count; ++$i) { foreach ((array) $user_update as $update) { if ($update->id == $paged_users[$i]->id) { $paged_users[$i]->latest_update = $update->latest_update; } } } return $paged_users; }
/** * Filter BP_User_Query::populate_extras to override each queries users fullname * * @since BuddyPress (1.7) * * @global BuddyPress $bp * @global WPDB $wpdb * @param BP_User_Query $user_query * @param string $user_ids_sql */ function bp_xprofile_filter_user_query_populate_extras(BP_User_Query $user_query, $user_ids_sql) { global $bp, $wpdb; if (bp_is_active('xprofile')) { $fullname_field_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name())); $user_id_names = $wpdb->get_results($wpdb->prepare("SELECT user_id, value as fullname FROM {$bp->profile->table_name_data} WHERE user_id IN ({$user_ids_sql}) AND field_id = %d", $fullname_field_id)); // Loop through names and override each user's fullname foreach ($user_id_names as $user) { if (isset($user_query->results[$user->user_id])) { $user_query->results[$user->user_id]->fullname = $user->fullname; } } } }
function get_fullname($user_id = 0) { if (empty($user_id)) { $user_id = bp_displayed_user_id(); } $field_name = bp_xprofile_fullname_field_name(); $data = xprofile_get_field_data($field_name, $user_id); return $data[$field_name]; }
/** * @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)); }
function get_fullname($user_id = 0) { global $bp; if (!$user_id) { $user_id = $bp->displayed_user->id; } $field_name = bp_xprofile_fullname_field_name(); $data = xprofile_get_field_data($field_name, $user_id); return $data[$field_name]; }
/** * Sort a list of user IDs by their display names. * * @todo Why does this exist, and why is it in bp-friends? * * @param array $user_ids Array of user IDs. * * @return array User IDs, sorted by the associated display names. */ public static function sort_by_name($user_ids) { global $wpdb; if (!bp_is_active('xprofile')) { return false; } $bp = buddypress(); $user_ids = implode(',', wp_parse_id_list($user_ids)); return $wpdb->get_results($wpdb->prepare("SELECT user_id FROM {$bp->profile->table_name_data} pd, {$bp->profile->table_name_fields} pf WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} ) ORDER BY pd.value ASC", bp_xprofile_fullname_field_name())); }