/** * 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); } }
/** * profile_screen_admin() * * Filters option for 'Teacher', only admins are allowed to access it. */ function profile_screen_admin($options) { for ($i = 0; $i < count($options); $i++) { if (!is_super_admin() && $options[$i]->name == __('Teacher', 'bpsp') && BP_XProfile_ProfileData::get_value_byid($options[$i]->parent_id) != __('Teacher', 'bpsp')) { unset($options[$i]); } } return array_merge($options); }
/** * @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')); }
/** * 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); } }
function the_invite() { global $group_id; $this->in_the_loop = true; $user_id = $this->next_invite(); $this->invite = new stdClass(); $this->invite->user = $this->invite_data[$user_id]; // This method previously populated the user object with // BP_Core_User. We manually configure BP_Core_User data for // backward compatibility. if (bp_is_active('xprofile')) { $this->invite->user->profile_data = BP_XProfile_ProfileData::get_all_for_user($user_id); } $this->invite->user->avatar = bp_core_fetch_avatar(array('item_id' => $user_id, 'type' => 'full', 'alt' => sprintf(__('Avatar of %s', 'buddypress'), $this->invite->user->fullname))); $this->invite->user->avatar_thumb = bp_core_fetch_avatar(array('item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf(__('Avatar of %s', 'buddypress'), $this->invite->user->fullname))); $this->invite->user->avatar_mini = bp_core_fetch_avatar(array('item_id' => $user_id, 'type' => 'thumb', 'alt' => sprintf(__('Avatar of %s', 'buddypress'), $this->invite->user->fullname), 'width' => 30, 'height' => 30)); $this->invite->user->email = $this->invite->user->user_email; $this->invite->user->user_url = bp_core_get_user_domain($user_id, $this->invite->user->user_nicename, $this->invite->user->user_login); $this->invite->user->user_link = "<a href='{$this->invite->user->user_url}' title='{$this->invite->user->fullname}'>{$this->invite->user->fullname}</a>"; $this->invite->user->last_active = bp_core_get_last_activity($this->invite->user->last_activity, __('active %s', 'buddypress')); if (bp_is_active('groups')) { $total_groups = BP_Groups_Member::total_group_count($user_id); $this->invite->user->total_groups = sprintf(_n('%d group', '%d groups', $total_groups, 'buddypress'), $total_groups); } if (bp_is_active('friends')) { $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count($user_id); } if (bp_is_active('friends')) { $this->invite->user->total_friends = BP_Friends_Friendship::total_friend_count($user_id); } $this->invite->user->total_blogs = null; $this->invite->group_id = $group_id; // Globaled in bp_group_has_invites() if (0 == $this->current_invite) { // loop has just started do_action('loop_start'); } }
/** * 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); }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $term_selected = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $html = ''; if ($options) { $taxonomy_selected = $options[0]->name; if (!empty($_POST['field_' . $this->field_obj->id])) { $new_term_selected = (int) $_POST['field_' . $this->field_obj->id]; $term_selected = $term_selected != $new_term_selected ? $new_term_selected : $term_selected; } // Get terms of custom taxonomy selected. $terms = get_terms($taxonomy_selected, array('hide_empty' => false)); if ($terms) { foreach ($terms as $term) { $html .= sprintf('<option value="%s"%s>%s</option>', $term->term_id, $term_selected == $term->term_id ? ' selected="selected"' : '', $term->name); } } } echo apply_filters('bp_get_the_profile_field_select_custom_taxonomy', $html, $args['type'], $term_selected, $this->field_obj->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; }
/** * Populates the BP_XProfile_Group object with profile field groups, fields, * and field data * * @package BuddyPress XProfile * * @global object $wpdb WordPress DB access object. * * @param array $args { * Array of optional arguments: * @type int $profile_group_id Limit results to a single profile group. * @type int $user_id Required if you want to load a specific user's data. * Default: displayed user's ID. * @type array|string $member_type Limit fields by those restricted to a given member type, or array of * member types. If `$user_id` is provided, the value of `$member_type` * will be overridden by the member types of the provided user. The * special value of 'any' will return only those fields that are * unrestricted by member type - i.e., those applicable to any type. * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. * @type bool $hide_empty_fields True to hide fields where the user has not provided data. * Default: false. * @type bool $fetch_fields Whether to fetch each group's fields. Default: false. * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. * Default: false. * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields, * and data. Default: true. * } * @return array $groups */ public static function get($args = array()) { global $wpdb; // Parse arguments. $r = wp_parse_args($args, array('profile_group_id' => false, 'user_id' => bp_displayed_user_id(), 'member_type' => false, 'hide_empty_groups' => false, 'hide_empty_fields' => false, 'fetch_fields' => false, 'fetch_field_data' => false, 'fetch_visibility_level' => false, 'exclude_groups' => false, 'exclude_fields' => false, 'update_meta_cache' => true)); // Keep track of object IDs for cache-priming. $object_ids = array('group' => array(), 'field' => array(), 'data' => array()); // WHERE. if (!empty($r['profile_group_id'])) { $where_sql = $wpdb->prepare('WHERE g.id = %d', $r['profile_group_id']); } elseif ($r['exclude_groups']) { $exclude = join(',', wp_parse_id_list($r['exclude_groups'])); $where_sql = "WHERE g.id NOT IN ({$exclude})"; } else { $where_sql = ''; } $bp = buddypress(); // Include or exclude empty groups. if (!empty($r['hide_empty_groups'])) { $group_ids = $wpdb->get_col("SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC"); } else { $group_ids = $wpdb->get_col("SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC"); } // Get all group data. $groups = self::get_group_data($group_ids); // Bail if not also getting fields. if (empty($r['fetch_fields'])) { return $groups; } // Get the group ids from the groups we found. $group_ids = wp_list_pluck($groups, 'id'); // Store for meta cache priming. $object_ids['group'] = $group_ids; // Bail if no groups found. if (empty($group_ids)) { return $groups; } // Setup IN query from group IDs. $group_ids_in = implode(',', (array) $group_ids); // Support arrays and comma-separated strings. $exclude_fields_cs = wp_parse_id_list($r['exclude_fields']); // Visibility - Handled here so as not to be overridden by sloppy use of the // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user(). $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user($r['user_id']); $exclude_fields_cs = array_merge($exclude_fields_cs, $hidden_user_fields); $exclude_fields_cs = implode(',', $exclude_fields_cs); // Set up NOT IN query for excluded field IDs. if (!empty($exclude_fields_cs)) { $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})"; } else { $exclude_fields_sql = ''; } // Set up IN query for included field IDs. $include_field_ids = array(); // Member-type restrictions. if (bp_get_member_types()) { if ($r['user_id'] || false !== $r['member_type']) { $member_types = $r['member_type']; if ($r['user_id']) { $member_types = bp_get_member_type($r['user_id'], false); if (empty($member_types)) { $member_types = array('null'); } } $member_types_fields = BP_XProfile_Field::get_fields_for_member_type($member_types); $include_field_ids += array_keys($member_types_fields); } } $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; $fields[$field_key]->data->id = $data->id; } // Store for meta cache priming. $object_ids['data'][] = $data->id; } } } } // Prime the meta cache, if necessary. if (!empty($r['update_meta_cache'])) { bp_xprofile_update_meta_cache($object_ids); } // Maybe fetch visibility levels. if (!empty($r['fetch_visibility_level'])) { $fields = self::fetch_visibility_level($r['user_id'], $fields); } // Merge the field array back in with the group array. foreach ((array) $groups as $group) { // Indexes may have been shifted after previous deletions, so we get a // fresh one each time through the loop. $index = array_search($group, $groups); foreach ((array) $fields as $field) { if ($group->id === $field->group_id) { $groups[$index]->fields[] = $field; } } // When we unset fields above, we may have created empty groups. // Remove them, if necessary. if (empty($group->fields) && !empty($r['hide_empty_groups'])) { unset($groups[$index]); } // Reset indexes. $groups = array_values($groups); } return $groups; }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $checkbox_acceptance = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); if (!empty($_POST['field_' . $this->field_obj->id])) { $new_checkbox_acceptance = $_POST['field_' . $this->field_obj->id]; $checkbox_acceptance = $checkbox_acceptance != $new_checkbox_acceptance ? $new_checkbox_acceptance : $checkbox_acceptance; } $html = '<input type="checkbox" name="check_acc_' . bp_get_the_profile_field_input_name() . '" id="check_acc_' . bp_get_the_profile_field_input_name() . '"'; if ($checkbox_acceptance == 1) { $html .= ' checked="checked"'; } if (isset($args['required']) && $args['required']) { $html .= ' required="required" aria-required="true"'; } $html .= ' value="1" /> '; $html .= '<input type="hidden" name="' . bp_get_the_profile_field_input_name() . '" id="' . bp_get_the_profile_field_input_name() . '"'; if ($checkbox_acceptance == 1) { $html .= ' value="1" /> '; } else { $html .= ' value="0" /> '; } if ($options) { foreach ($options as $option) { $html .= rawurldecode($option->name); } } // Javascript. $html .= ' <script> jQuery(document).ready(function() { jQuery("#check_acc_' . bp_get_the_profile_field_input_name() . '").click(function() { if (jQuery(this).is(":checked")) { jQuery("#' . bp_get_the_profile_field_input_name() . '").val("1"); } else { jQuery("#' . bp_get_the_profile_field_input_name() . '").val("0"); } }); }); </script> '; echo apply_filters('bp_get_the_profile_field_checkbox_acceptance', $html, $args['type'], $this->field_obj->id, $checkbox_acceptance); }
public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $posts_selected = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $html = ''; if ($options) { $post_type_selected = $options[0]->name; if (!empty($_POST['field_' . $this->field_obj->id])) { $new_posts_selected = $_POST['field_' . $this->field_obj->id]; $posts_selected = $posts_selected != $new_posts_selected ? $new_posts_selected : $posts_selected; } // Get posts of custom post type selected. $posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => $post_type_selected, 'orderby' => 'title', 'order' => 'ASC')); if ($posts) { foreach ($posts->posts as $post) { $html .= sprintf('<option value="%s"%s>%s</option>', $post->ID, !empty($posts_selected) && in_array($post->ID, $posts_selected) ? 'selected="selected"' : '', $post->post_title); } } } echo apply_filters('bp_get_the_profile_field_multiselect_custom_post_type', $html, $args['type'], $post_type_selected, $this->field_obj->id); }
function bp_get_the_profile_field_options( $args = '' ) { global $field; $defaults = array( 'type' => false ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( !method_exists( $field, 'get_children' ) ) $field = new BP_XProfile_Field( $field->id ); $options = $field->get_children(); switch ( $field->type ) { case 'selectbox': case 'multiselectbox': if ( 'multiselectbox' != $field->type ) $html .= '<option value="">--------</option>'; for ( $k = 0; $k < count($options); $k++ ) { $option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) ); $option_values = (array)$option_values; /* Check for updated posted values, but errors preventing them from being saved first time */ foreach( (array)$option_values as $i => $option_value ) { if ( isset( $_POST['field_' . $field->id] ) && $_POST['field_' . $field->id] != $option_value ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_values[$i] = $_POST['field_' . $field->id]; } } if ( in_array( $options[$k]->name, (array)$option_values ) || $options[$k]->is_default_option ) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '">' . stripslashes( esc_attr( $options[$k]->name ) ) . '</option>', $options[$k] ); } break; case 'radio': $html = '<div id="field_' . $field->id . '">'; for ( $k = 0; $k < count($options); $k++ ) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); /* Check for updated posted values, but errors preventing them from being saved first time */ if ( isset( $_POST['field_' . $field->id] ) && $option_value != $_POST['field_' . $field->id] ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_value = $_POST['field_' . $field->id]; } if ( $option_value == $options[$k]->name || $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) ) { $selected = ' checked="checked"'; } else { $selected = ''; } $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] ); } $html .= '</div>'; break; case 'checkbox': $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); /* Check for updated posted values, but errors preventing them from being saved first time */ if ( isset( $_POST['field_' . $field->id] ) && $option_values != maybe_serialize( $_POST['field_' . $field->id] ) ) { if ( !empty( $_POST['field_' . $field->id] ) ) $option_values = $_POST['field_' . $field->id]; } $option_values = maybe_unserialize($option_values); for ( $k = 0; $k < count($options); $k++ ) { for ( $j = 0; $j < count($option_values); $j++ ) { if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) || $options[$k]->is_default_option ) { $selected = ' checked="checked"'; break; } } $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] ); $selected = ''; } break; case 'datebox': if ( !empty( $field->data->value ) ) { $day = date("j", $field->data->value); $month = date("F", $field->data->value); $year = date("Y", $field->data->value); $default_select = ' selected="selected"'; } /* Check for updated posted values, but errors preventing them from being saved first time */ if ( !empty( $_POST['field_' . $field->id . '_day'] ) ) { if ( $day != $_POST['field_' . $field->id . '_day'] ) $day = $_POST['field_' . $field->id . '_day']; } if ( !empty( $_POST['field_' . $field->id . '_month'] ) ) { if ( $month != $_POST['field_' . $field->id . '_month'] ) $month = $_POST['field_' . $field->id . '_month']; } if ( !empty( $_POST['field_' . $field->id . '_year'] ) ) { if ( $year != date( "j", $_POST['field_' . $field->id . '_year'] ) ) $year = $_POST['field_' . $field->id . '_year']; } switch ( $type ) { case 'day': $html .= '<option value=""' . esc_attr( $default_select ) . '>--</option>'; for ( $i = 1; $i < 32; $i++ ) { if ( $day == $i ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; } break; case 'month': $eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); $months = array( __( 'January', 'buddypress' ), __( 'February', 'buddypress' ), __( 'March', 'buddypress' ), __( 'April', 'buddypress' ), __( 'May', 'buddypress' ), __( 'June', 'buddypress' ), __( 'July', 'buddypress' ), __( 'August', 'buddypress' ), __( 'September', 'buddypress' ), __( 'October', 'buddypress' ), __( 'November', 'buddypress' ), __( 'December', 'buddypress' ) ); $html .= '<option value=""' . esc_attr( $default_select ) . '>------</option>'; for ( $i = 0; $i < 12; $i++ ) { if ( $month == $eng_months[$i] ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $eng_months[$i] . '"' . $selected . '>' . $months[$i] . '</option>'; } break; case 'year': $html .= '<option value=""' . esc_attr( $default_select ) . '>----</option>'; for ( $i = date( 'Y', time() ); $i > 1899; $i-- ) { if ( $year == $i ) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; } break; } apply_filters( 'bp_get_the_profile_field_datebox', $html, $day, $month, $year, $default_select ); break; } return $html; }
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>'; }
/** * @group get_all_for_user */ public function test_get_all_for_user_cached() { $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(); $g0 = new BP_XProfile_Group(1); $f0 = new BP_XProfile_Field(1); $d0 = new BP_XProfile_ProfileData(1, $u); $d1 = new stdClass(); $d1->user_id = $u; $d1->field_id = $f1; $d1->value = 'foo'; $d1->last_updated = $time; $d1->id = 1; $d2 = new stdClass(); $d2->user_id = $u; $d2->field_id = $f2; $d2->value = 'bar'; $d2->last_updated = $time; $d2->id = 2; wp_cache_set("{$u}:{$f1}", $d1, 'bp_xprofile_data'); wp_cache_set("{$u}:{$f2}", $d2, '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)); }
/** * 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. * * @since 2.0.0 * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. */ public function edit_field_options_html(array $args = array()) { $date = BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id']); $day = 0; $month = 0; $year = 0; $html = ''; $eng_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Set day, month, year defaults. if (!empty($date)) { // If Unix timestamp. if (is_numeric($date)) { $day = date('j', $date); $month = date('F', $date); $year = date('Y', $date); // If MySQL timestamp. } else { $day = mysql2date('j', $date); $month = mysql2date('F', $date, false); // Not localized, so that selected() works below. $year = mysql2date('Y', $date); } } // Check for updated posted values, and errors preventing them from // being saved first time. if (!empty($_POST['field_' . $this->field_obj->id . '_day'])) { $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day']; $day = $day != $new_day ? $new_day : $day; } if (!empty($_POST['field_' . $this->field_obj->id . '_month'])) { if (in_array($_POST['field_' . $this->field_obj->id . '_month'], $eng_months)) { $new_month = $_POST['field_' . $this->field_obj->id . '_month']; } else { $new_month = $month; } $month = $month !== $new_month ? $new_month : $month; } if (!empty($_POST['field_' . $this->field_obj->id . '_year'])) { $new_year = (int) $_POST['field_' . $this->field_obj->id . '_year']; $year = $year != $new_year ? $new_year : $year; } // $type will be passed by calling function when needed. switch ($args['type']) { case 'day': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($day, 0, false), __('----', 'buddypress')); for ($i = 1; $i < 32; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($day, $i, false), (int) $i); } break; case 'month': $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html = sprintf('<option value="" %1$s>%2$s</option>', selected($month, 0, false), __('----', 'buddypress')); for ($i = 0; $i < 12; ++$i) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', esc_attr($eng_months[$i]), selected($month, $eng_months[$i], false), $months[$i]); } break; case 'year': $html = sprintf('<option value="" %1$s>%2$s</option>', selected($year, 0, false), __('----', 'buddypress')); for ($i = 2037; $i > 1901; $i--) { $html .= sprintf('<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected($year, $i, false), (int) $i); } break; } /** * Filters the output for the profile field datebox. * * @since 1.1.0 * * @param string $html HTML output for the field. * @param string $value Which date type is being rendered for. * @param string $day Date formatted for the current day. * @param string $month Date formatted for the current month. * @param string $year Date formatted for the current year. * @param int $id ID of the field object being rendered. * @param string $date Current date. */ echo apply_filters('bp_get_the_profile_field_datebox', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date); }
function get_edit_html($value = null) { global $bp; $asterisk = ''; if ($this->is_required) { $asterisk = '* '; } $error_class = ''; if ($this->message) { $this->message = '<p class="' . $this->message_type . '">' . $this->message . '</p>'; $message_class = ' class="' . $this->message_type . '"'; } if (!is_null($value)) { $this->data->value = $value; } $this->data->value = stripslashes(wp_filter_kses($this->data->value)); switch ($this->type) { case 'textbox': $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<input type="text" name="field_' . $this->id . '" id="field_' . $this->id . '" value="' . attribute_escape($this->data->value) . '" />'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'textarea': $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<textarea rows="5" cols="40" name="field_' . $this->id . '" id="field_' . $this->id . '">' . htmlspecialchars($this->data->value) . '</textarea>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'selectbox': $options = $this->get_children(); $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<select name="field_' . $this->id . '" id="field_' . $this->id . '">'; $html .= '<option value="">--------</option>'; for ($k = 0; $k < count($options); $k++) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); if ($option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= '<option' . $selected . ' value="' . attribute_escape($options[$k]->name) . '">' . $options[$k]->name . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'multiselectbox': $options = $this->get_children(); $html .= '<div class="signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . '<select class="multi-select" multiple="multiple" name="field_' . $this->id . '[]" id="field_' . $this->id . '">'; if ($value) { $option_values = maybe_unserialize($value); } else { $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); $option_values = maybe_unserialize($option_values); } for ($k = 0; $k < count($options); $k++) { if (@in_array($options[$k]->name, $option_values)) { $selected = ' selected="selected"'; } else { $selected = ''; } $html .= '<option' . $selected . ' value="' . attribute_escape($options[$k]->name) . '">' . $options[$k]->name . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; case 'radio': $options = $this->get_children(); $html .= '<div class="radio signup-field" id="field_' . $this->id . '"><span class="signup-label">' . $asterisk . $this->name . ':</span>' . $this->message; for ($k = 0; $k < count($options); $k++) { $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); if ($option_value == $options[$k]->name || $value == $options[$k]->name || $options[$k]->is_default_option) { $selected = ' checked="checked"'; } else { $selected = ''; } $html .= '<label><input' . $selected . ' type="radio" name="field_' . $this->id . '" id="option_' . $options[$k]->id . '" value="' . attribute_escape($options[$k]->name) . '"> ' . $options[$k]->name . '</label>'; } if (!$this->is_required) { $html .= '<a class="clear-value" style="text-decoration: none;" href="javascript:clear(\'field_' . $this->id . '\');"><img src="' . $bp->profile->image_base . '/cross.gif" alt="' . __('Clear', 'buddypress') . '" /> ' . __('Clear', 'buddypress') . '</a>'; } $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '<div class="clear"></div></div>'; break; case 'checkbox': $options = $this->get_children(); $html .= '<div class="checkbox signup-field" id="field_' . $this->id . '"><span class="signup-label">' . $asterisk . $this->name . ':</span>' . $this->message; if ($value) { $option_values = maybe_unserialize($value); } else { $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); $option_values = maybe_unserialize($option_values); } for ($k = 0; $k < count($options); $k++) { for ($j = 0; $j < count($option_values); $j++) { if ($option_values[$j] == $options[$k]->name || @in_array($options[$k]->name, $value) || $options[$k]->is_default_option) { $selected = ' checked="checked"'; break; } } $html .= '<label><input' . $selected . ' type="checkbox" name="field_' . $this->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . attribute_escape($options[$k]->name) . '"> ' . $options[$k]->name . '</label>'; $selected = ''; } $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '<div class="clear"></div></div>'; break; case 'datebox': if ($this->data->value != '') { $day = date("j", $this->data->value); $month = date("F", $this->data->value); $year = date("Y", $this->data->value); $default_select = ' selected="selected"'; } $html .= '<div id="field_' . $this->id . '" class="datefield signup-field">'; $html .= '<label class="signup-label" for="field_' . $this->id . '_day">' . $asterisk . $this->name . ':</label>'; $html .= $this->message . ' <select name="field_' . $this->id . '_day" id="field_' . $this->id . '_day">'; $html .= '<option value=""' . attribute_escape($default_select) . '>--</option>'; for ($i = 1; $i < 32; $i++) { if ($day == $i) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>'; } $html .= '</select>'; $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html .= ' <select name="field_' . $this->id . '_month" id="field_' . $this->id . '_month">'; $html .= '<option value=""' . attribute_escape($default_select) . '>------</option>'; for ($i = 0; $i < 12; $i++) { if ($month == $months[$i]) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $months[$i] . '"' . $selected . '>' . $months[$i] . '</option>'; } $html .= '</select>'; $html .= ' <select name="field_' . $this->id . '_year" id="field_' . $this->id . '_year">'; $html .= '<option value=""' . attribute_escape($default_select) . '>----</option>'; for ($i = date('Y', time()); $i > 1899; $i--) { if ($year == $i) { $selected = ' selected = "selected"'; } else { $selected = ''; } $html .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>'; } $html .= '</select>'; $html .= '<span class="signup-description">' . $this->desc . '</span>'; $html .= '</div>'; break; } return $html; }
/** * 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. * * @since 2.0.0 * * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}. */ public function edit_field_options_html(array $args = array()) { $options = $this->field_obj->get_children(); $option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($this->field_obj->id, $args['user_id'])); $option_values = $option_values ? (array) $option_values : array(); $html = ''; // Check for updated posted values, but errors preventing them from // being saved first time. if (isset($_POST['field_' . $this->field_obj->id]) && $option_values != maybe_serialize($_POST['field_' . $this->field_obj->id])) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values = array_map('sanitize_text_field', $_POST['field_' . $this->field_obj->id]); } } for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // First, check to see whether the user's saved values match the option. for ($j = 0, $count_values = count($option_values); $j < $count_values; ++$j) { // 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); if ($option_values[$j] === $allowed_options || in_array($allowed_options, $option_values)) { $selected = ' checked="checked"'; break; } } // If the user has not yet supplied a value for this field, check to // see whether there is a default value available. if (empty($option_values) && empty($selected) && !empty($options[$k]->is_default_option)) { $selected = ' checked="checked"'; } $new_html = sprintf('<label for="%3$s"><input %1$s type="checkbox" name="%2$s" id="%3$s" value="%4$s">%5$s</label>', $selected, esc_attr("field_{$this->field_obj->id}[]"), esc_attr("field_{$options[$k]->id}_{$k}"), esc_attr(stripslashes($options[$k]->name)), esc_html(stripslashes($options[$k]->name))); /** * Filters the HTML output for an individual field options checkbox. * * @since 1.1.0 * * @param string $new_html Label and checkbox 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_checkbox', $new_html, $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
function delete() { global $wpdb, $bp; if ( !$this->id || // Prevent deletion by url when can_delete is false. !$this->can_delete || // Prevent deletion of option 1 since this invalidates fields with options. ( $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; }
/** * 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 $user_id The ID of the deleted user * @uses get_user_meta() Get a user meta value based on meta key from nxt_usermeta * @uses delete_user_meta() Delete user meta value based on meta key from nxt_usermeta * @uses delete_data_for_user() Removes all profile data from the xprofile tables for the user */ function xprofile_remove_data($user_id) { BP_XProfile_ProfileData::delete_data_for_user($user_id); // delete any avatar files. @unlink(get_user_meta($user_id, 'bp_core_avatar_v1_path', true)); @unlink(get_user_meta($user_id, 'bp_core_avatar_v2_path', true)); // unset the usermeta for avatars from the usermeta table. delete_user_meta($user_id, 'bp_core_avatar_v1'); delete_user_meta($user_id, 'bp_core_avatar_v1_path'); delete_user_meta($user_id, 'bp_core_avatar_v2'); delete_user_meta($user_id, 'bp_core_avatar_v2_path'); }
function delete($delete_data = false) { global $wpdb, $bp; // Prevent deletion if no ID is present // Prevent deletion by url when can_delete is false. // Prevent deletion of option 1 since this invalidates fields with options. if (empty($this->id) || empty($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 if (true === $delete_data) { BP_XProfile_ProfileData::delete_for_field($this->id); } return true; }
/** * Fetch the display name for a group of users. * * Uses the 'Name' field in xprofile if available. Falls back on WP * display_name, and then user_nicename. * * @since 2.0.0 * * @param array $user_ids Array of user IDs to get display names for. * @return array */ function bp_core_get_user_displaynames($user_ids) { // Sanitize. $user_ids = wp_parse_id_list($user_ids); // Remove dupes and empties. $user_ids = array_unique(array_filter($user_ids)); if (empty($user_ids)) { return array(); } $uncached_ids = array(); foreach ($user_ids as $user_id) { if (false === wp_cache_get('bp_user_fullname_' . $user_id, 'bp')) { $uncached_ids[] = $user_id; } } // Prime caches. if (!empty($uncached_ids)) { if (bp_is_active('xprofile')) { $fullname_data = BP_XProfile_ProfileData::get_value_byid(1, $uncached_ids); // Key by user_id. $fullnames = array(); foreach ($fullname_data as $fd) { if (!empty($fd->value)) { $fullnames[intval($fd->user_id)] = $fd->value; } } // If xprofiledata is not found for any users, we'll look // them up separately. $no_xprofile_ids = array_diff($uncached_ids, array_keys($fullnames)); } else { $fullnames = array(); $no_xprofile_ids = $user_ids; } if (!empty($no_xprofile_ids)) { // Use WP_User_Query because we don't need BP information. $query = new WP_User_Query(array('include' => $no_xprofile_ids, 'fields' => array('ID', 'user_nicename', 'display_name'), 'count_total' => false, 'blog_id' => 0)); foreach ($query->results as $qr) { $fullnames[$qr->ID] = !empty($qr->display_name) ? $qr->display_name : $qr->user_nicename; // If xprofile is active, set this value as the // xprofile display name as well. if (bp_is_active('xprofile')) { xprofile_set_field_data(1, $qr->ID, $fullnames[$qr->ID]); } } } foreach ($fullnames as $fuser_id => $fname) { wp_cache_set('bp_user_fullname_' . $fuser_id, $fname, 'bp'); } } $retval = array(); foreach ($user_ids as $user_id) { $retval[$user_id] = wp_cache_get('bp_user_fullname_' . $user_id, 'bp'); } return $retval; }
/** * Ensures that BP data appears in comments array * * This filter loops through the comments return by a normal WordPress request * and swaps out user data with BP xprofile data, where available * * @param array $comments * @param int $post_id * @return array $comments */ function xprofile_filter_comments($comments, $post_id) { // Locate comment authors with WP accounts foreach ((array) $comments as $comment) { if ($comment->user_id) { $user_ids[] = $comment->user_id; } } // If none are found, just return the comments array if (empty($user_ids)) { return $comments; } // Pull up the xprofile fullname of each commenter if ($fullnames = BP_XProfile_ProfileData::get_value_byid(1, $user_ids)) { foreach ((array) $fullnames as $user) { $users[$user->user_id] = trim(stripslashes($user->value)); } } // Loop through and match xprofile fullname with commenters foreach ((array) $comments as $i => $comment) { if (!empty($comment->user_id)) { if (!empty($users[$comment->user_id])) { $comments[$i]->comment_author = $users[$comment->user_id]; } } } return $comments; }
/** * 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 seperately. * * 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 BuddyPress (2.0.0) */ 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'])); $options = $this->field_obj->get_children(); $html = '<option value="">' . esc_html__('----', 'buddypress') . '</option>'; if (empty($original_option_values) && !empty($_POST['field_' . $this->field_obj->id])) { $original_option_values = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $this->field_obj->id]) && $_POST['field_' . $this->field_obj->id] != $option_value) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values[$i] = 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); // First, check to see whether the user-entered value matches if (in_array($allowed_options, $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_html(stripslashes($options[$k]->name)) . '</option>', $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
function xprofile_filter_comments($comments, $post_id) { foreach ((array) $comments as $comment) { if ($comment->user_id) { $user_ids[] = $comment->user_id; } } if (empty($user_ids)) { return $comments; } if ($fullnames = BP_XProfile_ProfileData::get_value_byid(1, $user_ids)) { foreach ((array) $fullnames as $user) { $users[$user->user_id] = trim($user->value); } } foreach ((array) $comments as $i => $comment) { if (!empty($comment->user_id)) { if (!empty($users[$comment->user_id])) { $comments[$i]->comment_author = $users[$comment->user_id]; } } } return $comments; }
/** * 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. * * @since 2.0.0 * * @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'])); $options = $this->field_obj->get_children(); $html = ''; if (empty($original_option_values) && !empty($_POST['field_' . $this->field_obj->id])) { $original_option_values = sanitize_text_field($_POST['field_' . $this->field_obj->id]); } $option_values = $original_option_values ? (array) $original_option_values : array(); for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // Check for updated posted values, but errors preventing them from // being saved first time. foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $this->field_obj->id]) && $_POST['field_' . $this->field_obj->id][$i] != $option_value) { if (!empty($_POST['field_' . $this->field_obj->id][$i])) { $option_values[] = sanitize_text_field($_POST['field_' . $this->field_obj->id][$i]); } } } // 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); // First, check to see whether the user-entered value matches. if (in_array($allowed_options, $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults. if (!is_array($original_option_values) && empty($option_values) && !empty($options[$k]->is_default_option)) { $selected = ' selected="selected"'; } /** * Filters the HTML output for options in a multiselect input. * * @since 1.5.0 * * @param string $value Option tag for current value being rendered. * @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_multiselect', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_html(stripslashes($options[$k]->name)) . '</option>', $options[$k], $this->field_obj->id, $selected, $k); } echo $html; }
/** * bp_get_the_profile_field_options() * * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox', * 'radio', 'checkbox', and 'datebox'. * * @package BuddyPress Xprofile * @since 1.1 * * @uses BP_XProfile_Field::get_children() * @uses BP_XProfile_ProfileData::get_value_byid() * * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'. */ function bp_get_the_profile_field_options($args = '') { global $field; $defaults = array('type' => false); $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); if (!method_exists($field, 'get_children')) { $field = new BP_XProfile_Field($field->id); } $options = $field->get_children(); // Setup some defaults $html = ''; $selected = ''; switch ($field->type) { case 'selectbox': if (!$field->is_required) { $html .= '<option value="">' . __('----', 'buddypress') . '</option>'; } $original_option_values = ''; $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field->id)); if (empty($original_option_values) && !empty($_POST['field_' . $field->id])) { $original_option_values = $_POST['field_' . $field->id]; } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $field->id]) && $_POST['field_' . $field->id] != $option_value) { if (!empty($_POST['field_' . $field->id])) { $option_values[$i] = $_POST['field_' . $field->id]; } } } $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($options[$k]->name, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, (array) $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_attr(stripslashes($options[$k]->name)) . '</option>', $options[$k], $field->id, $selected, $k); } break; case 'multiselectbox': $original_option_values = ''; $original_option_values = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid($field->id)); if (empty($original_option_values) && !empty($_POST['field_' . $field->id])) { $original_option_values = $_POST['field_' . $field->id]; } $option_values = (array) $original_option_values; for ($k = 0, $count = count($options); $k < $count; ++$k) { // Check for updated posted values, but errors preventing them from being saved first time foreach ($option_values as $i => $option_value) { if (isset($_POST['field_' . $field->id]) && $_POST['field_' . $field->id][$i] != $option_value) { if (!empty($_POST['field_' . $field->id][$i])) { $option_values[] = $_POST['field_' . $field->id][$i]; } } } $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($options[$k]->name, false, false); // First, check to see whether the user-entered value matches if (in_array($allowed_options, (array) $option_values)) { $selected = ' selected="selected"'; } // Then, if the user has not provided a value, check for defaults if (!is_array($original_option_values) && empty($option_values) && $options[$k]->is_default_option) { $selected = ' selected="selected"'; } $html .= apply_filters('bp_get_the_profile_field_options_multiselect', '<option' . $selected . ' value="' . esc_attr(stripslashes($options[$k]->name)) . '">' . esc_attr(stripslashes($options[$k]->name)) . '</option>', $options[$k], $field->id, $selected, $k); } break; case 'radio': $html .= '<div id="field_' . $field->id . '">'; $option_value = BP_XProfile_ProfileData::get_value_byid($field->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_' . $field->id]) && $option_value != $_POST['field_' . $field->id]) { if (!empty($_POST['field_' . $field->id])) { $option_value = $_POST['field_' . $field->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($value) && $value == $allowed_options || empty($option_value) && $options[$k]->is_default_option) { $selected = ' checked="checked"'; } $html .= apply_filters('bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . esc_attr(stripslashes($options[$k]->name)) . '"> ' . esc_attr(stripslashes($options[$k]->name)) . '</label>', $options[$k], $field->id, $selected, $k); } $html .= '</div>'; break; case 'checkbox': $option_values = BP_XProfile_ProfileData::get_value_byid($field->id); $option_values = maybe_unserialize($option_values); // Check for updated posted values, but errors preventing them from being saved first time if (isset($_POST['field_' . $field->id]) && $option_values != maybe_serialize($_POST['field_' . $field->id])) { if (!empty($_POST['field_' . $field->id])) { $option_values = $_POST['field_' . $field->id]; } } for ($k = 0, $count = count($options); $k < $count; ++$k) { $selected = ''; // First, check to see whether the user's saved values // match the option for ($j = 0, $count_values = count($option_values); $j < $count_values; ++$j) { // 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); if ($option_values[$j] == $allowed_options || @in_array($allowed_options, $value)) { $selected = ' checked="checked"'; break; } } // If the user has not yet supplied a value for this field, // check to see whether there is a default value available if (!is_array($option_values) && empty($option_values) && !$selected && $options[$k]->is_default_option) { $selected = ' checked="checked"'; } $html .= apply_filters('bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . esc_attr(stripslashes($options[$k]->name)) . '"> ' . esc_attr(stripslashes($options[$k]->name)) . '</label>', $options[$k], $field->id, $selected, $k); } break; case 'datebox': $date = BP_XProfile_ProfileData::get_value_byid($field->id); // Set day, month, year defaults $day = ''; $month = ''; $year = ''; if (!empty($date)) { // If Unix timestamp if (is_numeric($date)) { $day = date('j', $date); $month = date('F', $date); $year = date('Y', $date); // If MySQL timestamp } else { $day = mysql2date('j', $date); $month = mysql2date('F', $date, false); // Not localized, so that selected() works below $year = mysql2date('Y', $date); } } // Check for updated posted values, but errors preventing them from being saved first time if (!empty($_POST['field_' . $field->id . '_day'])) { if ($day != $_POST['field_' . $field->id . '_day']) { $day = $_POST['field_' . $field->id . '_day']; } } if (!empty($_POST['field_' . $field->id . '_month'])) { if ($month != $_POST['field_' . $field->id . '_month']) { $month = $_POST['field_' . $field->id . '_month']; } } if (!empty($_POST['field_' . $field->id . '_year'])) { if ($year != date("j", $_POST['field_' . $field->id . '_year'])) { $year = $_POST['field_' . $field->id . '_year']; } } switch ($type) { case 'day': $html .= '<option value=""' . selected($day, '', false) . '>--</option>'; for ($i = 1; $i < 32; ++$i) { $html .= '<option value="' . $i . '"' . selected($day, $i, false) . '>' . $i . '</option>'; } break; case 'month': $eng_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $months = array(__('January', 'buddypress'), __('February', 'buddypress'), __('March', 'buddypress'), __('April', 'buddypress'), __('May', 'buddypress'), __('June', 'buddypress'), __('July', 'buddypress'), __('August', 'buddypress'), __('September', 'buddypress'), __('October', 'buddypress'), __('November', 'buddypress'), __('December', 'buddypress')); $html .= '<option value=""' . selected($month, '', false) . '>------</option>'; for ($i = 0; $i < 12; ++$i) { $html .= '<option value="' . $eng_months[$i] . '"' . selected($month, $eng_months[$i], false) . '>' . $months[$i] . '</option>'; } break; case 'year': $html .= '<option value=""' . selected($year, '', false) . '>----</option>'; for ($i = 2037; $i > 1901; $i--) { $html .= '<option value="' . $i . '"' . selected($year, $i, false) . '>' . $i . '</option>'; } break; } $html = apply_filters('bp_get_the_profile_field_datebox', $html, $type, $day, $month, $year, $field->id, $date); break; } return $html; }