/**
* Output the edit field options HTML for this field type.
*
* BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
* These are stored separately in the database, and their templating is handled separately.
*
* This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
* it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
*
*/
public function edit_field_options_html(array $args = array())
{
$original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']));
if (!empty($_POST['field_' . $this->field_obj->id])) {
$option_values = (array) $_POST['field_' . $this->field_obj->id];
$option_values = array_map('sanitize_text_field', $option_values);
} else {
$option_values = (array) $original_option_values;
}
//member types list as array
$options = self::get_roles();
$selected = '';
//$option_values = (array) $original_option_values;
if (empty($option_values) || in_array('none', $option_values)) {
$selected = ' selected="selected"';
}
$html = '<option value="" ' . $selected . ' >----' . '</option>';
echo $html;
foreach ($options as $role => $label) {
$selected = '';
// Run the allowed option name through the before_save filter, so we'll be sure to get a match
$allowed_options = xprofile_sanitize_data_value_before_save($role, false, false);
// First, check to see whether the user-entered value matches
if (in_array($allowed_options, (array) $option_values)) {
$selected = ' selected="selected"';
}
echo apply_filters('bp_get_the_profile_field_options_roles', '<option' . $selected . ' value="' . esc_attr(stripslashes($role)) . '">' . $label . '</option>', $role, $this->field_obj->id, $selected);
}
}
/**
* Output the edit field options HTML for this field type.
*
* BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
* These are stored separately in the database, and their templating is handled separately.
*
* This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
* it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
*
*/
public function edit_field_options_html(array $args = array())
{
$original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']));
if (!empty($_POST['field_' . $this->field_obj->id])) {
$option_values = (array) $_POST['field_' . $this->field_obj->id];
$option_values = array_map('sanitize_text_field', $option_values);
} else {
$option_values = (array) $original_option_values;
}
//member types list as array
$options = self::get_member_types();
//$option_values = (array) $original_option_values;
if ($this->display_as_radio()) {
$this->_edit_options_html_radio($option_values, $options);
} else {
$this->_edit_options_html($option_values, $options);
}
}
/**
* When a user is deleted, we need to clean up the database and remove all the
* profile data from each table. Also we need to clean anything up in the
* usermeta table that this component uses.
*
* @package BuddyPress XProfile
* @param int $user_id The ID of the deleted user
*/
function xprofile_remove_data($user_id)
{
BP_XProfile_ProfileData::delete_data_for_user($user_id);
}
/**
* Get a piece of user profile data.
*
* When used in a bp_has_members() loop, this function will attempt
* to fetch profile data cached in the template global. It is also safe
* to use outside of the loop.
*
* @param array|string $args {
* Array of config parameters.
* @type string $field Name of the profile field.
* @type int $user_id ID of the user whose data is being fetched.
* Defaults to the current member in the loop, or if not
* present, to the currently displayed user.
* }
* @return string|bool Profile data if found, otherwise false.
*/
function bp_get_member_profile_data($args = '')
{
global $members_template;
if (!bp_is_active('xprofile')) {
return false;
}
// Declare local variables.
$data = false;
// Guess at default $user_id.
$default_user_id = 0;
if (!empty($members_template->member->id)) {
$default_user_id = $members_template->member->id;
} elseif (bp_displayed_user_id()) {
$default_user_id = bp_displayed_user_id();
}
$defaults = array('field' => false, 'user_id' => $default_user_id);
$r = wp_parse_args($args, $defaults);
// If we're in a members loop, get the data from the global.
if (!empty($members_template->member->profile_data)) {
$profile_data = $members_template->member->profile_data;
}
// Otherwise query for the data.
if (empty($profile_data) && method_exists('BP_XProfile_ProfileData', 'get_all_for_user')) {
$profile_data = BP_XProfile_ProfileData::get_all_for_user($r['user_id']);
}
// If we're in the members loop, but the profile data has not
// been loaded into the global, cache it there for later use.
if (!empty($members_template->member) && empty($members_template->member->profile_data)) {
$members_template->member->profile_data = $profile_data;
}
// Get the data for the specific field requested.
if (!empty($profile_data) && !empty($profile_data[$r['field']]['field_type']) && !empty($profile_data[$r['field']]['field_data'])) {
$data = xprofile_format_profile_field($profile_data[$r['field']]['field_type'], $profile_data[$r['field']]['field_data']);
}
/**
* Filters resulting piece of member profile data.
*
* @since 1.2.0
*
* @param string|bool $data Profile data if found, otherwise false.
*/
return apply_filters('bp_get_member_profile_data', $data);
}
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());
}
/**
* Fetch xprofile data for the current user.
*
* @see BP_XProfile_ProfileData::get_all_for_user() for description of
* return value.
*
* @return array See {@link BP_XProfile_Profile_Data::get_all_for_user()}.
*/
public function get_profile_data()
{
return BP_XProfile_ProfileData::get_all_for_user($this->id);
}
function delete()
{
global $wpdb, $bp;
if (!$this->id || !$this->can_delete || $this->parent_id && $this->option_order == 1) {
return false;
}
if (!$wpdb->query($wpdb->prepare("DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id))) {
return false;
}
/* delete the data in the DB for this field */
BP_XProfile_ProfileData::delete_for_field($this->id);
return true;
}
//.........这里部分代码省略.........
$in_sql = '';
if (!empty($include_field_ids)) {
$include_field_ids_cs = implode(',', array_map('intval', $include_field_ids));
$in_sql = " AND id IN ({$include_field_ids_cs}) ";
}
// Fetch the fields.
$field_ids = $wpdb->get_col("SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order");
// Bail if no fields.
if (empty($field_ids)) {
return $groups;
}
$field_ids = array_map('intval', $field_ids);
// Prime the field cache.
$uncached_field_ids = bp_get_non_cached_ids($field_ids, 'bp_xprofile_fields');
if (!empty($uncached_field_ids)) {
$_uncached_field_ids = implode(',', array_map('intval', $uncached_field_ids));
$uncached_fields = $wpdb->get_results("SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})");
foreach ($uncached_fields as $uncached_field) {
$fid = intval($uncached_field->id);
wp_cache_set($fid, $uncached_field, 'bp_xprofile_fields');
}
}
// Pull field objects from the cache.
$fields = array();
foreach ($field_ids as $field_id) {
$fields[] = xprofile_get_field($field_id);
}
// Store field IDs for meta cache priming.
$object_ids['field'] = $field_ids;
// Maybe fetch field data.
if (!empty($r['fetch_field_data'])) {
// Get field data for user ID.
if (!empty($field_ids) && !empty($r['user_id'])) {
$field_data = BP_XProfile_ProfileData::get_data_for_user($r['user_id'], $field_ids);
}
// Remove data-less fields, if necessary.
if (!empty($r['hide_empty_fields']) && !empty($field_ids) && !empty($field_data)) {
// Loop through the results and find the fields that have data.
foreach ((array) $field_data as $data) {
// Empty fields may contain a serialized empty array.
$maybe_value = maybe_unserialize($data->value);
// Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
if ((!empty($maybe_value) || '0' == $maybe_value) && false !== ($key = array_search($data->field_id, $field_ids))) {
// Fields that have data get removed from the list.
unset($field_ids[$key]);
}
}
// The remaining members of $field_ids are empty. Remove them.
foreach ($fields as $field_key => $field) {
if (in_array($field->id, $field_ids)) {
unset($fields[$field_key]);
}
}
// Reset indexes.
$fields = array_values($fields);
}
// Field data was found.
if (!empty($fields) && !empty($field_data) && !is_wp_error($field_data)) {
// Loop through fields.
foreach ((array) $fields as $field_key => $field) {
// Loop through the data in each field.
foreach ((array) $field_data as $data) {
// Assign correct data value to the field.
if ($field->id == $data->field_id) {
$fields[$field_key]->data = new stdClass();
$fields[$field_key]->data->value = $data->value;
function bp_get_member_profile_data($args = '')
{
global $bp, $members_template;
if (!bp_is_active('xprofile')) {
return false;
}
// Declare local variables
$data = false;
$user_id = 0;
// Guess at default $user_id
if (!empty($members_template->member->id)) {
$user_id = $members_template->member->id;
} elseif (!empty($bp->displayed_user->id)) {
$user_id = $bp->displayed_user->id;
}
$defaults = array('field' => false, 'user_id' => $user_id);
$r = nxt_parse_args($args, $defaults);
extract($r, EXTR_SKIP);
// Populate the user if it hasn't been already.
if (empty($members_template->member->profile_data) && method_exists('BP_XProfile_ProfileData', 'get_all_for_user')) {
$members_template->member->profile_data = BP_XProfile_ProfileData::get_all_for_user($user_id);
}
// Get the field data if there is data to get
if (!empty($members_template->member->profile_data)) {
$data = xprofile_format_profile_field($members_template->member->profile_data[$field]['field_type'], $members_template->member->profile_data[$field]['field_data']);
}
return apply_filters('bp_get_member_profile_data', $data);
}
/**
* Output the edit field options HTML for this field type.
*
* BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
* These are stored separately in the database, and their templating is handled separately.
*
* This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
* it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
*
* Must be used inside the {@link bp_profile_fields()} template loop.
*
* @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
* @since 2.0.0
*/
public function edit_field_options_html(array $args = array())
{
$option_value = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']);
$options = $this->field_obj->get_children();
$html = sprintf('<div id="%s">', esc_attr('field_' . $this->field_obj->id));
for ($k = 0, $count = count($options); $k < $count; ++$k) {
// Check for updated posted values, but errors preventing them from
// being saved first time
if (isset($_POST['field_' . $this->field_obj->id]) && $option_value != $_POST['field_' . $this->field_obj->id]) {
if (!empty($_POST['field_' . $this->field_obj->id])) {
$option_value = sanitize_text_field($_POST['field_' . $this->field_obj->id]);
}
}
// Run the allowed option name through the before_save filter, so
// we'll be sure to get a match
$allowed_options = xprofile_sanitize_data_value_before_save($options[$k]->name, false, false);
$selected = '';
if ($option_value === $allowed_options || empty($option_value) && !empty($options[$k]->is_default_option)) {
$selected = ' checked="checked"';
}
$new_html = sprintf('<label><input %1$s type="radio" name="%2$s" id="%3$s" value="%4$s">%5$s</label>', $selected, esc_attr("field_{$this->field_obj->id}"), esc_attr("option_{$options[$k]->id}"), esc_attr(stripslashes($options[$k]->name)), esc_html(stripslashes($options[$k]->name)));
/**
* Filters the HTML output for an individual field options radio button.
*
* @since 1.1.0
*
* @param string $new_html Label and radio input field.
* @param object $value Current option being rendered for.
* @param int $id ID of the field object being rendered.
* @param string $selected Current selected value.
* @param string $k Current index in the foreach loop.
*/
$html .= apply_filters('bp_get_the_profile_field_options_radio', $new_html, $options[$k], $this->field_obj->id, $selected, $k);
}
echo $html . '</div>';
}
请发表评论