function cfbgr_migrate_xprofile_as_member_types() { global $wpdb; $buddypress = buddypress(); // Description of this tool, displayed to the user $statement = __('Migrating/Resetting xProfile data as member types: %s', 'buddypress-group-restrictions'); // Default to failure text $result = __('No xProfile data needs to be migrated or reset.', 'buddypress-group-restrictions'); // Default to unrepaired $repair = 0; $field = (int) bp_get_option('cfbgr_xfield_id', 0); if (empty($field)) { return array(0, sprintf($statement, $result)); } $member_types = bp_get_member_types(); // Walk through all users on the site $user_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->users}"); foreach ($user_ids as $user_id) { $value = sanitize_key(xprofile_get_field_data($field, $user_id)); // Do we have a matching member type ? if (isset($member_types[$value])) { // Set member types if empty or different if ($value !== bp_get_member_type($user_id)) { bp_set_member_type($user_id, $value); $repair += 1; } } } $result = sprintf(__('%d migrated or reset', 'buddypress-group-restrictions'), $repair); // All done! return array(0, sprintf($statement, $result)); }
/** * Return member type for display in the WordPress Users list table * * @param string $empty * @param string $column_name * @param int $user_id * * @return string Displayable bbPress user role */ public static function user_role_row($empty = '', $column_name, $user_id) { // Only looking for member type user role column switch ($column_name) { case 'member_type': // Get the member type $member_type = bp_get_member_type($user_id); if (isset($member_type) && !empty($member_type)) { return ucfirst($member_type); } break; default: break; } }
/** * Return member's type for display in the WP admin users list table. * * @since 2.7.0 * * @param string $retval * @param string $column_name * @param int $user_id * * @return string Member type as a link to filter all users. */ public function users_table_populate_type_cell($retval = '', $column_name = '', $user_id = 0) { // Only looking for member type column. if (bp_get_member_type_tax_name() !== $column_name) { return $retval; } // Get the member type. $type = bp_get_member_type($user_id); // Output the if ($type_obj = bp_get_member_type_object($type)) { $url = add_query_arg(array('bp-member-type' => urlencode($type))); $retval = '<a href="' . esc_url($url) . '">' . esc_html($type_obj->labels['singular_name']) . '</a>'; } return $retval; }
/** * Render the Member Type metabox. * * @since 2.2.0 * * @param WP_User|null $user The WP_User object to be edited. */ public function user_admin_member_type_metabox($user = null) { // Bail if no user ID. if (empty($user->ID)) { return; } $types = bp_get_member_types(array(), 'objects'); $current_type = bp_get_member_type($user->ID); ?> <label for="bp-members-profile-member-type" class="screen-reader-text"><?php esc_html_e('Select member type', 'buddypress'); ?> </label> <select name="bp-members-profile-member-type" id="bp-members-profile-member-type"> <option value="" <?php selected('', $current_type); ?> ><?php /* translators: no option picked in select box */ esc_attr_e('----', 'buddypress'); ?> </option> <?php foreach ($types as $type) { ?> <option value="<?php echo esc_attr($type->name); ?> " <?php selected($type->name, $current_type); ?> ><?php echo esc_html($type->labels['singular_name']); ?> </option> <?php } ?> </select> <?php wp_nonce_field('bp-member-type-change-' . $user->ID, 'bp-member-type-nonce'); }
/** * Check whether the given user has a certain member type. * * @since 2.3.0 * * @param int $user_id $user_id ID of the user. * @param string $member_type Member Type. * @return bool Whether the user has the given member type. */ function bp_has_member_type($user_id, $member_type) { // Bail if no valid member type was passed. if (empty($member_type) || !bp_get_member_type_object($member_type)) { return false; } // Get all user's member types. $types = bp_get_member_type($user_id, false); if (!is_array($types)) { return false; } return in_array($member_type, $types); }
/** * Customize the body class, according to the currently displayed BP content. * * Uses the above is_() functions to output a body class for each scenario. * * @since 1.1.0 * * @param array $wp_classes The body classes coming from WP. * @param array|bool $custom_classes Classes that were passed to get_body_class(). * @return array $classes The BP-adjusted body classes. */ function bp_get_the_body_class($wp_classes = array(), $custom_classes = false) { $bp_classes = array(); /* Pages *************************************************************/ if (is_front_page()) { $bp_classes[] = 'home-page'; } if (bp_is_directory()) { $bp_classes[] = 'directory'; } if (bp_is_single_item()) { $bp_classes[] = 'single-item'; } /* Components ********************************************************/ if (!bp_is_blog_page()) { if (bp_is_user_profile()) { $bp_classes[] = 'xprofile'; } if (bp_is_activity_component()) { $bp_classes[] = 'activity'; } if (bp_is_blogs_component()) { $bp_classes[] = 'blogs'; } if (bp_is_messages_component()) { $bp_classes[] = 'messages'; } if (bp_is_friends_component()) { $bp_classes[] = 'friends'; } if (bp_is_groups_component()) { $bp_classes[] = 'groups'; } if (bp_is_settings_component()) { $bp_classes[] = 'settings'; } } /* User **************************************************************/ if (bp_is_user()) { $bp_classes[] = 'bp-user'; // Add current user member types. if ($member_types = bp_get_member_type(bp_displayed_user_id(), false)) { foreach ($member_types as $member_type) { $bp_classes[] = sprintf('member-type-%s', esc_attr($member_type)); } } } if (!bp_is_directory()) { if (bp_is_user_blogs()) { $bp_classes[] = 'my-blogs'; } if (bp_is_user_groups()) { $bp_classes[] = 'my-groups'; } if (bp_is_user_activity()) { $bp_classes[] = 'my-activity'; } } else { if (bp_get_current_member_type()) { $bp_classes[] = 'type'; } } if (bp_is_my_profile()) { $bp_classes[] = 'my-account'; } if (bp_is_user_profile()) { $bp_classes[] = 'my-profile'; } if (bp_is_user_friends()) { $bp_classes[] = 'my-friends'; } if (bp_is_user_messages()) { $bp_classes[] = 'my-messages'; } if (bp_is_user_recent_commments()) { $bp_classes[] = 'recent-comments'; } if (bp_is_user_recent_posts()) { $bp_classes[] = 'recent-posts'; } if (bp_is_user_change_avatar()) { $bp_classes[] = 'change-avatar'; } if (bp_is_user_profile_edit()) { $bp_classes[] = 'profile-edit'; } if (bp_is_user_friends_activity()) { $bp_classes[] = 'friends-activity'; } if (bp_is_user_groups_activity()) { $bp_classes[] = 'groups-activity'; } /* Messages **********************************************************/ if (bp_is_messages_inbox()) { $bp_classes[] = 'inbox'; } if (bp_is_messages_sentbox()) { $bp_classes[] = 'sentbox'; } if (bp_is_messages_compose_screen()) { $bp_classes[] = 'compose'; } if (bp_is_notices()) { $bp_classes[] = 'notices'; } if (bp_is_user_friend_requests()) { $bp_classes[] = 'friend-requests'; } if (bp_is_create_blog()) { $bp_classes[] = 'create-blog'; } /* Groups ************************************************************/ if (bp_is_group()) { $bp_classes[] = 'group-' . groups_get_current_group()->slug; // Add current group types. if ($group_types = bp_groups_get_group_type(bp_get_current_group_id(), false)) { foreach ($group_types as $group_type) { $bp_classes[] = sprintf('group-type-%s', esc_attr($group_type)); } } } if (bp_is_group_leave()) { $bp_classes[] = 'leave-group'; } if (bp_is_group_invites()) { $bp_classes[] = 'group-invites'; } if (bp_is_group_members()) { $bp_classes[] = 'group-members'; } if (bp_is_group_forum_topic()) { $bp_classes[] = 'group-forum-topic'; } if (bp_is_group_forum_topic_edit()) { $bp_classes[] = 'group-forum-topic-edit'; } if (bp_is_group_forum()) { $bp_classes[] = 'group-forum'; } if (bp_is_group_admin_page()) { $bp_classes[] = 'group-admin'; $bp_classes[] = bp_get_group_current_admin_tab(); } if (bp_is_group_create()) { $bp_classes[] = 'group-create'; $bp_classes[] = bp_get_groups_current_create_step(); } if (bp_is_group_home()) { $bp_classes[] = 'group-home'; } if (bp_is_single_activity()) { $bp_classes[] = 'activity-permalink'; } /* Registration ******************************************************/ if (bp_is_register_page()) { $bp_classes[] = 'registration'; } if (bp_is_activation_page()) { $bp_classes[] = 'activation'; } /* Current Component & Action ****************************************/ if (!bp_is_blog_page()) { $bp_classes[] = bp_current_component(); $bp_classes[] = bp_current_action(); } /* Clean up ***********************************************************/ // Add BuddyPress class if we are within a BuddyPress page. if (!bp_is_blog_page()) { $bp_classes[] = 'buddypress'; } // Merge WP classes with BuddyPress classes and remove any duplicates. $classes = array_unique(array_merge((array) $bp_classes, (array) $wp_classes)); /** * Filters the BuddyPress classes to be added to body_class() * * @since 1.1.0 * * @param array $classes Array of body classes to add. * @param array $bp_classes Array of BuddyPress-based classes. * @param array $wp_classes Array of WordPress-based classes. * @param array $custom_classes Array of classes that were passed to get_body_class(). */ return apply_filters('bp_get_the_body_class', $classes, $bp_classes, $wp_classes, $custom_classes); }
/** * Filter the list of potential friends that can be invited to the group. * * @since 1.0.2 * * @param array|bool $friends An array of friends available to invite or false if none available. * @param int $user_id ID of the user doing the inviting. * @param int $group_id ID of the group being checked. * @return array */ function cfbgr_filter_group_invite_list($friends, $user_id, $group_id) { // Get the group restriction type. $restriction_type = groups_get_groupmeta(bp_get_current_group_id(), 'cf-buddypress-group-restrictions'); // Bail if the group is unrestricted. if (empty($restriction_type)) { return $friends; } $invites = array(); foreach ($friends as $friend) { // Get member type data. $member_type = bp_get_member_type($friend['id']); // Check if the restriction and member types match. if ($member_type === $restriction_type) { // Add this user to the invites array. $invites[] = array('id' => $friend['id'], 'full_name' => bp_core_get_user_displayname($friend['id'])); } } // If there's nobody to invite, return false. if (empty($invites)) { $invites = false; } return $invites; }
/** * Locate a custom user front template if it exists. * * @since 2.6.0 * * @param object|null $displayed_user Optional. Falls back to current user if not passed. * @return string|bool Path to front template on success; boolean false on failure. */ function bp_displayed_user_get_front_template($displayed_user = null) { if (!is_object($displayed_user) || empty($displayed_user->id)) { $displayed_user = bp_get_displayed_user(); } if (!isset($displayed_user->id)) { return false; } if (isset($displayed_user->front_template)) { return $displayed_user->front_template; } // Init the hierarchy $template_names = array('members/single/front-id-' . sanitize_file_name($displayed_user->id) . '.php', 'members/single/front-nicename-' . sanitize_file_name($displayed_user->userdata->user_nicename) . '.php'); /** * Check for member types and add it to the hierarchy * * Make sure to register your member * type using the hook 'bp_register_member_types' */ if (bp_get_member_types()) { $displayed_user_member_type = bp_get_member_type($displayed_user->id); if (!$displayed_user_member_type) { $displayed_user_member_type = 'none'; } $template_names[] = 'members/single/front-member-type-' . sanitize_file_name($displayed_user_member_type) . '.php'; } // Add The generic template to the end of the hierarchy $template_names[] = 'members/single/front.php'; /** * Filters the hierarchy of user front templates corresponding to a specific user. * * @since 2.6.0 * * @param array $template_names Array of template paths. */ return bp_locate_template(apply_filters('bp_displayed_user_get_front_template', $template_names), false, true); }
/** * @group BP6138 */ public function test_bp_has_member_type_should_return_true_on_success() { $u1 = $this->factory->user->create(); bp_register_member_type('foo'); bp_register_member_type('bar'); bp_set_member_type($u1, 'foo'); bp_set_member_type($u1, 'bar', true); $this->assertTrue(bp_has_member_type($u1, 'foo')); $types = bp_get_member_type($u1, false); $this->assertEqualSets(array('bar', 'foo'), $types); }
function kleo_bp_profile_member_type_label() { $member_type = bp_get_member_type(bp_displayed_user_id()); if (empty($member_type)) { return; } $member_type_object = bp_get_member_type_object($member_type); if ($member_type_object) { $member_type_label = '<p class="kleo_bp_profile_member_type_label">' . esc_html($member_type_object->labels['singular_name']) . '</p>'; echo apply_filters('kleo_bp_profile_member_type_label', $member_type_label); } }
/** * 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; }
/** * Output the edit field options HTML for this field type. * * Mainly of copy of BP_XProfile_Field_Type_Selectbox->edit_field_options_html * the only thing that changes is we get the value using bp_get_member_type() * and we make sure to use sanitize_key() just like it's done for member types */ public function edit_field_options_html(array $args = array()) { $original_option_values = bp_get_member_type($args['user_id']); $options = $this->field_obj->get_children(); $html = '<option value="">' . esc_html__('----', 'buddypress-group-restrictions') . '</option>'; if (empty($original_option_values) && !empty($_POST['field_' . $this->field_obj->id])) { $original_option_values = sanitize_key($_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] != $option_value) { if (!empty($_POST['field_' . $this->field_obj->id])) { $option_values[$i] = sanitize_key($_POST['field_' . $this->field_obj->id]); } } } $member_type_option = sanitize_key($options[$k]->name); // First, check to see whether the user-entered value matches if (in_array($member_type_option, $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 .= '<option' . $selected . ' value="' . esc_attr(stripslashes($member_type_option)) . '">' . esc_html(stripslashes($options[$k]->name)) . '</option>'; } echo $html; }
public function test_bp_get_member_type_should_not_return_unregistered_types() { $u1 = $this->factory->user->create(); bp_register_member_type('foo'); bp_set_member_type($u1, 'foo'); // Directly set a type that hasn't been registered. bp_set_object_terms($u1, 'ugh', bp_get_member_type_tax_name(), true); $type = bp_get_member_type($u1, false); $this->assertEquals(array('foo'), $type); }
"><?php echo bp_core_get_user_displayname(get_current_user_id()); ?> </span> <span> <?php echo bp_core_fetch_avatar(array('item_id' => get_current_user_id(), 'type' => 'full', 'width' => '100', 'height' => '100')); ?> </span> </a> <div class="pop"> <!-- Dashboard links --> <?php if (boss_get_option('boss_dashboard') && (current_user_can('level_10') || bp_get_member_type(get_current_user_id()) == 'teacher' || bp_get_member_type(get_current_user_id()) == 'group_leader')) { get_template_part('template-parts/header-dashboard-links'); } ?> <!-- Adminbar --> <div id="adminbar-links" class="bp_components"> <?php buddyboss_adminbar_myaccount(); ?> </div> <?php wp_nav_menu(array('theme_location' => 'header-my-account', 'fallback_cb' => '', 'menu_class' => 'links')); ?>
/** * Return whether the user has one of the field's member types * * @since 1.0.0 * * @uses bp_displayed_user_id() * @uses BP_XProfile_Field_For_Member_Types::get_xprofile_member_types() * @uses bp_get_member_type() * * @param int|object $field_group_id Field ID or * @param int $user_id Optional. User ID. Defaults to the displayed user. * @return bool User has field's member type */ public function has_user_field_member_type($field_id, $user_id = 0) { // Get field ID if (is_object($field_id)) { $field_id = $field_id->id; } // The primary field is for all, so bail if (1 === (int) $field_id) { return true; } // Default to displayed user if (!is_numeric($user_id)) { $user_id = bp_displayed_user_id(); } // Get the field's member types if ($member_types = $this->get_xprofile_member_types($field_id, 'field')) { // Default to 'none' when the user has no member type(s) if (!($u_member_types = bp_get_member_type($user_id, false))) { $u_member_types = array('none'); } // Validate user by the field's member types $validate = array_intersect($member_types, $u_member_types); // Return whether we have any matches return !empty($validate); // No member types were assigned, so user validates } else { return true; } }